aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * Common helpers for TestSummarizeRSetStats* tests aoqi@0: */ aoqi@0: aoqi@0: import sun.management.ManagementFactoryHelper; aoqi@0: import com.sun.management.HotSpotDiagnosticMXBean; aoqi@0: import com.sun.management.VMOption; aoqi@0: aoqi@0: import com.oracle.java.testlibrary.*; aoqi@0: import java.util.regex.Matcher; aoqi@0: import java.util.regex.Pattern; aoqi@0: import java.lang.Thread; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.Arrays; aoqi@0: aoqi@0: class VerifySummaryOutput { aoqi@0: // 4M size, both are directly allocated into the old gen aoqi@0: static Object[] largeObject1 = new Object[1024 * 1024]; aoqi@0: static Object[] largeObject2 = new Object[1024 * 1024]; aoqi@0: aoqi@0: static int[] temp; aoqi@0: aoqi@0: public static void main(String[] args) { aoqi@0: // create some cross-references between these objects aoqi@0: for (int i = 0; i < largeObject1.length; i++) { aoqi@0: largeObject1[i] = largeObject2; aoqi@0: } aoqi@0: aoqi@0: for (int i = 0; i < largeObject2.length; i++) { aoqi@0: largeObject2[i] = largeObject1; aoqi@0: } aoqi@0: aoqi@0: int numGCs = Integer.parseInt(args[0]); aoqi@0: aoqi@0: if (numGCs > 0) { aoqi@0: // try to force a minor collection: the young gen is 4M, the aoqi@0: // amount of data allocated below is roughly that (4*1024*1024 + aoqi@0: // some header data) aoqi@0: for (int i = 0; i < 1024 ; i++) { aoqi@0: temp = new int[1024]; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: for (int i = 0; i < numGCs - 1; i++) { aoqi@0: System.gc(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public class TestSummarizeRSetStatsTools { aoqi@0: aoqi@0: // the VM is currently run using G1GC, i.e. trying to test G1 functionality. aoqi@0: public static boolean testingG1GC() { aoqi@0: HotSpotDiagnosticMXBean diagnostic = ManagementFactoryHelper.getDiagnosticMXBean(); aoqi@0: aoqi@0: VMOption option = diagnostic.getVMOption("UseG1GC"); aoqi@0: if (option.getValue().equals("false")) { aoqi@0: System.out.println("Skipping this test. It is only a G1 test."); aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: public static String runTest(String[] additionalArgs, int numGCs) throws Exception { aoqi@0: ArrayList finalargs = new ArrayList(); aoqi@0: String[] defaultArgs = new String[] { aoqi@0: "-XX:+UseG1GC", aoqi@0: "-Xmn4m", aoqi@0: "-Xmx20m", aoqi@0: "-XX:InitiatingHeapOccupancyPercent=100", // we don't want the additional GCs due to initial marking aoqi@0: "-XX:+PrintGC", aoqi@0: "-XX:+UnlockDiagnosticVMOptions", aoqi@0: "-XX:G1HeapRegionSize=1M", aoqi@0: }; aoqi@0: aoqi@0: finalargs.addAll(Arrays.asList(defaultArgs)); aoqi@0: aoqi@0: if (additionalArgs != null) { aoqi@0: finalargs.addAll(Arrays.asList(additionalArgs)); aoqi@0: } aoqi@0: aoqi@0: finalargs.add(VerifySummaryOutput.class.getName()); aoqi@0: finalargs.add(String.valueOf(numGCs)); aoqi@0: aoqi@0: ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( aoqi@0: finalargs.toArray(new String[0])); aoqi@0: OutputAnalyzer output = new OutputAnalyzer(pb.start()); aoqi@0: aoqi@0: output.shouldHaveExitValue(0); aoqi@0: aoqi@0: String result = output.getStdout(); aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: private static void checkCounts(int expected, int actual, String which) throws Exception { aoqi@0: if (expected != actual) { aoqi@0: throw new Exception("RSet summaries mention " + which + " regions an incorrect number of times. Expected " + expected + ", got " + actual); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static void expectPerRegionRSetSummaries(String result, int expectedCumulative, int expectedPeriodic) throws Exception { aoqi@0: expectRSetSummaries(result, expectedCumulative, expectedPeriodic); aoqi@0: int actualYoung = result.split("Young regions").length - 1; aoqi@0: int actualHumonguous = result.split("Humonguous regions").length - 1; aoqi@0: int actualFree = result.split("Free regions").length - 1; aoqi@0: int actualOther = result.split("Old regions").length - 1; aoqi@0: aoqi@0: // the strings we check for above are printed four times per summary aoqi@0: int expectedPerRegionTypeInfo = (expectedCumulative + expectedPeriodic) * 4; aoqi@0: aoqi@0: checkCounts(expectedPerRegionTypeInfo, actualYoung, "Young"); aoqi@0: checkCounts(expectedPerRegionTypeInfo, actualHumonguous, "Humonguous"); aoqi@0: checkCounts(expectedPerRegionTypeInfo, actualFree, "Free"); aoqi@0: checkCounts(expectedPerRegionTypeInfo, actualOther, "Old"); aoqi@0: } aoqi@0: aoqi@0: public static void expectRSetSummaries(String result, int expectedCumulative, int expectedPeriodic) throws Exception { aoqi@0: int actualTotal = result.split("concurrent refinement").length - 1; aoqi@0: int actualCumulative = result.split("Cumulative RS summary").length - 1; aoqi@0: aoqi@0: if (expectedCumulative != actualCumulative) { aoqi@0: throw new Exception("Incorrect amount of RSet summaries at the end. Expected " + expectedCumulative + ", got " + actualCumulative); aoqi@0: } aoqi@0: aoqi@0: if (expectedPeriodic != (actualTotal - actualCumulative)) { aoqi@0: throw new Exception("Incorrect amount of per-period RSet summaries at the end. Expected " + expectedPeriodic + ", got " + (actualTotal - actualCumulative)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: