Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/strace/strace006.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/strace006.28* VM testbase keywords: [stress, strace]29* VM testbase readme:30* DESCRIPTION31* The test checks up java.lang.Thread.getAllStackTraces() method for many32* threads, that recursively invoke pure java and native methods by turns33* in running mode ("alive" stack).34* The test fails if:35* - amount of stack trace elements is more than depth of recursion plus36* four elements corresponding to invocations of Thread.run(), Thread.wait(),37* Thread.exit(), Thread.yield() and ThreadGroup.remove() methods;38* - there is at least one element corresponding to invocation of unexpected39* method.40* This test is almost the same as nsk.stress.strace.strace005 except for41* checking is performed for java.lang.Thread.getAllStackTraces() method.42* COMMENTS43* Similar assertion is thrown (see strace005.README).44*45* @library /vmTestbase46* /test/lib47* @run main/othervm/native nsk.stress.strace.strace00648*/4950package nsk.stress.strace;5152import nsk.share.ArgumentParser;53import nsk.share.Failure;54import nsk.share.Log;5556import java.io.PrintStream;57import java.util.Map;5859/**60* The test check up <code>java.lang.Thread.getStackTrace()</code> method for the pure61* java recursion.62* <p>The test creates <code>THRD_COUNT</code> instances of <code>strace006Thread</code>63* class, tries to get their stack traces and checks up that returned array contains64* correct stack frames. Each stack frame must be corresponded to one of the following65* methods defined by the <code>expectedMethod</code> array.</p>66* <p>These checking are performed <code>REPEAT_COUNT</code> times.</p>67*/68public class strace006 {6970static final int DEPTH = 500;71static final int THRD_COUNT = 100;72static final int REPEAT_COUNT = 10;73static final String[] EXPECTED_METHODS = {74"java.lang.System.arraycopy",75"java.lang.Object.wait",76"java.lang.Thread.exit",77"java.lang.Thread.yield",78"java.lang.ThreadGroup.remove",79"java.lang.ThreadGroup.threadTerminated",80"nsk.stress.strace.strace006Thread.run",81"nsk.stress.strace.strace006Thread.recursiveMethod1",82"nsk.stress.strace.strace006Thread.recursiveMethod2"83};848586static volatile boolean isLocked = false;87static PrintStream out;88static long waitTime = 2;8990static Object waitStart = new Object();9192static strace006Thread[] threads;93static StackTraceElement[][] snapshots = new StackTraceElement[THRD_COUNT][];94static Log log;9596volatile int achivedCount = 0;9798public static void main(String[] args) {99out = System.out;100int exitCode = run(args);101System.exit(exitCode + 95);102}103104public static int run(String[] args) {105ArgumentParser argHandler = new ArgumentParser(args);106log = new Log(out, argHandler);107waitTime = argHandler.getWaitTime() * 60000;108109strace006 test = new strace006();110boolean res = true;111112for (int j = 0; j < REPEAT_COUNT; j++) {113test.startThreads();114115if (!test.makeSnapshot(j + 1)) res = false;116117display("waiting for threads finished\n");118test.finishThreads();119}120121if (!res) {122complain("***>>>Test failed<<<***");123return 2;124}125126return 0;127}128129void startThreads() {130threads = new strace006Thread[THRD_COUNT];131achivedCount = 0;132133String tmp_name;134for (int i = 0; i < THRD_COUNT; i++) {135tmp_name = "strace006Thread" + Integer.toString(i);136threads[i] = new strace006Thread(this, tmp_name);137// threads[i].setPriority(Thread.MIN_PRIORITY);138}139140for (int i = 0; i < THRD_COUNT; i++) {141threads[i].start();142}143144waitFor("all threads started ...");145synchronized (waitStart) {146isLocked = true;147waitStart.notifyAll();148}149try {150Thread.yield();151Thread.sleep(1);152} catch (InterruptedException e) {153complain("" + e);154}155}156157void waitFor(String msg) {158if (msg.length() > 0)159display("waiting for " + msg);160161while (achivedCount < THRD_COUNT) {162try {163Thread.sleep(1);164} catch (InterruptedException e) {165complain("" + e);166}167}168achivedCount = 0;169}170171boolean makeSnapshot(int repeat_number) {172173// wait for native resolution completed (all threads have finished recursiveMethod2)174boolean isNativeResolved = false;175while (!isNativeResolved) {176try {177isNativeResolved = true;178for (int i = 0; i < threads.length; ++i)179if (!threads[i].isNativeResolved)180isNativeResolved = false;181Thread.sleep(20);182} catch (InterruptedException e) {183throw new Error(e);184}185}186187Map traces = Thread.getAllStackTraces();188for (int i = 0; i < threads.length; i++) {189snapshots[i] = (StackTraceElement[]) traces.get(threads[i]);190}191192return checkTraces(repeat_number);193}194195boolean checkTraces(int repeat_number) {196StackTraceElement[] elements;197198boolean res = true;199display(">>> snapshot " + repeat_number);200int expectedCount = DEPTH + 1;201202for (int i = 0; i < threads.length; i++) {203elements = snapshots[i];204205if (elements == null || elements.length == 0)206continue;207208if (elements.length > 0) {209display("\tchecking " + threads[i].getName()210+ "(trace elements: " + elements.length + ")");211}212213if (elements.length > expectedCount) {214complain(threads[i].getName() + ">Contains more then "215+ expectedCount + " elements");216}217218for (int j = 0; j < elements.length; j++) {219if (!checkElement(elements[j])) {220complain(threads[i].getName() + ">Unexpected method name: "221+ elements[j].getMethodName());222complain("\tat " + j + " position");223if (elements[j].isNativeMethod()) {224complain("\tline number: (native method)");225complain("\tclass name: " + elements[j].getClassName());226} else {227complain("\tline number: " + elements[j].getLineNumber());228complain("\tclass name: " + elements[j].getClassName());229complain("\tfile name: " + elements[j].getFileName());230}231res = false;232}233}234}235return res;236}237238boolean checkElement(StackTraceElement element) {239if (element.getClassName().equals("java.lang.ClassLoader"))240return true;241String name = element.getClassName() + "." + element.getMethodName();242for (int i = 0; i < EXPECTED_METHODS.length; i++) {243if (EXPECTED_METHODS[i].compareTo(name) == 0)244return true;245}246return false;247}248249void finishThreads() {250try {251for (int i = 0; i < threads.length; i++) {252if (threads[i].isAlive())253threads[i].join(waitTime / THRD_COUNT);254}255} catch (InterruptedException e) {256complain("" + e);257}258isLocked = false;259}260261static void display(String message) {262log.display(message);263}264265static void complain(String message) {266log.complain(message);267}268269}270271/**272* The test creates many instances of <code>strace006Thread</code> class and tries273* to get their stack traces.274*/275class strace006Thread extends Thread {276277private int currentDepth = 0;278public boolean isNativeResolved = false;279280strace006 test;281282static {283try {284System.loadLibrary("strace006");285} catch (UnsatisfiedLinkError e) {286System.err.println("Could not load strace006 library");287System.err.println("java.library.path:"288+ System.getProperty("java.library.path"));289throw e;290}291}292293strace006Thread(strace006 test, String name) {294this.test = test;295setName(name);296}297298public void run() {299300recursiveMethod1();301302}303304void recursiveMethod1() {305306currentDepth++;307308if (currentDepth == 1) {309synchronized (test) {310test.achivedCount++;311}312313int alltime = 0;314while (!strace006.isLocked) {315synchronized (test) {316try {317test.wait(1);318alltime++;319} catch (InterruptedException e) {320strace006.complain("" + e);321}322if (alltime > strace006.waitTime) {323throw new Failure("out of wait time");324}325}326}327} else if (currentDepth > 1 && !isNativeResolved)328isNativeResolved = true;329330if (strace006.DEPTH - currentDepth > 0) {331try {332Thread.yield();333recursiveMethod2();334} catch (StackOverflowError e) {335// ignore this exception336}337}338339currentDepth--;340}341342native void recursiveMethod2();343}344345346