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 const char* comment, 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 _comment = comment; 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 // CompileTask::code/set_code 128 // 129 nmethod* CompileTask::code() const { 130 if (_code_handle == NULL) return NULL; 131 return _code_handle->code(); 132 } 133 134 void CompileTask::set_code(nmethod* nm) { 135 if (_code_handle == NULL && nm == NULL) return; 136 guarantee(_code_handle != NULL, ""); 137 _code_handle->set_code(nm); 138 if (nm == NULL) _code_handle = NULL; // drop the handle also 139 } 140 141 void CompileTask::mark_on_stack() { 142 // Mark these methods as something redefine classes cannot remove. 143 _method->set_on_stack(true); 144 if (_hot_method != NULL) { 145 _hot_method->set_on_stack(true); 146 } 147 } 148 149 // RedefineClasses support 150 void CompileTask::metadata_do(void f(Metadata*)) { 151 f(method()); 152 if (hot_method() != NULL && hot_method() != method()) { 153 f(hot_method()); 154 } 155 } 156 157 // ------------------------------------------------------------------ 158 // CompileTask::print_line_on_error 159 // 160 // This function is called by fatal error handler when the thread 161 // causing troubles is a compiler thread. 162 // 163 // Do not grab any lock, do not allocate memory. 164 // 165 // Otherwise it's the same as CompileTask::print_line() 166 // 167 void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) { 168 // print compiler name 169 st->print("%s:", CompileBroker::compiler_name(comp_level())); 170 print(st); 171 } 172 173 // ------------------------------------------------------------------ 174 // CompileTask::print_tty 175 void CompileTask::print_tty() { 176 ttyLocker ttyl; // keep the following output all in one block 177 // print compiler name if requested 178 if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler_name(comp_level())); 179 print(tty); 180 } 181 182 // ------------------------------------------------------------------ 183 // CompileTask::print_impl 184 void CompileTask::print_impl(outputStream* st, Method* method, int compile_id, int comp_level, 185 bool is_osr_method, int osr_bci, bool is_blocking, 186 const char* msg, bool short_form, bool cr) { 187 if (!short_form) { 188 st->print("%7d ", (int) st->time_stamp().milliseconds()); // print timestamp 189 } 190 // print compiler name if requested 191 if (CIPrintCompilerName) { 192 st->print("%s:", CompileBroker::compiler_name(comp_level)); 193 } 194 st->print("%4d ", compile_id); // print compilation number 195 196 // For unloaded methods the transition to zombie occurs after the 197 // method is cleared so it's impossible to report accurate 198 // information for that case. 199 bool is_synchronized = false; 200 bool has_exception_handler = false; 201 bool is_native = false; 202 if (method != NULL) { 203 is_synchronized = method->is_synchronized(); 204 has_exception_handler = method->has_exception_handler(); 205 is_native = method->is_native(); 206 } 207 // method attributes 208 const char compile_type = is_osr_method ? '%' : ' '; 209 const char sync_char = is_synchronized ? 's' : ' '; 210 const char exception_char = has_exception_handler ? '!' : ' '; 211 const char blocking_char = is_blocking ? 'b' : ' '; 212 const char native_char = is_native ? 'n' : ' '; 213 214 // print method attributes 215 st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char); 216 217 if (TieredCompilation) { 218 if (comp_level != -1) st->print("%d ", comp_level); 219 else st->print("- "); 220 } 221 st->print(" "); // more indent 222 223 if (method == NULL) { 224 st->print("(method)"); 225 } else { 226 method->print_short_name(st); 227 if (is_osr_method) { 228 st->print(" @ %d", osr_bci); 229 } 230 if (method->is_native()) 231 st->print(" (native)"); 232 else 233 st->print(" (%d bytes)", method->code_size()); 234 } 235 236 if (msg != NULL) { 237 st->print(" %s", msg); 238 } 239 if (cr) { 240 st->cr(); 241 } 242 } 243 244 void CompileTask::print_inline_indent(int inline_level, outputStream* st) { 245 // 1234567 246 st->print(" "); // print timestamp 247 // 1234 248 st->print(" "); // print compilation number 249 // %s!bn 250 st->print(" "); // print method attributes 251 if (TieredCompilation) { 252 st->print(" "); 253 } 254 st->print(" "); // more indent 255 st->print(" "); // initial inlining indent 256 for (int i = 0; i < inline_level; i++) st->print(" "); 257 } 258 259 // ------------------------------------------------------------------ 260 // CompileTask::print_compilation 261 void CompileTask::print(outputStream* st, const char* msg, bool short_form, bool cr) { 262 bool is_osr_method = osr_bci() != InvocationEntryBci; 263 print_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form, cr); 264 } 265 266 // ------------------------------------------------------------------ 267 // CompileTask::log_task 268 void CompileTask::log_task(xmlStream* log) { 269 Thread* thread = Thread::current(); 270 methodHandle method(thread, this->method()); 271 ResourceMark rm(thread); 272 273 // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'> 274 log->print(" compile_id='%d'", _compile_id); 275 if (_osr_bci != CompileBroker::standard_entry_bci) { 276 log->print(" compile_kind='osr'"); // same as nmethod::compile_kind 277 } // else compile_kind='c2c' 278 if (!method.is_null()) log->method(method); 279 if (_osr_bci != CompileBroker::standard_entry_bci) { 280 log->print(" osr_bci='%d'", _osr_bci); 281 } 282 // Always print the level in tiered. 283 if (_comp_level != CompLevel_highest_tier || TieredCompilation) { 284 log->print(" level='%d'", _comp_level); 285 } 286 if (_is_blocking) { 287 log->print(" blocking='1'"); 288 } 289 log->stamp(); 290 } 291 292 // ------------------------------------------------------------------ 293 // CompileTask::log_task_queued 294 void CompileTask::log_task_queued() { 295 Thread* thread = Thread::current(); 296 ttyLocker ttyl; 297 ResourceMark rm(thread); 298 299 xtty->begin_elem("task_queued"); 300 log_task(xtty); 301 if (_comment != NULL) { 302 xtty->print(" comment='%s'", _comment); 303 } 304 if (_hot_method != NULL) { 305 methodHandle hot(thread, _hot_method); 306 methodHandle method(thread, _method); 307 if (hot() != method()) { 308 xtty->method(hot); 309 } 310 } 311 if (_hot_count != 0) { 312 xtty->print(" hot_count='%d'", _hot_count); 313 } 314 xtty->end_elem(); 315 } 316 317 318 // ------------------------------------------------------------------ 319 // CompileTask::log_task_dequeued 320 void CompileTask::log_task_dequeued(const char* comment) { 321 if (LogCompilation && xtty != NULL) { 322 Thread* thread = Thread::current(); 323 ttyLocker ttyl; 324 ResourceMark rm(thread); 325 326 xtty->begin_elem("task_dequeued"); 327 log_task(xtty); 328 if (comment != NULL) { 329 xtty->print(" comment='%s'", comment); 330 } 331 xtty->end_elem(); 332 } 333 } 334 335 336 // ------------------------------------------------------------------ 337 // CompileTask::log_task_start 338 void CompileTask::log_task_start(CompileLog* log) { 339 log->begin_head("task"); 340 log_task(log); 341 log->end_head(); 342 } 343 344 345 // ------------------------------------------------------------------ 346 // CompileTask::log_task_done 347 void CompileTask::log_task_done(CompileLog* log) { 348 Thread* thread = Thread::current(); 349 methodHandle method(thread, this->method()); 350 ResourceMark rm(thread); 351 352 if (!_is_success) { 353 const char* reason = _failure_reason != NULL ? _failure_reason : "unknown"; 354 log->elem("failure reason='%s'", reason); 355 } 356 357 // <task_done ... stamp='1.234'> </task> 358 nmethod* nm = code(); 359 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", 360 _is_success, nm == NULL ? 0 : nm->content_size(), 361 method->invocation_count()); 362 int bec = method->backedge_count(); 363 if (bec != 0) log->print(" backedge_count='%d'", bec); 364 // Note: "_is_complete" is about to be set, but is not. 365 if (_num_inlined_bytecodes != 0) { 366 log->print(" inlined_bytes='%d'", _num_inlined_bytecodes); 367 } 368 log->stamp(); 369 log->end_elem(); 370 log->clear_identities(); // next task will have different CI 371 log->tail("task"); 372 if (log->unflushed_count() > 2000) { 373 log->flush(); 374 } 375 log->mark_file_end(); 376 } 377 378 // ------------------------------------------------------------------ 379 // CompileTask::check_break_at_flags 380 bool CompileTask::check_break_at_flags() { 381 int compile_id = this->_compile_id; 382 bool is_osr = (_osr_bci != CompileBroker::standard_entry_bci); 383 384 if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) { 385 return true; 386 } else { 387 return (compile_id == CIBreakAt); 388 } 389 } 390 391 // ------------------------------------------------------------------ 392 // CompileTask::print_inlining 393 void CompileTask::print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) { 394 // 1234567 395 st->print(" "); // print timestamp 396 // 1234 397 st->print(" "); // print compilation number 398 399 // method attributes 400 if (method->is_loaded()) { 401 const char sync_char = method->is_synchronized() ? 's' : ' '; 402 const char exception_char = method->has_exception_handlers() ? '!' : ' '; 403 const char monitors_char = method->has_monitor_bytecodes() ? 'm' : ' '; 404 405 // print method attributes 406 st->print(" %c%c%c ", sync_char, exception_char, monitors_char); 407 } else { 408 // %s!bn 409 st->print(" "); // print method attributes 410 } 411 412 if (TieredCompilation) { 413 st->print(" "); 414 } 415 st->print(" "); // more indent 416 st->print(" "); // initial inlining indent 417 418 for (int i = 0; i < inline_level; i++) st->print(" "); 419 420 st->print("@ %d ", bci); // print bci 421 method->print_short_name(st); 422 if (method->is_loaded()) 423 st->print(" (%d bytes)", method->code_size()); 424 else 425 st->print(" (not loaded)"); 426 427 if (msg != NULL) { 428 st->print(" %s", msg); 429 } 430 st->cr(); 431 }