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/strace013.
 29  * VM testbase keywords: [stress, quick, strace, quarantine]
 30  * VM testbase comments: 8015950
 31  * VM testbase readme:
 32  * DESCRIPTION
 33  *     The test runs many threads, that recursively invoke a pure java method.
 34  *     After arriving at defined depth of recursion, each thread is switched to
 35  *     waits for a monitor. Then the test calls java.lang.Thread.getStackTrace()
 36  *     and java.lang.Thread.getAllStackTraces() methods and checks their results.
 37  *     The test fails if:
 38  *     - amount of stack trace elements and stack trace elements themselves are
 39  *       the same for both methods;
 40  *     - there is at least one element corresponding to invocation of unexpected
 41  *       method. Expected methods are Thread.sleep(), Thread.run() and the
 42  *       recursive method.
 43  *
 44  * @library /vmTestbase
 45  *          /test/lib
 46  * @run main/othervm nsk.stress.strace.strace013
 47  */
 48 
 49 package nsk.stress.strace;
 50 
 51 import nsk.share.ArgumentParser;
 52 import nsk.share.Log;
 53 
 54 import java.io.PrintStream;
 55 import java.util.Map;
 56 
 57 /**
 58  * The test runs <code>THRD_COUNT</code> instances of <code>strace010Thread</code>,
 59  * that recursively invoke a pure java method. After arriving at defined depth
 60  * <code>DEPTH</code> of recursion, each thread is switched to wait a monitor.
 61  * Then the test calls <code>java.lang.Thread.getStackTrace()</code> and
 62  * <code>java.lang.Thread.getAllStackTraces()</code> methods and checks their results.
 63  * <p>
 64  * <p>It is expected that these methods return the same stack traces. Each stack frame
 65  * for both stack traces must be corresponded to invocation of one of the methods
 66  * defined by the <code>EXPECTED_METHODS</code> array.</p>
 67  */
 68 public class strace013 {
 69 
 70     static final int DEPTH = 200;
 71     static final int THRD_COUNT = 100;
 72     static final String[] EXPECTED_METHODS = {
 73             "java.lang.Object.wait",
 74             "nsk.stress.strace.strace013Thread.run",
 75             "nsk.stress.strace.strace013Thread.recursiveMethod"
 76     };
 77 
 78 
 79     static PrintStream out;
 80     static long waitTime = 2;
 81 
 82     static Object lockedObject = new Object();
 83 
 84     volatile int achivedCount = 0;
 85     strace013Thread[] threads;
 86     static Log log;
 87 
 88     public static void main(String[] args) {
 89         out = System.out;
 90         int exitCode = run(args);
 91         System.exit(exitCode + 95);
 92     }
 93 
 94     public static int run(String[] args) {
 95         ArgumentParser argHandler = new ArgumentParser(args);
 96         log = new Log(out, argHandler);
 97         waitTime = argHandler.getWaitTime() * 60000;
 98 
 99         strace013 test = new strace013();
100         boolean res = true;
101 
102         test.startThreads();
103 
104         res = test.makeSnapshot();
105 
106         test.finishThreads();
107 
108         if (!res) {
109             complain("***>>>Test failed<<<***");
110             return 2;
111         }
112 
113         display(">>>Test passed<<<");
114         return 0;
115     }
116 
117     void startThreads() {
118         threads = new strace013Thread[THRD_COUNT];
119         achivedCount = 0;
120 
121         String tmp_name;
122         display("starting threads...");
123         for (int i = 0; i < THRD_COUNT; i++) {
124             tmp_name = "strace013Thread" + Integer.toString(i);
125             threads[i] = new strace013Thread(this, tmp_name);
126             threads[i].start();
127         }
128 
129         waitFor("the defined recursion depth ...");
130     }
131 
132     void waitFor(String msg) {
133         if (msg.length() > 0)
134             display("waiting for " + msg);
135 
136         while (achivedCount < THRD_COUNT) {
137             try {
138                 Thread.sleep(1);
139             } catch (InterruptedException e) {
140                 complain("" + e);
141             }
142         }
143         achivedCount = 0;
144     }
145 
146     boolean makeSnapshot() {
147 
148         Map traces = null;
149         int count = 0;
150         StackTraceElement[][] elements = null;
151 
152         display("making all threads snapshots...");
153         traces = Thread.getAllStackTraces();
154         count = ((StackTraceElement[]) traces.get(threads[0])).length;
155 
156         display("making snapshots of each thread...");
157         elements = new StackTraceElement[THRD_COUNT][];
158         for (int i = 0; i < THRD_COUNT; i++) {
159             elements[i] = threads[i].getStackTrace();
160         }
161 
162         display("notifying");
163         synchronized (strace013.lockedObject) {
164             strace013.lockedObject.notifyAll();
165         }
166 
167         display("");
168 
169         display("checking lengths of stack traces...");
170         StackTraceElement[] all;
171         for (int i = 1; i < THRD_COUNT; i++) {
172             all = (StackTraceElement[]) traces.get(threads[i]);
173             int k = all.length;
174             if (count - k > 2) {
175                 complain("wrong lengths of stack traces:\n\t"
176                         + threads[0].getName() + ": " + count
177                         + "\t"
178                         + threads[i].getName() + ": " + k);
179                 return false;
180             }
181         }
182 
183         display("checking stack traces...");
184         boolean res = true;
185         for (int i = 0; i < THRD_COUNT; i++) {
186             all = (StackTraceElement[]) traces.get(threads[i]);
187             if (!checkTraces(threads[i].getName(), elements[i], all)) {
188                 res = false;
189             }
190         }
191         return res;
192     }
193 
194     boolean checkTraces(String threadName, StackTraceElement[] threadSnap,
195                         StackTraceElement[] allSnap) {
196 
197         int checkedLength = threadSnap.length < allSnap.length ?
198                 threadSnap.length : allSnap.length;
199         boolean res = true;
200 
201         for (int j = 0; j < checkedLength; j++) {
202             if (!checkElement(threadSnap[j])) {
203                 complain("Unexpected " + j + "-element:");
204                 complain("\tmethod name: " + threadSnap[j].getMethodName());
205                 complain("\tclass name: " + threadSnap[j].getClassName());
206                 if (threadSnap[j].isNativeMethod()) {
207                     complain("\tline number: (native method)");
208                 } else {
209                     complain("\tline number: " + threadSnap[j].getLineNumber());
210                     complain("\tfile name: " + threadSnap[j].getFileName());
211                 }
212                 complain("");
213                 res = false;
214             }
215         }
216         return res;
217     }
218 
219     boolean checkElement(StackTraceElement element) {
220         String name = element.getClassName() + "." + element.getMethodName();
221         for (int i = 0; i < EXPECTED_METHODS.length; i++) {
222             if (EXPECTED_METHODS[i].compareTo(name) == 0)
223                 return true;
224         }
225         return false;
226     }
227 
228     void finishThreads() {
229         try {
230             for (int i = 0; i < threads.length; i++) {
231                 if (threads[i].isAlive()) {
232                     display("waiting for finish " + threads[i].getName());
233                     threads[i].join(waitTime);
234                 }
235             }
236         } catch (InterruptedException e) {
237             complain("" + e);
238         }
239     }
240 
241     static void display(String message) {
242         log.display(message);
243     }
244 
245     static void complain(String message) {
246         log.complain(message);
247     }
248 
249 }
250 
251 class strace013Thread extends Thread {
252 
253     private int currentDepth = 0;
254 
255     static int[] arr = new int[1000];
256     strace013 test;
257 
258     strace013Thread(strace013 test, String name) {
259         this.test = test;
260         setName(name);
261     }
262 
263     public void run() {
264         try {
265             recursiveMethod(arr);
266         } catch (Throwable throwable) {
267             System.err.println("# ERROR: " + getName() + ": " + throwable);
268             System.exit(1);
269         }
270     }
271 
272     void recursiveMethod(int[] arr) {
273         currentDepth++;
274 
275         if (strace013.DEPTH - currentDepth > 0) {
276             recursiveMethod(arr);
277         }
278 
279         if (strace013.DEPTH == currentDepth) {
280 
281             strace013.display(getName() + ">waiting on a monitor");
282 
283             synchronized (test) {
284                 test.achivedCount++;
285             }
286 
287             synchronized (strace013.lockedObject) {
288                 try {
289                     strace013.lockedObject.wait();
290                 } catch (InterruptedException e) {
291                     strace013.complain("" + e);
292                 }
293             }
294             strace013.display(getName() + ">notified");
295         }
296 
297         currentDepth--;
298     }
299 }