|
Java example source code file (TestMaxHeapSizeTools.java)
The TestMaxHeapSizeTools.java Java example source code/* * Copyright (c) 2013, 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. */ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.ArrayList; import java.util.Arrays; import com.oracle.java.testlibrary.*; import sun.hotspot.WhiteBox; class ErgoArgsPrinter { public static void main(String[] args) throws Exception { WhiteBox wb = WhiteBox.getWhiteBox(); wb.printHeapSizes(); } } final class MinInitialMaxValues { public long minHeapSize; public long initialHeapSize; public long maxHeapSize; public long minAlignment; public long maxAlignment; } class TestMaxHeapSizeTools { public static void checkMinInitialMaxHeapFlags(String gcflag) throws Exception { checkInvalidMinInitialHeapCombinations(gcflag); checkValidMinInitialHeapCombinations(gcflag); checkInvalidInitialMaxHeapCombinations(gcflag); checkValidInitialMaxHeapCombinations(gcflag); } public static void checkMinInitialErgonomics(String gcflag) throws Exception { // heap sizing ergonomics use the value NewSize + OldSize as default values // for ergonomics calculation. Retrieve these values. long[] values = new long[2]; getNewOldSize(gcflag, values); // we check cases with values smaller and larger than this default value. long newPlusOldSize = values[0] + values[1]; long smallValue = newPlusOldSize / 2; long largeValue = newPlusOldSize * 2; long maxHeapSize = largeValue + (2 * 1024 * 1024); // -Xms is not set checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize }, values, -1, -1); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=0" }, values, -1, -1); // -Xms is set to zero checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0" }, values, -1, -1); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=0" }, values, -1, -1); // -Xms is set to small value checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue }, values, -1, -1); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=0" }, values, smallValue, -1); // -Xms is set to large value checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue }, values, largeValue, largeValue); checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue, "-XX:InitialHeapSize=0" }, values, largeValue, -1); } private static long align_up(long value, long alignment) { long alignmentMinusOne = alignment - 1; return (value + alignmentMinusOne) & ~alignmentMinusOne; } private static void getNewOldSize(String gcflag, long[] values) throws Exception { ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag, "-XX:+PrintFlagsFinal", "-version"); OutputAnalyzer output = new OutputAnalyzer(pb.start()); output.shouldHaveExitValue(0); String stdout = output.getStdout(); values[0] = getFlagValue(" NewSize", stdout); values[1] = getFlagValue(" OldSize", stdout); } public static void checkGenMaxHeapErgo(String gcflag) throws Exception { TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 3); TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 4); TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 5); } private static void checkInvalidMinInitialHeapCombinations(String gcflag) throws Exception { expectError(new String[] { gcflag, "-Xms8M", "-XX:InitialHeapSize=4M", "-version" }); } private static void checkValidMinInitialHeapCombinations(String gcflag) throws Exception { expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms4M", "-version" }); expectValid(new String[] { gcflag, "-Xms4M", "-XX:InitialHeapSize=8M", "-version" }); expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms8M", "-version" }); // the following is not an error as -Xms sets both minimal and initial heap size expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-Xms8M", "-version" }); } private static void checkInvalidInitialMaxHeapCombinations(String gcflag) throws Exception { expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=8M", "-version" }); expectError(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" }); } private static void checkValidInitialMaxHeapCombinations(String gcflag) throws Exception { expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" }); expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:InitialHeapSize=4M", "-version" }); expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=4M", "-version" }); // a value of "0" for initial heap size means auto-detect expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=0M", "-version" }); } private static long valueAfter(String source, String match) { int start = source.indexOf(match) + match.length(); String tail = source.substring(start).split(" ")[0]; return Long.parseLong(tail); } /** * Executes a new VM process with the given class and parameters. * @param vmargs Arguments to the VM to run * @param classname Name of the class to run * @param arguments Arguments to the class * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string * @return The OutputAnalyzer with the results for the invocation. */ public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception { ArrayList<String> finalargs = new ArrayList Other Java examples (source code examples)Here is a short list of links related to this Java TestMaxHeapSizeTools.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.