--- old/src/share/vm/memory/collectorPolicy.cpp 2013-04-17 10:26:44.449201835 +0200 +++ new/src/share/vm/memory/collectorPolicy.cpp 2013-04-17 10:26:44.321201840 +0200 @@ -48,6 +48,10 @@ // CollectorPolicy methods. void CollectorPolicy::initialize_flags() { + if (MaxHeapSize < InitialHeapSize) { + vm_exit_during_initialization("Incompatible initial and maximum heap sizes specified"); + } + if (MetaspaceSize > MaxMetaspaceSize) { MaxMetaspaceSize = MetaspaceSize; } @@ -71,21 +75,9 @@ } void CollectorPolicy::initialize_size_info() { - // User inputs from -mx and ms are aligned - set_initial_heap_byte_size(InitialHeapSize); - if (initial_heap_byte_size() == 0) { - set_initial_heap_byte_size(NewSize + OldSize); - } - set_initial_heap_byte_size(align_size_up(_initial_heap_byte_size, - min_alignment())); - - set_min_heap_byte_size(Arguments::min_heap_size()); - if (min_heap_byte_size() == 0) { - set_min_heap_byte_size(NewSize + OldSize); - } - set_min_heap_byte_size(align_size_up(_min_heap_byte_size, - min_alignment())); - + // User inputs from -mx and ms must be aligned + set_min_heap_byte_size(align_size_up(Arguments::min_heap_size(), min_alignment())); + set_initial_heap_byte_size(align_size_up(InitialHeapSize, min_alignment())); set_max_heap_byte_size(align_size_up(MaxHeapSize, max_alignment())); // Check heap parameter properties @@ -233,9 +225,6 @@ GenCollectorPolicy::initialize_flags(); OldSize = align_size_down(OldSize, min_alignment()); - if (NewSize + OldSize > MaxHeapSize) { - MaxHeapSize = NewSize + OldSize; - } if (FLAG_IS_CMDLINE(OldSize) && FLAG_IS_DEFAULT(NewSize)) { // NewRatio will be used later to set the young generation size so we use @@ -250,6 +239,27 @@ } MaxHeapSize = align_size_up(MaxHeapSize, max_alignment()); + // adjust max heap size if necessary + if (NewSize + OldSize > MaxHeapSize) { + if (FLAG_IS_CMDLINE(MaxHeapSize)) { + // somebody set a maximum heap size with the intention that we should not + // exceed it. Adjust New/OldSize as necessary. + uintx calculated_size = NewSize + OldSize; + double shrink_factor = (double) MaxHeapSize / calculated_size; + // align + NewSize = align_size_down((uintx) (NewSize * shrink_factor), min_alignment()); + // OldSize is already aligned because above we aligned MaxHeapSize to + // max_alignment(), and we just made sure that NewSize is aligned to + // min_alignment(). In initialize_flags() we verified that max_alignment() + // is a multiple of min_alignment(). + OldSize = MaxHeapSize - NewSize; + } else { + MaxHeapSize = NewSize + OldSize; + } + } + // need to do this again + MaxHeapSize = align_size_up(MaxHeapSize, max_alignment()); + always_do_update_barrier = UseConcMarkSweepGC; // Check validity of heap flags --- old/src/share/vm/prims/whitebox.cpp 2013-04-17 10:26:45.109201807 +0200 +++ new/src/share/vm/prims/whitebox.cpp 2013-04-17 10:26:44.949201814 +0200 @@ -92,6 +92,15 @@ return closure.found(); WB_END +WB_ENTRY(void, WB_PrintHeapSizes(JNIEnv* env, jobject o)) { + CollectorPolicy * p = Universe::heap()->collector_policy(); + gclog_or_tty->print_cr("Minimum heap "SIZE_FORMAT" Initial heap " + SIZE_FORMAT" Maximum heap "SIZE_FORMAT" Min alignment "SIZE_FORMAT" Max alignment "SIZE_FORMAT, + p->min_heap_byte_size(), p->initial_heap_byte_size(), p->max_heap_byte_size(), + p->min_alignment(), p->max_alignment()); +} +WB_END + #if INCLUDE_ALL_GCS WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj)) G1CollectedHeap* g1 = G1CollectedHeap::heap(); @@ -333,6 +342,7 @@ CC"(Ljava/lang/String;[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;", (void*) &WB_ParseCommandLine }, + {CC"printHeapSizes", CC"()V", (void*)&WB_PrintHeapSizes }, #if INCLUDE_ALL_GCS {CC"g1InConcurrentMark", CC"()Z", (void*)&WB_G1InConcurrentMark}, {CC"g1IsHumongous", CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous }, --- old/src/share/vm/runtime/arguments.cpp 2013-04-17 10:26:45.789201779 +0200 +++ new/src/share/vm/runtime/arguments.cpp 2013-04-17 10:26:45.621201786 +0200 @@ -1617,30 +1617,38 @@ FLAG_SET_ERGO(uintx, MaxHeapSize, (uintx)reasonable_max); } - // If the initial_heap_size has not been set with InitialHeapSize - // or -Xms, then set it as fraction of the size of physical memory, - // respecting the maximum and minimum sizes of the heap. - if (FLAG_IS_DEFAULT(InitialHeapSize)) { + // If the minimum or initial heap_size have not been set or requested to be set + // ergonomically, set them accordingly. + if (InitialHeapSize == 0 || min_heap_size() == 0) { julong reasonable_minimum = (julong)(OldSize + NewSize); reasonable_minimum = MIN2(reasonable_minimum, (julong)MaxHeapSize); reasonable_minimum = limit_by_allocatable_memory(reasonable_minimum); - julong reasonable_initial = phys_mem / InitialRAMFraction; + if (InitialHeapSize == 0) { + julong reasonable_initial = phys_mem / InitialRAMFraction; - reasonable_initial = MAX2(reasonable_initial, reasonable_minimum); - reasonable_initial = MIN2(reasonable_initial, (julong)MaxHeapSize); + reasonable_initial = MAX3(reasonable_initial, reasonable_minimum, (julong)min_heap_size()); + reasonable_initial = MIN2(reasonable_initial, (julong)MaxHeapSize); - reasonable_initial = limit_by_allocatable_memory(reasonable_initial); + reasonable_initial = limit_by_allocatable_memory(reasonable_initial); - if (PrintGCDetails && Verbose) { - // Cannot use gclog_or_tty yet. - tty->print_cr(" Initial heap size " SIZE_FORMAT, (uintx)reasonable_initial); - tty->print_cr(" Minimum heap size " SIZE_FORMAT, (uintx)reasonable_minimum); + if (PrintGCDetails && Verbose) { + // Cannot use gclog_or_tty yet. + tty->print_cr(" Initial heap size " SIZE_FORMAT, (uintx)reasonable_initial); + } + FLAG_SET_ERGO(uintx, InitialHeapSize, (uintx)reasonable_initial); + } + // If the minimum heap size has not been set (via -Xms), + // synchronize with InitialHeapSize to avoid errors with the default value. + if (min_heap_size() == 0) { + set_min_heap_size(MIN2((uintx)reasonable_minimum, InitialHeapSize)); + if (PrintGCDetails && Verbose) { + // Cannot use gclog_or_tty yet. + tty->print_cr(" Minimum heap size " SIZE_FORMAT, min_heap_size()); + } } - FLAG_SET_ERGO(uintx, InitialHeapSize, (uintx)reasonable_initial); - set_min_heap_size((uintx)reasonable_minimum); } } @@ -2373,7 +2381,8 @@ // -Xms } else if (match_option(option, "-Xms", &tail)) { julong long_initial_heap_size = 0; - ArgsRange errcode = parse_memory_size(tail, &long_initial_heap_size, 1); + // an initial heap size of 0 means automatically determine + ArgsRange errcode = parse_memory_size(tail, &long_initial_heap_size, 0); if (errcode != arg_in_range) { jio_fprintf(defaultStream::error_stream(), "Invalid initial heap size: %s\n", option->optionString); @@ -2384,7 +2393,7 @@ // Currently the minimum size and the initial heap sizes are the same. set_min_heap_size(InitialHeapSize); // -Xmx - } else if (match_option(option, "-Xmx", &tail)) { + } else if (match_option(option, "-Xmx", &tail) || match_option(option, "-XX:MaxHeapSize=", &tail)) { julong long_max_heap_size = 0; ArgsRange errcode = parse_memory_size(tail, &long_max_heap_size, 1); if (errcode != arg_in_range) { --- old/src/share/vm/runtime/globals.hpp 2013-04-17 10:26:46.657201742 +0200 +++ new/src/share/vm/runtime/globals.hpp 2013-04-17 10:26:46.465201751 +0200 @@ -2965,7 +2965,7 @@ \ /* gc parameters */ \ product(uintx, InitialHeapSize, 0, \ - "Initial heap size (in bytes); zero means OldSize + NewSize") \ + "Initial heap size (in bytes); zero means use ergonomics") \ \ product(uintx, MaxHeapSize, ScaleForWordSize(96*M), \ "Maximum heap size (in bytes)") \ --- old/test/testlibrary/whitebox/sun/hotspot/WhiteBox.java 2013-04-17 10:26:47.845201693 +0200 +++ new/test/testlibrary/whitebox/sun/hotspot/WhiteBox.java 2013-04-17 10:26:47.657201700 +0200 @@ -61,6 +61,9 @@ registerNatives(); } + // Arguments + public native void printHeapSizes(); + // Memory public native long getObjectAddress(Object o); public native int getHeapOopSize(); --- /dev/null 2013-04-16 09:14:15.018414535 +0200 +++ new/test/gc/8006088/TestCMS.java 2013-04-17 10:26:48.409201668 +0200 @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* This code is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License version 2 only, as +* published by the Free Software Foundation. +* +* This code is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* version 2 for more details (a copy is included in the LICENSE file that +* accompanied this code). +* +* You should have received a copy of the GNU General Public License version +* 2 along with this work; if not, write to the Free Software Foundation, +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +* +* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +* or visit www.oracle.com if you need additional information or have any +* questions. +*/ + +/* + * @test TestCMS + * @key gc + * @bug 8006088 + * @summary Tests argument processing for initial and maximum heap size for the CMS collector + * @library /testlibrary /testlibrary/whitebox + * @build TestCMS TestMaxHeapSizeTools + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * @run main/othervm TestCMS + * @author thomas.schatzl@oracle.com + */ + +public class TestCMS { + + public static void main(String args[]) throws Exception { + final String gcName = "-XX:+UseConcMarkSweepGC"; + + TestMaxHeapSizeTools.checkMinInitialMaxHeapFlags(gcName); + + TestMaxHeapSizeTools.checkGenMaxHeapErgo(gcName); + } +} + --- /dev/null 2013-04-16 09:14:15.018414535 +0200 +++ new/test/gc/8006088/TestG1.java 2013-04-17 10:26:48.861201650 +0200 @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* This code is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License version 2 only, as +* published by the Free Software Foundation. +* +* This code is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* version 2 for more details (a copy is included in the LICENSE file that +* accompanied this code). +* +* You should have received a copy of the GNU General Public License version +* 2 along with this work; if not, write to the Free Software Foundation, +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +* +* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +* or visit www.oracle.com if you need additional information or have any +* questions. +*/ + +/* + * @test TestG1 + * @key gc + * @bug 8006088 + * @summary Tests argument processing for initial and maximum heap size for the G1 collector + * @library /testlibrary /testlibrary/whitebox + * @build TestG1 TestMaxHeapSizeTools + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * @run main/othervm TestG1 + * @author thomas.schatzl@oracle.com + */ + +public class TestG1 { + + public static void main(String args[]) throws Exception { + final String gcName = "-XX:+UseG1GC"; + + TestMaxHeapSizeTools.checkMinInitialMaxHeapFlags(gcName); + + TestMaxHeapSizeTools.checkGenMaxHeapErgo(gcName); + } +} + --- /dev/null 2013-04-16 09:14:15.018414535 +0200 +++ new/test/gc/8006088/TestMaxHeapSizeTools.java 2013-04-17 10:26:49.305201631 +0200 @@ -0,0 +1,277 @@ +/* +* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* This code is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License version 2 only, as +* published by the Free Software Foundation. +* +* This code is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* version 2 for more details (a copy is included in the LICENSE file that +* accompanied this code). +* +* You should have received a copy of the GNU General Public License version +* 2 along with this work; if not, write to the Free Software Foundation, +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +* +* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +* or visit www.oracle.com if you need additional information or have any +* questions. +*/ + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.ArrayList; +import java.util.Arrays; + +import com.oracle.java.testlibrary.*; +import sun.hotspot.WhiteBox; + +class ErgoArgsPrinter { + public static void main(String[] args) throws Exception { + WhiteBox wb = WhiteBox.getWhiteBox(); + wb.printHeapSizes(); + } +} + +final class MinInitialMaxValues { + public long minHeapSize; + public long initialHeapSize; + public long maxHeapSize; + + public long minAlignment; + public long maxAlignment; +} + +class TestMaxHeapSizeTools { + + public static void checkMinInitialMaxHeapFlags(String gcflag) throws Exception { + checkInvalidMinInitialHeapCombinations(gcflag); + checkValidMinInitialHeapCombinations(gcflag); + checkInvalidInitialMaxHeapCombinations(gcflag); + checkValidInitialMaxHeapCombinations(gcflag); + } + + public static void checkMinInitialErgonomics(String gcflag) throws Exception { + // heap sizing ergonomics use the value NewSize + OldSize as default values + // for ergonomics calculation. Retrieve these values. + long[] values = new long[2]; + getNewOldSize(gcflag, values); + + // we check cases with values smaller and larger than this default value. + long newPlusOldSize = values[0] + values[1]; + long smallValue = newPlusOldSize / 2; + long largeValue = newPlusOldSize * 2; + + // -Xms is not set + checkErgonomics(new String[] { gcflag, "-Xmx16M" }, values, -1, -1); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-XX:InitialHeapSize=0" }, values, -1, -1); + + // -Xms is set to zero + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms0" }, values, -1, -1); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms0", "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms0", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms0", "-XX:InitialHeapSize=0" }, values, -1, -1); + + // -Xms is set to small value + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + smallValue }, values, -1, -1); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + smallValue, "-XX:InitialHeapSize=0" }, values, smallValue, -1); + + // -Xms is set to large value + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + largeValue }, values, largeValue, largeValue); + // the next case has already been checked elsewhere and gives an error + // checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + largeValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue); + // the next case has already been checked elsewhere too + // checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + largeValue, "-XX:InitialHeapSize=" + largeValue }, values, values[0], largeValue); + checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + largeValue, "-XX:InitialHeapSize=0" }, values, largeValue, -1); + } + + private static long align_up(long value, long alignment) { + long alignmentMinusOne = alignment - 1; + return (value + alignmentMinusOne) & ~alignmentMinusOne; + } + + private static void getNewOldSize(String gcflag, long[] values) throws Exception { + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag, + "-XX:+PrintFlagsFinal", "-version"); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldHaveExitValue(0); + + String stdout = output.getStdout(); + values[0] = getFlagValue(" NewSize", stdout); + values[1] = getFlagValue(" OldSize", stdout); + } + + public static void checkGenMaxHeapErgo(String gcflag) throws Exception { + TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 3); + TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 4); + TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 5); + } + + private static void checkInvalidMinInitialHeapCombinations(String gcflag) throws Exception { + expectError(new String[] { gcflag, "-Xms8M", "-XX:InitialHeapSize=4M", "-version" }); + } + + private static void checkValidMinInitialHeapCombinations(String gcflag) throws Exception { + expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms4M", "-version" }); + expectValid(new String[] { gcflag, "-Xms4M", "-XX:InitialHeapSize=8M", "-version" }); + expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms8M", "-version" }); + // the following is not an error as -Xms sets both minimal and initial heap size + expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-Xms8M", "-version" }); + } + + private static void checkInvalidInitialMaxHeapCombinations(String gcflag) throws Exception { + expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=8M", "-version" }); + expectError(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" }); + } + + private static void checkValidInitialMaxHeapCombinations(String gcflag) throws Exception { + expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" }); + expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:InitialHeapSize=4M", "-version" }); + expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=4M", "-version" }); + // a value of "0" for initial heap size means auto-detect + expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=0M", "-version" }); + } + + private static long valueAfter(String source, String match) { + int start = source.indexOf(match) + match.length(); + String tail = source.substring(start).split(" ")[0]; + return Long.parseLong(tail); + } + + private static void getMinInitialMaxHeap(String[] args, MinInitialMaxValues val) throws Exception { + ArrayList finalargs = new ArrayList(); + + String[] testVMopts = new String[] { + "-Xbootclasspath/a:.", + "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", + "-cp", System.getProperty("java.class.path"), + }; + // System.getProperty("test.java.opts") is '' if no options is set, + // we need to skip such a result + String[] externalVMOpts = new String[0]; + if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) { + externalVMOpts = System.getProperty("test.java.opts").split(" "); + } + + finalargs.addAll(Arrays.asList(externalVMOpts)); + finalargs.addAll(Arrays.asList(args)); + finalargs.addAll(Arrays.asList(testVMopts)); + finalargs.add(ErgoArgsPrinter.class.getName()); + + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0])); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldHaveExitValue(0); + + // the output we watch for has the following format: + // + // "Minimum heap X Initial heap Y Maximum heap Z Min alignment A Max Alignment B" + // + // where A, B, X, Y and Z are sizes in bytes. + // Unfortunately there is no other way to retrieve the minimum heap size and + // the alignments. + + Matcher m = Pattern.compile("Minimum heap \\d+ Initial heap \\d+ Maximum heap \\d+ Min alignment \\d+ Max alignment \\d+"). + matcher(output.getStdout()); + if (!m.find()) { + throw new RuntimeException("Could not find heap size string."); + } + + String match = m.group(); + + // actual values + val.minHeapSize = valueAfter(match, "Minimum heap "); + val.initialHeapSize = valueAfter(match, "Initial heap "); + val.maxHeapSize = valueAfter(match, "Maximum heap "); + val.minAlignment = valueAfter(match, "Min alignment "); + val.maxAlignment = valueAfter(match, "Max alignment "); + } + + /** + * Verify whether the VM automatically synchronizes minimum and initial heap size if only + * one is given for the GC specified. + */ + public static void checkErgonomics(String[] args, long[] newoldsize, + long expectedMin, long expectedInitial) throws Exception { + + MinInitialMaxValues v = new MinInitialMaxValues(); + getMinInitialMaxHeap(args, v); + + if ((expectedMin != -1) && (align_up(expectedMin, v.minAlignment) != v.minHeapSize)) { + throw new RuntimeException("Actual minimum heap size of " + v.minHeapSize + + " differs from expected minimum heap size of " + expectedMin); + } + + if ((expectedInitial != -1) && (align_up(expectedInitial, v.minAlignment) != v.initialHeapSize)) { + throw new RuntimeException("Actual initial heap size of " + v.initialHeapSize + + " differs from expected initial heap size of " + expectedInitial); + } + + // always check the invariant min <= initial <= max heap size + if (!(v.minHeapSize <= v.initialHeapSize && v.initialHeapSize <= v.maxHeapSize)) { + throw new RuntimeException("Inconsistent min/initial/max heap sizes, they are " + + v.minHeapSize + "/" + v.initialHeapSize + "/" + v.maxHeapSize); + } + } + + /** + * Verify whether the VM respects the given maximum heap size in MB for the + * GC specified. + * @param gcflag The garbage collector to test as command line flag. E.g. -XX:+UseG1GC + * @param maxHeapSize the maximum heap size to verify, in MB. + */ + public static void checkGenMaxHeapSize(String gcflag, long maxHeapsize) throws Exception { + final long K = 1024; + + MinInitialMaxValues v = new MinInitialMaxValues(); + getMinInitialMaxHeap(new String[] { gcflag, "-XX:MaxHeapSize=" + maxHeapsize + "M" }, v); + + long expectedHeapSize = align_up(maxHeapsize * K * K, v.maxAlignment); + long actualHeapSize = v.maxHeapSize; + + if (actualHeapSize > expectedHeapSize) { + throw new RuntimeException("Heap has " + actualHeapSize + + " bytes, expected to be less than " + expectedHeapSize); + } + } + + private static long getFlagValue(String flag, String where) { + Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where); + if (!m.find()) { + throw new RuntimeException("Could not find value for flag " + flag + " in output string"); + } + String match = m.group(); + return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length())); + } + + private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception { + if (contains) { + output.shouldContain(message); + } else { + output.shouldNotContain(message); + } + } + + private static void expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception { + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + shouldContainOrNot(output, hasWarning, "Warning"); + shouldContainOrNot(output, hasError, "Error"); + output.shouldHaveExitValue(errorcode); + } + + private static void expectError(String[] flags) throws Exception { + expect(flags, false, true, 1); + } + + private static void expectValid(String[] flags) throws Exception { + expect(flags, false, false, 0); + } +} + --- /dev/null 2013-04-16 09:14:15.018414535 +0200 +++ new/test/gc/8006088/TestMinInitialErgonomics.java 2013-04-17 10:26:49.749201612 +0200 @@ -0,0 +1,45 @@ +/* +* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* This code is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License version 2 only, as +* published by the Free Software Foundation. +* +* This code is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* version 2 for more details (a copy is included in the LICENSE file that +* accompanied this code). +* +* You should have received a copy of the GNU General Public License version +* 2 along with this work; if not, write to the Free Software Foundation, +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +* +* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +* or visit www.oracle.com if you need additional information or have any +* questions. +*/ + +/** + * @test TestMinInitialErgonomics + * @key gc + * @bug 8006088 + * @summary Test ergonomics decisions related to minimum and initial heap size. + * @library /testlibrary /testlibrary/whitebox + * @build TestMinInitialErgonomics TestMaxHeapSizeTools + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * @run main/othervm TestMinInitialErgonomics + * @author thomas.schatzl@oracle.com + */ + +public class TestMinInitialErgonomics { + + public static void main(String args[]) throws Exception { + final String gcName = "-XX:+UseParallelGC"; + // check ergonomic decisions about minimum and initial heap size in + // a single gc only as ergonomics are the same everywhere. + TestMaxHeapSizeTools.checkMinInitialErgonomics(gcName); + } +} + --- /dev/null 2013-04-16 09:14:15.018414535 +0200 +++ new/test/gc/8006088/TestParallel.java 2013-04-17 10:26:50.185201594 +0200 @@ -0,0 +1,49 @@ +/* +* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* This code is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License version 2 only, as +* published by the Free Software Foundation. +* +* This code is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* version 2 for more details (a copy is included in the LICENSE file that +* accompanied this code). +* +* You should have received a copy of the GNU General Public License version +* 2 along with this work; if not, write to the Free Software Foundation, +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +* +* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +* or visit www.oracle.com if you need additional information or have any +* questions. +*/ + +/* + * @test TestParallel + * @key gc + * @bug 8006088 + * @summary Tests argument processing for initial and maximum heap size for the + * parallel collectors. + * @library /testlibrary /testlibrary/whitebox + * @build TestParallel TestMaxHeapSizeTools + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * @run main/othervm TestParallel + * @author thomas.schatzl@oracle.com + */ + +public class TestParallel { + + public static void main(String args[]) throws Exception { + // just pick one of the parallel generational collectors. Sizing logic is the + // same. + final String gcName = "-XX:+UseParallelOldGC"; + + TestMaxHeapSizeTools.checkMinInitialMaxHeapFlags(gcName); + + TestMaxHeapSizeTools.checkGenMaxHeapErgo(gcName); + } +} + --- /dev/null 2013-04-16 09:14:15.018414535 +0200 +++ new/test/gc/8006088/TestSerial.java 2013-04-17 10:26:50.609201576 +0200 @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* This code is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License version 2 only, as +* published by the Free Software Foundation. +* +* This code is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* version 2 for more details (a copy is included in the LICENSE file that +* accompanied this code). +* +* You should have received a copy of the GNU General Public License version +* 2 along with this work; if not, write to the Free Software Foundation, +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +* +* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +* or visit www.oracle.com if you need additional information or have any +* questions. +*/ + +/* + * @test TestSerial + * @key gc + * @bug 8006088 + * @summary Tests argument processing for initial and maximum heap size for the Serial collector + * @library /testlibrary /testlibrary/whitebox + * @build TestSerial TestMaxHeapSizeTools + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * @run main/othervm TestSerial + * @author thomas.schatzl@oracle.com + */ + +public class TestSerial { + + public static void main(String args[]) throws Exception { + final String gcName = "-XX:+UseSerialGC"; + + TestMaxHeapSizeTools.checkMinInitialMaxHeapFlags(gcName); + + TestMaxHeapSizeTools.checkGenMaxHeapErgo(gcName); + } +} +