1 /*
  2  * Copyright (c) 2013, 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 TestMaxNewSize
 28  * @key gc
 29  * @bug 7057939
 30  * @summary Make sure that MaxNewSize always has a useful value after argument
 31  * processing.
 32  * @requires vm.gc=="null"
 33  * @library /test/lib
 34  * @modules java.base/jdk.internal.misc
 35  *          java.management
 36  * @run main gc.arguments.TestMaxNewSize -XX:+UseSerialGC
 37  * @run main gc.arguments.TestMaxNewSize -XX:+UseParallelGC
 38  * @run main gc.arguments.TestMaxNewSize -XX:+UseG1GC
 39  * @author thomas.schatzl@oracle.com, jesper.wilhelmsson@oracle.com
 40  */
 41 
 42 /*
 43  * @test TestMaxNewSizeCMS
 44  * @key gc
 45  * @bug 7057939
 46  * @comment Graal does not support CMS
 47  * @requires vm.gc=="null" & !vm.graal.enabled
 48  * @library /test/lib
 49  * @modules java.base/jdk.internal.misc
 50  *          java.management
 51  * @run main gc.arguments.TestMaxNewSize -XX:+UseConcMarkSweepGC
 52  */
 53 
 54 import java.util.regex.Matcher;
 55 import java.util.regex.Pattern;
 56 
 57 import java.math.BigInteger;
 58 
 59 import java.util.ArrayList;
 60 import java.util.Arrays;
 61 
 62 import jdk.test.lib.process.OutputAnalyzer;
 63 import jdk.test.lib.process.ProcessTools;
 64 
 65 public class TestMaxNewSize {
 66 
 67   private static void checkMaxNewSize(String[] flags, int heapsize) throws Exception {
 68     BigInteger actual = new BigInteger(getMaxNewSize(flags));
 69     System.out.println(actual);
 70     if (actual.compareTo(new BigInteger((new Long(heapsize)).toString())) == 1) {
 71       throw new RuntimeException("MaxNewSize value set to \"" + actual +
 72         "\", expected otherwise when running with the following flags: " + Arrays.asList(flags).toString());
 73     }
 74   }
 75 
 76   private static void checkIncompatibleNewSize(String[] flags) throws Exception {
 77     ArrayList<String> finalargs = new ArrayList<String>();
 78     finalargs.addAll(Arrays.asList(flags));
 79     finalargs.add("-version");
 80 
 81     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
 82     OutputAnalyzer output = new OutputAnalyzer(pb.start());
 83     output.shouldContain("Initial young gen size set larger than the maximum young gen size");
 84   }
 85 
 86   private static boolean isRunningG1(String[] args) {
 87     for (int i = 0; i < args.length; i++) {
 88       if (args[i].contains("+UseG1GC")) {
 89         return true;
 90       }
 91     }
 92     return false;
 93   }
 94 
 95   private static String getMaxNewSize(String[] flags) throws Exception {
 96     ArrayList<String> finalargs = new ArrayList<String>();
 97     finalargs.addAll(Arrays.asList(flags));
 98     if (isRunningG1(flags)) {
 99       finalargs.add("-XX:G1HeapRegionSize=1M");
100     }
101     finalargs.add("-XX:+PrintFlagsFinal");
102     finalargs.add("-version");
103 
104     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
105     OutputAnalyzer output = new OutputAnalyzer(pb.start());
106     output.shouldHaveExitValue(0);
107     String stdout = output.getStdout();
108     //System.out.println(stdout);
109     return getFlagValue("MaxNewSize", stdout);
110   }
111 
112   private static String getFlagValue(String flag, String where) {
113     Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
114     if (!m.find()) {
115       throw new RuntimeException("Could not find value for flag " + flag + " in output string");
116     }
117     String match = m.group();
118     return match.substring(match.lastIndexOf(" ") + 1, match.length());
119   }
120 
121   public static void main(String args[]) throws Exception {
122     String gcName = args[0];
123     final int M32 = 32 * 1024 * 1024;
124     final int M64 = 64 * 1024 * 1024;
125     final int M96 = 96 * 1024 * 1024;
126     final int M128 = 128 * 1024 * 1024;
127     checkMaxNewSize(new String[] { gcName, "-Xmx128M" }, M128);
128     checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:NewRatio=5" }, M128);
129     checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:NewSize=32M" }, M128);
130     checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:OldSize=96M" }, M128);
131     checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:MaxNewSize=32M" }, M32);
132     checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:NewSize=32M", "-XX:MaxNewSize=32M" }, M32);
133     checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:NewRatio=6", "-XX:MaxNewSize=32M" }, M32);
134     checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-Xms96M" }, M128);
135     checkMaxNewSize(new String[] { gcName, "-Xmx96M", "-Xms96M" }, M96);
136     checkMaxNewSize(new String[] { gcName, "-XX:NewSize=128M", "-XX:MaxNewSize=50M"}, M128);
137   }
138 }