--- old/src/share/vm/gc_implementation/parallelScavenge/psScavenge.cpp 2013-11-06 16:25:54.000000000 -0800 +++ new/src/share/vm/gc_implementation/parallelScavenge/psScavenge.cpp 2013-11-06 16:25:54.000000000 -0800 @@ -537,7 +537,7 @@ if (PrintTenuringDistribution) { gclog_or_tty->cr(); - gclog_or_tty->print_cr("Desired survivor size " SIZE_FORMAT " bytes, new threshold %u (max %u)", + gclog_or_tty->print_cr("Desired survivor size " SIZE_FORMAT " bytes, new threshold %u (max threshold %u)", size_policy->calculated_survivor_size_in_bytes(), _tenuring_threshold, MaxTenuringThreshold); } @@ -817,10 +817,8 @@ void PSScavenge::initialize() { // Arguments must have been parsed - if (AlwaysTenure) { - _tenuring_threshold = 0; - } else if (NeverTenure) { - _tenuring_threshold = markOopDesc::max_age + 1; + if (AlwaysTenure || NeverTenure) { + _tenuring_threshold = MaxTenuringThreshold; } else { // We want to smooth out our startup times for the AdaptiveSizePolicy _tenuring_threshold = (UseAdaptiveSizePolicy) ? InitialTenuringThreshold : --- old/src/share/vm/gc_implementation/shared/ageTable.cpp 2013-11-06 16:25:54.000000000 -0800 +++ new/src/share/vm/gc_implementation/shared/ageTable.cpp 2013-11-06 16:25:54.000000000 -0800 @@ -80,28 +80,34 @@ uint ageTable::compute_tenuring_threshold(size_t survivor_capacity) { size_t desired_survivor_size = (size_t)((((double) survivor_capacity)*TargetSurvivorRatio)/100); - size_t total = 0; - uint age = 1; - assert(sizes[0] == 0, "no objects with age zero should be recorded"); - while (age < table_size) { - total += sizes[age]; - // check if including objects of age 'age' made us pass the desired - // size, if so 'age' is the new threshold - if (total > desired_survivor_size) break; - age++; + uint result; + + if (AlwaysTenure || NeverTenure) { + result = MaxTenuringThreshold; + } else { + size_t total = 0; + uint age = 1; + assert(sizes[0] == 0, "no objects with age zero should be recorded"); + while (age < table_size) { + total += sizes[age]; + // check if including objects of age 'age' made us pass the desired + // size, if so 'age' is the new threshold + if (total > desired_survivor_size) break; + age++; + } + result = age < MaxTenuringThreshold ? age : MaxTenuringThreshold; } - uint result = age < MaxTenuringThreshold ? age : MaxTenuringThreshold; if (PrintTenuringDistribution || UsePerfData) { if (PrintTenuringDistribution) { gclog_or_tty->cr(); - gclog_or_tty->print_cr("Desired survivor size " SIZE_FORMAT " bytes, new threshold %u (max %u)", + gclog_or_tty->print_cr("Desired survivor size " SIZE_FORMAT " bytes, new threshold %u (max threshold %u)", desired_survivor_size*oopSize, result, MaxTenuringThreshold); } - total = 0; - age = 1; + size_t total = 0; + uint age = 1; while (age < table_size) { total += sizes[age]; if (sizes[age] > 0) { --- old/src/share/vm/runtime/arguments.cpp 2013-11-06 16:25:55.000000000 -0800 +++ new/src/share/vm/runtime/arguments.cpp 2013-11-06 16:25:55.000000000 -0800 @@ -1176,11 +1176,6 @@ FLAG_SET_DEFAULT(OldPLABSize, (intx)1024); } - // AlwaysTenure flag should make ParNew promote all at first collection. - // See CR 6362902. - if (AlwaysTenure) { - FLAG_SET_CMDLINE(uintx, MaxTenuringThreshold, 0); - } // When using compressed oops, we use local overflow stacks, // rather than using a global overflow list chained through // the klass word of the object's pre-image. @@ -2273,10 +2268,8 @@ status = status && verify_percentage(YoungGenerationSizeSupplement, "YoungGenerationSizeSupplement"); status = status && verify_percentage(TenuredGenerationSizeSupplement, "TenuredGenerationSizeSupplement"); - // the "age" field in the oop header is 4 bits; do not want to pull in markOop.hpp - // just for that, so hardcode here. - status = status && verify_interval(MaxTenuringThreshold, 0, 15, "MaxTenuringThreshold"); - status = status && verify_interval(InitialTenuringThreshold, 0, MaxTenuringThreshold, "MaxTenuringThreshold"); + status = status && verify_interval(MaxTenuringThreshold, 0, markOopDesc::max_age + 1, "MaxTenuringThreshold"); + status = status && verify_interval(InitialTenuringThreshold, 0, MaxTenuringThreshold, "InitialTenuringThreshold"); status = status && verify_percentage(TargetSurvivorRatio, "TargetSurvivorRatio"); status = status && verify_percentage(MarkSweepDeadRatio, "MarkSweepDeadRatio"); @@ -3008,14 +3001,31 @@ // but disallow DR and offlining (5008695). FLAG_SET_CMDLINE(bool, BindGCTaskThreadsToCPUs, true); + // Need to keep consistency of MaxTenuringThreshold and AlwaysTenure/NeverTenure; + // and the last option wins. } else if (match_option(option, "-XX:+NeverTenure", &tail)) { - // The last option must always win. - FLAG_SET_CMDLINE(bool, AlwaysTenure, false); FLAG_SET_CMDLINE(bool, NeverTenure, true); + FLAG_SET_CMDLINE(bool, AlwaysTenure, false); + FLAG_SET_CMDLINE(uintx, MaxTenuringThreshold, markOopDesc::max_age + 1); } else if (match_option(option, "-XX:+AlwaysTenure", &tail)) { - // The last option must always win. FLAG_SET_CMDLINE(bool, NeverTenure, false); FLAG_SET_CMDLINE(bool, AlwaysTenure, true); + FLAG_SET_CMDLINE(uintx, MaxTenuringThreshold, 0); + } else if (match_option(option, "-XX:MaxTenuringThreshold=", &tail)) { + uintx max_tenuring_thresh = 0; + if(!parse_uintx(tail, &max_tenuring_thresh, 0)) { + jio_fprintf(defaultStream::error_stream(), + "Invalid MaxTenuringThreshold: %s\n", option->optionString); + } + FLAG_SET_CMDLINE(uintx, MaxTenuringThreshold, max_tenuring_thresh); + + if (MaxTenuringThreshold == 0) { + FLAG_SET_CMDLINE(bool, NeverTenure, false); + FLAG_SET_CMDLINE(bool, AlwaysTenure, true); + } else { + FLAG_SET_CMDLINE(bool, NeverTenure, false); + FLAG_SET_CMDLINE(bool, AlwaysTenure, false); + } } else if (match_option(option, "-XX:+CMSPermGenSweepingEnabled", &tail) || match_option(option, "-XX:-CMSPermGenSweepingEnabled", &tail)) { jio_fprintf(defaultStream::error_stream(), --- old/test/gc/arguments/TestInitialTenuringThreshold.java 2013-11-06 16:25:55.000000000 -0800 +++ new/test/gc/arguments/TestInitialTenuringThreshold.java 2013-11-06 16:25:55.000000000 -0800 @@ -63,13 +63,14 @@ // successful tests runWithThresholds(0, 10, false); runWithThresholds(5, 5, false); + runWithThresholds(8, 16, false); // failing tests runWithThresholds(10, 0, true); runWithThresholds(9, 8, true); runWithThresholds(-1, 8, true); runWithThresholds(8, -1, true); - runWithThresholds(8, 16, true); runWithThresholds(16, 8, true); + runWithThresholds(8, 17, true); } } --- /dev/null 2013-11-06 16:25:56.000000000 -0800 +++ new/test/gc/arguments/FlagsOutput.java 2013-11-06 16:25:56.000000000 -0800 @@ -0,0 +1,43 @@ +/* +* 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.*; + +public class FlagsOutput { + public static boolean getFlagBoolValue(String flag, String where) { + Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where); + if (!m.find()) { + throw new RuntimeException("Could not find value for flag " + flag + " in output string"); + } + return m.group(1).equals("true"); + } + + public static long getFlagLongValue(String flag, String where) { + Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where); + if (!m.find()) { + throw new RuntimeException("Could not find value for flag " + flag + " in output string"); + } + String match = m.group(); + return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length())); + } +} \ No newline at end of file --- /dev/null 2013-11-06 16:25:56.000000000 -0800 +++ new/test/gc/arguments/TestObjectTenuringFlags.java 2013-11-06 16:25:56.000000000 -0800 @@ -0,0 +1,242 @@ +/* +* 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. +*/ + +/* + * @test TestObjectTenuringFlags + * @key gc + * @bug 6521376 + * @summary Tests argument processing for NeverTenure, AlwaysTenure, + * and MaxTenuringThreshold + * @library /testlibrary + * @build TestObjectTenuringFlags FlagsOutput + * @run main/othervm TestObjectTenuringFlags + * @author tao.mao@oracle.com + */ + +import com.oracle.java.testlibrary.*; + +import java.util.*; + +public class TestObjectTenuringFlags { + static List vmOpts = new ArrayList<>(); + static boolean shouldFail; + static ExpectedTenuringFlags expectedFlags; + + public static void main(String args[]) throws Exception { + // default case + vmOpts.clear(); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, + false /* neverTenure */, + 7 /* initialTenuringThreshold */, + 15 /* maxTenuringThreshold */); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + // valid cases + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:+NeverTenure"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, true /* neverTenure */, 7, 16); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:+AlwaysTenure"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:MaxTenuringThreshold=0"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:MaxTenuringThreshold=5"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 5, 5); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:MaxTenuringThreshold=10"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 7, 10); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:MaxTenuringThreshold=15"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 7, 15); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:MaxTenuringThreshold=16"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 7, 16); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:InitialTenuringThreshold=0"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 0, 15); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:InitialTenuringThreshold=5"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 5, 15); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:InitialTenuringThreshold=10"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 10, 15); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:InitialTenuringThreshold=15"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 15, 15); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + // "Last option wins" cases + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:+AlwaysTenure", "-XX:+NeverTenure"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, true /* neverTenure */, 7, 16); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:+NeverTenure", "-XX:+AlwaysTenure"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:MaxTenuringThreshold=16", "-XX:+AlwaysTenure"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:+AlwaysTenure", "-XX:MaxTenuringThreshold=16"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 7, 16); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:MaxTenuringThreshold=0", "-XX:+NeverTenure"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(false /* alwaysTenure */, true /* neverTenure */, 7, 16); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:+NeverTenure", "-XX:MaxTenuringThreshold=0"); + shouldFail = false; + expectedFlags = new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + // Illegal cases + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:MaxTenuringThreshold=17"); + shouldFail = true; + expectedFlags = new ExpectedTenuringFlags(); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:InitialTenuringThreshold=16"); + shouldFail = true; + expectedFlags = new ExpectedTenuringFlags(); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + + vmOpts.clear(); + Collections.addAll(vmOpts, "-XX:InitialTenuringThreshold=17"); + shouldFail = true; + expectedFlags = new ExpectedTenuringFlags(); + runTenurinigFlagsConsistencyTest(vmOpts, shouldFail, expectedFlags); + } + + private static void runTenurinigFlagsConsistencyTest(List vmOpts, + boolean shouldFail, + ExpectedTenuringFlags expectedFlags) throws Exception { + Collections.addAll(vmOpts, "-XX:+PrintFlagsFinal", "-version"); + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(vmOpts.toArray(new String[vmOpts.size()])); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + + if (shouldFail) { + output.shouldHaveExitValue(1); + } else { + String stdout = output.getStdout(); + output.shouldHaveExitValue(0); + try { + checkTenurinigFlagsConsistency(stdout, + expectedFlags.alwaysTenure, + expectedFlags.neverTenure, + expectedFlags.initialTenuringThreshold, + expectedFlags.maxTenuringThreshold); + } catch (Exception e) { + throw new RuntimeException("Object tenuring flags are not consistent\n" + + "Failed JVM options: " + ((ArrayList) vmOpts).toString()); + } + } + } + + private static void checkTenurinigFlagsConsistency(String output, + boolean alwaysTenure, + boolean neverTenure, + long initialTenuringThreshold, + long maxTenuringThreshold) throws Exception { + boolean consistencyCheck = true; + consistencyCheck = consistencyCheck && + (alwaysTenure == FlagsOutput.getFlagBoolValue("AlwaysTenure", output)); + consistencyCheck = consistencyCheck && + (neverTenure == FlagsOutput.getFlagBoolValue("NeverTenure", output)); + consistencyCheck = consistencyCheck && + (initialTenuringThreshold == FlagsOutput.getFlagLongValue("InitialTenuringThreshold", output)); + consistencyCheck = consistencyCheck && + (maxTenuringThreshold == FlagsOutput.getFlagLongValue("MaxTenuringThreshold", output)); + + if (!consistencyCheck) { + throw new RuntimeException(); + } + } +} + +class ExpectedTenuringFlags { + public boolean alwaysTenure; + public boolean neverTenure; + public long initialTenuringThreshold; + public long maxTenuringThreshold; + + public ExpectedTenuringFlags(boolean alwaysTenure, + boolean neverTenure, + long initialTenuringThreshold, + long maxTenuringThreshold) { + this.alwaysTenure = alwaysTenure; + this.neverTenure = neverTenure; + this.initialTenuringThreshold = initialTenuringThreshold; + this.maxTenuringThreshold = maxTenuringThreshold; + } + + public ExpectedTenuringFlags() {} +} \ No newline at end of file