test/gc/arguments/TestUseCompressedOopsErgoTools.java

changeset 5701
40136aa2cdb1
child 5717
2f426063daea
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/gc/arguments/TestUseCompressedOopsErgoTools.java	Wed Sep 11 16:25:02 2013 +0200
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*
     1.5 +* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 +*
     1.8 +* This code is free software; you can redistribute it and/or modify it
     1.9 +* under the terms of the GNU General Public License version 2 only, as
    1.10 +* published by the Free Software Foundation.
    1.11 +*
    1.12 +* This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 +* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 +* version 2 for more details (a copy is included in the LICENSE file that
    1.16 +* accompanied this code).
    1.17 +*
    1.18 +* You should have received a copy of the GNU General Public License version
    1.19 +* 2 along with this work; if not, write to the Free Software Foundation,
    1.20 +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 +*
    1.22 +* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 +* or visit www.oracle.com if you need additional information or have any
    1.24 +* questions.
    1.25 +*/
    1.26 +
    1.27 +import sun.management.ManagementFactoryHelper;
    1.28 +import com.sun.management.HotSpotDiagnosticMXBean;
    1.29 +import com.sun.management.VMOption;
    1.30 +
    1.31 +import java.util.regex.Matcher;
    1.32 +import java.util.regex.Pattern;
    1.33 +import java.util.ArrayList;
    1.34 +import java.util.Arrays;
    1.35 +
    1.36 +import com.oracle.java.testlibrary.*;
    1.37 +import sun.hotspot.WhiteBox;
    1.38 +
    1.39 +class DetermineMaxHeapForCompressedOops {
    1.40 +  public static void main(String[] args) throws Exception {
    1.41 +    WhiteBox wb = WhiteBox.getWhiteBox();
    1.42 +    System.out.print(wb.getCompressedOopsMaxHeapSize());
    1.43 +  }
    1.44 +}
    1.45 +
    1.46 +class TestUseCompressedOopsErgoTools {
    1.47 +
    1.48 +  private static long getClassMetaspaceSize() {
    1.49 +    HotSpotDiagnosticMXBean diagnostic = ManagementFactoryHelper.getDiagnosticMXBean();
    1.50 +
    1.51 +    VMOption option = diagnostic.getVMOption("ClassMetaspaceSize");
    1.52 +    return Long.parseLong(option.getValue());
    1.53 +  }
    1.54 +
    1.55 +
    1.56 +  public static long getMaxHeapForCompressedOops(String[] vmargs) throws Exception {
    1.57 +    OutputAnalyzer output = runWhiteBoxTest(vmargs, DetermineMaxHeapForCompressedOops.class.getName(), new String[] {}, false);
    1.58 +    return Long.parseLong(output.getStdout());
    1.59 +  }
    1.60 +
    1.61 +  public static boolean is64bitVM() {
    1.62 +    String val = System.getProperty("sun.arch.data.model");
    1.63 +    if (val == null) {
    1.64 +      throw new RuntimeException("Could not read sun.arch.data.model");
    1.65 +    }
    1.66 +    if (val.equals("64")) {
    1.67 +      return true;
    1.68 +    } else if (val.equals("32")) {
    1.69 +      return false;
    1.70 +    }
    1.71 +    throw new RuntimeException("Unexpected value " + val + " of sun.arch.data.model");
    1.72 +  }
    1.73 +
    1.74 +  /**
    1.75 +   * Executes a new VM process with the given class and parameters.
    1.76 +   * @param vmargs Arguments to the VM to run
    1.77 +   * @param classname Name of the class to run
    1.78 +   * @param arguments Arguments to the class
    1.79 +   * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string
    1.80 +   * @return The OutputAnalyzer with the results for the invocation.
    1.81 +   */
    1.82 +  public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception {
    1.83 +    ArrayList<String> finalargs = new ArrayList<String>();
    1.84 +
    1.85 +    String[] whiteboxOpts = new String[] {
    1.86 +      "-Xbootclasspath/a:.",
    1.87 +      "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
    1.88 +      "-cp", System.getProperty("java.class.path"),
    1.89 +    };
    1.90 +
    1.91 +    if (useTestDotJavaDotOpts) {
    1.92 +      // System.getProperty("test.java.opts") is '' if no options is set,
    1.93 +      // we need to skip such a result
    1.94 +      String[] externalVMOpts = new String[0];
    1.95 +      if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) {
    1.96 +        externalVMOpts = System.getProperty("test.java.opts").split(" ");
    1.97 +      }
    1.98 +      finalargs.addAll(Arrays.asList(externalVMOpts));
    1.99 +    }
   1.100 +
   1.101 +    finalargs.addAll(Arrays.asList(vmargs));
   1.102 +    finalargs.addAll(Arrays.asList(whiteboxOpts));
   1.103 +    finalargs.add(classname);
   1.104 +    finalargs.addAll(Arrays.asList(arguments));
   1.105 +
   1.106 +    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
   1.107 +    OutputAnalyzer output = new OutputAnalyzer(pb.start());
   1.108 +    output.shouldHaveExitValue(0);
   1.109 +    return output;
   1.110 +  }
   1.111 +
   1.112 +  private static String[] join(String[] part1, String part2) {
   1.113 +    ArrayList<String> result = new ArrayList<String>();
   1.114 +    result.addAll(Arrays.asList(part1));
   1.115 +    result.add(part2);
   1.116 +    return result.toArray(new String[0]);
   1.117 +  }
   1.118 +
   1.119 +  public static void checkCompressedOopsErgo(String[] gcflags) throws Exception {
   1.120 +    long maxHeapForCompressedOops = getMaxHeapForCompressedOops(gcflags);
   1.121 +
   1.122 +    checkUseCompressedOops(gcflags, maxHeapForCompressedOops, true);
   1.123 +    checkUseCompressedOops(gcflags, maxHeapForCompressedOops - 1, true);
   1.124 +    checkUseCompressedOops(gcflags, maxHeapForCompressedOops + 1, false);
   1.125 +
   1.126 +    // the use of HeapBaseMinAddress should not change the outcome
   1.127 +    checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops, true);
   1.128 +    checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops - 1, true);
   1.129 +    checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops + 1, false);
   1.130 +
   1.131 +    // use a different object alignment
   1.132 +    maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"));
   1.133 +
   1.134 +    checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops, true);
   1.135 +    checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops - 1, true);
   1.136 +    checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops + 1, false);
   1.137 +
   1.138 +    // use a different ClassMetaspaceSize
   1.139 +    String classMetaspaceSizeArg = "-XX:ClassMetaspaceSize=" + 2 * getClassMetaspaceSize();
   1.140 +    maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, classMetaspaceSizeArg));
   1.141 +
   1.142 +    checkUseCompressedOops(join(gcflags, classMetaspaceSizeArg), maxHeapForCompressedOops, true);
   1.143 +    checkUseCompressedOops(join(gcflags, classMetaspaceSizeArg), maxHeapForCompressedOops - 1, true);
   1.144 +    checkUseCompressedOops(join(gcflags, classMetaspaceSizeArg), maxHeapForCompressedOops + 1, false);
   1.145 +  }
   1.146 +
   1.147 +  private static void checkUseCompressedOops(String[] args, long heapsize, boolean expectUseCompressedOops) throws Exception {
   1.148 +     ArrayList<String> finalargs = new ArrayList<String>();
   1.149 +     finalargs.addAll(Arrays.asList(args));
   1.150 +     finalargs.add("-Xmx" + heapsize);
   1.151 +     finalargs.add("-XX:+PrintFlagsFinal");
   1.152 +     finalargs.add("-version");
   1.153 +
   1.154 +     String output = expectValid(finalargs.toArray(new String[0]));
   1.155 +
   1.156 +     boolean actualUseCompressedOops = getFlagBoolValue(" UseCompressedOops", output);
   1.157 +
   1.158 +     if (expectUseCompressedOops != actualUseCompressedOops) {
   1.159 +       throw new RuntimeException("Expected use of compressed oops: " + expectUseCompressedOops + " but was: " + actualUseCompressedOops);
   1.160 +     }
   1.161 +  }
   1.162 +
   1.163 +  private static boolean getFlagBoolValue(String flag, String where) {
   1.164 +    Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where);
   1.165 +    if (!m.find()) {
   1.166 +      throw new RuntimeException("Could not find value for flag " + flag + " in output string");
   1.167 +    }
   1.168 +    String match = m.group(1).equals("true");
   1.169 +  }
   1.170 +
   1.171 +  private static String expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {
   1.172 +    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);
   1.173 +    OutputAnalyzer output = new OutputAnalyzer(pb.start());
   1.174 +    output.shouldHaveExitValue(errorcode);
   1.175 +    return output.getStdout();
   1.176 +  }
   1.177 +
   1.178 +  private static String expectValid(String[] flags) throws Exception {
   1.179 +    return expect(flags, false, false, 0);
   1.180 +  }
   1.181 +}
   1.182 +

mercurial