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