1 /* 2 * Copyright (c) 1998, 2017, 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 Thread* thread = Thread::current(); 90 _compile_id = compile_id; 91 _method = method(); 92 _method_holder = JNIHandles::make_global(Handle(thread, method->method_holder()->klass_holder())); 93 _osr_bci = osr_bci; 94 _is_blocking = is_blocking; 95 JVMCI_ONLY(_has_waiter = CompileBroker::compiler(comp_level)->is_jvmci();) 96 JVMCI_ONLY(_jvmci_compiler_thread = NULL;) 97 _comp_level = comp_level; 98 _num_inlined_bytecodes = 0; 99 100 _is_complete = false; 101 _is_success = false; 102 _code_handle = NULL; 103 104 _hot_method = NULL; 105 _hot_method_holder = NULL; 106 _hot_count = hot_count; 107 _time_queued = 0; // tidy 108 _compile_reason = compile_reason; 109 _failure_reason = NULL; 110 111 if (LogCompilation) { 112 _time_queued = os::elapsed_counter(); 113 if (hot_method.not_null()) { 114 if (hot_method == method) { 115 _hot_method = _method; 116 } else { 117 _hot_method = hot_method(); 118 // only add loader or mirror if different from _method_holder 119 _hot_method_holder = JNIHandles::make_global(Handle(thread, hot_method->method_holder()->klass_holder())); 120 } 121 } 122 } 123 124 _next = NULL; 125 } 126 127 /** 128 * Returns the compiler for this task. 129 */ 130 AbstractCompiler* CompileTask::compiler() { 131 return CompileBroker::compiler(_comp_level); 132 } 133 134 // ------------------------------------------------------------------ 135 // CompileTask::code/set_code 136 // 137 nmethod* CompileTask::code() const { 138 if (_code_handle == NULL) return NULL; 139 CodeBlob *blob = _code_handle->code(); 140 if (blob != NULL) { 141 return blob->as_nmethod(); 142 } 143 return NULL; 144 } 145 146 void CompileTask::set_code(nmethod* nm) { 147 if (_code_handle == NULL && nm == NULL) return; 148 guarantee(_code_handle != NULL, ""); 149 _code_handle->set_code(nm); 150 if (nm == NULL) _code_handle = NULL; // drop the handle also 151 } 152 153 void CompileTask::mark_on_stack() { 154 // Mark these methods as something redefine classes cannot remove. 155 _method->set_on_stack(true); 156 if (_hot_method != NULL) { 157 _hot_method->set_on_stack(true); 158 } 159 } 160 161 // RedefineClasses support 162 void CompileTask::metadata_do(void f(Metadata*)) { 163 f(method()); 164 if (hot_method() != NULL && hot_method() != method()) { 165 f(hot_method()); 166 } 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:", CompileBroker::compiler_name(comp_level())); 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 compiler name if requested 190 if (CIPrintCompilerName) { 191 tty->print("%s:", CompileBroker::compiler_name(comp_level())); 192 } 193 print(tty); 194 } 195 196 // ------------------------------------------------------------------ 197 // CompileTask::print_impl 198 void CompileTask::print_impl(outputStream* st, Method* method, int compile_id, int comp_level, 199 bool is_osr_method, int osr_bci, bool is_blocking, 200 const char* msg, bool short_form, bool cr) { 201 if (!short_form) { 202 st->print("%7d ", (int) st->time_stamp().milliseconds()); // print timestamp 203 } 204 // print compiler name if requested 205 if (CIPrintCompilerName) { 206 st->print("%s:", CompileBroker::compiler_name(comp_level)); 207 } 208 st->print("%4d ", compile_id); // print compilation number 209 210 // For unloaded methods the transition to zombie occurs after the 211 // method is cleared so it's impossible to report accurate 212 // information for that case. 213 bool is_synchronized = false; 214 bool has_exception_handler = false; 215 bool is_native = false; 216 if (method != NULL) { 217 is_synchronized = method->is_synchronized(); 218 has_exception_handler = method->has_exception_handler(); 219 is_native = method->is_native(); 220 } 221 // method attributes 222 const char compile_type = is_osr_method ? '%' : ' '; 223 const char sync_char = is_synchronized ? 's' : ' '; 224 const char exception_char = has_exception_handler ? '!' : ' '; 225 const char blocking_char = is_blocking ? 'b' : ' '; 226 const char native_char = is_native ? 'n' : ' '; 227 228 // print method attributes 229 st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char); 230 231 if (TieredCompilation) { 232 if (comp_level != -1) st->print("%d ", comp_level); 233 else st->print("- "); 234 } 235 st->print(" "); // more indent 236 237 if (method == NULL) { 238 st->print("(method)"); 239 } else { 240 method->print_short_name(st); 241 if (is_osr_method) { 242 st->print(" @ %d", osr_bci); 243 } 244 if (method->is_native()) 245 st->print(" (native)"); 246 else 247 st->print(" (%d bytes)", method->code_size()); 248 } 249 250 if (msg != NULL) { 251 st->print(" %s", msg); 252 } 253 if (cr) { 254 st->cr(); 255 } 256 } 257 258 void CompileTask::print_inline_indent(int inline_level, outputStream* st) { 259 // 1234567 260 st->print(" "); // print timestamp 261 // 1234 262 st->print(" "); // print compilation number 263 // %s!bn 264 st->print(" "); // print method attributes 265 if (TieredCompilation) { 266 st->print(" "); 267 } 268 st->print(" "); // more indent 269 st->print(" "); // initial inlining indent 270 for (int i = 0; i < inline_level; i++) st->print(" "); 271 } 272 273 // ------------------------------------------------------------------ 274 // CompileTask::print_compilation 275 void CompileTask::print(outputStream* st, const char* msg, bool short_form, bool cr) { 276 bool is_osr_method = osr_bci() != InvocationEntryBci; 277 print_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form, cr); 278 } 279 280 // ------------------------------------------------------------------ 281 // CompileTask::log_task 282 void CompileTask::log_task(xmlStream* log) { 283 Thread* thread = Thread::current(); 284 methodHandle method(thread, this->method()); 285 ResourceMark rm(thread); 286 287 // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'> 288 log->print(" compile_id='%d'", _compile_id); 289 if (_osr_bci != CompileBroker::standard_entry_bci) { 290 log->print(" compile_kind='osr'"); // same as nmethod::compile_kind 291 } // else compile_kind='c2c' 292 if (!method.is_null()) log->method(method); 293 if (_osr_bci != CompileBroker::standard_entry_bci) { 294 log->print(" osr_bci='%d'", _osr_bci); 295 } 296 if (_comp_level != CompLevel_highest_tier) { 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_start 333 void CompileTask::log_task_start(CompileLog* log) { 334 log->begin_head("task"); 335 log_task(log); 336 log->end_head(); 337 } 338 339 340 // ------------------------------------------------------------------ 341 // CompileTask::log_task_done 342 void CompileTask::log_task_done(CompileLog* log) { 343 Thread* thread = Thread::current(); 344 methodHandle method(thread, this->method()); 345 ResourceMark rm(thread); 346 347 if (!_is_success) { 348 const char* reason = _failure_reason != NULL ? _failure_reason : "unknown"; 349 log->elem("failure reason='%s'", reason); 350 } 351 352 // <task_done ... stamp='1.234'> </task> 353 nmethod* nm = code(); 354 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", 355 _is_success, nm == NULL ? 0 : nm->content_size(), 356 method->invocation_count()); 357 int bec = method->backedge_count(); 358 if (bec != 0) log->print(" backedge_count='%d'", bec); 359 // Note: "_is_complete" is about to be set, but is not. 360 if (_num_inlined_bytecodes != 0) { 361 log->print(" inlined_bytes='%d'", _num_inlined_bytecodes); 362 } 363 log->stamp(); 364 log->end_elem(); 365 log->clear_identities(); // next task will have different CI 366 log->tail("task"); 367 if (log->unflushed_count() > 2000) { 368 log->flush(); 369 } 370 log->mark_file_end(); 371 } 372 373 // ------------------------------------------------------------------ 374 // CompileTask::check_break_at_flags 375 bool CompileTask::check_break_at_flags() { 376 int compile_id = this->_compile_id; 377 bool is_osr = (_osr_bci != CompileBroker::standard_entry_bci); 378 379 if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) { 380 return true; 381 } else { 382 return (compile_id == CIBreakAt); 383 } 384 } 385 386 // ------------------------------------------------------------------ 387 // CompileTask::print_inlining 388 void CompileTask::print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) { 389 // 1234567 390 st->print(" "); // print timestamp 391 // 1234 392 st->print(" "); // print compilation number 393 394 // method attributes 395 if (method->is_loaded()) { 396 const char sync_char = method->is_synchronized() ? 's' : ' '; 397 const char exception_char = method->has_exception_handlers() ? '!' : ' '; 398 const char monitors_char = method->has_monitor_bytecodes() ? 'm' : ' '; 399 400 // print method attributes 401 st->print(" %c%c%c ", sync_char, exception_char, monitors_char); 402 } else { 403 // %s!bn 404 st->print(" "); // print method attributes 405 } 406 407 if (TieredCompilation) { 408 st->print(" "); 409 } 410 st->print(" "); // more indent 411 st->print(" "); // initial inlining indent 412 413 for (int i = 0; i < inline_level; i++) st->print(" "); 414 415 st->print("@ %d ", bci); // print bci 416 method->print_short_name(st); 417 if (method->is_loaded()) 418 st->print(" (%d bytes)", method->code_size()); 419 else 420 st->print(" (not loaded)"); 421 422 if (msg != NULL) { 423 st->print(" %s", msg); 424 } 425 st->cr(); 426 } 427 428