1 /* 2 * Copyright (c) 2015, 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 * @library /testlibrary /test/lib 29 * @modules java.base/jdk.internal.misc 30 * java.management 31 * @build TestSurvivorRatioFlag 32 * @run main ClassFileInstaller sun.hotspot.WhiteBox 33 * @run driver TestSurvivorRatioFlag 34 */ 35 36 import jdk.test.lib.AllocationHelper; 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.HeapRegionUsageTool; 42 import jdk.test.lib.OutputAnalyzer; 43 import jdk.test.lib.ProcessTools; 44 import jdk.test.lib.Utils; 45 import sun.hotspot.WhiteBox; 46 47 public class TestSurvivorRatioFlag { 48 49 public static final long M = 1024 * 1024; 50 public static final long HEAP_SIZE = 200 * M; 51 public static final long NEW_SIZE = 100 * M; 52 53 public static void main(String args[]) throws Exception { 54 LinkedList<String> options = new LinkedList<>( 55 Arrays.asList(Utils.getFilteredTestJavaOpts("-XX:[^ ]*SurvivorRatio=[^ ]+")) 56 ); 57 58 testSurvivorRatio(3, options); 59 testSurvivorRatio(6, options); 60 testSurvivorRatio(10, options); 61 testSurvivorRatio(15, options); 62 testSurvivorRatio(20, options); 63 } 64 65 /** 66 * Verify that actual survivor ratio equal to specified. 67 * 68 * @param ratio survivor ratio that be verified 69 * @param options additional options to JVM 70 */ 71 public static void testSurvivorRatio(int ratio, LinkedList<String> options) throws Exception { 72 73 LinkedList<String> vmOptions = new LinkedList<>(options); 74 75 Collections.addAll(vmOptions, 76 "-Xbootclasspath/a:.", 77 "-XaddExports:java.base/jdk.internal.misc=ALL-UNNAMED", 78 "-XX:+UnlockDiagnosticVMOptions", 79 "-XX:+WhiteBoxAPI", 80 "-XX:GCLockerEdenExpansionPercent=0", 81 "-XX:MaxNewSize=" + NEW_SIZE, 82 "-XX:NewSize=" + NEW_SIZE, 83 "-Xmx" + HEAP_SIZE, 84 "-Xms" + HEAP_SIZE, 85 "-XX:SurvivorRatio=" + ratio, 86 SurvivorRatioVerifier.class.getName(), 87 Integer.toString(ratio) 88 ); 89 90 ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(vmOptions.toArray(new String[vmOptions.size()])); 91 OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start()); 92 analyzer.shouldHaveExitValue(0); 93 } 94 95 /** 96 * Class that verifies survivor ratio. 97 */ 98 public static class SurvivorRatioVerifier { 99 100 static WhiteBox wb = WhiteBox.getWhiteBox(); 101 102 public static final int MAX_ITERATIONS = 10; 103 public static final int ARRAY_LENGTH = 10000; 104 public static final int CHUNK_SIZE = 10000; 105 106 public static void main(String args[]) throws Exception { 107 if (args.length != 1) { 108 throw new IllegalArgumentException("Expected 1 arg: <ratio>"); 109 } 110 final int ratio = Integer.valueOf(args[0]); 111 112 AllocationHelper allocator = new AllocationHelper(MAX_ITERATIONS, ARRAY_LENGTH, CHUNK_SIZE, () -> (verifySurvivorRatio(ratio))); 113 allocator.allocateMemoryAndVerify(); 114 } 115 116 /** 117 * Verify that actual survivor ratio is equal to expected. 118 * Depending on selected young GC we verify that: 119 * - for DefNew and ParNew: eden_size / survivor_size is close to expectedRatio; 120 * - for PSNew: survivor_size equal to young_gen_size / expectedRatio; 121 * - for G1: survivor_regions <= young_list_length / expectedRatio. 122 */ 123 public static Void verifySurvivorRatio(int expectedRatio) { 124 GCTypes.YoungGCType type = GCTypes.YoungGCType.getYoungGCType(); 125 switch (type) { 126 case DefNew: 127 case ParNew: 128 verifyDefNewSurvivorRatio(expectedRatio); 129 break; 130 case PSNew: 131 verifyPSSurvivorRatio(expectedRatio); 132 break; 133 case G1: 134 verifyG1SurvivorRatio(expectedRatio); 135 break; 136 default: 137 throw new RuntimeException("Unexpected young GC type"); 138 } 139 return null; 140 } 141 142 private static void verifyDefNewSurvivorRatio(int expectedRatio) { 143 MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage(); 144 MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage(); 145 146 int actualRatio = (int) (edenUsage.getCommitted() / survivorUsage.getCommitted()); 147 if (Math.abs(actualRatio - expectedRatio) > 1) { 148 throw new RuntimeException("Expected survivor ratio is: " + expectedRatio 149 + ", but observed ratio is: " + actualRatio); 150 } 151 } 152 153 private static void verifyPSSurvivorRatio(int expectedRatio) { 154 MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage(); 155 MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage(); 156 157 long youngGenSize = edenUsage.getMax() + 2 * survivorUsage.getMax(); 158 // for Paralle GC Min/InitialSurvivorRatio = SurvivorRatio + 2 159 long expectedSize = HeapRegionUsageTool.alignDown(youngGenSize / (expectedRatio + 2), 160 wb.psHeapGenerationAlignment()); 161 162 if (expectedSize != survivorUsage.getCommitted()) { 163 throw new RuntimeException("Expected survivor size is: " + expectedSize 164 + ", but observed size is: " + survivorUsage.getCommitted()); 165 } 166 } 167 168 private static void verifyG1SurvivorRatio(int expectedRatio) { 169 MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage(); 170 171 int regionSize = wb.g1RegionSize(); 172 int youngListLength = (int) Math.max(NEW_SIZE / regionSize, 1); 173 int expectedSurvivorRegions = (int) Math.ceil(youngListLength / (double) expectedRatio); 174 int observedSurvivorRegions = (int) (survivorUsage.getCommitted() / regionSize); 175 176 if (expectedSurvivorRegions < observedSurvivorRegions) { 177 throw new RuntimeException("Expected amount of G1 survivor regions is " 178 + expectedSurvivorRegions + ", but observed " 179 + observedSurvivorRegions); 180 } 181 } 182 } 183 }