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