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