Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/strace/strace011.java
41155 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key stress26*27* @summary converted from VM testbase nsk/stress/strace/strace011.28* VM testbase keywords: [stress, quick, strace]29* VM testbase readme:30* DESCRIPTION31* The test runs many threads, that recursively invoke a native method.32* After arriving at defined depth of recursion, each thread is blocked33* on entering a monitor. Then the test calls java.lang.Thread.getStackTrace()34* and java.lang.Thread.getAllStackTraces() methods and checks their results.35* The test fails if:36* - amount of stack trace elements and stack trace elements themselves are37* the same for both methods;38* - there is at least one element corresponding to invocation of unexpected39* method. Expected methods are Thread.sleep(), Thread.run() and the40* recursive method.41* This test is almost the same as nsk.stress.strace.010 except for42* the recursive method is a native one.43*44* @library /vmTestbase45* /test/lib46* @run main/othervm/native nsk.stress.strace.strace01147*/4849package nsk.stress.strace;5051import nsk.share.ArgumentParser;52import nsk.share.Log;5354import java.io.PrintStream;55import java.util.Map;5657/**58* The test runs <code>THRD_COUNT</code> instances of <code>strace011Thread</code>,59* that recursively invoke a native method. After arriving at defined depth60* <code>DEPTH</code> of recursion, each thread is blocked on entering a monitor.61* Then the test calls <code>java.lang.Thread.getStackTrace()</code> and62* <code>java.lang.Thread.getAllStackTraces()</code> methods and checks their results.63* <p>64* <p>It is expected that these methods return the same stack traces. Each stack frame65* for both stack traces must be corresponded to invocation of one of the methods66* defined by the <code>EXPECTED_METHODS</code> array.</p>67*/68public class strace011 {6970static final int DEPTH = 100;71static final int THRD_COUNT = 50;72static final String[] EXPECTED_METHODS = {73"java.lang.Thread.sleep",74"nsk.stress.strace.strace011Thread.run",75"nsk.stress.strace.strace011Thread.recursiveMethod"76};777879static PrintStream out;80static long waitTime = 2;8182public static Object lockedObject = new Object();83static volatile boolean isLocked = false;8485static volatile int achivedCount = 0;86strace011Thread[] threads;87static Log log;8889public static void main(String[] args) {90out = System.out;91int exitCode = run(args);92System.exit(exitCode + 95);93}9495public static int run(String[] args) {96ArgumentParser argHandler = new ArgumentParser(args);97log = new Log(out, argHandler);98waitTime = argHandler.getWaitTime() * 60000;99100strace011 test = new strace011();101boolean res = true;102103test.startThreads();104105res = test.makeSnapshot();106107test.finishThreads();108109if (!res) {110complain("***>>>Test failed<<<***");111return 2;112}113114display(">>>Test passed<<<");115return 0;116}117118void startThreads() {119threads = new strace011Thread[THRD_COUNT];120achivedCount = 0;121122String tmp_name;123display("starting threads...");124for (int i = 0; i < THRD_COUNT; i++) {125tmp_name = "strace011Thread" + Integer.toString(i);126threads[i] = new strace011Thread(this, tmp_name);127threads[i].start();128}129130waitFor("the defined recursion depth ...");131}132133void waitFor(String msg) {134if (msg.length() > 0)135display("waiting for " + msg);136137while (achivedCount < THRD_COUNT) {138try {139Thread.sleep(1);140} catch (InterruptedException e) {141complain("" + e);142}143}144achivedCount = 0;145}146147boolean makeSnapshot() {148149Map traces = null;150int count = 0;151StackTraceElement[][] elements = null;152153display("locking object...");154synchronized (strace011.lockedObject) {155isLocked = true;156synchronized (this) {157notifyAll();158}159Thread.currentThread().yield();160waitFor("lockedObject");161162display("making all threads snapshots...");163traces = Thread.getAllStackTraces();164count = ((StackTraceElement[]) traces.get(threads[0])).length;165166display("making snapshots of each thread...");167elements = new StackTraceElement[THRD_COUNT][];168for (int i = 0; i < THRD_COUNT; i++) {169elements[i] = threads[i].getStackTrace();170}171}172display("object unlocked");173174display("");175176display("checking lengths of stack traces...");177StackTraceElement[] all;178for (int i = 1; i < THRD_COUNT; i++) {179all = (StackTraceElement[]) traces.get(threads[i]);180int k = all.length;181if (count - k > 2) {182complain("wrong lengths of stack traces:\n\t"183+ threads[0].getName() + ": " + count184+ "\t"185+ threads[i].getName() + ": " + k);186return false;187}188}189190display("checking stack traces...");191boolean res = true;192for (int i = 0; i < THRD_COUNT; i++) {193all = (StackTraceElement[]) traces.get(threads[i]);194if (!checkTraces(threads[i].getName(), elements[i], all)) {195res = false;196}197}198return res;199}200201boolean checkTraces(String threadName, StackTraceElement[] threadSnap,202StackTraceElement[] allSnap) {203204int checkedLength = threadSnap.length < allSnap.length ?205threadSnap.length : allSnap.length;206boolean res = true;207208for (int j = 0; j < checkedLength; j++) {209if (!checkElement(threadSnap[j])) {210complain("Unexpected " + j + "-element:");211complain("\tmethod name: " + threadSnap[j].getMethodName());212complain("\tclass name: " + threadSnap[j].getClassName());213if (threadSnap[j].isNativeMethod()) {214complain("\tline number: (native method)");215} else {216complain("\tline number: " + threadSnap[j].getLineNumber());217complain("\tfile name: " + threadSnap[j].getFileName());218}219complain("");220res = false;221}222}223return res;224}225226boolean checkElement(StackTraceElement element) {227String name = element.getClassName() + "." + element.getMethodName();228for (int i = 0; i < EXPECTED_METHODS.length; i++) {229if (EXPECTED_METHODS[i].compareTo(name) == 0)230return true;231}232return false;233}234235void finishThreads() {236try {237for (int i = 0; i < threads.length; i++) {238if (threads[i].isAlive()) {239display("waiting for finish " + threads[i].getName());240threads[i].join(waitTime);241}242}243} catch (InterruptedException e) {244complain("" + e);245}246isLocked = false;247}248249static void display(String message) {250log.display(message);251}252253static void complain(String message) {254log.complain(message);255}256257}258259class strace011Thread extends Thread {260261private int currentDepth = 0;262263strace011 test;264265static {266try {267System.loadLibrary("strace011");268} catch (UnsatisfiedLinkError e) {269System.err.println("Could not load strace011 library");270System.err.println("java.library.path:"271+ System.getProperty("java.library.path"));272throw e;273}274}275276strace011Thread(strace011 test, String name) {277this.test = test;278setName(name);279}280281public void run() {282283recursiveMethod();284285}286287native void recursiveMethod();288}289290291