1 /*
   2  * Copyright (c) 2014, 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 CorrectnessTest
  26  * @bug 8038418
  27  * @library /testlibrary /testlibrary/whitebox
  28  * @compile execution/TypeConflict.java execution/TypeProfile.java
  29  *          execution/MethodHandleDelegate.java
  30  * @build CorrectnessTest
  31  * @build OffTest
  32  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  33  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  34  * @run main/timeout=1200 OffTest
  35  */
  36 
  37 import com.oracle.java.testlibrary.OutputAnalyzer;
  38 import com.oracle.java.testlibrary.ProcessTools;
  39 import com.oracle.java.testlibrary.Utils;
  40 import java.util.Random;
  41 import scenarios.ProfilingType;
  42 
  43 public class OffTest {
  44     private static final String[] OPTIONS = {
  45             "-Xbootclasspath/a:.",
  46             "-XX:+IgnoreUnrecognizedVMOptions",
  47             "-XX:+UnlockExperimentalVMOptions",
  48             "-XX:+UnlockDiagnosticVMOptions",
  49             "-XX:+WhiteBoxAPI",
  50             "-XX:CompileCommand=exclude,execution/*::methodNotToCompile",
  51             "-XX:CompileCommand=dontinline,scenarios/Scenario::collectReturnType",
  52             "", // -XX:TypeProfileLevel=?
  53             "", // -XX:?UseTypeSpeculation
  54             CorrectnessTest.class.getName(),
  55             "", // ProfilingType.name()
  56     };
  57 
  58     private static final String TYPE_PROFILE_LEVEL = "TypeProfileLevel";
  59     private static final String USE_TYPE_SPECULATION = "UseTypeSpeculation";
  60     private static final int TYPE_PROFILE_LEVEL_LENGTH = 3;
  61     private static final int TYPE_PROFILE_LEVEL_BOUND = 3;
  62     private static final int DEFAULT_COUNT = 10;
  63     private static final int PROFILING_TYPE_INDEX = OPTIONS.length - 1;
  64     private static final int TYPE_PROFILE_INDEX = OPTIONS.length - 4;
  65     private static final int USE_TYPE_SPECULATION_INDEX = OPTIONS.length - 3;
  66     private static final Random RNG = Utils.getRandomInstance();
  67 
  68     public static void main(String[] args) throws Exception {
  69         int count = DEFAULT_COUNT;
  70         if (args.length > 0) {
  71             count = Integer.parseInt(args[0]) ;
  72         }
  73         for (int i = 0; i < count; ++i) {
  74             runTest();
  75         }
  76     }
  77 
  78     private static void runTest() throws Exception {
  79         String useTypeSpeculation = "-XX:" + (RNG.nextBoolean() ? "+" : "-") +  USE_TYPE_SPECULATION;
  80         String typeProfileLevel = "-XX:" + TYPE_PROFILE_LEVEL + "=" + randomTypeProfileLevel();
  81         ProfilingType type = randomProfileType();
  82         OPTIONS[TYPE_PROFILE_INDEX] = typeProfileLevel;
  83         OPTIONS[USE_TYPE_SPECULATION_INDEX] = useTypeSpeculation;
  84         OPTIONS[PROFILING_TYPE_INDEX] = type.name();
  85         ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(/* addTestVmOptions= */ true, OPTIONS);
  86         OutputAnalyzer outputAnalyzer = new OutputAnalyzer(processBuilder.start());
  87         outputAnalyzer.shouldHaveExitValue(0);
  88     }
  89 
  90     private static ProfilingType randomProfileType() {
  91         ProfilingType[] value = ProfilingType.values();
  92         return value[RNG.nextInt(value.length)];
  93     }
  94 
  95     private static String randomTypeProfileLevel() {
  96         StringBuilder stringBuilder = new StringBuilder();
  97         for (int i = 0; i < TYPE_PROFILE_LEVEL_LENGTH; ++i) {
  98             stringBuilder.append(RNG.nextInt(TYPE_PROFILE_LEVEL_BOUND));
  99         }
 100         return stringBuilder.toString();
 101     }
 102 }