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