Path: blob/master/test/hotspot/jtreg/gc/arguments/TestMinAndInitialSurvivorRatioFlags.java
40948 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 TestMinAndInitialSurvivorRatioFlags27* @summary Verify that MinSurvivorRatio and InitialSurvivorRatio flags work28* @library /test/lib29* @library /30* @modules java.base/jdk.internal.misc31* java.management32* @build sun.hotspot.WhiteBox33* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox34* @run driver/timeout=240 gc.arguments.TestMinAndInitialSurvivorRatioFlags35*/3637import java.lang.management.MemoryUsage;38import java.util.Arrays;39import java.util.Collections;40import java.util.LinkedList;41import jdk.test.lib.process.OutputAnalyzer;42import jdk.test.lib.process.ProcessTools;43import jdk.test.lib.Utils;44import sun.hotspot.WhiteBox;4546/* Test verifies that VM can start with any GC when MinSurvivorRatio and47* InitialSurvivorRatio flags passed and for Parallel GC it verifies that48* after start up survivor ratio equal to InitialSurvivorRatio value and49* that actual survivor ratio will never be less than MinSurvivorRatio.50*/51public class TestMinAndInitialSurvivorRatioFlags {5253public static final long M = 1024 * 1024;54public static final long HEAP_SIZE = 200 * M;55public static final long NEW_SIZE = 100 * M;5657public static void main(String args[]) throws Exception {58LinkedList<String> options = new LinkedList<>(59Arrays.asList(Utils.getFilteredTestJavaOpts("-XX:[^ ]*SurvivorRatio=[^ ]+"))60);6162testSurvivorRatio(5, -1, -1, options, true);63testSurvivorRatio(10, -1, -1, options, true);64testSurvivorRatio(-1, 5, 3, options, true);65testSurvivorRatio(-1, 15, 3, options, true);66testSurvivorRatio(-1, 15, 3, options, false);67testSurvivorRatio(-1, 10, 10, options, true);68testSurvivorRatio(-1, 3, 15, options, true);69testSurvivorRatio(-1, 3, 15, options, false);70}7172/**73* Test that MinSurvivorRatio and InitialSurvivorRatio flags work.74*75* @param survivorRatio value for -XX:SurvivorRatio option, omitted if negative76* @param initRatio value for -XX:InitialSurvivorRatio option, omitted if negative77* @param minRatio value for -XX:MinSurvivorRatio option, omitted if negative78* @param options additional options for VM79* @param useAdaptiveSizePolicy turn on or off UseAdaptiveSizePolicy option80*/81public static void testSurvivorRatio(int survivorRatio,82int initRatio,83int minRatio,84LinkedList<String> options,85boolean useAdaptiveSizePolicy) throws Exception {8687LinkedList<String> vmOptions = new LinkedList<>(options);88Collections.addAll(vmOptions,89"-Xbootclasspath/a:.",90"-XX:+UnlockDiagnosticVMOptions",91"-XX:+WhiteBoxAPI",92"-XX:MaxNewSize=" + NEW_SIZE, "-XX:NewSize=" + NEW_SIZE,93"-Xmx" + HEAP_SIZE, "-Xms" + HEAP_SIZE,94(survivorRatio >= 0 ? "-XX:SurvivorRatio=" + survivorRatio : ""),95(initRatio >= 0 ? "-XX:InitialSurvivorRatio=" + initRatio : ""),96(minRatio >= 0 ? "-XX:MinSurvivorRatio=" + minRatio : ""),97(useAdaptiveSizePolicy ? "-XX:+UseAdaptiveSizePolicy" : "-XX:-UseAdaptiveSizePolicy"),98SurvivorRatioVerifier.class.getName(),99Integer.toString(survivorRatio),100Integer.toString(initRatio),101Integer.toString(minRatio),102Boolean.toString(useAdaptiveSizePolicy)103);104vmOptions.removeIf((String p) -> p.isEmpty());105ProcessBuilder procBuilder = GCArguments.createJavaProcessBuilder(vmOptions);106OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());107analyzer.shouldHaveExitValue(0);108}109110/**111* Class that verifies survivor ratio.112* Will be executed in tested VM. Checks initial size of eden and survivor paces with alignment.113*/114public static class SurvivorRatioVerifier {115116public static WhiteBox wb = WhiteBox.getWhiteBox();117118public static final int MAX_ITERATIONS = 10;119public static final int ARRAY_LENGTH = 10000;120public static final int CHUNK_SIZE = 10000;121122public static byte garbage[][] = new byte[ARRAY_LENGTH][];123124public static void main(String args[]) throws Exception {125if (args.length != 4) {126throw new IllegalArgumentException("Expected 4 args: <survivorRatio> <initRatio> <minRatio> <useAdaptiveSizePolicy>");127}128final int survivorRatio = Integer.valueOf(args[0]);129final int initRatio = Integer.valueOf(args[1]);130final int minRatio = Integer.valueOf(args[2]);131final boolean useAdaptiveSizePolicy = Boolean.valueOf(args[3]);132133// we stop testing only here to ensure that JVM will accept134// both MinSurvivorRatio and InitialSurvivorRatio regardles to GC135if (GCTypes.YoungGCType.getYoungGCType() != GCTypes.YoungGCType.PSNew) {136System.out.println("Test is only applicable to Parallel GC");137return;138}139140// verify initial survivor ratio141verifySurvivorRatio(survivorRatio, initRatio, minRatio, useAdaptiveSizePolicy, true);142143// force GC144AllocationHelper allocator = new AllocationHelper(MAX_ITERATIONS, ARRAY_LENGTH, CHUNK_SIZE,145() -> (verifySurvivorRatio(survivorRatio, initRatio, minRatio, useAdaptiveSizePolicy, false)));146allocator.allocateMemoryAndVerify();147}148149/**150* Verify actual survivor ratio.151*152* @param survivorRatio value of SurvivorRatio option, omitted if negative153* @param initRatio value of InitialSurvivorRatio option, omitted if negative154* @param minRatio value of MinSurvivorRatio option, omitted if negative155* @param useAdaptiveSizePolicy value of UseAdaptiveSizePolicy option156* @param verifyInitialRatio true if we are going to verify initial ratio157*/158public static Void verifySurvivorRatio(int survivorRatio,159int initRatio,160int minRatio,161boolean useAdaptiveSizePolicy,162boolean verifyInitialRatio) {163164MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage();165MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage();166167long alignedNewSize = edenUsage.getMax() + 2 * survivorUsage.getMax();168long generationAlignment = wb.psHeapGenerationAlignment();169170if (survivorRatio >= 0) {171// -XX:SurvivorRatio was passed to JVM, actual ratio should be SurvivorRatio + 2172long expectedSize = HeapRegionUsageTool.alignDown(alignedNewSize / (survivorRatio + 2),173generationAlignment);174175if (survivorUsage.getCommitted() != expectedSize) {176throw new RuntimeException("Expected survivor size is: " + expectedSize177+ ", but observed size is: " + survivorUsage.getCommitted());178}179} else if (verifyInitialRatio || !useAdaptiveSizePolicy) {180// In case of initial ratio verification or disabled adaptive size policy181// ratio should be equal to InitialSurvivorRatio value182long expectedSize = HeapRegionUsageTool.alignDown(alignedNewSize / initRatio,183generationAlignment);184if (survivorUsage.getCommitted() != expectedSize) {185throw new RuntimeException("Expected survivor size is: " + expectedSize186+ ", but observed size is: " + survivorUsage.getCommitted());187}188} else {189// In any other case actual survivor ratio should not be lower than MinSurvivorRatio190// or is should be equal to InitialSurvivorRatio191long expectedMinSize = HeapRegionUsageTool.alignDown(alignedNewSize / minRatio,192generationAlignment);193long expectedInitSize = HeapRegionUsageTool.alignDown(alignedNewSize / initRatio,194generationAlignment);195if (survivorUsage.getCommitted() != expectedInitSize196&& survivorUsage.getCommitted() < expectedMinSize) {197throw new RuntimeException("Expected survivor size should be " + expectedMinSize198+ " or should be greater then " + expectedMinSize199+ ", but observer survivor size is " + survivorUsage.getCommitted());200}201}202return null;203}204}205}206207208