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