1 /*
   2  * Copyright (c) 2015, 2016, 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 import jdk.test.lib.*;
  25 
  26 /*
  27  * @test CheckCheckCICompilerCount
  28  * @bug 8130858
  29  * @bug 8132525
  30  * @summary Check that correct range of values for CICompilerCount are allowed depending on whether tiered is enabled or not
  31  * @library /testlibrary
  32  * @modules java.base/sun.misc
  33  *          java.management
  34  * @run main CheckCICompilerCount
  35  */
  36 
  37 public class CheckCICompilerCount {
  38     private static final String[][] NON_TIERED_ARGUMENTS = {
  39         {
  40             "-server",
  41             "-XX:-TieredCompilation",
  42             "-XX:+PrintFlagsFinal",
  43             "-XX:CICompilerCount=0",
  44             "-version"
  45         },
  46         {
  47             "-server",
  48             "-XX:-TieredCompilation",
  49             "-XX:+PrintFlagsFinal",
  50             "-XX:CICompilerCount=1",
  51             "-version"
  52         },
  53         {
  54             "-client",
  55             "-XX:-TieredCompilation",
  56             "-XX:+PrintFlagsFinal",
  57             "-XX:CICompilerCount=0",
  58             "-version"
  59         },
  60         {
  61             "-client",
  62             "-XX:-TieredCompilation",
  63             "-XX:+PrintFlagsFinal",
  64             "-XX:CICompilerCount=1",
  65             "-version"
  66         }
  67     };
  68 
  69     private static final String[][] NON_TIERED_EXPECTED_OUTPUTS = {
  70         {
  71             "CICompilerCount (0) must be at least 1",
  72             "Improperly specified VM option 'CICompilerCount=0'"
  73         },
  74         {
  75             "intx CICompilerCount                          := 1                                   {product}"
  76         },
  77         {
  78             "CICompilerCount (0) must be at least 1",
  79             "Improperly specified VM option 'CICompilerCount=0'"
  80         },
  81         {
  82             "intx CICompilerCount                          := 1                                   {product}"
  83         }
  84     };
  85 
  86     private static final int[] NON_TIERED_EXIT = {
  87         1,
  88         0,
  89         1,
  90         0
  91     };
  92 
  93     private static final String[][] TIERED_ARGUMENTS = {
  94         {
  95             "-server",
  96             "-XX:+TieredCompilation",
  97             "-XX:+PrintFlagsFinal",
  98             "-XX:CICompilerCount=1",
  99             "-version"
 100         },
 101         {
 102             "-server",
 103             "-XX:+TieredCompilation",
 104             "-XX:+PrintFlagsFinal",
 105             "-XX:CICompilerCount=2",
 106             "-version"
 107         },
 108         {
 109             "-client",
 110             "-XX:+TieredCompilation",
 111             "-XX:+PrintFlagsFinal",
 112             "-XX:CICompilerCount=1",
 113             "-version"
 114         },
 115         {
 116             "-client",
 117             "-XX:+TieredCompilation",
 118             "-XX:+PrintFlagsFinal",
 119             "-XX:CICompilerCount=2",
 120             "-version"
 121         }
 122     };
 123 
 124     private static final String[][] TIERED_EXPECTED_OUTPUTS = {
 125         {
 126             "CICompilerCount (1) must be at least 2",
 127             "Improperly specified VM option 'CICompilerCount=1'"
 128         },
 129         {
 130             "intx CICompilerCount                          := 2                                   {product}"
 131         },
 132         {
 133             "CICompilerCount (1) must be at least 2",
 134             "Improperly specified VM option 'CICompilerCount=1'"
 135         },
 136         {
 137             "intx CICompilerCount                          := 2                                   {product}"
 138         }
 139     };
 140 
 141     private static final int[] TIERED_EXIT = {
 142         1,
 143         0,
 144         1,
 145         0
 146     };
 147 
 148     private static void verifyValidOption(String[] arguments, String[] expected_outputs, int exit, boolean tiered) throws Exception {
 149         ProcessBuilder pb;
 150         OutputAnalyzer out;
 151 
 152         pb = ProcessTools.createJavaProcessBuilder(arguments);
 153         out = new OutputAnalyzer(pb.start());
 154 
 155         try {
 156             out.shouldHaveExitValue(exit);
 157             for (String expected_output : expected_outputs) {
 158                 out.shouldContain(expected_output);
 159             }
 160         } catch (RuntimeException e) {
 161             // Check if tiered compilation is available in this JVM
 162             // Version. Throw exception only if it is available.
 163             if (!(tiered && out.getOutput().contains("-XX:+TieredCompilation not supported in this VM"))) {
 164                 throw new RuntimeException(e);
 165             }
 166         }
 167     }
 168 
 169     public static void main(String[] args) throws Exception {
 170         if (NON_TIERED_ARGUMENTS.length != NON_TIERED_EXPECTED_OUTPUTS.length || NON_TIERED_ARGUMENTS.length != NON_TIERED_EXIT.length) {
 171             throw new RuntimeException("Test is set up incorrectly: length of arguments, expected outputs and exit codes in non-tiered mode of operation do not match.");
 172         }
 173 
 174         if (TIERED_ARGUMENTS.length != TIERED_EXPECTED_OUTPUTS.length || TIERED_ARGUMENTS.length != TIERED_EXIT.length) {
 175             throw new RuntimeException("Test is set up incorrectly: length of arguments, expected outputs and exit codes in tiered mode of operation do not match.");
 176         }
 177 
 178         for (int i = 0; i < NON_TIERED_ARGUMENTS.length; i++) {
 179             verifyValidOption(NON_TIERED_ARGUMENTS[i], NON_TIERED_EXPECTED_OUTPUTS[i], NON_TIERED_EXIT[i], false);
 180         }
 181 
 182         for (int i = 0; i < TIERED_ARGUMENTS.length; i++) {
 183             verifyValidOption(TIERED_ARGUMENTS[i], TIERED_EXPECTED_OUTPUTS[i], TIERED_EXIT[i], true);
 184         }
 185     }
 186 }