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