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 #include "jvm.h" 26 27 #include "aot/aotCodeHeap.hpp" 28 #include "aot/aotLoader.inline.hpp" 29 #include "jvmci/jvmciRuntime.hpp" 30 #include "oops/method.hpp" 31 #include "runtime/os.hpp" 32 #include "runtime/timerTrace.hpp" 33 34 GrowableArray<AOTCodeHeap*>* AOTLoader::_heaps = new(ResourceObj::C_HEAP, mtCode) GrowableArray<AOTCodeHeap*> (2, true); 35 GrowableArray<AOTLib*>* AOTLoader::_libraries = new(ResourceObj::C_HEAP, mtCode) GrowableArray<AOTLib*> (2, true); 36 37 // Iterate over all AOT CodeHeaps 38 #define FOR_ALL_AOT_HEAPS(heap) for (GrowableArrayIterator<AOTCodeHeap*> heap = heaps()->begin(); heap != heaps()->end(); ++heap) 39 // Iterate over all AOT Libraries 40 #define FOR_ALL_AOT_LIBRARIES(lib) for (GrowableArrayIterator<AOTLib*> lib = libraries()->begin(); lib != libraries()->end(); ++lib) 41 42 void AOTLoader::load_for_klass(InstanceKlass* ik, Thread* thread) { 43 if (ik->is_anonymous()) { 44 // don't even bother 45 return; 46 } 47 if (UseAOT) { 48 FOR_ALL_AOT_HEAPS(heap) { 49 (*heap)->load_klass_data(ik, thread); 50 } 51 } 52 } 53 54 uint64_t AOTLoader::get_saved_fingerprint(InstanceKlass* ik) { 55 if (ik->is_anonymous()) { 56 // don't even bother 57 return 0; 58 } 59 FOR_ALL_AOT_HEAPS(heap) { 60 AOTKlassData* klass_data = (*heap)->find_klass(ik); 61 if (klass_data != NULL) { 62 return klass_data->_fingerprint; 63 } 64 } 65 return 0; 66 } 67 68 bool AOTLoader::find_klass(InstanceKlass* ik) { 69 FOR_ALL_AOT_HEAPS(heap) { 70 if ((*heap)->find_klass(ik) != NULL) { 71 return true; 72 } 73 } 74 return false; 75 } 76 77 bool AOTLoader::contains(address p) { 78 FOR_ALL_AOT_HEAPS(heap) { 79 if ((*heap)->contains(p)) { 80 return true; 81 } 82 } 83 return false; 84 } 85 86 void AOTLoader::oops_do(OopClosure* f) { 87 if (UseAOT) { 88 FOR_ALL_AOT_HEAPS(heap) { 89 (*heap)->oops_do(f); 90 } 91 } 92 } 93 94 void AOTLoader::metadata_do(void f(Metadata*)) { 95 if (UseAOT) { 96 FOR_ALL_AOT_HEAPS(heap) { 97 (*heap)->metadata_do(f); 98 } 99 } 100 } 101 102 // Flushing and deoptimization in case of evolution 103 void AOTLoader::flush_evol_dependents_on(InstanceKlass* dependee) { 104 // make non entrant and mark for deoptimization 105 FOR_ALL_AOT_HEAPS(heap) { 106 (*heap)->flush_evol_dependents_on(dependee); 107 } 108 Deoptimization::deoptimize_dependents(); 109 } 110 111 /** 112 * List of core modules for which we search for shared libraries. 113 */ 114 static const char* modules[] = { 115 "java.base", 116 "java.logging", 117 "jdk.compiler", 118 "jdk.scripting.nashorn", 119 "jdk.internal.vm.ci", 120 "jdk.internal.vm.compiler" 121 }; 122 123 void AOTLoader::initialize() { 124 TraceTime timer("AOT initialization", TRACETIME_LOG(Info, aot, startuptime)); 125 126 if (FLAG_IS_DEFAULT(UseAOT) && AOTLibrary != NULL) { 127 // Don't need to set UseAOT on command line when AOTLibrary is specified 128 FLAG_SET_DEFAULT(UseAOT, true); 129 } 130 if (UseAOT) { 131 // EagerInitialization is not compatible with AOT 132 if (EagerInitialization) { 133 if (PrintAOT) { 134 warning("EagerInitialization is not compatible with AOT (switching AOT off)"); 135 } 136 FLAG_SET_DEFAULT(UseAOT, false); 137 return; 138 } 139 140 // -Xint is not compatible with AOT 141 if (Arguments::is_interpreter_only()) { 142 if (PrintAOT) { 143 warning("-Xint is not compatible with AOT (switching AOT off)"); 144 } 145 FLAG_SET_DEFAULT(UseAOT, false); 146 return; 147 } 148 149 // Scan the AOTLibrary option. 150 if (AOTLibrary != NULL) { 151 const int len = (int)strlen(AOTLibrary); 152 char* cp = NEW_C_HEAP_ARRAY(char, len+1, mtCode); 153 if (cp != NULL) { // No memory? 154 memcpy(cp, AOTLibrary, len); 155 cp[len] = '\0'; 156 char* end = cp + len; 157 while (cp < end) { 158 const char* name = cp; 159 while ((*cp) != '\0' && (*cp) != '\n' && (*cp) != ',' && (*cp) != ':' && (*cp) != ';') cp++; 160 cp[0] = '\0'; // Terminate name 161 cp++; 162 load_library(name, true); 163 } 164 } 165 } 166 167 // Load well-know AOT libraries from Java installation directory. 168 const char* home = Arguments::get_java_home(); 169 const char* file_separator = os::file_separator(); 170 171 for (int i = 0; i < (int) (sizeof(modules) / sizeof(const char*)); i++) { 172 char library[JVM_MAXPATHLEN]; 173 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()); 174 load_library(library, false); 175 } 176 } 177 } 178 179 void AOTLoader::universe_init() { 180 if (UseAOT && libraries_count() > 0) { 181 // Shifts are static values which initialized by 0 until java heap initialization. 182 // AOT libs are loaded before heap initialized so shift values are not set. 183 // It is okay since ObjectAlignmentInBytes flag which defines shifts value is set before AOT libs are loaded. 184 // Set shifts value based on first AOT library config. 185 if (UseCompressedOops && AOTLib::narrow_oop_shift_initialized()) { 186 int oop_shift = Universe::narrow_oop_shift(); 187 if (oop_shift == 0) { 188 Universe::set_narrow_oop_shift(AOTLib::narrow_oop_shift()); 189 } else { 190 FOR_ALL_AOT_LIBRARIES(lib) { 191 (*lib)->verify_flag(AOTLib::narrow_oop_shift(), oop_shift, "Universe::narrow_oop_shift"); 192 } 193 } 194 if (UseCompressedClassPointers) { // It is set only if UseCompressedOops is set 195 int klass_shift = Universe::narrow_klass_shift(); 196 if (klass_shift == 0) { 197 Universe::set_narrow_klass_shift(AOTLib::narrow_klass_shift()); 198 } else { 199 FOR_ALL_AOT_LIBRARIES(lib) { 200 (*lib)->verify_flag(AOTLib::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift"); 201 } 202 } 203 } 204 } 205 // Create heaps for all the libraries 206 FOR_ALL_AOT_LIBRARIES(lib) { 207 if ((*lib)->is_valid()) { 208 AOTCodeHeap* heap = new AOTCodeHeap(*lib); 209 { 210 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 211 add_heap(heap); 212 CodeCache::add_heap(heap); 213 } 214 } 215 } 216 } 217 if (heaps_count() == 0) { 218 if (FLAG_IS_DEFAULT(UseAOT)) { 219 FLAG_SET_DEFAULT(UseAOT, false); 220 } 221 } 222 } 223 224 void AOTLoader::set_narrow_klass_shift() { 225 // This method could be called from Metaspace::set_narrow_klass_base_and_shift(). 226 // In case it is not called (during dump CDS, for example) the corresponding code in 227 // AOTLoader::universe_init(), which is called later, will set the shift value. 228 if (UseAOT && libraries_count() > 0 && 229 UseCompressedOops && AOTLib::narrow_oop_shift_initialized() && 230 UseCompressedClassPointers) { 231 int klass_shift = Universe::narrow_klass_shift(); 232 if (klass_shift == 0) { 233 Universe::set_narrow_klass_shift(AOTLib::narrow_klass_shift()); 234 } else { 235 FOR_ALL_AOT_LIBRARIES(lib) { 236 (*lib)->verify_flag(AOTLib::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift"); 237 } 238 } 239 } 240 } 241 242 void AOTLoader::load_library(const char* name, bool exit_on_error) { 243 // Skip library if a library with the same name is already loaded. 244 const int file_separator = *os::file_separator(); 245 const char* start = strrchr(name, file_separator); 246 const char* new_name = (start == NULL) ? name : (start + 1); 247 FOR_ALL_AOT_LIBRARIES(lib) { 248 const char* lib_name = (*lib)->name(); 249 start = strrchr(lib_name, file_separator); 250 const char* old_name = (start == NULL) ? lib_name : (start + 1); 251 if (strcmp(old_name, new_name) == 0) { 252 if (PrintAOT) { 253 warning("AOT library %s is already loaded as %s.", name, lib_name); 254 } 255 return; 256 } 257 } 258 char ebuf[1024]; 259 void* handle = os::dll_load(name, ebuf, sizeof ebuf); 260 if (handle == NULL) { 261 if (exit_on_error) { 262 tty->print_cr("error opening file: %s", ebuf); 263 vm_exit(1); 264 } 265 return; 266 } 267 const int dso_id = libraries_count() + 1; 268 AOTLib* lib = new AOTLib(handle, name, dso_id); 269 if (!lib->is_valid()) { 270 delete lib; 271 os::dll_unload(handle); 272 return; 273 } 274 add_library(lib); 275 } 276 277 #ifndef PRODUCT 278 void AOTLoader::print_statistics() { 279 { ttyLocker ttyl; 280 tty->print_cr("--- AOT Statistics ---"); 281 tty->print_cr("AOT libraries loaded: %d", heaps_count()); 282 AOTCodeHeap::print_statistics(); 283 } 284 } 285 #endif 286 287 288 bool AOTLoader::reconcile_dynamic_invoke(InstanceKlass* holder, int index, Method* adapter_method, Klass* appendix_klass) { 289 if (!UseAOT) { 290 return true; 291 } 292 JavaThread* thread = JavaThread::current(); 293 ResourceMark rm(thread); 294 RegisterMap map(thread, false); 295 frame caller_frame = thread->last_frame().sender(&map); // Skip stub 296 CodeBlob* caller_cb = caller_frame.cb(); 297 guarantee(caller_cb != NULL && caller_cb->is_compiled(), "must be called from compiled method"); 298 CompiledMethod* cm = caller_cb->as_compiled_method(); 299 300 if (!cm->is_aot()) { 301 return true; 302 } 303 AOTCompiledMethod* aot = (AOTCompiledMethod*)cm; 304 305 AOTCodeHeap* caller_heap = NULL; 306 FOR_ALL_AOT_HEAPS(heap) { 307 if ((*heap)->contains_blob(aot)) { 308 caller_heap = *heap; 309 break; 310 } 311 } 312 guarantee(caller_heap != NULL, "CodeHeap not found"); 313 bool success = caller_heap->reconcile_dynamic_invoke(aot, holder, index, adapter_method, appendix_klass); 314 vmassert(success || thread->last_frame().sender(&map).is_deoptimized_frame(), "caller not deoptimized on failure"); 315 return success; 316 }