1 /* 2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test TestSurvivorRatioFlag 26 * @key gc 27 * @summary Verify that actual survivor ratio is equal to specified SurvivorRatio value 28 * @requires vm.gc != "Z" & vm.gc != "Shenandoah" 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc 31 * java.management 32 * @build sun.hotspot.WhiteBox 33 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 34 * @run driver TestSurvivorRatioFlag 35 */ 36 37 import java.lang.management.MemoryUsage; 38 import java.util.Arrays; 39 import java.util.Collections; 40 import java.util.LinkedList; 41 import jdk.test.lib.process.OutputAnalyzer; 42 import jdk.test.lib.process.ProcessTools; 43 import jdk.test.lib.Utils; 44 import sun.hotspot.WhiteBox; 45 46 public class TestSurvivorRatioFlag { 47 48 public static final long M = 1024 * 1024; 49 public static final long HEAP_SIZE = 200 * M; 50 public static final long NEW_SIZE = 100 * M; 51 52 public static void main(String args[]) throws Exception { 53 LinkedList<String> options = new LinkedList<>( 54 Arrays.asList(Utils.getFilteredTestJavaOpts("-XX:[^ ]*SurvivorRatio=[^ ]+")) 55 ); 56 57 testSurvivorRatio(3, options); 58 testSurvivorRatio(6, options); 59 testSurvivorRatio(10, options); 60 testSurvivorRatio(15, options); 61 testSurvivorRatio(20, options); 62 } 63 64 /** 65 * Verify that actual survivor ratio equal to specified. 66 * 67 * @param ratio survivor ratio that be verified 68 * @param options additional options to JVM 69 */ 70 public static void testSurvivorRatio(int ratio, LinkedList<String> options) throws Exception { 71 72 LinkedList<String> vmOptions = new LinkedList<>(options); 73 74 Collections.addAll(vmOptions, 75 "-Xbootclasspath/a:.", 76 "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED", 77 "-XX:+UnlockDiagnosticVMOptions", 78 "-XX:+WhiteBoxAPI", 79 "-XX:GCLockerEdenExpansionPercent=0", 80 "-XX:MaxNewSize=" + NEW_SIZE, 81 "-XX:NewSize=" + NEW_SIZE, 82 "-Xmx" + HEAP_SIZE, 83 "-Xms" + HEAP_SIZE, 84 "-XX:SurvivorRatio=" + ratio, 85 SurvivorRatioVerifier.class.getName(), 86 Integer.toString(ratio) 87 ); 88 89 ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(vmOptions.toArray(new String[vmOptions.size()])); 90 OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start()); 91 analyzer.shouldHaveExitValue(0); 92 } 93 94 /** 95 * Class that verifies survivor ratio. 96 */ 97 public static class SurvivorRatioVerifier { 98 99 static WhiteBox wb = WhiteBox.getWhiteBox(); 100 101 public static final int MAX_ITERATIONS = 10; 102 public static final int ARRAY_LENGTH = 10000; 103 public static final int CHUNK_SIZE = 10000; 104 105 public static void main(String args[]) throws Exception { 106 if (args.length != 1) { 107 throw new IllegalArgumentException("Expected 1 arg: <ratio>"); 108 } 109 final int ratio = Integer.valueOf(args[0]); 110 111 AllocationHelper allocator = new AllocationHelper(MAX_ITERATIONS, ARRAY_LENGTH, CHUNK_SIZE, () -> (verifySurvivorRatio(ratio))); 112 allocator.allocateMemoryAndVerify(); 113 } 114 115 /** 116 * Verify that actual survivor ratio is equal to expected. 117 * Depending on selected young GC we verify that: 118 * - for DefNew and ParNew: eden_size / survivor_size is close to expectedRatio; 119 * - for PSNew: survivor_size equal to young_gen_size / expectedRatio; 120 * - for G1: survivor_regions <= young_list_length / expectedRatio. 121 */ 122 public static Void verifySurvivorRatio(int expectedRatio) { 123 GCTypes.YoungGCType type = GCTypes.YoungGCType.getYoungGCType(); 124 switch (type) { 125 case DefNew: 126 case ParNew: 127 verifyDefNewSurvivorRatio(expectedRatio); 128 break; 129 case PSNew: 130 verifyPSSurvivorRatio(expectedRatio); 131 break; 132 case G1: 133 verifyG1SurvivorRatio(expectedRatio); 134 break; 135 default: 136 throw new RuntimeException("Unexpected young GC type"); 137 } 138 return null; 139 } 140 141 private static void verifyDefNewSurvivorRatio(int expectedRatio) { 142 MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage(); 143 MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage(); 144 145 int actualRatio = (int) (edenUsage.getCommitted() / survivorUsage.getCommitted()); 146 if (Math.abs(actualRatio - expectedRatio) > 1) { 147 throw new RuntimeException("Expected survivor ratio is: " + expectedRatio 148 + ", but observed ratio is: " + actualRatio); 149 } 150 } 151 152 private static void verifyPSSurvivorRatio(int expectedRatio) { 153 MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage(); 154 MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage(); 155 156 long youngGenSize = edenUsage.getMax() + 2 * survivorUsage.getMax(); 157 // for Paralle GC Min/InitialSurvivorRatio = SurvivorRatio + 2 158 long expectedSize = HeapRegionUsageTool.alignDown(youngGenSize / (expectedRatio + 2), 159 wb.psHeapGenerationAlignment()); 160 161 if (expectedSize != survivorUsage.getCommitted()) { 162 throw new RuntimeException("Expected survivor size is: " + expectedSize 163 + ", but observed size is: " + survivorUsage.getCommitted()); 164 } 165 } 166 167 private static void verifyG1SurvivorRatio(int expectedRatio) { 168 MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage(); 169 170 int regionSize = wb.g1RegionSize(); 171 int youngListLength = (int) Math.max(NEW_SIZE / regionSize, 1); 172 int expectedSurvivorRegions = (int) Math.ceil(youngListLength / (double) expectedRatio); 173 int observedSurvivorRegions = (int) (survivorUsage.getCommitted() / regionSize); 174 175 if (expectedSurvivorRegions < observedSurvivorRegions) { 176 throw new RuntimeException("Expected amount of G1 survivor regions is " 177 + expectedSurvivorRegions + ", but observed " 178 + observedSurvivorRegions); 179 } 180 } 181 } 182 }