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