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 TestG1ConcRefinementThreads
28  * @key gc
29  * @bug 8047976
30  * @requires vm.gc.G1
31  * @summary Tests argument processing for G1ConcRefinementThreads
32  * @library /test/lib
33  * @library /
34  * @modules java.base/jdk.internal.misc
35  *          java.management
36  * @run main gc.arguments.TestG1ConcRefinementThreads
37  */
38 
39 import jdk.test.lib.process.OutputAnalyzer;
40 import jdk.test.lib.process.ProcessTools;
41 import java.util.*;
42 import java.util.regex.*;
43 
44 public class TestG1ConcRefinementThreads {
45 
46   static final int AUTO_SELECT_THREADS_COUNT = -1;
47   static final int PASSED_THREADS_COUNT = 11;
48 
main(String args[])49   public static void main(String args[]) throws Exception {
50     // default case
51     runG1ConcRefinementThreadsTest(
52         new String[]{}, // automatically selected
53         AUTO_SELECT_THREADS_COUNT /* use default setting */);
54 
55     // zero setting case
56     runG1ConcRefinementThreadsTest(
57         new String[]{"-XX:G1ConcRefinementThreads=0"},
58         0);
59 
60     // non-zero sestting case
61     runG1ConcRefinementThreadsTest(
62         new String[]{"-XX:G1ConcRefinementThreads="+Integer.toString(PASSED_THREADS_COUNT)},
63         PASSED_THREADS_COUNT);
64   }
65 
runG1ConcRefinementThreadsTest(String[] passedOpts, int expectedValue)66   private static void runG1ConcRefinementThreadsTest(String[] passedOpts,
67           int expectedValue) throws Exception {
68     List<String> vmOpts = new ArrayList<>();
69     if (passedOpts.length > 0) {
70       Collections.addAll(vmOpts, passedOpts);
71     }
72     Collections.addAll(vmOpts, "-XX:+UseG1GC", "-XX:+PrintFlagsFinal", "-version");
73 
74     ProcessBuilder pb = GCArguments.createJavaProcessBuilder(vmOpts.toArray(new String[vmOpts.size()]));
75     OutputAnalyzer output = new OutputAnalyzer(pb.start());
76 
77     output.shouldHaveExitValue(0);
78     String stdout = output.getStdout();
79     checkG1ConcRefinementThreadsConsistency(stdout, expectedValue);
80   }
81 
checkG1ConcRefinementThreadsConsistency(String output, int expectedValue)82   private static void checkG1ConcRefinementThreadsConsistency(String output, int expectedValue) {
83     int actualValue = getIntValue("G1ConcRefinementThreads", output);
84 
85     if (expectedValue == AUTO_SELECT_THREADS_COUNT) {
86       // If expectedValue is automatically selected, set it same as ParallelGCThreads.
87       expectedValue = getIntValue("ParallelGCThreads", output);
88     }
89 
90     if (expectedValue != actualValue) {
91       throw new RuntimeException(
92             "Actual G1ConcRefinementThreads(" + Integer.toString(actualValue)
93             + ") is not equal to expected value(" + Integer.toString(expectedValue) + ")");
94     }
95   }
96 
getIntValue(String flag, String where)97   public static int getIntValue(String flag, String where) {
98     Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
99     if (!m.find()) {
100       throw new RuntimeException("Could not find value for flag " + flag + " in output string");
101     }
102     String match = m.group();
103     return Integer.parseInt(match.substring(match.lastIndexOf(" ") + 1, match.length()));
104   }
105 }
106