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