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