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 if (_comp_level != CompLevel_highest_tier) { 296 log->print(" level='%d'", _comp_level); 297 } 298 if (_is_blocking) { 299 log->print(" blocking='1'"); 300 } 301 log->stamp(); 302 } 303 304 // ------------------------------------------------------------------ 305 // CompileTask::log_task_queued 306 void CompileTask::log_task_queued() { 307 Thread* thread = Thread::current(); 308 ttyLocker ttyl; 309 ResourceMark rm(thread); 310 311 xtty->begin_elem("task_queued"); 312 log_task(xtty); 313 assert(_compile_reason > CompileTask::Reason_None && _compile_reason < CompileTask::Reason_Count, "Valid values"); 314 xtty->print(" comment='%s'", reason_name(_compile_reason)); 315 316 if (_hot_method != NULL) { 317 methodHandle hot(thread, _hot_method); 318 methodHandle method(thread, _method); 319 if (hot() != method()) { 320 xtty->method(hot); 321 } 322 } 323 if (_hot_count != 0) { 324 xtty->print(" hot_count='%d'", _hot_count); 325 } 326 xtty->end_elem(); 327 } 328 329 330 // ------------------------------------------------------------------ 331 // CompileTask::log_task_start 332 void CompileTask::log_task_start(CompileLog* log) { 333 log->begin_head("task"); 334 log_task(log); 335 log->end_head(); 336 } 337 338 339 // ------------------------------------------------------------------ 340 // CompileTask::log_task_done 341 void CompileTask::log_task_done(CompileLog* log) { 342 Thread* thread = Thread::current(); 343 methodHandle method(thread, this->method()); 344 ResourceMark rm(thread); 345 346 if (!_is_success) { 347 const char* reason = _failure_reason != NULL ? _failure_reason : "unknown"; 348 log->elem("failure reason='%s'", reason); 349 } 350 351 // <task_done ... stamp='1.234'> </task> 352 nmethod* nm = code(); 353 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", 354 _is_success, nm == NULL ? 0 : nm->content_size(), 355 method->invocation_count()); 356 int bec = method->backedge_count(); 357 if (bec != 0) log->print(" backedge_count='%d'", bec); 358 // Note: "_is_complete" is about to be set, but is not. 359 if (_num_inlined_bytecodes != 0) { 360 log->print(" inlined_bytes='%d'", _num_inlined_bytecodes); 361 } 362 log->stamp(); 363 log->end_elem(); 364 log->clear_identities(); // next task will have different CI 365 log->tail("task"); 366 if (log->unflushed_count() > 2000) { 367 log->flush(); 368 } 369 log->mark_file_end(); 370 } 371 372 // ------------------------------------------------------------------ 373 // CompileTask::check_break_at_flags 374 bool CompileTask::check_break_at_flags() { 375 int compile_id = this->_compile_id; 376 bool is_osr = (_osr_bci != CompileBroker::standard_entry_bci); 377 378 if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) { 379 return true; 380 } else { 381 return (compile_id == CIBreakAt); 382 } 383 } 384 385 // ------------------------------------------------------------------ 386 // CompileTask::print_inlining 387 void CompileTask::print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) { 388 // 1234567 389 st->print(" "); // print timestamp 390 // 1234 391 st->print(" "); // print compilation number 392 393 // method attributes 394 if (method->is_loaded()) { 395 const char sync_char = method->is_synchronized() ? 's' : ' '; 396 const char exception_char = method->has_exception_handlers() ? '!' : ' '; 397 const char monitors_char = method->has_monitor_bytecodes() ? 'm' : ' '; 398 399 // print method attributes 400 st->print(" %c%c%c ", sync_char, exception_char, monitors_char); 401 } else { 402 // %s!bn 403 st->print(" "); // print method attributes 404 } 405 406 if (TieredCompilation) { 407 st->print(" "); 408 } 409 st->print(" "); // more indent 410 st->print(" "); // initial inlining indent 411 412 for (int i = 0; i < inline_level; i++) st->print(" "); 413 414 st->print("@ %d ", bci); // print bci 415 method->print_short_name(st); 416 if (method->is_loaded()) 417 st->print(" (%d bytes)", method->code_size()); 418 else 419 st->print(" (not loaded)"); 420 421 if (msg != NULL) { 422 st->print(" %s", msg); 423 } 424 st->cr(); 425 } 426 427