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 import sun.management.ManagementFactoryHelper; 25 import com.sun.management.HotSpotDiagnosticMXBean; 26 import com.sun.management.VMOption; 27 28 import java.util.regex.Matcher; 29 import java.util.regex.Pattern; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 33 import com.oracle.java.testlibrary.*; 34 import sun.hotspot.WhiteBox; 35 36 class DetermineMaxHeapForCompressedOops { 37 public static void main(String[] args) throws Exception { 38 WhiteBox wb = WhiteBox.getWhiteBox(); 39 System.out.print(wb.getCompressedOopsMaxHeapSize()); 40 } 41 } 42 43 class TestUseCompressedOopsErgoTools { 44 45 private static long getCompressedClassSpaceSize() { 46 HotSpotDiagnosticMXBean diagnostic = ManagementFactoryHelper.getDiagnosticMXBean(); 47 48 VMOption option = diagnostic.getVMOption("CompressedClassSpaceSize"); 49 return Long.parseLong(option.getValue()); 50 } 51 52 53 public static long getMaxHeapForCompressedOops(String[] vmargs) throws Exception { 54 OutputAnalyzer output = runWhiteBoxTest(vmargs, DetermineMaxHeapForCompressedOops.class.getName(), new String[] {}, false); 55 return Long.parseLong(output.getStdout()); 56 } 57 58 public static boolean is64bitVM() { 59 String val = System.getProperty("sun.arch.data.model"); 60 if (val == null) { 61 throw new RuntimeException("Could not read sun.arch.data.model"); 62 } 63 if (val.equals("64")) { 64 return true; 65 } else if (val.equals("32")) { 66 return false; 67 } 68 throw new RuntimeException("Unexpected value " + val + " of sun.arch.data.model"); 69 } 70 71 /** 72 * Executes a new VM process with the given class and parameters. 73 * @param vmargs Arguments to the VM to run 74 * @param classname Name of the class to run 75 * @param arguments Arguments to the class 76 * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string 77 * @return The OutputAnalyzer with the results for the invocation. 78 */ 79 public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception { 80 ArrayList<String> finalargs = new ArrayList<String>(); 81 82 String[] whiteboxOpts = new String[] { 83 "-Xbootclasspath/a:.", 84 "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", 85 "-cp", System.getProperty("java.class.path"), 86 }; 87 88 if (useTestDotJavaDotOpts) { 89 // System.getProperty("test.java.opts") is '' if no options is set, 90 // we need to skip such a result 91 String[] externalVMOpts = new String[0]; 92 if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) { 93 externalVMOpts = System.getProperty("test.java.opts").split(" "); 94 } 95 finalargs.addAll(Arrays.asList(externalVMOpts)); 96 } 97 98 finalargs.addAll(Arrays.asList(vmargs)); 99 finalargs.addAll(Arrays.asList(whiteboxOpts)); 100 finalargs.add(classname); 101 finalargs.addAll(Arrays.asList(arguments)); 102 103 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0])); 104 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 105 output.shouldHaveExitValue(0); 106 return output; 107 } 108 109 private static String[] join(String[] part1, String part2) { 110 ArrayList<String> result = new ArrayList<String>(); 111 result.addAll(Arrays.asList(part1)); 112 result.add(part2); 113 return result.toArray(new String[0]); 114 } 115 116 public static void checkCompressedOopsErgo(String[] gcflags) throws Exception { 117 long maxHeapForCompressedOops = getMaxHeapForCompressedOops(gcflags); 118 119 checkUseCompressedOops(gcflags, maxHeapForCompressedOops, true); 120 checkUseCompressedOops(gcflags, maxHeapForCompressedOops - 1, true); 121 checkUseCompressedOops(gcflags, maxHeapForCompressedOops + 1, false); 122 123 // the use of HeapBaseMinAddress should not change the outcome 124 checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops, true); 125 checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops - 1, true); 126 checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops + 1, false); 127 128 // use a different object alignment 129 maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16")); 130 131 checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops, true); 132 checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops - 1, true); 133 checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops + 1, false); 134 135 // use a different CompressedClassSpaceSize 136 String compressedClassSpaceSizeArg = "-XX:CompressedClassSpaceSize=" + 2 * getCompressedClassSpaceSize(); 137 maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, compressedClassSpaceSizeArg)); 138 139 checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops, true); 140 checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops - 1, true); 141 checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops + 1, false); 142 } 143 144 private static void checkUseCompressedOops(String[] args, long heapsize, boolean expectUseCompressedOops) throws Exception { 145 ArrayList<String> finalargs = new ArrayList<String>(); 146 finalargs.addAll(Arrays.asList(args)); 147 finalargs.add("-Xmx" + heapsize); 148 finalargs.add("-XX:+PrintFlagsFinal"); 149 finalargs.add("-version"); 150 151 String output = expectValid(finalargs.toArray(new String[0])); 152 153 boolean actualUseCompressedOops = getFlagBoolValue(" UseCompressedOops", output); 154 155 Asserts.assertEQ(expectUseCompressedOops, actualUseCompressedOops); 156 } 157 158 private static boolean getFlagBoolValue(String flag, String where) { 159 Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where); 160 if (!m.find()) { 161 throw new RuntimeException("Could not find value for flag " + flag + " in output string"); 162 } 163 return m.group(1).equals("true"); 164 } 165 166 private static String expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception { 167 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags); 168 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 169 output.shouldHaveExitValue(errorcode); 170 return output.getStdout(); 171 } 172 173 private static String expectValid(String[] flags) throws Exception { 174 return expect(flags, false, false, 0); 175 } 176 } 177