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/strace006.
 29  * VM testbase keywords: [stress, strace]
 30  * VM testbase readme:
 31  * DESCRIPTION
 32  *     The test checks up java.lang.Thread.getAllStackTraces() method for many
 33  *     threads, that recursively invoke pure java and native methods by turns
 34  *     in running mode ("alive" stack).
 35  *     The test fails if:
 36  *     - amount of stack trace elements is more than depth of recursion plus
 37  *       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 unexpected
 40  *       method.
 41  *     This test is almost the same as nsk.stress.strace.strace005 except for
 42  *     checking is performed for java.lang.Thread.getAllStackTraces() method.
 43  * COMMENTS
 44  *     Similar assertion is thrown (see strace005.README).
 45  *
 46  * @library /vmTestbase
 47  *          /test/lib
 48  * @run main/othervm/native nsk.stress.strace.strace006
 49  */
 50 
 51 package nsk.stress.strace;
 52 
 53 import nsk.share.ArgumentParser;
 54 import nsk.share.Failure;
 55 import nsk.share.Log;
 56 
 57 import java.io.PrintStream;
 58 import java.util.Map;
 59 
 60 /**
 61  * The test check up <code>java.lang.Thread.getStackTrace()</code> method for the pure
 62  * java recursion.
 63  * <p>The test creates <code>THRD_COUNT</code> instances of <code>strace006Thread</code>
 64  * class, tries to get their stack traces and checks up that returned array contains
 65  * correct stack frames. Each stack frame must be corresponded to one of the following
 66  * methods defined by the <code>expectedMethod</code> array.</p>
 67  * <p>These checking are performed <code>REPEAT_COUNT</code> times.</p>
 68  */
 69 public class strace006 {
 70 
 71     static final int DEPTH = 500;
 72     static final int THRD_COUNT = 100;
 73     static final int REPEAT_COUNT = 10;
 74     static final String[] EXPECTED_METHODS = {
 75             "java.lang.System.arraycopy",
 76             "java.lang.Object.wait",
 77             "java.lang.Object.wait0",
 78             "java.lang.System$2.headStackableScope",
 79             "java.lang.Thread.headStackableScopes",
 80             "java.lang.Thread.exit",
 81             "java.lang.Thread.yield",
 82             "java.lang.Thread.yield0",
 83             "java.lang.Thread.clearReferences",
 84             "java.lang.Thread.getVirtualThread",
 85             "java.lang.Thread.currentCarrierThread",
 86             "java.lang.Thread.currentThread",
 87             "java.lang.Thread.currentThread0",
 88             "java.lang.Thread.threadContainer",
 89             "java.lang.ThreadGroup.remove",
 90             "java.lang.ThreadGroup.threadTerminated",
 91             "jdk.internal.vm.StackableScope.head",
 92             "jdk.internal.vm.StackableScope.popAll",
 93             "nsk.stress.strace.strace006Thread.run",
 94             "nsk.stress.strace.strace006Thread.recursiveMethod1",
 95             "nsk.stress.strace.strace006Thread.recursiveMethod2"
 96     };
 97 
 98 
 99     static volatile boolean isLocked = false;
100     static PrintStream out;
101     static long waitTime = 2;
102 
103     static Object waitStart = new Object();
104 
105     static strace006Thread[] threads;
106     static StackTraceElement[][] snapshots = new StackTraceElement[THRD_COUNT][];
107     static Log log;
108 
109     volatile int achivedCount = 0;
110 
111     public static void main(String[] args) {
112         out = System.out;
113         int exitCode = run(args);
114         System.exit(exitCode + 95);
115     }
116 
117     public static int run(String[] args) {
118         ArgumentParser argHandler = new ArgumentParser(args);
119         log = new Log(out, argHandler);
120         waitTime = argHandler.getWaitTime() * 60000;
121 
122         strace006 test = new strace006();
123         boolean res = true;
124 
125         for (int j = 0; j < REPEAT_COUNT; j++) {
126             test.startThreads();
127 
128             if (!test.makeSnapshot(j + 1)) res = false;
129 
130             display("waiting for threads finished\n");
131             test.finishThreads();
132         }
133 
134         if (!res) {
135             complain("***>>>Test failed<<<***");
136             return 2;
137         }
138 
139         return 0;
140     }
141 
142     void startThreads() {
143         threads = new strace006Thread[THRD_COUNT];
144         achivedCount = 0;
145 
146         String tmp_name;
147         for (int i = 0; i < THRD_COUNT; i++) {
148             tmp_name = "strace006Thread" + Integer.toString(i);
149             threads[i] = new strace006Thread(this, tmp_name);
150 //            threads[i].setPriority(Thread.MIN_PRIORITY);
151         }
152 
153         for (int i = 0; i < THRD_COUNT; i++) {
154             threads[i].start();
155         }
156 
157         waitFor("all threads started ...");
158         synchronized (waitStart) {
159             isLocked = true;
160             waitStart.notifyAll();
161         }
162         try {
163             Thread.yield();
164             Thread.sleep(1);
165         } catch (InterruptedException e) {
166             complain("" + e);
167         }
168     }
169 
170     void waitFor(String msg) {
171         if (msg.length() > 0)
172             display("waiting for " + msg);
173 
174         while (achivedCount < THRD_COUNT) {
175             try {
176                 Thread.sleep(1);
177             } catch (InterruptedException e) {
178                 complain("" + e);
179             }
180         }
181         achivedCount = 0;
182     }
183 
184     boolean makeSnapshot(int repeat_number) {
185 
186         // wait for native resolution completed (all threads have finished recursiveMethod2)
187         boolean isNativeResolved = false;
188         while (!isNativeResolved) {
189             try {
190                 isNativeResolved = true;
191                 for (int i = 0; i < threads.length; ++i)
192                     if (!threads[i].isNativeResolved)
193                         isNativeResolved = false;
194                 Thread.sleep(20);
195             } catch (InterruptedException e) {
196                 throw new Error(e);
197             }
198         }
199 
200         Map traces = Thread.getAllStackTraces();
201         for (int i = 0; i < threads.length; i++) {
202             snapshots[i] = (StackTraceElement[]) traces.get(threads[i]);
203         }
204 
205         return checkTraces(repeat_number);
206     }
207 
208     boolean checkTraces(int repeat_number) {
209         StackTraceElement[] elements;
210 
211         boolean res = true;
212         display(">>> snapshot " + repeat_number);
213         int expectedCount = DEPTH + 1;
214 
215         for (int i = 0; i < threads.length; i++) {
216             elements = snapshots[i];
217 
218             if (elements == null || elements.length == 0)
219                 continue;
220 
221             if (elements.length > 0) {
222                 display("\tchecking " + threads[i].getName()
223                         + "(trace elements: " + elements.length + ")");
224             }
225 
226             if (elements.length > expectedCount) {
227                 complain(threads[i].getName() + ">Contains more then "
228                         + expectedCount + " elements");
229             }
230 
231             for (int j = 0; j < elements.length; j++) {
232                 if (!checkElement(elements[j])) {
233                     complain(threads[i].getName() + ">Unexpected method name: "
234                             + elements[j].getMethodName());
235                     complain("\tat " + j + " position");
236                     if (elements[j].isNativeMethod()) {
237                         complain("\tline number: (native method)");
238                         complain("\tclass name: " + elements[j].getClassName());
239                     } else {
240                         complain("\tline number: " + elements[j].getLineNumber());
241                         complain("\tclass name: " + elements[j].getClassName());
242                         complain("\tfile name: " + elements[j].getFileName());
243                     }
244                     res = false;
245                 }
246             }
247         }
248         return res;
249     }
250 
251     boolean checkElement(StackTraceElement element) {
252         if (element.getClassName().equals("java.lang.ClassLoader"))
253             return true;
254         String name = element.getClassName() + "." + element.getMethodName();
255         for (int i = 0; i < EXPECTED_METHODS.length; i++) {
256             if (name.startsWith(EXPECTED_METHODS[i]))
257                 return true;
258         }
259         return false;
260     }
261 
262     void finishThreads() {
263         try {
264             for (int i = 0; i < threads.length; i++) {
265                 if (threads[i].isAlive())
266                     threads[i].join(waitTime / THRD_COUNT);
267             }
268         } catch (InterruptedException e) {
269             complain("" + e);
270         }
271         isLocked = false;
272     }
273 
274     static void display(String message) {
275         log.display(message);
276     }
277 
278     static void complain(String message) {
279         log.complain(message);
280     }
281 
282 }
283 
284 /**
285  * The test creates many instances of <code>strace006Thread</code> class and tries
286  * to get their stack traces.
287  */
288 class strace006Thread extends Thread {
289 
290     private int currentDepth = 0;
291     public boolean isNativeResolved = false;
292 
293     strace006 test;
294 
295     static {
296         try {
297             System.loadLibrary("strace006");
298         } catch (UnsatisfiedLinkError e) {
299             System.err.println("Could not load strace006 library");
300             System.err.println("java.library.path:"
301                     + System.getProperty("java.library.path"));
302             throw e;
303         }
304     }
305 
306     strace006Thread(strace006 test, String name) {
307         this.test = test;
308         setName(name);
309     }
310 
311     public void run() {
312 
313         recursiveMethod1();
314 
315     }
316 
317     void recursiveMethod1() {
318 
319         currentDepth++;
320 
321         if (currentDepth == 1) {
322             synchronized (test) {
323                 test.achivedCount++;
324             }
325 
326             int alltime = 0;
327             while (!strace006.isLocked) {
328                 synchronized (test) {
329                     try {
330                         test.wait(1);
331                         alltime++;
332                     } catch (InterruptedException e) {
333                         strace006.complain("" + e);
334                     }
335                     if (alltime > strace006.waitTime) {
336                         throw new Failure("out of wait time");
337                     }
338                 }
339             }
340         } else if (currentDepth > 1 && !isNativeResolved)
341             isNativeResolved = true;
342 
343         if (strace006.DEPTH - currentDepth > 0) {
344             try {
345                 Thread.yield();
346                 recursiveMethod2();
347             } catch (StackOverflowError e) {
348                 // ignore this exception
349             }
350         }
351 
352         currentDepth--;
353     }
354 
355     native void recursiveMethod2();
356 }