aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test TestMaxNewSize aoqi@0: * @key gc aoqi@0: * @bug 7057939 aoqi@0: * @summary Make sure that MaxNewSize always has a useful value after argument aoqi@0: * processing. aoqi@0: * @library /testlibrary aoqi@0: * @build TestMaxNewSize aoqi@0: * @run main TestMaxNewSize -XX:+UseSerialGC aoqi@0: * @run main TestMaxNewSize -XX:+UseParallelGC aoqi@0: * @run main TestMaxNewSize -XX:+UseConcMarkSweepGC aoqi@0: * @run main TestMaxNewSize -XX:+UseG1GC aoqi@0: * @author thomas.schatzl@oracle.com, jesper.wilhelmsson@oracle.com aoqi@0: */ aoqi@0: aoqi@0: import java.util.regex.Matcher; aoqi@0: import java.util.regex.Pattern; aoqi@0: aoqi@0: import java.math.BigInteger; aoqi@0: aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.Arrays; aoqi@0: aoqi@0: import com.oracle.java.testlibrary.*; aoqi@0: aoqi@0: public class TestMaxNewSize { aoqi@0: aoqi@0: private static void checkMaxNewSize(String[] flags, int heapsize) throws Exception { aoqi@0: BigInteger actual = new BigInteger(getMaxNewSize(flags)); aoqi@0: System.out.println(actual); aoqi@0: if (actual.compareTo(new BigInteger((new Long(heapsize)).toString())) == 1) { aoqi@0: throw new RuntimeException("MaxNewSize value set to \"" + actual + aoqi@0: "\", expected otherwise when running with the following flags: " + Arrays.asList(flags).toString()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static void checkIncompatibleNewSize(String[] flags) throws Exception { aoqi@0: ArrayList finalargs = new ArrayList(); aoqi@0: finalargs.addAll(Arrays.asList(flags)); aoqi@0: finalargs.add("-version"); aoqi@0: aoqi@0: ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0])); aoqi@0: OutputAnalyzer output = new OutputAnalyzer(pb.start()); aoqi@0: output.shouldContain("Initial young gen size set larger than the maximum young gen size"); aoqi@0: } aoqi@0: aoqi@0: private static boolean isRunningG1(String[] args) { aoqi@0: for (int i = 0; i < args.length; i++) { aoqi@0: if (args[i].contains("+UseG1GC")) { aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: private static String getMaxNewSize(String[] flags) throws Exception { aoqi@0: ArrayList finalargs = new ArrayList(); aoqi@0: finalargs.addAll(Arrays.asList(flags)); aoqi@0: if (isRunningG1(flags)) { aoqi@0: finalargs.add("-XX:G1HeapRegionSize=1M"); aoqi@0: } aoqi@0: finalargs.add("-XX:+PrintFlagsFinal"); aoqi@0: finalargs.add("-version"); aoqi@0: aoqi@0: ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0])); aoqi@0: OutputAnalyzer output = new OutputAnalyzer(pb.start()); aoqi@0: output.shouldHaveExitValue(0); aoqi@0: String stdout = output.getStdout(); aoqi@0: //System.out.println(stdout); aoqi@0: return getFlagValue("MaxNewSize", stdout); aoqi@0: } aoqi@0: aoqi@0: private static String getFlagValue(String flag, String where) { aoqi@0: Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where); aoqi@0: if (!m.find()) { aoqi@0: throw new RuntimeException("Could not find value for flag " + flag + " in output string"); aoqi@0: } aoqi@0: String match = m.group(); aoqi@0: return match.substring(match.lastIndexOf(" ") + 1, match.length()); aoqi@0: } aoqi@0: aoqi@0: public static void main(String args[]) throws Exception { aoqi@0: String gcName = args[0]; aoqi@0: final int M32 = 32 * 1024 * 1024; aoqi@0: final int M64 = 64 * 1024 * 1024; aoqi@0: final int M96 = 96 * 1024 * 1024; aoqi@0: final int M128 = 128 * 1024 * 1024; aoqi@0: checkMaxNewSize(new String[] { gcName, "-Xmx128M" }, M128); aoqi@0: checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:NewRatio=5" }, M128); aoqi@0: checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:NewSize=32M" }, M128); aoqi@0: checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:OldSize=96M" }, M128); aoqi@0: checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:MaxNewSize=32M" }, M32); aoqi@0: checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:NewSize=32M", "-XX:MaxNewSize=32M" }, M32); aoqi@0: checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-XX:NewRatio=6", "-XX:MaxNewSize=32M" }, M32); aoqi@0: checkMaxNewSize(new String[] { gcName, "-Xmx128M", "-Xms96M" }, M128); aoqi@0: checkMaxNewSize(new String[] { gcName, "-Xmx96M", "-Xms96M" }, M96); aoqi@0: checkMaxNewSize(new String[] { gcName, "-XX:NewSize=128M", "-XX:MaxNewSize=50M"}, M128); aoqi@0: } aoqi@0: }