1 /* 2 * Copyright (c) 1998, 2025, 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 25 #include "compiler/compilationPolicy.hpp" 26 #include "compiler/compileBroker.hpp" 27 #include "compiler/compileLog.hpp" 28 #include "compiler/compilerDirectives.hpp" 29 #include "compiler/compileTask.hpp" 30 #include "logging/log.hpp" 31 #include "logging/logStream.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "oops/klass.inline.hpp" 34 #include "oops/method.inline.hpp" 35 #include "runtime/handles.inline.hpp" 36 #include "runtime/jniHandles.hpp" 37 #include "runtime/mutexLocker.hpp" 38 39 CompileTask* CompileTask::_task_free_list = nullptr; 40 41 /** 42 * Allocate a CompileTask, from the free list if possible. 43 */ 44 CompileTask* CompileTask::allocate() { 45 MutexLocker locker(CompileTaskAlloc_lock); 46 CompileTask* task = nullptr; 47 48 if (_task_free_list != nullptr) { 49 task = _task_free_list; 50 _task_free_list = task->next(); 51 task->set_next(nullptr); 52 } else { 53 task = new CompileTask(); 54 task->set_next(nullptr); 55 task->set_is_free(true); 56 } 57 assert(task->is_free(), "Task must be free."); 58 task->set_is_free(false); 59 return task; 60 } 61 62 /** 63 * Add a task to the free list. 64 */ 65 void CompileTask::free(CompileTask* task) { 66 MutexLocker locker(CompileTaskAlloc_lock); 67 if (!task->is_free()) { 68 assert(!task->lock()->is_locked(), "Should not be locked when freed"); 69 if ((task->_method_holder != nullptr && JNIHandles::is_weak_global_handle(task->_method_holder))) { 70 JNIHandles::destroy_weak_global(task->_method_holder); 71 } else { 72 JNIHandles::destroy_global(task->_method_holder); 73 } 74 if (task->_failure_reason_on_C_heap && task->_failure_reason != nullptr) { 75 os::free((void*) task->_failure_reason); 76 } 77 task->_failure_reason = nullptr; 78 task->_failure_reason_on_C_heap = false; 79 80 task->set_is_free(true); 81 task->set_next(_task_free_list); 82 _task_free_list = task; 83 } 84 } 85 86 void CompileTask::initialize(int compile_id, 87 const methodHandle& method, 88 int osr_bci, 89 int comp_level, 90 int hot_count, 91 CompileTask::CompileReason compile_reason, 92 bool is_blocking) { 93 assert(!_lock->is_locked(), "bad locking"); 94 95 Thread* thread = Thread::current(); 96 _compile_id = compile_id; 97 _method = method(); 98 _method_holder = JNIHandles::make_weak_global(Handle(thread, method->method_holder()->klass_holder())); 99 _osr_bci = osr_bci; 100 _is_blocking = is_blocking; 101 JVMCI_ONLY(_has_waiter = CompileBroker::compiler(comp_level)->is_jvmci();) 102 JVMCI_ONLY(_blocking_jvmci_compile_state = nullptr;) 103 _comp_level = comp_level; 104 _num_inlined_bytecodes = 0; 105 106 _waiting_count = 0; 107 108 _is_complete = false; 109 _is_success = false; 110 111 _hot_count = hot_count; 112 _time_queued = os::elapsed_counter(); 113 _time_started = 0; 114 _compile_reason = compile_reason; 115 _nm_content_size = 0; 116 AbstractCompiler* comp = compiler(); 117 _directive = DirectivesStack::getMatchingDirective(method, comp); 118 _nm_insts_size = 0; 119 _nm_total_size = 0; 120 _failure_reason = nullptr; 121 _failure_reason_on_C_heap = false; 122 _training_data = nullptr; 123 _arena_bytes = 0; 124 125 _next = nullptr; 126 } 127 128 /** 129 * Returns the compiler for this task. 130 */ 131 AbstractCompiler* CompileTask::compiler() const { 132 return CompileBroker::compiler(_comp_level); 133 } 134 135 // Replace weak handles by strong handles to avoid unloading during compilation. 136 CompileTask* CompileTask::select_for_compilation() { 137 if (is_unloaded()) { 138 // Guard against concurrent class unloading 139 return nullptr; 140 } 141 Thread* thread = Thread::current(); 142 assert(_method->method_holder()->is_loader_alive(), "should be alive"); 143 Handle method_holder(thread, _method->method_holder()->klass_holder()); 144 JNIHandles::destroy_weak_global(_method_holder); 145 _method_holder = JNIHandles::make_global(method_holder); 146 return this; 147 } 148 149 void CompileTask::mark_on_stack() { 150 if (is_unloaded()) { 151 return; 152 } 153 // Mark these methods as something redefine classes cannot remove. 154 _method->set_on_stack(true); 155 } 156 157 bool CompileTask::is_unloaded() const { 158 return _method_holder != nullptr && JNIHandles::is_weak_global_handle(_method_holder) && JNIHandles::is_weak_global_cleared(_method_holder); 159 } 160 161 // RedefineClasses support 162 void CompileTask::metadata_do(MetadataClosure* f) { 163 if (is_unloaded()) { 164 return; 165 } 166 f->do_metadata(method()); 167 } 168 169 // ------------------------------------------------------------------ 170 // CompileTask::print_line_on_error 171 // 172 // This function is called by fatal error handler when the thread 173 // causing troubles is a compiler thread. 174 // 175 // Do not grab any lock, do not allocate memory. 176 // 177 // Otherwise it's the same as CompileTask::print_line() 178 // 179 void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) { 180 // print compiler name 181 st->print("%s:", CompileBroker::compiler_name(comp_level())); 182 print(st); 183 } 184 185 // ------------------------------------------------------------------ 186 // CompileTask::print_tty 187 void CompileTask::print_tty() { 188 ttyLocker ttyl; // keep the following output all in one block 189 print(tty); 190 } 191 192 // ------------------------------------------------------------------ 193 // CompileTask::print_impl 194 void CompileTask::print_impl(outputStream* st, Method* method, int compile_id, int comp_level, 195 bool is_osr_method, int osr_bci, bool is_blocking, 196 const char* msg, bool short_form, bool cr, 197 jlong time_queued, jlong time_started) { 198 if (!short_form) { 199 // Print current time 200 st->print(UINT64_FORMAT " ", (uint64_t) tty->time_stamp().milliseconds()); 201 if (Verbose && time_queued != 0) { 202 // Print time in queue and time being processed by compiler thread 203 jlong now = os::elapsed_counter(); 204 st->print("%.0f ", TimeHelper::counter_to_millis(now-time_queued)); 205 if (time_started != 0) { 206 st->print("%.0f ", TimeHelper::counter_to_millis(now-time_started)); 207 } 208 } 209 } 210 // print compiler name if requested 211 if (CIPrintCompilerName) { 212 st->print("%s:", CompileBroker::compiler_name(comp_level)); 213 } 214 st->print("%4d ", compile_id); // print compilation number 215 216 bool is_synchronized = false; 217 bool has_exception_handler = false; 218 bool is_native = false; 219 if (method != nullptr) { 220 is_synchronized = method->is_synchronized(); 221 has_exception_handler = method->has_exception_handler(); 222 is_native = method->is_native(); 223 } 224 // method attributes 225 const char compile_type = is_osr_method ? '%' : ' '; 226 const char sync_char = is_synchronized ? 's' : ' '; 227 const char exception_char = has_exception_handler ? '!' : ' '; 228 const char blocking_char = is_blocking ? 'b' : ' '; 229 const char native_char = is_native ? 'n' : ' '; 230 231 // print method attributes 232 st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char); 233 234 if (TieredCompilation) { 235 if (comp_level != -1) st->print("%d ", comp_level); 236 else st->print("- "); 237 } 238 st->print(" "); // more indent 239 240 if (method == nullptr) { 241 st->print("(method)"); 242 } else { 243 method->print_short_name(st); 244 if (is_osr_method) { 245 st->print(" @ %d", osr_bci); 246 } 247 if (method->is_native()) 248 st->print(" (native)"); 249 else 250 st->print(" (%d bytes)", method->code_size()); 251 } 252 253 if (msg != nullptr) { 254 st->print(" %s", msg); 255 } 256 if (cr) { 257 st->cr(); 258 } 259 } 260 261 // ------------------------------------------------------------------ 262 // CompileTask::print_compilation 263 void CompileTask::print(outputStream* st, const char* msg, bool short_form, bool cr) { 264 bool is_osr_method = osr_bci() != InvocationEntryBci; 265 print_impl(st, is_unloaded() ? nullptr : method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form, cr, _time_queued, _time_started); 266 } 267 268 // ------------------------------------------------------------------ 269 // CompileTask::log_task 270 void CompileTask::log_task(xmlStream* log) { 271 Thread* thread = Thread::current(); 272 methodHandle method(thread, this->method()); 273 ResourceMark rm(thread); 274 275 // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'> 276 log->print(" compile_id='%d'", _compile_id); 277 if (_osr_bci != CompileBroker::standard_entry_bci) { 278 log->print(" compile_kind='osr'"); // same as nmethod::compile_kind 279 } // else compile_kind='c2c' 280 if (!method.is_null()) log->method(method()); 281 if (_osr_bci != CompileBroker::standard_entry_bci) { 282 log->print(" osr_bci='%d'", _osr_bci); 283 } 284 if (_comp_level != CompilationPolicy::highest_compile_level()) { 285 log->print(" level='%d'", _comp_level); 286 } 287 if (_is_blocking) { 288 log->print(" blocking='1'"); 289 } 290 log->stamp(); 291 } 292 293 // ------------------------------------------------------------------ 294 // CompileTask::log_task_queued 295 void CompileTask::log_task_queued() { 296 ttyLocker ttyl; 297 ResourceMark rm; 298 NoSafepointVerifier nsv; 299 300 xtty->begin_elem("task_queued"); 301 log_task(xtty); 302 assert(_compile_reason > CompileTask::Reason_None && _compile_reason < CompileTask::Reason_Count, "Valid values"); 303 xtty->print(" comment='%s'", reason_name(_compile_reason)); 304 305 if (_hot_count != 0) { 306 xtty->print(" hot_count='%d'", _hot_count); 307 } 308 xtty->end_elem(); 309 } 310 311 312 // ------------------------------------------------------------------ 313 // CompileTask::log_task_start 314 void CompileTask::log_task_start(CompileLog* log) { 315 log->begin_head("task"); 316 log_task(log); 317 log->end_head(); 318 } 319 320 321 // ------------------------------------------------------------------ 322 // CompileTask::log_task_done 323 void CompileTask::log_task_done(CompileLog* log) { 324 Thread* thread = Thread::current(); 325 methodHandle method(thread, this->method()); 326 ResourceMark rm(thread); 327 328 if (!_is_success) { 329 assert(_failure_reason != nullptr, "missing"); 330 const char* reason = _failure_reason != nullptr ? _failure_reason : "unknown"; 331 log->begin_elem("failure reason='"); 332 log->text("%s", reason); 333 log->print("'"); 334 log->end_elem(); 335 } 336 337 // <task_done ... stamp='1.234'> </task> 338 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", 339 _is_success, _nm_content_size, 340 method->invocation_count()); 341 int bec = method->backedge_count(); 342 if (bec != 0) log->print(" backedge_count='%d'", bec); 343 // Note: "_is_complete" is about to be set, but is not. 344 if (_num_inlined_bytecodes != 0) { 345 log->print(" inlined_bytes='%d'", _num_inlined_bytecodes); 346 } 347 log->stamp(); 348 log->end_elem(); 349 log->clear_identities(); // next task will have different CI 350 log->tail("task"); 351 log->flush(); 352 log->mark_file_end(); 353 } 354 355 // ------------------------------------------------------------------ 356 // CompileTask::check_break_at_flags 357 bool CompileTask::check_break_at_flags() { 358 int compile_id = this->_compile_id; 359 bool is_osr = (_osr_bci != CompileBroker::standard_entry_bci); 360 361 if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) { 362 return true; 363 } else { 364 return (compile_id == CIBreakAt); 365 } 366 } 367 368 // ------------------------------------------------------------------ 369 // CompileTask::print_inlining 370 void CompileTask::print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg) { 371 print_inlining_header(st, method, inline_level, bci); 372 print_inlining_inner_message(st, result, msg); 373 st->cr(); 374 } 375 376 void CompileTask::print_inlining_header(outputStream* st, ciMethod* method, int inline_level, int bci) { 377 // 1234567 378 st->print(" "); // print timestamp 379 // 1234 380 st->print(" "); // print compilation number 381 382 // method attributes 383 if (method->is_loaded()) { 384 const char sync_char = method->is_synchronized() ? 's' : ' '; 385 const char exception_char = method->has_exception_handlers() ? '!' : ' '; 386 const char monitors_char = method->has_monitor_bytecodes() ? 'm' : ' '; 387 388 // print method attributes 389 st->print(" %c%c%c ", sync_char, exception_char, monitors_char); 390 } else { 391 // %s!bn 392 st->print(" "); // print method attributes 393 } 394 395 if (TieredCompilation) { 396 st->print(" "); 397 } 398 st->print(" "); // more indent 399 st->print(" "); // initial inlining indent 400 401 for (int i = 0; i < inline_level; i++) { 402 st->print(" "); 403 } 404 405 st->print("@ %d ", bci); // print bci 406 print_inline_inner_method_info(st, method); 407 } 408 409 void CompileTask::print_inline_inner_method_info(outputStream* st, ciMethod* method) { 410 method->print_short_name(st); 411 if (method->is_loaded()) { 412 st->print(" (%d bytes)", method->code_size()); 413 } else { 414 st->print(" (not loaded)"); 415 } 416 } 417 418 void CompileTask::print_inline_indent(int inline_level, outputStream* st) { 419 // 1234567 420 st->print(" "); // print timestamp 421 // 1234 422 st->print(" "); // print compilation number 423 // %s!bn 424 st->print(" "); // print method attributes 425 if (TieredCompilation) { 426 st->print(" "); 427 } 428 st->print(" "); // more indent 429 st->print(" "); // initial inlining indent 430 for (int i = 0; i < inline_level; i++) { 431 st->print(" "); 432 } 433 } 434 435 void CompileTask::print_inlining_inner_message(outputStream* st, InliningResult result, const char* msg) { 436 if (msg != nullptr) { 437 st->print(" %s%s", result == InliningResult::SUCCESS ? "" : "failed to inline: ", msg); 438 } else if (result == InliningResult::FAILURE) { 439 st->print(" %s", "failed to inline"); 440 } 441 } 442 443 void CompileTask::print_ul(const char* msg){ 444 LogTarget(Info, jit, compilation) lt; 445 if (lt.is_enabled()) { 446 LogStream ls(lt); 447 print(&ls, msg, /* short form */ true, /* cr */ true); 448 } 449 } 450 451 void CompileTask::print_ul(const nmethod* nm, const char* msg) { 452 LogTarget(Info, jit, compilation) lt; 453 if (lt.is_enabled()) { 454 LogStream ls(lt); 455 print_impl(&ls, nm->method(), nm->compile_id(), 456 nm->comp_level(), nm->is_osr_method(), 457 nm->is_osr_method() ? nm->osr_entry_bci() : -1, 458 /*is_blocking*/ false, 459 msg, /* short form */ true, /* cr */ true); 460 } 461 } 462 463 void CompileTask::print_inlining_ul(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg) { 464 LogTarget(Debug, jit, inlining) lt; 465 if (lt.is_enabled()) { 466 LogStream ls(lt); 467 print_inlining_inner(&ls, method, inline_level, bci, result, msg); 468 } 469 } 470