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 TestPLABResize
  26  * @bug 8141278 8141141
  27  * @summary Test for PLAB resizing
  28  * @requires vm.gc=="G1" | vm.gc=="null"
  29  * @requires vm.opt.FlightRecorder != true
  30  * @library /testlibrary /test/lib /
  31  * @modules java.base/jdk.internal.misc
  32  * @modules java.management
  33  * @build ClassFileInstaller
  34  *        sun.hotspot.WhiteBox
  35  *        gc.g1.plab.lib.LogParser
  36  *        gc.g1.plab.lib.MemoryConsumer
  37  *        gc.g1.plab.lib.PLABUtils
  38  *        gc.g1.plab.lib.AppPLABResize
  39  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  40  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  41  * @run main gc.g1.plab.TestPLABResize
  42  */
  43 package gc.g1.plab;
  44 
  45 import java.util.Arrays;
  46 import java.util.List;
  47 import java.util.stream.Collectors;
  48 import java.io.PrintStream;
  49 
  50 import gc.g1.plab.lib.LogParser;
  51 import gc.g1.plab.lib.PLABUtils;
  52 import gc.g1.plab.lib.AppPLABResize;
  53 import gc.g1.plab.lib.PlabReport;
  54 
  55 import jdk.test.lib.OutputAnalyzer;
  56 import jdk.test.lib.ProcessTools;
  57 
  58 /**
  59  * Test for PLAB resizing.
  60  */
  61 public class TestPLABResize {
  62 
  63     private static final int OBJECT_SIZE_SMALL = 10;
  64     private static final int OBJECT_SIZE_MEDIUM = 70;
  65     private static final int OBJECT_SIZE_HIGH = 150;
  66     private static final int GC_NUM_SMALL = 1;
  67     private static final int GC_NUM_MEDIUM = 3;
  68     private static final int GC_NUM_HIGH = 7;
  69     private static final int WASTE_PCT_SMALL = 10;
  70     private static final int WASTE_PCT_MEDIUM = 20;
  71     private static final int WASTE_PCT_HIGH = 30;
  72 
  73     private static final int ITERATIONS_SMALL = 3;
  74     private static final int ITERATIONS_MEDIUM = 5;
  75     private static final int ITERATIONS_HIGH = 8;
  76 
  77     private static final String PLAB_SIZE_FIELD_NAME = "actual";
  78 
  79     private final static TestCase[] TEST_CASES = {
  80         new TestCase(WASTE_PCT_SMALL, OBJECT_SIZE_SMALL, GC_NUM_SMALL, ITERATIONS_MEDIUM),
  81         new TestCase(WASTE_PCT_SMALL, OBJECT_SIZE_MEDIUM, GC_NUM_HIGH, ITERATIONS_SMALL),
  82         new TestCase(WASTE_PCT_SMALL, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, ITERATIONS_HIGH),
  83         new TestCase(WASTE_PCT_MEDIUM, OBJECT_SIZE_SMALL, GC_NUM_HIGH, ITERATIONS_MEDIUM),
  84         new TestCase(WASTE_PCT_MEDIUM, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, ITERATIONS_SMALL),
  85         new TestCase(WASTE_PCT_MEDIUM, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, ITERATIONS_HIGH),
  86         new TestCase(WASTE_PCT_HIGH, OBJECT_SIZE_SMALL, GC_NUM_HIGH, ITERATIONS_MEDIUM),
  87         new TestCase(WASTE_PCT_HIGH, OBJECT_SIZE_MEDIUM, GC_NUM_SMALL, ITERATIONS_SMALL),
  88         new TestCase(WASTE_PCT_HIGH, OBJECT_SIZE_HIGH, GC_NUM_MEDIUM, ITERATIONS_HIGH)
  89     };
  90 
  91     public static void main(String[] args) throws Throwable {
  92         for (TestCase testCase : TEST_CASES) {
  93             testCase.print(System.out);
  94             List<String> options = PLABUtils.prepareOptions(testCase.toOptions());
  95             options.add(AppPLABResize.class.getName());
  96             OutputAnalyzer out = ProcessTools.executeTestJvm(options.toArray(new String[options.size()]));
  97             PLABUtils.commonCheck(out);
  98             checkResults(out.getOutput(), testCase);
  99         }
 100     }
 101 
 102     /**
 103      * Checks testing results.
 104      * Expected results - desired PLAB size is decreased and increased during promotion to Survivor.
 105      *
 106      * @param output - VM output
 107      * @param testCase
 108      */
 109     private static void checkResults(String output, TestCase testCase) {
 110         final LogParser log = new LogParser(output);
 111         final PlabReport report = log.getEntries();
 112 
 113         final List<Long> plabSizes = report.entryStream()
 114                 .map(item -> item.getValue()
 115                         .get(LogParser.ReportType.SURVIVOR_STATS)
 116                         .get(PLAB_SIZE_FIELD_NAME)
 117                 )
 118                 .collect(Collectors.toList());
 119 
 120         // Check that desired plab size was changed during iterations.
 121         // The test case does 3 rounds of allocations.  The second round of N allocations and GC's
 122         // has a decreasing size of allocations so that iterations N to 2*N -1 will be of decreasing size.
 123         // The third round with iterations 2*N to 3*N -1 has increasing sizes of allocation.
 124         if ( plabSizes.size() != testCase.iterations * 3 ) {
 125             System.out.println(output);
 126             throw new RuntimeException ("Expects for " + testCase.iterations * 3 + " PLAB entries in log, found " + plabSizes.size());
 127         }
 128 
 129         long startDesiredPLABSize = plabSizes.get(testCase.getIterations());
 130         long endDesiredPLABSize = plabSizes.get(testCase.getIterations() * 2 - 1);
 131 
 132         if (startDesiredPLABSize < endDesiredPLABSize) {
 133             System.out.println(output);
 134             throw new RuntimeException("Test failed! Expect that initial PLAB size should be greater than checked. Initial size: " + startDesiredPLABSize + " Checked size:" + endDesiredPLABSize);
 135         }
 136 
 137         startDesiredPLABSize = plabSizes.get(testCase.getIterations() * 2);
 138         endDesiredPLABSize = plabSizes.get(testCase.getIterations() * 3 - 1);
 139 
 140         if (startDesiredPLABSize > endDesiredPLABSize) {
 141             System.out.println(output);
 142             throw new RuntimeException("Test failed! Expect that initial PLAB size should be less than checked. Initial size: " + startDesiredPLABSize + " Checked size:" + endDesiredPLABSize);
 143         }
 144 
 145         System.out.println("Test passed!");
 146     }
 147 
 148     /**
 149      * Description of one test case.
 150      */
 151     private static class TestCase {
 152 
 153         private final int wastePct;
 154         private final int chunkSize;
 155         private final int parGCThreads;
 156         private final int iterations;
 157 
 158         /**
 159          * @param wastePct
 160          * ParallelGCBufferWastePct
 161          * @param chunkSize
 162          * requested object size for memory consumption
 163          * @param parGCThreads
 164          * -XX:ParallelGCThreads
 165          * @param iterations
 166          *
 167          */
 168         public TestCase(int wastePct,
 169                 int chunkSize,
 170                 int parGCThreads,
 171                 int iterations
 172         ) {
 173             if (wastePct == 0 || chunkSize == 0 || parGCThreads == 0 || iterations == 0) {
 174                 throw new IllegalArgumentException("Parameters should not be 0");
 175             }
 176             this.wastePct = wastePct;
 177 
 178             this.chunkSize = chunkSize;
 179             this.parGCThreads = parGCThreads;
 180             this.iterations = iterations;
 181         }
 182 
 183         /**
 184          * Convert current TestCase to List of options.
 185          *
 186          * @return
 187          * List of options
 188          */
 189         public List<String> toOptions() {
 190             return Arrays.asList("-XX:ParallelGCThreads=" + parGCThreads,
 191                     "-XX:ParallelGCBufferWastePct=" + wastePct,
 192                     "-XX:+ResizePLAB",
 193                     "-Dchunk.size=" + chunkSize,
 194                     "-Diterations=" + iterations,
 195                     "-XX:NewSize=16m",
 196                     "-XX:MaxNewSize=16m"
 197             );
 198         }
 199 
 200         /**
 201          * Print details about test case.
 202          */
 203         public void print(PrintStream out) {
 204             out.println("Test case details:");
 205             out.println("  Parallel GC buffer waste pct : " + wastePct);
 206             out.println("  Chunk size : " + chunkSize);
 207             out.println("  Parallel GC threads : " + parGCThreads);
 208             out.println("  Iterations: " + iterations);
 209         }
 210 
 211         /**
 212          * @return iterations
 213          */
 214         public int getIterations() {
 215             return iterations;
 216         }
 217     }
 218 }