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