1 /* 2 * Copyright (c) 1998, 2018, 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/compileTask.hpp" 27 #include "compiler/compileLog.hpp" 28 #include "compiler/compileBroker.hpp" 29 #include "compiler/compilerDirectives.hpp" 30 #include "logging/log.hpp" 31 #include "logging/logStream.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "runtime/handles.inline.hpp" 34 35 CompileTask* CompileTask::_task_free_list = NULL; 36 #ifdef ASSERT 37 int CompileTask::_num_allocated_tasks = 0; 38 #endif 39 40 /** 41 * Allocate a CompileTask, from the free list if possible. 42 */ 43 CompileTask* CompileTask::allocate() { 44 MutexLocker locker(CompileTaskAlloc_lock); 45 CompileTask* task = NULL; 46 47 if (_task_free_list != NULL) { 48 task = _task_free_list; 49 _task_free_list = task->next(); 50 task->set_next(NULL); 51 } else { 52 task = new CompileTask(); 53 DEBUG_ONLY(_num_allocated_tasks++;) 54 assert (WhiteBoxAPI || JVMCI_ONLY(UseJVMCICompiler ||) _num_allocated_tasks < 10000, "Leaking compilation tasks?"); 55 task->set_next(NULL); 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 67 void CompileTask::free(CompileTask* task) { 68 MutexLocker locker(CompileTaskAlloc_lock); 69 if (!task->is_free()) { 70 task->set_code(NULL); 71 assert(!task->lock()->is_locked(), "Should not be locked when freed"); 72 JNIHandles::destroy_global(task->_method_holder); 73 JNIHandles::destroy_global(task->_hot_method_holder); 74 75 task->set_is_free(true); 76 task->set_next(_task_free_list); 77 _task_free_list = task; 78 } 79 } 80 81 82 void CompileTask::initialize(int compile_id, 83 const methodHandle& method, 84 int osr_bci, 85 int comp_level, 86 const methodHandle& hot_method, 87 int hot_count, 88 CompileTask::CompileReason compile_reason, 89 bool is_blocking) { 90 assert(!_lock->is_locked(), "bad locking"); 91 92 Thread* thread = Thread::current(); 93 _compile_id = compile_id; 94 _method = method(); 95 _method_holder = JNIHandles::make_global(Handle(thread, method->method_holder()->klass_holder())); 96 _osr_bci = osr_bci; 97 _is_blocking = is_blocking; 98 JVMCI_ONLY(_has_waiter = CompileBroker::compiler(comp_level)->is_jvmci();) 99 JVMCI_ONLY(_jvmci_compiler_thread = NULL;) 100 _comp_level = comp_level; 101 _num_inlined_bytecodes = 0; 102 103 _is_complete = false; 104 _is_success = false; 105 _code_handle = NULL; 106 107 _hot_method = NULL; 108 _hot_method_holder = NULL; 109 _hot_count = hot_count; 110 _time_queued = 0; // tidy 111 _compile_reason = compile_reason; 112 _failure_reason = NULL; 113 114 if (LogCompilation) { 115 _time_queued = os::elapsed_counter(); 116 if (hot_method.not_null()) { 117 if (hot_method == method) { 118 _hot_method = _method; 119 } else { 120 _hot_method = hot_method(); 121 // only add loader or mirror if different from _method_holder 122 _hot_method_holder = JNIHandles::make_global(Handle(thread, hot_method->method_holder()->klass_holder())); 123 } 124 } 125 } 126 127 _next = NULL; 128 } 129 130 /** 131 * Returns the compiler for this task. 132 */ 133 AbstractCompiler* CompileTask::compiler() { 134 return CompileBroker::compiler(_comp_level); 135 } 136 137 // ------------------------------------------------------------------ 138 // CompileTask::code/set_code 139 // 140 nmethod* CompileTask::code() const { 141 if (_code_handle == NULL) return NULL; 142 CodeBlob *blob = _code_handle->code(); 143 if (blob != NULL) { 144 return blob->as_nmethod(); 145 } 146 return NULL; 147 } 148 149 void CompileTask::set_code(nmethod* nm) { 150 if (_code_handle == NULL && nm == NULL) return; 151 guarantee(_code_handle != NULL, ""); 152 _code_handle->set_code(nm); 153 if (nm == NULL) _code_handle = NULL; // drop the handle also 154 } 155 156 void CompileTask::mark_on_stack() { 157 // Mark these methods as something redefine classes cannot remove. 158 _method->set_on_stack(true); 159 if (_hot_method != NULL) { 160 _hot_method->set_on_stack(true); 161 } 162 } 163 164 // RedefineClasses support 165 void CompileTask::metadata_do(void f(Metadata*)) { 166 f(method()); 167 if (hot_method() != NULL && hot_method() != method()) { 168 f(hot_method()); 169 } 170 } 171 172 // ------------------------------------------------------------------ 173 // CompileTask::print_line_on_error 174 // 175 // This function is called by fatal error handler when the thread 176 // causing troubles is a compiler thread. 177 // 178 // Do not grab any lock, do not allocate memory. 179 // 180 // Otherwise it's the same as CompileTask::print_line() 181 // 182 void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) { 183 // print compiler name 184 st->print("%s:", CompileBroker::compiler_name(comp_level())); 185 print(st); 186 } 187 188 // ------------------------------------------------------------------ 189 // CompileTask::print_tty 190 void CompileTask::print_tty() { 191 ttyLocker ttyl; // keep the following output all in one block 192 // print compiler name if requested 193 if (CIPrintCompilerName) { 194 tty->print("%s:", CompileBroker::compiler_name(comp_level())); 195 } 196 print(tty); 197 } 198 199 // ------------------------------------------------------------------ 200 // CompileTask::print_impl 201 void CompileTask::print_impl(outputStream* st, Method* method, int compile_id, int comp_level, 202 bool is_osr_method, int osr_bci, bool is_blocking, 203 const char* msg, bool short_form, bool cr) { 204 if (!short_form) { 205 st->print("%7d ", (int) st->time_stamp().milliseconds()); // print timestamp 206 } 207 // print compiler name if requested 208 if (CIPrintCompilerName) { 209 st->print("%s:", CompileBroker::compiler_name(comp_level)); 210 } 211 st->print("%4d ", compile_id); // print compilation number 212 213 // For unloaded methods the transition to zombie occurs after the 214 // method is cleared so it's impossible to report accurate 215 // information for that case. 216 bool is_synchronized = false; 217 bool has_exception_handler = false; 218 bool is_native = false; 219 if (method != NULL) { 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 == NULL) { 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 != NULL) { 254 st->print(" %s", msg); 255 } 256 if (cr) { 257 st->cr(); 258 } 259 } 260 261 void CompileTask::print_inline_indent(int inline_level, outputStream* st) { 262 // 1234567 263 st->print(" "); // print timestamp 264 // 1234 265 st->print(" "); // print compilation number 266 // %s!bn 267 st->print(" "); // print method attributes 268 if (TieredCompilation) { 269 st->print(" "); 270 } 271 st->print(" "); // more indent 272 st->print(" "); // initial inlining indent 273 for (int i = 0; i < inline_level; i++) st->print(" "); 274 } 275 276 // ------------------------------------------------------------------ 277 // CompileTask::print_compilation 278 void CompileTask::print(outputStream* st, const char* msg, bool short_form, bool cr) { 279 bool is_osr_method = osr_bci() != InvocationEntryBci; 280 print_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form, cr); 281 } 282 283 // ------------------------------------------------------------------ 284 // CompileTask::log_task 285 void CompileTask::log_task(xmlStream* log) { 286 Thread* thread = Thread::current(); 287 methodHandle method(thread, this->method()); 288 ResourceMark rm(thread); 289 290 // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'> 291 log->print(" compile_id='%d'", _compile_id); 292 if (_osr_bci != CompileBroker::standard_entry_bci) { 293 log->print(" compile_kind='osr'"); // same as nmethod::compile_kind 294 } // else compile_kind='c2c' 295 if (!method.is_null()) log->method(method); 296 if (_osr_bci != CompileBroker::standard_entry_bci) { 297 log->print(" osr_bci='%d'", _osr_bci); 298 } 299 if (_comp_level != CompLevel_highest_tier) { 300 log->print(" level='%d'", _comp_level); 301 } 302 if (_is_blocking) { 303 log->print(" blocking='1'"); 304 } 305 log->stamp(); 306 } 307 308 // ------------------------------------------------------------------ 309 // CompileTask::log_task_queued 310 void CompileTask::log_task_queued() { 311 Thread* thread = Thread::current(); 312 ttyLocker ttyl; 313 ResourceMark rm(thread); 314 315 xtty->begin_elem("task_queued"); 316 log_task(xtty); 317 assert(_compile_reason > CompileTask::Reason_None && _compile_reason < CompileTask::Reason_Count, "Valid values"); 318 xtty->print(" comment='%s'", reason_name(_compile_reason)); 319 320 if (_hot_method != NULL) { 321 methodHandle hot(thread, _hot_method); 322 methodHandle method(thread, _method); 323 if (hot() != method()) { 324 xtty->method(hot); 325 } 326 } 327 if (_hot_count != 0) { 328 xtty->print(" hot_count='%d'", _hot_count); 329 } 330 xtty->end_elem(); 331 } 332 333 334 // ------------------------------------------------------------------ 335 // CompileTask::log_task_start 336 void CompileTask::log_task_start(CompileLog* log) { 337 log->begin_head("task"); 338 log_task(log); 339 log->end_head(); 340 } 341 342 343 // ------------------------------------------------------------------ 344 // CompileTask::log_task_done 345 void CompileTask::log_task_done(CompileLog* log) { 346 Thread* thread = Thread::current(); 347 methodHandle method(thread, this->method()); 348 ResourceMark rm(thread); 349 350 if (!_is_success) { 351 const char* reason = _failure_reason != NULL ? _failure_reason : "unknown"; 352 log->elem("failure reason='%s'", reason); 353 } 354 355 // <task_done ... stamp='1.234'> </task> 356 nmethod* nm = code(); 357 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", 358 _is_success, nm == NULL ? 0 : nm->content_size(), 359 method->invocation_count()); 360 int bec = method->backedge_count(); 361 if (bec != 0) log->print(" backedge_count='%d'", bec); 362 // Note: "_is_complete" is about to be set, but is not. 363 if (_num_inlined_bytecodes != 0) { 364 log->print(" inlined_bytes='%d'", _num_inlined_bytecodes); 365 } 366 log->stamp(); 367 log->end_elem(); 368 log->clear_identities(); // next task will have different CI 369 log->tail("task"); 370 if (log->unflushed_count() > 2000) { 371 log->flush(); 372 } 373 log->mark_file_end(); 374 } 375 376 // ------------------------------------------------------------------ 377 // CompileTask::check_break_at_flags 378 bool CompileTask::check_break_at_flags() { 379 int compile_id = this->_compile_id; 380 bool is_osr = (_osr_bci != CompileBroker::standard_entry_bci); 381 382 if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) { 383 return true; 384 } else { 385 return (compile_id == CIBreakAt); 386 } 387 } 388 389 // ------------------------------------------------------------------ 390 // CompileTask::print_inlining 391 void CompileTask::print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) { 392 // 1234567 393 st->print(" "); // print timestamp 394 // 1234 395 st->print(" "); // print compilation number 396 397 // method attributes 398 if (method->is_loaded()) { 399 const char sync_char = method->is_synchronized() ? 's' : ' '; 400 const char exception_char = method->has_exception_handlers() ? '!' : ' '; 401 const char monitors_char = method->has_monitor_bytecodes() ? 'm' : ' '; 402 403 // print method attributes 404 st->print(" %c%c%c ", sync_char, exception_char, monitors_char); 405 } else { 406 // %s!bn 407 st->print(" "); // print method attributes 408 } 409 410 if (TieredCompilation) { 411 st->print(" "); 412 } 413 st->print(" "); // more indent 414 st->print(" "); // initial inlining indent 415 416 for (int i = 0; i < inline_level; i++) st->print(" "); 417 418 st->print("@ %d ", bci); // print bci 419 method->print_short_name(st); 420 if (method->is_loaded()) 421 st->print(" (%d bytes)", method->code_size()); 422 else 423 st->print(" (not loaded)"); 424 425 if (msg != NULL) { 426 st->print(" %s", msg); 427 } 428 st->cr(); 429 } 430 431 void CompileTask::print_ul(const char* msg){ 432 LogTarget(Debug, jit, compilation) lt; 433 if (lt.is_enabled()) { 434 LogStream ls(lt); 435 print(&ls, msg, /* short form */ true, /* cr */ true); 436 } 437 } 438 439 void CompileTask::print_ul(const nmethod* nm, const char* msg) { 440 LogTarget(Debug, jit, compilation) lt; 441 if (lt.is_enabled()) { 442 LogStream ls(lt); 443 print_impl(&ls, nm->method(), nm->compile_id(), 444 nm->comp_level(), nm->is_osr_method(), 445 nm->is_osr_method() ? nm->osr_entry_bci() : -1, 446 /*is_blocking*/ false, 447 msg, /* short form */ true, /* cr */ true); 448 } 449 } 450 451 void CompileTask::print_inlining_ul(ciMethod* method, int inline_level, int bci, const char* msg) { 452 LogTarget(Debug, jit, inlining) lt; 453 if (lt.is_enabled()) { 454 LogStream ls(lt); 455 print_inlining_inner(&ls, method, inline_level, bci, msg); 456 } 457 } 458