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