hotspot/test/compiler/jvmci/compilerToVM/MaterializeVirtualObjectTest.java
changeset 43961 441de9600d40
parent 43672 b188841d7ac8
child 43972 1ade39b8381b
equal deleted inserted replaced
43960:27d762084344 43961:441de9600d40
    37  *          jdk.vm.ci/jdk.vm.ci.meta
    37  *          jdk.vm.ci/jdk.vm.ci.meta
    38  *
    38  *
    39  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox
    39  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox
    40  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
    40  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
    41  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
    41  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
    42  * @run main/othervm -Xmixed -Xbootclasspath/a:.
    42  * @run main/othervm -Xmixed -Xbatch -Xbootclasspath/a:.
    43  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
    43  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
    44  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
    44  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
    45  *                   -XX:CompileCommand=exclude,*::check
    45  *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
    46  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
    46  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
    47  *                   -XX:CompileCommand=dontinline,compiler/jvmci/compilerToVM/MaterializeVirtualObjectTest,testFrame
    47  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
    48  *                   -XX:CompileCommand=inline,compiler/jvmci/compilerToVM/MaterializeVirtualObjectTest,recurse
    48  *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
    49  *                   -Xbatch
    49  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
       
    50  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=true
    50  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
    51  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
    51  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
    52  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
    52  * @run main/othervm -Xmixed -Xbootclasspath/a:.
    53  * @run main/othervm -Xmixed -Xbatch -Xbootclasspath/a:.
    53  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
    54  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
    54  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
    55  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
    55  *                   -XX:CompileCommand=exclude,*::check
    56  *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
    56  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
    57  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
    57  *                   -Xbatch
    58  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
       
    59  *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
       
    60  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
       
    61  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=false
       
    62  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
       
    63  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
       
    64  * @run main/othervm -Xmixed -Xbatch -Xbootclasspath/a:.
       
    65  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
       
    66  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
       
    67  *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
       
    68  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
       
    69  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
       
    70  *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
       
    71  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
       
    72  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=true
       
    73  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
       
    74  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
       
    75  * @run main/othervm -Xmixed -Xbatch -Xbootclasspath/a:.
       
    76  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
       
    77  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
       
    78  *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
       
    79  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
       
    80  *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
       
    81  *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
       
    82  *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
       
    83  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=false
    58  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
    84  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
    59  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
    85  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
    60  */
    86  */
    61 
    87 
    62 package compiler.jvmci.compilerToVM;
    88 package compiler.jvmci.compilerToVM;
    72 
    98 
    73 import java.lang.reflect.Method;
    99 import java.lang.reflect.Method;
    74 
   100 
    75 public class MaterializeVirtualObjectTest {
   101 public class MaterializeVirtualObjectTest {
    76     private static final WhiteBox WB;
   102     private static final WhiteBox WB;
    77     private static final Method METHOD;
       
    78     private static final ResolvedJavaMethod RESOLVED_METHOD;
       
    79     private static final boolean INVALIDATE;
   103     private static final boolean INVALIDATE;
    80     private static final int COMPILE_THRESHOLD;
   104     private static final int COMPILE_THRESHOLD;
       
   105     private static final Method MATERIALIZED_METHOD;
       
   106     private static final Method NOT_MATERIALIZED_METHOD;
       
   107     private static final ResolvedJavaMethod MATERIALIZED_RESOLVED;
       
   108     private static final ResolvedJavaMethod NOT_MATERIALIZED_RESOLVED;
       
   109     private static final boolean MATERIALIZE_FIRST;
    81 
   110 
    82     static {
   111     static {
       
   112         Method method1;
       
   113         Method method2;
    83         WB = WhiteBox.getWhiteBox();
   114         WB = WhiteBox.getWhiteBox();
    84         try {
   115         try {
    85             METHOD = MaterializeVirtualObjectTest.class.getDeclaredMethod(
   116             method1 = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame",
    86                     "testFrame", String.class, int.class);
   117                     String.class, int.class);
       
   118             method2 = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame2",
       
   119                     String.class, int.class);
    87         } catch (NoSuchMethodException e) {
   120         } catch (NoSuchMethodException e) {
    88             throw new Error("Can't get executable for test method", e);
   121             throw new Error("Can't get executable for test method", e);
    89         }
   122         }
    90         RESOLVED_METHOD = CTVMUtilities.getResolvedMethod(METHOD);
   123         ResolvedJavaMethod resolved1;
       
   124         ResolvedJavaMethod resolved2;
       
   125         resolved1 = CTVMUtilities.getResolvedMethod(method1);
       
   126         resolved2 = CTVMUtilities.getResolvedMethod(method2);
    91         INVALIDATE = Boolean.getBoolean(
   127         INVALIDATE = Boolean.getBoolean(
    92                 "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate");
   128                 "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate");
    93         COMPILE_THRESHOLD = WB.getBooleanVMFlag("TieredCompilation")
   129         COMPILE_THRESHOLD = WB.getBooleanVMFlag("TieredCompilation")
    94                 ? CompilerWhiteBoxTest.THRESHOLD
   130                 ? CompilerWhiteBoxTest.THRESHOLD
    95                 : CompilerWhiteBoxTest.THRESHOLD * 2;
   131                 : CompilerWhiteBoxTest.THRESHOLD * 2;
       
   132         MATERIALIZE_FIRST = Boolean.getBoolean(
       
   133                 "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst");
       
   134         MATERIALIZED_RESOLVED = MATERIALIZE_FIRST ? resolved1 : resolved2;
       
   135         NOT_MATERIALIZED_RESOLVED = MATERIALIZE_FIRST ? resolved2 : resolved1;
       
   136         MATERIALIZED_METHOD = MATERIALIZE_FIRST ? method1 : method2;
       
   137         NOT_MATERIALIZED_METHOD = MATERIALIZE_FIRST ? method2 : method1;
    96     }
   138     }
    97 
   139 
    98     public static void main(String[] args) {
   140     public static void main(String[] args) {
    99         int levels[] = CompilerUtils.getAvailableCompilationLevels();
   141         int levels[] = CompilerUtils.getAvailableCompilationLevels();
   100         // we need compilation level 4 to use EscapeAnalysis
   142         // we need compilation level 4 to use EscapeAnalysis
   105             new MaterializeVirtualObjectTest().test();
   147             new MaterializeVirtualObjectTest().test();
   106         }
   148         }
   107     }
   149     }
   108 
   150 
   109     private static String getName() {
   151     private static String getName() {
   110         return "CASE: invalidate=" + INVALIDATE;
   152         return "CASE: invalidate=" + INVALIDATE + ", materializedMethod="
       
   153                 + (MATERIALIZE_FIRST ? "testFrame" : "testFrame2")
       
   154                 + ", notMaterializedMethod="
       
   155                 + (MATERIALIZE_FIRST ? "testFrame2" : "testFrame");
   111     }
   156     }
   112 
   157 
   113     private void test() {
   158     private void test() {
   114         System.out.println(getName());
   159         System.out.println(getName());
   115         Asserts.assertFalse(WB.isMethodCompiled(METHOD), getName()
   160         Asserts.assertFalse(WB.isMethodCompiled(MATERIALIZED_METHOD),
   116                 + " : method unexpectedly compiled");
   161                 getName() + " : materialized method is compiled");
   117         /* need to trigger compilation by multiple method invocations
   162         Asserts.assertFalse(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),
   118            in order to have method profile data to be gathered */
   163                 getName() + " : not materialized method is compiled");
   119         for (int i = 0; i < COMPILE_THRESHOLD; i++) {
   164         for (int i = 0; i < CompilerWhiteBoxTest.THRESHOLD; i++) {
   120             testFrame("someString", i);
   165             testFrame("someString", i);
   121         }
   166         }
   122         Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
   167         Asserts.assertTrue(WB.isMethodCompiled(MATERIALIZED_METHOD), getName()
   123                 + "Method unexpectedly not compiled");
   168                 + " : materialized method not compiled");
   124         Asserts.assertTrue(WB.getMethodCompilationLevel(METHOD) == 4, getName()
   169         Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),
   125                 + "Method not compiled at level 4");
   170                 getName() + " : not materialized method not compiled");
   126         testFrame("someString", COMPILE_THRESHOLD);
   171         testFrame("someString", /* materialize */ CompilerWhiteBoxTest.THRESHOLD);
   127     }
   172     }
   128 
   173 
   129     private void testFrame(String str, int iteration) {
   174     private void testFrame(String str, int iteration) {
       
   175         Helper helper = new Helper(str);
       
   176         testFrame2(str, iteration);
       
   177         Asserts.assertTrue((helper.string != null) && (this != null)
       
   178                 && (helper != null), String.format("%s : some locals are null", getName()));
       
   179      }
       
   180 
       
   181     private void testFrame2(String str, int iteration) {
   130         Helper helper = new Helper(str);
   182         Helper helper = new Helper(str);
   131         recurse(2, iteration);
   183         recurse(2, iteration);
   132         Asserts.assertTrue((helper.string != null) && (this != null)
   184         Asserts.assertTrue((helper.string != null) && (this != null)
   133                            && (helper != null), String.format("%s : some locals are null", getName()));
   185                 && (helper != null), String.format("%s : some locals are null", getName()));
   134     }
   186     }
       
   187 
   135     private void recurse(int depth, int iteration) {
   188     private void recurse(int depth, int iteration) {
   136         if (depth == 0) {
   189         if (depth == 0) {
   137             check(iteration);
   190             check(iteration);
   138         } else {
   191         } else {
   139             Integer s = new Integer(depth);
   192             Integer s = new Integer(depth);
   140             recurse(depth - 1, iteration);
   193             recurse(depth - 1, iteration);
   141             Asserts.assertEQ(s.intValue(), depth, String.format("different values: %s != %s", s.intValue(), depth));
   194             Asserts.assertEQ(s.intValue(), depth,
       
   195                     String.format("different values: %s != %s", s.intValue(), depth));
   142         }
   196         }
   143     }
   197     }
   144 
   198 
   145     private void check(int iteration) {
   199     private void check(int iteration) {
   146         // Materialize virtual objects on last invocation
   200         // Materialize virtual objects on last invocation
   147         if (iteration == COMPILE_THRESHOLD) {
   201         if (iteration == COMPILE_THRESHOLD) {
   148             HotSpotStackFrameReference hsFrame = CompilerToVMHelper
   202             // get frames and check not-null
   149                     .getNextStackFrame(/* topmost frame */ null,
   203             HotSpotStackFrameReference materialized = CompilerToVMHelper.getNextStackFrame(
   150                             new ResolvedJavaMethod[]{
   204                     /* topmost frame */ null, new ResolvedJavaMethod[]{MATERIALIZED_RESOLVED},
   151                                 RESOLVED_METHOD}, /* don't skip any */ 0);
   205                     /* don't skip any */ 0);
   152             Asserts.assertNotNull(hsFrame, getName() + " : got null frame");
   206             Asserts.assertNotNull(materialized, getName()
   153             Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
   207                     + " : got null frame for materialized method");
   154                     + "Test method should be compiled");
   208             HotSpotStackFrameReference notMaterialized = CompilerToVMHelper.getNextStackFrame(
   155             Asserts.assertTrue(hsFrame.hasVirtualObjects(), getName()
   209                     /* topmost frame */ null, new ResolvedJavaMethod[]{NOT_MATERIALIZED_RESOLVED},
   156                     + ": has no virtual object before materialization");
   210                     /* don't skip any */ 0);
   157             CompilerToVMHelper.materializeVirtualObjects(hsFrame, INVALIDATE);
   211             Asserts.assertNE(materialized, notMaterialized,
   158             Asserts.assertFalse(hsFrame.hasVirtualObjects(), getName()
   212                     "Got same frame pointer for both tested frames");
   159                     + " : has virtual object after materialization");
   213             Asserts.assertNotNull(notMaterialized, getName()
   160             Asserts.assertEQ(WB.isMethodCompiled(METHOD), !INVALIDATE, getName()
   214                     + " : got null frame for not materialized method");
   161                     + " : unexpected compiled status");
   215             // check that frames has virtual objects before materialization stage
       
   216             Asserts.assertTrue(materialized.hasVirtualObjects(), getName()
       
   217                     + ": materialized frame has no virtual object before materialization");
       
   218             Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()
       
   219                     + ": notMaterialized frame has no virtual object before materialization");
       
   220             // materialize
       
   221             CompilerToVMHelper.materializeVirtualObjects(materialized, INVALIDATE);
       
   222             // check that only not materialized frame has virtual objects
       
   223             Asserts.assertFalse(materialized.hasVirtualObjects(), getName()
       
   224                     + " : materialized has virtual object after materialization");
       
   225             Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()
       
   226                     + " : notMaterialized has no virtual object after materialization");
       
   227             // check that materialized frame was deoptimized in case invalidate=true
       
   228             Asserts.assertEQ(WB.isMethodCompiled(MATERIALIZED_METHOD), !INVALIDATE, getName()
       
   229                     + " : materialized method has unexpected compiled status");
       
   230             // check that not materialized frame wasn't deoptimized
       
   231             Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD), getName()
       
   232                     + " : not materialized method has unexpected compiled status");
   162         }
   233         }
   163     }
   234     }
   164 
   235 
   165     private class Helper {
   236     private class Helper {
   166         public String string;
   237         public String string;