Path: blob/master/src/hotspot/share/jvmci/jvmci.cpp
40949 views
/*1* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "classfile/systemDictionary.hpp"25#include "compiler/compileTask.hpp"26#include "compiler/compilerThread.hpp"27#include "gc/shared/collectedHeap.hpp"28#include "jvmci/jvmci.hpp"29#include "jvmci/jvmciJavaClasses.hpp"30#include "jvmci/jvmciEnv.hpp"31#include "jvmci/jvmciRuntime.hpp"32#include "jvmci/metadataHandles.hpp"33#include "memory/resourceArea.hpp"34#include "memory/universe.hpp"35#include "runtime/arguments.hpp"36#include "utilities/events.hpp"3738JVMCIRuntime* JVMCI::_compiler_runtime = NULL;39JVMCIRuntime* JVMCI::_java_runtime = NULL;40volatile bool JVMCI::_is_initialized = false;41bool JVMCI::_box_caches_initialized = false;42void* JVMCI::_shared_library_handle = NULL;43char* JVMCI::_shared_library_path = NULL;44volatile bool JVMCI::_in_shutdown = false;45StringEventLog* JVMCI::_events = NULL;46StringEventLog* JVMCI::_verbose_events = NULL;4748void jvmci_vmStructs_init() NOT_DEBUG_RETURN;4950bool JVMCI::can_initialize_JVMCI() {51// Initializing JVMCI requires the module system to be initialized past phase 3.52// The JVMCI API itself isn't available until phase 2 and ServiceLoader (which53// JVMCI initialization requires) isn't usable until after phase 3. Testing54// whether the system loader is initialized satisfies all these invariants.55if (SystemDictionary::java_system_loader() == NULL) {56return false;57}58assert(Universe::is_module_initialized(), "must be");59return true;60}6162void* JVMCI::get_shared_library(char*& path, bool load) {63void* sl_handle = _shared_library_handle;64if (sl_handle != NULL || !load) {65path = _shared_library_path;66return sl_handle;67}68assert(JVMCI_lock->owner() == Thread::current(), "must be");69path = NULL;70if (_shared_library_handle == NULL) {71char path[JVM_MAXPATHLEN];72char ebuf[1024];73if (JVMCILibPath != NULL) {74if (!os::dll_locate_lib(path, sizeof(path), JVMCILibPath, JVMCI_SHARED_LIBRARY_NAME)) {75fatal("Unable to create path to JVMCI shared library based on value of JVMCILibPath (%s)", JVMCILibPath);76}77} else {78if (!os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), JVMCI_SHARED_LIBRARY_NAME)) {79fatal("Unable to create path to JVMCI shared library");80}81}8283void* handle = os::dll_load(path, ebuf, sizeof ebuf);84if (handle == NULL) {85fatal("Unable to load JVMCI shared library from %s: %s", path, ebuf);86}87_shared_library_handle = handle;88_shared_library_path = strdup(path);8990JVMCI_event_1("loaded JVMCI shared library from %s", path);91}92path = _shared_library_path;93return _shared_library_handle;94}9596void JVMCI::initialize_compiler(TRAPS) {97if (JVMCILibDumpJNIConfig) {98JNIJVMCI::initialize_ids(NULL);99ShouldNotReachHere();100}101102JVMCI::compiler_runtime()->call_getCompiler(CHECK);103}104105void JVMCI::initialize_globals() {106jvmci_vmStructs_init();107if (LogEvents) {108if (JVMCIEventLogLevel > 0) {109_events = new StringEventLog("JVMCI Events", "jvmci");110if (JVMCIEventLogLevel > 1) {111int count = LogEventsBufferEntries;112for (int i = 1; i < JVMCIEventLogLevel && i < max_EventLog_level; i++) {113// Expand event buffer by 10x for each level above 1114count = count * 10;115}116_verbose_events = new StringEventLog("Verbose JVMCI Events", "verbose-jvmci", count);117}118}119}120if (UseJVMCINativeLibrary) {121// There are two runtimes.122_compiler_runtime = new JVMCIRuntime(0);123_java_runtime = new JVMCIRuntime(-1);124} else {125// There is only a single runtime126_java_runtime = _compiler_runtime = new JVMCIRuntime(0);127}128}129130void JVMCI::ensure_box_caches_initialized(TRAPS) {131if (_box_caches_initialized) {132return;133}134135// While multiple threads may reach here, that's fine136// since class initialization is synchronized.137Symbol* box_classes[] = {138java_lang_Boolean::symbol(),139java_lang_Byte_ByteCache::symbol(),140java_lang_Short_ShortCache::symbol(),141java_lang_Character_CharacterCache::symbol(),142java_lang_Integer_IntegerCache::symbol(),143java_lang_Long_LongCache::symbol()144};145146for (unsigned i = 0; i < sizeof(box_classes) / sizeof(Symbol*); i++) {147Klass* k = SystemDictionary::resolve_or_fail(box_classes[i], true, CHECK);148InstanceKlass* ik = InstanceKlass::cast(k);149if (ik->is_not_initialized()) {150ik->initialize(CHECK);151}152}153_box_caches_initialized = true;154}155156JavaThread* JVMCI::compilation_tick(JavaThread* thread) {157if (thread->is_Compiler_thread()) {158CompileTask *task = CompilerThread::cast(thread)->task();159if (task != NULL) {160JVMCICompileState *state = task->blocking_jvmci_compile_state();161if (state != NULL) {162state->inc_compilation_ticks();163}164}165}166return thread;167}168169void JVMCI::metadata_do(void f(Metadata*)) {170if (_java_runtime != NULL) {171_java_runtime->_metadata_handles->metadata_do(f);172}173if (_compiler_runtime != NULL && _compiler_runtime != _java_runtime) {174_compiler_runtime->_metadata_handles->metadata_do(f);175}176}177178void JVMCI::do_unloading(bool unloading_occurred) {179if (unloading_occurred) {180if (_java_runtime != NULL) {181_java_runtime->_metadata_handles->do_unloading();182}183if (_compiler_runtime != NULL && _compiler_runtime != _java_runtime) {184_compiler_runtime->_metadata_handles->do_unloading();185}186}187}188189bool JVMCI::is_compiler_initialized() {190return _is_initialized;191}192193void JVMCI::shutdown() {194ResourceMark rm;195{196MutexLocker locker(JVMCI_lock);197_in_shutdown = true;198JVMCI_event_1("shutting down JVMCI");199}200JVMCIRuntime* java_runtime = _java_runtime;201if (java_runtime != compiler_runtime()) {202java_runtime->shutdown();203}204if (compiler_runtime() != NULL) {205compiler_runtime()->shutdown();206}207}208209bool JVMCI::in_shutdown() {210return _in_shutdown;211}212213void JVMCI::vlog(int level, const char* format, va_list ap) {214if (LogEvents && JVMCIEventLogLevel >= level) {215StringEventLog* events = level == 1 ? _events : _verbose_events;216guarantee(events != NULL, "JVMCI event log not yet initialized");217Thread* thread = Thread::current_or_null_safe();218if (thread != NULL) {219events->logv(thread, format, ap);220}221}222}223224void JVMCI::vtrace(int level, const char* format, va_list ap) {225if (JVMCITraceLevel >= level) {226Thread* thread = Thread::current_or_null_safe();227if (thread != NULL) {228ResourceMark rm;229tty->print("JVMCITrace-%d[%s]:%*c", level, thread->name(), level, ' ');230} else {231tty->print("JVMCITrace-%d[?]:%*c", level, level, ' ');232}233tty->vprint_cr(format, ap);234}235}236237#define LOG_TRACE(level) { va_list ap; \238va_start(ap, format); vlog(level, format, ap); va_end(ap); \239va_start(ap, format); vtrace(level, format, ap); va_end(ap); \240}241242void JVMCI::event(int level, const char* format, ...) LOG_TRACE(level)243void JVMCI::event1(const char* format, ...) LOG_TRACE(1)244void JVMCI::event2(const char* format, ...) LOG_TRACE(2)245void JVMCI::event3(const char* format, ...) LOG_TRACE(3)246void JVMCI::event4(const char* format, ...) LOG_TRACE(4)247248#undef LOG_TRACE249250251