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