Path: blob/master/test/hotspot/jtreg/gc/arguments/TestNewRatioFlag.java
40942 views
/*1* Copyright (c) 2015, 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 TestNewRatioFlag27* @bug 802516628* @summary Verify that heap devided among generations according to NewRatio29* @requires vm.gc != "Z" & vm.gc != "Shenandoah"30* @library /test/lib31* @library /32* @modules java.base/jdk.internal.misc33* java.management34* @build sun.hotspot.WhiteBox35* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox36* @run driver gc.arguments.TestNewRatioFlag37*/3839import java.util.Arrays;40import java.util.Collections;41import java.util.LinkedList;42import jdk.test.lib.process.ProcessTools;43import jdk.test.lib.process.OutputAnalyzer;44import jdk.test.lib.Utils;45import sun.hotspot.WhiteBox;4647public class TestNewRatioFlag {4849public static final long M = 1024 * 1024;50public static final long HEAP_SIZE = 100 * M;5152public static void main(String args[]) throws Exception {53LinkedList<String> options = new LinkedList<>(54Arrays.asList(Utils.getFilteredTestJavaOpts("(-XX:[^ ]*NewSize=[^ ]+)|(-Xm[ns][^ ]+)"))55);5657testNewRatio(4, options);58testNewRatio(6, options);59testNewRatio(10, options);60testNewRatio(15, options);61testNewRatio(20, options);62}6364/**65* Verify that actual size of young gen conforms specified NewRatio66*67* @param ratio value of NewRatio option68* @param options additional options for VM69*/70public static void testNewRatio(int ratio, LinkedList<String> options) throws Exception {71LinkedList<String> vmOptions = new LinkedList<>(options);72Collections.addAll(vmOptions,73"-Xbootclasspath/a:.",74"-XX:+UnlockDiagnosticVMOptions",75"-XX:+WhiteBoxAPI",76"-XX:GCLockerEdenExpansionPercent=0",77"-Xmx" + HEAP_SIZE,78"-Xms" + HEAP_SIZE,79"-XX:NewRatio=" + ratio,80"-XX:-UseLargePages",81NewRatioVerifier.class.getName(),82Integer.toString(ratio)83);8485ProcessBuilder procBuilder = GCArguments.createJavaProcessBuilder(vmOptions);86OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());87analyzer.shouldHaveExitValue(0);88System.out.println(analyzer.getOutput());89}9091public static class NewRatioVerifier {9293static WhiteBox wb = WhiteBox.getWhiteBox();9495public static void main(String args[]) {96if (args.length != 1) {97throw new IllegalArgumentException("Expected 1 arg: <expectedRatio>");98}99int expectedRatio = Integer.valueOf(args[0]);100switch (GCTypes.YoungGCType.getYoungGCType()) {101case DefNew:102verifyDefNewNewRatio(expectedRatio);103break;104case PSNew:105verifyPSNewRatio(expectedRatio);106break;107case G1:108verifyG1NewRatio(expectedRatio);109break;110default:111throw new RuntimeException("Unexpected young GC type");112}113}114115/**116* Verify NewSize for DefNew and ParNew collectors.117*118* Compare expected NewSize calculated according to sizing policies used by DefNew119* with NewSize value reported by MemoryPoolMXBeans.120*/121public static void verifyDefNewNewRatio(int expectedRatio) {122long initEden = HeapRegionUsageTool.getEdenUsage().getInit();123long initSurv = HeapRegionUsageTool.getSurvivorUsage().getInit();124long initOld = HeapRegionUsageTool.getOldUsage().getInit();125126long newSize = initEden + 2 * initSurv;127128long expectedNewSize = HeapRegionUsageTool.alignDown(initOld / expectedRatio,129wb.getHeapSpaceAlignment());130131if (expectedNewSize != newSize) {132throw new RuntimeException("Expected young gen size is: " + expectedNewSize133+ ", but observed new size is: " + newSize);134}135}136137/**138* Verify NewSize for PS collector.139* Expected NewSize calculated according to alignment policies used by PS140* and then compared with actual NewSize obtained from MemoryPoolMXBeans.141*/142public static void verifyPSNewRatio(int expectedRatio) {143long initEden = HeapRegionUsageTool.getEdenUsage().getInit();144long initSurv = HeapRegionUsageTool.getSurvivorUsage().getInit();145long initOld = HeapRegionUsageTool.getOldUsage().getInit();146147long newSize = initEden + 2 * initSurv;148149long alignedDownNewSize = HeapRegionUsageTool.alignDown(initOld / expectedRatio,150wb.getHeapSpaceAlignment());151long expectedNewSize = HeapRegionUsageTool.alignUp(alignedDownNewSize,152wb.psVirtualSpaceAlignment());153154if (expectedNewSize != newSize) {155throw new RuntimeException("Expected young gen size is: " + expectedNewSize156+ ", but observed new size is: " + newSize);157}158}159160/**161* Verify NewSize for G1 GC.162* Amount of young regions calculated according to sizing policies used by G1163* and then compared with actual number of young regions derived from164* values reported by MemoryPoolMXBeans and region size.165*/166public static void verifyG1NewRatio(int expectedRatio) {167long initEden = HeapRegionUsageTool.getEdenUsage().getInit();168long initSurv = HeapRegionUsageTool.getSurvivorUsage().getInit();169long maxOld = HeapRegionUsageTool.getOldUsage().getMax();170171int regionSize = wb.g1RegionSize();172int youngListLength = (int) ((initEden + initSurv) / regionSize);173int maxRegions = (int) (maxOld / regionSize);174int expectedYoungListLength = (int) (maxRegions / (double) (expectedRatio + 1));175176if (youngListLength != expectedYoungListLength) {177throw new RuntimeException("Expected G1 young list length is: " + expectedYoungListLength178+ ", but observed young list length is: " + youngListLength);179}180}181}182}183184185