< prev index next >

test/compiler/jvmci/compilerToVM/AllocateCompileIdTest.java

Print this page
rev 9252 : 8138810: rework tests for CompilerToVM::allocateCompiledId


  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
  26  * @bug 8136421
  27  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  28  * @library /testlibrary /../../test/lib /
  29  * @compile ../common/CompilerToVMHelper.java
  30  * @build sun.hotspot.WhiteBox
  31  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  32  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  33  *                              jdk.vm.ci.hotspot.CompilerToVMHelper
  34  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  35  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  36  *      -XX:-BackgroundCompilation
  37         -XX:+LogCompilation
  38  *      compiler.jvmci.compilerToVM.AllocateCompileIdTest
  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 import java.util.HashSet;


  52 
  53 import compiler.jvmci.common.testcases.TestCase;
  54 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  55 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  56 import jdk.test.lib.Asserts;
  57 import jdk.test.lib.Pair;
  58 import jdk.test.lib.Utils;
  59 import sun.hotspot.WhiteBox;
  60 import sun.hotspot.code.NMethod;
  61 
  62 public class AllocateCompileIdTest {
  63 

  64     private final HashSet<Integer> ids = new HashSet<>();
  65 
  66     public static void main(String[] args) {
  67         AllocateCompileIdTest test = new AllocateCompileIdTest();
  68         createTestCasesCorrectBci().forEach(test::runSanityCorrectTest);
  69         createTestCasesIncorrectBci().forEach(test::runSanityIncorrectTest);
  70     }
  71 
  72 
  73     private static List<CompileCodeTestCase> createTestCasesCorrectBci() {
  74         List<CompileCodeTestCase> result = new ArrayList<>();
  75         try {
  76             Class<?> aClass = DummyClass.class;
  77             Method method = aClass.getMethod("withLoop");
  78             Object receiver = new DummyClass();
  79             result.add(new CompileCodeTestCase(receiver, method, 17));
  80             result.add(new CompileCodeTestCase(receiver, method, -1));
  81         } catch (NoSuchMethodException e) {
  82             throw new Error("TEST BUG : " + e, e);
  83         }
  84         return result;
  85     }
  86 
  87 
  88     private static List<Pair<CompileCodeTestCase, Class<? extends Throwable>>>
  89             createTestCasesIncorrectBci() {
  90         List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> result
  91                 = new ArrayList<>();
  92 
  93         try {
  94             Class<?> aClass = DummyClass.class;
  95             Object receiver = new DummyClass();
  96             Method method = aClass.getMethod("dummyInstanceFunction");
  97             // greater than bytecode.length
  98             int[] bcis = new int[] {30, 50, 200};
  99             for (int bci : bcis) {
 100                 result.add(new Pair<>(
 101                         new CompileCodeTestCase(receiver, method, bci),
 102                         IllegalArgumentException.class));
 103             }
 104             bcis = new int[] {-4, -50, -200};
 105             for (int bci : bcis) {
 106                 result.add(new Pair<>(


 107                         new CompileCodeTestCase(receiver, method, bci),
 108                         IllegalArgumentException.class));
 109             }
 110         } catch (NoSuchMethodException e) {
 111             throw new Error("TEST BUG : " + e.getMessage(), e);
 112         }
 113         return result;
 114     }
 115 
 116     private void runSanityCorrectTest(CompileCodeTestCase testCase) {
 117         System.out.println(testCase);
 118         Executable aMethod = testCase.executable;
 119         // to generate ciTypeFlow
 120         System.out.println(testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes())));
 121         int bci = testCase.bci;
 122         HotSpotResolvedJavaMethod method = CTVMUtilities
 123                 .getResolvedMethod(aMethod);

 124         int wbCompileID = getWBCompileID(testCase);
 125         int id = CompilerToVMHelper.allocateCompileId(method, bci);
 126         Asserts.assertNE(id, 0, testCase + " : zero compile id");
 127 
 128         if (wbCompileID > 0) {
 129             Asserts.assertGT(id, wbCompileID, testCase
 130                     + " : allocated 'compile id' not  greater than existed");
 131             if (!ids.add(wbCompileID)) {
 132                 throw new AssertionError(String.format(
 133                         "%s : vm compilation allocated existed id -- %d",
 134                         testCase, id));
 135             }
 136         }
 137         if (!ids.add(id)) {
 138             throw new AssertionError(String.format(
 139                     "%s : allocateCompileId returned existed id %d",
 140                     testCase, id));
 141         }
 142     }
 143 
 144     private void runSanityIncorrectTest(
 145             Pair<CompileCodeTestCase, Class<? extends Throwable>> testCase) {
 146         System.out.println(testCase);
 147         Class<? extends Throwable> exception = testCase.second;
 148         Executable aMethod = testCase.first.executable;
 149         int bci = testCase.first.bci;
 150         HotSpotResolvedJavaMethod method = CTVMUtilities
 151                 .getResolvedMethod(aMethod);
 152         Utils.runAndCheckException(
 153                 () -> CompilerToVMHelper.allocateCompileId(method, bci),
 154                 exception);
 155     }
 156 
 157     private int getWBCompileID(CompileCodeTestCase testCase) {
 158         NMethod nm = testCase.deoptimizeAndCompile();
 159         if (nm == null) {
 160             throw new Error("[TEST BUG] cannot compile method " + testCase);
 161         }
 162         return nm.compile_id;
 163     }
 164 }


  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
  26  * @bug 8136421
  27  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  28  * @library /testlibrary /../../test/lib /
  29  * @compile ../common/CompilerToVMHelper.java
  30  * @build sun.hotspot.WhiteBox
  31  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  32  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  33  *                              jdk.vm.ci.hotspot.CompilerToVMHelper
  34  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  35  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  36  *      -XX:-BackgroundCompilation

  37  *      compiler.jvmci.compilerToVM.AllocateCompileIdTest
  38  */
  39 
  40 package compiler.jvmci.compilerToVM;
  41 
  42 import compiler.jvmci.common.CTVMUtilities;
  43 
  44 import java.lang.reflect.Executable;
  45 import java.lang.reflect.Method;
  46 import java.util.ArrayList;

  47 import java.util.List;

  48 import java.util.HashSet;
  49 import java.util.stream.Collectors;
  50 import java.util.stream.Stream;
  51 

  52 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  53 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  54 import jdk.test.lib.Asserts;
  55 import jdk.test.lib.Pair;
  56 import jdk.test.lib.Utils;

  57 import sun.hotspot.code.NMethod;
  58 
  59 public class AllocateCompileIdTest {
  60 
  61     private static final int SOME_REPEAT_VALUE = 5;
  62     private final HashSet<Integer> ids = new HashSet<>();
  63 
  64     public static void main(String[] args) {
  65         AllocateCompileIdTest test = new AllocateCompileIdTest();
  66         createTestCasesCorrectBci().forEach(test::runSanityCorrectTest);
  67         createTestCasesIncorrectBci().forEach(test::runSanityIncorrectTest);
  68     }
  69 

  70     private static List<CompileCodeTestCase> createTestCasesCorrectBci() {
  71         List<CompileCodeTestCase> result = new ArrayList<>();
  72         try {
  73             Class<?> aClass = DummyClass.class;
  74             Method method = aClass.getMethod("withLoop");
  75             Object receiver = new DummyClass();
  76             result.add(new CompileCodeTestCase(receiver, method, 17));
  77             result.add(new CompileCodeTestCase(receiver, method, -1));
  78         } catch (NoSuchMethodException e) {
  79             throw new Error("TEST BUG : " + e, e);
  80         }
  81         return result;
  82     }
  83 

  84     private static List<Pair<CompileCodeTestCase, Class<? extends Throwable>>>
  85             createTestCasesIncorrectBci() {
  86         List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> result
  87                 = new ArrayList<>();

  88         try {
  89             Class<?> aClass = DummyClass.class;
  90             Object receiver = new DummyClass();
  91             Method method = aClass.getMethod("dummyInstanceFunction");
  92             // greater than bytecode.length
  93             byte[] bytecode = CompilerToVMHelper.getBytecode(CTVMUtilities
  94                     .getResolvedMethod(method));
  95             Stream.of(
  96                     // greater than bytecode.length
  97                     bytecode.length + 4,
  98                     bytecode.length + 50,
  99                     bytecode.length + 200,
 100                     // negative cases
 101                     -4, -50, -200)
 102                     .map(bci -> new Pair<CompileCodeTestCase,
 103                             Class<? extends Throwable>>(
 104                             new CompileCodeTestCase(receiver, method, bci),
 105                             IllegalArgumentException.class))
 106                     .collect(Collectors.toList());
 107         } catch (NoSuchMethodException e) {
 108             throw new Error("TEST BUG : " + e.getMessage(), e);
 109         }
 110         return result;
 111     }
 112 
 113     private void runSanityCorrectTest(CompileCodeTestCase testCase) {
 114         System.out.println(testCase);
 115         Executable aMethod = testCase.executable;
 116         // to generate ciTypeFlow
 117         testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes()));
 118         int bci = testCase.bci;
 119         HotSpotResolvedJavaMethod method = CTVMUtilities
 120                 .getResolvedMethod(aMethod);
 121         for (int i = 0; i < SOME_REPEAT_VALUE; ++i) {
 122             int wbCompileID = getWBCompileID(testCase);
 123             int id = CompilerToVMHelper.allocateCompileId(method, bci);
 124             Asserts.assertNE(id, 0, testCase + " : zero compile id");


 125             Asserts.assertGT(id, wbCompileID, testCase
 126                     + " : allocated 'compile id' not  greater than existed");
 127             Asserts.assertTrue(ids.add(wbCompileID), testCase
 128                     + " : vm compilation allocated existed id " + id);
 129             Asserts.assertTrue(ids.add(id), testCase
 130                     + " : allocateCompileId returned existed id " + id);






 131         }
 132     }
 133 
 134     private void runSanityIncorrectTest(
 135             Pair<CompileCodeTestCase, Class<? extends Throwable>> testCase) {
 136         System.out.println(testCase);
 137         Class<? extends Throwable> exception = testCase.second;
 138         Executable aMethod = testCase.first.executable;
 139         int bci = testCase.first.bci;
 140         HotSpotResolvedJavaMethod method = CTVMUtilities
 141                 .getResolvedMethod(aMethod);
 142         Utils.runAndCheckException(
 143                 () -> CompilerToVMHelper.allocateCompileId(method, bci),
 144                 exception);
 145     }
 146 
 147     private int getWBCompileID(CompileCodeTestCase testCase) {
 148         NMethod nm = testCase.deoptimizeAndCompile();
 149         if (nm == null || nm.compile_id <= 0) {
 150             throw new Error("TEST BUG : cannot compile method " + testCase);
 151         }
 152         return nm.compile_id;
 153     }
 154 }
< prev index next >