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