1 /*
   2  * Copyright (c) 2016, 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @summary Intrinsic for JFR
  29  * @key jfr
  30  * @library /test/lib
  31  *
  32  * @modules jdk.jfr/jdk.jfr.internal
  33  *          java.base/jdk.internal.misc
  34  *          java.management
  35  *
  36  * @build sun.hotspot.WhiteBox
  37  * @build ClassFileInstaller
  38  *
  39  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  40  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  41  *
  42  * @run main/othervm -Xbootclasspath/a:. -ea -Xmixed -Xbatch -XX:TieredStopAtLevel=4 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  43  *      jdk.jfr.jvm.TestJFRIntrinsic
  44  *
  45   * @run main/othervm -Xbootclasspath/a:. -ea -Xmixed -Xbatch -XX:TieredStopAtLevel=1 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  46  *      jdk.jfr.jvm.TestJFRIntrinsic
  47  */
  48 
  49 package jdk.jfr.jvm;
  50 
  51 import java.lang.reflect.Method;
  52 import java.util.Arrays;
  53 import java.util.stream.IntStream;
  54 import jdk.jfr.internal.JVM;
  55 import jdk.test.lib.Platform;
  56 import sun.hotspot.WhiteBox;
  57 import sun.hotspot.code.NMethod;
  58 
  59 public class TestJFRIntrinsic {
  60 
  61     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  62     public Object eventWriter;
  63 
  64     public static void main(String... args) throws Exception {
  65         /*
  66         Temporarily excluded until getClassId is reworked to accommodate epoch shift tagging
  67         Method classid = TestJFRIntrinsic.class.getDeclaredMethod("getClassIdIntrinsic",  Class.class);
  68         ti.runIntrinsicTest(classid);
  69         */
  70         TestJFRIntrinsic ti = new TestJFRIntrinsic();
  71         Method eventWriterMethod = TestJFRIntrinsic.class.getDeclaredMethod("getEventWriterIntrinsic", Class.class);
  72         ti.runIntrinsicTest(eventWriterMethod);
  73     }
  74 
  75     /*
  76     public void getClassIdIntrinsic(Class<?> cls) {
  77         long exp = JVM.getClassId(cls);
  78         if (exp == 0) {
  79             throw new RuntimeException("Class id is zero");
  80         }
  81     }
  82     */
  83 
  84     public void getEventWriterIntrinsic(Class<?> cls) {
  85         Object o = JVM.getEventWriter();
  86         if (o != null) {
  87             eventWriter = o;
  88         }
  89     }
  90 
  91     void runIntrinsicTest(Method method) throws Exception {
  92         if (getMaxCompilationLevel() < 1) {
  93             /* no compiler */
  94             return;
  95         }
  96         /* load it */
  97         try {
  98             method.invoke(this, TestJFRIntrinsic.class);
  99         } catch(Exception e) {
 100             throw new RuntimeException(e);
 101         }
 102 
 103         int[] lvls = getAvailableCompilationLevels();
 104         for (int i : lvls) {
 105             if (!WHITE_BOX.enqueueMethodForCompilation(method, i)) {
 106                 throw new RuntimeException("Failed to enqueue method on level: " + i);
 107             }
 108 
 109             if (WHITE_BOX.isMethodCompiled(method)) {
 110                 NMethod nm = NMethod.get(method, false);
 111                 if (nm.comp_level != i) {
 112                     throw new RuntimeException("Failed to compile on correct level: " + i);
 113                 }
 114                 System.out.println("Compiled " + method + " on level "  + i);
 115             }
 116         }
 117     }
 118 
 119     /* below is copied from CompilerUtil in hotspot test lib, removed this when it's moved */
 120 
 121     /**
 122      * Returns available compilation levels
 123      *
 124      * @return int array with compilation levels
 125      */
 126     public static int[] getAvailableCompilationLevels() {
 127         if (!WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompiler")) {
 128             return new int[0];
 129         }
 130         if (WhiteBox.getWhiteBox().getBooleanVMFlag("TieredCompilation")) {
 131             Long flagValue = WhiteBox.getWhiteBox()
 132                     .getIntxVMFlag("TieredStopAtLevel");
 133             int maxLevel = flagValue.intValue();
 134             return IntStream.rangeClosed(1, maxLevel).toArray();
 135         } else {
 136             if (Platform.isServer() && !Platform.isEmulatedClient()) {
 137                 return new int[]{4};
 138             }
 139             if (Platform.isClient() || Platform.isMinimal() || Platform.isEmulatedClient()) {
 140                 return new int[]{1};
 141             }
 142         }
 143         return new int[0];
 144     }
 145 
 146     /**
 147      * Returns maximum compilation level available
 148      * @return an int value representing maximum compilation level available
 149      */
 150     public static int getMaxCompilationLevel() {
 151         return Arrays.stream(getAvailableCompilationLevels())
 152                 .max()
 153                 .getAsInt();
 154     }
 155 }