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