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   }
105 
106   if (!EnableJVMCI) {
107     // Switch off eager JVMCI initialization if JVMCI is disabled.
108     // Don't throw error if EagerJVMCI is set to allow testing.
109     if (EagerJVMCI) {
110       FLAG_SET_DEFAULT(EagerJVMCI, false);
111     }
112   }
113   JVMCI_FLAG_CHECKED(EagerJVMCI)
114 
115   CHECK_NOT_SET(JVMCIEventLogLevel,           EnableJVMCI)
116   CHECK_NOT_SET(JVMCITraceLevel,              EnableJVMCI)
117   CHECK_NOT_SET(JVMCICounterSize,             EnableJVMCI)
118   CHECK_NOT_SET(JVMCICountersExcludeCompiler, EnableJVMCI)
119   CHECK_NOT_SET(JVMCIUseFastLocking,          EnableJVMCI)
120   CHECK_NOT_SET(JVMCINMethodSizeLimit,        EnableJVMCI)
121   CHECK_NOT_SET(JVMCIPrintProperties,         EnableJVMCI)
122   CHECK_NOT_SET(UseJVMCINativeLibrary,        EnableJVMCI)
123   CHECK_NOT_SET(JVMCILibPath,                 EnableJVMCI)
124   CHECK_NOT_SET(JVMCILibDumpJNIConfig,        EnableJVMCI)
125 
126 #ifndef COMPILER2
127   JVMCI_FLAG_CHECKED(MaxVectorSize)
128   JVMCI_FLAG_CHECKED(ReduceInitialCardMarks)
129   JVMCI_FLAG_CHECKED(UseMultiplyToLenIntrinsic)
130   JVMCI_FLAG_CHECKED(UseSquareToLenIntrinsic)
131   JVMCI_FLAG_CHECKED(UseMulAddIntrinsic)
132   JVMCI_FLAG_CHECKED(UseMontgomeryMultiplyIntrinsic)
133   JVMCI_FLAG_CHECKED(UseMontgomerySquareIntrinsic)
134 #endif // !COMPILER2
135 
136 #ifndef PRODUCT
137 #define JVMCI_CHECK4(type, name, value, ...) assert(name##checked, #name " flag not checked");
138 #define JVMCI_CHECK3(type, name, ...)        assert(name##checked, #name " flag not checked");
139   // Ensures that all JVMCI flags are checked by this method.
140   APPLY_JVMCI_FLAGS(JVMCI_CHECK3, JVMCI_CHECK4)
141 #undef APPLY_JVMCI_FLAGS
142 #undef JVMCI_DECLARE_CHECK3
143 #undef JVMCI_DECLARE_CHECK4
144 #undef JVMCI_CHECK3
145 #undef JVMCI_CHECK4
146 #undef JVMCI_FLAG_CHECKED
147 #endif // PRODUCT
148 #undef CHECK_NOT_SET
149 
150   if (JVMCILibDumpJNIConfig != NULL) {
151     _jni_config_file = new(ResourceObj::C_HEAP, mtJVMCI) fileStream(JVMCILibDumpJNIConfig);
152     if (_jni_config_file == NULL || !_jni_config_file->is_open()) {
153       jio_fprintf(defaultStream::error_stream(),
154           "Could not open file for dumping JVMCI shared library JNI config: %s\n", JVMCILibDumpJNIConfig);
155       return false;
156     }
157   }
158 
159   return true;
160 }
161 
162 // Convert JVMCI flags from experimental to product
163 bool JVMCIGlobals::enable_jvmci_product_mode(JVMFlagOrigin origin) {
164   const char *JVMCIFlags[] = {
165     "EnableJVMCI",
166     "EnableJVMCIProduct",
167     "UseJVMCICompiler",
168     "JVMCIPrintProperties",
169     "EagerJVMCI",
170     "JVMCIThreads",
171     "JVMCICounterSize",
172     "JVMCICountersExcludeCompiler",
173     "JVMCINMethodSizeLimit",
174     "JVMCIEventLogLevel",
175     "JVMCITraceLevel",
176     "JVMCILibPath",
177     "JVMCILibDumpJNIConfig",
178     "UseJVMCINativeLibrary",
179     NULL
180   };
181 
182   for (int i = 0; JVMCIFlags[i] != NULL; i++) {
183     JVMFlag *jvmciFlag = (JVMFlag *)JVMFlag::find_declared_flag(JVMCIFlags[i]);
184     if (jvmciFlag == NULL) {
185       return false;
186     }
187     jvmciFlag->clear_experimental();
188     jvmciFlag->set_product();
189   }
190 
191   bool value = true;
192   JVMFlag *jvmciEnableFlag = JVMFlag::find_flag("EnableJVMCIProduct");
193   if (JVMFlagAccess::set_bool(jvmciEnableFlag, &value, origin) != JVMFlag::SUCCESS) {
194     return false;
195   }
196 
197   // Effect of EnableJVMCIProduct on changing defaults of EnableJVMCI
198   // and UseJVMCICompiler is deferred to check_jvmci_flags_are_consistent
199   // so that setting these flags explicitly (e.g. on the command line)
200   // takes precedence.
201 
202   return true;
203 }
204 
205 bool JVMCIGlobals::gc_supports_jvmci() {
206   return UseSerialGC || UseParallelGC || UseG1GC;
207 }
208 
209 void JVMCIGlobals::check_jvmci_supported_gc() {
210   if (EnableJVMCI) {
211     // Check if selected GC is supported by JVMCI and Java compiler
212     if (!gc_supports_jvmci()) {
213       log_warning(gc, jvmci)("Setting EnableJVMCI to false as selected GC does not support JVMCI: %s", GCConfig::hs_err_name());
214       FLAG_SET_DEFAULT(EnableJVMCI, false);
215       FLAG_SET_DEFAULT(UseJVMCICompiler, false);
216     }
217   }
218 }