/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ #include "precompiled.hpp" #include "aot/aotCodeHeap.hpp" #include "aot/aotLoader.inline.hpp" #include "jvmci/jvmciRuntime.hpp" #include "oops/method.hpp" GrowableArray* AOTLoader::_heaps = new(ResourceObj::C_HEAP, mtCode) GrowableArray (2, true); // Iterate over all AOT CodeHeaps #define FOR_ALL_AOT_HEAPS(heap) for (GrowableArrayIterator heap = heaps()->begin(); heap != heaps()->end(); ++heap) void AOTLoader::load_for_klass(const instanceKlassHandle& kh, Thread* thread) { if (UseAOT) { FOR_ALL_AOT_HEAPS(heap) { (*heap)->load_klass_data(kh, thread); } } } uint64_t AOTLoader::get_saved_fingerprint(InstanceKlass* ik) { FOR_ALL_AOT_HEAPS(heap) { AOTKlassData* klass_data = (*heap)->find_klass(ik); if (klass_data != NULL) { return klass_data->_fingerprint; } } return 0; } bool AOTLoader::find_klass(InstanceKlass* ik) { FOR_ALL_AOT_HEAPS(heap) { if ((*heap)->find_klass(ik) != NULL) { return true; } } return false; } bool AOTLoader::contains(address p) { FOR_ALL_AOT_HEAPS(heap) { if ((*heap)->contains(p)) { return true; } } return false; } void AOTLoader::oops_do(OopClosure* f) { if (UseAOT) { FOR_ALL_AOT_HEAPS(heap) { (*heap)->oops_do(f); } } } void AOTLoader::metadata_do(void f(Metadata*)) { if (UseAOT) { FOR_ALL_AOT_HEAPS(heap) { (*heap)->metadata_do(f); } } } address AOTLoader::exception_begin(JavaThread* thread, CodeBlob* blob, address return_address) { assert(blob->is_aot(), "sanity"); AOTCompiledMethod* aotm = (AOTCompiledMethod*)blob; // Set flag if return address is a method handle call site. thread->set_is_method_handle_return(aotm->is_method_handle_return(return_address)); return aotm->exception_begin(); } // Flushing and deoptimization in case of evolution void AOTLoader::flush_evol_dependents_on(const instanceKlassHandle& dependee) { // make non entrant and mark for deoptimization FOR_ALL_AOT_HEAPS(heap) { (*heap)->flush_evol_dependents_on(dependee); } Deoptimization::deoptimize_dependents(); } /** * List of core modules for which we search for shared libraries. */ static const char* modules[] = { "java.base", "java.logging", "jdk.compiler", "jdk.scripting.nashorn", "jdk.vm.ci", "jdk.vm.compiler" }; void AOTLoader::initialize() { // Probe if we have libraries for core modules and load them if available. bool libraries_found = false; if (FLAG_IS_DEFAULT(UseAOT) && AOTLibrary != NULL) { // Don't need to set UseAOT on command line when AOTLibrary is specified FLAG_SET_DEFAULT(UseAOT, true); } if (UseAOT) { // EagerInitialization is not compatible with AOT if (EagerInitialization) { warning("EagerInitialization is not compatible with AOT (switching AOT off)"); FLAG_SET_DEFAULT(UseAOT, false); return; } // -Xint is not compatible with AOT if (Arguments::is_interpreter_only()) { warning("-Xint is not compatible with AOT (switching AOT off)"); FLAG_SET_DEFAULT(UseAOT, false); return; } const char* home = Arguments::get_java_home(); const char* file_separator = os::file_separator(); for (int i = 0; i < (int) (sizeof(modules) / sizeof(const char*)); i++) { char library[JVM_MAXPATHLEN]; jio_snprintf(library, sizeof(library), "%s%slib%slib%s%s%s.so", home, file_separator, file_separator, modules[i], UseCompressedOops ? "-coop" : "", UseG1GC ? "" : "-nong1"); bool found = load_library(library, false); if (found) { libraries_found = true; } } // Scan the AOTLibrary option. if (AOTLibrary != NULL) { const int len = strlen(AOTLibrary); char* cp = NEW_C_HEAP_ARRAY(char, len+1, mtCode); memcpy(cp, AOTLibrary, len); cp[len] = '\0'; char* end = cp + len; while (cp < end) { const char* name = cp; while ((*cp) != '\0' && (*cp) != '\n' && (*cp) != ',' && (*cp) != ':' && (*cp) != ';') cp++; cp[0] = '\0'; // Terminate name cp++; bool found = load_library(name, true); if (found) { libraries_found = true; } } } // If we didn't find a library we are turning AOT off, if not requested otherwise. if (!libraries_found) { if (FLAG_IS_DEFAULT(UseAOT)) { FLAG_SET_DEFAULT(UseAOT, false); } } } } void AOTLoader::universe_init() { if (UseAOT && heaps_count() > 0) { // Shifts are static values which initialized by 0 until java heap initialization. // AOT libs are loaded before heap initialized so shift values are not set. // It is okay since ObjectAlignmentInBytes flag which defines shifts value is set before AOT libs are loaded. // Set shifts value based on first AOT library config. if (UseCompressedOops && AOTCodeHeap::narrow_oop_shift_initialized()) { int oop_shift = Universe::narrow_oop_shift(); if (oop_shift == 0) { Universe::set_narrow_oop_shift(AOTCodeHeap::narrow_oop_shift()); } else { FOR_ALL_AOT_HEAPS(heap) { (*heap)->verify_flag(AOTCodeHeap::narrow_oop_shift(), oop_shift, "Universe::narrow_oop_shift"); } } if (UseCompressedClassPointers) { // It is set only if UseCompressedOops is set int klass_shift = Universe::narrow_klass_shift(); if (klass_shift == 0) { Universe::set_narrow_klass_shift(AOTCodeHeap::narrow_klass_shift()); } else { FOR_ALL_AOT_HEAPS(heap) { (*heap)->verify_flag(AOTCodeHeap::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift"); } } } } } } void AOTLoader::set_narrow_klass_shift() { // This method could be called from Metaspace::set_narrow_klass_base_and_shift(). // In case it is not called (during dump CDS, for example) the corresponding code in // AOTLoader::universe_init(), which is called later, will set the shift value. if (UseAOT && heaps_count() > 0 && UseCompressedOops && AOTCodeHeap::narrow_oop_shift_initialized() && UseCompressedClassPointers) { int klass_shift = Universe::narrow_klass_shift(); if (klass_shift == 0) { Universe::set_narrow_klass_shift(AOTCodeHeap::narrow_klass_shift()); } else { FOR_ALL_AOT_HEAPS(heap) { (*heap)->verify_flag(AOTCodeHeap::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift"); } } } } bool AOTLoader::load_library(const char* name, bool exit_on_error) { void* handle = dlopen(name, RTLD_LAZY); if (handle == NULL) { if (exit_on_error) { tty->print_cr("error opening file: %s", dlerror()); vm_exit(1); } return false; } const int dso_id = heaps_count() + 1; AOTCodeHeap* heap = new AOTCodeHeap(handle, name, dso_id); if (!heap->is_valid()) { delete heap; dlclose(handle); return false; } { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); add_heap(heap); CodeCache::add_heap(heap); } return true; } #ifndef PRODUCT void AOTLoader::print_statistics() { { ttyLocker ttyl; tty->print_cr("--- AOT Statistics ---"); tty->print_cr("AOT libraries loaded: %d", heaps_count()); AOTCodeHeap::print_statistics(); } } #endif