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 (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.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     private static final int COMPILE_THRESHOLD;
  79 
  80     static {
  81         WB = WhiteBox.getWhiteBox();
  82         try {
  83             METHOD = MaterializeVirtualObjectTest.class.getDeclaredMethod(
  84                     "testFrame", String.class, int.class);
  85         } catch (NoSuchMethodException e) {
  86             throw new Error("Can't get executable for test method", e);
  87         }
  88         RESOLVED_METHOD = CTVMUtilities.getResolvedMethod(METHOD);
  89         INVALIDATE = Boolean.getBoolean(
  90                 "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate");
  91         COMPILE_THRESHOLD = WB.getBooleanVMFlag("TieredCompilation")
  92                 ? CompilerWhiteBoxTest.THRESHOLD
  93                 : CompilerWhiteBoxTest.THRESHOLD * 2;
  94     }
  95 
  96     public static void main(String[] args) {
  97         int levels[] = CompilerUtils.getAvailableCompilationLevels();
  98         // we need compilation level 4 to use EscapeAnalysis
  99         if (levels.length < 1 || levels[levels.length - 1] != 4) {
 100             System.out.println("INFO: Test needs compilation level 4 to"
 101                     + " be available. Skipping.");
 102         } else {
 103             new MaterializeVirtualObjectTest().test();
 104         }
 105     }
 106 
 107     private static String getName() {
 108         return "CASE: invalidate=" + INVALIDATE;
 109     }
 110 
 111     private void test() {
 112         System.out.println(getName());
 113         Asserts.assertFalse(WB.isMethodCompiled(METHOD), getName()
 114                 + " : method unexpectedly compiled");
 115         /* need to trigger compilation by multiple method invocations
 116            in order to have method profile data to be gathered */
 117         for (int i = 0; i < COMPILE_THRESHOLD; i++) {
 118             testFrame("someString", i);
 119         }
 120         Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
 121                 + "Method unexpectedly not compiled");
 122         testFrame("someString", COMPILE_THRESHOLD);
 123     }
 124 
 125     private void testFrame(String str, int iteration) {
 126         Helper helper = new Helper(str);
 127         check(iteration);
 128         Asserts.assertTrue((helper.string != null) && (this != null)
 129                 && (helper != null), getName() + " : some locals are null");
 130     }
 131 
 132     private void check(int iteration) {
 133         // Materialize virtual objects on last invocation
 134         if (iteration == COMPILE_THRESHOLD) {
 135             HotSpotStackFrameReference hsFrame = CompilerToVMHelper
 136                     .getNextStackFrame(/* topmost frame */ null,
 137                             new ResolvedJavaMethod[]{
 138                                 RESOLVED_METHOD}, /* don't skip any */ 0);
 139             Asserts.assertNotNull(hsFrame, getName() + " : got null frame");
 140             Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
 141                     + "Test method should be compiled");
 142             Asserts.assertTrue(hsFrame.hasVirtualObjects(), getName()
 143                     + ": has no virtual object before materialization");
 144             CompilerToVMHelper.materializeVirtualObjects(hsFrame, INVALIDATE);
 145             Asserts.assertFalse(hsFrame.hasVirtualObjects(), getName()
 146                     + " : has virtual object after materialization");
 147             Asserts.assertEQ(WB.isMethodCompiled(METHOD), !INVALIDATE, getName()
 148                     + " : unexpected compiled status");
 149         }
 150     }
 151 
 152     private class Helper {
 153         public String string;
 154 
 155         public Helper(String s) {
 156             this.string = s;
 157         }
 158     }
 159 }