Path: blob/master/test/hotspot/jtreg/gc/arguments/TestUseCompressedOopsErgoTools.java
40943 views
/*1* Copyright (c) 2013, 2020, 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*/2223package gc.arguments;2425import com.sun.management.HotSpotDiagnosticMXBean;26import com.sun.management.VMOption;2728import java.util.regex.Matcher;29import java.util.regex.Pattern;30import java.util.ArrayList;31import java.util.Arrays;3233import jdk.test.lib.Asserts;34import jdk.test.lib.process.ProcessTools;35import jdk.test.lib.process.OutputAnalyzer;36import java.lang.management.ManagementFactory;37import sun.hotspot.WhiteBox;3839class DetermineMaxHeapForCompressedOops {40public static void main(String[] args) throws Exception {41WhiteBox wb = WhiteBox.getWhiteBox();42System.out.print(wb.getCompressedOopsMaxHeapSize());43}44}4546class TestUseCompressedOopsErgoTools {4748private static long getCompressedClassSpaceSize() {49HotSpotDiagnosticMXBean diagnostic =50ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);5152VMOption option = diagnostic.getVMOption("CompressedClassSpaceSize");53return Long.parseLong(option.getValue());54}555657public static long getMaxHeapForCompressedOops(String[] vmargs) throws Exception {58OutputAnalyzer output = runWhiteBoxTest(vmargs, DetermineMaxHeapForCompressedOops.class.getName(), new String[] {});59return Long.parseLong(output.getStdout());60}6162public static boolean is64bitVM() {63String val = System.getProperty("sun.arch.data.model");64if (val == null) {65throw new RuntimeException("Could not read sun.arch.data.model");66}67if (val.equals("64")) {68return true;69} else if (val.equals("32")) {70return false;71}72throw new RuntimeException("Unexpected value " + val + " of sun.arch.data.model");73}7475/**76* Executes a new VM process with the given class and parameters.77* @param vmargs Arguments to the VM to run78* @param classname Name of the class to run79* @param arguments Arguments to the class80* @return The OutputAnalyzer with the results for the invocation.81*/82public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments) throws Exception {83ArrayList<String> finalargs = new ArrayList<String>();8485String[] whiteboxOpts = new String[] {86"-Xbootclasspath/a:.",87"-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",88"-cp", System.getProperty("java.class.path"),89};9091finalargs.addAll(Arrays.asList(vmargs));92finalargs.addAll(Arrays.asList(whiteboxOpts));93finalargs.add(classname);94finalargs.addAll(Arrays.asList(arguments));9596ProcessBuilder pb = GCArguments.createJavaProcessBuilder(finalargs);97OutputAnalyzer output = new OutputAnalyzer(pb.start());98output.shouldHaveExitValue(0);99return output;100}101102private static String[] join(String[] part1, String part2) {103ArrayList<String> result = new ArrayList<String>();104result.addAll(Arrays.asList(part1));105result.add(part2);106return result.toArray(String[]::new);107}108109public static void checkCompressedOopsErgo(String[] gcflags) throws Exception {110long maxHeapForCompressedOops = getMaxHeapForCompressedOops(gcflags);111112checkUseCompressedOops(gcflags, maxHeapForCompressedOops, true);113checkUseCompressedOops(gcflags, maxHeapForCompressedOops - 1, true);114checkUseCompressedOops(gcflags, maxHeapForCompressedOops + 1, false);115116// the use of HeapBaseMinAddress should not change the outcome117checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops, true);118checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops - 1, true);119checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), maxHeapForCompressedOops + 1, false);120121// use a different object alignment122maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"));123124checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops, true);125checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops - 1, true);126checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), maxHeapForCompressedOops + 1, false);127128// use a different CompressedClassSpaceSize129String compressedClassSpaceSizeArg = "-XX:CompressedClassSpaceSize=" + 2 * getCompressedClassSpaceSize();130maxHeapForCompressedOops = getMaxHeapForCompressedOops(join(gcflags, compressedClassSpaceSizeArg));131132checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops, true);133checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops - 1, true);134checkUseCompressedOops(join(gcflags, compressedClassSpaceSizeArg), maxHeapForCompressedOops + 1, false);135}136137private static void checkUseCompressedOops(String[] args, long heapsize, boolean expectUseCompressedOops) throws Exception {138ArrayList<String> finalargs = new ArrayList<String>();139finalargs.addAll(Arrays.asList(args));140finalargs.add("-Xmx" + heapsize);141finalargs.add("-XX:+PrintFlagsFinal");142finalargs.add("-version");143144String output = expectValid(finalargs.toArray(new String[0]));145146boolean actualUseCompressedOops = getFlagBoolValue(" UseCompressedOops", output);147148Asserts.assertEQ(expectUseCompressedOops, actualUseCompressedOops);149}150151private static boolean getFlagBoolValue(String flag, String where) {152Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where);153if (!m.find()) {154throw new RuntimeException("Could not find value for flag " + flag + " in output string");155}156return m.group(1).equals("true");157}158159private static String expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {160ProcessBuilder pb = GCArguments.createJavaProcessBuilder(flags);161OutputAnalyzer output = new OutputAnalyzer(pb.start());162output.shouldHaveExitValue(errorcode);163return output.getStdout();164}165166private static String expectValid(String[] flags) throws Exception {167return expect(flags, false, false, 0);168}169}170171172