1 /*
   2 * Copyright (c) 2013, 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  * @test TestObjectTenuringFlags
  26  * @key gc
  27  * @bug 6521376
  28  * @summary Tests argument processing for NeverTenure, AlwaysTenure,
  29  * and MaxTenuringThreshold
  30  * @library /testlibrary
  31  * @build TestObjectTenuringFlags FlagsValue
  32  * @run main/othervm TestObjectTenuringFlags
  33  * @author tao.mao@oracle.com
  34  */
  35 
  36 import com.oracle.java.testlibrary.*;
  37 
  38 import java.util.*;
  39 
  40 public class TestObjectTenuringFlags {
  41   public static void main(String args[]) throws Exception {    
  42     // default case
  43     runTenuringFlagsConsistencyTest(
  44         new String[]{},
  45         false /* shouldFail */,
  46         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 7, 15));
  47 
  48     // valid cases
  49     runTenuringFlagsConsistencyTest(
  50         new String[]{"-XX:+NeverTenure"},
  51         false /* shouldFail */,
  52         new ExpectedTenuringFlags(false /* alwaysTenure */, true /* neverTenure */, 7, 16));
  53 
  54     runTenuringFlagsConsistencyTest(
  55         new String[]{"-XX:+AlwaysTenure"},
  56         false /* shouldFail */,
  57         new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0));
  58 
  59     runTenuringFlagsConsistencyTest(
  60         new String[]{"-XX:MaxTenuringThreshold=0"},
  61         false /* shouldFail */,
  62         new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0));
  63 
  64     runTenuringFlagsConsistencyTest(
  65         new String[]{"-XX:MaxTenuringThreshold=5"},
  66         false /* shouldFail */,
  67         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 5, 5));
  68 
  69     runTenuringFlagsConsistencyTest(
  70         new String[]{"-XX:MaxTenuringThreshold=10"},
  71         false /* shouldFail */,
  72         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 7, 10));
  73 
  74     runTenuringFlagsConsistencyTest(
  75         new String[]{"-XX:MaxTenuringThreshold=15"},
  76         false /* shouldFail */,
  77         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 7, 15));
  78 
  79     runTenuringFlagsConsistencyTest(
  80         new String[]{"-XX:MaxTenuringThreshold=16"},
  81         false /* shouldFail */,
  82         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 7, 16));
  83 
  84     runTenuringFlagsConsistencyTest(
  85         new String[]{"-XX:InitialTenuringThreshold=0"},
  86         false /* shouldFail */,
  87         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 0, 15));
  88 
  89     runTenuringFlagsConsistencyTest(
  90         new String[]{"-XX:InitialTenuringThreshold=5"},
  91         false /* shouldFail */,
  92         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 5, 15));
  93 
  94     runTenuringFlagsConsistencyTest(
  95         new String[]{"-XX:InitialTenuringThreshold=10"},
  96         false /* shouldFail */,
  97         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 10, 15));
  98 
  99     runTenuringFlagsConsistencyTest(
 100         new String[]{"-XX:InitialTenuringThreshold=15"},
 101         false /* shouldFail */,
 102         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 15, 15));
 103 
 104     // "Last option wins" cases
 105     runTenuringFlagsConsistencyTest(
 106         new String[]{"-XX:+AlwaysTenure", "-XX:+NeverTenure"},
 107         false /* shouldFail */,
 108         new ExpectedTenuringFlags(false /* alwaysTenure */, true /* neverTenure */, 7, 16));
 109 
 110     runTenuringFlagsConsistencyTest(
 111         new String[]{"-XX:+NeverTenure", "-XX:+AlwaysTenure"},
 112         false /* shouldFail */,
 113         new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0));
 114 
 115     runTenuringFlagsConsistencyTest(
 116         new String[]{"-XX:MaxTenuringThreshold=16", "-XX:+AlwaysTenure"},
 117         false /* shouldFail */,
 118         new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0));
 119 
 120     runTenuringFlagsConsistencyTest(
 121         new String[]{"-XX:+AlwaysTenure", "-XX:MaxTenuringThreshold=16"},
 122         false /* shouldFail */,
 123         new ExpectedTenuringFlags(false /* alwaysTenure */, false /* neverTenure */, 7, 16));
 124 
 125     runTenuringFlagsConsistencyTest(
 126         new String[]{"-XX:MaxTenuringThreshold=0", "-XX:+NeverTenure"},
 127         false /* shouldFail */,
 128         new ExpectedTenuringFlags(false /* alwaysTenure */, true /* neverTenure */, 7, 16));
 129 
 130     runTenuringFlagsConsistencyTest(
 131         new String[]{"-XX:+NeverTenure", "-XX:MaxTenuringThreshold=0"},
 132         false /* shouldFail */,
 133         new ExpectedTenuringFlags(true /* alwaysTenure */, false /* neverTenure */, 0, 0));
 134 
 135     // Illegal cases
 136     runTenuringFlagsConsistencyTest(
 137         new String[]{"-XX:MaxTenuringThreshold=17"},
 138         true /* shouldFail */,
 139         new ExpectedTenuringFlags());
 140 
 141     runTenuringFlagsConsistencyTest(
 142         new String[]{"-XX:InitialTenuringThreshold=16"},
 143         true /* shouldFail */,
 144         new ExpectedTenuringFlags());
 145 
 146     runTenuringFlagsConsistencyTest(
 147         new String[]{"-XX:InitialTenuringThreshold=17"},
 148         true /* shouldFail */,
 149         new ExpectedTenuringFlags());
 150   }
 151 
 152   private static void runTenuringFlagsConsistencyTest(String[] tenuringFlags,
 153           boolean shouldFail,
 154           ExpectedTenuringFlags expectedFlags) throws Exception {
 155     List<String> vmOpts = new ArrayList<>();
 156     if (tenuringFlags.length > 0) {
 157       Collections.addAll(vmOpts, tenuringFlags);
 158     }
 159     Collections.addAll(vmOpts, "-XX:+PrintFlagsFinal", "-version");
 160 
 161     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(vmOpts.toArray(new String[vmOpts.size()]));
 162     OutputAnalyzer output = new OutputAnalyzer(pb.start());
 163 
 164     if (shouldFail) {
 165       output.shouldHaveExitValue(1);
 166     } else {
 167       output.shouldHaveExitValue(0);
 168       String stdout = output.getStdout();
 169       checkTenuringFlagsConsistency(stdout, expectedFlags);
 170     }
 171   }
 172 
 173   private static void checkTenuringFlagsConsistency(String output, ExpectedTenuringFlags expectedFlags) {
 174     if (expectedFlags.alwaysTenure != FlagsValue.getFlagBoolValue("AlwaysTenure", output)) {
 175       throw new RuntimeException(
 176             "Actual flag AlwaysTenure " + FlagsValue.getFlagBoolValue("AlwaysTenure", output) +
 177             " is not equal to expected flag AlwaysTenure " + expectedFlags.alwaysTenure);
 178     }
 179 
 180     if (expectedFlags.neverTenure != FlagsValue.getFlagBoolValue("NeverTenure", output)) {
 181       throw new RuntimeException(
 182             "Actual flag NeverTenure " + FlagsValue.getFlagBoolValue("NeverTenure", output) +
 183             " is not equal to expected flag NeverTenure " + expectedFlags.neverTenure);
 184     }
 185 
 186     if (expectedFlags.initialTenuringThreshold != FlagsValue.getFlagLongValue("InitialTenuringThreshold", output)) {
 187       throw new RuntimeException(
 188             "Actual flag InitialTenuringThreshold " + FlagsValue.getFlagLongValue("InitialTenuringThreshold", output) +
 189             " is not equal to expected flag InitialTenuringThreshold " + expectedFlags.initialTenuringThreshold);
 190     }
 191 
 192     if (expectedFlags.maxTenuringThreshold != FlagsValue.getFlagLongValue("MaxTenuringThreshold", output)) {
 193       throw new RuntimeException(
 194             "Actual flag MaxTenuringThreshold " + FlagsValue.getFlagLongValue("MaxTenuringThreshold", output) +
 195             " is not equal to expected flag MaxTenuringThreshold " + expectedFlags.maxTenuringThreshold);
 196     }
 197   }
 198 }
 199 
 200 class ExpectedTenuringFlags {
 201     public boolean alwaysTenure;
 202     public boolean neverTenure;
 203     public long initialTenuringThreshold;
 204     public long maxTenuringThreshold;
 205 
 206     public ExpectedTenuringFlags(boolean alwaysTenure,
 207             boolean neverTenure,
 208             long initialTenuringThreshold,
 209             long maxTenuringThreshold) {
 210       this.alwaysTenure = alwaysTenure;
 211       this.neverTenure = neverTenure;
 212       this.initialTenuringThreshold = initialTenuringThreshold;
 213       this.maxTenuringThreshold = maxTenuringThreshold;
 214     }
 215     public ExpectedTenuringFlags() {}
 216 }