compileBroker.cpp revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 1999, 2010, 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 "incls/_precompiled.incl" 26#include "incls/_compileBroker.cpp.incl" 27 28#ifdef DTRACE_ENABLED 29 30// Only bother with this argument setup if dtrace is available 31 32HS_DTRACE_PROBE_DECL8(hotspot, method__compile__begin, 33 char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t); 34HS_DTRACE_PROBE_DECL9(hotspot, method__compile__end, 35 char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t, bool); 36 37#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(compiler, method) \ 38 { \ 39 char* comp_name = (char*)(compiler)->name(); \ 40 symbolOop klass_name = (method)->klass_name(); \ 41 symbolOop name = (method)->name(); \ 42 symbolOop signature = (method)->signature(); \ 43 HS_DTRACE_PROBE8(hotspot, method__compile__begin, \ 44 comp_name, strlen(comp_name), \ 45 klass_name->bytes(), klass_name->utf8_length(), \ 46 name->bytes(), name->utf8_length(), \ 47 signature->bytes(), signature->utf8_length()); \ 48 } 49 50#define DTRACE_METHOD_COMPILE_END_PROBE(compiler, method, success) \ 51 { \ 52 char* comp_name = (char*)(compiler)->name(); \ 53 symbolOop klass_name = (method)->klass_name(); \ 54 symbolOop name = (method)->name(); \ 55 symbolOop signature = (method)->signature(); \ 56 HS_DTRACE_PROBE9(hotspot, method__compile__end, \ 57 comp_name, strlen(comp_name), \ 58 klass_name->bytes(), klass_name->utf8_length(), \ 59 name->bytes(), name->utf8_length(), \ 60 signature->bytes(), signature->utf8_length(), (success)); \ 61 } 62 63#else // ndef DTRACE_ENABLED 64 65#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(compiler, method) 66#define DTRACE_METHOD_COMPILE_END_PROBE(compiler, method, success) 67 68#endif // ndef DTRACE_ENABLED 69 70bool CompileBroker::_initialized = false; 71volatile bool CompileBroker::_should_block = false; 72volatile jint CompileBroker::_should_compile_new_jobs = run_compilation; 73 74// The installed compiler(s) 75AbstractCompiler* CompileBroker::_compilers[2]; 76 77// These counters are used for assigning id's to each compilation 78uint CompileBroker::_compilation_id = 0; 79uint CompileBroker::_osr_compilation_id = 0; 80 81// Debugging information 82int CompileBroker::_last_compile_type = no_compile; 83int CompileBroker::_last_compile_level = CompLevel_none; 84char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length]; 85 86// Performance counters 87PerfCounter* CompileBroker::_perf_total_compilation = NULL; 88PerfCounter* CompileBroker::_perf_osr_compilation = NULL; 89PerfCounter* CompileBroker::_perf_standard_compilation = NULL; 90 91PerfCounter* CompileBroker::_perf_total_bailout_count = NULL; 92PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL; 93PerfCounter* CompileBroker::_perf_total_compile_count = NULL; 94PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL; 95PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL; 96 97PerfCounter* CompileBroker::_perf_sum_osr_bytes_compiled = NULL; 98PerfCounter* CompileBroker::_perf_sum_standard_bytes_compiled = NULL; 99PerfCounter* CompileBroker::_perf_sum_nmethod_size = NULL; 100PerfCounter* CompileBroker::_perf_sum_nmethod_code_size = NULL; 101 102PerfStringVariable* CompileBroker::_perf_last_method = NULL; 103PerfStringVariable* CompileBroker::_perf_last_failed_method = NULL; 104PerfStringVariable* CompileBroker::_perf_last_invalidated_method = NULL; 105PerfVariable* CompileBroker::_perf_last_compile_type = NULL; 106PerfVariable* CompileBroker::_perf_last_compile_size = NULL; 107PerfVariable* CompileBroker::_perf_last_failed_type = NULL; 108PerfVariable* CompileBroker::_perf_last_invalidated_type = NULL; 109 110// Timers and counters for generating statistics 111elapsedTimer CompileBroker::_t_total_compilation; 112elapsedTimer CompileBroker::_t_osr_compilation; 113elapsedTimer CompileBroker::_t_standard_compilation; 114 115int CompileBroker::_total_bailout_count = 0; 116int CompileBroker::_total_invalidated_count = 0; 117int CompileBroker::_total_compile_count = 0; 118int CompileBroker::_total_osr_compile_count = 0; 119int CompileBroker::_total_standard_compile_count = 0; 120 121int CompileBroker::_sum_osr_bytes_compiled = 0; 122int CompileBroker::_sum_standard_bytes_compiled = 0; 123int CompileBroker::_sum_nmethod_size = 0; 124int CompileBroker::_sum_nmethod_code_size = 0; 125 126CompileQueue* CompileBroker::_method_queue = NULL; 127CompileTask* CompileBroker::_task_free_list = NULL; 128 129GrowableArray<CompilerThread*>* CompileBroker::_method_threads = NULL; 130 131// CompileTaskWrapper 132// 133// Assign this task to the current thread. Deallocate the task 134// when the compilation is complete. 135class CompileTaskWrapper : StackObj { 136public: 137 CompileTaskWrapper(CompileTask* task); 138 ~CompileTaskWrapper(); 139}; 140 141CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) { 142 CompilerThread* thread = CompilerThread::current(); 143 thread->set_task(task); 144 CompileLog* log = thread->log(); 145 if (log != NULL) task->log_task_start(log); 146} 147 148CompileTaskWrapper::~CompileTaskWrapper() { 149 CompilerThread* thread = CompilerThread::current(); 150 CompileTask* task = thread->task(); 151 CompileLog* log = thread->log(); 152 if (log != NULL) task->log_task_done(log); 153 thread->set_task(NULL); 154 task->set_code_handle(NULL); 155 DEBUG_ONLY(thread->set_env((ciEnv*)badAddress)); 156 if (task->is_blocking()) { 157 MutexLocker notifier(task->lock(), thread); 158 task->mark_complete(); 159 // Notify the waiting thread that the compilation has completed. 160 task->lock()->notify_all(); 161 } else { 162 task->mark_complete(); 163 164 // By convention, the compiling thread is responsible for 165 // recycling a non-blocking CompileTask. 166 CompileBroker::free_task(task); 167 } 168} 169 170 171// ------------------------------------------------------------------ 172// CompileTask::initialize 173void CompileTask::initialize(int compile_id, 174 methodHandle method, 175 int osr_bci, 176 int comp_level, 177 methodHandle hot_method, 178 int hot_count, 179 const char* comment, 180 bool is_blocking) { 181 assert(!_lock->is_locked(), "bad locking"); 182 183 _compile_id = compile_id; 184 _method = JNIHandles::make_global(method); 185 _osr_bci = osr_bci; 186 _is_blocking = is_blocking; 187 _comp_level = comp_level; 188 _num_inlined_bytecodes = 0; 189 190 _is_complete = false; 191 _is_success = false; 192 _code_handle = NULL; 193 194 _hot_method = NULL; 195 _hot_count = hot_count; 196 _time_queued = 0; // tidy 197 _comment = comment; 198 199 if (LogCompilation) { 200 _time_queued = os::elapsed_counter(); 201 if (hot_method.not_null()) { 202 if (hot_method == method) { 203 _hot_method = _method; 204 } else { 205 _hot_method = JNIHandles::make_global(hot_method); 206 } 207 } 208 } 209 210 _next = NULL; 211} 212 213// ------------------------------------------------------------------ 214// CompileTask::code/set_code 215nmethod* CompileTask::code() const { 216 if (_code_handle == NULL) return NULL; 217 return _code_handle->code(); 218} 219void CompileTask::set_code(nmethod* nm) { 220 if (_code_handle == NULL && nm == NULL) return; 221 guarantee(_code_handle != NULL, ""); 222 _code_handle->set_code(nm); 223 if (nm == NULL) _code_handle = NULL; // drop the handle also 224} 225 226// ------------------------------------------------------------------ 227// CompileTask::free 228void CompileTask::free() { 229 set_code(NULL); 230 assert(!_lock->is_locked(), "Should not be locked when freed"); 231 if (_hot_method != NULL && _hot_method != _method) { 232 JNIHandles::destroy_global(_hot_method); 233 } 234 JNIHandles::destroy_global(_method); 235} 236 237 238// ------------------------------------------------------------------ 239// CompileTask::print 240void CompileTask::print() { 241 tty->print("<CompileTask compile_id=%d ", _compile_id); 242 tty->print("method="); 243 ((methodOop)JNIHandles::resolve(_method))->print_name(tty); 244 tty->print_cr(" osr_bci=%d is_blocking=%s is_complete=%s is_success=%s>", 245 _osr_bci, bool_to_str(_is_blocking), 246 bool_to_str(_is_complete), bool_to_str(_is_success)); 247} 248 249// ------------------------------------------------------------------ 250// CompileTask::print_line_on_error 251// 252// This function is called by fatal error handler when the thread 253// causing troubles is a compiler thread. 254// 255// Do not grab any lock, do not allocate memory. 256// 257// Otherwise it's the same as CompileTask::print_line() 258// 259void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) { 260 methodOop method = (methodOop)JNIHandles::resolve(_method); 261 262 // print compiler name 263 st->print("%s:", CompileBroker::compiler(comp_level())->name()); 264 265 // print compilation number 266 st->print("%3d", compile_id()); 267 268 // print method attributes 269 const bool is_osr = osr_bci() != CompileBroker::standard_entry_bci; 270 { const char blocking_char = is_blocking() ? 'b' : ' '; 271 const char compile_type = is_osr ? '%' : ' '; 272 const char sync_char = method->is_synchronized() ? 's' : ' '; 273 const char exception_char = method->has_exception_handler() ? '!' : ' '; 274 const char tier_char = 275 is_highest_tier_compile(comp_level()) ? ' ' : ('0' + comp_level()); 276 st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, tier_char); 277 } 278 279 // Use buf to get method name and signature 280 if (method != NULL) st->print("%s", method->name_and_sig_as_C_string(buf, buflen)); 281 282 // print osr_bci if any 283 if (is_osr) st->print(" @ %d", osr_bci()); 284 285 // print method size 286 st->print_cr(" (%d bytes)", method->code_size()); 287} 288 289// ------------------------------------------------------------------ 290// CompileTask::print_line 291void CompileTask::print_line() { 292 Thread *thread = Thread::current(); 293 methodHandle method(thread, 294 (methodOop)JNIHandles::resolve(method_handle())); 295 ResourceMark rm(thread); 296 297 ttyLocker ttyl; // keep the following output all in one block 298 299 // print compiler name if requested 300 if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler(comp_level())->name()); 301 302 // print compilation number 303 tty->print("%3d", compile_id()); 304 305 // print method attributes 306 const bool is_osr = osr_bci() != CompileBroker::standard_entry_bci; 307 { const char blocking_char = is_blocking() ? 'b' : ' '; 308 const char compile_type = is_osr ? '%' : ' '; 309 const char sync_char = method->is_synchronized() ? 's' : ' '; 310 const char exception_char = method->has_exception_handler() ? '!' : ' '; 311 const char tier_char = 312 is_highest_tier_compile(comp_level()) ? ' ' : ('0' + comp_level()); 313 tty->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, tier_char); 314 } 315 316 // print method name 317 method->print_short_name(tty); 318 319 // print osr_bci if any 320 if (is_osr) tty->print(" @ %d", osr_bci()); 321 322 // print method size 323 tty->print_cr(" (%d bytes)", method->code_size()); 324} 325 326 327// ------------------------------------------------------------------ 328// CompileTask::log_task 329void CompileTask::log_task(xmlStream* log) { 330 Thread* thread = Thread::current(); 331 methodHandle method(thread, 332 (methodOop)JNIHandles::resolve(method_handle())); 333 ResourceMark rm(thread); 334 335 // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'> 336 if (_compile_id != 0) log->print(" compile_id='%d'", _compile_id); 337 if (_osr_bci != CompileBroker::standard_entry_bci) { 338 log->print(" compile_kind='osr'"); // same as nmethod::compile_kind 339 } // else compile_kind='c2c' 340 if (!method.is_null()) log->method(method); 341 if (_osr_bci != CompileBroker::standard_entry_bci) { 342 log->print(" osr_bci='%d'", _osr_bci); 343 } 344 if (_comp_level != CompLevel_highest_tier) { 345 log->print(" level='%d'", _comp_level); 346 } 347 if (_is_blocking) { 348 log->print(" blocking='1'"); 349 } 350 log->stamp(); 351} 352 353 354// ------------------------------------------------------------------ 355// CompileTask::log_task_queued 356void CompileTask::log_task_queued() { 357 Thread* thread = Thread::current(); 358 ttyLocker ttyl; 359 ResourceMark rm(thread); 360 361 xtty->begin_elem("task_queued"); 362 log_task(xtty); 363 if (_comment != NULL) { 364 xtty->print(" comment='%s'", _comment); 365 } 366 if (_hot_method != NULL) { 367 methodHandle hot(thread, 368 (methodOop)JNIHandles::resolve(_hot_method)); 369 methodHandle method(thread, 370 (methodOop)JNIHandles::resolve(_method)); 371 if (hot() != method()) { 372 xtty->method(hot); 373 } 374 } 375 if (_hot_count != 0) { 376 xtty->print(" hot_count='%d'", _hot_count); 377 } 378 xtty->end_elem(); 379} 380 381 382// ------------------------------------------------------------------ 383// CompileTask::log_task_start 384void CompileTask::log_task_start(CompileLog* log) { 385 log->begin_head("task"); 386 log_task(log); 387 log->end_head(); 388} 389 390 391// ------------------------------------------------------------------ 392// CompileTask::log_task_done 393void CompileTask::log_task_done(CompileLog* log) { 394 Thread* thread = Thread::current(); 395 methodHandle method(thread, 396 (methodOop)JNIHandles::resolve(method_handle())); 397 ResourceMark rm(thread); 398 399 // <task_done ... stamp='1.234'> </task> 400 nmethod* nm = code(); 401 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", 402 _is_success, nm == NULL ? 0 : nm->instructions_size(), 403 method->invocation_count()); 404 int bec = method->backedge_count(); 405 if (bec != 0) log->print(" backedge_count='%d'", bec); 406 // Note: "_is_complete" is about to be set, but is not. 407 if (_num_inlined_bytecodes != 0) { 408 log->print(" inlined_bytes='%d'", _num_inlined_bytecodes); 409 } 410 log->stamp(); 411 log->end_elem(); 412 log->tail("task"); 413 log->clear_identities(); // next task will have different CI 414 if (log->unflushed_count() > 2000) { 415 log->flush(); 416 } 417 log->mark_file_end(); 418} 419 420 421 422// ------------------------------------------------------------------ 423// CompileQueue::add 424// 425// Add a CompileTask to a CompileQueue 426void CompileQueue::add(CompileTask* task) { 427 assert(lock()->owned_by_self(), "must own lock"); 428 429 task->set_next(NULL); 430 431 if (_last == NULL) { 432 // The compile queue is empty. 433 assert(_first == NULL, "queue is empty"); 434 _first = task; 435 _last = task; 436 } else { 437 // Append the task to the queue. 438 assert(_last->next() == NULL, "not last"); 439 _last->set_next(task); 440 _last = task; 441 } 442 443 // Mark the method as being in the compile queue. 444 ((methodOop)JNIHandles::resolve(task->method_handle()))->set_queued_for_compilation(); 445 446 if (CIPrintCompileQueue) { 447 print(); 448 } 449 450 if (LogCompilation && xtty != NULL) { 451 task->log_task_queued(); 452 } 453 454 // Notify CompilerThreads that a task is available. 455 lock()->notify(); 456} 457 458 459// ------------------------------------------------------------------ 460// CompileQueue::get 461// 462// Get the next CompileTask from a CompileQueue 463CompileTask* CompileQueue::get() { 464 NMethodSweeper::possibly_sweep(); 465 466 MutexLocker locker(lock()); 467 468 // Wait for an available CompileTask. 469 while (_first == NULL) { 470 // There is no work to be done right now. Wait. 471 if (UseCodeCacheFlushing && (!CompileBroker::should_compile_new_jobs() || CodeCache::needs_flushing())) { 472 // During the emergency sweeping periods, wake up and sweep occasionally 473 bool timedout = lock()->wait(!Mutex::_no_safepoint_check_flag, NmethodSweepCheckInterval*1000); 474 if (timedout) { 475 MutexUnlocker ul(lock()); 476 // When otherwise not busy, run nmethod sweeping 477 NMethodSweeper::possibly_sweep(); 478 } 479 } else { 480 // During normal operation no need to wake up on timer 481 lock()->wait(); 482 } 483 } 484 485 CompileTask* task = _first; 486 487 // Update queue first and last 488 _first =_first->next(); 489 if (_first == NULL) { 490 _last = NULL; 491 } 492 493 return task; 494 495} 496 497 498// ------------------------------------------------------------------ 499// CompileQueue::print 500void CompileQueue::print() { 501 tty->print_cr("Contents of %s", name()); 502 tty->print_cr("----------------------"); 503 CompileTask* task = _first; 504 while (task != NULL) { 505 task->print_line(); 506 task = task->next(); 507 } 508 tty->print_cr("----------------------"); 509} 510 511CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) { 512 513 _current_method[0] = '\0'; 514 _compile_type = CompileBroker::no_compile; 515 516 if (UsePerfData) { 517 ResourceMark rm; 518 519 // create the thread instance name space string - don't create an 520 // instance subspace if instance is -1 - keeps the adapterThread 521 // counters from having a ".0" namespace. 522 const char* thread_i = (instance == -1) ? thread_name : 523 PerfDataManager::name_space(thread_name, instance); 524 525 526 char* name = PerfDataManager::counter_name(thread_i, "method"); 527 _perf_current_method = 528 PerfDataManager::create_string_variable(SUN_CI, name, 529 cmname_buffer_length, 530 _current_method, CHECK); 531 532 name = PerfDataManager::counter_name(thread_i, "type"); 533 _perf_compile_type = PerfDataManager::create_variable(SUN_CI, name, 534 PerfData::U_None, 535 (jlong)_compile_type, 536 CHECK); 537 538 name = PerfDataManager::counter_name(thread_i, "time"); 539 _perf_time = PerfDataManager::create_counter(SUN_CI, name, 540 PerfData::U_Ticks, CHECK); 541 542 name = PerfDataManager::counter_name(thread_i, "compiles"); 543 _perf_compiles = PerfDataManager::create_counter(SUN_CI, name, 544 PerfData::U_Events, CHECK); 545 } 546} 547 548 549// ------------------------------------------------------------------ 550// CompileBroker::compilation_init 551// 552// Initialize the Compilation object 553void CompileBroker::compilation_init() { 554 _last_method_compiled[0] = '\0'; 555 556 // Set the interface to the current compiler(s). 557#ifdef COMPILER1 558 _compilers[0] = new Compiler(); 559#ifndef COMPILER2 560 _compilers[1] = _compilers[0]; 561#endif 562#endif // COMPILER1 563 564#ifdef COMPILER2 565 _compilers[1] = new C2Compiler(); 566#ifndef COMPILER1 567 _compilers[0] = _compilers[1]; 568#endif 569#endif // COMPILER2 570 571 // Initialize the CompileTask free list 572 _task_free_list = NULL; 573 574 // Start the CompilerThreads 575 init_compiler_threads(compiler_count()); 576 577 578 // totalTime performance counter is always created as it is required 579 // by the implementation of java.lang.management.CompilationMBean. 580 { 581 EXCEPTION_MARK; 582 _perf_total_compilation = 583 PerfDataManager::create_counter(JAVA_CI, "totalTime", 584 PerfData::U_Ticks, CHECK); 585 } 586 587 588 if (UsePerfData) { 589 590 EXCEPTION_MARK; 591 592 // create the jvmstat performance counters 593 _perf_osr_compilation = 594 PerfDataManager::create_counter(SUN_CI, "osrTime", 595 PerfData::U_Ticks, CHECK); 596 597 _perf_standard_compilation = 598 PerfDataManager::create_counter(SUN_CI, "standardTime", 599 PerfData::U_Ticks, CHECK); 600 601 _perf_total_bailout_count = 602 PerfDataManager::create_counter(SUN_CI, "totalBailouts", 603 PerfData::U_Events, CHECK); 604 605 _perf_total_invalidated_count = 606 PerfDataManager::create_counter(SUN_CI, "totalInvalidates", 607 PerfData::U_Events, CHECK); 608 609 _perf_total_compile_count = 610 PerfDataManager::create_counter(SUN_CI, "totalCompiles", 611 PerfData::U_Events, CHECK); 612 _perf_total_osr_compile_count = 613 PerfDataManager::create_counter(SUN_CI, "osrCompiles", 614 PerfData::U_Events, CHECK); 615 616 _perf_total_standard_compile_count = 617 PerfDataManager::create_counter(SUN_CI, "standardCompiles", 618 PerfData::U_Events, CHECK); 619 620 _perf_sum_osr_bytes_compiled = 621 PerfDataManager::create_counter(SUN_CI, "osrBytes", 622 PerfData::U_Bytes, CHECK); 623 624 _perf_sum_standard_bytes_compiled = 625 PerfDataManager::create_counter(SUN_CI, "standardBytes", 626 PerfData::U_Bytes, CHECK); 627 628 _perf_sum_nmethod_size = 629 PerfDataManager::create_counter(SUN_CI, "nmethodSize", 630 PerfData::U_Bytes, CHECK); 631 632 _perf_sum_nmethod_code_size = 633 PerfDataManager::create_counter(SUN_CI, "nmethodCodeSize", 634 PerfData::U_Bytes, CHECK); 635 636 _perf_last_method = 637 PerfDataManager::create_string_variable(SUN_CI, "lastMethod", 638 CompilerCounters::cmname_buffer_length, 639 "", CHECK); 640 641 _perf_last_failed_method = 642 PerfDataManager::create_string_variable(SUN_CI, "lastFailedMethod", 643 CompilerCounters::cmname_buffer_length, 644 "", CHECK); 645 646 _perf_last_invalidated_method = 647 PerfDataManager::create_string_variable(SUN_CI, "lastInvalidatedMethod", 648 CompilerCounters::cmname_buffer_length, 649 "", CHECK); 650 651 _perf_last_compile_type = 652 PerfDataManager::create_variable(SUN_CI, "lastType", 653 PerfData::U_None, 654 (jlong)CompileBroker::no_compile, 655 CHECK); 656 657 _perf_last_compile_size = 658 PerfDataManager::create_variable(SUN_CI, "lastSize", 659 PerfData::U_Bytes, 660 (jlong)CompileBroker::no_compile, 661 CHECK); 662 663 664 _perf_last_failed_type = 665 PerfDataManager::create_variable(SUN_CI, "lastFailedType", 666 PerfData::U_None, 667 (jlong)CompileBroker::no_compile, 668 CHECK); 669 670 _perf_last_invalidated_type = 671 PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType", 672 PerfData::U_None, 673 (jlong)CompileBroker::no_compile, 674 CHECK); 675 } 676 677 _initialized = true; 678} 679 680 681 682// ------------------------------------------------------------------ 683// CompileBroker::make_compiler_thread 684CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, TRAPS) { 685 CompilerThread* compiler_thread = NULL; 686 687 klassOop k = 688 SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_Thread(), 689 true, CHECK_0); 690 instanceKlassHandle klass (THREAD, k); 691 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0); 692 Handle string = java_lang_String::create_from_str(name, CHECK_0); 693 694 // Initialize thread_oop to put it into the system threadGroup 695 Handle thread_group (THREAD, Universe::system_thread_group()); 696 JavaValue result(T_VOID); 697 JavaCalls::call_special(&result, thread_oop, 698 klass, 699 vmSymbolHandles::object_initializer_name(), 700 vmSymbolHandles::threadgroup_string_void_signature(), 701 thread_group, 702 string, 703 CHECK_0); 704 705 { 706 MutexLocker mu(Threads_lock, THREAD); 707 compiler_thread = new CompilerThread(queue, counters); 708 // At this point the new CompilerThread data-races with this startup 709 // thread (which I believe is the primoridal thread and NOT the VM 710 // thread). This means Java bytecodes being executed at startup can 711 // queue compile jobs which will run at whatever default priority the 712 // newly created CompilerThread runs at. 713 714 715 // At this point it may be possible that no osthread was created for the 716 // JavaThread due to lack of memory. We would have to throw an exception 717 // in that case. However, since this must work and we do not allow 718 // exceptions anyway, check and abort if this fails. 719 720 if (compiler_thread == NULL || compiler_thread->osthread() == NULL){ 721 vm_exit_during_initialization("java.lang.OutOfMemoryError", 722 "unable to create new native thread"); 723 } 724 725 java_lang_Thread::set_thread(thread_oop(), compiler_thread); 726 727 // Note that this only sets the JavaThread _priority field, which by 728 // definition is limited to Java priorities and not OS priorities. 729 // The os-priority is set in the CompilerThread startup code itself 730 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); 731 // CLEANUP PRIORITIES: This -if- statement hids a bug whereby the compiler 732 // threads never have their OS priority set. The assumption here is to 733 // enable the Performance group to do flag tuning, figure out a suitable 734 // CompilerThreadPriority, and then remove this 'if' statement (and 735 // comment) and unconditionally set the priority. 736 737 // Compiler Threads should be at the highest Priority 738 if ( CompilerThreadPriority != -1 ) 739 os::set_native_priority( compiler_thread, CompilerThreadPriority ); 740 else 741 os::set_native_priority( compiler_thread, os::java_to_os_priority[NearMaxPriority]); 742 743 // Note that I cannot call os::set_priority because it expects Java 744 // priorities and I am *explicitly* using OS priorities so that it's 745 // possible to set the compiler thread priority higher than any Java 746 // thread. 747 748 java_lang_Thread::set_daemon(thread_oop()); 749 750 compiler_thread->set_threadObj(thread_oop()); 751 Threads::add(compiler_thread); 752 Thread::start(compiler_thread); 753 } 754 // Let go of Threads_lock before yielding 755 os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS) 756 757 return compiler_thread; 758} 759 760 761// ------------------------------------------------------------------ 762// CompileBroker::init_compiler_threads 763// 764// Initialize the compilation queue 765void CompileBroker::init_compiler_threads(int compiler_count) { 766 EXCEPTION_MARK; 767 768 _method_queue = new CompileQueue("MethodQueue", MethodCompileQueue_lock); 769 _method_threads = 770 new (ResourceObj::C_HEAP) GrowableArray<CompilerThread*>(compiler_count, true); 771 772 char name_buffer[256]; 773 int i; 774 for (i = 0; i < compiler_count; i++) { 775 // Create a name for our thread. 776 sprintf(name_buffer, "CompilerThread%d", i); 777 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); 778 779 CompilerThread* new_thread = make_compiler_thread(name_buffer, _method_queue, counters, CHECK); 780 _method_threads->append(new_thread); 781 } 782 if (UsePerfData) { 783 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, 784 compiler_count, CHECK); 785 } 786} 787 788// ------------------------------------------------------------------ 789// CompileBroker::is_idle 790bool CompileBroker::is_idle() { 791 if (!_method_queue->is_empty()) { 792 return false; 793 } else { 794 int num_threads = _method_threads->length(); 795 for (int i=0; i<num_threads; i++) { 796 if (_method_threads->at(i)->task() != NULL) { 797 return false; 798 } 799 } 800 801 // No pending or active compilations. 802 return true; 803 } 804} 805 806 807// ------------------------------------------------------------------ 808// CompileBroker::compile_method 809// 810// Request compilation of a method. 811void CompileBroker::compile_method_base(methodHandle method, 812 int osr_bci, 813 int comp_level, 814 methodHandle hot_method, 815 int hot_count, 816 const char* comment, 817 TRAPS) { 818 // do nothing if compiler thread(s) is not available 819 if (!_initialized ) { 820 return; 821 } 822 823 guarantee(!method->is_abstract(), "cannot compile abstract methods"); 824 assert(method->method_holder()->klass_part()->oop_is_instance(), 825 "sanity check"); 826 assert(!instanceKlass::cast(method->method_holder())->is_not_initialized(), 827 "method holder must be initialized"); 828 829 if (CIPrintRequests) { 830 tty->print("request: "); 831 method->print_short_name(tty); 832 if (osr_bci != InvocationEntryBci) { 833 tty->print(" osr_bci: %d", osr_bci); 834 } 835 tty->print(" comment: %s count: %d", comment, hot_count); 836 if (!hot_method.is_null()) { 837 tty->print(" hot: "); 838 if (hot_method() != method()) { 839 hot_method->print_short_name(tty); 840 } else { 841 tty->print("yes"); 842 } 843 } 844 tty->cr(); 845 } 846 847 // A request has been made for compilation. Before we do any 848 // real work, check to see if the method has been compiled 849 // in the meantime with a definitive result. 850 if (compilation_is_complete(method, osr_bci, comp_level)) { 851 return; 852 } 853 854 // If this method is already in the compile queue, then 855 // we do not block the current thread. 856 if (compilation_is_in_queue(method, osr_bci)) { 857 // We may want to decay our counter a bit here to prevent 858 // multiple denied requests for compilation. This is an 859 // open compilation policy issue. Note: The other possibility, 860 // in the case that this is a blocking compile request, is to have 861 // all subsequent blocking requesters wait for completion of 862 // ongoing compiles. Note that in this case we'll need a protocol 863 // for freeing the associated compile tasks. [Or we could have 864 // a single static monitor on which all these waiters sleep.] 865 return; 866 } 867 868 // Outputs from the following MutexLocker block: 869 CompileTask* task = NULL; 870 bool blocking = false; 871 872 // Acquire our lock. 873 { 874 MutexLocker locker(_method_queue->lock(), THREAD); 875 876 // Make sure the method has not slipped into the queues since 877 // last we checked; note that those checks were "fast bail-outs". 878 // Here we need to be more careful, see 14012000 below. 879 if (compilation_is_in_queue(method, osr_bci)) { 880 return; 881 } 882 883 // We need to check again to see if the compilation has 884 // completed. A previous compilation may have registered 885 // some result. 886 if (compilation_is_complete(method, osr_bci, comp_level)) { 887 return; 888 } 889 890 // We now know that this compilation is not pending, complete, 891 // or prohibited. Assign a compile_id to this compilation 892 // and check to see if it is in our [Start..Stop) range. 893 uint compile_id = assign_compile_id(method, osr_bci); 894 if (compile_id == 0) { 895 // The compilation falls outside the allowed range. 896 return; 897 } 898 899 // Should this thread wait for completion of the compile? 900 blocking = is_compile_blocking(method, osr_bci); 901 902 // We will enter the compilation in the queue. 903 // 14012000: Note that this sets the queued_for_compile bits in 904 // the target method. We can now reason that a method cannot be 905 // queued for compilation more than once, as follows: 906 // Before a thread queues a task for compilation, it first acquires 907 // the compile queue lock, then checks if the method's queued bits 908 // are set or it has already been compiled. Thus there can not be two 909 // instances of a compilation task for the same method on the 910 // compilation queue. Consider now the case where the compilation 911 // thread has already removed a task for that method from the queue 912 // and is in the midst of compiling it. In this case, the 913 // queued_for_compile bits must be set in the method (and these 914 // will be visible to the current thread, since the bits were set 915 // under protection of the compile queue lock, which we hold now. 916 // When the compilation completes, the compiler thread first sets 917 // the compilation result and then clears the queued_for_compile 918 // bits. Neither of these actions are protected by a barrier (or done 919 // under the protection of a lock), so the only guarantee we have 920 // (on machines with TSO (Total Store Order)) is that these values 921 // will update in that order. As a result, the only combinations of 922 // these bits that the current thread will see are, in temporal order: 923 // <RESULT, QUEUE> : 924 // <0, 1> : in compile queue, but not yet compiled 925 // <1, 1> : compiled but queue bit not cleared 926 // <1, 0> : compiled and queue bit cleared 927 // Because we first check the queue bits then check the result bits, 928 // we are assured that we cannot introduce a duplicate task. 929 // Note that if we did the tests in the reverse order (i.e. check 930 // result then check queued bit), we could get the result bit before 931 // the compilation completed, and the queue bit after the compilation 932 // completed, and end up introducing a "duplicate" (redundant) task. 933 // In that case, the compiler thread should first check if a method 934 // has already been compiled before trying to compile it. 935 // NOTE: in the event that there are multiple compiler threads and 936 // there is de-optimization/recompilation, things will get hairy, 937 // and in that case it's best to protect both the testing (here) of 938 // these bits, and their updating (here and elsewhere) under a 939 // common lock. 940 task = create_compile_task(_method_queue, 941 compile_id, method, 942 osr_bci, comp_level, 943 hot_method, hot_count, comment, 944 blocking); 945 } 946 947 if (blocking) { 948 wait_for_completion(task); 949 } 950} 951 952 953nmethod* CompileBroker::compile_method(methodHandle method, int osr_bci, 954 methodHandle hot_method, int hot_count, 955 const char* comment, TRAPS) { 956 // make sure arguments make sense 957 assert(method->method_holder()->klass_part()->oop_is_instance(), "not an instance method"); 958 assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range"); 959 assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods"); 960 assert(!instanceKlass::cast(method->method_holder())->is_not_initialized(), "method holder must be initialized"); 961 962 int comp_level = CompilationPolicy::policy()->compilation_level(method, osr_bci); 963 964#ifdef TIERED 965 if (TieredCompilation && StressTieredRuntime) { 966 static int flipper = 0; 967 if (is_even(flipper++)) { 968 comp_level = CompLevel_fast_compile; 969 } else { 970 comp_level = CompLevel_full_optimization; 971 } 972 } 973#ifdef SPARC 974 // QQQ FIX ME 975 // C2 only returns long results in G1 and c1 doesn't understand so disallow c2 976 // compiles of long results 977 if (TieredCompilation && method()->result_type() == T_LONG) { 978 comp_level = CompLevel_fast_compile; 979 } 980#endif // SPARC 981#endif // TIERED 982 983 // return quickly if possible 984 985 // lock, make sure that the compilation 986 // isn't prohibited in a straightforward way. 987 988 if (compiler(comp_level) == NULL || compilation_is_prohibited(method, osr_bci, comp_level)) { 989 return NULL; 990 } 991 992 if (osr_bci == InvocationEntryBci) { 993 // standard compilation 994 nmethod* method_code = method->code(); 995 if (method_code != NULL 996#ifdef TIERED 997 && ( method_code->is_compiled_by_c2() || comp_level == CompLevel_fast_compile ) 998#endif // TIERED 999 ) { 1000 return method_code; 1001 } 1002 if (method->is_not_compilable(comp_level)) return NULL; 1003 1004 if (UseCodeCacheFlushing) { 1005 nmethod* saved = CodeCache::find_and_remove_saved_code(method()); 1006 if (saved != NULL) { 1007 method->set_code(method, saved); 1008 return saved; 1009 } 1010 } 1011 1012 } else { 1013 // osr compilation 1014#ifndef TIERED 1015 // seems like an assert of dubious value 1016 assert(comp_level == CompLevel_full_optimization, 1017 "all OSR compiles are assumed to be at a single compilation lavel"); 1018#endif // TIERED 1019 nmethod* nm = method->lookup_osr_nmethod_for(osr_bci); 1020 if (nm != NULL) return nm; 1021 if (method->is_not_osr_compilable()) return NULL; 1022 } 1023 1024 assert(!HAS_PENDING_EXCEPTION, "No exception should be present"); 1025 // some prerequisites that are compiler specific 1026 if (compiler(comp_level)->is_c2()) { 1027 method->constants()->resolve_string_constants(CHECK_0); 1028 // Resolve all classes seen in the signature of the method 1029 // we are compiling. 1030 methodOopDesc::load_signature_classes(method, CHECK_0); 1031 } 1032 1033 // If the method is native, do the lookup in the thread requesting 1034 // the compilation. Native lookups can load code, which is not 1035 // permitted during compilation. 1036 // 1037 // Note: A native method implies non-osr compilation which is 1038 // checked with an assertion at the entry of this method. 1039 if (method->is_native()) { 1040 bool in_base_library; 1041 address adr = NativeLookup::lookup(method, in_base_library, THREAD); 1042 if (HAS_PENDING_EXCEPTION) { 1043 // In case of an exception looking up the method, we just forget 1044 // about it. The interpreter will kick-in and throw the exception. 1045 method->set_not_compilable(); // implies is_not_osr_compilable() 1046 CLEAR_PENDING_EXCEPTION; 1047 return NULL; 1048 } 1049 assert(method->has_native_function(), "must have native code by now"); 1050 } 1051 1052 // RedefineClasses() has replaced this method; just return 1053 if (method->is_old()) { 1054 return NULL; 1055 } 1056 1057 // JVMTI -- post_compile_event requires jmethod_id() that may require 1058 // a lock the compiling thread can not acquire. Prefetch it here. 1059 if (JvmtiExport::should_post_compiled_method_load()) { 1060 method->jmethod_id(); 1061 } 1062 1063 // If the compiler is shut off due to code cache flushing or otherwise, 1064 // fail out now so blocking compiles dont hang the java thread 1065 if (!should_compile_new_jobs() || (UseCodeCacheFlushing && CodeCache::needs_flushing())) { 1066 method->invocation_counter()->decay(); 1067 method->backedge_counter()->decay(); 1068 return NULL; 1069 } 1070 1071 // do the compilation 1072 if (method->is_native()) { 1073 if (!PreferInterpreterNativeStubs) { 1074 (void) AdapterHandlerLibrary::create_native_wrapper(method); 1075 } else { 1076 return NULL; 1077 } 1078 } else { 1079 compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, CHECK_0); 1080 } 1081 1082 // return requested nmethod 1083 return osr_bci == InvocationEntryBci ? method->code() : method->lookup_osr_nmethod_for(osr_bci); 1084} 1085 1086 1087// ------------------------------------------------------------------ 1088// CompileBroker::compilation_is_complete 1089// 1090// See if compilation of this method is already complete. 1091bool CompileBroker::compilation_is_complete(methodHandle method, 1092 int osr_bci, 1093 int comp_level) { 1094 bool is_osr = (osr_bci != standard_entry_bci); 1095 if (is_osr) { 1096 if (method->is_not_osr_compilable()) { 1097 return true; 1098 } else { 1099 nmethod* result = method->lookup_osr_nmethod_for(osr_bci); 1100 return (result != NULL); 1101 } 1102 } else { 1103 if (method->is_not_compilable(comp_level)) { 1104 return true; 1105 } else { 1106 nmethod* result = method->code(); 1107 if (result == NULL) return false; 1108#ifdef TIERED 1109 if (comp_level == CompLevel_fast_compile) { 1110 // At worst the code is from c1 1111 return true; 1112 } 1113 // comp level must be full opt 1114 return result->is_compiled_by_c2(); 1115#endif // TIERED 1116 return true; 1117 } 1118 } 1119} 1120 1121 1122// ------------------------------------------------------------------ 1123// CompileBroker::compilation_is_in_queue 1124// 1125// See if this compilation is already requested. 1126// 1127// Implementation note: there is only a single "is in queue" bit 1128// for each method. This means that the check below is overly 1129// conservative in the sense that an osr compilation in the queue 1130// will block a normal compilation from entering the queue (and vice 1131// versa). This can be remedied by a full queue search to disambiguate 1132// cases. If it is deemed profitible, this may be done. 1133bool CompileBroker::compilation_is_in_queue(methodHandle method, 1134 int osr_bci) { 1135 return method->queued_for_compilation(); 1136} 1137 1138 1139// ------------------------------------------------------------------ 1140// CompileBroker::compilation_is_prohibited 1141// 1142// See if this compilation is not allowed. 1143bool CompileBroker::compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level) { 1144 bool is_native = method->is_native(); 1145 // Some compilers may not support the compilation of natives. 1146 // QQQ this needs some work ought to only record not compilable at 1147 // the specified level 1148 if (is_native && 1149 (!CICompileNatives || !compiler(comp_level)->supports_native())) { 1150 method->set_not_compilable_quietly(); 1151 return true; 1152 } 1153 1154 bool is_osr = (osr_bci != standard_entry_bci); 1155 // Some compilers may not support on stack replacement. 1156 if (is_osr && 1157 (!CICompileOSR || !compiler(comp_level)->supports_osr())) { 1158 method->set_not_osr_compilable(); 1159 return true; 1160 } 1161 1162 // The method may be explicitly excluded by the user. 1163 bool quietly; 1164 if (CompilerOracle::should_exclude(method, quietly)) { 1165 if (!quietly) { 1166 // This does not happen quietly... 1167 ResourceMark rm; 1168 tty->print("### Excluding %s:%s", 1169 method->is_native() ? "generation of native wrapper" : "compile", 1170 (method->is_static() ? " static" : "")); 1171 method->print_short_name(tty); 1172 tty->cr(); 1173 } 1174 method->set_not_compilable_quietly(); 1175 } 1176 1177 return false; 1178} 1179 1180 1181// ------------------------------------------------------------------ 1182// CompileBroker::assign_compile_id 1183// 1184// Assign a serialized id number to this compilation request. If the 1185// number falls out of the allowed range, return a 0. OSR 1186// compilations may be numbered separately from regular compilations 1187// if certain debugging flags are used. 1188uint CompileBroker::assign_compile_id(methodHandle method, int osr_bci) { 1189 assert(_method_queue->lock()->owner() == JavaThread::current(), 1190 "must hold the compilation queue lock"); 1191 bool is_osr = (osr_bci != standard_entry_bci); 1192 assert(!method->is_native(), "no longer compile natives"); 1193 uint id; 1194 if (CICountOSR && is_osr) { 1195 id = ++_osr_compilation_id; 1196 if ((uint)CIStartOSR <= id && id < (uint)CIStopOSR) { 1197 return id; 1198 } 1199 } else { 1200 id = ++_compilation_id; 1201 if ((uint)CIStart <= id && id < (uint)CIStop) { 1202 return id; 1203 } 1204 } 1205 1206 // Method was not in the appropriate compilation range. 1207 method->set_not_compilable_quietly(); 1208 return 0; 1209} 1210 1211 1212// ------------------------------------------------------------------ 1213// CompileBroker::is_compile_blocking 1214// 1215// Should the current thread be blocked until this compilation request 1216// has been fulfilled? 1217bool CompileBroker::is_compile_blocking(methodHandle method, int osr_bci) { 1218 return !BackgroundCompilation; 1219} 1220 1221 1222// ------------------------------------------------------------------ 1223// CompileBroker::preload_classes 1224void CompileBroker::preload_classes(methodHandle method, TRAPS) { 1225 // Move this code over from c1_Compiler.cpp 1226 ShouldNotReachHere(); 1227} 1228 1229 1230// ------------------------------------------------------------------ 1231// CompileBroker::create_compile_task 1232// 1233// Create a CompileTask object representing the current request for 1234// compilation. Add this task to the queue. 1235CompileTask* CompileBroker::create_compile_task(CompileQueue* queue, 1236 int compile_id, 1237 methodHandle method, 1238 int osr_bci, 1239 int comp_level, 1240 methodHandle hot_method, 1241 int hot_count, 1242 const char* comment, 1243 bool blocking) { 1244 CompileTask* new_task = allocate_task(); 1245 new_task->initialize(compile_id, method, osr_bci, comp_level, 1246 hot_method, hot_count, comment, 1247 blocking); 1248 queue->add(new_task); 1249 return new_task; 1250} 1251 1252 1253// ------------------------------------------------------------------ 1254// CompileBroker::allocate_task 1255// 1256// Allocate a CompileTask, from the free list if possible. 1257CompileTask* CompileBroker::allocate_task() { 1258 MutexLocker locker(CompileTaskAlloc_lock); 1259 CompileTask* task = NULL; 1260 if (_task_free_list != NULL) { 1261 task = _task_free_list; 1262 _task_free_list = task->next(); 1263 task->set_next(NULL); 1264 } else { 1265 task = new CompileTask(); 1266 task->set_next(NULL); 1267 } 1268 return task; 1269} 1270 1271 1272// ------------------------------------------------------------------ 1273// CompileBroker::free_task 1274// 1275// Add a task to the free list. 1276void CompileBroker::free_task(CompileTask* task) { 1277 MutexLocker locker(CompileTaskAlloc_lock); 1278 task->free(); 1279 task->set_next(_task_free_list); 1280 _task_free_list = task; 1281} 1282 1283 1284// ------------------------------------------------------------------ 1285// CompileBroker::wait_for_completion 1286// 1287// Wait for the given method CompileTask to complete. 1288void CompileBroker::wait_for_completion(CompileTask* task) { 1289 if (CIPrintCompileQueue) { 1290 tty->print_cr("BLOCKING FOR COMPILE"); 1291 } 1292 1293 assert(task->is_blocking(), "can only wait on blocking task"); 1294 1295 JavaThread *thread = JavaThread::current(); 1296 thread->set_blocked_on_compilation(true); 1297 1298 methodHandle method(thread, 1299 (methodOop)JNIHandles::resolve(task->method_handle())); 1300 { 1301 MutexLocker waiter(task->lock(), thread); 1302 1303 while (!task->is_complete()) 1304 task->lock()->wait(); 1305 } 1306 // It is harmless to check this status without the lock, because 1307 // completion is a stable property (until the task object is recycled). 1308 assert(task->is_complete(), "Compilation should have completed"); 1309 assert(task->code_handle() == NULL, "must be reset"); 1310 1311 thread->set_blocked_on_compilation(false); 1312 1313 // By convention, the waiter is responsible for recycling a 1314 // blocking CompileTask. Since there is only one waiter ever 1315 // waiting on a CompileTask, we know that no one else will 1316 // be using this CompileTask; we can free it. 1317 free_task(task); 1318} 1319 1320// ------------------------------------------------------------------ 1321// CompileBroker::compiler_thread_loop 1322// 1323// The main loop run by a CompilerThread. 1324void CompileBroker::compiler_thread_loop() { 1325 CompilerThread* thread = CompilerThread::current(); 1326 CompileQueue* queue = thread->queue(); 1327 1328 // For the thread that initializes the ciObjectFactory 1329 // this resource mark holds all the shared objects 1330 ResourceMark rm; 1331 1332 // First thread to get here will initialize the compiler interface 1333 1334 if (!ciObjectFactory::is_initialized()) { 1335 ASSERT_IN_VM; 1336 MutexLocker only_one (CompileThread_lock, thread); 1337 if (!ciObjectFactory::is_initialized()) { 1338 ciObjectFactory::initialize(); 1339 } 1340 } 1341 1342 // Open a log. 1343 if (LogCompilation) { 1344 init_compiler_thread_log(); 1345 } 1346 CompileLog* log = thread->log(); 1347 if (log != NULL) { 1348 log->begin_elem("start_compile_thread thread='" UINTX_FORMAT "' process='%d'", 1349 os::current_thread_id(), 1350 os::current_process_id()); 1351 log->stamp(); 1352 log->end_elem(); 1353 } 1354 1355 while (true) { 1356 { 1357 // We need this HandleMark to avoid leaking VM handles. 1358 HandleMark hm(thread); 1359 1360 if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) { 1361 // the code cache is really full 1362 handle_full_code_cache(); 1363 } else if (UseCodeCacheFlushing && CodeCache::needs_flushing()) { 1364 // Attempt to start cleaning the code cache while there is still a little headroom 1365 NMethodSweeper::handle_full_code_cache(false); 1366 } 1367 1368 CompileTask* task = queue->get(); 1369 1370 // Give compiler threads an extra quanta. They tend to be bursty and 1371 // this helps the compiler to finish up the job. 1372 if( CompilerThreadHintNoPreempt ) 1373 os::hint_no_preempt(); 1374 1375 // trace per thread time and compile statistics 1376 CompilerCounters* counters = ((CompilerThread*)thread)->counters(); 1377 PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter()); 1378 1379 // Assign the task to the current thread. Mark this compilation 1380 // thread as active for the profiler. 1381 CompileTaskWrapper ctw(task); 1382 nmethodLocker result_handle; // (handle for the nmethod produced by this task) 1383 task->set_code_handle(&result_handle); 1384 methodHandle method(thread, 1385 (methodOop)JNIHandles::resolve(task->method_handle())); 1386 1387 // Never compile a method if breakpoints are present in it 1388 if (method()->number_of_breakpoints() == 0) { 1389 // Compile the method. 1390 if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) { 1391#ifdef COMPILER1 1392 // Allow repeating compilations for the purpose of benchmarking 1393 // compile speed. This is not useful for customers. 1394 if (CompilationRepeat != 0) { 1395 int compile_count = CompilationRepeat; 1396 while (compile_count > 0) { 1397 invoke_compiler_on_method(task); 1398 nmethod* nm = method->code(); 1399 if (nm != NULL) { 1400 nm->make_zombie(); 1401 method->clear_code(); 1402 } 1403 compile_count--; 1404 } 1405 } 1406#endif /* COMPILER1 */ 1407 invoke_compiler_on_method(task); 1408 } else { 1409 // After compilation is disabled, remove remaining methods from queue 1410 method->clear_queued_for_compilation(); 1411 } 1412 } 1413 } 1414 } 1415} 1416 1417 1418// ------------------------------------------------------------------ 1419// CompileBroker::init_compiler_thread_log 1420// 1421// Set up state required by +LogCompilation. 1422void CompileBroker::init_compiler_thread_log() { 1423 CompilerThread* thread = CompilerThread::current(); 1424 char fileBuf[4*K]; 1425 FILE* fp = NULL; 1426 char* file = NULL; 1427 intx thread_id = os::current_thread_id(); 1428 for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) { 1429 const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL); 1430 if (dir == NULL) { 1431 jio_snprintf(fileBuf, sizeof(fileBuf), "hs_c" UINTX_FORMAT "_pid%u.log", 1432 thread_id, os::current_process_id()); 1433 } else { 1434 jio_snprintf(fileBuf, sizeof(fileBuf), 1435 "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir, 1436 os::file_separator(), thread_id, os::current_process_id()); 1437 } 1438 fp = fopen(fileBuf, "at"); 1439 if (fp != NULL) { 1440 file = NEW_C_HEAP_ARRAY(char, strlen(fileBuf)+1); 1441 strcpy(file, fileBuf); 1442 break; 1443 } 1444 } 1445 if (fp == NULL) { 1446 warning("Cannot open log file: %s", fileBuf); 1447 } else { 1448 if (LogCompilation && Verbose) 1449 tty->print_cr("Opening compilation log %s", file); 1450 CompileLog* log = new(ResourceObj::C_HEAP) CompileLog(file, fp, thread_id); 1451 thread->init_log(log); 1452 1453 if (xtty != NULL) { 1454 ttyLocker ttyl; 1455 1456 // Record any per thread log files 1457 xtty->elem("thread_logfile thread='%d' filename='%s'", thread_id, file); 1458 } 1459 } 1460} 1461 1462// ------------------------------------------------------------------ 1463// CompileBroker::set_should_block 1464// 1465// Set _should_block. 1466// Call this from the VM, with Threads_lock held and a safepoint requested. 1467void CompileBroker::set_should_block() { 1468 assert(Threads_lock->owner() == Thread::current(), "must have threads lock"); 1469 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint already"); 1470#ifndef PRODUCT 1471 if (PrintCompilation && (Verbose || WizardMode)) 1472 tty->print_cr("notifying compiler thread pool to block"); 1473#endif 1474 _should_block = true; 1475} 1476 1477// ------------------------------------------------------------------ 1478// CompileBroker::maybe_block 1479// 1480// Call this from the compiler at convenient points, to poll for _should_block. 1481void CompileBroker::maybe_block() { 1482 if (_should_block) { 1483#ifndef PRODUCT 1484 if (PrintCompilation && (Verbose || WizardMode)) 1485 tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", Thread::current()); 1486#endif 1487 ThreadInVMfromNative tivfn(JavaThread::current()); 1488 } 1489} 1490 1491 1492// ------------------------------------------------------------------ 1493// CompileBroker::invoke_compiler_on_method 1494// 1495// Compile a method. 1496// 1497void CompileBroker::invoke_compiler_on_method(CompileTask* task) { 1498 if (PrintCompilation) { 1499 ResourceMark rm; 1500 task->print_line(); 1501 } 1502 elapsedTimer time; 1503 1504 CompilerThread* thread = CompilerThread::current(); 1505 ResourceMark rm(thread); 1506 1507 // Common flags. 1508 uint compile_id = task->compile_id(); 1509 int osr_bci = task->osr_bci(); 1510 bool is_osr = (osr_bci != standard_entry_bci); 1511 bool should_log = (thread->log() != NULL); 1512 bool should_break = false; 1513 { 1514 // create the handle inside it's own block so it can't 1515 // accidentally be referenced once the thread transitions to 1516 // native. The NoHandleMark before the transition should catch 1517 // any cases where this occurs in the future. 1518 methodHandle method(thread, 1519 (methodOop)JNIHandles::resolve(task->method_handle())); 1520 should_break = check_break_at(method, compile_id, is_osr); 1521 if (should_log && !CompilerOracle::should_log(method)) { 1522 should_log = false; 1523 } 1524 assert(!method->is_native(), "no longer compile natives"); 1525 1526 // Save information about this method in case of failure. 1527 set_last_compile(thread, method, is_osr, task->comp_level()); 1528 1529 DTRACE_METHOD_COMPILE_BEGIN_PROBE(compiler(task->comp_level()), method); 1530 } 1531 1532 // Allocate a new set of JNI handles. 1533 push_jni_handle_block(); 1534 jobject target_handle = JNIHandles::make_local(thread, JNIHandles::resolve(task->method_handle())); 1535 int compilable = ciEnv::MethodCompilable; 1536 { 1537 int system_dictionary_modification_counter; 1538 { 1539 MutexLocker locker(Compile_lock, thread); 1540 system_dictionary_modification_counter = SystemDictionary::number_of_modifications(); 1541 } 1542 1543 NoHandleMark nhm; 1544 ThreadToNativeFromVM ttn(thread); 1545 1546 ciEnv ci_env(task, system_dictionary_modification_counter); 1547 if (should_break) { 1548 ci_env.set_break_at_compile(true); 1549 } 1550 if (should_log) { 1551 ci_env.set_log(thread->log()); 1552 } 1553 assert(thread->env() == &ci_env, "set by ci_env"); 1554 // The thread-env() field is cleared in ~CompileTaskWrapper. 1555 1556 // Cache Jvmti state 1557 ci_env.cache_jvmti_state(); 1558 1559 // Cache DTrace flags 1560 ci_env.cache_dtrace_flags(); 1561 1562 ciMethod* target = ci_env.get_method_from_handle(target_handle); 1563 1564 TraceTime t1("compilation", &time); 1565 1566 compiler(task->comp_level())->compile_method(&ci_env, target, osr_bci); 1567 1568 if (!ci_env.failing() && task->code() == NULL) { 1569 //assert(false, "compiler should always document failure"); 1570 // The compiler elected, without comment, not to register a result. 1571 // Do not attempt further compilations of this method. 1572 ci_env.record_method_not_compilable("compile failed"); 1573 } 1574 1575 if (ci_env.failing()) { 1576 // Copy this bit to the enclosing block: 1577 compilable = ci_env.compilable(); 1578 if (PrintCompilation) { 1579 const char* reason = ci_env.failure_reason(); 1580 if (compilable == ciEnv::MethodCompilable_not_at_tier) { 1581 if (is_highest_tier_compile(ci_env.comp_level())) { 1582 // Already at highest tier, promote to not compilable. 1583 compilable = ciEnv::MethodCompilable_never; 1584 } else { 1585 tty->print_cr("%3d COMPILE SKIPPED: %s (retry at different tier)", compile_id, reason); 1586 } 1587 } 1588 1589 if (compilable == ciEnv::MethodCompilable_never) { 1590 tty->print_cr("%3d COMPILE SKIPPED: %s (not retryable)", compile_id, reason); 1591 } else if (compilable == ciEnv::MethodCompilable) { 1592 tty->print_cr("%3d COMPILE SKIPPED: %s", compile_id, reason); 1593 } 1594 } 1595 } else { 1596 task->mark_success(); 1597 task->set_num_inlined_bytecodes(ci_env.num_inlined_bytecodes()); 1598 } 1599 } 1600 pop_jni_handle_block(); 1601 1602 methodHandle method(thread, 1603 (methodOop)JNIHandles::resolve(task->method_handle())); 1604 1605 DTRACE_METHOD_COMPILE_END_PROBE(compiler(task->comp_level()), method, task->is_success()); 1606 1607 collect_statistics(thread, time, task); 1608 1609 if (compilable == ciEnv::MethodCompilable_never) { 1610 if (is_osr) { 1611 method->set_not_osr_compilable(); 1612 } else { 1613 method->set_not_compilable_quietly(); 1614 } 1615 } else if (compilable == ciEnv::MethodCompilable_not_at_tier) { 1616 method->set_not_compilable_quietly(task->comp_level()); 1617 } 1618 1619 // Note that the queued_for_compilation bits are cleared without 1620 // protection of a mutex. [They were set by the requester thread, 1621 // when adding the task to the complie queue -- at which time the 1622 // compile queue lock was held. Subsequently, we acquired the compile 1623 // queue lock to get this task off the compile queue; thus (to belabour 1624 // the point somewhat) our clearing of the bits must be occurring 1625 // only after the setting of the bits. See also 14012000 above. 1626 method->clear_queued_for_compilation(); 1627 1628#ifdef ASSERT 1629 if (CollectedHeap::fired_fake_oom()) { 1630 // The current compile received a fake OOM during compilation so 1631 // go ahead and exit the VM since the test apparently succeeded 1632 tty->print_cr("*** Shutting down VM after successful fake OOM"); 1633 vm_exit(0); 1634 } 1635#endif 1636} 1637 1638 1639// ------------------------------------------------------------------ 1640// CompileBroker::handle_full_code_cache 1641// 1642// The CodeCache is full. Print out warning and disable compilation or 1643// try code cache cleaning so compilation can continue later. 1644void CompileBroker::handle_full_code_cache() { 1645 UseInterpreter = true; 1646 if (UseCompiler || AlwaysCompileLoopMethods ) { 1647 CompilerThread* thread = CompilerThread::current(); 1648 CompileLog* log = thread->log(); 1649 if (log != NULL) { 1650 log->begin_elem("code_cache_full"); 1651 log->stamp(); 1652 log->end_elem(); 1653 } 1654 #ifndef PRODUCT 1655 warning("CodeCache is full. Compiler has been disabled"); 1656 if (CompileTheWorld || ExitOnFullCodeCache) { 1657 before_exit(JavaThread::current()); 1658 exit_globals(); // will delete tty 1659 vm_direct_exit(CompileTheWorld ? 0 : 1); 1660 } 1661 #endif 1662 if (UseCodeCacheFlushing) { 1663 NMethodSweeper::handle_full_code_cache(true); 1664 } else { 1665 UseCompiler = false; 1666 AlwaysCompileLoopMethods = false; 1667 } 1668 } 1669} 1670 1671// ------------------------------------------------------------------ 1672// CompileBroker::set_last_compile 1673// 1674// Record this compilation for debugging purposes. 1675void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) { 1676 ResourceMark rm; 1677 char* method_name = method->name()->as_C_string(); 1678 strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length); 1679 char current_method[CompilerCounters::cmname_buffer_length]; 1680 size_t maxLen = CompilerCounters::cmname_buffer_length; 1681 1682 if (UsePerfData) { 1683 const char* class_name = method->method_holder()->klass_part()->name()->as_C_string(); 1684 1685 size_t s1len = strlen(class_name); 1686 size_t s2len = strlen(method_name); 1687 1688 // check if we need to truncate the string 1689 if (s1len + s2len + 2 > maxLen) { 1690 1691 // the strategy is to lop off the leading characters of the 1692 // class name and the trailing characters of the method name. 1693 1694 if (s2len + 2 > maxLen) { 1695 // lop of the entire class name string, let snprintf handle 1696 // truncation of the method name. 1697 class_name += s1len; // null string 1698 } 1699 else { 1700 // lop off the extra characters from the front of the class name 1701 class_name += ((s1len + s2len + 2) - maxLen); 1702 } 1703 } 1704 1705 jio_snprintf(current_method, maxLen, "%s %s", class_name, method_name); 1706 } 1707 1708 if (CICountOSR && is_osr) { 1709 _last_compile_type = osr_compile; 1710 } else { 1711 _last_compile_type = normal_compile; 1712 } 1713 _last_compile_level = comp_level; 1714 1715 if (UsePerfData) { 1716 CompilerCounters* counters = thread->counters(); 1717 counters->set_current_method(current_method); 1718 counters->set_compile_type((jlong)_last_compile_type); 1719 } 1720} 1721 1722 1723// ------------------------------------------------------------------ 1724// CompileBroker::push_jni_handle_block 1725// 1726// Push on a new block of JNI handles. 1727void CompileBroker::push_jni_handle_block() { 1728 JavaThread* thread = JavaThread::current(); 1729 1730 // Allocate a new block for JNI handles. 1731 // Inlined code from jni_PushLocalFrame() 1732 JNIHandleBlock* java_handles = thread->active_handles(); 1733 JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread); 1734 assert(compile_handles != NULL && java_handles != NULL, "should not be NULL"); 1735 compile_handles->set_pop_frame_link(java_handles); // make sure java handles get gc'd. 1736 thread->set_active_handles(compile_handles); 1737} 1738 1739 1740// ------------------------------------------------------------------ 1741// CompileBroker::pop_jni_handle_block 1742// 1743// Pop off the current block of JNI handles. 1744void CompileBroker::pop_jni_handle_block() { 1745 JavaThread* thread = JavaThread::current(); 1746 1747 // Release our JNI handle block 1748 JNIHandleBlock* compile_handles = thread->active_handles(); 1749 JNIHandleBlock* java_handles = compile_handles->pop_frame_link(); 1750 thread->set_active_handles(java_handles); 1751 compile_handles->set_pop_frame_link(NULL); 1752 JNIHandleBlock::release_block(compile_handles, thread); // may block 1753} 1754 1755 1756// ------------------------------------------------------------------ 1757// CompileBroker::check_break_at 1758// 1759// Should the compilation break at the current compilation. 1760bool CompileBroker::check_break_at(methodHandle method, int compile_id, bool is_osr) { 1761 if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) { 1762 return true; 1763 } else if( CompilerOracle::should_break_at(method) ) { // break when compiling 1764 return true; 1765 } else { 1766 return (compile_id == CIBreakAt); 1767 } 1768} 1769 1770// ------------------------------------------------------------------ 1771// CompileBroker::collect_statistics 1772// 1773// Collect statistics about the compilation. 1774 1775void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) { 1776 bool success = task->is_success(); 1777 methodHandle method (thread, (methodOop)JNIHandles::resolve(task->method_handle())); 1778 uint compile_id = task->compile_id(); 1779 bool is_osr = (task->osr_bci() != standard_entry_bci); 1780 nmethod* code = task->code(); 1781 CompilerCounters* counters = thread->counters(); 1782 1783 assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker"); 1784 MutexLocker locker(CompileStatistics_lock); 1785 1786 // _perf variables are production performance counters which are 1787 // updated regardless of the setting of the CITime and CITimeEach flags 1788 // 1789 if (!success) { 1790 _total_bailout_count++; 1791 if (UsePerfData) { 1792 _perf_last_failed_method->set_value(counters->current_method()); 1793 _perf_last_failed_type->set_value(counters->compile_type()); 1794 _perf_total_bailout_count->inc(); 1795 } 1796 } else if (code == NULL) { 1797 if (UsePerfData) { 1798 _perf_last_invalidated_method->set_value(counters->current_method()); 1799 _perf_last_invalidated_type->set_value(counters->compile_type()); 1800 _perf_total_invalidated_count->inc(); 1801 } 1802 _total_invalidated_count++; 1803 } else { 1804 // Compilation succeeded 1805 1806 // update compilation ticks - used by the implementation of 1807 // java.lang.management.CompilationMBean 1808 _perf_total_compilation->inc(time.ticks()); 1809 1810 if (CITime) { 1811 _t_total_compilation.add(time); 1812 if (is_osr) { 1813 _t_osr_compilation.add(time); 1814 _sum_osr_bytes_compiled += method->code_size() + task->num_inlined_bytecodes(); 1815 } else { 1816 _t_standard_compilation.add(time); 1817 _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes(); 1818 } 1819 } 1820 1821 if (UsePerfData) { 1822 // save the name of the last method compiled 1823 _perf_last_method->set_value(counters->current_method()); 1824 _perf_last_compile_type->set_value(counters->compile_type()); 1825 _perf_last_compile_size->set_value(method->code_size() + 1826 task->num_inlined_bytecodes()); 1827 if (is_osr) { 1828 _perf_osr_compilation->inc(time.ticks()); 1829 _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes()); 1830 } else { 1831 _perf_standard_compilation->inc(time.ticks()); 1832 _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes()); 1833 } 1834 } 1835 1836 if (CITimeEach) { 1837 float bytes_per_sec = 1.0 * (method->code_size() + task->num_inlined_bytecodes()) / time.seconds(); 1838 tty->print_cr("%3d seconds: %f bytes/sec : %f (bytes %d + %d inlined)", 1839 compile_id, time.seconds(), bytes_per_sec, method->code_size(), task->num_inlined_bytecodes()); 1840 } 1841 1842 // Collect counts of successful compilations 1843 _sum_nmethod_size += code->total_size(); 1844 _sum_nmethod_code_size += code->code_size(); 1845 _total_compile_count++; 1846 1847 if (UsePerfData) { 1848 _perf_sum_nmethod_size->inc(code->total_size()); 1849 _perf_sum_nmethod_code_size->inc(code->code_size()); 1850 _perf_total_compile_count->inc(); 1851 } 1852 1853 if (is_osr) { 1854 if (UsePerfData) _perf_total_osr_compile_count->inc(); 1855 _total_osr_compile_count++; 1856 } else { 1857 if (UsePerfData) _perf_total_standard_compile_count->inc(); 1858 _total_standard_compile_count++; 1859 } 1860 } 1861 // set the current method for the thread to null 1862 if (UsePerfData) counters->set_current_method(""); 1863} 1864 1865 1866 1867void CompileBroker::print_times() { 1868 tty->cr(); 1869 tty->print_cr("Accumulated compiler times (for compiled methods only)"); 1870 tty->print_cr("------------------------------------------------"); 1871 //0000000000111111111122222222223333333333444444444455555555556666666666 1872 //0123456789012345678901234567890123456789012345678901234567890123456789 1873 tty->print_cr(" Total compilation time : %6.3f s", CompileBroker::_t_total_compilation.seconds()); 1874 tty->print_cr(" Standard compilation : %6.3f s, Average : %2.3f", 1875 CompileBroker::_t_standard_compilation.seconds(), 1876 CompileBroker::_t_standard_compilation.seconds() / CompileBroker::_total_standard_compile_count); 1877 tty->print_cr(" On stack replacement : %6.3f s, Average : %2.3f", CompileBroker::_t_osr_compilation.seconds(), CompileBroker::_t_osr_compilation.seconds() / CompileBroker::_total_osr_compile_count); 1878 1879 if (compiler(CompLevel_fast_compile)) { 1880 compiler(CompLevel_fast_compile)->print_timers(); 1881 if (compiler(CompLevel_fast_compile) != compiler(CompLevel_highest_tier)) 1882 compiler(CompLevel_highest_tier)->print_timers(); 1883 } 1884 1885 tty->cr(); 1886 int tcb = CompileBroker::_sum_osr_bytes_compiled + CompileBroker::_sum_standard_bytes_compiled; 1887 tty->print_cr(" Total compiled bytecodes : %6d bytes", tcb); 1888 tty->print_cr(" Standard compilation : %6d bytes", CompileBroker::_sum_standard_bytes_compiled); 1889 tty->print_cr(" On stack replacement : %6d bytes", CompileBroker::_sum_osr_bytes_compiled); 1890 int bps = (int)(tcb / CompileBroker::_t_total_compilation.seconds()); 1891 tty->print_cr(" Average compilation speed: %6d bytes/s", bps); 1892 tty->cr(); 1893 tty->print_cr(" nmethod code size : %6d bytes", CompileBroker::_sum_nmethod_code_size); 1894 tty->print_cr(" nmethod total size : %6d bytes", CompileBroker::_sum_nmethod_size); 1895} 1896 1897 1898// Debugging output for failure 1899void CompileBroker::print_last_compile() { 1900 if ( _last_compile_level != CompLevel_none && 1901 compiler(_last_compile_level) != NULL && 1902 _last_method_compiled != NULL && 1903 _last_compile_type != no_compile) { 1904 if (_last_compile_type == osr_compile) { 1905 tty->print_cr("Last parse: [osr]%d+++(%d) %s", 1906 _osr_compilation_id, _last_compile_level, _last_method_compiled); 1907 } else { 1908 tty->print_cr("Last parse: %d+++(%d) %s", 1909 _compilation_id, _last_compile_level, _last_method_compiled); 1910 } 1911 } 1912} 1913 1914 1915void CompileBroker::print_compiler_threads_on(outputStream* st) { 1916#ifndef PRODUCT 1917 st->print_cr("Compiler thread printing unimplemented."); 1918 st->cr(); 1919#endif 1920} 1921