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 sun.hotspot.WhiteBox;
  27 
  28 /**
  29  * API to obtain information about enabled JIT compilers
  30  * retrieved from the VM with the WhiteBox API.
  31  */
  32 public class Compiler {
  33 
  34     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  35 
  36     /**
  37      * Check if Graal is used as JIT compiler.
  38      *
  39      * Graal is enabled if following conditions are true:
  40      * - we are not in Interpreter mode
  41      * - UseJVMCICompiler flag is true
  42      * - jvmci.Compiler variable is equal to 'graal'
  43      * - TieredCompilation is not used or TieredStopAtLevel is greater than 3
  44      * No need to check client mode because it set UseJVMCICompiler to false.
  45      *
  46      * @return true if Graal is used as JIT compiler.
  47      */
  48     public static boolean isGraalEnabled() {
  49         Boolean useCompiler = WB.getBooleanVMFlag("UseCompiler");
  50         if (useCompiler == null || !useCompiler) {
  51             return false;
  52         }
  53         Boolean useJvmciComp = WB.getBooleanVMFlag("UseJVMCICompiler");
  54         if (useJvmciComp == null || !useJvmciComp) {
  55             return false;
  56         }
  57         // This check might be redundant but let's keep it for now.
  58         String jvmciCompiler = System.getProperty("jvmci.Compiler");
  59         if (jvmciCompiler == null || !jvmciCompiler.equals("graal")) {
  60             return false;
  61         }
  62 
  63         Boolean tieredCompilation = WB.getBooleanVMFlag("TieredCompilation");
  64         Long compLevel = WB.getIntxVMFlag("TieredStopAtLevel");
  65         // if TieredCompilation is enabled and compilation level is <= 3 then no Graal is used
  66         if (tieredCompilation != null && tieredCompilation &&
  67             compLevel != null && compLevel <= 3) {
  68             return false;
  69         }
  70         return true;
  71     }
  72 
  73     /**
  74      * Check if C2 is used as JIT compiler.
  75      *
  76      * C2 is enabled if following conditions are true:
  77      * - we are not in Interpreter mode
  78      * - we are in Server compilation mode
  79      * - TieredCompilation is not used or TieredStopAtLevel is greater than 3
  80      * - Graal is not used
  81      *
  82      * @return true if C2 is used as JIT compiler.
  83      */
  84     public static boolean isC2Enabled() {
  85         Boolean useCompiler = WB.getBooleanVMFlag("UseCompiler");
  86         if (useCompiler == null || !useCompiler) {
  87             return false;
  88         }
  89         Boolean serverMode = WB.getBooleanVMFlag("ProfileInterpreter");
  90         if (serverMode == null || !serverMode) {
  91             return false;
  92         }
  93 
  94         Boolean tieredCompilation = WB.getBooleanVMFlag("TieredCompilation");
  95         Long compLevel = WB.getIntxVMFlag("TieredStopAtLevel");
  96         // if TieredCompilation is enabled and compilation level is <= 3 then no Graal is used
  97         if (tieredCompilation != null && tieredCompilation &&
  98             compLevel != null && compLevel <= 3) {
  99             return false;
 100         }
 101 
 102         if (isGraalEnabled()) {
 103             return false;
 104         }
 105 
 106         return true;
 107     }
 108 
 109     /*
 110      * Check if C1 is used as JIT compiler.
 111      *
 112      * C1 is enabled if following conditions are true:
 113      * - we are not in Interpreter mode
 114      * - we are not in Server compilation mode
 115      * - TieredCompilation is used in Server mode
 116      *
 117      * @return true if C1 is used as JIT compiler.
 118      */
 119     public static boolean isC1Enabled() {
 120         Boolean useCompiler = WB.getBooleanVMFlag("UseCompiler");
 121         if (useCompiler == null || !useCompiler) {
 122             return false;
 123         }
 124         Boolean serverMode = WB.getBooleanVMFlag("ProfileInterpreter");
 125         if (serverMode == null || !serverMode) {
 126             return true; // Client mode
 127         }
 128 
 129         Boolean tieredCompilation = WB.getBooleanVMFlag("TieredCompilation");
 130         // C1 is not used in server mode if TieredCompilation is off.
 131         if (tieredCompilation != null && !tieredCompilation) {
 132             return false;
 133         }
 134         return true;
 135     }
 136 }