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