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 int CompileTask::_active_tasks = 0; 41 42 /** 43 * Allocate a CompileTask, from the free list if possible. 44 */ 45 CompileTask* CompileTask::allocate() { 46 MonitorLocker locker(CompileTaskAlloc_lock); 47 CompileTask* task = nullptr; 48 49 if (_task_free_list != nullptr) { 50 task = _task_free_list; 51 _task_free_list = task->next(); 52 task->set_next(nullptr); 53 } else { 54 task = new CompileTask(); 55 task->set_next(nullptr); 56 task->set_is_free(true); 57 } 58 assert(task->is_free(), "Task must be free."); 59 task->set_is_free(false); 60 _active_tasks++; 61 return task; 62 } 63 64 /** 65 * Add a task to the free list. 66 */ 67 void CompileTask::free(CompileTask* task) { 68 MonitorLocker locker(CompileTaskAlloc_lock); 69 if (!task->is_free()) { 70 assert(!task->lock()->is_locked(), "Should not be locked when freed"); 71 if ((task->_method_holder != nullptr && JNIHandles::is_weak_global_handle(task->_method_holder)) || 72 (task->_hot_method_holder != nullptr && JNIHandles::is_weak_global_handle(task->_hot_method_holder))) { 73 JNIHandles::destroy_weak_global(task->_method_holder); 74 JNIHandles::destroy_weak_global(task->_hot_method_holder); 75 } else { 76 JNIHandles::destroy_global(task->_method_holder); 77 JNIHandles::destroy_global(task->_hot_method_holder); 78 } 79 if (task->_failure_reason_on_C_heap && task->_failure_reason != nullptr) { 80 os::free((void*) task->_failure_reason); 81 } 82 task->_failure_reason = nullptr; 83 task->_failure_reason_on_C_heap = false; 84 85 task->set_is_free(true); 86 task->set_next(_task_free_list); 87 _task_free_list = task; 88 _active_tasks--; 89 if (_active_tasks == 0) { 90 locker.notify_all(); 91 } 92 } 93 } 94 95 void CompileTask::wait_for_no_active_tasks() { 96 MonitorLocker locker(CompileTaskAlloc_lock); 97 while (_active_tasks > 0) { 98 locker.wait(); 99 } 100 } 101 102 void CompileTask::initialize(int compile_id, 103 const methodHandle& method, 104 int osr_bci, 105 int comp_level, 106 const methodHandle& hot_method, 107 int hot_count, 108 AOTCodeEntry* aot_code_entry, 109 CompileTask::CompileReason compile_reason, 110 CompileQueue* compile_queue, 111 bool requires_online_compilation, 112 bool is_blocking) { 113 assert(!_lock->is_locked(), "bad locking"); 114 115 Thread* thread = Thread::current(); 116 _compile_id = compile_id; 117 _method = method(); 118 _method_holder = JNIHandles::make_weak_global(Handle(thread, method->method_holder()->klass_holder())); 119 _osr_bci = osr_bci; 120 _requires_online_compilation = requires_online_compilation; 121 _is_blocking = is_blocking; 122 _comp_level = comp_level; 123 _num_inlined_bytecodes = 0; 124 125 _waiting_count = 0; 126 127 _is_complete = false; 128 _is_success = false; 129 130 _next = nullptr; 131 _prev = nullptr; 132 133 _hot_method = nullptr; 134 _hot_method_holder = nullptr; 135 _hot_count = hot_count; 136 _time_created = os::elapsed_counter(); 137 _time_queued = 0; 138 _time_started = 0; 139 _time_finished = 0; 140 _aot_load_start = 0; 141 _aot_load_finish = 0; 142 _compile_reason = compile_reason; 143 _nm_content_size = 0; 144 _nm_insts_size = 0; 145 _nm_total_size = 0; 146 _failure_reason = nullptr; 147 _failure_reason_on_C_heap = false; 148 _training_data = nullptr; 149 _aot_code_entry = aot_code_entry; 150 _compile_queue = compile_queue; 151 152 AbstractCompiler* comp = CompileBroker::compiler(comp_level); 153 #if INCLUDE_JVMCI 154 if (comp->is_jvmci() && CompileBroker::compiler3() != nullptr) { 155 assert(_method != nullptr, "sanity"); 156 if (((JVMCICompiler*)comp)->force_comp_at_level_simple(method)) { 157 comp = CompileBroker::compiler3(); 158 } 159 } 160 #endif 161 _compiler = comp; 162 _directive = DirectivesStack::getMatchingDirective(method, comp); 163 164 JVMCI_ONLY(_has_waiter = comp->is_jvmci();) 165 JVMCI_ONLY(_blocking_jvmci_compile_state = nullptr;) 166 _arena_bytes = 0; 167 168 if (LogCompilation) { 169 if (hot_method.not_null()) { 170 if (hot_method == method) { 171 _hot_method = _method; 172 } else { 173 _hot_method = hot_method(); 174 // only add loader or mirror if different from _method_holder 175 _hot_method_holder = JNIHandles::make_weak_global(Handle(thread, hot_method->method_holder()->klass_holder())); 176 } 177 } 178 } 179 180 _next = nullptr; 181 } 182 183 /** 184 * Returns the compiler for this task. 185 */ 186 AbstractCompiler* CompileTask::compiler() const { 187 assert(_compiler != nullptr, "should be set"); 188 return _compiler; 189 } 190 191 // Replace weak handles by strong handles to avoid unloading during compilation. 192 CompileTask* CompileTask::select_for_compilation() { 193 if (_compile_reason == Reason_Preload) { 194 return this; 195 } 196 if (is_unloaded()) { 197 // Guard against concurrent class unloading 198 return nullptr; 199 } 200 Thread* thread = Thread::current(); 201 assert(_method->method_holder()->is_loader_alive(), "should be alive"); 202 Handle method_holder(thread, _method->method_holder()->klass_holder()); 203 JNIHandles::destroy_weak_global(_method_holder); 204 JNIHandles::destroy_weak_global(_hot_method_holder); 205 _method_holder = JNIHandles::make_global(method_holder); 206 if (_hot_method != nullptr) { 207 _hot_method_holder = JNIHandles::make_global(Handle(thread, _hot_method->method_holder()->klass_holder())); 208 } 209 return this; 210 } 211 212 void CompileTask::mark_on_stack() { 213 if (is_unloaded()) { 214 return; 215 } 216 // Mark these methods as something redefine classes cannot remove. 217 _method->set_on_stack(true); 218 if (_hot_method != nullptr) { 219 _hot_method->set_on_stack(true); 220 } 221 } 222 223 bool CompileTask::is_unloaded() const { 224 if (preload()) return false; 225 return _method_holder != nullptr && JNIHandles::is_weak_global_handle(_method_holder) && JNIHandles::is_weak_global_cleared(_method_holder); 226 } 227 228 // RedefineClasses support 229 void CompileTask::metadata_do(MetadataClosure* f) { 230 if (is_unloaded()) { 231 return; 232 } 233 f->do_metadata(method()); 234 if (hot_method() != nullptr && hot_method() != method()) { 235 f->do_metadata(hot_method()); 236 } 237 } 238 239 // ------------------------------------------------------------------ 240 // CompileTask::print_line_on_error 241 // 242 // This function is called by fatal error handler when the thread 243 // causing troubles is a compiler thread. 244 // 245 // Do not grab any lock, do not allocate memory. 246 // 247 // Otherwise it's the same as CompileTask::print_line() 248 // 249 void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) { 250 // print compiler name 251 st->print("%s:", compiler()->name()); 252 print(st); 253 } 254 255 // ------------------------------------------------------------------ 256 // CompileTask::print_tty 257 void CompileTask::print_tty() { 258 ttyLocker ttyl; // keep the following output all in one block 259 print(tty); 260 } 261 262 // ------------------------------------------------------------------ 263 // CompileTask::print_impl 264 void CompileTask::print_impl(outputStream* st, Method* method, int compile_id, int comp_level, 265 bool is_osr_method, int osr_bci, bool is_blocking, bool is_aot, bool is_preload, 266 const char* compiler_name, 267 const char* msg, bool short_form, bool cr, 268 jlong time_created, jlong time_queued, jlong time_started, jlong time_finished, 269 jlong aot_load_start, jlong aot_load_finish) { 270 if (!short_form) { 271 { 272 stringStream ss; 273 ss.print(UINT64_FORMAT, (uint64_t) tty->time_stamp().milliseconds()); 274 st->print("%7s ", ss.freeze()); 275 } 276 { // Time waiting to be put on queue 277 stringStream ss; 278 if (time_created != 0 && time_queued != 0) { 279 ss.print("W%.1f", TimeHelper::counter_to_millis(time_queued - time_created)); 280 } 281 st->print("%7s ", ss.freeze()); 282 } 283 { // Time in queue 284 stringStream ss; 285 if (time_queued != 0 && time_started != 0) { 286 ss.print("Q%.1f", TimeHelper::counter_to_millis(time_started - time_queued)); 287 } 288 st->print("%7s ", ss.freeze()); 289 } 290 { // Time in compilation 291 stringStream ss; 292 if (time_started != 0 && time_finished != 0) { 293 ss.print("C%.1f", TimeHelper::counter_to_millis(time_finished - time_started)); 294 } 295 st->print("%7s ", ss.freeze()); 296 } 297 { // Time to load from AOT code cache 298 stringStream ss; 299 if (aot_load_start != 0 && aot_load_finish != 0) { 300 ss.print("A%.1f", TimeHelper::counter_to_millis(aot_load_finish - aot_load_start)); 301 } 302 st->print("%7s ", ss.freeze()); 303 } 304 st->print(" "); 305 } 306 307 // print compiler name if requested 308 if (CIPrintCompilerName) { 309 st->print("%s:", compiler_name); 310 } 311 st->print("%4d ", compile_id); // print compilation number 312 313 bool is_synchronized = false; 314 bool has_exception_handler = false; 315 bool is_native = false; 316 if (method != nullptr) { 317 is_synchronized = method->is_synchronized(); 318 has_exception_handler = method->has_exception_handler(); 319 is_native = method->is_native(); 320 } 321 // method attributes 322 const char compile_type = is_osr_method ? '%' : ' '; 323 const char sync_char = is_synchronized ? 's' : ' '; 324 const char exception_char = has_exception_handler ? '!' : ' '; 325 const char blocking_char = is_blocking ? 'b' : ' '; 326 const char native_char = is_native ? 'n' : ' '; 327 const char aot_char = is_aot ? 'A' : ' '; 328 const char preload_char = is_preload ? 'P' : ' '; 329 330 // print method attributes 331 st->print("%c%c%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char, aot_char, preload_char); 332 333 if (TieredCompilation) { 334 if (comp_level != -1) st->print("%d ", comp_level); 335 else st->print("- "); 336 } 337 st->print(" "); // more indent 338 339 if (method == nullptr) { 340 st->print("(method)"); 341 } else { 342 method->print_short_name(st); 343 if (is_osr_method) { 344 st->print(" @ %d", osr_bci); 345 } 346 if (method->is_native()) 347 st->print(" (native)"); 348 else 349 st->print(" (%d bytes)", method->code_size()); 350 } 351 352 if (msg != nullptr) { 353 st->print(" %s", msg); 354 } 355 if (cr) { 356 st->cr(); 357 } 358 } 359 360 // ------------------------------------------------------------------ 361 // CompileTask::print_compilation 362 void CompileTask::print(outputStream* st, const char* msg, bool short_form, bool cr) { 363 bool is_osr_method = osr_bci() != InvocationEntryBci; 364 print_impl(st, is_unloaded() ? nullptr : method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), is_aot(), preload(), 365 compiler()->name(), msg, short_form, cr, _time_created, _time_queued, _time_started, _time_finished, _aot_load_start, _aot_load_finish); 366 } 367 368 // ------------------------------------------------------------------ 369 // CompileTask::log_task 370 void CompileTask::log_task(xmlStream* log) { 371 Thread* thread = Thread::current(); 372 methodHandle method(thread, this->method()); 373 ResourceMark rm(thread); 374 375 // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'> 376 log->print(" compile_id='%d'", _compile_id); 377 if (_osr_bci != CompileBroker::standard_entry_bci) { 378 log->print(" compile_kind='osr'"); // same as nmethod::compile_kind 379 } // else compile_kind='c2c' 380 if (!method.is_null()) log->method(method()); 381 if (_osr_bci != CompileBroker::standard_entry_bci) { 382 log->print(" osr_bci='%d'", _osr_bci); 383 } 384 if (_comp_level != CompilationPolicy::highest_compile_level()) { 385 log->print(" level='%d'", _comp_level); 386 } 387 if (_is_blocking) { 388 log->print(" blocking='1'"); 389 } 390 } 391 392 // ------------------------------------------------------------------ 393 // CompileTask::log_task_queued 394 void CompileTask::log_task_queued() { 395 ttyLocker ttyl; 396 ResourceMark rm; 397 NoSafepointVerifier nsv; 398 399 xtty->begin_elem("task_queued"); 400 log_task(xtty); 401 assert(_compile_reason > CompileTask::Reason_None && _compile_reason < CompileTask::Reason_Count, "Valid values"); 402 xtty->print(" comment='%s'", reason_name(_compile_reason)); 403 404 if (_hot_method != nullptr && _hot_method != _method) { 405 xtty->method(_hot_method, "hot_"); 406 } 407 if (_hot_count != 0) { 408 xtty->print(" hot_count='%d'", _hot_count); 409 } 410 xtty->stamp(); 411 xtty->end_elem(); 412 } 413 414 415 // ------------------------------------------------------------------ 416 // CompileTask::log_task_start 417 void CompileTask::log_task_start(CompileLog* log) { 418 log->begin_head("task"); 419 log_task(log); 420 log->stamp(); 421 log->end_head(); 422 } 423 424 425 // ------------------------------------------------------------------ 426 // CompileTask::log_task_done 427 void CompileTask::log_task_done(CompileLog* log) { 428 Thread* thread = Thread::current(); 429 methodHandle method(thread, this->method()); 430 ResourceMark rm(thread); 431 432 if (!_is_success) { 433 assert(_failure_reason != nullptr, "missing"); 434 const char* reason = _failure_reason != nullptr ? _failure_reason : "unknown"; 435 log->begin_elem("failure reason='"); 436 log->text("%s", reason); 437 log->print("'"); 438 log->end_elem(); 439 } 440 441 // <task_done ... stamp='1.234'> </task> 442 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", 443 _is_success, _nm_content_size, 444 method->invocation_count()); 445 int bec = method->backedge_count(); 446 if (bec != 0) log->print(" backedge_count='%d'", bec); 447 // Note: "_is_complete" is about to be set, but is not. 448 if (_num_inlined_bytecodes != 0) { 449 log->print(" inlined_bytes='%d'", _num_inlined_bytecodes); 450 } 451 log->stamp(); 452 log->end_elem(); 453 log->clear_identities(); // next task will have different CI 454 log->tail("task"); 455 log->flush(); 456 log->mark_file_end(); 457 } 458 459 // ------------------------------------------------------------------ 460 // CompileTask::check_break_at_flags 461 bool CompileTask::check_break_at_flags() { 462 int compile_id = this->_compile_id; 463 bool is_osr = (_osr_bci != CompileBroker::standard_entry_bci); 464 465 if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) { 466 return true; 467 } else { 468 return (compile_id == CIBreakAt); 469 } 470 } 471 472 // ------------------------------------------------------------------ 473 // CompileTask::print_inlining 474 void CompileTask::print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg) { 475 print_inlining_header(st, method, inline_level, bci); 476 print_inlining_inner_message(st, result, msg); 477 st->cr(); 478 } 479 480 void CompileTask::print_inlining_header(outputStream* st, ciMethod* method, int inline_level, int bci) { 481 // 1234567 482 st->print(" "); // print timestamp 483 // 1234 484 st->print(" "); // print compilation number 485 486 // method attributes 487 if (method->is_loaded()) { 488 const char sync_char = method->is_synchronized() ? 's' : ' '; 489 const char exception_char = method->has_exception_handlers() ? '!' : ' '; 490 const char monitors_char = method->has_monitor_bytecodes() ? 'm' : ' '; 491 492 // print method attributes 493 st->print(" %c%c%c ", sync_char, exception_char, monitors_char); 494 } else { 495 // %s!bn 496 st->print(" "); // print method attributes 497 } 498 499 if (TieredCompilation) { 500 st->print(" "); 501 } 502 st->print(" "); // more indent 503 st->print(" "); // initial inlining indent 504 505 for (int i = 0; i < inline_level; i++) { 506 st->print(" "); 507 } 508 509 st->print("@ %d ", bci); // print bci 510 print_inline_inner_method_info(st, method); 511 } 512 513 void CompileTask::print_inline_inner_method_info(outputStream* st, ciMethod* method) { 514 method->print_short_name(st); 515 if (method->is_loaded()) { 516 st->print(" (%d bytes)", method->code_size()); 517 } else { 518 st->print(" (not loaded)"); 519 } 520 } 521 522 void CompileTask::print_inline_indent(int inline_level, outputStream* st) { 523 // 1234567 524 st->print(" "); // print timestamp 525 // 1234 526 st->print(" "); // print compilation number 527 // %s!bn 528 st->print(" "); // print method attributes 529 if (TieredCompilation) { 530 st->print(" "); 531 } 532 st->print(" "); // more indent 533 st->print(" "); // initial inlining indent 534 for (int i = 0; i < inline_level; i++) { 535 st->print(" "); 536 } 537 } 538 539 void CompileTask::print_inlining_inner_message(outputStream* st, InliningResult result, const char* msg) { 540 if (msg != nullptr) { 541 st->print(" %s%s", result == InliningResult::SUCCESS ? "" : "failed to inline: ", msg); 542 } else if (result == InliningResult::FAILURE) { 543 st->print(" %s", "failed to inline"); 544 } 545 } 546 547 void CompileTask::print_ul(const char* msg){ 548 LogTarget(Debug, jit, compilation) lt; 549 if (lt.is_enabled()) { 550 LogStream ls(lt); 551 print(&ls, msg, /* short form */ true, /* cr */ true); 552 } 553 } 554 555 void CompileTask::print_ul(const nmethod* nm, const char* msg) { 556 LogTarget(Debug, jit, compilation) lt; 557 if (lt.is_enabled()) { 558 LogStream ls(lt); 559 print_impl(&ls, nm->method(), nm->compile_id(), 560 nm->comp_level(), nm->is_osr_method(), 561 nm->is_osr_method() ? nm->osr_entry_bci() : -1, 562 /*is_blocking*/ false, nm->aot_code_entry() != nullptr, 563 nm->preloaded(), nm->compiler_name(), 564 msg, /* short form */ true, /* cr */ true); 565 } 566 } 567 568 void CompileTask::print_inlining_ul(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg) { 569 LogTarget(Debug, jit, inlining) lt; 570 if (lt.is_enabled()) { 571 LogStream ls(lt); 572 print_inlining_inner(&ls, method, inline_level, bci, result, msg); 573 } 574 } 575