1 /*
  2  * Copyright (c) 2000, 2023, 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 = nullptr;
 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   JVMCI_FLAG_CHECKED(UseGraalJIT)
 75 
 76   CHECK_NOT_SET(BootstrapJVMCI,   UseJVMCICompiler)
 77   CHECK_NOT_SET(PrintBootstrap,   UseJVMCICompiler)
 78   CHECK_NOT_SET(JVMCIThreads,     UseJVMCICompiler)
 79   CHECK_NOT_SET(JVMCIHostThreads, UseJVMCICompiler)
 80 
 81   if (UseJVMCICompiler) {
 82     if (FLAG_IS_DEFAULT(UseJVMCINativeLibrary) && !UseJVMCINativeLibrary) {
 83       char path[JVM_MAXPATHLEN];
 84       if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), JVMCI_SHARED_LIBRARY_NAME)) {
 85         // If a JVMCI native library is present,
 86         // we enable UseJVMCINativeLibrary by default.
 87         FLAG_SET_DEFAULT(UseJVMCINativeLibrary, true);
 88       }
 89     }
 90     if (!FLAG_IS_DEFAULT(EnableJVMCI) && !EnableJVMCI) {
 91       jio_fprintf(defaultStream::error_stream(),
 92           "Improperly specified VM option UseJVMCICompiler: EnableJVMCI cannot be disabled\n");
 93       return false;
 94     }
 95     FLAG_SET_DEFAULT(EnableJVMCI, true);
 96     if (BootstrapJVMCI && UseJVMCINativeLibrary) {
 97       jio_fprintf(defaultStream::error_stream(), "-XX:+BootstrapJVMCI is not compatible with -XX:+UseJVMCINativeLibrary\n");
 98       return false;
 99     }
100     if (BootstrapJVMCI && (TieredStopAtLevel < CompLevel_full_optimization)) {
101       jio_fprintf(defaultStream::error_stream(),
102           "-XX:+BootstrapJVMCI is not compatible with -XX:TieredStopAtLevel=%d\n", TieredStopAtLevel);
103       return false;
104     }
105   }
106 
107   if (!EnableJVMCI) {
108     // Switch off eager JVMCI initialization if JVMCI is disabled.
109     // Don't throw error if EagerJVMCI is set to allow testing.
110     if (EagerJVMCI) {
111       FLAG_SET_DEFAULT(EagerJVMCI, false);
112     }
113   }
114   JVMCI_FLAG_CHECKED(EagerJVMCI)
115 
116   CHECK_NOT_SET(JVMCIEventLogLevel,                  EnableJVMCI)
117   CHECK_NOT_SET(JVMCITraceLevel,                     EnableJVMCI)
118   CHECK_NOT_SET(JVMCICounterSize,                    EnableJVMCI)
119   CHECK_NOT_SET(JVMCICountersExcludeCompiler,        EnableJVMCI)
120   CHECK_NOT_SET(JVMCINMethodSizeLimit,               EnableJVMCI)
121   CHECK_NOT_SET(JVMCIPrintProperties,                EnableJVMCI)
122   CHECK_NOT_SET(JVMCIThreadsPerNativeLibraryRuntime, EnableJVMCI)
123   CHECK_NOT_SET(JVMCICompilerIdleDelay,              EnableJVMCI)
124   CHECK_NOT_SET(UseJVMCINativeLibrary,               EnableJVMCI)
125   CHECK_NOT_SET(JVMCINativeLibraryThreadFraction,    EnableJVMCI)
126   CHECK_NOT_SET(JVMCILibPath,                        EnableJVMCI)
127   CHECK_NOT_SET(JVMCINativeLibraryErrorFile,         EnableJVMCI)
128   CHECK_NOT_SET(JVMCILibDumpJNIConfig,               EnableJVMCI)
129 
130 #ifndef COMPILER2
131   JVMCI_FLAG_CHECKED(MaxVectorSize)
132   JVMCI_FLAG_CHECKED(ReduceInitialCardMarks)
133   JVMCI_FLAG_CHECKED(UseMultiplyToLenIntrinsic)
134   JVMCI_FLAG_CHECKED(UseSquareToLenIntrinsic)
135   JVMCI_FLAG_CHECKED(UseMulAddIntrinsic)
136   JVMCI_FLAG_CHECKED(UseMontgomeryMultiplyIntrinsic)
137   JVMCI_FLAG_CHECKED(UseMontgomerySquareIntrinsic)
138 #endif // !COMPILER2
139 
140 #ifndef PRODUCT
141 #define JVMCI_CHECK4(type, name, value, ...) assert(name##checked, #name " flag not checked");
142 #define JVMCI_CHECK3(type, name, ...)        assert(name##checked, #name " flag not checked");
143   // Ensures that all JVMCI flags are checked by this method.
144   APPLY_JVMCI_FLAGS(JVMCI_CHECK3, JVMCI_CHECK4)
145 #undef APPLY_JVMCI_FLAGS
146 #undef JVMCI_DECLARE_CHECK3
147 #undef JVMCI_DECLARE_CHECK4
148 #undef JVMCI_CHECK3
149 #undef JVMCI_CHECK4
150 #undef JVMCI_FLAG_CHECKED
151 #endif // PRODUCT
152 #undef CHECK_NOT_SET
153 
154   if (JVMCILibDumpJNIConfig != nullptr) {
155     _jni_config_file = new(mtJVMCI) fileStream(JVMCILibDumpJNIConfig);
156     if (_jni_config_file == nullptr || !_jni_config_file->is_open()) {
157       jio_fprintf(defaultStream::error_stream(),
158           "Could not open file for dumping JVMCI shared library JNI config: %s\n", JVMCILibDumpJNIConfig);
159       return false;
160     }
161   }
162 
163   return true;
164 }
165 
166 // Convert JVMCI flags from experimental to product
167 bool JVMCIGlobals::enable_jvmci_product_mode(JVMFlagOrigin origin, bool use_graal_jit) {
168   const char *JVMCIFlags[] = {
169     "EnableJVMCI",
170     "EnableJVMCIProduct",
171     "UseJVMCICompiler",
172     "JVMCIThreadsPerNativeLibraryRuntime",
173     "JVMCICompilerIdleDelay",
174     "JVMCIPrintProperties",
175     "EagerJVMCI",
176     "JVMCIThreads",
177     "JVMCICounterSize",
178     "JVMCICountersExcludeCompiler",
179     "JVMCINMethodSizeLimit",
180     "JVMCIEventLogLevel",
181     "JVMCITraceLevel",
182     "JVMCILibPath",
183     "JVMCILibDumpJNIConfig",
184     "UseJVMCINativeLibrary",
185     "JVMCINativeLibraryThreadFraction",
186     "JVMCINativeLibraryErrorFile",
187     nullptr
188   };
189 
190   for (int i = 0; JVMCIFlags[i] != nullptr; i++) {
191     JVMFlag *jvmciFlag = (JVMFlag *)JVMFlag::find_declared_flag(JVMCIFlags[i]);
192     if (jvmciFlag == nullptr) {
193       return false;
194     }
195     jvmciFlag->clear_experimental();
196     jvmciFlag->set_product();
197   }
198 
199   bool value = true;
200   JVMFlag *jvmciEnableFlag = JVMFlag::find_flag("EnableJVMCIProduct");
201   if (JVMFlagAccess::set_bool(jvmciEnableFlag, &value, origin) != JVMFlag::SUCCESS) {
202     return false;
203   }
204   if (use_graal_jit) {
205     JVMFlag *useGraalJITFlag = JVMFlag::find_flag("UseGraalJIT");
206     if (JVMFlagAccess::set_bool(useGraalJITFlag, &value, origin) != JVMFlag::SUCCESS) {
207       return false;
208     }
209   }
210 
211   // Effect of EnableJVMCIProduct on changing defaults of EnableJVMCI
212   // and UseJVMCICompiler is deferred to check_jvmci_flags_are_consistent
213   // so that setting these flags explicitly (e.g. on the command line)
214   // takes precedence.
215 
216   return true;
217 }
218 
219 bool JVMCIGlobals::gc_supports_jvmci() {
220   return UseSerialGC || UseParallelGC || UseG1GC || (UseZGC && !ZGenerational);
221 }
222 
223 void JVMCIGlobals::check_jvmci_supported_gc() {
224   if (EnableJVMCI) {
225     // Check if selected GC is supported by JVMCI and Java compiler
226     if (!gc_supports_jvmci()) {
227       log_warning(gc, jvmci)("Setting EnableJVMCI to false as selected GC does not support JVMCI: %s", GCConfig::hs_err_name());
228       FLAG_SET_DEFAULT(EnableJVMCI, false);
229       FLAG_SET_DEFAULT(UseJVMCICompiler, false);
230     }
231   }
232 }