1 /*
   2  * Copyright (c) 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.
   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 package sun.hotspot.code;
  25 
  26 import java.lang.reflect.Executable;
  27 import sun.hotspot.WhiteBox;
  28 
  29 /**
  30  * API to obtain information about enabled JIT compilers
  31  * retrieved from the VM with the WhiteBox API.
  32  */
  33 public class Compiler {
  34 
  35     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  36 
  37     /**
  38      * Check if Graal is used as JIT compiler.
  39      *
  40      * Graal is enabled if following conditions are true:
  41      * - we are not in Interpreter mode
  42      * - UseJVMCICompiler flag is true
  43      * - jvmci.Compiler variable is equal to 'graal'
  44      * - TieredCompilation is not used or TieredStopAtLevel is greater than 3
  45      * No need to check client mode because it set UseJVMCICompiler to false.
  46      *
  47      * @return true if Graal is used as JIT compiler.
  48      */
  49     public static boolean isGraalEnabled() {
  50         Boolean useCompiler = WB.getBooleanVMFlag("UseCompiler");
  51         if (useCompiler == null || !useCompiler) {
  52             return false;
  53         }
  54         Boolean useJvmciComp = WB.getBooleanVMFlag("UseJVMCICompiler");
  55         if (useJvmciComp == null || !useJvmciComp) {
  56             return false;
  57         }
  58 
  59         Boolean tieredCompilation = WB.getBooleanVMFlag("TieredCompilation");
  60         Long compLevel = WB.getIntxVMFlag("TieredStopAtLevel");
  61         // if TieredCompilation is enabled and compilation level is <= 3 then no Graal is used
  62         if (tieredCompilation != null && tieredCompilation &&
  63             compLevel != null && compLevel <= 3) {
  64             return false;
  65         }
  66         return true;
  67     }
  68 
  69     /**
  70      * Check if C2 is used as JIT compiler.
  71      *
  72      * C2 is enabled if following conditions are true:
  73      * - we are not in Interpreter mode
  74      * - we are in Server compilation mode
  75      * - TieredCompilation is not used or TieredStopAtLevel is greater than 3
  76      * - Graal is not used
  77      *
  78      * @return true if C2 is used as JIT compiler.
  79      */
  80     public static boolean isC2Enabled() {
  81         Boolean useCompiler = WB.getBooleanVMFlag("UseCompiler");
  82         if (useCompiler == null || !useCompiler) {
  83             return false;
  84         }
  85         Boolean serverMode = WB.getBooleanVMFlag("ProfileInterpreter");
  86         if (serverMode == null || !serverMode) {
  87             return false;
  88         }
  89 
  90         Boolean tieredCompilation = WB.getBooleanVMFlag("TieredCompilation");
  91         Long compLevel = WB.getIntxVMFlag("TieredStopAtLevel");
  92         // if TieredCompilation is enabled and compilation level is <= 3 then no Graal is used
  93         if (tieredCompilation != null && tieredCompilation &&
  94             compLevel != null && compLevel <= 3) {
  95             return false;
  96         }
  97 
  98         if (isGraalEnabled()) {
  99             return false;
 100         }
 101 
 102         return true;
 103     }
 104 
 105     /*
 106      * Check if C1 is used as JIT compiler.
 107      *
 108      * C1 is enabled if following conditions are true:
 109      * - we are not in Interpreter mode
 110      * - we are not in Server compilation mode
 111      * - TieredCompilation is used in Server mode
 112      *
 113      * @return true if C1 is used as JIT compiler.
 114      */
 115     public static boolean isC1Enabled() {
 116         Boolean useCompiler = WB.getBooleanVMFlag("UseCompiler");
 117         if (useCompiler == null || !useCompiler) {
 118             return false;
 119         }
 120         Boolean serverMode = WB.getBooleanVMFlag("ProfileInterpreter");
 121         if (serverMode == null || !serverMode) {
 122             return true; // Client mode
 123         }
 124 
 125         Boolean tieredCompilation = WB.getBooleanVMFlag("TieredCompilation");
 126         // C1 is not used in server mode if TieredCompilation is off.
 127         if (tieredCompilation != null && !tieredCompilation) {
 128             return false;
 129         }
 130         return true;
 131     }
 132 
 133     /*
 134      * Determine if the compiler corresponding to the compilation level 'compLevel'
 135      * provides an intrinsic for 'class'.'method'.
 136      */
 137     public static boolean isIntrinsicAvailable(int compLevel, String klass, String method, Class<?>... parameterTypes) {
 138         Executable intrinsicMethod;
 139         try {
 140             intrinsicMethod = Class.forName(klass).getDeclaredMethod(method, parameterTypes);
 141         } catch (NoSuchMethodException e) {
 142             throw new RuntimeException("Test bug, '" + method + "' method unavailable. " + e);
 143         } catch (ClassNotFoundException e) {
 144             throw new RuntimeException("Test bug, '" + klass + "' class unavailable. " + e);
 145         }
 146         return WB.isIntrinsicAvailable(intrinsicMethod, compLevel);
 147     }
 148 }