1 /*
   2  * Copyright (c) 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 /**
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  29  * @library /testlibrary /../../test/lib /
  30  * @compile ../common/CompilerToVMHelper.java
  31  * @build sun.hotspot.WhiteBox
  32  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  33  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  34  *                              jdk.vm.ci.hotspot.CompilerToVMHelper
  35  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  36  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  37  *      -XX:-BackgroundCompilation
  38  *      compiler.jvmci.compilerToVM.HasCompiledCodeForOSRTest
  39  */
  40 
  41 package compiler.jvmci.compilerToVM;
  42 
  43 import compiler.jvmci.common.CTVMUtilities;
  44 
  45 import java.lang.reflect.Executable;
  46 import java.lang.reflect.Method;
  47 import java.util.ArrayList;
  48 import java.util.HashMap;
  49 import java.util.List;
  50 import java.util.Map;
  51 
  52 import compiler.testlibrary.CompilerUtils;
  53 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl;
  54 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  55 import jdk.test.lib.Asserts;
  56 import sun.hotspot.WhiteBox;
  57 import sun.hotspot.code.NMethod;
  58 
  59 public class HasCompiledCodeForOSRTest {
  60     public static void main(String[] args) {
  61         List<CompileCodeTestCase>testCases = createTestCases();
  62         testCases.forEach(HasCompiledCodeForOSRTest::runSanityTest);
  63     }
  64 
  65     public static List<CompileCodeTestCase> createTestCases() {
  66         List<CompileCodeTestCase> testCases = new ArrayList<>();
  67 
  68         try {
  69             Class<?> aClass = DummyClass.class;
  70             testCases.add(new CompileCodeTestCase(
  71                     aClass.getMethod("withLoop"), 17));
  72         } catch (NoSuchMethodException e) {
  73             throw new Error("TEST BUG : " + e.getMessage(), e);
  74         }
  75         return testCases;
  76     }
  77 
  78     private static void runSanityTest(CompileCodeTestCase testCase) {
  79         System.out.println(testCase);
  80         Executable aMethod = testCase.executable;
  81         HotSpotResolvedJavaMethodImpl method = CTVMUtilities
  82                 .getResolvedMethod(aMethod);
  83         testCase.deoptimize();
  84         int[] levels = CompilerUtils.getAvailableCompilationLevels();
  85         // not compiled
  86         for (int level : levels) {
  87             boolean isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(
  88                     method, testCase.bci, level);
  89             Asserts.assertFalse(isCompiled, String.format(
  90                     "%s : unexpected return value for non-compiled method at "
  91                             + "level %d", testCase, level));
  92         }
  93         NMethod nm = testCase.compile();
  94         if (nm == null) {
  95             throw new Error(String.format(
  96                     "TEST BUG : %s : cannot compile method", testCase));
  97         }
  98 
  99         boolean isCompiled;
 100         int level = nm.comp_level;
 101         for (int i : levels) {
 102             isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(
 103                     method, testCase.bci, i);
 104             Asserts.assertEQ(isCompiled, level == i, String.format(
 105                     "%s : unexpected return value for compiled method at "
 106                             + "level %d", testCase, i));
 107         }
 108 
 109         for (int i : new int[] {-1, +1}) {
 110             int bci = testCase.bci + i;
 111             isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(
 112                     method, bci, level);
 113             Asserts.assertFalse(isCompiled, String.format(
 114                     "%s : unexpected return value for compiled method at "
 115                             + "level %d with bci = %d ",
 116                     testCase, level, bci));
 117         }
 118     }
 119 }