Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/strace/strace002.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/strace002.29* VM testbase keywords: [stress, quick, strace]30* VM testbase readme:31* DESCRIPTION32* The test checks up java.lang.Thread.getAllStackTraces() method for many33* threads, that recursively invoke a pure java method in running mode34* ("alive" stack).35* The test fails if:36* - amount of stack trace elements is more than depth of recursion plus37* four elements corresponding to invocations of Thread.run(), Thread.wait(),38* Thread.exit(), Thread.yield() and ThreadGroup.remove() methods;39* - there is at least one element corresponding to invocation of unexpected40* method.41* This test is almost the same as nsk.stress.strace.strace001 except for42* checking is performed for java.lang.Thread.getAllStackTraces() method.43*44* @library /vmTestbase45* /test/lib46* @run main/othervm nsk.stress.strace.strace00247*/484950package 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.getAllStackTraces()</code> method for many61* threads, that recursively invoke a pure java method in running mode ("alive" stack).62* <p>63* <p>The test creates <code>THRD_COUNT</code> instances of <code>strace002Thread</code>64* class, tries to get their stack traces and checks up that returned array contains65* correct stack frames. Each stack frame must be corresponded to one of the following66* methods defined by the <code>EXPECTED_METHODS</code> array.</p>67* <p>These checking are performed <code>REPEAT_COUNT</code> times.</p>68*/69public class strace002 {7071static final int DEPTH = 200;72static final int THRD_COUNT = 100;73static final int REPEAT_COUNT = 10;74static final String[] EXPECTED_METHODS = {75"java.lang.System.arraycopy",76"java.lang.Object.wait",77"java.lang.Thread.exit",78"java.lang.Thread.yield",79"java.lang.ThreadGroup.remove",80"java.lang.ThreadGroup.threadTerminated",81"nsk.stress.strace.strace002Thread.run",82"nsk.stress.strace.strace002Thread.recursiveMethod"83};848586static volatile boolean isLocked = false;87static PrintStream out;88static long waitTime = 2;8990static Object waitStart = new Object();9192static strace002Thread[] threads;93static StackTraceElement[][] snapshots = new StackTraceElement[THRD_COUNT][];94static Log log;9596public static void main(String[] args) {97out = System.out;98int exitCode = run(args);99System.exit(exitCode + 95);100}101102volatile int achivedCount = 0;103104public static int run(String[] args) {105106ArgumentParser argHandler = new ArgumentParser(args);107log = new Log(out, argHandler);108waitTime = argHandler.getWaitTime() * 60000;109110strace002 test = new strace002();111boolean res = true;112113for (int j = 0; j < REPEAT_COUNT; j++) {114test.startThreads();115116if (!test.makeSnapshot(j + 1)) res = false;117118display("waiting for threads finished\n");119test.finishThreads();120}121122if (!res) {123complain("***>>>Test failed<<<***");124return 2;125}126127return 0;128}129130void startThreads() {131threads = new strace002Thread[THRD_COUNT];132achivedCount = 0;133134String tmp_name;135for (int i = 0; i < THRD_COUNT; i++) {136tmp_name = "strace002Thread" + Integer.toString(i);137threads[i] = new strace002Thread(this, tmp_name);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) {172173Map traces = Thread.getAllStackTraces();174for (int i = 0; i < threads.length; i++) {175snapshots[i] = (StackTraceElement[]) traces.get(threads[i]);176}177178return checkTraces(repeat_number);179}180181boolean checkTraces(int repeat_number) {182StackTraceElement[] elements;183184boolean res = true;185display(">>> snapshot " + repeat_number);186int expectedCount = DEPTH + 1;187188for (int i = 0; i < threads.length; i++) {189elements = snapshots[i];190191if (elements == null)192continue;193194if (elements.length == 0)195continue;196197if (elements.length > 3) {198display("\tchecking " + threads[i].getName()199+ "(trace elements: " + elements.length + ")");200}201202if (elements.length > expectedCount) {203complain(threads[i].getName() + ">Contains more then " +204+expectedCount + " elements");205}206207for (int j = 0; j < elements.length; j++) {208if (!checkElement(elements[j])) {209complain(threads[i].getName() + ">Unexpected method name: "210+ elements[j].getMethodName());211complain("\tat " + j + " position");212if (elements[j].isNativeMethod()) {213complain("\tline number: (native method)");214complain("\tclass name: " + elements[j].getClassName());215} else {216complain("\tline number: " + elements[j].getLineNumber());217complain("\tclass name: " + elements[j].getClassName());218complain("\tfile name: " + elements[j].getFileName());219}220res = false;221}222}223}224return res;225}226227boolean checkElement(StackTraceElement element) {228String name = element.getClassName() + "." + element.getMethodName();229for (int i = 0; i < EXPECTED_METHODS.length; i++) {230if (EXPECTED_METHODS[i].compareTo(name) == 0)231return true;232}233return false;234}235236void finishThreads() {237try {238for (int i = 0; i < threads.length; i++) {239if (threads[i].isAlive())240threads[i].join(waitTime / THRD_COUNT);241}242} catch (InterruptedException e) {243complain("" + e);244}245isLocked = false;246}247248static void display(String message) {249log.display(message);250}251252static void complain(String message) {253log.complain(message);254}255256257}258259class strace002Thread extends Thread {260261private int currentDepth = 0;262263strace002 test;264265strace002Thread(strace002 test, String name) {266this.test = test;267setName(name);268}269270public void run() {271try {272recursiveMethod();273} catch (Throwable throwable) {274System.err.println("# ERROR: " + getName() + ": " + throwable);275System.exit(1);276}277}278279void recursiveMethod() {280281currentDepth++;282283if (currentDepth == 1) {284synchronized (test) {285test.achivedCount++;286}287288int alltime = 0;289while (!test.isLocked) {290synchronized (test) {291try {292test.wait(1);293alltime++;294} catch (InterruptedException e) {295strace002.complain("" + e);296}297if (alltime > strace002.waitTime) {298throw new Failure("out of wait time");299}300}301}302}303304if (strace002.DEPTH - currentDepth > 0) {305Thread.yield();306recursiveMethod();307}308309currentDepth--;310}311}312313314