test/gc/arguments/TestUseCompressedOopsErgoTools.java

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 5717
2f426063daea
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 import sun.management.ManagementFactoryHelper;
aoqi@0 25 import com.sun.management.HotSpotDiagnosticMXBean;
aoqi@0 26 import com.sun.management.VMOption;
aoqi@0 27
aoqi@0 28 import java.util.regex.Matcher;
aoqi@0 29 import java.util.regex.Pattern;
aoqi@0 30 import java.util.ArrayList;
aoqi@0 31 import java.util.Arrays;
aoqi@0 32
aoqi@0 33 import com.oracle.java.testlibrary.*;
aoqi@0 34 import sun.hotspot.WhiteBox;
aoqi@0 35
aoqi@0 36 class DetermineMaxHeapForCompressedOops {
aoqi@0 37 public static void main(String[] args) throws Exception {
aoqi@0 38 WhiteBox wb = WhiteBox.getWhiteBox();
aoqi@0 39 System.out.print(wb.getCompressedOopsMaxHeapSize());
aoqi@0 40 }
aoqi@0 41 }
aoqi@0 42
aoqi@0 43 class TestUseCompressedOopsErgoTools {
aoqi@0 44
aoqi@0 45 private static long getCompressedClassSpaceSize() {
aoqi@0 46 HotSpotDiagnosticMXBean diagnostic = ManagementFactoryHelper.getDiagnosticMXBean();
aoqi@0 47
aoqi@0 48 VMOption option = diagnostic.getVMOption("CompressedClassSpaceSize");
aoqi@0 49 return Long.parseLong(option.getValue());
aoqi@0 50 }
aoqi@0 51
aoqi@0 52
aoqi@0 53 public static long getMaxHeapForCompressedOops(String[] vmargs) throws Exception {
aoqi@0 54 OutputAnalyzer output = runWhiteBoxTest(vmargs, DetermineMaxHeapForCompressedOops.class.getName(), new String[] {}, false);
aoqi@0 55 return Long.parseLong(output.getStdout());
aoqi@0 56 }
aoqi@0 57
aoqi@0 58 public static boolean is64bitVM() {
aoqi@0 59 String val = System.getProperty("sun.arch.data.model");
aoqi@0 60 if (val == null) {
aoqi@0 61 throw new RuntimeException("Could not read sun.arch.data.model");
aoqi@0 62 }
aoqi@0 63 if (val.equals("64")) {
aoqi@0 64 return true;
aoqi@0 65 } else if (val.equals("32")) {
aoqi@0 66 return false;
aoqi@0 67 }
aoqi@0 68 throw new RuntimeException("Unexpected value " + val + " of sun.arch.data.model");
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 /**
aoqi@0 72 * Executes a new VM process with the given class and parameters.
aoqi@0 73 * @param vmargs Arguments to the VM to run
aoqi@0 74 * @param classname Name of the class to run
aoqi@0 75 * @param arguments Arguments to the class
aoqi@0 76 * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string
aoqi@0 77 * @return The OutputAnalyzer with the results for the invocation.
aoqi@0 78 */
aoqi@0 79 public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception {
aoqi@0 80 ArrayList<String> finalargs = new ArrayList<String>();
aoqi@0 81
aoqi@0 82 String[] whiteboxOpts = new String[] {
aoqi@0 83 "-Xbootclasspath/a:.",
aoqi@0 84 "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
aoqi@0 85 "-cp", System.getProperty("java.class.path"),
aoqi@0 86 };
aoqi@0 87
aoqi@0 88 if (useTestDotJavaDotOpts) {
aoqi@0 89 // System.getProperty("test.java.opts") is '' if no options is set,
aoqi@0 90 // we need to skip such a result
aoqi@0 91 String[] externalVMOpts = new String[0];
aoqi@0 92 if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) {
aoqi@0 93 externalVMOpts = System.getProperty("test.java.opts").split(" ");
aoqi@0 94 }
aoqi@0 95 finalargs.addAll(Arrays.asList(externalVMOpts));
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 finalargs.addAll(Arrays.asList(vmargs));
aoqi@0 99 finalargs.addAll(Arrays.asList(whiteboxOpts));
aoqi@0 100 finalargs.add(classname);
aoqi@0 101 finalargs.addAll(Arrays.asList(arguments));
aoqi@0 102
aoqi@0 103 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
aoqi@0 104 OutputAnalyzer output = new OutputAnalyzer(pb.start());
aoqi@0 105 output.shouldHaveExitValue(0);
aoqi@0 106 return output;
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 private static String[] join(String[] part1, String part2) {
aoqi@0 110 ArrayList<String> result = new ArrayList<String>();
aoqi@0 111 result.addAll(Arrays.asList(part1));
aoqi@0 112 result.add(part2);
aoqi@0 113 return result.toArray(new String[0]);
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 public static void checkCompressedOopsErgo(String[] gcflags) throws Exception {
aoqi@0 117 long maxHeapForCompressedOops = getMaxHeapForCompressedOops(gcflags);
aoqi@0 118
aoqi@0 119 checkUseCompressedOops(gcflags, maxHeapForCompressedOops, true);
aoqi@0 120 checkUseCompressedOops(gcflags, maxHeapForCompressedOops - 1, true);
aoqi@0 121 checkUseCompressedOops(gcflags, maxHeapForCompressedOops + 1, false);
aoqi@0 122
aoqi@0 123 // the use of HeapBaseMinAddress should not change the outcome
aoqi@0 124 checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops, true);
aoqi@0 125 checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops - 1, true);
aoqi@0 126 checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops + 1, false);
aoqi@0 127
aoqi@0 128 // use a different object alignment
aoqi@0 129 maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"));
aoqi@0 130
aoqi@0 131 checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops, true);
aoqi@0 132 checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops - 1, true);
aoqi@0 133 checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops + 1, false);
aoqi@0 134
aoqi@0 135 // use a different CompressedClassSpaceSize
aoqi@0 136 String compressedClassSpaceSizeArg = "-XX:CompressedClassSpaceSize=" + 2 * getCompressedClassSpaceSize();
aoqi@0 137 maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, compressedClassSpaceSizeArg));
aoqi@0 138
aoqi@0 139 checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops, true);
aoqi@0 140 checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops - 1, true);
aoqi@0 141 checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops + 1, false);
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 private static void checkUseCompressedOops(String[] args, long heapsize, boolean expectUseCompressedOops) throws Exception {
aoqi@0 145 ArrayList<String> finalargs = new ArrayList<String>();
aoqi@0 146 finalargs.addAll(Arrays.asList(args));
aoqi@0 147 finalargs.add("-Xmx" + heapsize);
aoqi@0 148 finalargs.add("-XX:+PrintFlagsFinal");
aoqi@0 149 finalargs.add("-version");
aoqi@0 150
aoqi@0 151 String output = expectValid(finalargs.toArray(new String[0]));
aoqi@0 152
aoqi@0 153 boolean actualUseCompressedOops = getFlagBoolValue(" UseCompressedOops", output);
aoqi@0 154
aoqi@0 155 Asserts.assertEQ(expectUseCompressedOops, actualUseCompressedOops);
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 private static boolean getFlagBoolValue(String flag, String where) {
aoqi@0 159 Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where);
aoqi@0 160 if (!m.find()) {
aoqi@0 161 throw new RuntimeException("Could not find value for flag " + flag + " in output string");
aoqi@0 162 }
aoqi@0 163 return m.group(1).equals("true");
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 private static String expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {
aoqi@0 167 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);
aoqi@0 168 OutputAnalyzer output = new OutputAnalyzer(pb.start());
aoqi@0 169 output.shouldHaveExitValue(errorcode);
aoqi@0 170 return output.getStdout();
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 private static String expectValid(String[] flags) throws Exception {
aoqi@0 174 return expect(flags, false, false, 0);
aoqi@0 175 }
aoqi@0 176 }
aoqi@0 177

mercurial