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