Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/strace/strace001.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*/222324/*25* @test26* @key stress27*28* @summary converted from VM testbase nsk/stress/strace/strace001.29* VM testbase keywords: [stress, quick, strace]30* VM testbase readme:31* DESCRIPTION32* The test checks up java.lang.Thread.getStackTrace() method for many threads,33* that recursively invoke a pure java method 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*41* @library /vmTestbase42* /test/lib43* @run main/othervm nsk.stress.strace.strace00144*/45package nsk.stress.strace;4647import nsk.share.ArgumentParser;48import nsk.share.Failure;49import nsk.share.Log;5051import java.io.PrintStream;5253/**54* The test check up <code>java.lang.Thread.getStackTrace()</code> method for many threads,55* that recursively invoke a pure java method in running mode ("alive" stack).56* <p>57* <p>The test creates <code>THRD_COUNT</code> instances of <code>strace001Thread</code>58* class, tries to get their stack traces and checks up that returned array contains59* correct stack frames. Each stack frame must be corresponded to one of the following60* methods defined by the <code>EXPECTED_METHODS</code> array.</p>61* <p>These checking are performed <code>REPEAT_COUNT</code> times.</p>62*/63public class strace001 {6465static final int DEPTH = 200;66static final int THRD_COUNT = 100;67static final int REPEAT_COUNT = 10;68static final String[] EXPECTED_METHODS = {69"java.lang.System.arraycopy",70"java.lang.Object.wait",71"java.lang.Thread.exit",72"java.lang.Thread.yield",73"java.lang.ThreadGroup.remove",74"java.lang.ThreadGroup.threadTerminated",75"nsk.stress.strace.strace001Thread.run",76"nsk.stress.strace.strace001Thread.recursiveMethod"77};787980static volatile boolean isLocked = false;81static PrintStream out;82static long waitTime = 2;8384static Object waitStart = new Object();8586static strace001Thread[] threads;87static StackTraceElement[][] snapshots = new StackTraceElement[THRD_COUNT][];88static Log log;8990public static void main(String[] args) {91out = System.out;92int exitCode = run(args);93System.exit(exitCode + 95);94}9596volatile int achivedCount = 0;9798public static int run(String[] args) {99100ArgumentParser argHandler = new ArgumentParser(args);101log = new Log(out, argHandler);102waitTime = argHandler.getWaitTime() * 60000;103104strace001 test = new strace001();105boolean res = true;106107for (int j = 0; j < REPEAT_COUNT; j++) {108test.startThreads();109110if (!test.makeSnapshot(j + 1)) res = false;111112display("waiting for threads finished\n");113test.finishThreads();114}115116if (!res) {117complain("***>>>Test failed<<<***");118return 2;119}120121return 0;122}123124void startThreads() {125threads = new strace001Thread[THRD_COUNT];126achivedCount = 0;127128String tmp_name;129for (int i = 0; i < THRD_COUNT; i++) {130tmp_name = "strace001Thread" + Integer.toString(i);131threads[i] = new strace001Thread(this, tmp_name);132}133134for (int i = 0; i < THRD_COUNT; i++) {135threads[i].start();136}137138waitFor("all threads started ...");139synchronized (waitStart) {140isLocked = true;141waitStart.notifyAll();142}143try {144Thread.yield();145Thread.sleep(1);146} catch (InterruptedException e) {147complain("" + e);148}149}150151void waitFor(String msg) {152if (msg.length() > 0)153display("waiting for " + msg);154155while (achivedCount < THRD_COUNT) {156try {157Thread.sleep(1);158} catch (InterruptedException e) {159complain("" + e);160}161}162achivedCount = 0;163}164165boolean makeSnapshot(int repeat_number) {166167for (int i = 0; i < threads.length; i++) {168snapshots[i] = threads[i].getStackTrace();169}170171return checkTraces(repeat_number);172}173174boolean checkTraces(int repeat_number) {175StackTraceElement[] elements;176177boolean res = true;178display(">>> snapshot " + repeat_number);179int expectedCount = DEPTH + 1;180181for (int i = 0; i < threads.length; i++) {182elements = snapshots[i];183184if (elements == null)185continue;186187if (elements.length == 0)188continue;189190if (elements.length > 3) {191display("\tchecking " + threads[i].getName()192+ "(trace elements: " + elements.length + ")");193}194195if (elements.length > expectedCount) {196complain(threads[i].getName() + ">Contains more then " +197+expectedCount + " elements");198}199200for (int j = 0; j < elements.length; j++) {201if (!checkElement(elements[j])) {202complain(threads[i].getName() + ">Unexpected method name: "203+ elements[j].getMethodName());204complain("\tat " + j + " position");205if (elements[j].isNativeMethod()) {206complain("\tline number: (native method)");207complain("\tclass name: " + elements[j].getClassName());208} else {209complain("\tline number: " + elements[j].getLineNumber());210complain("\tclass name: " + elements[j].getClassName());211complain("\tfile name: " + elements[j].getFileName());212}213res = false;214}215}216}217return res;218}219220boolean checkElement(StackTraceElement element) {221String name = element.getClassName() + "." + element.getMethodName();222for (int i = 0; i < EXPECTED_METHODS.length; i++) {223if (EXPECTED_METHODS[i].compareTo(name) == 0)224return true;225}226return false;227}228229void finishThreads() {230try {231for (int i = 0; i < threads.length; i++) {232if (threads[i].isAlive())233threads[i].join(waitTime / THRD_COUNT);234}235} catch (InterruptedException e) {236complain("" + e);237}238isLocked = false;239}240241static void display(String message) {242log.display(message);243}244245static void complain(String message) {246log.complain(message);247}248}249250class strace001Thread extends Thread {251252private int currentDepth = 0;253254strace001 test;255256strace001Thread(strace001 test, String name) {257this.test = test;258setName(name);259}260261public void run() {262try {263recursiveMethod();264} catch (Throwable throwable) {265System.err.println("# ERROR: " + getName() + ": " + throwable);266System.exit(1);267}268}269270void recursiveMethod() {271272currentDepth++;273274if (currentDepth == 1) {275synchronized (test) {276test.achivedCount++;277}278279int alltime = 0;280while (!strace001.isLocked) {281synchronized (test) {282try {283test.wait(1);284alltime++;285} catch (InterruptedException e) {286strace001.complain("" + e);287}288if (alltime > strace001.waitTime) {289throw new Failure("out of wait time");290}291}292}293}294295if (strace001.DEPTH - currentDepth > 0) {296Thread.yield();297recursiveMethod();298}299300currentDepth--;301}302}303304305