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 TestParallelGCThreads
  28  * @key gc
  29  * @bug 8059527 8081382
  30  * @requires vm.gc.ConcMarkSweep & vm.gc.Parallel & vm.gc.G1
  31  * @summary Tests argument processing for ParallelGCThreads
  32  * @library /test/lib
  33  * @library /
  34  * @modules java.base/jdk.internal.misc
  35  *          java.management
  36  * @run driver gc.arguments.TestParallelGCThreads
  37  */
  38 
  39 import jdk.test.lib.Asserts;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 import jdk.test.lib.process.ProcessTools;
  42 
  43 public class TestParallelGCThreads {
  44 
  45   public static void main(String args[]) throws Exception {
  46     testFlags();
  47     testDefaultValue();
  48   }
  49 
  50   private static final String flagName = "ParallelGCThreads";
  51 
  52   // uint ParallelGCThreads = 23 {product}
  53   private static final String printFlagsFinalPattern = " *uint *" + flagName + " *:?= *(\\d+) *\\{product\\} *";
  54 
  55   public static void testDefaultValue()  throws Exception {
  56     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  57       "-XX:+UnlockExperimentalVMOptions", "-XX:+PrintFlagsFinal", "-version");
  58 
  59     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  60     String value = output.firstMatch(printFlagsFinalPattern, 1);
  61 
  62     try {
  63       Asserts.assertNotNull(value, "Couldn't find uint flag " + flagName);
  64 
  65       Long longValue = new Long(value);
  66 
  67       // Sanity check that we got a non-zero value.
  68       Asserts.assertNotEquals(longValue, "0");
  69 
  70       output.shouldHaveExitValue(0);
  71     } catch (Exception e) {
  72       System.err.println(output.getOutput());
  73       throw e;
  74     }
  75   }
  76 
  77   public static void testFlags() throws Exception {
  78     // For each parallel collector (G1, Parallel, ParNew/CMS)
  79     for (String gc : new String[] {"G1", "Parallel", "ConcMarkSweep"}) {
  80 
  81       // Make sure the VM does not allow ParallelGCThreads set to 0
  82       String[] flags = new String[] {"-XX:+Use" + gc + "GC", "-XX:ParallelGCThreads=0", "-XX:+PrintFlagsFinal", "-version"};
  83       ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);
  84       OutputAnalyzer output = new OutputAnalyzer(pb.start());
  85       output.shouldHaveExitValue(1);
  86 
  87       // Do some basic testing to ensure the flag updates the count
  88       for (long i = 1; i <= 3; i++) {
  89         flags = new String[] {"-XX:+Use" + gc + "GC", "-XX:ParallelGCThreads=" + i, "-XX:+PrintFlagsFinal", "-version"};
  90         long count = getParallelGCThreadCount(flags);
  91         Asserts.assertEQ(count, i, "Specifying ParallelGCThreads=" + i + " for " + gc + "GC does not set the thread count properly!");
  92       }
  93     }
  94 
  95     // 4294967295 == (unsigned int) -1
  96     // So setting ParallelGCThreads=4294967295 should give back 4294967295
  97     // and setting ParallelGCThreads=4294967296 should give back 0. (SerialGC is ok with ParallelGCThreads=0)
  98     for (long i = 4294967295L; i <= 4294967296L; i++) {
  99       String[] flags = new String[] {"-XX:+UseSerialGC", "-XX:ParallelGCThreads=" + i, "-XX:+PrintFlagsFinal", "-version"};
 100       long count = getParallelGCThreadCount(flags);
 101       Asserts.assertEQ(count, i % 4294967296L, "Specifying ParallelGCThreads=" + i + " does not set the thread count properly!");
 102     }
 103   }
 104 
 105   public static long getParallelGCThreadCount(String flags[]) throws Exception {
 106     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);
 107     OutputAnalyzer output = new OutputAnalyzer(pb.start());
 108     output.shouldHaveExitValue(0);
 109     String stdout = output.getStdout();
 110     return FlagsValue.getFlagLongValue("ParallelGCThreads", stdout);
 111   }
 112 }