1 /*
   2  * Copyright (c) 2013, 2020, 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 package gc.g1;
  25 
  26 /*
  27  * Common helpers for TestRemsetLogging* tests
  28  */
  29 
  30 import sun.hotspot.WhiteBox;
  31 
  32 import jdk.test.lib.process.ProcessTools;
  33 import jdk.test.lib.process.OutputAnalyzer;
  34 import java.util.ArrayList;
  35 import java.util.Arrays;
  36 
  37 class VerifySummaryOutput {
  38     public static void main(String[] args) {
  39         int numGCs = Integer.parseInt(args[0]);
  40 
  41         // Perform the requested amount of GCs.
  42         WhiteBox wb = WhiteBox.getWhiteBox();
  43         for (int i = 0; i < numGCs - 1; i++) {
  44             wb.youngGC();
  45         }
  46         if (numGCs > 0) {
  47           wb.fullGC();
  48         }
  49     }
  50 }
  51 
  52 public class TestRemsetLoggingTools {
  53 
  54     public static String runTest(String[] additionalArgs, int numGCs) throws Exception {
  55         ArrayList<String> finalargs = new ArrayList<String>();
  56         String[] defaultArgs = new String[] {
  57             "-Xbootclasspath/a:.",
  58             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
  59             "-cp", System.getProperty("java.class.path"),
  60             "-XX:+UseG1GC",
  61             "-Xmn4m",
  62             "-Xint", // -Xint makes the test run faster
  63             "-Xms20m",
  64             "-Xmx20m",
  65             "-XX:ParallelGCThreads=1",
  66             "-XX:InitiatingHeapOccupancyPercent=100", // we don't want the additional GCs due to initial marking
  67             "-XX:+UnlockDiagnosticVMOptions",
  68             "-XX:G1HeapRegionSize=1M",
  69         };
  70 
  71         finalargs.addAll(Arrays.asList(defaultArgs));
  72 
  73         if (additionalArgs != null) {
  74             finalargs.addAll(Arrays.asList(additionalArgs));
  75         }
  76 
  77         finalargs.add(VerifySummaryOutput.class.getName());
  78         finalargs.add(String.valueOf(numGCs));
  79 
  80         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs);
  81         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  82 
  83         output.shouldHaveExitValue(0);
  84 
  85         String result = output.getStdout();
  86         return result;
  87     }
  88 
  89     private static void checkCounts(int expected, int actual, String which) throws Exception {
  90         if (expected != actual) {
  91             throw new Exception("RSet summaries mention " + which + " regions an incorrect number of times. Expected " + expected + ", got " + actual);
  92         }
  93     }
  94 
  95     public static void expectPerRegionRSetSummaries(String result, int expectedCumulative, int expectedPeriodic) throws Exception {
  96         expectRSetSummaries(result, expectedCumulative, expectedPeriodic);
  97         int actualYoung = result.split("Young regions").length - 1;
  98         int actualHumongous = result.split("Humongous regions").length - 1;
  99         int actualFree = result.split("Free regions").length - 1;
 100         int actualOther = result.split("Old regions").length - 1;
 101 
 102         // the strings we check for above are printed four times per summary
 103         int expectedPerRegionTypeInfo = (expectedCumulative + expectedPeriodic) * 4;
 104 
 105         checkCounts(expectedPerRegionTypeInfo, actualYoung, "Young");
 106         checkCounts(expectedPerRegionTypeInfo, actualHumongous, "Humongous");
 107         checkCounts(expectedPerRegionTypeInfo, actualFree, "Free");
 108         checkCounts(expectedPerRegionTypeInfo, actualOther, "Old");
 109     }
 110 
 111     public static void expectRSetSummaries(String result, int expectedCumulative, int expectedPeriodic) throws Exception {
 112         int actualTotal = result.split("Concurrent refinement threads times").length - 1;
 113         int actualCumulative = result.split("Cumulative RS summary").length - 1;
 114 
 115         if (expectedCumulative != actualCumulative) {
 116             throw new Exception("Incorrect amount of RSet summaries at the end. Expected " + expectedCumulative + ", got " + actualCumulative);
 117         }
 118 
 119         if (expectedPeriodic != (actualTotal - actualCumulative)) {
 120             throw new Exception("Incorrect amount of per-period RSet summaries at the end. Expected " + expectedPeriodic + ", got " + (actualTotal - actualCumulative));
 121         }
 122     }
 123 }