1 /*
  2  * Copyright (c) 2000, 2021, 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 
 25 #include "precompiled.hpp"
 26 #include "compiler/compilerDefinitions.hpp"
 27 #include "gc/shared/gcConfig.hpp"
 28 #include "jvm.h"
 29 #include "jvmci/jvmci_globals.hpp"
 30 #include "logging/log.hpp"
 31 #include "runtime/arguments.hpp"
 32 #include "runtime/flags/jvmFlagAccess.hpp"
 33 #include "runtime/globals_extension.hpp"
 34 #include "utilities/defaultStream.hpp"
 35 #include "utilities/ostream.hpp"
 36 
 37 fileStream* JVMCIGlobals::_jni_config_file = NULL;
 38 
 39 // Return true if jvmci flags are consistent.
 40 bool JVMCIGlobals::check_jvmci_flags_are_consistent() {
 41 
 42 #ifndef PRODUCT
 43 #define APPLY_JVMCI_FLAGS(params3, params4) \
 44   JVMCI_FLAGS(params4, params3, params4, params3, params4, IGNORE_RANGE, IGNORE_CONSTRAINT)
 45 #define JVMCI_DECLARE_CHECK4(type, name, value, ...) bool name##checked = false;
 46 #define JVMCI_DECLARE_CHECK3(type, name, ...)        bool name##checked = false;
 47 #define JVMCI_FLAG_CHECKED(name)                          name##checked = true;
 48   APPLY_JVMCI_FLAGS(JVMCI_DECLARE_CHECK3, JVMCI_DECLARE_CHECK4)
 49 #else
 50 #define JVMCI_FLAG_CHECKED(name)
 51 #endif
 52 
 53   // Checks that a given flag is not set if a given guard flag is false.
 54 #define CHECK_NOT_SET(FLAG, GUARD)                     \
 55   JVMCI_FLAG_CHECKED(FLAG)                             \
 56   if (!GUARD && !FLAG_IS_DEFAULT(FLAG)) {              \
 57     jio_fprintf(defaultStream::error_stream(),         \
 58         "Improperly specified VM option '%s': '%s' must be enabled\n", #FLAG, #GUARD); \
 59     return false;                                      \
 60   }
 61 
 62   if (EnableJVMCIProduct) {
 63     if (FLAG_IS_DEFAULT(EnableJVMCI)) {
 64       FLAG_SET_DEFAULT(EnableJVMCI, true);
 65     }
 66     if (EnableJVMCI && FLAG_IS_DEFAULT(UseJVMCICompiler)) {
 67       FLAG_SET_DEFAULT(UseJVMCICompiler, true);
 68     }
 69   }
 70 
 71   JVMCI_FLAG_CHECKED(UseJVMCICompiler)
 72   JVMCI_FLAG_CHECKED(EnableJVMCI)
 73   JVMCI_FLAG_CHECKED(EnableJVMCIProduct)
 74 
 75   CHECK_NOT_SET(BootstrapJVMCI,   UseJVMCICompiler)
 76   CHECK_NOT_SET(PrintBootstrap,   UseJVMCICompiler)
 77   CHECK_NOT_SET(JVMCIThreads,     UseJVMCICompiler)
 78   CHECK_NOT_SET(JVMCIHostThreads, UseJVMCICompiler)
 79 
 80   if (UseJVMCICompiler) {
 81     if (FLAG_IS_DEFAULT(UseJVMCINativeLibrary) && !UseJVMCINativeLibrary) {
 82       char path[JVM_MAXPATHLEN];
 83       if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), JVMCI_SHARED_LIBRARY_NAME)) {
 84         // If a JVMCI native library is present,
 85         // we enable UseJVMCINativeLibrary by default.
 86         FLAG_SET_DEFAULT(UseJVMCINativeLibrary, true);
 87       }
 88     }
 89     if (!FLAG_IS_DEFAULT(EnableJVMCI) && !EnableJVMCI) {
 90       jio_fprintf(defaultStream::error_stream(),
 91           "Improperly specified VM option UseJVMCICompiler: EnableJVMCI cannot be disabled\n");
 92       return false;
 93     }
 94     FLAG_SET_DEFAULT(EnableJVMCI, true);
 95     if (BootstrapJVMCI && UseJVMCINativeLibrary) {
 96       jio_fprintf(defaultStream::error_stream(), "-XX:+BootstrapJVMCI is not compatible with -XX:+UseJVMCINativeLibrary\n");
 97       return false;
 98     }
 99     if (BootstrapJVMCI && (TieredStopAtLevel < CompLevel_full_optimization)) {
100       jio_fprintf(defaultStream::error_stream(),
101           "-XX:+BootstrapJVMCI is not compatible with -XX:TieredStopAtLevel=%d\n", TieredStopAtLevel);
102       return false;
103     }
104     if (UseCompactObjectHeaders) {
105       log_warning(jvmci)("-XX:+UseCompactObjectHeaders not supported by JVMCI, disabling UseCompactObjectHeaders");
106       FLAG_SET_DEFAULT(UseCompactObjectHeaders, false);
107     }
108   }
109 
110   if (!EnableJVMCI) {
111     // Switch off eager JVMCI initialization if JVMCI is disabled.
112     // Don't throw error if EagerJVMCI is set to allow testing.
113     if (EagerJVMCI) {
114       FLAG_SET_DEFAULT(EagerJVMCI, false);
115     }
116   }
117   JVMCI_FLAG_CHECKED(EagerJVMCI)
118 
119   CHECK_NOT_SET(JVMCIEventLogLevel,           EnableJVMCI)
120   CHECK_NOT_SET(JVMCITraceLevel,              EnableJVMCI)
121   CHECK_NOT_SET(JVMCICounterSize,             EnableJVMCI)
122   CHECK_NOT_SET(JVMCICountersExcludeCompiler, EnableJVMCI)
123   CHECK_NOT_SET(JVMCINMethodSizeLimit,        EnableJVMCI)
124   CHECK_NOT_SET(JVMCIPrintProperties,         EnableJVMCI)
125   CHECK_NOT_SET(UseJVMCINativeLibrary,        EnableJVMCI)
126   CHECK_NOT_SET(JVMCILibPath,                 EnableJVMCI)
127   CHECK_NOT_SET(JVMCILibDumpJNIConfig,        EnableJVMCI)
128 
129 #ifndef COMPILER2
130   JVMCI_FLAG_CHECKED(MaxVectorSize)
131   JVMCI_FLAG_CHECKED(ReduceInitialCardMarks)
132   JVMCI_FLAG_CHECKED(UseMultiplyToLenIntrinsic)
133   JVMCI_FLAG_CHECKED(UseSquareToLenIntrinsic)
134   JVMCI_FLAG_CHECKED(UseMulAddIntrinsic)
135   JVMCI_FLAG_CHECKED(UseMontgomeryMultiplyIntrinsic)
136   JVMCI_FLAG_CHECKED(UseMontgomerySquareIntrinsic)
137 #endif // !COMPILER2
138 
139 #ifndef PRODUCT
140 #define JVMCI_CHECK4(type, name, value, ...) assert(name##checked, #name " flag not checked");
141 #define JVMCI_CHECK3(type, name, ...)        assert(name##checked, #name " flag not checked");
142   // Ensures that all JVMCI flags are checked by this method.
143   APPLY_JVMCI_FLAGS(JVMCI_CHECK3, JVMCI_CHECK4)
144 #undef APPLY_JVMCI_FLAGS
145 #undef JVMCI_DECLARE_CHECK3
146 #undef JVMCI_DECLARE_CHECK4
147 #undef JVMCI_CHECK3
148 #undef JVMCI_CHECK4
149 #undef JVMCI_FLAG_CHECKED
150 #endif // PRODUCT
151 #undef CHECK_NOT_SET
152 
153   if (JVMCILibDumpJNIConfig != NULL) {
154     _jni_config_file = new(ResourceObj::C_HEAP, mtJVMCI) fileStream(JVMCILibDumpJNIConfig);
155     if (_jni_config_file == NULL || !_jni_config_file->is_open()) {
156       jio_fprintf(defaultStream::error_stream(),
157           "Could not open file for dumping JVMCI shared library JNI config: %s\n", JVMCILibDumpJNIConfig);
158       return false;
159     }
160   }
161 
162   return true;
163 }
164 
165 // Convert JVMCI flags from experimental to product
166 bool JVMCIGlobals::enable_jvmci_product_mode(JVMFlagOrigin origin) {
167   const char *JVMCIFlags[] = {
168     "EnableJVMCI",
169     "EnableJVMCIProduct",
170     "UseJVMCICompiler",
171     "JVMCIPrintProperties",
172     "EagerJVMCI",
173     "JVMCIThreads",
174     "JVMCICounterSize",
175     "JVMCICountersExcludeCompiler",
176     "JVMCINMethodSizeLimit",
177     "JVMCIEventLogLevel",
178     "JVMCITraceLevel",
179     "JVMCILibPath",
180     "JVMCILibDumpJNIConfig",
181     "UseJVMCINativeLibrary",
182     NULL
183   };
184 
185   for (int i = 0; JVMCIFlags[i] != NULL; i++) {
186     JVMFlag *jvmciFlag = (JVMFlag *)JVMFlag::find_declared_flag(JVMCIFlags[i]);
187     if (jvmciFlag == NULL) {
188       return false;
189     }
190     jvmciFlag->clear_experimental();
191     jvmciFlag->set_product();
192   }
193 
194   bool value = true;
195   JVMFlag *jvmciEnableFlag = JVMFlag::find_flag("EnableJVMCIProduct");
196   if (JVMFlagAccess::set_bool(jvmciEnableFlag, &value, origin) != JVMFlag::SUCCESS) {
197     return false;
198   }
199 
200   // Effect of EnableJVMCIProduct on changing defaults of EnableJVMCI
201   // and UseJVMCICompiler is deferred to check_jvmci_flags_are_consistent
202   // so that setting these flags explicitly (e.g. on the command line)
203   // takes precedence.
204 
205   return true;
206 }
207 
208 bool JVMCIGlobals::gc_supports_jvmci() {
209   return UseSerialGC || UseParallelGC || UseG1GC;
210 }
211 
212 void JVMCIGlobals::check_jvmci_supported_gc() {
213   if (EnableJVMCI) {
214     // Check if selected GC is supported by JVMCI and Java compiler
215     if (!gc_supports_jvmci()) {
216       log_warning(gc, jvmci)("Setting EnableJVMCI to false as selected GC does not support JVMCI: %s", GCConfig::hs_err_name());
217       FLAG_SET_DEFAULT(EnableJVMCI, false);
218       FLAG_SET_DEFAULT(UseJVMCICompiler, false);
219     }
220   }
221 }