1 /*
  2  * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @key stress
 27  *
 28  * @summary converted from VM testbase nsk/stress/strace/strace011.
 29  * VM testbase keywords: [stress, quick, strace]
 30  * VM testbase readme:
 31  * DESCRIPTION
 32  *     The test runs many threads, that recursively invoke a native method.
 33  *     After arriving at defined depth of recursion, each thread is blocked
 34  *     on entering a monitor. Then the test calls java.lang.Thread.getStackTrace()
 35  *     and java.lang.Thread.getAllStackTraces() methods and checks their results.
 36  *     The test fails if:
 37  *     - amount of stack trace elements and stack trace elements themselves are
 38  *       the same for both methods;
 39  *     - there is at least one element corresponding to invocation of unexpected
 40  *       method. Expected methods are Thread.sleep(), Thread.run() and the
 41  *       recursive method.
 42  *     This test is almost the same as nsk.stress.strace.010 except for
 43  *     the recursive method is a native one.
 44  *
 45  * @library /vmTestbase
 46  *          /test/lib
 47  * @run main/othervm/native nsk.stress.strace.strace011
 48  */
 49 
 50 package nsk.stress.strace;
 51 
 52 import nsk.share.ArgumentParser;
 53 import nsk.share.Log;
 54 
 55 import java.io.PrintStream;
 56 import java.util.Map;
 57 
 58 /**
 59  * The test runs <code>THRD_COUNT</code> instances of <code>strace011Thread</code>,
 60  * that recursively invoke a native method. After arriving at defined depth
 61  * <code>DEPTH</code> of recursion, each thread is blocked on entering a monitor.
 62  * Then the test calls <code>java.lang.Thread.getStackTrace()</code> and
 63  * <code>java.lang.Thread.getAllStackTraces()</code> methods and checks their results.
 64  * <p>
 65  * <p>It is expected that these methods return the same stack traces. Each stack frame
 66  * for both stack traces must be corresponded to invocation of one of the methods
 67  * defined by the <code>EXPECTED_METHODS</code> array.</p>
 68  */
 69 public class strace011 {
 70 
 71     static final int DEPTH = 100;
 72     static final int THRD_COUNT = 50;
 73     static final String[] EXPECTED_METHODS = {
 74             "java.lang.Thread.sleep",
 75             "java.lang.Thread.sleep0",
 76             "java.lang.Thread.getVirtualThread",
 77             "jdk.internal.event.ThreadSleepEvent.<clinit>",
 78             "jdk.internal.event.ThreadSleepEvent.isTurnedOn",
 79             "jdk.internal.event.ThreadSleepEvent.isEnabled",
 80             "java.lang.Thread.sleepMillis",
 81             "java.lang.Thread.currentCarrierThread",
 82             "java.lang.Thread.currentThread",
 83             "java.lang.Thread.currentThread0",
 84             "nsk.stress.strace.strace011Thread.run",
 85             "nsk.stress.strace.strace011Thread.recursiveMethod"
 86     };
 87 
 88 
 89     static PrintStream out;
 90     static long waitTime = 2;
 91 
 92     public static Object lockedObject = new Object();
 93     static volatile boolean isLocked = false;
 94 
 95     static volatile int achivedCount = 0;
 96     strace011Thread[] threads;
 97     static Log log;
 98 
 99     public static void main(String[] args) {
100         out = System.out;
101         int exitCode = run(args);
102         System.exit(exitCode + 95);
103     }
104 
105     public static int run(String[] args) {
106         ArgumentParser argHandler = new ArgumentParser(args);
107         log = new Log(out, argHandler);
108         waitTime = argHandler.getWaitTime() * 60000;
109 
110         strace011 test = new strace011();
111         boolean res = true;
112 
113         test.startThreads();
114 
115         res = test.makeSnapshot();
116 
117         test.finishThreads();
118 
119         if (!res) {
120             complain("***>>>Test failed<<<***");
121             return 2;
122         }
123 
124         display(">>>Test passed<<<");
125         return 0;
126     }
127 
128     void startThreads() {
129         threads = new strace011Thread[THRD_COUNT];
130         achivedCount = 0;
131 
132         String tmp_name;
133         display("starting threads...");
134         for (int i = 0; i < THRD_COUNT; i++) {
135             tmp_name = "strace011Thread" + Integer.toString(i);
136             threads[i] = new strace011Thread(this, tmp_name);
137             threads[i].start();
138         }
139 
140         waitFor("the defined recursion depth ...");
141     }
142 
143     void waitFor(String msg) {
144         if (msg.length() > 0)
145             display("waiting for " + msg);
146 
147         while (achivedCount < THRD_COUNT) {
148             try {
149                 Thread.sleep(1);
150             } catch (InterruptedException e) {
151                 complain("" + e);
152             }
153         }
154         achivedCount = 0;
155     }
156 
157     boolean makeSnapshot() {
158 
159         Map traces = null;
160         int count = 0;
161         StackTraceElement[][] elements = null;
162 
163         display("locking object...");
164         synchronized (strace011.lockedObject) {
165             isLocked = true;
166             synchronized (this) {
167                 notifyAll();
168             }
169             Thread.currentThread().yield();
170             waitFor("lockedObject");
171 
172             display("making all threads snapshots...");
173             traces = Thread.getAllStackTraces();
174             count = ((StackTraceElement[]) traces.get(threads[0])).length;
175 
176             display("making snapshots of each thread...");
177             elements = new StackTraceElement[THRD_COUNT][];
178             for (int i = 0; i < THRD_COUNT; i++) {
179                 elements[i] = threads[i].getStackTrace();
180             }
181         }
182         display("object unlocked");
183 
184         display("");
185 
186         display("checking lengths of stack traces...");
187         StackTraceElement[] all;
188         for (int i = 1; i < THRD_COUNT; i++) {
189             all = (StackTraceElement[]) traces.get(threads[i]);
190             int k = all.length;
191             if (count - k > 2) {
192                 complain("wrong lengths of stack traces:\n\t"
193                         + threads[0].getName() + ": " + count
194                         + "\t"
195                         + threads[i].getName() + ": " + k);
196                 return false;
197             }
198         }
199 
200         display("checking stack traces...");
201         boolean res = true;
202         for (int i = 0; i < THRD_COUNT; i++) {
203             all = (StackTraceElement[]) traces.get(threads[i]);
204             if (!checkTraces(threads[i].getName(), elements[i], all)) {
205                 res = false;
206             }
207         }
208         return res;
209     }
210 
211     boolean checkTraces(String threadName, StackTraceElement[] threadSnap,
212                         StackTraceElement[] allSnap) {
213 
214         int checkedLength = threadSnap.length < allSnap.length ?
215                 threadSnap.length : allSnap.length;
216         boolean res = true;
217 
218         for (int j = 0; j < checkedLength; j++) {
219             if (!checkElement(threadSnap[j])) {
220                 complain("Unexpected " + j + "-element:");
221                 complain("\tmethod name: " + threadSnap[j].getMethodName());
222                 complain("\tclass name: " + threadSnap[j].getClassName());
223                 if (threadSnap[j].isNativeMethod()) {
224                     complain("\tline number: (native method)");
225                 } else {
226                     complain("\tline number: " + threadSnap[j].getLineNumber());
227                     complain("\tfile name: " + threadSnap[j].getFileName());
228                 }
229                 complain("");
230                 res = false;
231             }
232         }
233         return res;
234     }
235 
236     boolean checkElement(StackTraceElement element) {
237         String name = element.getClassName() + "." + element.getMethodName();
238         for (int i = 0; i < EXPECTED_METHODS.length; i++) {
239             if (name.startsWith(EXPECTED_METHODS[i]))
240                 return true;
241         }
242         return false;
243     }
244 
245     void finishThreads() {
246         try {
247             for (int i = 0; i < threads.length; i++) {
248                 if (threads[i].isAlive()) {
249                     display("waiting for finish " + threads[i].getName());
250                     threads[i].join(waitTime);
251                 }
252             }
253         } catch (InterruptedException e) {
254             complain("" + e);
255         }
256         isLocked = false;
257     }
258 
259     static void display(String message) {
260         log.display(message);
261     }
262 
263     static void complain(String message) {
264         log.complain(message);
265     }
266 
267 }
268 
269 class strace011Thread extends Thread {
270 
271     private int currentDepth = 0;
272 
273     strace011 test;
274 
275     static {
276         try {
277             System.loadLibrary("strace011");
278         } catch (UnsatisfiedLinkError e) {
279             System.err.println("Could not load strace011 library");
280             System.err.println("java.library.path:"
281                     + System.getProperty("java.library.path"));
282             throw e;
283         }
284     }
285 
286     strace011Thread(strace011 test, String name) {
287         this.test = test;
288         setName(name);
289     }
290 
291     public void run() {
292 
293         recursiveMethod();
294 
295     }
296 
297     native void recursiveMethod();
298 }