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 TestPLABEvacuationFailure
  26  * @bug 8148376
  27  * @summary Checks PLAB statistics on evacuation failure
  28  * @requires vm.gc.G1
  29  * @library /testlibrary /
  30  * @modules java.base/jdk.internal.misc
  31  * @modules java.management
  32  * @build gc.g1.plab.lib.LogParser
  33  *        gc.g1.plab.lib.AppPLABEvacuationFailure
  34  * @run main gc.g1.plab.TestPLABEvacuationFailure
  35  */
  36 package gc.g1.plab;
  37 
  38 import java.util.Arrays;
  39 import java.util.List;
  40 import java.util.Map;
  41 import java.util.ArrayList;
  42 import java.util.Collections;
  43 import java.util.regex.Pattern;
  44 import java.util.stream.Collectors;
  45 
  46 import jdk.test.lib.OutputAnalyzer;
  47 import jdk.test.lib.ProcessTools;
  48 import jdk.test.lib.Utils;
  49 
  50 import gc.g1.plab.lib.LogParser;
  51 import gc.g1.plab.lib.AppPLABEvacuationFailure;
  52 import gc.g1.plab.lib.PlabInfo;
  53 
  54 /**
  55  * The test runs the AppPLABEvacuationFailure application to provoke a number of
  56  * Evacuation Failures, parses GC log and analyzes PLAB statistics. The test checks
  57  * that both fields 'failure_waste' and 'failure_used' for Evacuation Failure statistic
  58  * are non zero, and zero for other statistics.
  59  */
  60 public class TestPLABEvacuationFailure {
  61 
  62     /* PLAB statistics fields which are checked.
  63      * Test expects to find 0 in this fields in survivor statistics.
  64      * Expects to find 0 in old statistics for GC when evacuation failure
  65      * did not happen. And expects to find not 0 in old statistics in case when
  66      * GC causes to evacuation failure.
  67      */
  68     private static final List<String> FAILURE_STAT_FIELDS = new ArrayList<>(Arrays.asList(
  69             "failure used",
  70             "failure wasted"));
  71 
  72     private static final String[] COMMON_OPTIONS = {
  73         "-Xlog:gc=debug,gc+plab=debug,gc+phases=trace",
  74         "-XX:+UseG1GC",
  75         "-XX:InitiatingHeapOccupancyPercent=100",
  76         "-XX:-G1UseAdaptiveIHOP",
  77         "-XX:G1HeapRegionSize=1m"};
  78 
  79     private static final Pattern GC_ID_PATTERN = Pattern.compile("GC\\((\\d+)\\)");
  80     private static List<Long> evacuationFailureIDs;
  81     private static LogParser logParser;
  82     private static String appPlabEvacFailureOutput;
  83 
  84     public static void main(String[] args) throws Throwable {
  85         // ParallelGCBufferWastePct, PLAB Size, ParallelGCBufferWastePct, MaxHeapSize, is plab fixed.
  86         runTest(10, 1024, 3, 16, true);
  87         runTest(15, 2048, 4, 256, true);
  88         runTest(20, 65536, 7, 128, false);
  89         runTest(25, 1024, 3, 16, true);
  90         runTest(30, 16384, 7, 256, false);
  91         runTest(10, 65536, 4, 32, false);
  92     }
  93 
  94     private static void runTest(int wastePct, int plabSize, int parGCThreads, int heapSize, boolean plabIsFixed) throws Throwable {
  95         System.out.println("Test case details:");
  96         System.out.println("  Heap size : " + heapSize + "M");
  97         System.out.println("  Initial PLAB size : " + plabSize);
  98         System.out.println("  Parallel GC buffer waste pct : " + wastePct);
  99         System.out.println("  Parallel GC threads : " + parGCThreads);
 100         System.out.println("  PLAB size is fixed: " + (plabIsFixed ? "yes" : "no"));
 101         // Set up test GC and PLAB options
 102         List<String> testOptions = new ArrayList<>();
 103         Collections.addAll(testOptions, COMMON_OPTIONS);
 104         Collections.addAll(testOptions, Utils.getTestJavaOpts());
 105         Collections.addAll(testOptions,
 106                 "-XX:ParallelGCThreads=" + parGCThreads,
 107                 "-XX:ParallelGCBufferWastePct=" + wastePct,
 108                 "-XX:OldPLABSize=" + plabSize,
 109                 "-XX:YoungPLABSize=" + plabSize,
 110                 "-XX:" + (plabIsFixed ? "-" : "+") + "ResizePLAB",
 111                 "-XX:MaxHeapSize=" + heapSize + "m");
 112         testOptions.add(AppPLABEvacuationFailure.class.getName());
 113         OutputAnalyzer out = ProcessTools.executeTestJvm(testOptions.toArray(new String[testOptions.size()]));
 114 
 115         appPlabEvacFailureOutput = out.getOutput();
 116         if (out.getExitValue() != 0) {
 117             System.out.println(appPlabEvacFailureOutput);
 118             throw new RuntimeException("Expect exit code 0.");
 119         }
 120         // Get list of GC ID on evacuation failure
 121         evacuationFailureIDs = getGcIdPlabEvacFailures(out);
 122         logParser = new LogParser(appPlabEvacFailureOutput);
 123         checkResults();
 124     }
 125 
 126     private static void checkResults() {
 127 
 128         if (evacuationFailureIDs.isEmpty()) {
 129             System.out.println(appPlabEvacFailureOutput);
 130             throw new RuntimeException("AppPLABEvacuationFailure did not reach Evacuation Failure.");
 131         }
 132 
 133         Map<Long, PlabInfo> valuesToCheck = getNonEvacFailureSurvivorStats();
 134         checkValuesIsZero(valuesToCheck, "Expect that SURVIVOR PLAB failure statistics should be 0 when no evacuation failure");
 135 
 136         valuesToCheck = getNonEvacFailureOldStats();
 137         checkValuesIsZero(valuesToCheck, "Expect that OLD PLAB failure statistics should be 0 when no evacuation failure");
 138 
 139         valuesToCheck = getEvacFailureSurvivorStats();
 140         checkValuesIsZero(valuesToCheck, "Expect that failure statistics should be 0 in SURVIVOR PLAB statistics at evacuation failure");
 141 
 142         valuesToCheck = getEvacFailureOldStats();
 143         checkValuesIsNotZero(valuesToCheck, "Expect that failure statistics should not be 0 in OLD PLAB statistics at evacuation failure");
 144     }
 145 
 146     /**
 147      * Checks logItems for non-zero values. Throws RuntimeException if found.
 148      *
 149      * @param logItems
 150      * @param errorMessage
 151      */
 152     private static void checkValuesIsZero(Map<Long, PlabInfo> logItems, String errorMessage) {
 153         checkValues(logItems, errorMessage, true);
 154     }
 155 
 156     /**
 157      * Checks logItems for zero values. Throws RuntimeException if found.
 158      *
 159      * @param logItems
 160      * @param errorMessage
 161      */
 162     private static void checkValuesIsNotZero(Map<Long, PlabInfo> logItems, String errorMessage) {
 163         checkValues(logItems, errorMessage, false);
 164     }
 165 
 166     private static void checkValues(Map<Long, PlabInfo> logItems, String errorMessage, boolean expectZero) {
 167         logItems.entrySet()
 168                 .forEach(item -> item.getValue()
 169                         .values()
 170                         .forEach(items -> {
 171                             if (expectZero != (items == 0)) {
 172                                 System.out.println(appPlabEvacFailureOutput);
 173                                 throw new RuntimeException(errorMessage);
 174                             }
 175                         })
 176                 );
 177     }
 178 
 179     /**
 180      * For tracking PLAB statistics for specified PLAB type - survivor and old
 181      */
 182     private static Map<Long, PlabInfo> getNonEvacFailureSurvivorStats() {
 183         return logParser.getExcludedSpecifiedStats(evacuationFailureIDs, LogParser.ReportType.SURVIVOR_STATS, FAILURE_STAT_FIELDS);
 184     }
 185 
 186     private static Map<Long, PlabInfo> getNonEvacFailureOldStats() {
 187         return logParser.getExcludedSpecifiedStats(evacuationFailureIDs, LogParser.ReportType.OLD_STATS, FAILURE_STAT_FIELDS);
 188     }
 189 
 190     private static Map<Long, PlabInfo> getEvacFailureSurvivorStats() {
 191         return logParser.getSpecifiedStats(evacuationFailureIDs, LogParser.ReportType.SURVIVOR_STATS, FAILURE_STAT_FIELDS);
 192     }
 193 
 194     private static Map<Long, PlabInfo> getEvacFailureOldStats() {
 195         return logParser.getSpecifiedStats(evacuationFailureIDs, LogParser.ReportType.OLD_STATS, FAILURE_STAT_FIELDS);
 196     }
 197 
 198     private static List<Long> getGcIdPlabEvacFailures(OutputAnalyzer out) {
 199         return out.asLines().stream()
 200                 .filter(line -> line.contains("Evacuation Failure"))
 201                 .map(line -> LogParser.getGcIdFromLine(line, GC_ID_PATTERN))
 202                 .collect(Collectors.toList());
 203     }
 204 }