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.management
  32  * @build ClassFileInstaller
  33  *        sun.hotspot.WhiteBox
  34  *        gc.g1.plab.lib.LogParser
  35  *        gc.g1.plab.lib.MemoryConsumer
  36  *        gc.g1.plab.lib.PLABUtils
  37  *        gc.g1.plab.lib.AppPLABResize
  38  * @ignore 8150183
  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             if (out.getExitValue() != 0) {
  98                 System.out.println(out.getOutput());
  99                 throw new RuntimeException("Exit code is not 0");
 100             }
 101             checkResults(out.getOutput(), testCase);
 102         }
 103     }
 104 
 105     /**
 106      * Checks testing results.
 107      * Expected results - desired PLAB size is decreased and increased during promotion to Survivor.
 108      *
 109      * @param output - VM output
 110      * @param testCase
 111      */
 112     private static void checkResults(String output, TestCase testCase) {
 113         final LogParser log = new LogParser(output);
 114         final PlabReport report = log.getEntries();
 115 
 116         final List<Long> plabSizes = report.entryStream()
 117                 .map(item -> item.getValue()
 118                         .get(LogParser.ReportType.SURVIVOR_STATS)
 119                         .get(PLAB_SIZE_FIELD_NAME)
 120                 )
 121                 .collect(Collectors.toList());
 122 
 123         // Check that desired plab size was changed during iterations.
 124         // The test case does 3 rounds of allocations.  The second round of N allocations and GC's
 125         // has a decreasing size of allocations so that iterations N to 2*N -1 will be of decreasing size.
 126         // The third round with iterations 2*N to 3*N -1 has increasing sizes of allocation.
 127         long startDesiredPLABSize = plabSizes.get(testCase.getIterations());
 128         long endDesiredPLABSize = plabSizes.get(testCase.getIterations() * 2 - 1);
 129 
 130         if (startDesiredPLABSize < endDesiredPLABSize) {
 131             System.out.println(output);
 132             throw new RuntimeException("Test failed! Expect that initial PLAB size should be greater than checked. Initial size: " + startDesiredPLABSize + " Checked size:" + endDesiredPLABSize);
 133         }
 134 
 135         startDesiredPLABSize = plabSizes.get(testCase.getIterations() * 2);
 136         endDesiredPLABSize = plabSizes.get(testCase.getIterations() * 3 - 1);
 137 
 138         if (startDesiredPLABSize > endDesiredPLABSize) {
 139             System.out.println(output);
 140             throw new RuntimeException("Test failed! Expect that initial PLAB size should be less than checked. Initial size: " + startDesiredPLABSize + " Checked size:" + endDesiredPLABSize);
 141         }
 142 
 143         System.out.println("Test passed!");
 144     }
 145 
 146     /**
 147      * Description of one test case.
 148      */
 149     private static class TestCase {
 150 
 151         private final int wastePct;
 152         private final int chunkSize;
 153         private final int parGCThreads;
 154         private final int iterations;
 155 
 156         /**
 157          * @param wastePct
 158          * ParallelGCBufferWastePct
 159          * @param chunkSize
 160          * requested object size for memory consumption
 161          * @param parGCThreads
 162          * -XX:ParallelGCThreads
 163          * @param iterations
 164          *
 165          */
 166         public TestCase(int wastePct,
 167                 int chunkSize,
 168                 int parGCThreads,
 169                 int iterations
 170         ) {
 171             if (wastePct == 0 || chunkSize == 0 || parGCThreads == 0 || iterations == 0) {
 172                 throw new IllegalArgumentException("Parameters should not be 0");
 173             }
 174             this.wastePct = wastePct;
 175 
 176             this.chunkSize = chunkSize;
 177             this.parGCThreads = parGCThreads;
 178             this.iterations = iterations;
 179         }
 180 
 181         /**
 182          * Convert current TestCase to List of options.
 183          *
 184          * @return
 185          * List of options
 186          */
 187         public List<String> toOptions() {
 188             return Arrays.asList("-XX:ParallelGCThreads=" + parGCThreads,
 189                     "-XX:ParallelGCBufferWastePct=" + wastePct,
 190                     "-XX:+ResizePLAB",
 191                     "-Dchunk.size=" + chunkSize,
 192                     "-Diterations=" + iterations,
 193                     "-XX:NewSize=16m",
 194                     "-XX:MaxNewSize=16m"
 195             );
 196         }
 197 
 198         /**
 199          * Print details about test case.
 200          */
 201         public void print(PrintStream out) {
 202             out.println("Test case details:");
 203             out.println("  Parallel GC buffer waste pct : " + wastePct);
 204             out.println("  Chunk size : " + chunkSize);
 205             out.println("  Parallel GC threads : " + parGCThreads);
 206             out.println("  Iterations: " + iterations);
 207         }
 208 
 209         /**
 210          * @return iterations
 211          */
 212         public int getIterations() {
 213             return iterations;
 214         }
 215     }
 216 }