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