--- /dev/null 2013-03-06 12:38:34.589973472 +0100 +++ new/test/gc/8006088/TestMaxHeapSizeTools.java 2013-03-18 10:49:09.873736074 +0100 @@ -0,0 +1,94 @@ +/* +* 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 com.oracle.java.testlibrary.OutputAnalyzer; +import com.oracle.java.testlibrary.ProcessTools; + +class TestMaxHeapSizeTools { + + public static void checkInvalidInitialMaxHeapCombination(String gcflag) throws Exception { + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=8M", "-version"); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldNotContain("Warning"); + output.shouldContain("Error"); + output.shouldHaveExitValue(1); + } + + public static void checkEqualInitialMaxHeap(String gcflag) throws Exception { + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=4M", "-version"); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldNotContain("Warning"); + output.shouldNotContain("Error"); + output.shouldHaveExitValue(0); + } + + public static void checkValidInitialMaxHeap(String gcflag) throws Exception { + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag, "-XX:MaxHeapSize=8M", "-XX:InitialHeapSize=4M", "-version"); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldNotContain("Warning"); + output.shouldNotContain("Error"); + output.shouldHaveExitValue(0); + } + + /** + * 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, int maxHeapsize) throws Exception { + final int K = 1024; + // the alignment for the heap. This is card size * page size, i.e. 2^9 * 2^12 = 2M. + // Unfortunately there is no way to retrieve this from the VM directly. + // It is the same for all GCs that use a card table in Hotspot (i.e. all). + final long alignment = 2 * K; + + long expectedHeapSize = (maxHeapsize * K + alignment - 1) & ~(alignment - 1); + + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag, + "-XX:MaxHeapSize=" + expectedHeapSize + "K", "-XX:+PrintFlagsFinal", "-version"); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldHaveExitValue(0); + + long actualHeapSize = getFlagValue("MaxHeapSize", output.getStdout()) / K; + + if (actualHeapSize > expectedHeapSize) { + throw new RuntimeException("Heap has " + actualHeapSize + + "K , expected to less than " + expectedHeapSize + "K"); + } + } + + 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 in output string"); + } + String match = m.group(); + return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length())); + } + +} +