1 /*
  2  * Copyright (c) 2016, 2021, 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  * @summary Intrinsic for JFR
 27  * @key jfr
 28  * @requires vm.hasJFR
 29  * @library /test/lib
 30  *
 31  * @modules jdk.jfr/jdk.jfr.internal
 32  *          java.base/jdk.internal.misc
 33  *          java.management
 34  *
 35  * @build sun.hotspot.WhiteBox
 36  *
 37  * @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
 38  *
 39  * @run main/othervm -Xbootclasspath/a:. -ea -Xmixed -Xbatch -XX:TieredStopAtLevel=4 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 40  *      jdk.jfr.jvm.TestJFRIntrinsic
 41  *
 42   * @run main/othervm -Xbootclasspath/a:. -ea -Xmixed -Xbatch -XX:TieredStopAtLevel=1 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 43  *      jdk.jfr.jvm.TestJFRIntrinsic
 44  */
 45 
 46 package jdk.jfr.jvm;
 47 
 48 import java.lang.reflect.Method;
 49 import java.util.Arrays;
 50 import java.util.stream.IntStream;
 51 import jdk.jfr.internal.JVM;
 52 import jdk.test.lib.Platform;
 53 import sun.hotspot.WhiteBox;
 54 import sun.hotspot.code.NMethod;
 55 
 56 public class TestJFRIntrinsic {
 57     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 58     public Class<?> eventWriterClazz;
 59     public Object eventWriter;
 60 
 61     TestJFRIntrinsic() throws Exception {
 62         // the intrinsic is premised on this class being loaded already - the event writer object is massaged heavily before returning
 63         eventWriterClazz = Class.forName("jdk.jfr.internal.EventWriter", true, TestJFRIntrinsic.class.getClassLoader());
 64     }
 65 
 66     public static void main(String... args) throws Exception {
 67         JVM.getJVM().createNativeJFR();
 68         TestJFRIntrinsic ti = new TestJFRIntrinsic();
 69         Method classid = TestJFRIntrinsic.class.getDeclaredMethod("getClassIdIntrinsic",  Class.class);
 70         ti.runIntrinsicTest(classid);
 71         Method eventWriterMethod = TestJFRIntrinsic.class.getDeclaredMethod("getEventWriterIntrinsic", Class.class);
 72         ti.runIntrinsicTest(eventWriterMethod);
 73     }
 74 
 75     public void getClassIdIntrinsic(Class<?> cls) {
 76         long exp = JVM.getClassId(cls);
 77         if (exp == 0) {
 78             throw new RuntimeException("Class id is zero");
 79         }
 80     }
 81 
 82     public void getEventWriterIntrinsic(Class<?> cls) {
 83         Object o = JVM.getEventWriter();
 84         if (o != null) {
 85             eventWriter = o;
 86         }
 87     }
 88 
 89     void runIntrinsicTest(Method method) throws Exception {
 90         if (getMaxCompilationLevel() < 1) {
 91             /* no compiler */
 92             return;
 93         }
 94         /* load it */
 95         try {
 96             method.invoke(this, TestJFRIntrinsic.class);
 97         } catch(Exception e) {
 98             throw new RuntimeException(e);
 99         }
100 
101         int[] lvls = getAvailableCompilationLevels();
102         for (int i : lvls) {
103             if (!WHITE_BOX.enqueueMethodForCompilation(method, i)) {
104                 throw new RuntimeException("Failed to enqueue method on level: " + i);
105             }
106 
107             if (WHITE_BOX.isMethodCompiled(method)) {
108                 NMethod nm = NMethod.get(method, false);
109                 if (nm.comp_level != i) {
110                     throw new RuntimeException("Failed to compile on correct level: " + i);
111                 }
112                 System.out.println("Compiled " + method + " on level "  + i);
113             }
114         }
115     }
116 
117     /* below is copied from CompilerUtil in hotspot test lib, removed this when it's moved */
118 
119     /**
120      * Returns available compilation levels
121      *
122      * @return int array with compilation levels
123      */
124     public static int[] getAvailableCompilationLevels() {
125         if (!WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompiler")) {
126             return new int[0];
127         }
128         if (WhiteBox.getWhiteBox().getBooleanVMFlag("TieredCompilation")) {
129             Long flagValue = WhiteBox.getWhiteBox()
130                     .getIntxVMFlag("TieredStopAtLevel");
131             int maxLevel = flagValue.intValue();
132             return IntStream.rangeClosed(1, maxLevel).toArray();
133         } else {
134             if (Platform.isServer() && !Platform.isEmulatedClient()) {
135                 return new int[]{4};
136             }
137             if (Platform.isClient() || Platform.isMinimal() || Platform.isEmulatedClient()) {
138                 return new int[]{1};
139             }
140         }
141         return new int[0];
142     }
143 
144     /**
145      * Returns maximum compilation level available
146      * @return an int value representing maximum compilation level available
147      */
148     public static int getMaxCompilationLevel() {
149         return Arrays.stream(getAvailableCompilationLevels())
150                 .max()
151                 .getAsInt();
152     }
153 }