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