Path: blob/master/test/hotspot/jtreg/gc/arguments/TestMaxRAMFlags.java
40948 views
/*1* Copyright (c) 2019, 2021, 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;2425/*26* @test TestMaxRAMFlags27* @bug 822225228* @summary Verify correct MaxHeapSize and UseCompressedOops when MaxRAM and MaxRAMPercentage29* are specified.30* @library /test/lib31* @library /32* @requires vm.bits == "64"33* @modules java.base/jdk.internal.misc34* java.management35* @build sun.hotspot.WhiteBox36* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox37* @run driver gc.arguments.TestMaxRAMFlags38* @author [email protected]39*/4041import java.util.regex.Matcher;42import java.util.regex.Pattern;4344import java.util.ArrayList;45import java.util.Arrays;4647import jdk.test.lib.process.OutputAnalyzer;48import jdk.test.lib.process.ProcessTools;4950public class TestMaxRAMFlags {5152private static void checkMaxRAMSize(long maxram, int maxrampercent, boolean forcecoop, long expectheap, boolean expectcoop) throws Exception {5354ArrayList<String> args = new ArrayList<String>();55args.add("-XX:MaxRAM=" + maxram);56args.add("-XX:MaxRAMPercentage=" + maxrampercent);57if (forcecoop) {58args.add("-XX:+UseCompressedOops");59}6061args.add("-XX:+PrintFlagsFinal");62args.add("-version");6364ProcessBuilder pb = GCArguments.createJavaProcessBuilder(args);65OutputAnalyzer output = new OutputAnalyzer(pb.start());66output.shouldHaveExitValue(0);67String stdout = output.getStdout();6869long actualheap = new Long(getFlagValue("MaxHeapSize", stdout)).longValue();70if (actualheap != expectheap) {71throw new RuntimeException("MaxHeapSize value set to " + actualheap +72", expected " + expectheap + " when running with the following flags: " + Arrays.asList(args).toString());73}7475boolean actualcoop = getFlagBoolValue("UseCompressedOops", stdout);76if (actualcoop != expectcoop) {77throw new RuntimeException("UseCompressedOops set to " + actualcoop +78", expected " + expectcoop + " when running with the following flags: " + Arrays.asList(args).toString());79}80}8182private static long getHeapBaseMinAddress() throws Exception {83ArrayList<String> args = new ArrayList<String>();84args.add("-XX:+PrintFlagsFinal");85args.add("-version");8687ProcessBuilder pb = GCArguments.createJavaProcessBuilder(args);88OutputAnalyzer output = new OutputAnalyzer(pb.start());89output.shouldHaveExitValue(0);90String stdout = output.getStdout();91return (new Long(getFlagValue("HeapBaseMinAddress", stdout)).longValue());92}9394private static String getFlagValue(String flag, String where) {95Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);96if (!m.find()) {97throw new RuntimeException("Could not find value for flag " + flag + " in output string");98}99String match = m.group();100return match.substring(match.lastIndexOf(" ") + 1, match.length());101}102103private static boolean getFlagBoolValue(String flag, String where) {104Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where);105if (!m.find()) {106throw new RuntimeException("Could not find value for flag " + flag + " in output string");107}108return m.group(1).equals("true");109}110111public static void main(String args[]) throws Exception {112// Tests113// 1. Verify that MaxRAMPercentage overrides UseCompressedOops Ergo114// 2. Verify that UseCompressedOops forces compressed oops limit even115// when other flags are specified.116117long oneG = 1L * 1024L * 1024L * 1024L;118119// Hotspot startup logic reduces MaxHeapForCompressedOops by HeapBaseMinAddress120// in order to get zero based compressed oops offsets.121long heapbaseminaddr = getHeapBaseMinAddress();122long maxcoopheap = TestUseCompressedOopsErgoTools.getMaxHeapForCompressedOops(new String [0]) - heapbaseminaddr;123124// Args: MaxRAM , MaxRAMPercentage, forcecoop, expect heap, expect coop125checkMaxRAMSize(maxcoopheap - oneG, 100, false, maxcoopheap - oneG, true);126checkMaxRAMSize(maxcoopheap + oneG, 100, false, maxcoopheap + oneG, false);127checkMaxRAMSize(maxcoopheap + oneG, 100, true, maxcoopheap, true);128}129}130131132