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