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