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