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
  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("desired_plab_sz");
 121                 })
 122                 .collect(Collectors.toCollection(ArrayList::new));
 123 
 124         // Check that desired plab size was changed during iterations.
 125         // It should decrease during first half of iterations
 126         // and increase after.
 127         List<Long> decreasedPlabs = plabSizes.subList(testCase.getIterations(), testCase.getIterations() * 2);
 128         List<Long> increasedPlabs = plabSizes.subList(testCase.getIterations() * 2, testCase.getIterations() * 3);
 129 
 130         Long prev = decreasedPlabs.get(0);
 131         for (int index = 1; index < decreasedPlabs.size(); ++index) {
 132             Long current = decreasedPlabs.get(index);
 133             if (prev < current) {
 134                 System.out.println(output);
 135                 throw new RuntimeException("Test failed! Expect that previous PLAB size should be greater than current. Prev.size: " + prev + " Current size:" + current);
 136             }
 137             prev = current;
 138         }
 139 
 140         prev = increasedPlabs.get(0);
 141         for (int index = 1; index < increasedPlabs.size(); ++index) {
 142             Long current = increasedPlabs.get(index);
 143             if (prev > current) {
 144                 System.out.println(output);
 145                 throw new RuntimeException("Test failed! Expect that previous PLAB size should be less than current. Prev.size: " + prev + " Current size:" + current);
 146             }
 147             prev = current;
 148         }
 149 
 150         System.out.println("Test passed!");
 151     }
 152 
 153     /**
 154      * Description of one test case.
 155      */
 156     private static class TestCase {
 157 
 158         private final int wastePct;
 159         private final int chunkSize;
 160         private final int parGCThreads;
 161         private final int iterations;
 162 
 163         /**
 164          * @param wastePct
 165          * ParallelGCBufferWastePct
 166          * @param chunkSize
 167          * requested object size for memory consumption
 168          * @param parGCThreads
 169          * -XX:ParallelGCThreads
 170          * @param iterations
 171          *
 172          */
 173         public TestCase(int wastePct,
 174                 int chunkSize,
 175                 int parGCThreads,
 176                 int iterations
 177         ) {
 178             if (wastePct == 0 || chunkSize == 0 || parGCThreads == 0 || iterations == 0) {
 179                 throw new IllegalArgumentException("Parameters should not be 0");
 180             }
 181             this.wastePct = wastePct;
 182 
 183             this.chunkSize = chunkSize;
 184             this.parGCThreads = parGCThreads;
 185             this.iterations = iterations;
 186         }
 187 
 188         /**
 189          * Convert current TestCase to List of options.
 190          *
 191          * @return
 192          * List of options
 193          */
 194         public List<String> toOptions() {
 195             return Arrays.asList("-XX:ParallelGCThreads=" + parGCThreads,
 196                     "-XX:ParallelGCBufferWastePct=" + wastePct,
 197                     "-XX:+ResizePLAB",
 198                     "-Dthreads=" + parGCThreads,
 199                     "-Dchunk.size=" + chunkSize,
 200                     "-Diterations=" + iterations,
 201                     "-XX:NewSize=16m",
 202                     "-XX:MaxNewSize=16m"
 203             );
 204         }
 205 
 206         /**
 207          * Print details about test case.
 208          */
 209         public void print(PrintStream out) {
 210             out.println("Test case details:");
 211             out.println("  Parallel GC buffer waste pct : " + wastePct);
 212             out.println("  Chunk size : " + chunkSize);
 213             out.println("  Parallel GC threads : " + parGCThreads);
 214             out.println("  Iterations: " + iterations);
 215         }
 216 
 217         /**
 218          * @return iterations
 219          */
 220         public int getIterations() {
 221             return iterations;
 222         }
 223     }
 224 }