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  * @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 MaterializeVirtualObjectTest
  31  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  32  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  33  *                              jdk.vm.ci.hotspot.CompilerToVMHelper
  34  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  35  *     -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  36  *     -XX:CompileCommand=exclude,*::check -XX:+DoEscapeAnalysis
  37  *     compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  38  */
  39 
  40 package compiler.jvmci.compilerToVM;
  41 
  42 import compiler.jvmci.common.CTVMUtilities;
  43 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  44 import jdk.test.lib.Asserts;
  45 import jdk.test.lib.Utils;
  46 import sun.hotspot.WhiteBox;
  47 import java.lang.reflect.Method;
  48 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl;
  49 import jdk.vm.ci.hotspot.HotSpotStackFrameReference;
  50 
  51 public class MaterializeVirtualObjectTest {
  52     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  53     private static final int INVOCATIONS = 100_000;
  54     private static final Method METHOD;
  55     private static final HotSpotResolvedJavaMethodImpl RESOLVED_METHOD;
  56     private final boolean invalidate;
  57 
  58     static {
  59         try {
  60             METHOD = MaterializeVirtualObjectTest.class.getDeclaredMethod(
  61                     "testFrame", String.class, Integer.class);
  62         } catch (NoSuchMethodException e) {
  63             throw new Error("Can't get executable for test method", e);
  64         }
  65         RESOLVED_METHOD = CTVMUtilities.getResolvedMethod(METHOD);
  66     }
  67 
  68     public MaterializeVirtualObjectTest(boolean invalidate) {
  69         this.invalidate = invalidate;
  70     }
  71 
  72     public static void main(String[] args) {
  73         new MaterializeVirtualObjectTest(true).test();
  74         new MaterializeVirtualObjectTest(false).test();
  75     }
  76 
  77     private void test() {
  78         System.out.printf("CASE: invalidate=%b%n", invalidate);
  79         for (int i = 0; i < INVOCATIONS; i++) {
  80             testFrame("someString", i);
  81         }
  82         Utils.waitForCondition(() -> WB.isMethodCompiled(METHOD), 100L);
  83         Asserts.assertTrue(WB.isMethodCompiled(METHOD));
  84         testFrame("someString", INVOCATIONS);
  85     }
  86 
  87     private void testFrame(String str, Integer iteration) {
  88         Helper helper = new Helper(str);
  89         check(iteration);
  90         Asserts.assertTrue((helper.string != null) && (this != null)
  91                 && (helper != null), "Some locals are null");
  92     }
  93 
  94     private void check(int iteration) {
  95         // Materialize virtual objects on last invocation
  96         if (iteration == INVOCATIONS) {
  97             HotSpotStackFrameReference hsFrame = CompilerToVMHelper
  98                     .getNextStackFrame(/* topmost frame */ null,
  99                             new HotSpotResolvedJavaMethodImpl[]{
 100                                 RESOLVED_METHOD}, /* don't skip any */ 0);
 101             Asserts.assertNotNull(hsFrame, "Got null frame");
 102             Asserts.assertTrue(WB.isMethodCompiled(METHOD),
 103                     "Expected test method to be compiled");
 104             Asserts.assertTrue(hsFrame.hasVirtualObjects(),
 105                     "Has no virtual object before materialization");
 106             CompilerToVMHelper.materializeVirtualObjects(hsFrame, invalidate);
 107             Asserts.assertFalse(hsFrame.hasVirtualObjects(),
 108                     "Has virtual object after materialization");
 109             Asserts.assertEQ(WB.isMethodCompiled(METHOD), !invalidate,
 110                     "Unexpected compiled status");
 111         }
 112     }
 113 
 114     private class Helper {
 115         public String string;
 116 
 117         public Helper(String s) {
 118             this.string = s;
 119         }
 120     }
 121 }