1 /* 2 * Copyright (c) 2016, 2024, 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 TestPLABPromotion 26 * @bug 8141278 8141141 27 * @summary Test PLAB promotion 28 * @requires vm.gc.G1 29 * @requires vm.flagless 30 * @library /test/lib / 31 * @modules java.base/jdk.internal.misc 32 * @modules java.management 33 * @build jdk.test.whitebox.WhiteBox 34 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox 35 * @run main/othervm/timeout=240 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI gc.g1.plab.TestPLABPromotion 36 */ 37 package gc.g1.plab; 38 39 import java.util.List; 40 import java.util.Arrays; 41 import java.io.PrintStream; 42 43 import gc.g1.plab.lib.AppPLABPromotion; 44 import gc.g1.plab.lib.LogParser; 45 import gc.g1.plab.lib.PLABUtils; 46 import gc.g1.plab.lib.PlabInfo; 47 48 import jdk.test.lib.Platform; 49 import jdk.test.lib.process.OutputAnalyzer; 50 import jdk.test.lib.process.ProcessTools; 51 import jdk.test.whitebox.WhiteBox; 52 53 /** 54 * Test checks PLAB promotion of different size objects. 55 */ 56 public class TestPLABPromotion { 57 58 private static final boolean COMPACT_HEADERS = Platform.is64bit() && WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompactObjectHeaders"); 59 60 // GC ID with survivor PLAB statistics 61 private final static long GC_ID_SURVIVOR_STATS = 1l; 62 // GC ID with old PLAB statistics 63 private final static long GC_ID_OLD_STATS = 2l; 64 65 private final static String PLAB_USED_FIELD_NAME = "used"; 66 private final static String PLAB_DIRECT_ALLOCATED_FIELD_NAME = "direct allocated"; 67 private final static List<String> FIELDS_TO_EXTRACT = Arrays.asList(PLAB_USED_FIELD_NAME, PLAB_DIRECT_ALLOCATED_FIELD_NAME); 68 69 private static final int HEAP_WORD_SIZE = Platform.is32bit() ? 4 : 8; 70 71 private static String output; 72 73 // Allowable difference for memory consumption (percentage) 74 private final static long MEM_DIFFERENCE_PCT = 5; 75 private static final int PLAB_SIZE_SMALL = 1024; 76 private static final int PLAB_SIZE_MEDIUM = 4096; 77 private static final int PLAB_SIZE_HIGH = 65536; 78 private static final int OBJECT_SIZE_SMALL = 10 * HEAP_WORD_SIZE; 79 private static final int OBJECT_SIZE_MEDIUM = 128 * HEAP_WORD_SIZE; 80 private static final int OBJECT_SIZE_HIGH = (COMPACT_HEADERS ? 3266 : 3250) * HEAP_WORD_SIZE; 81 private static final int GC_NUM_SMALL = 1; 82 private static final int GC_NUM_MEDIUM = 3; 83 private static final int GC_NUM_HIGH = 7; 84 private static final int WASTE_PCT_SMALL = 10; 85 private static final int WASTE_PCT_MEDIUM = 20; 86 private static final int WASTE_PCT_HIGH = 30; 87 private static final int YOUNG_SIZE_LOW = 16; 88 private static final int YOUNG_SIZE_HIGH = 64; 89 private static final boolean PLAB_FIXED = true; 90 private static final boolean PLAB_DYNAMIC = false; 91 92 private final static TestCase[] TEST_CASES = { 93 // Test cases for unreachable object, PLAB size is fixed 94 new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_SMALL, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_FIXED, false, false), 95 new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_MEDIUM, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_FIXED, false, false), 96 // Test cases for reachable objects, PLAB size is fixed 97 new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_SMALL, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_FIXED, true, true), 98 new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_MEDIUM, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_FIXED, true, true), 99 new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_SMALL, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, YOUNG_SIZE_LOW, PLAB_FIXED, true, false), 100 new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_HIGH, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_FIXED, true, true), 101 new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_SMALL, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_FIXED, true, true), 102 new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_MEDIUM, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, YOUNG_SIZE_LOW, PLAB_FIXED, true, false), 103 new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_MEDIUM, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, YOUNG_SIZE_LOW, PLAB_FIXED, true, true), 104 new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_SMALL, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_FIXED, true, true), 105 new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_HIGH, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_FIXED, true, true), 106 new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_SMALL, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, YOUNG_SIZE_HIGH, PLAB_FIXED, true, false), 107 // Test cases for unreachable object, PLAB size is not fixed 108 new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_MEDIUM, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_LOW, PLAB_DYNAMIC, false, false), 109 // Test cases for reachable objects, PLAB size is not fixed 110 new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_HIGH, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_DYNAMIC, true, true), 111 new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_MEDIUM, OBJECT_SIZE_SMALL, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_DYNAMIC, true, true), 112 new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_MEDIUM, OBJECT_SIZE_HIGH, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_DYNAMIC, true, false), 113 new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_HIGH, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_LOW, PLAB_DYNAMIC, true, true) 114 }; 115 116 public static void main(String[] args) throws Throwable { 117 118 for (TestCase testCase : TEST_CASES) { 119 // What we going to check. 120 testCase.print(System.out); 121 List<String> options = PLABUtils.prepareOptions(testCase.toOptions()); 122 options.add(AppPLABPromotion.class.getName()); 123 OutputAnalyzer out = ProcessTools.executeTestJava(options); 124 PLABUtils.commonCheck(out); 125 output = out.getOutput(); 126 checkResults(testCase); 127 } 128 } 129 130 private static void checkResults(TestCase testCase) { 131 long plabAllocatedSurvivor; 132 long directAllocatedSurvivor; 133 long plabAllocatedOld; 134 long directAllocatedOld; 135 long memAllocated = testCase.getMemToFill(); 136 LogParser logParser = new LogParser(output); 137 138 PlabInfo survivorPlabInfo = logParser.getSpecifiedStats(GC_ID_SURVIVOR_STATS, LogParser.ReportType.SURVIVOR_STATS, FIELDS_TO_EXTRACT); 139 PlabInfo oldPlabInfo = logParser.getSpecifiedStats(GC_ID_OLD_STATS, LogParser.ReportType.OLD_STATS, FIELDS_TO_EXTRACT); 140 141 checkFields(survivorPlabInfo); 142 checkFields(oldPlabInfo); 143 144 plabAllocatedSurvivor = survivorPlabInfo.get(PLAB_USED_FIELD_NAME); 145 directAllocatedSurvivor = survivorPlabInfo.get(PLAB_DIRECT_ALLOCATED_FIELD_NAME); 146 plabAllocatedOld = oldPlabInfo.get(PLAB_USED_FIELD_NAME); 147 directAllocatedOld = oldPlabInfo.get(PLAB_DIRECT_ALLOCATED_FIELD_NAME); 148 149 System.out.printf("Survivor PLAB allocated:%17d Direct allocated: %17d Mem consumed:%17d%n", plabAllocatedSurvivor, directAllocatedSurvivor, memAllocated); 150 System.out.printf("Old PLAB allocated:%17d Direct allocated: %17d Mem consumed:%17d%n", plabAllocatedOld, directAllocatedOld, memAllocated); 151 152 // Unreachable objects case 153 if (testCase.isDeadObjectCase()) { 154 checkDeadObjectsPromotion(plabAllocatedSurvivor, directAllocatedSurvivor, memAllocated); 155 checkDeadObjectsPromotion(plabAllocatedOld, directAllocatedOld, memAllocated); 156 157 } else { 158 // Live objects case 159 if (testCase.isPromotedByPLAB()) { 160 checkLiveObjectsPromotion(plabAllocatedSurvivor, memAllocated, "Expect that Survivor PLAB allocation are similar to all mem consumed"); 161 checkLiveObjectsPromotion(plabAllocatedOld, memAllocated, "Expect that Old PLAB allocation are similar to all mem consumed"); 162 } else { 163 // All big objects should be directly allocated 164 checkLiveObjectsPromotion(directAllocatedSurvivor, memAllocated, "Expect that Survivor direct allocation are similar to all mem consumed"); 165 checkLiveObjectsPromotion(directAllocatedOld, memAllocated, "Expect that Old direct allocation are similar to all mem consumed"); 166 } 167 168 checkTotalPromotion(plabAllocatedSurvivor, directAllocatedSurvivor, memAllocated, "Expect that Survivor gen total allocation are similar to all mem consumed"); 169 checkTotalPromotion(plabAllocatedOld, directAllocatedOld, memAllocated, "Expect that Old gen total allocation are similar to all mem consumed"); 170 } 171 System.out.println("Test passed!"); 172 } 173 174 private static void checkTotalPromotion(long plabAllocatedSurvivor, long directAllocatedSurvivor, long memAllocated, String exceptionMessage) { 175 // All promoted objects size should be similar to all consumed memory 176 if (!checkDifferenceRatio(plabAllocatedSurvivor + directAllocatedSurvivor, memAllocated)) { 177 System.out.println(output); 178 throw new RuntimeException(exceptionMessage); 179 } 180 } 181 182 /** 183 * Checks that live objects were promoted as expected. 184 * @param plabAllocated 185 * @param totalMemAllocated 186 * @param exceptionMessage 187 */ 188 private static void checkLiveObjectsPromotion(long plabAllocated, long totalMemAllocated, String exceptionMessage) { 189 // All live small objects should be promoted using PLAB 190 if (!checkDifferenceRatio(plabAllocated, totalMemAllocated)) { 191 System.out.println(output); 192 throw new RuntimeException(exceptionMessage); 193 } 194 } 195 196 /** 197 * Checks that dead objects are not promoted. 198 * @param plabPromoted promoted by PLAB 199 * @param directlyPromoted 200 * @param memoryAllocated total memory allocated 201 */ 202 private static void checkDeadObjectsPromotion(long plabPromoted, long directlyPromoted, long memoryAllocated) { 203 // No dead objects should be promoted 204 if (!(checkRatio(plabPromoted, memoryAllocated) && checkRatio(directlyPromoted, memoryAllocated))) { 205 System.out.println(output); 206 throw new RuntimeException("Unreachable objects should not be allocated using PLAB or directly allocated to Survivor/Old"); 207 } 208 } 209 210 /** 211 * Checks that PLAB statistics contains expected fields. 212 * @param info 213 */ 214 private static void checkFields(PlabInfo info) { 215 if (!info.checkFields(FIELDS_TO_EXTRACT)) { 216 System.out.println(output); 217 throw new RuntimeException("PLAB log does not contain expected fields"); 218 } 219 } 220 221 /** 222 * Returns true if checkedValue is less than MEM_DIFFERENCE_PCT percent of controlValue. 223 * 224 * @param checkedValue - checked value 225 * @param controlValue - referent value 226 * @return true if checkedValue is less than MEM_DIFFERENCE_PCT percent of controlValue 227 */ 228 private static boolean checkRatio(long checkedValue, long controlValue) { 229 return Math.abs(checkedValue * 100.0 / controlValue) < MEM_DIFFERENCE_PCT; 230 } 231 232 /** 233 * Returns true if difference of checkedValue and controlValue is less than 234 * MEM_DIFFERENCE_PCT percent of controlValue. 235 * 236 * @param checkedValue - checked value 237 * @param controlValue - referent value 238 * @return true if difference of checkedValue and controlValue is less than 239 * MEM_DIFFERENCE_PCT percent of controlValue 240 */ 241 private static boolean checkDifferenceRatio(long checkedValue, long controlValue) { 242 return Math.abs((checkedValue - controlValue) * 100.0 / controlValue) < MEM_DIFFERENCE_PCT; 243 } 244 245 /** 246 * Description of one test case. 247 */ 248 private static class TestCase { 249 250 private final int wastePct; 251 private final int plabSize; 252 private final int chunkSize; 253 private final int parGCThreads; 254 private final int edenSize; 255 private final boolean plabIsFixed; 256 private final boolean objectsAreReachable; 257 private final boolean promotedByPLAB; 258 259 /** 260 * @param wastePct 261 * ParallelGCBufferWastePct 262 * @param plabSize 263 * -XX:OldPLABSize and -XX:YoungPLABSize 264 * @param chunkSize 265 * requested object size for memory consumption 266 * @param parGCThreads 267 * -XX:ParallelGCThreads 268 * @param edenSize 269 * NewSize and MaxNewSize 270 * @param plabIsFixed 271 * Use dynamic PLAB or fixed size PLAB 272 * @param objectsAreReachable 273 * true - allocate live objects 274 * false - allocate unreachable objects 275 * @param promotedByPLAB 276 * true - we expect to see PLAB allocation during promotion 277 * false - objects will be directly allocated during promotion 278 */ 279 public TestCase(int wastePct, 280 int plabSize, 281 int chunkSize, 282 int parGCThreads, 283 int edenSize, 284 boolean plabIsFixed, 285 boolean objectsAreReachable, 286 boolean promotedByPLAB 287 ) { 288 if (wastePct == 0 || plabSize == 0 || chunkSize == 0 || parGCThreads == 0 || edenSize == 0) { 289 throw new IllegalArgumentException("Parameters should not be 0"); 290 } 291 this.wastePct = wastePct; 292 this.plabSize = plabSize; 293 this.chunkSize = chunkSize; 294 this.parGCThreads = parGCThreads; 295 this.edenSize = edenSize; 296 this.plabIsFixed = plabIsFixed; 297 this.objectsAreReachable = objectsAreReachable; 298 this.promotedByPLAB = promotedByPLAB; 299 } 300 301 /** 302 * Convert current TestCase to List of options. 303 * Assume test will fill half of existed eden. 304 * 305 * @return 306 * List of options 307 */ 308 public List<String> toOptions() { 309 return Arrays.asList("-XX:ParallelGCThreads=" + parGCThreads, 310 "-XX:ParallelGCBufferWastePct=" + wastePct, 311 "-XX:ParallelGCThreads=1", // Avoid dynamic sizing of threads. 312 "-XX:OldPLABSize=" + plabSize, 313 "-XX:YoungPLABSize=" + plabSize, 314 "-XX:" + (plabIsFixed ? "-" : "+") + "ResizePLAB", 315 "-Dchunk.size=" + chunkSize, 316 "-Dreachable=" + objectsAreReachable, 317 "-XX:NewSize=" + edenSize + "m", 318 "-XX:MaxNewSize=" + edenSize + "m", 319 "-Dmem.to.fill=" + getMemToFill() 320 ); 321 } 322 323 /** 324 * Print details about test case. 325 */ 326 public void print(PrintStream out) { 327 boolean expectPLABAllocation = promotedByPLAB && objectsAreReachable; 328 boolean expectDirectAllocation = (!promotedByPLAB) && objectsAreReachable; 329 330 out.println("Test case details:"); 331 out.println(" Young gen size : " + edenSize + "M"); 332 out.println(" Predefined PLAB size : " + plabSize); 333 out.println(" Parallel GC buffer waste pct : " + wastePct); 334 out.println(" Chunk size : " + chunkSize); 335 out.println(" Parallel GC threads : " + parGCThreads); 336 out.println(" Objects are created : " + (objectsAreReachable ? "reachable" : "unreachable")); 337 out.println(" PLAB size is fixed: " + (plabIsFixed ? "yes" : "no")); 338 out.println("Test expectations:"); 339 out.println(" PLAB allocation : " + (expectPLABAllocation ? "expected" : "unexpected")); 340 out.println(" Direct allocation : " + (expectDirectAllocation ? "expected" : "unexpected")); 341 } 342 343 /** 344 * @return 345 * true if we expect PLAB allocation 346 * false if no 347 */ 348 public boolean isPromotedByPLAB() { 349 return promotedByPLAB; 350 } 351 352 /** 353 * @return 354 * true if it is test case for unreachable objects 355 * false for live objects 356 */ 357 public boolean isDeadObjectCase() { 358 return !objectsAreReachable; 359 } 360 361 /** 362 * Returns amount of memory to fill 363 * 364 * @return amount of memory 365 */ 366 public long getMemToFill() { 367 return (long) (edenSize) * 1024l * 1024l / 2; 368 } 369 } 370 }