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