Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/strace/strace015.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/strace015.28* VM testbase keywords: [stress, strace, quarantine]29* VM testbase comments: 819958130* VM testbase readme:31* DESCRIPTION32* The test runs many threads, that recursively invoke pure java and native33* method by turns. After arriving at defined depth of recursion, each thread34* is switched to waits for a monitor. Then the test calls35* java.lang.Thread.getStackTrace() and java.lang.Thread.getAllStackTraces()36* methods and checks their results.37* The test fails if:38* - amount of stack trace elements and stack trace elements themselves are39* the same for both methods;40* - there is at least one element corresponding to invocation of unexpected41* method. Expected methods are Thread.sleep(), Thread.run() and the42* recursive method.43* This test is almost the same as nsk.stress.strace.strace013 except for44* recursion is presented by pure java and native method invocation.45*46* @library /vmTestbase47* /test/lib48* @run main/othervm/native nsk.stress.strace.strace01549*/5051package nsk.stress.strace;5253import nsk.share.ArgumentParser;54import nsk.share.Log;5556import java.io.PrintStream;57import java.util.Map;5859/**60* The test runs <code>THRD_COUNT</code> instances of <code>strace010Thread</code>,61* that recursively invoke pure java and native method by turns. After arriving at62* defined depth <code>DEPTH</code> of recursion, each thread is switched to wait63* a monitor. Then the test calls <code>java.lang.Thread.getStackTrace()</code> and64* <code>java.lang.Thread.getAllStackTraces()</code> methods and checks their results.65* <p>66* <p>It is expected that these methods return the same stack traces. Each stack frame67* for both stack traces must be corresponded to invocation of one of the methods68* defined by the <code>EXPECTED_METHODS</code> array.</p>69*/70public class strace015 {7172static final int DEPTH = 100;73static final int THRD_COUNT = 100;74static final String[] EXPECTED_METHODS = {75"java.lang.Object.wait",76"nsk.stress.strace.strace015Thread.run",77"nsk.stress.strace.strace015Thread.recursiveMethod1",78"nsk.stress.strace.strace015Thread.recursiveMethod2"79};808182static PrintStream out;83static long waitTime = 2;8485static Object lockedObject = new Object();8687static volatile int achivedCount = 0;88strace015Thread[] threads;89static Log log;9091public static void main(String[] args) {92out = System.out;93int exitCode = run(args);94System.exit(exitCode + 95);95}9697public static int run(String[] args) {98ArgumentParser argHandler = new ArgumentParser(args);99log = new Log(out, argHandler);100waitTime = argHandler.getWaitTime() * 60000;101102strace015 test = new strace015();103boolean res = true;104105test.startThreads();106107res = test.makeSnapshot();108109test.finishThreads();110111if (!res) {112complain("***>>>Test failed<<<***");113return 2;114}115116display(">>>Test passed<<<");117return 0;118}119120void startThreads() {121threads = new strace015Thread[THRD_COUNT];122achivedCount = 0;123124String tmp_name;125display("starting threads...");126for (int i = 0; i < THRD_COUNT; i++) {127tmp_name = "strace015Thread" + Integer.toString(i);128threads[i] = new strace015Thread(this, tmp_name);129threads[i].start();130}131132waitFor("the defined recursion depth ...");133}134135void waitFor(String msg) {136if (msg.length() > 0)137display("waiting for " + msg);138139while (achivedCount < THRD_COUNT) {140try {141Thread.sleep(1);142} catch (InterruptedException e) {143complain("" + e);144}145}146achivedCount = 0;147}148149boolean makeSnapshot() {150151Map traces = null;152int count = 0;153StackTraceElement[][] elements = null;154155display("making all threads snapshots...");156traces = Thread.getAllStackTraces();157count = ((StackTraceElement[]) traces.get(threads[0])).length;158159display("making snapshots of each thread...");160elements = new StackTraceElement[THRD_COUNT][];161for (int i = 0; i < THRD_COUNT; i++) {162elements[i] = threads[i].getStackTrace();163}164165display("notifying");166synchronized (strace015.lockedObject) {167strace015.lockedObject.notifyAll();168}169170display("");171172display("checking lengths of stack traces...");173StackTraceElement[] all;174for (int i = 1; i < THRD_COUNT; i++) {175all = (StackTraceElement[]) traces.get(threads[i]);176int k = all.length;177if (count - k > 2) {178complain("wrong lengths of stack traces:\n\t"179+ threads[0].getName() + ": " + count180+ "\t"181+ threads[i].getName() + ": " + k);182return false;183}184}185186display("checking stack traces...");187boolean res = true;188for (int i = 0; i < THRD_COUNT; i++) {189all = (StackTraceElement[]) traces.get(threads[i]);190if (!checkTraces(threads[i].getName(), elements[i], all)) {191res = false;192}193}194return res;195}196197boolean checkTraces(String threadName, StackTraceElement[] threadSnap,198StackTraceElement[] allSnap) {199200int checkedLength = threadSnap.length < allSnap.length ?201threadSnap.length : allSnap.length;202boolean res = true;203204for (int j = checkedLength; j < 0; j--) {205if (!checkElement(threadSnap[j])) {206complain("Unexpected " + j + "-element:");207complain("\tmethod name: " + threadSnap[j].getMethodName());208complain("\tclass name: " + threadSnap[j].getClassName());209if (threadSnap[j].isNativeMethod()) {210complain("\tline number: (native method)");211} else {212complain("\tline number: " + threadSnap[j].getLineNumber());213complain("\tfile name: " + threadSnap[j].getFileName());214}215complain("");216res = false;217}218}219return res;220}221222boolean checkElement(StackTraceElement element) {223String name = element.getClassName() + "." + element.getMethodName();224for (int i = 0; i < EXPECTED_METHODS.length; i++) {225if (EXPECTED_METHODS[i].compareTo(name) == 0)226return true;227}228return false;229}230231void finishThreads() {232try {233for (int i = 0; i < threads.length; i++) {234if (threads[i].isAlive()) {235display("waiting for finish " + threads[i].getName());236threads[i].join(waitTime);237}238}239} catch (InterruptedException e) {240complain("" + e);241}242}243244static void display(String message) {245log.display(message);246}247248static void complain(String message) {249log.complain(message);250}251252}253254/**255* The test creates many instances of <code>strace015Thread</code> class and tries256* to get their stack traces.257*/258class strace015Thread extends Thread {259260private int currentDepth = 0;261262strace015 test;263264static {265try {266System.loadLibrary("strace015");267} catch (UnsatisfiedLinkError e) {268System.err.println("Could not load strace015 library");269System.err.println("java.library.path:"270+ System.getProperty("java.library.path"));271throw e;272}273}274275strace015Thread(strace015 test, String name) {276this.test = test;277setName(name);278}279280public void run() {281282recursiveMethod1();283284}285286void recursiveMethod1() {287currentDepth++;288289if (strace015.DEPTH - currentDepth > 0) {290recursiveMethod2();291}292293if (strace015.DEPTH == currentDepth) {294295strace015.display(getName() + ">waiting on a monitor");296297synchronized (test) {298test.achivedCount++;299}300301synchronized (strace015.lockedObject) {302try {303strace015.lockedObject.wait();304} catch (InterruptedException e) {305strace015.complain("" + e);306}307}308strace015.display(getName() + ">notified");309}310311currentDepth--;312}313314native void recursiveMethod2();315}316317318