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