1 /* 2 * Copyright (c) 2011, 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 #include "memory/oopFactory.hpp" 26 #include "memory/resourceArea.hpp" 27 #include "oops/oop.inline.hpp" 28 #include "runtime/javaCalls.hpp" 29 #include "runtime/handles.hpp" 30 #include "jvmci/jvmciJavaClasses.hpp" 31 #include "jvmci/jvmciCompiler.hpp" 32 #include "jvmci/jvmciEnv.hpp" 33 #include "jvmci/jvmciRuntime.hpp" 34 #include "runtime/compilationPolicy.hpp" 35 #include "runtime/globals_extension.hpp" 36 37 JVMCICompiler* JVMCICompiler::_instance = NULL; 38 elapsedTimer JVMCICompiler::_codeInstallTimer; 39 40 JVMCICompiler::JVMCICompiler() : AbstractCompiler(jvmci) { 41 _bootstrapping = false; 42 _bootstrap_compilation_request_handled = false; 43 _methods_compiled = 0; 44 assert(_instance == NULL, "only one instance allowed"); 45 _instance = this; 46 } 47 48 // Initialization 49 void JVMCICompiler::initialize() { 50 if (!UseCompiler || !EnableJVMCI || !UseJVMCICompiler || !should_perform_init()) { 51 return; 52 } 53 54 set_state(initialized); 55 56 // JVMCI is considered as application code so we need to 57 // stop the VM deferring compilation now. 58 CompilationPolicy::completed_vm_startup(); 59 } 60 61 void JVMCICompiler::bootstrap(TRAPS) { 62 if (Arguments::mode() == Arguments::_int) { 63 // Nothing to do in -Xint mode 64 return; 65 } 66 #ifndef PRODUCT 67 // We turn off CompileTheWorld so that compilation requests are not 68 // ignored during bootstrap or that JVMCI can be compiled by C1/C2. 69 FlagSetting ctwOff(CompileTheWorld, false); 70 #endif 71 72 _bootstrapping = true; 73 ResourceMark rm; 74 HandleMark hm; 75 if (PrintBootstrap) { 76 tty->print("Bootstrapping JVMCI"); 77 } 78 jlong start = os::javaTimeMillis(); 79 80 Array<Method*>* objectMethods = SystemDictionary::Object_klass()->methods(); 81 // Initialize compile queue with a selected set of methods. 82 int len = objectMethods->length(); 83 for (int i = 0; i < len; i++) { 84 methodHandle mh = objectMethods->at(i); 85 if (!mh->is_native() && !mh->is_static() && !mh->is_initializer()) { 86 ResourceMark rm; 87 int hot_count = 10; // TODO: what's the appropriate value? 88 CompileBroker::compile_method(mh, InvocationEntryBci, CompLevel_full_optimization, mh, hot_count, CompileTask::Reason_Bootstrap, THREAD); 89 } 90 } 91 92 int qsize; 93 bool first_round = true; 94 int z = 0; 95 do { 96 // Loop until there is something in the queue. 97 do { 98 os::sleep(THREAD, 100, true); 99 qsize = CompileBroker::queue_size(CompLevel_full_optimization); 100 } while (!_bootstrap_compilation_request_handled && first_round && qsize == 0); 101 first_round = false; 102 if (PrintBootstrap) { 103 while (z < (_methods_compiled / 100)) { 104 ++z; 105 tty->print_raw("."); 106 } 107 } 108 } while (qsize != 0); 109 110 if (PrintBootstrap) { 111 tty->print_cr(" in " JLONG_FORMAT " ms (compiled %d methods)", os::javaTimeMillis() - start, _methods_compiled); 112 } 113 _bootstrapping = false; 114 JVMCIRuntime::bootstrap_finished(CHECK); 115 } 116 117 #define CHECK_ABORT THREAD); \ 118 if (HAS_PENDING_EXCEPTION) { \ 119 char buf[256]; \ 120 jio_snprintf(buf, 256, "Uncaught exception at %s:%d", __FILE__, __LINE__); \ 121 JVMCICompiler::abort_on_pending_exception(PENDING_EXCEPTION, buf); \ 122 return; \ 123 } \ 124 (void)(0 125 126 void JVMCICompiler::compile_method(const methodHandle& method, int entry_bci, JVMCIEnv* env) { 127 JVMCI_EXCEPTION_CONTEXT 128 if (SystemDictionary::java_system_loader() == NULL) { 129 // 8160121: The system class loader is cached in the VM after VM init phase 3 has completed. 130 // We cannot compile before then as the ServiceLoader cannot be used until after phase 3. 131 return; 132 } 133 134 bool is_osr = entry_bci != InvocationEntryBci; 135 if (_bootstrapping && is_osr) { 136 // no OSR compilations during bootstrap - the compiler is just too slow at this point, 137 // and we know that there are no endless loops 138 return; 139 } 140 141 JVMCIRuntime::initialize_well_known_classes(CHECK_ABORT); 142 143 HandleMark hm; 144 Handle receiver = JVMCIRuntime::get_HotSpotJVMCIRuntime(CHECK_ABORT); 145 146 JavaValue method_result(T_OBJECT); 147 JavaCallArguments args; 148 args.push_long((jlong) (address) method()); 149 JavaCalls::call_static(&method_result, SystemDictionary::HotSpotResolvedJavaMethodImpl_klass(), 150 vmSymbols::fromMetaspace_name(), vmSymbols::method_fromMetaspace_signature(), &args, THREAD); 151 152 JavaValue result(T_OBJECT); 153 if (!HAS_PENDING_EXCEPTION) { 154 JavaCallArguments args; 155 args.push_oop(receiver); 156 args.push_oop((oop)method_result.get_jobject()); 157 args.push_int(entry_bci); 158 args.push_long((jlong) (address) env); 159 args.push_int(env->task()->compile_id()); 160 JavaCalls::call_special(&result, receiver->klass(), 161 vmSymbols::compileMethod_name(), vmSymbols::compileMethod_signature(), &args, THREAD); 162 } 163 164 // An uncaught exception was thrown during compilation. Generally these 165 // should be handled by the Java code in some useful way but if they leak 166 // through to here report them instead of dying or silently ignoring them. 167 if (HAS_PENDING_EXCEPTION) { 168 Handle exception(THREAD, PENDING_EXCEPTION); 169 CLEAR_PENDING_EXCEPTION; 170 171 java_lang_Throwable::java_printStackTrace(exception, THREAD); 172 if (HAS_PENDING_EXCEPTION) { 173 CLEAR_PENDING_EXCEPTION; 174 } 175 176 env->set_failure("exception throw", false); 177 } else { 178 oop result_object = (oop) result.get_jobject(); 179 if (result_object != NULL) { 180 oop failure_message = HotSpotCompilationRequestResult::failureMessage(result_object); 181 if (failure_message != NULL) { 182 const char* failure_reason = java_lang_String::as_utf8_string(failure_message); 183 env->set_failure(failure_reason, HotSpotCompilationRequestResult::retry(result_object) != 0); 184 } else { 185 if (env->task()->code() == NULL) { 186 env->set_failure("no nmethod produced", true); 187 } else { 188 env->task()->set_num_inlined_bytecodes(HotSpotCompilationRequestResult::inlinedBytecodes(result_object)); 189 Atomic::inc(&_methods_compiled); 190 } 191 } 192 } else { 193 assert(false, "JVMCICompiler.compileMethod should always return non-null"); 194 } 195 } 196 if (_bootstrapping) { 197 _bootstrap_compilation_request_handled = true; 198 } 199 } 200 201 CompLevel JVMCIRuntime::adjust_comp_level(methodHandle method, bool is_osr, CompLevel level, JavaThread* thread) { 202 if (!thread->adjusting_comp_level()) { 203 thread->set_adjusting_comp_level(true); 204 level = adjust_comp_level_inner(method, is_osr, level, thread); 205 thread->set_adjusting_comp_level(false); 206 } 207 return level; 208 } 209 210 /** 211 * Aborts the VM due to an unexpected exception. 212 */ 213 void JVMCICompiler::abort_on_pending_exception(Handle exception, const char* message, bool dump_core) { 214 Thread* THREAD = Thread::current(); 215 CLEAR_PENDING_EXCEPTION; 216 217 java_lang_Throwable::java_printStackTrace(exception, THREAD); 218 219 // Give other aborting threads to also print their stack traces. 220 // This can be very useful when debugging class initialization 221 // failures. 222 assert(THREAD->is_Java_thread(), "compiler threads should be Java threads"); 223 const bool interruptible = true; 224 os::sleep(THREAD, 200, interruptible); 225 226 vm_abort(dump_core); 227 } 228 229 // Compilation entry point for methods 230 void JVMCICompiler::compile_method(ciEnv* env, ciMethod* target, int entry_bci, DirectiveSet* directive) { 231 ShouldNotReachHere(); 232 } 233 234 bool JVMCICompiler::is_trivial(Method* method) { 235 if (_bootstrapping) { 236 return false; 237 } 238 return JVMCIRuntime::treat_as_trivial(method); 239 } 240 241 // Print compilation timers and statistics 242 void JVMCICompiler::print_timers() { 243 print_compilation_timers(); 244 } 245 246 // Print compilation timers and statistics 247 void JVMCICompiler::print_compilation_timers() { 248 TRACE_jvmci_1("JVMCICompiler::print_timers"); 249 tty->print_cr(" JVMCI code install time: %6.3f s", _codeInstallTimer.seconds()); 250 }