1 /* 2 * Copyright (c) 2016, 2017, 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 #include "precompiled.hpp" 25 26 #include "aot/aotCodeHeap.hpp" 27 #include "aot/aotLoader.inline.hpp" 28 #include "jvmci/jvmciRuntime.hpp" 29 #include "oops/method.hpp" 30 #include "runtime/os.hpp" 31 32 GrowableArray<AOTCodeHeap*>* AOTLoader::_heaps = new(ResourceObj::C_HEAP, mtCode) GrowableArray<AOTCodeHeap*> (2, true); 33 GrowableArray<AOTLib*>* AOTLoader::_libraries = new(ResourceObj::C_HEAP, mtCode) GrowableArray<AOTLib*> (2, true); 34 35 // Iterate over all AOT CodeHeaps 36 #define FOR_ALL_AOT_HEAPS(heap) for (GrowableArrayIterator<AOTCodeHeap*> heap = heaps()->begin(); heap != heaps()->end(); ++heap) 37 // Iterate over all AOT Libraries 38 #define FOR_ALL_AOT_LIBRARIES(lib) for (GrowableArrayIterator<AOTLib*> lib = libraries()->begin(); lib != libraries()->end(); ++lib) 39 40 void AOTLoader::load_for_klass(instanceKlassHandle kh, Thread* thread) { 41 if (UseAOT) { 42 FOR_ALL_AOT_HEAPS(heap) { 43 (*heap)->load_klass_data(kh, thread); 44 } 45 } 46 } 47 48 uint64_t AOTLoader::get_saved_fingerprint(InstanceKlass* ik) { 49 FOR_ALL_AOT_HEAPS(heap) { 50 AOTKlassData* klass_data = (*heap)->find_klass(ik); 51 if (klass_data != NULL) { 52 return klass_data->_fingerprint; 53 } 54 } 55 return 0; 56 } 57 58 bool AOTLoader::find_klass(InstanceKlass* ik) { 59 FOR_ALL_AOT_HEAPS(heap) { 60 if ((*heap)->find_klass(ik) != NULL) { 61 return true; 62 } 63 } 64 return false; 65 } 66 67 bool AOTLoader::contains(address p) { 68 FOR_ALL_AOT_HEAPS(heap) { 69 if ((*heap)->contains(p)) { 70 return true; 71 } 72 } 73 return false; 74 } 75 76 void AOTLoader::oops_do(OopClosure* f) { 77 if (UseAOT) { 78 FOR_ALL_AOT_HEAPS(heap) { 79 (*heap)->oops_do(f); 80 } 81 } 82 } 83 84 void AOTLoader::metadata_do(void f(Metadata*)) { 85 if (UseAOT) { 86 FOR_ALL_AOT_HEAPS(heap) { 87 (*heap)->metadata_do(f); 88 } 89 } 90 } 91 92 address AOTLoader::exception_begin(JavaThread* thread, CodeBlob* blob, address return_address) { 93 assert(blob->is_aot(), "sanity"); 94 AOTCompiledMethod* aotm = (AOTCompiledMethod*)blob; 95 // Set flag if return address is a method handle call site. 96 thread->set_is_method_handle_return(aotm->is_method_handle_return(return_address)); 97 return aotm->exception_begin(); 98 } 99 100 // Flushing and deoptimization in case of evolution 101 void AOTLoader::flush_evol_dependents_on(instanceKlassHandle dependee) { 102 // make non entrant and mark for deoptimization 103 FOR_ALL_AOT_HEAPS(heap) { 104 (*heap)->flush_evol_dependents_on(dependee); 105 } 106 Deoptimization::deoptimize_dependents(); 107 } 108 109 /** 110 * List of core modules for which we search for shared libraries. 111 */ 112 static const char* modules[] = { 113 "java.base", 114 "java.logging", 115 "jdk.compiler", 116 "jdk.scripting.nashorn", 117 "jdk.vm.ci", 118 "jdk.vm.compiler" 119 }; 120 121 void AOTLoader::initialize() { 122 if (FLAG_IS_DEFAULT(UseAOT) && AOTLibrary != NULL) { 123 // Don't need to set UseAOT on command line when AOTLibrary is specified 124 FLAG_SET_DEFAULT(UseAOT, true); 125 } 126 if (UseAOT) { 127 // EagerInitialization is not compatible with AOT 128 if (EagerInitialization) { 129 if (PrintAOT) { 130 warning("EagerInitialization is not compatible with AOT (switching AOT off)"); 131 } 132 FLAG_SET_DEFAULT(UseAOT, false); 133 return; 134 } 135 136 // -Xint is not compatible with AOT 137 if (Arguments::is_interpreter_only()) { 138 if (PrintAOT) { 139 warning("-Xint is not compatible with AOT (switching AOT off)"); 140 } 141 FLAG_SET_DEFAULT(UseAOT, false); 142 return; 143 } 144 145 const char* home = Arguments::get_java_home(); 146 const char* file_separator = os::file_separator(); 147 148 for (int i = 0; i < (int) (sizeof(modules) / sizeof(const char*)); i++) { 149 char library[JVM_MAXPATHLEN]; 150 jio_snprintf(library, sizeof(library), "%s%slib%slib%s%s%s%s", home, file_separator, file_separator, modules[i], UseCompressedOops ? "-coop" : "", UseG1GC ? "" : "-nong1", os::dll_file_extension()); 151 load_library(library, false); 152 } 153 154 // Scan the AOTLibrary option. 155 if (AOTLibrary != NULL) { 156 const int len = (int)strlen(AOTLibrary); 157 char* cp = NEW_C_HEAP_ARRAY(char, len+1, mtCode); 158 if (cp != NULL) { // No memory? 159 memcpy(cp, AOTLibrary, len); 160 cp[len] = '\0'; 161 char* end = cp + len; 162 while (cp < end) { 163 const char* name = cp; 164 while ((*cp) != '\0' && (*cp) != '\n' && (*cp) != ',' && (*cp) != ':' && (*cp) != ';') cp++; 165 cp[0] = '\0'; // Terminate name 166 cp++; 167 load_library(name, true); 168 } 169 } 170 } 171 } 172 } 173 174 void AOTLoader::universe_init() { 175 if (UseAOT && libraries_count() > 0) { 176 // Shifts are static values which initialized by 0 until java heap initialization. 177 // AOT libs are loaded before heap initialized so shift values are not set. 178 // It is okay since ObjectAlignmentInBytes flag which defines shifts value is set before AOT libs are loaded. 179 // Set shifts value based on first AOT library config. 180 if (UseCompressedOops && AOTLib::narrow_oop_shift_initialized()) { 181 int oop_shift = Universe::narrow_oop_shift(); 182 if (oop_shift == 0) { 183 Universe::set_narrow_oop_shift(AOTLib::narrow_oop_shift()); 184 } else { 185 FOR_ALL_AOT_LIBRARIES(lib) { 186 (*lib)->verify_flag(AOTLib::narrow_oop_shift(), oop_shift, "Universe::narrow_oop_shift"); 187 } 188 } 189 if (UseCompressedClassPointers) { // It is set only if UseCompressedOops is set 190 int klass_shift = Universe::narrow_klass_shift(); 191 if (klass_shift == 0) { 192 Universe::set_narrow_klass_shift(AOTLib::narrow_klass_shift()); 193 } else { 194 FOR_ALL_AOT_LIBRARIES(lib) { 195 (*lib)->verify_flag(AOTLib::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift"); 196 } 197 } 198 } 199 } 200 // Create heaps for all the libraries 201 FOR_ALL_AOT_LIBRARIES(lib) { 202 if ((*lib)->is_valid()) { 203 AOTCodeHeap* heap = new AOTCodeHeap(*lib); 204 { 205 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 206 add_heap(heap); 207 CodeCache::add_heap(heap); 208 } 209 } 210 } 211 } 212 if (heaps_count() == 0) { 213 if (FLAG_IS_DEFAULT(UseAOT)) { 214 FLAG_SET_DEFAULT(UseAOT, false); 215 } 216 } 217 } 218 219 void AOTLoader::set_narrow_klass_shift() { 220 // This method could be called from Metaspace::set_narrow_klass_base_and_shift(). 221 // In case it is not called (during dump CDS, for example) the corresponding code in 222 // AOTLoader::universe_init(), which is called later, will set the shift value. 223 if (UseAOT && libraries_count() > 0 && 224 UseCompressedOops && AOTLib::narrow_oop_shift_initialized() && 225 UseCompressedClassPointers) { 226 int klass_shift = Universe::narrow_klass_shift(); 227 if (klass_shift == 0) { 228 Universe::set_narrow_klass_shift(AOTLib::narrow_klass_shift()); 229 } else { 230 FOR_ALL_AOT_LIBRARIES(lib) { 231 (*lib)->verify_flag(AOTLib::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift"); 232 } 233 } 234 } 235 } 236 237 void AOTLoader::load_library(const char* name, bool exit_on_error) { 238 char ebuf[1024]; 239 void* handle = os::dll_load(name, ebuf, sizeof ebuf); 240 if (handle == NULL) { 241 if (exit_on_error) { 242 tty->print_cr("error opening file: %s", ebuf); 243 vm_exit(1); 244 } 245 return; 246 } 247 const int dso_id = libraries_count() + 1; 248 AOTLib* lib = new AOTLib(handle, name, dso_id); 249 if (!lib->is_valid()) { 250 delete lib; 251 os::dll_unload(handle); 252 return; 253 } 254 add_library(lib); 255 } 256 257 #ifndef PRODUCT 258 void AOTLoader::print_statistics() { 259 { ttyLocker ttyl; 260 tty->print_cr("--- AOT Statistics ---"); 261 tty->print_cr("AOT libraries loaded: %d", heaps_count()); 262 AOTCodeHeap::print_statistics(); 263 } 264 } 265 #endif