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