1 /* 2 * Copyright (c) 2013, 2015, 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.sun.management.HotSpotDiagnosticMXBean; 29 import com.sun.management.VMOption; 30 31 import jdk.test.lib.*; 32 import java.lang.management.ManagementFactory; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 36 class VerifySummaryOutput { 37 // 4M size, both are directly allocated into the old gen 38 static Object[] largeObject1 = new Object[1024 * 1024]; 39 static Object[] largeObject2 = new Object[1024 * 1024]; 40 41 static int[] temp; 42 43 public static void main(String[] args) { 44 // create some cross-references between these objects 45 for (int i = 0; i < largeObject1.length; i++) { 46 largeObject1[i] = largeObject2; 47 } 48 49 for (int i = 0; i < largeObject2.length; i++) { 50 largeObject2[i] = largeObject1; 51 } 52 53 int numGCs = Integer.parseInt(args[0]); 54 55 if (numGCs > 0) { 56 // try to force a minor collection: the young gen is 4M, the 57 // amount of data allocated below is roughly that (4*1024*1024 + 58 // some header data) 59 for (int i = 0; i < 1024 ; i++) { 60 temp = new int[1024]; 61 } 62 } 63 64 for (int i = 0; i < numGCs - 1; i++) { 65 System.gc(); 66 } 67 } 68 } 69 70 public class TestSummarizeRSetStatsTools { 71 72 // the VM is currently run using G1GC, i.e. trying to test G1 functionality. 73 public static boolean testingG1GC() { 74 HotSpotDiagnosticMXBean diagnostic = 75 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); 76 77 VMOption option = diagnostic.getVMOption("UseG1GC"); 78 if (option.getValue().equals("false")) { 79 System.out.println("Skipping this test. It is only a G1 test."); 80 return false; 81 } 82 return true; 83 } 84 85 public static String runTest(String[] additionalArgs, int numGCs) throws Exception { 86 ArrayList<String> finalargs = new ArrayList<String>(); 87 String[] defaultArgs = new String[] { 88 "-XX:+UseG1GC", 89 "-Xmn4m", 90 "-Xms20m", 91 "-Xmx20m", 92 "-XX:InitiatingHeapOccupancyPercent=100", // we don't want the additional GCs due to initial marking 93 "-XX:+PrintGC", 94 "-XX:+UnlockDiagnosticVMOptions", 95 "-XX:G1HeapRegionSize=1M", 96 }; 97 98 finalargs.addAll(Arrays.asList(defaultArgs)); 99 100 if (additionalArgs != null) { 101 finalargs.addAll(Arrays.asList(additionalArgs)); 102 } 103 104 finalargs.add(VerifySummaryOutput.class.getName()); 105 finalargs.add(String.valueOf(numGCs)); 106 107 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 108 finalargs.toArray(new String[0])); 109 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 110 111 output.shouldHaveExitValue(0); 112 113 String result = output.getStdout(); 114 return result; 115 } 116 117 private static void checkCounts(int expected, int actual, String which) throws Exception { 118 if (expected != actual) { 119 throw new Exception("RSet summaries mention " + which + " regions an incorrect number of times. Expected " + expected + ", got " + actual); 120 } 121 } 122 123 public static void expectPerRegionRSetSummaries(String result, int expectedCumulative, int expectedPeriodic) throws Exception { 124 expectRSetSummaries(result, expectedCumulative, expectedPeriodic); 125 int actualYoung = result.split("Young regions").length - 1; 126 int actualHumonguous = result.split("Humonguous regions").length - 1; 127 int actualFree = result.split("Free regions").length - 1; 128 int actualOther = result.split("Old regions").length - 1; 129 130 // the strings we check for above are printed four times per summary 131 int expectedPerRegionTypeInfo = (expectedCumulative + expectedPeriodic) * 4; 132 133 checkCounts(expectedPerRegionTypeInfo, actualYoung, "Young"); 134 checkCounts(expectedPerRegionTypeInfo, actualHumonguous, "Humonguous"); 135 checkCounts(expectedPerRegionTypeInfo, actualFree, "Free"); 136 checkCounts(expectedPerRegionTypeInfo, actualOther, "Old"); 137 } 138 139 public static void expectRSetSummaries(String result, int expectedCumulative, int expectedPeriodic) throws Exception { 140 int actualTotal = result.split("concurrent refinement").length - 1; 141 int actualCumulative = result.split("Cumulative RS summary").length - 1; 142 143 if (expectedCumulative != actualCumulative) { 144 throw new Exception("Incorrect amount of RSet summaries at the end. Expected " + expectedCumulative + ", got " + actualCumulative); 145 } 146 147 if (expectedPeriodic != (actualTotal - actualCumulative)) { 148 throw new Exception("Incorrect amount of per-period RSet summaries at the end. Expected " + expectedPeriodic + ", got " + (actualTotal - actualCumulative)); 149 } 150 } 151 }