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.simpleArch == "aarch64")
  28  * @library / /testlibrary /test/lib
  29  * @library ../common/patches
  30  * @modules java.base/jdk.internal.misc
  31  * @modules java.base/jdk.internal.org.objectweb.asm
  32  *          java.base/jdk.internal.org.objectweb.asm.tree
  33  *          jdk.vm.ci/jdk.vm.ci.hotspot
  34  *          jdk.vm.ci/jdk.vm.ci.code
  35  *          jdk.vm.ci/jdk.vm.ci.meta
  36  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  37  * @build compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  38  * @build sun.hotspot.WhiteBox
  39  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  40  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  41  * @run main/othervm -Xmixed -Xbootclasspath/a:.
  42  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  43  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  44  *                   -XX:CompileCommand=exclude,*::check
  45  *                   -XX:+DoEscapeAnalysis
  46  *                   -Xbatch
  47  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
  48  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  49  * @run main/othervm -Xmixed -Xbootclasspath/a:.
  50  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  51  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  52  *                   -XX:CompileCommand=exclude,*::check
  53  *                   -XX:+DoEscapeAnalysis
  54  *                   -Xbatch
  55  *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
  56  *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
  57  */
  58 
  59 package compiler.jvmci.compilerToVM;
  60 
  61 import java.lang.reflect.Method;
  62 import jdk.vm.ci.hotspot.HotSpotStackFrameReference;
  63 import jdk.vm.ci.meta.ResolvedJavaMethod;
  64 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  65 import jdk.test.lib.Asserts;
  66 
  67 import compiler.jvmci.common.CTVMUtilities;
  68 import compiler.testlibrary.CompilerUtils;
  69 import compiler.whitebox.CompilerWhiteBoxTest;
  70 
  71 import sun.hotspot.WhiteBox;
  72 
  73 public class MaterializeVirtualObjectTest {
  74     private static final WhiteBox WB;
  75     private static final Method METHOD;
  76     private static final ResolvedJavaMethod RESOLVED_METHOD;
  77     private static final boolean INVALIDATE;
  78 
  79     static {
  80         WB = WhiteBox.getWhiteBox();
  81         try {
  82             METHOD = MaterializeVirtualObjectTest.class.getDeclaredMethod(
  83                     "testFrame", String.class, int.class);
  84         } catch (NoSuchMethodException e) {
  85             throw new Error("Can't get executable for test method", e);
  86         }
  87         RESOLVED_METHOD = CTVMUtilities.getResolvedMethod(METHOD);
  88         INVALIDATE = Boolean.getBoolean(
  89             "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate");
  90     }
  91 
  92     public static void main(String[] args) {
  93         int levels[] = CompilerUtils.getAvailableCompilationLevels();
  94         // we need compilation level 4 to use EscapeAnalysis
  95         if (levels.length < 1 || levels[levels.length - 1] != 4) {
  96             System.out.println("INFO: Test needs compilation level 4 to"
  97                     + " be available. Skipping.");
  98         } else {
  99             new MaterializeVirtualObjectTest().test();
 100         }
 101     }
 102 
 103     private static String getName() {
 104         return "CASE: invalidate=" + INVALIDATE;
 105     }
 106 
 107     private void test() {
 108         System.out.println(getName());
 109         Asserts.assertFalse(WB.isMethodCompiled(METHOD), getName()
 110                 + " : method unexpectedly compiled");
 111         /* need to trigger compilation by multiple method invocations
 112            in order to have method profile data to be gathered */
 113         boolean isTiered = WB.getBooleanVMFlag("TieredCompilation");
 114         int COMPILE_THRESHOLD = isTiered ? CompilerWhiteBoxTest.THRESHOLD
 115                 : CompilerWhiteBoxTest.THRESHOLD * 2;
 116         for (int i = 0; i < COMPILE_THRESHOLD; i++) {
 117             testFrame("someString", i);
 118         }
 119         Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
 120                 + "Method unexpectedly not compiled");
 121         testFrame("someString", CompilerWhiteBoxTest.THRESHOLD);
 122     }
 123 
 124     private void testFrame(String str, int iteration) {
 125         Helper helper = new Helper(str);
 126         check(iteration);
 127         Asserts.assertTrue((helper.string != null) && (this != null)
 128                 && (helper != null), getName() + " : some locals are null");
 129     }
 130 
 131     private void check(int iteration) {
 132         // Materialize virtual objects on last invocation
 133         if (iteration == CompilerWhiteBoxTest.THRESHOLD) {
 134             HotSpotStackFrameReference hsFrame = CompilerToVMHelper
 135                     .getNextStackFrame(/* topmost frame */ null,
 136                             new ResolvedJavaMethod[]{
 137                                 RESOLVED_METHOD}, /* don't skip any */ 0);
 138             Asserts.assertNotNull(hsFrame, getName() + " : got null frame");
 139             Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
 140                     + "Test method should be compiled");
 141             Asserts.assertTrue(hsFrame.hasVirtualObjects(), getName()
 142                     + ": has no virtual object before materialization");
 143             CompilerToVMHelper.materializeVirtualObjects(hsFrame, INVALIDATE);
 144             Asserts.assertFalse(hsFrame.hasVirtualObjects(), getName()
 145                     + " : has virtual object after materialization");
 146             Asserts.assertEQ(WB.isMethodCompiled(METHOD), !INVALIDATE, getName()
 147                     + " : unexpected compiled status");
 148         }
 149     }
 150 
 151     private class Helper {
 152         public String string;
 153 
 154         public Helper(String s) {
 155             this.string = s;
 156         }
 157     }
 158 }