1 /*
   2  * Copyright (c) 2013, 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 TestSummarizeRSetStats.java
  26  * @bug 8013895
  27  * @library /testlibrary
  28  * @build TestSummarizeRSetStats
  29  * @summary Verify output of -XX:+G1SummarizeRSetStats
  30  * @run main TestSummarizeRSetStats
  31  *
  32  * Test the output of G1SummarizeRSetStats in conjunction with G1SummarizeRSetStatsPeriod.
  33  */
  34 
  35 import com.oracle.java.testlibrary.*;
  36 import java.lang.Thread;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 
  40 class RunSystemGCs {
  41     // 4M size, both are directly allocated into the old gen
  42     static Object[] largeObject1 = new Object[1024 * 1024];
  43     static Object[] largeObject2 = new Object[1024 * 1024];
  44 
  45     static int[] temp;
  46 
  47     public static void main(String[] args) {
  48         // create some cross-references between these objects
  49         for (int i = 0; i < largeObject1.length; i++) {
  50             largeObject1[i] = largeObject2;
  51         }
  52 
  53         for (int i = 0; i < largeObject2.length; i++) {
  54             largeObject2[i] = largeObject1;
  55         }
  56 
  57         int numGCs = Integer.parseInt(args[0]);
  58 
  59         if (numGCs > 0) {
  60             // try to force a minor collection: the young gen is 4M, the
  61             // amount of data allocated below is roughly that (4*1024*1024 +
  62             // some header data)
  63             for (int i = 0; i < 1024 ; i++) {
  64                 temp = new int[1024];
  65             }
  66         }
  67 
  68         for (int i = 0; i < numGCs - 1; i++) {
  69             System.gc();
  70         }
  71     }
  72 }
  73 
  74 public class TestSummarizeRSetStats {
  75 
  76     public static String runTest(String[] additionalArgs, int numGCs) throws Exception {
  77         ArrayList<String> finalargs = new ArrayList<String>();
  78         String[] defaultArgs = new String[] {
  79             "-XX:+UseG1GC",
  80             "-Xmn4m",
  81             "-Xmx20m",
  82             "-XX:InitiatingHeapOccupancyPercent=100", // we don't want the additional GCs due to initial marking
  83             "-XX:+PrintGC",
  84             "-XX:+UnlockDiagnosticVMOptions",
  85             "-XX:G1HeapRegionSize=1M",
  86         };
  87 
  88         finalargs.addAll(Arrays.asList(defaultArgs));
  89 
  90         if (additionalArgs != null) {
  91             finalargs.addAll(Arrays.asList(additionalArgs));
  92         }
  93 
  94         finalargs.add(RunSystemGCs.class.getName());
  95         finalargs.add(String.valueOf(numGCs));
  96 
  97         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  98             finalargs.toArray(new String[0]));
  99         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 100 
 101         output.shouldHaveExitValue(0);
 102 
 103         String result = output.getStdout();
 104         return result;
 105     }
 106 
 107     private static void expectStatistics(String result, int expectedCumulative, int expectedPeriodic) throws Exception {
 108         int actualTotal = result.split("Concurrent RS processed").length - 1;
 109         int actualCumulative = result.split("Cumulative RS summary").length - 1;
 110 
 111         if (expectedCumulative != actualCumulative) {
 112             throw new Exception("Incorrect amount of RSet summaries at the end. Expected " + expectedCumulative + ", got " + actualCumulative);
 113         }
 114 
 115         if (expectedPeriodic != (actualTotal - actualCumulative)) {
 116             throw new Exception("Incorrect amount of per-period RSet summaries at the end. Expected " + expectedPeriodic + ", got " + (actualTotal - actualCumulative));
 117         }
 118     }
 119 
 120     public static void main(String[] args) throws Exception {
 121         String result;
 122 
 123         // no RSet statistics output
 124         result = runTest(null, 0);
 125         expectStatistics(result, 0, 0);
 126 
 127         // no RSet statistics output
 128         result = runTest(null, 2);
 129         expectStatistics(result, 0, 0);
 130 
 131         // no RSet statistics output
 132         result = runTest(new String[] { "-XX:G1SummarizeRSetStatsPeriod=1" }, 3);
 133         expectStatistics(result, 0, 0);
 134 
 135         // single RSet statistics output at the end
 136         result = runTest(new String[] { "-XX:+G1SummarizeRSetStats" }, 0);
 137         expectStatistics(result, 1, 0);
 138 
 139         // single RSet statistics output at the end
 140         result = runTest(new String[] { "-XX:+G1SummarizeRSetStats" }, 2);
 141         expectStatistics(result, 1, 0);
 142 
 143         // single RSet statistics output
 144         result = runTest(new String[] { "-XX:+G1SummarizeRSetStats", "-XX:G1SummarizeRSetStatsPeriod=1" }, 0);
 145         expectStatistics(result, 1, 0);
 146 
 147         // two times RSet statistics output
 148         result = runTest(new String[] { "-XX:+G1SummarizeRSetStats", "-XX:G1SummarizeRSetStatsPeriod=1" }, 1);
 149         expectStatistics(result, 1, 1);
 150 
 151         // four times RSet statistics output
 152         result = runTest(new String[] { "-XX:+G1SummarizeRSetStats", "-XX:G1SummarizeRSetStatsPeriod=1" }, 3);
 153         expectStatistics(result, 1, 3);
 154 
 155         // three times RSet statistics output
 156         result = runTest(new String[] { "-XX:+G1SummarizeRSetStats", "-XX:G1SummarizeRSetStatsPeriod=2" }, 3);
 157         expectStatistics(result, 1, 2);
 158 
 159         // single RSet statistics output
 160         result = runTest(new String[] { "-XX:+G1SummarizeRSetStats", "-XX:G1SummarizeRSetStatsPeriod=100" }, 3);
 161         expectStatistics(result, 1, 1);
 162     }
 163 }
 164