Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/strace/strace012.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/strace012.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 blocked on entering 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.strace010 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.strace01249*/5051package nsk.stress.strace;5253import nsk.share.ArgumentParser;54import nsk.share.Failure;55import nsk.share.Log;5657import java.io.PrintStream;58import java.util.Map;5960/**61* The test runs <code>THRD_COUNT</code> instances of <code>strace012Thread</code>,62* that recursively invoke pure java and native method by turns. After arriving at63* defined depth <code>DEPTH</code> of recursion, each thread is blocked on entering64* a monitor. Then the test calls <code>java.lang.Thread.getStackTrace()</code> and65* <code>java.lang.Thread.getAllStackTraces()</code> methods and checks their results.66* <p>67* <p>It is expected that these methods return the same stack traces. Each stack frame68* for both stack traces must be corresponded to invocation of one of the methods69* defined by the <code>EXPECTED_METHODS</code> array.</p>70*/71public class strace012 {7273static final int DEPTH = 100;74static final int THRD_COUNT = 100;75static final String[] EXPECTED_METHODS = {76"java.lang.Thread.sleep",77"nsk.stress.strace.strace012Thread.run",78"nsk.stress.strace.strace012Thread.recursiveMethod1",79"nsk.stress.strace.strace012Thread.recursiveMethod2"80};818283static PrintStream out;84static long waitTime = 2;8586public static Object lockedObject = new Object();87static volatile boolean isLocked = false;8889static volatile int achivedCount = 0;90strace012Thread[] threads;91static Log log;9293public static void main(String[] args) {94out = System.out;95int exitCode = run(args);96System.exit(exitCode + 95);97}9899public static int run(String[] args) {100ArgumentParser argHandler = new ArgumentParser(args);101log = new Log(out, argHandler);102waitTime = argHandler.getWaitTime() * 60000;103104strace012 test = new strace012();105boolean res = true;106107test.startThreads();108109res = test.makeSnapshot();110111test.finishThreads();112113if (!res) {114complain("***>>>Test failed<<<***");115return 2;116}117118display(">>>Test passed<<<");119return 0;120}121122void startThreads() {123threads = new strace012Thread[THRD_COUNT];124achivedCount = 0;125126String tmp_name;127display("starting threads...");128for (int i = 0; i < THRD_COUNT; i++) {129tmp_name = "strace012Thread" + Integer.toString(i);130threads[i] = new strace012Thread(this, tmp_name);131threads[i].start();132}133134waitFor("the defined recursion depth ...");135}136137void waitFor(String msg) {138if (msg.length() > 0)139display("waiting for " + msg);140141while (achivedCount < THRD_COUNT) {142try {143Thread.sleep(1);144} catch (InterruptedException e) {145complain("" + e);146}147}148achivedCount = 0;149}150151boolean makeSnapshot() {152153Map traces = null;154int count = 0;155StackTraceElement[][] elements = null;156157display("locking object...");158synchronized (strace012.lockedObject) {159isLocked = true;160synchronized (this) {161notifyAll();162}163Thread.currentThread().yield();164waitFor("");165166display("making all threads snapshots...");167traces = Thread.getAllStackTraces();168count = ((StackTraceElement[]) traces.get(threads[0])).length;169170display("making snapshots of each thread...");171elements = new StackTraceElement[THRD_COUNT][];172for (int i = 0; i < THRD_COUNT; i++) {173elements[i] = threads[i].getStackTrace();174}175}176display("object unlocked");177178display("");179180display("checking lengths of stack traces...");181StackTraceElement[] all;182for (int i = 1; i < THRD_COUNT; i++) {183all = (StackTraceElement[]) traces.get(threads[i]);184int k = all.length;185if (count - k > 2) {186complain("wrong lengths of stack traces:\n\t"187+ threads[0].getName() + ": " + count188+ "\t"189+ threads[i].getName() + ": " + k);190return false;191}192}193194display("checking stack traces...");195boolean res = true;196for (int i = 0; i < THRD_COUNT; i++) {197all = (StackTraceElement[]) traces.get(threads[i]);198if (!checkTraces(threads[i].getName(), elements[i], all)) {199res = false;200}201}202return res;203}204205boolean checkTraces(String threadName, StackTraceElement[] threadSnap,206StackTraceElement[] allSnap) {207208int checkedLength = threadSnap.length < allSnap.length ?209threadSnap.length : allSnap.length;210boolean res = true;211212for (int j = 0; j < checkedLength; j++) {213if (!checkElement(threadSnap[j])) {214complain("Unexpected " + j + "-element:");215complain("\tmethod name: " + threadSnap[j].getMethodName());216complain("\tclass name: " + threadSnap[j].getClassName());217if (threadSnap[j].isNativeMethod()) {218complain("\tline number: (native method)");219} else {220complain("\tline number: " + threadSnap[j].getLineNumber());221complain("\tfile name: " + threadSnap[j].getFileName());222}223complain("");224res = false;225}226}227return res;228}229230boolean checkElement(StackTraceElement element) {231String name = element.getClassName() + "." + element.getMethodName();232for (int i = 0; i < EXPECTED_METHODS.length; i++) {233if (EXPECTED_METHODS[i].compareTo(name) == 0)234return true;235}236return false;237}238239void finishThreads() {240try {241for (int i = 0; i < threads.length; i++) {242if (threads[i].isAlive()) {243display("waiting for finish " + threads[i].getName());244threads[i].join(waitTime);245}246}247} catch (InterruptedException e) {248complain("" + e);249}250isLocked = false;251}252253static void display(String message) {254log.display(message);255}256257static void complain(String message) {258log.complain(message);259}260261}262263class strace012Thread extends Thread {264265private int currentDepth = 0;266267strace012 test;268269static {270try {271System.loadLibrary("strace012");272} catch (UnsatisfiedLinkError e) {273System.err.println("Could not load strace012 library");274System.err.println("java.library.path:"275+ System.getProperty("java.library.path"));276throw e;277}278}279280strace012Thread(strace012 test, String name) {281this.test = test;282setName(name);283}284285public void run() {286287recursiveMethod1();288289}290291void recursiveMethod1() {292currentDepth++;293294if (strace012.DEPTH - currentDepth > 0) {295recursiveMethod2();296}297298if (strace012.DEPTH == currentDepth) {299300synchronized (test) {301strace012.achivedCount++;302}303304int alltime = 0;305while (!test.isLocked) {306synchronized (test) {307try {308test.wait(1);309alltime++;310} catch (InterruptedException e) {311strace012.complain("" + e);312}313if (alltime > strace012.waitTime) {314throw new Failure("out of wait time");315}316}317}318319strace012.display(getName() + ">entering to monitor");320synchronized (test) {321strace012.achivedCount++;322}323synchronized (strace012.lockedObject) {324strace012.display(getName() + ">exiting from monitor");325}326}327328currentDepth--;329}330331native void recursiveMethod2();332}333334335