/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test TestDisableExplicitGCFlag * @requires (vm.opt.DisplayVMOutput == null) | (vm.opt.DisplayVMOutput == true) * @summary Verify that DisableExplicitGC flag affects System.gc() behavior. * @library /testlibrary /../../test/lib * @build TestDisableExplicitGCFlag * @run main ClassFileInstaller sun.hotspot.WhiteBox * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestDisableExplicitGCFlag true false 1 1 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestDisableExplicitGCFlag false false 1 1 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestDisableExplicitGCFlag false true 1 0 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestDisableExplicitGCFlag true false 3 2 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestDisableExplicitGCFlag false false 3 2 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestDisableExplicitGCFlag false true 3 1 */ import com.oracle.java.testlibrary.*; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import sun.hotspot.WhiteBox; public class TestDisableExplicitGCFlag { public final static String DIS_EXPLICIT_GC_FLAG = "DisableExplicitGC"; public final static String[] PARALLEL_GC_OPTIONS = {"UseParallelGC", "UseParallelOldGC"}; public final static Pattern SYSTEM_GC_PATTERN = Pattern.compile("System.gc\\(\\)"); // Parallel and ParallelOld GC produces different output - System.gc() message occurred twice per System.gc() call public final static Pattern SYSTEM_GC_PARALLEL_PATTERN = Pattern.compile("Full GC \\(System.gc\\(\\)\\)"); public final static WhiteBox WHITE_BOX = WhiteBox.getWhiteBox(); public static void main(String args[]) throws Exception { boolean checkDefaultValue = Boolean.parseBoolean(args[0]); boolean checkValue = Boolean.parseBoolean(args[1]); int iterationCount = Integer.parseInt(args[2]); int expectedGCCount = Integer.parseInt(args[3]); testDisableExplicitGC(checkDefaultValue, checkValue, iterationCount, expectedGCCount); } /** * @param checkDefaultValue Check default value of DisableExplicitGC VM flag * @param checkValue check VM behavior with DisableExplicitGC=checkValue * @param iterationCount how many times provoke System.gc() with changing DisableExplicitGC flag * @param expectedGCCount how many System.gc() messages should be in VM output * @throws RuntimeException if expected count of System.gc() message does not match with occurred * @throws Exception from OutputAnalyzer or ProcessTools */ public static void testDisableExplicitGC(boolean checkDefaultValue, boolean checkValue, int iterationCount, int expectedGCCount) throws Exception { List vmOpts = new LinkedList( Arrays.asList( new String[]{ "-Xbootclasspath/a:.", "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", "-XX:+PrintGC", "-XX:+PrintGCCause", GCProvoker.class.getName(), Integer.toString(iterationCount) })); // In case if we don't need to check default flag behavior - add VM option if (checkDefaultValue == false) { vmOpts.add(0, "-XX:" + (checkValue ? "+" : "-") + DIS_EXPLICIT_GC_FLAG); } vmOpts.addAll(0, Utils.getVmOptions()); ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(true, vmOpts.toArray(new String[0])); OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start()); analyzer.shouldHaveExitValue(0); String output = analyzer.getOutput(); System.out.println(output); checkResults(output, expectedGCCount); } private static void checkResults(String output, int expectedGCCount) { int multiplier = 1; Pattern pattern = SYSTEM_GC_PATTERN; for (String option : PARALLEL_GC_OPTIONS) { if (WHITE_BOX.getBooleanVMFlag(option)) { pattern = SYSTEM_GC_PARALLEL_PATTERN; } } System.out.println("Expect " + expectedGCCount * multiplier + " System.gc() messages."); Matcher matcher = pattern.matcher(output); int occurrence = 0; while (matcher.find()) { ++occurrence; } if (occurrence != expectedGCCount) { throw new RuntimeException("Occurred " + occurrence + " System.gc() messages."); } } public static class GCProvoker { public static void main(String args[]) { int iterationCount = Integer.parseInt(args[0]); if (iterationCount == 1) { System.gc(); } else { System.out.println("Will perform " + iterationCount + " iterations."); for (int i = 0; i < iterationCount; i++) { System.gc(); // Invert flag for next iteration boolean value = WHITE_BOX.getBooleanVMFlag(DIS_EXPLICIT_GC_FLAG); WHITE_BOX.setBooleanVMFlag(DIS_EXPLICIT_GC_FLAG, !value); } } } } }