1 /*
2  * Copyright (c) 2017, 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 import java.io.File;
26 import java.nio.file.Paths;
27 import java.time.Duration;
28 
29 import jdk.test.lib.process.ProcessTools;
30 import jdk.test.lib.process.OutputAnalyzer;
31 
32 import sun.hotspot.WhiteBox;
33 
34 /*
35  * @test HandshakeTransitionTest
36  * @summary This does a sanity test of the poll in the native wrapper.
37  * @requires vm.debug
38  * @library /testlibrary /test/lib
39  * @build HandshakeTransitionTest
40  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
41  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
42  * @run main/othervm/native -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI HandshakeTransitionTest
43  */
44 
45 public class HandshakeTransitionTest {
46 
someTime(int ms)47     public static native void someTime(int ms);
48 
main(String[] args)49     public static void main(String[] args) throws Exception {
50         String lib = System.getProperty("test.nativepath");
51         WhiteBox wb = WhiteBox.getWhiteBox();
52         Boolean useJVMCICompiler = wb.getBooleanVMFlag("UseJVMCICompiler");
53         String useJVMCICompilerStr;
54         if (useJVMCICompiler != null) {
55             useJVMCICompilerStr = useJVMCICompiler ?  "-XX:+UseJVMCICompiler" : "-XX:-UseJVMCICompiler";
56         } else {
57             // pass something innocuous
58             useJVMCICompilerStr = "-XX:+UnlockExperimentalVMOptions";
59         }
60         ProcessBuilder pb =
61             ProcessTools.createJavaProcessBuilder(
62                     true,
63                     "-Djava.library.path=" + lib,
64                     "-XX:+SafepointALot",
65                     "-XX:GuaranteedSafepointInterval=20",
66                     "-Xlog:ergo*",
67                     "-XX:ParallelGCThreads=1",
68                     "-XX:ConcGCThreads=1",
69                     "-XX:CICompilerCount=2",
70                     "-XX:+UnlockExperimentalVMOptions",
71                     useJVMCICompilerStr,
72                     "HandshakeTransitionTest$Test");
73 
74 
75         OutputAnalyzer output = ProcessTools.executeProcess(pb);
76         output.reportDiagnosticSummary();
77         output.shouldHaveExitValue(0);
78         output.stdoutShouldContain("JOINED");
79     }
80 
81     static class Test implements Runnable {
82         final static int testLoops = 2000;
83         final static int testSleep = 1; //ms
84 
main(String[] args)85         public static void main(String[] args) throws Exception {
86             System.loadLibrary("HandshakeTransitionTest");
87             Test test = new Test();
88             Thread[] threads = new Thread[64];
89             for (int i = 0; i<threads.length ; i++) {
90                 threads[i] = new Thread(test);
91                 threads[i].start();
92             }
93             for (Thread t : threads) {
94                 t.join();
95             }
96             System.out.println("JOINED");
97         }
98 
99         @Override
run()100         public void run() {
101             try {
102                 for (int i = 0; i<testLoops ; i++) {
103                     someTime(testSleep);
104                 }
105             } catch (Exception e) {
106                 System.out.println(e.getMessage());
107                 System.exit(1);
108             }
109         }
110     }
111 }
112