1 /*
2  * Copyright (c) 2018, 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 TestParallelRefProc
28  * @key gc
29  * @summary Test defaults processing for -XX:+ParallelRefProcEnabled.
30  * @library /test/lib
31  * @library /
32  * @build sun.hotspot.WhiteBox
33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
34  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI gc.arguments.TestParallelRefProc
35  */
36 
37 import java.util.Arrays;
38 import java.util.ArrayList;
39 
40 import jdk.test.lib.process.OutputAnalyzer;
41 import jdk.test.lib.process.ProcessTools;
42 
43 import jtreg.SkippedException;
44 import sun.hotspot.gc.GC;
45 
46 public class TestParallelRefProc {
47 
main(String args[])48     public static void main(String args[]) throws Exception {
49         boolean noneGCSupported = true;
50         if (GC.Serial.isSupported()) {
51             noneGCSupported = false;
52             testFlag(new String[] { "-XX:+UseSerialGC" }, false);
53         }
54         if (GC.Parallel.isSupported()) {
55             noneGCSupported = false;
56             testFlag(new String[] { "-XX:+UseParallelGC" }, false);
57         }
58         if (GC.G1.isSupported()) {
59             noneGCSupported = false;
60             testFlag(new String[] { "-XX:+UseG1GC", "-XX:ParallelGCThreads=1" }, false);
61             testFlag(new String[] { "-XX:+UseG1GC", "-XX:ParallelGCThreads=2" }, true);
62             testFlag(new String[] { "-XX:+UseG1GC", "-XX:-ParallelRefProcEnabled", "-XX:ParallelGCThreads=2" }, false);
63         }
64         if (noneGCSupported) {
65             throw new SkippedException("Skipping test because none of Serial/Parallel/G1 is supported.");
66         }
67     }
68 
69     private static final String parallelRefProcEnabledPattern =
70         " *bool +ParallelRefProcEnabled *= *true +\\{product\\}";
71 
72     private static final String parallelRefProcDisabledPattern =
73         " *bool +ParallelRefProcEnabled *= *false +\\{product\\}";
74 
testFlag(String[] args, boolean expectedTrue)75     private static void testFlag(String[] args, boolean expectedTrue) throws Exception {
76         ArrayList<String> result = new ArrayList<String>();
77         result.addAll(Arrays.asList(args));
78         result.add("-XX:+PrintFlagsFinal");
79         result.add("-version");
80         ProcessBuilder pb = GCArguments.createJavaProcessBuilder(result.toArray(new String[0]));
81 
82         OutputAnalyzer output = new OutputAnalyzer(pb.start());
83 
84         output.shouldHaveExitValue(0);
85 
86         final String expectedPattern = expectedTrue ? parallelRefProcEnabledPattern : parallelRefProcDisabledPattern;
87 
88         String value = output.firstMatch(expectedPattern);
89         if (value == null) {
90             throw new RuntimeException(
91                 Arrays.toString(args) + " didn't set ParallelRefProcEnabled to " + expectedTrue + " as expected");
92         }
93     }
94 }
95