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  * Common helpers for TestSummarizeRSetStats* tests
  26  */
  27 
  28 import com.oracle.java.testlibrary.*;
  29 import java.lang.Thread;
  30 import java.util.ArrayList;
  31 import java.util.Arrays;
  32 
  33 class RunSystemGCs {
  34     // 4M size, both are directly allocated into the old gen
  35     static Object[] largeObject1 = new Object[1024 * 1024];
  36     static Object[] largeObject2 = new Object[1024 * 1024];
  37 
  38     static int[] temp;
  39 
  40     public static void main(String[] args) {
  41         // create some cross-references between these objects
  42         for (int i = 0; i < largeObject1.length; i++) {
  43             largeObject1[i] = largeObject2;
  44         }
  45 
  46         for (int i = 0; i < largeObject2.length; i++) {
  47             largeObject2[i] = largeObject1;
  48         }
  49 
  50         int numGCs = Integer.parseInt(args[0]);
  51 
  52         if (numGCs > 0) {
  53             // try to force a minor collection: the young gen is 4M, the
  54             // amount of data allocated below is roughly that (4*1024*1024 +
  55             // some header data)
  56             for (int i = 0; i < 1024 ; i++) {
  57                 temp = new int[1024];
  58             }
  59         }
  60 
  61         for (int i = 0; i < numGCs - 1; i++) {
  62             System.gc();
  63         }
  64     }
  65 }
  66 
  67 public class TestSummarizeRSetStatsTools {
  68 
  69     public static String runTest(String[] additionalArgs, int numGCs) throws Exception {
  70         ArrayList<String> finalargs = new ArrayList<String>();
  71         String[] defaultArgs = new String[] {
  72             "-XX:+UseG1GC",
  73             "-Xmn4m",
  74             "-Xmx20m",
  75             "-XX:InitiatingHeapOccupancyPercent=100", // we don't want the additional GCs due to initial marking
  76             "-XX:+PrintGC",
  77             "-XX:+UnlockDiagnosticVMOptions",
  78             "-XX:G1HeapRegionSize=1M",
  79         };
  80 
  81         finalargs.addAll(Arrays.asList(defaultArgs));
  82 
  83         if (additionalArgs != null) {
  84             finalargs.addAll(Arrays.asList(additionalArgs));
  85         }
  86 
  87         finalargs.add(RunSystemGCs.class.getName());
  88         finalargs.add(String.valueOf(numGCs));
  89 
  90         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  91             finalargs.toArray(new String[0]));
  92         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  93 
  94         output.shouldHaveExitValue(0);
  95 
  96         String result = output.getStdout();
  97         return result;
  98     }
  99 
 100     private static void checkCounts(int expected, int actual, String which) throws Exception {
 101         if (expected != actual) {
 102             throw new Exception("RSet summaries mention " + which + " regions an incorrect number of times. Expected " + expected + ", got " + actual);
 103         }
 104     }
 105 
 106     public static void expectPerRegionRSetSummaries(String result, int expectedCumulative, int expectedPeriodic) throws Exception {
 107         expectRSetSummaries(result, expectedCumulative, expectedPeriodic);
 108         int actualYoung = result.split("Young regions").length - 1;
 109         int actualHumonguous = result.split("Humonguous regions").length - 1;
 110         int actualFree = result.split("Free regions").length - 1;
 111         int actualOther = result.split("Old regions").length - 1;
 112 
 113         // the strings we check for above are printed twice per summary
 114         int expectedPerRegionTypeInfo = (expectedCumulative + expectedPeriodic) * 2;
 115 
 116         checkCounts(expectedPerRegionTypeInfo, actualYoung, "Young");
 117         checkCounts(expectedPerRegionTypeInfo, actualHumonguous, "Humonguous");
 118         checkCounts(expectedPerRegionTypeInfo, actualFree, "Free");
 119         checkCounts(expectedPerRegionTypeInfo, actualOther, "Old");
 120     }
 121 
 122     public static void expectRSetSummaries(String result, int expectedCumulative, int expectedPeriodic) throws Exception {
 123         int actualTotal = result.split("Concurrent RS processed").length - 1;
 124         int actualCumulative = result.split("Cumulative RS summary").length - 1;
 125 
 126         if (expectedCumulative != actualCumulative) {
 127             throw new Exception("Incorrect amount of RSet summaries at the end. Expected " + expectedCumulative + ", got " + actualCumulative);
 128         }
 129 
 130         if (expectedPeriodic != (actualTotal - actualCumulative)) {
 131             throw new Exception("Incorrect amount of per-period RSet summaries at the end. Expected " + expectedPeriodic + ", got " + (actualTotal - actualCumulative));
 132         }
 133     }
 134 }
 135