tschatzl@5701: /* tschatzl@5701: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. tschatzl@5701: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. tschatzl@5701: * tschatzl@5701: * This code is free software; you can redistribute it and/or modify it tschatzl@5701: * under the terms of the GNU General Public License version 2 only, as tschatzl@5701: * published by the Free Software Foundation. tschatzl@5701: * tschatzl@5701: * This code is distributed in the hope that it will be useful, but WITHOUT tschatzl@5701: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or tschatzl@5701: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License tschatzl@5701: * version 2 for more details (a copy is included in the LICENSE file that tschatzl@5701: * accompanied this code). tschatzl@5701: * tschatzl@5701: * You should have received a copy of the GNU General Public License version tschatzl@5701: * 2 along with this work; if not, write to the Free Software Foundation, tschatzl@5701: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. tschatzl@5701: * tschatzl@5701: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA tschatzl@5701: * or visit www.oracle.com if you need additional information or have any tschatzl@5701: * questions. tschatzl@5701: */ tschatzl@5701: tschatzl@5701: import sun.management.ManagementFactoryHelper; tschatzl@5701: import com.sun.management.HotSpotDiagnosticMXBean; tschatzl@5701: import com.sun.management.VMOption; tschatzl@5701: tschatzl@5701: import java.util.regex.Matcher; tschatzl@5701: import java.util.regex.Pattern; tschatzl@5701: import java.util.ArrayList; tschatzl@5701: import java.util.Arrays; tschatzl@5701: tschatzl@5701: import com.oracle.java.testlibrary.*; tschatzl@5701: import sun.hotspot.WhiteBox; tschatzl@5701: tschatzl@5701: class DetermineMaxHeapForCompressedOops { tschatzl@5701: public static void main(String[] args) throws Exception { tschatzl@5701: WhiteBox wb = WhiteBox.getWhiteBox(); tschatzl@5701: System.out.print(wb.getCompressedOopsMaxHeapSize()); tschatzl@5701: } tschatzl@5701: } tschatzl@5701: tschatzl@5701: class TestUseCompressedOopsErgoTools { tschatzl@5701: tschatzl@5717: private static long getCompressedClassSpaceSize() { tschatzl@5701: HotSpotDiagnosticMXBean diagnostic = ManagementFactoryHelper.getDiagnosticMXBean(); tschatzl@5701: tschatzl@5717: VMOption option = diagnostic.getVMOption("CompressedClassSpaceSize"); tschatzl@5701: return Long.parseLong(option.getValue()); tschatzl@5701: } tschatzl@5701: tschatzl@5701: tschatzl@5701: public static long getMaxHeapForCompressedOops(String[] vmargs) throws Exception { tschatzl@5701: OutputAnalyzer output = runWhiteBoxTest(vmargs, DetermineMaxHeapForCompressedOops.class.getName(), new String[] {}, false); tschatzl@5701: return Long.parseLong(output.getStdout()); tschatzl@5701: } tschatzl@5701: tschatzl@5701: public static boolean is64bitVM() { tschatzl@5701: String val = System.getProperty("sun.arch.data.model"); tschatzl@5701: if (val == null) { tschatzl@5701: throw new RuntimeException("Could not read sun.arch.data.model"); tschatzl@5701: } tschatzl@5701: if (val.equals("64")) { tschatzl@5701: return true; tschatzl@5701: } else if (val.equals("32")) { tschatzl@5701: return false; tschatzl@5701: } tschatzl@5701: throw new RuntimeException("Unexpected value " + val + " of sun.arch.data.model"); tschatzl@5701: } tschatzl@5701: tschatzl@5701: /** tschatzl@5701: * Executes a new VM process with the given class and parameters. tschatzl@5701: * @param vmargs Arguments to the VM to run tschatzl@5701: * @param classname Name of the class to run tschatzl@5701: * @param arguments Arguments to the class tschatzl@5701: * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string tschatzl@5701: * @return The OutputAnalyzer with the results for the invocation. tschatzl@5701: */ tschatzl@5701: public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception { tschatzl@5701: ArrayList finalargs = new ArrayList(); tschatzl@5701: tschatzl@5701: String[] whiteboxOpts = new String[] { tschatzl@5701: "-Xbootclasspath/a:.", tschatzl@5701: "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", tschatzl@5701: "-cp", System.getProperty("java.class.path"), tschatzl@5701: }; tschatzl@5701: tschatzl@5701: if (useTestDotJavaDotOpts) { tschatzl@5701: // System.getProperty("test.java.opts") is '' if no options is set, tschatzl@5701: // we need to skip such a result tschatzl@5701: String[] externalVMOpts = new String[0]; tschatzl@5701: if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) { tschatzl@5701: externalVMOpts = System.getProperty("test.java.opts").split(" "); tschatzl@5701: } tschatzl@5701: finalargs.addAll(Arrays.asList(externalVMOpts)); tschatzl@5701: } tschatzl@5701: tschatzl@5701: finalargs.addAll(Arrays.asList(vmargs)); tschatzl@5701: finalargs.addAll(Arrays.asList(whiteboxOpts)); tschatzl@5701: finalargs.add(classname); tschatzl@5701: finalargs.addAll(Arrays.asList(arguments)); tschatzl@5701: tschatzl@5701: ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0])); tschatzl@5701: OutputAnalyzer output = new OutputAnalyzer(pb.start()); tschatzl@5701: output.shouldHaveExitValue(0); tschatzl@5701: return output; tschatzl@5701: } tschatzl@5701: tschatzl@5701: private static String[] join(String[] part1, String part2) { tschatzl@5701: ArrayList result = new ArrayList(); tschatzl@5701: result.addAll(Arrays.asList(part1)); tschatzl@5701: result.add(part2); tschatzl@5701: return result.toArray(new String[0]); tschatzl@5701: } tschatzl@5701: tschatzl@5701: public static void checkCompressedOopsErgo(String[] gcflags) throws Exception { tschatzl@5701: long maxHeapForCompressedOops = getMaxHeapForCompressedOops(gcflags); tschatzl@5701: tschatzl@5701: checkUseCompressedOops(gcflags, maxHeapForCompressedOops, true); tschatzl@5701: checkUseCompressedOops(gcflags, maxHeapForCompressedOops - 1, true); tschatzl@5701: checkUseCompressedOops(gcflags, maxHeapForCompressedOops + 1, false); tschatzl@5701: tschatzl@5701: // the use of HeapBaseMinAddress should not change the outcome tschatzl@5701: checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops, true); tschatzl@5701: checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops - 1, true); tschatzl@5701: checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops + 1, false); tschatzl@5701: tschatzl@5701: // use a different object alignment tschatzl@5701: maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16")); tschatzl@5701: tschatzl@5701: checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops, true); tschatzl@5701: checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops - 1, true); tschatzl@5701: checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops + 1, false); tschatzl@5701: tschatzl@5717: // use a different CompressedClassSpaceSize tschatzl@5717: String compressedClassSpaceSizeArg = "-XX:CompressedClassSpaceSize=" + 2 * getCompressedClassSpaceSize(); tschatzl@5717: maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, compressedClassSpaceSizeArg)); tschatzl@5701: tschatzl@5717: checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops, true); tschatzl@5717: checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops - 1, true); tschatzl@5717: checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops + 1, false); tschatzl@5701: } tschatzl@5701: tschatzl@5701: private static void checkUseCompressedOops(String[] args, long heapsize, boolean expectUseCompressedOops) throws Exception { tschatzl@5701: ArrayList finalargs = new ArrayList(); tschatzl@5701: finalargs.addAll(Arrays.asList(args)); tschatzl@5701: finalargs.add("-Xmx" + heapsize); tschatzl@5701: finalargs.add("-XX:+PrintFlagsFinal"); tschatzl@5701: finalargs.add("-version"); tschatzl@5701: tschatzl@5701: String output = expectValid(finalargs.toArray(new String[0])); tschatzl@5701: tschatzl@5701: boolean actualUseCompressedOops = getFlagBoolValue(" UseCompressedOops", output); tschatzl@5701: tschatzl@5717: Asserts.assertEQ(expectUseCompressedOops, actualUseCompressedOops); tschatzl@5701: } tschatzl@5701: tschatzl@5701: private static boolean getFlagBoolValue(String flag, String where) { tschatzl@5701: Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where); tschatzl@5701: if (!m.find()) { tschatzl@5701: throw new RuntimeException("Could not find value for flag " + flag + " in output string"); tschatzl@5701: } tschatzl@5717: return m.group(1).equals("true"); tschatzl@5701: } tschatzl@5701: tschatzl@5701: private static String expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception { tschatzl@5701: ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags); tschatzl@5701: OutputAnalyzer output = new OutputAnalyzer(pb.start()); tschatzl@5701: output.shouldHaveExitValue(errorcode); tschatzl@5701: return output.getStdout(); tschatzl@5701: } tschatzl@5701: tschatzl@5701: private static String expectValid(String[] flags) throws Exception { tschatzl@5701: return expect(flags, false, false, 0); tschatzl@5701: } tschatzl@5701: } tschatzl@5701: