Path: blob/master/test/hotspot/jtreg/gc/arguments/TestNewRatioFlag.java
64474 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 initHeap = HeapRegionUsageTool.getHeapUsage().getInit();125126long newSize = initEden + 2 * initSurv;127128// See GenArguments::scale_by_NewRatio_aligned for calculation in the JVM.129long expectedNewSize = HeapRegionUsageTool.alignDown(initHeap / (expectedRatio + 1),130wb.getHeapSpaceAlignment());131132if (expectedNewSize != newSize) {133throw new RuntimeException("Expected young gen size is: " + expectedNewSize134+ ", but observed new size is: " + newSize);135}136}137138/**139* Verify NewSize for PS collector.140* Expected NewSize calculated according to alignment policies used by PS141* and then compared with actual NewSize obtained from MemoryPoolMXBeans.142*/143public static void verifyPSNewRatio(int expectedRatio) {144long initEden = HeapRegionUsageTool.getEdenUsage().getInit();145long initSurv = HeapRegionUsageTool.getSurvivorUsage().getInit();146long initHeap = HeapRegionUsageTool.getHeapUsage().getInit();147148long newSize = initEden + 2 * initSurv;149150// See GenArguments::scale_by_NewRatio_aligned for calculation in the JVM.151long alignedDownNewSize = HeapRegionUsageTool.alignDown(initHeap / (expectedRatio + 1),152wb.getHeapSpaceAlignment());153long expectedNewSize = HeapRegionUsageTool.alignUp(alignedDownNewSize,154wb.psVirtualSpaceAlignment());155156if (expectedNewSize != newSize) {157throw new RuntimeException("Expected young gen size is: " + expectedNewSize158+ ", but observed new size is: " + newSize);159}160}161162/**163* Verify NewSize for G1 GC.164* Amount of young regions calculated according to sizing policies used by G1165* and then compared with actual number of young regions derived from166* values reported by MemoryPoolMXBeans and region size.167*/168public static void verifyG1NewRatio(int expectedRatio) {169long initEden = HeapRegionUsageTool.getEdenUsage().getInit();170long initSurv = HeapRegionUsageTool.getSurvivorUsage().getInit();171long maxOld = HeapRegionUsageTool.getOldUsage().getMax();172173int regionSize = wb.g1RegionSize();174int youngListLength = (int) ((initEden + initSurv) / regionSize);175int maxRegions = (int) (maxOld / regionSize);176int expectedYoungListLength = (int) (maxRegions / (double) (expectedRatio + 1));177178if (youngListLength != expectedYoungListLength) {179throw new RuntimeException("Expected G1 young list length is: " + expectedYoungListLength180+ ", but observed young list length is: " + youngListLength);181}182}183}184}185186187