1 /*
   2  * Copyright (c) 2013, 2015, 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 IsMethodCompilableTest
  26  * @bug 8007270 8006683 8007288 8022832
  27  * @summary testing of WB::isMethodCompilable()
  28  * @requires vm.flavor == "server"
  29  * @library /testlibrary /test/lib /
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  *
  33  * @build jdk.test.lib.*
  34  *        sun.hotspot.WhiteBox
  35  * @build compiler.whitebox.IsMethodCompilableTest
  36  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  37  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  38  *                                jdk.test.lib.Platform
  39  * @run main/othervm/timeout=2400 -XX:-TieredCompilation -Xmixed
  40  *      -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  41  *      -XX:PerMethodRecompilationCutoff=3 -XX:-UseCounterDecay
  42  *      -XX:CompileCommand=compileonly,compiler.whitebox.SimpleTestCaseHelper::*
  43  *      compiler.whitebox.IsMethodCompilableTest
  44  */
  45 
  46 package compiler.whitebox;
  47 
  48 import jdk.test.lib.Platform;
  49 
  50 public class IsMethodCompilableTest extends CompilerWhiteBoxTest {
  51     /**
  52      * Value of {@code -XX:PerMethodRecompilationCutoff}
  53      */
  54     protected static final long PER_METHOD_RECOMPILATION_CUTOFF;
  55 
  56     static {
  57         long tmp = Long.parseLong(
  58                 getVMOption("PerMethodRecompilationCutoff", "400"));
  59         if (tmp == -1) {
  60             PER_METHOD_RECOMPILATION_CUTOFF = -1 /* Inf */;
  61         } else {
  62             PER_METHOD_RECOMPILATION_CUTOFF = (0xFFFFFFFFL & tmp);
  63         }
  64     }
  65 
  66     public static void main(String[] args) throws Exception {
  67         CompilerWhiteBoxTest.main(IsMethodCompilableTest::new, args);
  68     }
  69 
  70     private IsMethodCompilableTest(TestCase testCase) {
  71         super(testCase);
  72         // to prevent inlining of #method
  73         WHITE_BOX.testSetDontInlineMethod(method, true);
  74     }
  75 
  76     /**
  77      * Tests {@code WB::isMethodCompilable()} by recompilation of tested method
  78      * 'PerMethodRecompilationCutoff' times and checks compilation status. Also
  79      * checks that WB::clearMethodState() clears no-compilable flags. Only
  80      * applicable to c2 compiled methods.
  81      *
  82      * @throws Exception if one of the checks fails.
  83      */
  84     @Override
  85     protected void test() throws Exception {
  86 
  87         // Only c2 compilations can be disabled through PerMethodRecompilationCutoff
  88         if (!Platform.isServer()) {
  89             throw new Error("TESTBUG: Not server VM");
  90         }
  91 
  92         if (skipXcompOSR()) {
  93             return;
  94         }
  95         if (!isCompilable(COMP_LEVEL_FULL_OPTIMIZATION)) {
  96             throw new RuntimeException(method + " must be compilable");
  97         }
  98         System.out.println("PerMethodRecompilationCutoff = "
  99                 + PER_METHOD_RECOMPILATION_CUTOFF);
 100         if (PER_METHOD_RECOMPILATION_CUTOFF == -1) {
 101             System.err.println(
 102                     "Warning: test is not applicable if PerMethodRecompilationCutoff == Inf");
 103             return;
 104         }
 105 
 106         // deoptimize 'PerMethodRecompilationCutoff' times
 107         for (long attempts = 0, successes = 0;
 108                (successes < PER_METHOD_RECOMPILATION_CUTOFF)  &&
 109                (attempts < PER_METHOD_RECOMPILATION_CUTOFF*2) &&
 110                isCompilable(COMP_LEVEL_FULL_OPTIMIZATION); attempts++) {
 111             if (compileAndDeoptimize() == COMP_LEVEL_FULL_OPTIMIZATION) {
 112                 successes++;
 113             }
 114         }
 115 
 116         if (!testCase.isOsr() && !isCompilable(COMP_LEVEL_FULL_OPTIMIZATION)) {
 117             // in osr test case count of deopt maybe more than iterations
 118             throw new RuntimeException(method + " is not compilable after "
 119                     + PER_METHOD_RECOMPILATION_CUTOFF + " iterations");
 120         }
 121 
 122         // Now compile once more
 123         compileAndDeoptimize();
 124 
 125         if (isCompilable(COMP_LEVEL_FULL_OPTIMIZATION)) {
 126             throw new RuntimeException(method + " is still compilable after "
 127                     + PER_METHOD_RECOMPILATION_CUTOFF + " iterations");
 128         }
 129         checkNotCompiled();
 130         compile();
 131         waitBackgroundCompilation();
 132         checkNotCompiled(COMP_LEVEL_FULL_OPTIMIZATION);
 133 
 134         // WB.clearMethodState() must reset no-compilable flags
 135         WHITE_BOX.clearMethodState(method);
 136         if (!isCompilable(COMP_LEVEL_FULL_OPTIMIZATION)) {
 137             throw new RuntimeException(method
 138                     + " is not compilable after clearMethodState()");
 139         }
 140         compile();
 141         checkCompiled();
 142     }
 143 
 144     private int compileAndDeoptimize() throws Exception {
 145         compile();
 146         waitBackgroundCompilation();
 147         int compLevel = getCompLevel();
 148         deoptimize();
 149         return compLevel;
 150     }
 151 }