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.HotSpotResolvedJavaMethod;
  54 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  55 import jdk.test.lib.Asserts;
  56 import jdk.test.lib.Utils;
  57 import sun.hotspot.WhiteBox;
  58 import sun.hotspot.code.NMethod;
  59 
  60 public class HasCompiledCodeForOSRTest {
  61     public static void main(String[] args) {
  62         List<CompileCodeTestCase>testCases = createTestCases();
  63         testCases.forEach(HasCompiledCodeForOSRTest::runSanityTest);
  64     }
  65 
  66     public static List<CompileCodeTestCase> createTestCases() {
  67         List<CompileCodeTestCase> testCases = new ArrayList<>();
  68 
  69         try {
  70             Class<?> aClass = DummyClass.class;
  71             Object receiver = new DummyClass();
  72             testCases.add(new CompileCodeTestCase(receiver,
  73                     aClass.getMethod("withLoop"), 17));
  74         } catch (NoSuchMethodException e) {
  75             throw new Error("TEST BUG : " + e.getMessage(), e);
  76         }
  77         return testCases;
  78     }
  79 
  80     private static void runSanityTest(CompileCodeTestCase testCase) {
  81         System.out.println(testCase);
  82         Executable aMethod = testCase.executable;
  83         HotSpotResolvedJavaMethod method = CTVMUtilities
  84                 .getResolvedMethod(aMethod);
  85         testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes()));
  86         testCase.deoptimize();
  87         int[] levels = CompilerUtils.getAvailableCompilationLevels();
  88         // not compiled
  89         for (int level : levels) {
  90             boolean isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(
  91                     method, testCase.bci, level);
  92             Asserts.assertFalse(isCompiled, String.format(
  93                     "%s : unexpected return value for non-compiled method at "
  94                             + "level %d", testCase, level));
  95         }
  96         NMethod nm = testCase.compile();
  97         if (nm == null) {
  98             throw new Error(String.format(
  99                     "TEST BUG : %s : cannot compile method", testCase));
 100         }
 101 
 102         boolean isCompiled;
 103         int level = nm.comp_level;
 104         for (int i : levels) {
 105             isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(
 106                     method, testCase.bci, i);
 107             Asserts.assertEQ(isCompiled, level == i, String.format(
 108                     "%s : unexpected return value for compiled method at "
 109                             + "level %d", testCase, i));
 110         }
 111 
 112         for (int i : new int[] {-1, +1}) {
 113             int bci = testCase.bci + i;
 114             isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(
 115                     method, bci, level);
 116             Asserts.assertFalse(isCompiled, String.format(
 117                     "%s : unexpected return value for compiled method at "
 118                             + "level %d with bci = %d ",
 119                     testCase, level, bci));
 120         }
 121     }
 122 }