Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/arguments/TestUseCompressedOopsErgoTools.java
32285 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import sun.management.ManagementFactoryHelper;24import com.sun.management.HotSpotDiagnosticMXBean;25import com.sun.management.VMOption;2627import java.util.regex.Matcher;28import java.util.regex.Pattern;29import java.util.ArrayList;30import java.util.Arrays;3132import com.oracle.java.testlibrary.*;33import sun.hotspot.WhiteBox;3435class DetermineMaxHeapForCompressedOops {36public static void main(String[] args) throws Exception {37WhiteBox wb = WhiteBox.getWhiteBox();38System.out.print(wb.getCompressedOopsMaxHeapSize());39}40}4142class TestUseCompressedOopsErgoTools {4344private static long getCompressedClassSpaceSize() {45HotSpotDiagnosticMXBean diagnostic = ManagementFactoryHelper.getDiagnosticMXBean();4647VMOption option = diagnostic.getVMOption("CompressedClassSpaceSize");48return Long.parseLong(option.getValue());49}505152public static long getMaxHeapForCompressedOops(String[] vmargs) throws Exception {53OutputAnalyzer output = runWhiteBoxTest(vmargs, DetermineMaxHeapForCompressedOops.class.getName(), new String[] {}, false);54return Long.parseLong(output.getStdout());55}5657public static boolean is64bitVM() {58String val = System.getProperty("sun.arch.data.model");59if (val == null) {60throw new RuntimeException("Could not read sun.arch.data.model");61}62if (val.equals("64")) {63return true;64} else if (val.equals("32")) {65return false;66}67throw new RuntimeException("Unexpected value " + val + " of sun.arch.data.model");68}6970/**71* Executes a new VM process with the given class and parameters.72* @param vmargs Arguments to the VM to run73* @param classname Name of the class to run74* @param arguments Arguments to the class75* @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string76* @return The OutputAnalyzer with the results for the invocation.77*/78public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception {79ArrayList<String> finalargs = new ArrayList<String>();8081String[] whiteboxOpts = new String[] {82"-Xbootclasspath/a:.",83"-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",84"-cp", System.getProperty("java.class.path"),85};8687if (useTestDotJavaDotOpts) {88// System.getProperty("test.java.opts") is '' if no options is set,89// we need to skip such a result90String[] externalVMOpts = new String[0];91if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) {92externalVMOpts = System.getProperty("test.java.opts").split(" ");93}94finalargs.addAll(Arrays.asList(externalVMOpts));95}9697finalargs.addAll(Arrays.asList(vmargs));98finalargs.addAll(Arrays.asList(whiteboxOpts));99finalargs.add(classname);100finalargs.addAll(Arrays.asList(arguments));101102ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));103OutputAnalyzer output = new OutputAnalyzer(pb.start());104output.shouldHaveExitValue(0);105return output;106}107108private static String[] join(String[] part1, String part2) {109ArrayList<String> result = new ArrayList<String>();110result.addAll(Arrays.asList(part1));111result.add(part2);112return result.toArray(new String[0]);113}114115public static void checkCompressedOopsErgo(String[] gcflags) throws Exception {116long maxHeapForCompressedOops = getMaxHeapForCompressedOops(gcflags);117118checkUseCompressedOops(gcflags, maxHeapForCompressedOops, true);119checkUseCompressedOops(gcflags, maxHeapForCompressedOops - 1, true);120checkUseCompressedOops(gcflags, maxHeapForCompressedOops + 1, false);121122// the use of HeapBaseMinAddress should not change the outcome123checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops, true);124checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops - 1, true);125checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops + 1, false);126127// use a different object alignment128maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"));129130checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops, true);131checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops - 1, true);132checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops + 1, false);133134// use a different CompressedClassSpaceSize135String compressedClassSpaceSizeArg = "-XX:CompressedClassSpaceSize=" + 2 * getCompressedClassSpaceSize();136maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, compressedClassSpaceSizeArg));137138checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops, true);139checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops - 1, true);140checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops + 1, false);141}142143private static void checkUseCompressedOops(String[] args, long heapsize, boolean expectUseCompressedOops) throws Exception {144ArrayList<String> finalargs = new ArrayList<String>();145finalargs.addAll(Arrays.asList(args));146finalargs.add("-Xmx" + heapsize);147finalargs.add("-XX:+PrintFlagsFinal");148finalargs.add("-version");149150String output = expectValid(finalargs.toArray(new String[0]));151152boolean actualUseCompressedOops = getFlagBoolValue(" UseCompressedOops", output);153154Asserts.assertEQ(expectUseCompressedOops, actualUseCompressedOops);155}156157private static boolean getFlagBoolValue(String flag, String where) {158Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where);159if (!m.find()) {160throw new RuntimeException("Could not find value for flag " + flag + " in output string");161}162return m.group(1).equals("true");163}164165private static String expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {166ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);167OutputAnalyzer output = new OutputAnalyzer(pb.start());168output.shouldHaveExitValue(errorcode);169return output.getStdout();170}171172private static String expectValid(String[] flags) throws Exception {173return expect(flags, false, false, 0);174}175}176177178179