1 /*
2  * Copyright (c) 2015, 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 TestG1ConcMarkStepDurationMillis
28  * @key gc
29  * @requires vm.gc.G1
30  * @summary Tests argument processing for double type flag, G1ConcMarkStepDurationMillis
31  * @library /test/lib
32  * @library /
33  * @modules java.base/jdk.internal.misc
34  *          java.management
35  * @run main gc.arguments.TestG1ConcMarkStepDurationMillis
36  */
37 
38 import jdk.test.lib.process.ProcessTools;
39 import jdk.test.lib.process.OutputAnalyzer;
40 import java.util.*;
41 import java.util.regex.*;
42 
43 public class TestG1ConcMarkStepDurationMillis {
44 
45   static final int PASS                = 0;
46   static final int FAIL_IMPROPER_VALUE = 1;
47   static final int FAIL_OUT_RANGE      = 2;
48 
49   static final String DOUBLE_1       = "1.0";
50   static final String DOUBLE_MAX     = "1.79e+308";
51 
52   static final String DOUBLE_NEG_EXP = "1.0e-30";
53   static final String NEG_DOUBLE_1   = "-1.0";
54 
55   static final String DOUBLE_INF     = "1.79e+309";
56   static final String NEG_DOUBLE_INF = "-1.79e+309";
57   static final String DOUBLE_NAN     = "abe+309";
58   static final String WRONG_DOUBLE_1 = "1.79e+308e";
59   static final String WRONG_DOUBLE_2 = "1.79ee+308";
60 
main(String args[])61   public static void main(String args[]) throws Exception {
62     // Pass cases
63     runG1ConcMarkStepDurationMillisTest(DOUBLE_1,       PASS);
64     runG1ConcMarkStepDurationMillisTest(DOUBLE_MAX,     PASS);
65 
66     // Fail cases: out of range
67     runG1ConcMarkStepDurationMillisTest(DOUBLE_NEG_EXP, FAIL_OUT_RANGE);
68     runG1ConcMarkStepDurationMillisTest(NEG_DOUBLE_1,   FAIL_OUT_RANGE);
69 
70     // Fail cases: not double
71     runG1ConcMarkStepDurationMillisTest(DOUBLE_INF,     FAIL_IMPROPER_VALUE);
72     runG1ConcMarkStepDurationMillisTest(NEG_DOUBLE_INF, FAIL_IMPROPER_VALUE);
73     runG1ConcMarkStepDurationMillisTest(DOUBLE_NAN,     FAIL_IMPROPER_VALUE);
74     runG1ConcMarkStepDurationMillisTest(WRONG_DOUBLE_1, FAIL_IMPROPER_VALUE);
75     runG1ConcMarkStepDurationMillisTest(WRONG_DOUBLE_2, FAIL_IMPROPER_VALUE);
76   }
77 
runG1ConcMarkStepDurationMillisTest(String expectedValue, int expectedResult)78   private static void runG1ConcMarkStepDurationMillisTest(String expectedValue, int expectedResult) throws Exception {
79     List<String> vmOpts = new ArrayList<>();
80 
81     Collections.addAll(vmOpts, "-XX:+UseG1GC", "-XX:G1ConcMarkStepDurationMillis="+expectedValue, "-XX:+PrintFlagsFinal", "-version");
82 
83     ProcessBuilder pb = GCArguments.createJavaProcessBuilder(vmOpts.toArray(new String[vmOpts.size()]));
84     OutputAnalyzer output = new OutputAnalyzer(pb.start());
85 
86     output.shouldHaveExitValue(expectedResult == PASS ? 0 : 1);
87     String stdout = output.getStdout();
88     if (expectedResult == PASS) {
89       checkG1ConcMarkStepDurationMillisConsistency(stdout, expectedValue);
90     } else if (expectedResult == FAIL_IMPROPER_VALUE) {
91       output.shouldContain("Improperly specified VM option");
92     } else if (expectedResult == FAIL_OUT_RANGE) {
93       output.shouldContain("outside the allowed range");
94     }
95   }
96 
checkG1ConcMarkStepDurationMillisConsistency(String output, String expectedValue)97   private static void checkG1ConcMarkStepDurationMillisConsistency(String output, String expectedValue) {
98     double actualValue = getDoubleValue("G1ConcMarkStepDurationMillis", output);
99 
100     if (Double.parseDouble(expectedValue) != actualValue) {
101       throw new RuntimeException(
102             "Actual G1ConcMarkStepDurationMillis(" + Double.toString(actualValue)
103             + ") is not equal to expected value(" + expectedValue + ")");
104     }
105   }
106 
getDoubleValue(String flag, String where)107   public static double getDoubleValue(String flag, String where) {
108     Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
109     if (!m.find()) {
110       throw new RuntimeException("Could not find value for flag " + flag + " in output string");
111     }
112     String match = m.group();
113     return Double.parseDouble(match.substring(match.lastIndexOf(" ") + 1, match.length()));
114   }
115 }
116