1 /*
   2  * Copyright (c) 2016, 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
  27  * @summary Test PLAB promotions
  28  * @requires vm.gc=="G1" | vm.gc=="null"
  29  * @library /testlibrary /../../test/lib /
  30  * @modules java.management
  31  * @build ClassFileInstaller
  32  *        sun.hotspot.WhiteBox
  33  *        gc.g1.plab.lib.MemoryConsumer
  34  *        gc.g1.plab.lib.LogParser
  35  *        gc.g1.plab.lib.AppPLABPromotion
  36  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  37  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  38  * @run main gc.g1.plab.TestPLABPromotion
  39  */
  40 package gc.g1.plab;
  41 
  42 import java.util.List;
  43 import java.util.Map;
  44 import java.util.Arrays;
  45 import java.io.PrintStream;
  46 
  47 import gc.g1.plab.lib.AppPLABPromotion;
  48 import gc.g1.plab.lib.LogParser;
  49 import gc.g1.plab.lib.PLABUtils;
  50 
  51 import jdk.test.lib.OutputAnalyzer;
  52 import jdk.test.lib.ProcessTools;
  53 import jdk.test.lib.Platform;
  54 
  55 /**
  56  * Test checks PLAB promotion of different size objects.
  57  */
  58 public class TestPLABPromotion {
  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     // Threshold to determine whether the correct amount of objects were promoted.
  66     // This is only an approximate threshold for these checks.
  67     private final static long MEM_CONSUMPTION_THRESHOLD = 256l * 1024l;
  68 
  69     private static final int PLAB_SIZE_SMALL = 1024;
  70     private static final int PLAB_SIZE_MEDIUM = 4096;
  71     private static final int PLAB_SIZE_HIGH = 65536;
  72     private static final int OBJECT_SIZE_SMALL = 10;
  73     private static final int OBJECT_SIZE_MEDIUM = 100;
  74     private static final int OBJECT_SIZE_HIGH = 1000;
  75     private static final int GC_NUM_SMALL = 1;
  76     private static final int GC_NUM_MEDIUM = 3;
  77     private static final int GC_NUM_HIGH = 7;
  78     private static final int WASTE_PCT_SMALL = 10;
  79     private static final int WASTE_PCT_MEDIUM = 20;
  80     private static final int WASTE_PCT_HIGH = 30;
  81     private static final int YOUNG_SIZE_LOW = 16;
  82     private static final int YOUNG_SIZE_HIGH = 64;
  83     private static final boolean PLAB_FIXED = true;
  84     private static final boolean PLAB_DYNAMIC = false;
  85 
  86     private final static TestCase[] TEST_CASES = {
  87         // Test cases for unreachable object, PLAB size is fixed
  88         new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_SMALL, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_FIXED, false, false),
  89         new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_MEDIUM, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_FIXED, false, false),
  90         // Test cases for reachable objects, PLAB size is fixed
  91         new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_SMALL, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_FIXED, true, true),
  92         new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_MEDIUM, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_FIXED, true, true),
  93         new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_SMALL, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, YOUNG_SIZE_LOW, PLAB_FIXED, true, false),
  94         new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_HIGH, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_FIXED, true, true),
  95         new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_SMALL, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_FIXED, true, true),
  96         new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_MEDIUM, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, YOUNG_SIZE_LOW, PLAB_FIXED, true, true),
  97         new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_SMALL, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_FIXED, true, true),
  98         new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_HIGH, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_FIXED, true, true),
  99         new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_SMALL, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, YOUNG_SIZE_HIGH, PLAB_FIXED, true, false),
 100         // Test cases for unreachable object, PLAB size is not fixed
 101         new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_MEDIUM, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_LOW, PLAB_DYNAMIC, false, false),
 102         // Test cases for reachable objects, PLAB size is not fixed
 103         new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_HIGH, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_DYNAMIC, true, true),
 104         new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_MEDIUM, OBJECT_SIZE_SMALL, GC_NUM_SMALL, YOUNG_SIZE_LOW, PLAB_DYNAMIC, true, true),
 105         new TestCase(WASTE_PCT_SMALL, PLAB_SIZE_MEDIUM, OBJECT_SIZE_HIGH, GC_NUM_HIGH, YOUNG_SIZE_HIGH, PLAB_DYNAMIC, true, false),
 106         new TestCase(WASTE_PCT_MEDIUM, PLAB_SIZE_SMALL, OBJECT_SIZE_MEDIUM, GC_NUM_MEDIUM, YOUNG_SIZE_LOW, PLAB_DYNAMIC, true, true),
 107         new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_HIGH, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, YOUNG_SIZE_HIGH, PLAB_DYNAMIC, true, true),
 108         new TestCase(WASTE_PCT_HIGH, PLAB_SIZE_HIGH, OBJECT_SIZE_SMALL, GC_NUM_HIGH, YOUNG_SIZE_LOW, PLAB_DYNAMIC, true, true)
 109     };
 110 
 111     public static void main(String[] args) throws Throwable {
 112 
 113         for (TestCase testCase : TEST_CASES) {
 114             // What we going to check.
 115             testCase.print(System.out);
 116             List<String> options = PLABUtils.prepareOptions(testCase.toOptions());
 117             options.add(AppPLABPromotion.class.getName());
 118             OutputAnalyzer out = ProcessTools.executeTestJvm(options.toArray(new String[options.size()]));
 119             if (out.getExitValue() != 0) {
 120                 System.out.println(out.getOutput());
 121                 throw new RuntimeException("Expect exit code 0.");
 122             }
 123             checkResults(out.getOutput(), testCase);
 124         }
 125     }
 126 
 127     private static void checkResults(String output, TestCase testCase) {
 128         long plabAllocatedSurvivor;
 129         long directAllocatedSurvivor;
 130         long plabAllocatedOld;
 131         long directAllocatedOld;
 132         long memAllocated = testCase.getMemToFill();
 133         long wordSize = Platform.is32bit() ? 4l : 8l;
 134         LogParser logParser = new LogParser(output);
 135 
 136         Map<String, Long> survivorStats = getPlabStats(logParser, LogParser.ReportType.SURVIVOR_STATS, GC_ID_SURVIVOR_STATS);
 137         Map<String, Long> oldStats = getPlabStats(logParser, LogParser.ReportType.OLD_STATS, GC_ID_OLD_STATS);
 138 
 139         plabAllocatedSurvivor = wordSize * survivorStats.get("used");
 140         directAllocatedSurvivor = wordSize * survivorStats.get("direct_allocated");
 141         plabAllocatedOld = wordSize * oldStats.get("used");
 142         directAllocatedOld = wordSize * oldStats.get("direct_allocated");
 143 
 144         System.out.printf("Survivor PLAB allocated:%17d Direct allocated: %17d Mem consumed:%17d%n", plabAllocatedSurvivor, directAllocatedSurvivor, memAllocated);
 145         System.out.printf("Old      PLAB allocated:%17d Direct allocated: %17d Mem consumed:%17d%n", plabAllocatedOld, directAllocatedOld, memAllocated);
 146 
 147         System.out.println(" " + plabAllocatedSurvivor + " " + directAllocatedSurvivor + " " + plabAllocatedOld + " " + directAllocatedOld + " " + memAllocated);
 148         System.out.println(" " + (memAllocated - plabAllocatedSurvivor));
 149         System.out.println(" " + (memAllocated - directAllocatedSurvivor));
 150         System.out.println(" " + (memAllocated - plabAllocatedOld));
 151         System.out.println(" " + (memAllocated - directAllocatedOld));
 152 
 153         // Unreachable objects case
 154         if (testCase.isDeadObjectCase()) {
 155             // No dead objects should be promoted
 156             if (plabAllocatedSurvivor > MEM_CONSUMPTION_THRESHOLD || directAllocatedSurvivor > MEM_CONSUMPTION_THRESHOLD) {
 157                 System.out.println(output);
 158                 throw new RuntimeException("Unreachable objects should not be allocated using PLAB or direct allocated to Survivor");
 159             }
 160             if (plabAllocatedOld > MEM_CONSUMPTION_THRESHOLD || directAllocatedOld > MEM_CONSUMPTION_THRESHOLD) {
 161                 System.out.println(output);
 162                 throw new RuntimeException("Unreachable objects should not be allocated using PLAB or direct allocated to Old");
 163             }
 164         } else {
 165             // Live objects case
 166             if (testCase.isPromotedByPLAB()) {
 167                 // All live small objects should be promoted using PLAB
 168                 if (Math.abs(plabAllocatedSurvivor - memAllocated) > MEM_CONSUMPTION_THRESHOLD) {
 169                     System.out.println(output);
 170                     throw new RuntimeException("Expect that Survivor PLAB allocation are similar to all mem consumed");
 171                 }
 172                 if (Math.abs(plabAllocatedOld - memAllocated) > MEM_CONSUMPTION_THRESHOLD) {
 173                     System.out.println(output);
 174                     throw new RuntimeException("Expect that Old PLAB allocation are similar to all mem consumed");
 175                 }
 176             } else {
 177                 // All big objects should be directly allocated
 178                 if (Math.abs(directAllocatedSurvivor - memAllocated) > MEM_CONSUMPTION_THRESHOLD) {
 179                     System.out.println(output);
 180                     throw new RuntimeException("Test fails. Expect that Survivor direct allocation are similar to all mem consumed");
 181                 }
 182                 if (Math.abs(directAllocatedOld - memAllocated) > MEM_CONSUMPTION_THRESHOLD) {
 183                     System.out.println(output);
 184                     throw new RuntimeException("Test fails. Expect that Old direct allocation are similar to all mem consumed");
 185                 }
 186             }
 187 
 188             // All promoted objects size should be similar to all consumed memory
 189             if (Math.abs(plabAllocatedSurvivor + directAllocatedSurvivor - memAllocated) > MEM_CONSUMPTION_THRESHOLD) {
 190                 System.out.println(output);
 191                 throw new RuntimeException("Test fails. Expect that Survivor gen total allocation are similar to all mem consumed");
 192             }
 193             if (Math.abs(plabAllocatedOld + directAllocatedOld - memAllocated) > MEM_CONSUMPTION_THRESHOLD) {
 194                 System.out.println(output);
 195                 throw new RuntimeException("Test fails. Expect that Old gen total allocation are similar to all mem consumed");
 196             }
 197         }
 198         System.out.println("Test passed!");
 199     }
 200 
 201     private static Map<String, Long> getPlabStats(LogParser logParser, LogParser.ReportType type, long gc_id) {
 202 
 203         Map<String, Long> survivorStats = logParser.getEntries()
 204                 .get(gc_id)
 205                 .get(type);
 206         return survivorStats;
 207     }
 208 
 209     /**
 210      * Description of one test case.
 211      */
 212     private static class TestCase {
 213 
 214         private final int wastePct;
 215         private final int plabSize;
 216         private final int chunkSize;
 217         private final int parGCThreads;
 218         private final int edenSize;
 219         private final boolean plabIsFixed;
 220         private final boolean objectsAreReachable;
 221         private final boolean promotedByPLAB;
 222 
 223         /**
 224          * @param wastePct
 225          * ParallelGCBufferWastePct
 226          * @param plabSize
 227          * -XX:OldPLABSize and -XX:YoungPLABSize
 228          * @param chunkSize
 229          * requested object size for memory consumption
 230          * @param parGCThreads
 231          * -XX:ParallelGCThreads
 232          * @param edenSize
 233          * NewSize and MaxNewSize
 234          * @param plabIsFixed
 235          * Use dynamic PLAB or fixed size PLAB
 236          * @param objectsAreReachable
 237          * true - allocate live objects
 238          * false - allocate unreachable objects
 239          * @param promotedByPLAB
 240          * true - we expect to see PLAB allocation during promotion
 241          * false - objects will be directly allocated during promotion
 242          */
 243         public TestCase(int wastePct,
 244                 int plabSize,
 245                 int chunkSize,
 246                 int parGCThreads,
 247                 int edenSize,
 248                 boolean plabIsFixed,
 249                 boolean objectsAreReachable,
 250                 boolean promotedByPLAB
 251         ) {
 252             if (wastePct == 0 || plabSize == 0 || chunkSize == 0 || parGCThreads == 0 || edenSize == 0) {
 253                 throw new IllegalArgumentException("Parameters should not be 0");
 254             }
 255             this.wastePct = wastePct;
 256             this.plabSize = plabSize;
 257             this.chunkSize = chunkSize;
 258             this.parGCThreads = parGCThreads;
 259             this.edenSize = edenSize;
 260             this.plabIsFixed = plabIsFixed;
 261             this.objectsAreReachable = objectsAreReachable;
 262             this.promotedByPLAB = promotedByPLAB;
 263         }
 264 
 265         /**
 266          * Convert current TestCase to List of options.
 267          * Assume test will fill half of existed eden.
 268          *
 269          * @return
 270          * List of options
 271          */
 272         public List<String> toOptions() {
 273             return Arrays.asList("-XX:ParallelGCThreads=" + parGCThreads,
 274                     "-XX:ParallelGCBufferWastePct=" + wastePct,
 275                     "-XX:OldPLABSize=" + plabSize,
 276                     "-XX:YoungPLABSize=" + plabSize,
 277                     "-XX:" + (plabIsFixed ? "-" : "+") + "ResizePLAB",
 278                     "-Dchunk.size=" + chunkSize,
 279                     "-Dreachable=" + objectsAreReachable,
 280                     "-XX:NewSize=" + edenSize + "m",
 281                     "-XX:MaxNewSize=" + edenSize + "m",
 282                     "-Dmem.to.fill=" + getMemToFill()
 283             );
 284         }
 285 
 286         /**
 287          * Print details about test case.
 288          */
 289         public void print(PrintStream out) {
 290             boolean expectPLABAllocation = promotedByPLAB && objectsAreReachable;
 291             boolean expectDirectAllocation = (!promotedByPLAB) && objectsAreReachable;
 292 
 293             out.println("Test case details:");
 294             out.println("  Young gen size : " + edenSize + "M");
 295             out.println("  Predefined PLAB size : " + plabSize);
 296             out.println("  Parallel GC buffer waste pct : " + wastePct);
 297             out.println("  Chunk size : " + chunkSize);
 298             out.println("  Parallel GC threads : " + parGCThreads);
 299             out.println("  Objects are created : " + (objectsAreReachable ? "reachable" : "unreachable"));
 300             out.println("  PLAB size is fixed: " + (plabIsFixed ? "yes" : "no"));
 301             out.println("Test expectations:");
 302             out.println("  PLAB allocation : " + (expectPLABAllocation ? "expected" : "unexpected"));
 303             out.println("  Direct allocation : " + (expectDirectAllocation ? "expected" : "unexpected"));
 304         }
 305 
 306         /**
 307          * @return
 308          * true if we expect PLAB allocation
 309          * false if no
 310          */
 311         public boolean isPromotedByPLAB() {
 312             return promotedByPLAB;
 313         }
 314 
 315         /**
 316          * @return
 317          * true if it is test case for unreachable objects
 318          * false for live objects
 319          */
 320         public boolean isDeadObjectCase() {
 321             return !objectsAreReachable;
 322         }
 323 
 324         /**
 325          * Returns amount of memory to fill
 326          *
 327          * @return amount of memory
 328          */
 329         public long getMemToFill() {
 330             return (long) (edenSize) * 1024l * 1024l / 2;
 331         }
 332     }
 333 }