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 /*
 26  * @test
 27  * @key stress
 28  *
 29  * @summary converted from VM testbase nsk/stress/strace/strace002.
 30  * VM testbase keywords: [stress, quick, strace]
 31  * VM testbase readme:
 32  * DESCRIPTION
 33  *     The test checks up java.lang.Thread.getAllStackTraces() method for many
 34  *     threads, that recursively invoke a pure java method in running mode
 35  *     ("alive" stack).
 36  *     The test fails if:
 37  *     - amount of stack trace elements is more than depth of recursion plus
 38  *       four elements corresponding to invocations of Thread.run(), Thread.wait(),
 39  *       Thread.exit(), Thread.yield() and ThreadGroup.remove() methods;
 40  *     - there is at least one element corresponding to invocation of unexpected
 41  *       method.
 42  *     This test is almost the same as nsk.stress.strace.strace001 except for
 43  *     checking is performed for java.lang.Thread.getAllStackTraces() method.
 44  *
 45  * @library /vmTestbase
 46  *          /test/lib
 47  * @run main/othervm nsk.stress.strace.strace002
 48  */
 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.getAllStackTraces()</code> method for many
 62  * threads, that recursively invoke a pure java method in running mode ("alive" stack).
 63  * <p>
 64  * <p>The test creates <code>THRD_COUNT</code> instances of <code>strace002Thread</code>
 65  * class, tries to get their stack traces and checks up that returned array contains
 66  * correct stack frames. Each stack frame must be corresponded to one of the following
 67  * methods defined by the <code>EXPECTED_METHODS</code> array.</p>
 68  * <p>These checking are performed <code>REPEAT_COUNT</code> times.</p>
 69  */
 70 public class strace002 {
 71 
 72     static final int DEPTH = 200;
 73     static final int THRD_COUNT = 100;
 74     static final int REPEAT_COUNT = 10;
 75     static final String[] EXPECTED_METHODS = {
 76             "java.lang.System.arraycopy",
 77             "java.lang.Object.wait",
 78             "java.lang.Object.wait0",
 79             "java.lang.Thread.exit",
 80             "java.lang.Thread.yield",
 81             "java.lang.Thread.yield0",
 82             "java.lang.Thread.currentCarrierThread",
 83             "java.lang.Thread.currentThread",
 84             "java.lang.Thread.currentThread0",
 85             "java.lang.Thread.getVirtualThread",
 86             "java.lang.ThreadGroup.remove",
 87             "java.lang.ThreadGroup.threadTerminated",
 88             "nsk.stress.strace.strace002Thread.run",
 89             "nsk.stress.strace.strace002Thread.recursiveMethod"
 90     };
 91 
 92 
 93     static volatile boolean isLocked = false;
 94     static PrintStream out;
 95     static long waitTime = 2;
 96 
 97     static Object waitStart = new Object();
 98 
 99     static strace002Thread[] threads;
100     static StackTraceElement[][] snapshots = new StackTraceElement[THRD_COUNT][];
101     static Log log;
102 
103     public static void main(String[] args) {
104         out = System.out;
105         int exitCode = run(args);
106         System.exit(exitCode + 95);
107     }
108 
109     volatile int achivedCount = 0;
110 
111     public static int run(String[] args) {
112 
113         ArgumentParser argHandler = new ArgumentParser(args);
114         log = new Log(out, argHandler);
115         waitTime = argHandler.getWaitTime() * 60000;
116 
117         strace002 test = new strace002();
118         boolean res = true;
119 
120         for (int j = 0; j < REPEAT_COUNT; j++) {
121             test.startThreads();
122 
123             if (!test.makeSnapshot(j + 1)) res = false;
124 
125             display("waiting for threads finished\n");
126             test.finishThreads();
127         }
128 
129         if (!res) {
130             complain("***>>>Test failed<<<***");
131             return 2;
132         }
133 
134         return 0;
135     }
136 
137     void startThreads() {
138         threads = new strace002Thread[THRD_COUNT];
139         achivedCount = 0;
140 
141         String tmp_name;
142         for (int i = 0; i < THRD_COUNT; i++) {
143             tmp_name = "strace002Thread" + Integer.toString(i);
144             threads[i] = new strace002Thread(this, tmp_name);
145         }
146 
147         for (int i = 0; i < THRD_COUNT; i++) {
148             threads[i].start();
149         }
150 
151         waitFor("all threads started ...");
152         synchronized (waitStart) {
153             isLocked = true;
154             waitStart.notifyAll();
155         }
156         try {
157             Thread.yield();
158             Thread.sleep(1);
159         } catch (InterruptedException e) {
160             complain("" + e);
161         }
162     }
163 
164     void waitFor(String msg) {
165         if (msg.length() > 0)
166             display("waiting for " + msg);
167 
168         while (achivedCount < THRD_COUNT) {
169             try {
170                 Thread.sleep(1);
171             } catch (InterruptedException e) {
172                 complain("" + e);
173             }
174         }
175         achivedCount = 0;
176     }
177 
178     boolean makeSnapshot(int repeat_number) {
179 
180         Map traces = Thread.getAllStackTraces();
181         for (int i = 0; i < threads.length; i++) {
182             snapshots[i] = (StackTraceElement[]) traces.get(threads[i]);
183         }
184 
185         return checkTraces(repeat_number);
186     }
187 
188     boolean checkTraces(int repeat_number) {
189         StackTraceElement[] elements;
190 
191         boolean res = true;
192         display(">>> snapshot " + repeat_number);
193         int expectedCount = DEPTH + 1;
194 
195         for (int i = 0; i < threads.length; i++) {
196             elements = snapshots[i];
197 
198             if (elements == null)
199                 continue;
200 
201             if (elements.length == 0)
202                 continue;
203 
204             if (elements.length > 3) {
205                 display("\tchecking " + threads[i].getName()
206                         + "(trace elements: " + elements.length + ")");
207             }
208 
209             if (elements.length > expectedCount) {
210                 complain(threads[i].getName() + ">Contains more then " +
211                         +expectedCount + " elements");
212             }
213 
214             for (int j = 0; j < elements.length; j++) {
215                 if (!checkElement(elements[j])) {
216                     complain(threads[i].getName() + ">Unexpected method name: "
217                             + elements[j].getMethodName());
218                     complain("\tat " + j + " position");
219                     if (elements[j].isNativeMethod()) {
220                         complain("\tline number: (native method)");
221                         complain("\tclass name: " + elements[j].getClassName());
222                     } else {
223                         complain("\tline number: " + elements[j].getLineNumber());
224                         complain("\tclass name: " + elements[j].getClassName());
225                         complain("\tfile name: " + elements[j].getFileName());
226                     }
227                     res = false;
228                 }
229             }
230         }
231         return res;
232     }
233 
234     boolean checkElement(StackTraceElement element) {
235         String name = element.getClassName() + "." + element.getMethodName();
236         for (int i = 0; i < EXPECTED_METHODS.length; i++) {
237             if (name.startsWith(EXPECTED_METHODS[i]))
238                 return true;
239         }
240         return false;
241     }
242 
243     void finishThreads() {
244         try {
245             for (int i = 0; i < threads.length; i++) {
246                 if (threads[i].isAlive())
247                     threads[i].join(waitTime / THRD_COUNT);
248             }
249         } catch (InterruptedException e) {
250             complain("" + e);
251         }
252         isLocked = false;
253     }
254 
255     static void display(String message) {
256         log.display(message);
257     }
258 
259     static void complain(String message) {
260         log.complain(message);
261     }
262 
263 
264 }
265 
266 class strace002Thread extends Thread {
267 
268     private int currentDepth = 0;
269 
270     strace002 test;
271 
272     strace002Thread(strace002 test, String name) {
273         this.test = test;
274         setName(name);
275     }
276 
277     public void run() {
278         try {
279             recursiveMethod();
280         } catch (Throwable throwable) {
281             System.err.println("# ERROR: " + getName() + ": " + throwable);
282             System.exit(1);
283         }
284     }
285 
286     void recursiveMethod() {
287 
288         currentDepth++;
289 
290         if (currentDepth == 1) {
291             synchronized (test) {
292                 test.achivedCount++;
293             }
294 
295             int alltime = 0;
296             while (!test.isLocked) {
297                 synchronized (test) {
298                     try {
299                         test.wait(1);
300                         alltime++;
301                     } catch (InterruptedException e) {
302                         strace002.complain("" + e);
303                     }
304                     if (alltime > strace002.waitTime) {
305                         throw new Failure("out of wait time");
306                     }
307                 }
308             }
309         }
310 
311         if (strace002.DEPTH - currentDepth > 0) {
312             Thread.yield();
313             recursiveMethod();
314         }
315 
316         currentDepth--;
317     }
318 }