1 /*
2  * Copyright (c) 2014, 2019, 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 package gc.arguments;
25 
26 /*
27  * @test TestParallelGCThreads
28  * @key gc
29  * @bug 8059527 8081382
30  * @summary Tests argument processing for ParallelGCThreads
31  * @library /test/lib
32  * @library /
33  * @modules java.base/jdk.internal.misc
34  *          java.management
35  * @build sun.hotspot.WhiteBox
36  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
37  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI gc.arguments.TestParallelGCThreads
38  */
39 
40 import java.util.ArrayList;
41 import java.util.List;
42 import jdk.test.lib.Asserts;
43 import jdk.test.lib.process.OutputAnalyzer;
44 import jdk.test.lib.process.ProcessTools;
45 import jtreg.SkippedException;
46 import sun.hotspot.gc.GC;
47 
48 public class TestParallelGCThreads {
49 
main(String args[])50   public static void main(String args[]) throws Exception {
51     testFlags();
52     testDefaultValue();
53   }
54 
55   private static final String flagName = "ParallelGCThreads";
56 
57   // uint ParallelGCThreads = 23 {product}
58   private static final String printFlagsFinalPattern = " *uint *" + flagName + " *:?= *(\\d+) *\\{product\\} *";
59 
testDefaultValue()60   public static void testDefaultValue()  throws Exception {
61     ProcessBuilder pb = GCArguments.createJavaProcessBuilder(
62       "-XX:+UnlockExperimentalVMOptions", "-XX:+PrintFlagsFinal", "-version");
63 
64     OutputAnalyzer output = new OutputAnalyzer(pb.start());
65     String value = output.firstMatch(printFlagsFinalPattern, 1);
66 
67     try {
68       Asserts.assertNotNull(value, "Couldn't find uint flag " + flagName);
69 
70       Long longValue = new Long(value);
71 
72       // Sanity check that we got a non-zero value.
73       Asserts.assertNotEquals(longValue, "0");
74 
75       output.shouldHaveExitValue(0);
76     } catch (Exception e) {
77       System.err.println(output.getOutput());
78       throw e;
79     }
80   }
81 
testFlags()82   public static void testFlags() throws Exception {
83     // For each parallel collector (G1, Parallel)
84     List<String> supportedGC = new ArrayList<String>();
85 
86     if (GC.G1.isSupported()) {
87       supportedGC.add("G1");
88     }
89     if (GC.Parallel.isSupported()) {
90       supportedGC.add("Parallel");
91     }
92 
93     if (supportedGC.isEmpty()) {
94       throw new SkippedException("Skipping test because none of G1/Parallel is supported.");
95     }
96 
97     for (String gc : supportedGC) {
98 
99       // Make sure the VM does not allow ParallelGCThreads set to 0
100       String[] flags = new String[] {"-XX:+Use" + gc + "GC", "-XX:ParallelGCThreads=0", "-XX:+PrintFlagsFinal", "-version"};
101       ProcessBuilder pb = GCArguments.createJavaProcessBuilder(flags);
102       OutputAnalyzer output = new OutputAnalyzer(pb.start());
103       output.shouldHaveExitValue(1);
104 
105       // Do some basic testing to ensure the flag updates the count
106       for (long i = 1; i <= 3; i++) {
107         flags = new String[] {"-XX:+Use" + gc + "GC", "-XX:ParallelGCThreads=" + i, "-XX:+PrintFlagsFinal", "-version"};
108         long count = getParallelGCThreadCount(flags);
109         Asserts.assertEQ(count, i, "Specifying ParallelGCThreads=" + i + " for " + gc + "GC does not set the thread count properly!");
110       }
111     }
112 
113     // 4294967295 == (unsigned int) -1
114     // So setting ParallelGCThreads=4294967295 should give back 4294967295
115     // and setting ParallelGCThreads=4294967296 should give back 0. (SerialGC is ok with ParallelGCThreads=0)
116     for (long i = 4294967295L; i <= 4294967296L; i++) {
117       String[] flags = new String[] {"-XX:+UseSerialGC", "-XX:ParallelGCThreads=" + i, "-XX:+PrintFlagsFinal", "-version"};
118       long count = getParallelGCThreadCount(flags);
119       Asserts.assertEQ(count, i % 4294967296L, "Specifying ParallelGCThreads=" + i + " does not set the thread count properly!");
120     }
121   }
122 
getParallelGCThreadCount(String flags[])123   public static long getParallelGCThreadCount(String flags[]) throws Exception {
124     ProcessBuilder pb = GCArguments.createJavaProcessBuilder(flags);
125     OutputAnalyzer output = new OutputAnalyzer(pb.start());
126     output.shouldHaveExitValue(0);
127     String stdout = output.getStdout();
128     return FlagsValue.getFlagLongValue("ParallelGCThreads", stdout);
129   }
130 }
131