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(JVMCIUseFastLocking,          EnableJVMCI)
124   CHECK_NOT_SET(JVMCINMethodSizeLimit,        EnableJVMCI)
125   CHECK_NOT_SET(JVMCIPrintProperties,         EnableJVMCI)
126   CHECK_NOT_SET(UseJVMCINativeLibrary,        EnableJVMCI)
127   CHECK_NOT_SET(JVMCILibPath,                 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 != NULL) {
155     _jni_config_file = new(ResourceObj::C_HEAP, mtJVMCI) fileStream(JVMCILibDumpJNIConfig);
156     if (_jni_config_file == NULL || !_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) {
168   const char *JVMCIFlags[] = {
169     "EnableJVMCI",
170     "EnableJVMCIProduct",
171     "UseJVMCICompiler",
172     "JVMCIPrintProperties",
173     "EagerJVMCI",
174     "JVMCIThreads",
175     "JVMCICounterSize",
176     "JVMCICountersExcludeCompiler",
177     "JVMCINMethodSizeLimit",
178     "JVMCIEventLogLevel",
179     "JVMCITraceLevel",
180     "JVMCILibPath",
181     "JVMCILibDumpJNIConfig",
182     "UseJVMCINativeLibrary",
183     NULL
184   };
185 
186   for (int i = 0; JVMCIFlags[i] != NULL; i++) {
187     JVMFlag *jvmciFlag = (JVMFlag *)JVMFlag::find_declared_flag(JVMCIFlags[i]);
188     if (jvmciFlag == NULL) {
189       return false;
190     }
191     jvmciFlag->clear_experimental();
192     jvmciFlag->set_product();
193   }
194 
195   bool value = true;
196   JVMFlag *jvmciEnableFlag = JVMFlag::find_flag("EnableJVMCIProduct");
197   if (JVMFlagAccess::set_bool(jvmciEnableFlag, &value, origin) != JVMFlag::SUCCESS) {
198     return false;
199   }
200 
201   // Effect of EnableJVMCIProduct on changing defaults of EnableJVMCI
202   // and UseJVMCICompiler is deferred to check_jvmci_flags_are_consistent
203   // so that setting these flags explicitly (e.g. on the command line)
204   // takes precedence.
205 
206   return true;
207 }
208 
209 bool JVMCIGlobals::gc_supports_jvmci() {
210   return UseSerialGC || UseParallelGC || UseG1GC;
211 }
212 
213 void JVMCIGlobals::check_jvmci_supported_gc() {
214   if (EnableJVMCI) {
215     // Check if selected GC is supported by JVMCI and Java compiler
216     if (!gc_supports_jvmci()) {
217       log_warning(gc, jvmci)("Setting EnableJVMCI to false as selected GC does not support JVMCI: %s", GCConfig::hs_err_name());
218       FLAG_SET_DEFAULT(EnableJVMCI, false);
219       FLAG_SET_DEFAULT(UseJVMCICompiler, false);
220     }
221   }
222 }