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