src/share/vm/compiler/compileBroker.cpp

Print this page




 169 elapsedTimer CompileBroker::_t_osr_compilation;
 170 elapsedTimer CompileBroker::_t_standard_compilation;
 171 
 172 int CompileBroker::_total_bailout_count          = 0;
 173 int CompileBroker::_total_invalidated_count      = 0;
 174 int CompileBroker::_total_compile_count          = 0;
 175 int CompileBroker::_total_osr_compile_count      = 0;
 176 int CompileBroker::_total_standard_compile_count = 0;
 177 
 178 int CompileBroker::_sum_osr_bytes_compiled       = 0;
 179 int CompileBroker::_sum_standard_bytes_compiled  = 0;
 180 int CompileBroker::_sum_nmethod_size             = 0;
 181 int CompileBroker::_sum_nmethod_code_size        = 0;
 182 
 183 long CompileBroker::_peak_compilation_time       = 0;
 184 
 185 CompileQueue* CompileBroker::_c2_method_queue    = NULL;
 186 CompileQueue* CompileBroker::_c1_method_queue    = NULL;
 187 CompileTask*  CompileBroker::_task_free_list     = NULL;
 188 
 189 GrowableArray<CompilerThread*>* CompileBroker::_method_threads = NULL;
 190 
 191 
 192 class CompilationLog : public StringEventLog {
 193  public:
 194   CompilationLog() : StringEventLog("Compilation events") {
 195   }
 196 
 197   void log_compile(JavaThread* thread, CompileTask* task) {
 198     StringLogMessage lm;
 199     stringStream sstr = lm.stream();
 200     // msg.time_stamp().update_to(tty->time_stamp().ticks());
 201     task->print_compilation(&sstr, NULL, true);
 202     log(thread, "%s", (const char*)lm);
 203   }
 204 
 205   void log_nmethod(JavaThread* thread, nmethod* nm) {
 206     log(thread, "nmethod %d%s " INTPTR_FORMAT " code ["INTPTR_FORMAT ", " INTPTR_FORMAT "]",
 207         nm->compile_id(), nm->is_osr_method() ? "%" : "",
 208         nm, nm->code_begin(), nm->code_end());
 209   }


 570                   _is_success, nm == NULL ? 0 : nm->content_size(),
 571                   method->invocation_count());
 572   int bec = method->backedge_count();
 573   if (bec != 0)  log->print(" backedge_count='%d'", bec);
 574   // Note:  "_is_complete" is about to be set, but is not.
 575   if (_num_inlined_bytecodes != 0) {
 576     log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);
 577   }
 578   log->stamp();
 579   log->end_elem();
 580   log->tail("task");
 581   log->clear_identities();   // next task will have different CI
 582   if (log->unflushed_count() > 2000) {
 583     log->flush();
 584   }
 585   log->mark_file_end();
 586 }
 587 
 588 
 589 
 590 // ------------------------------------------------------------------
 591 // CompileQueue::add
 592 //
 593 // Add a CompileTask to a CompileQueue
 594 void CompileQueue::add(CompileTask* task) {
 595   assert(lock()->owned_by_self(), "must own lock");
 596 
 597   task->set_next(NULL);
 598   task->set_prev(NULL);
 599 
 600   if (_last == NULL) {
 601     // The compile queue is empty.
 602     assert(_first == NULL, "queue is empty");
 603     _first = task;
 604     _last = task;
 605   } else {
 606     // Append the task to the queue.
 607     assert(_last->next() == NULL, "not last");
 608     _last->set_next(task);
 609     task->set_prev(_last);
 610     _last = task;
 611   }
 612   ++_size;
 613 
 614   // Mark the method as being in the compile queue.
 615   task->method()->set_queued_for_compilation();
 616 
 617   if (CIPrintCompileQueue) {
 618     print();
 619   }
 620 
 621   if (LogCompilation && xtty != NULL) {
 622     task->log_task_queued();
 623   }
 624 
 625   // Notify CompilerThreads that a task is available.
 626   lock()->notify_all();
 627 }
 628 










 629 // ------------------------------------------------------------------
 630 // CompileQueue::get
 631 //
 632 // Get the next CompileTask from a CompileQueue
 633 CompileTask* CompileQueue::get() {
 634   NMethodSweeper::possibly_sweep();
 635 
 636   MutexLocker locker(lock());
 637   // If _first is NULL we have no more compile jobs. There are two reasons for
 638   // having no compile jobs: First, we compiled everything we wanted. Second,
 639   // we ran out of code cache so compilation has been disabled. In the latter
 640   // case we perform code cache sweeps to free memory such that we can re-enable
 641   // compilation.
 642   while (_first == NULL) {
 643     if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) {
 644       // Wait a certain amount of time to possibly do another sweep.
 645       // We must wait until stack scanning has happened so that we can
 646       // transition a method's state from 'not_entrant' to 'zombie'.
 647       long wait_time = NmethodSweepCheckInterval * 1000;
 648       if (FLAG_IS_DEFAULT(NmethodSweepCheckInterval)) {


 874                                               CHECK);
 875 
 876 
 877     _perf_last_failed_type =
 878              PerfDataManager::create_variable(SUN_CI, "lastFailedType",
 879                                               PerfData::U_None,
 880                                               (jlong)CompileBroker::no_compile,
 881                                               CHECK);
 882 
 883     _perf_last_invalidated_type =
 884          PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
 885                                           PerfData::U_None,
 886                                           (jlong)CompileBroker::no_compile,
 887                                           CHECK);
 888   }
 889 
 890   _initialized = true;
 891 }
 892 
 893 
 894 
 895 // ------------------------------------------------------------------
 896 // CompileBroker::make_compiler_thread
 897 CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, TRAPS) {
 898   CompilerThread* compiler_thread = NULL;
 899 
 900   Klass* k =
 901     SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
 902                                       true, CHECK_0);
 903   instanceKlassHandle klass (THREAD, k);
 904   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
 905   Handle string = java_lang_String::create_from_str(name, CHECK_0);
 906 
 907   // Initialize thread_oop to put it into the system threadGroup
 908   Handle thread_group (THREAD,  Universe::system_thread_group());
 909   JavaValue result(T_VOID);
 910   JavaCalls::call_special(&result, thread_oop,
 911                        klass,
 912                        vmSymbols::object_initializer_name(),
 913                        vmSymbols::threadgroup_string_void_signature(),
 914                        thread_group,
 915                        string,
 916                        CHECK_0);
 917 


 944     java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
 945 
 946     // Note that we cannot call os::set_priority because it expects Java
 947     // priorities and we are *explicitly* using OS priorities so that it's
 948     // possible to set the compiler thread priority higher than any Java
 949     // thread.
 950 
 951     int native_prio = CompilerThreadPriority;
 952     if (native_prio == -1) {
 953       if (UseCriticalCompilerThreadPriority) {
 954         native_prio = os::java_to_os_priority[CriticalPriority];
 955       } else {
 956         native_prio = os::java_to_os_priority[NearMaxPriority];
 957       }
 958     }
 959     os::set_native_priority(compiler_thread, native_prio);
 960 
 961     java_lang_Thread::set_daemon(thread_oop());
 962 
 963     compiler_thread->set_threadObj(thread_oop());

 964     Threads::add(compiler_thread);
 965     Thread::start(compiler_thread);
 966   }
 967 
 968   // Let go of Threads_lock before yielding
 969   os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
 970 
 971   return compiler_thread;
 972 }
 973 
 974 
 975 // ------------------------------------------------------------------
 976 // CompileBroker::init_compiler_threads
 977 //
 978 // Initialize the compilation queue
 979 void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) {
 980   EXCEPTION_MARK;
 981 #if !defined(ZERO) && !defined(SHARK)
 982   assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
 983 #endif // !ZERO && !SHARK

 984   if (c2_compiler_count > 0) {
 985     _c2_method_queue  = new CompileQueue("C2MethodQueue",  MethodCompileQueue_lock);

 986   }
 987   if (c1_compiler_count > 0) {
 988     _c1_method_queue  = new CompileQueue("C1MethodQueue",  MethodCompileQueue_lock);

 989   }
 990 
 991   int compiler_count = c1_compiler_count + c2_compiler_count;
 992 
 993   _method_threads =
 994     new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true);
 995 
 996   char name_buffer[256];
 997   for (int i = 0; i < c2_compiler_count; i++) {
 998     // Create a name for our thread.
 999     sprintf(name_buffer, "C2 CompilerThread%d", i);
1000     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1001     CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, CHECK);
1002     _method_threads->append(new_thread);

1003   }
1004 
1005   for (int i = c2_compiler_count; i < compiler_count; i++) {
1006     // Create a name for our thread.
1007     sprintf(name_buffer, "C1 CompilerThread%d", i);
1008     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1009     CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, CHECK);
1010     _method_threads->append(new_thread);

1011   }
1012 
1013   if (UsePerfData) {
1014     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes,
1015                                      compiler_count, CHECK);
1016   }
1017 }
1018 
1019 
1020 // Set the methods on the stack as on_stack so that redefine classes doesn't
1021 // reclaim them
1022 void CompileBroker::mark_on_stack() {
1023   if (_c2_method_queue != NULL) {
1024     _c2_method_queue->mark_on_stack();
1025   }
1026   if (_c1_method_queue != NULL) {
1027     _c1_method_queue->mark_on_stack();
1028   }
1029 }
1030 
1031 // ------------------------------------------------------------------
1032 // CompileBroker::is_idle
1033 bool CompileBroker::is_idle() {
1034   if (_c2_method_queue != NULL && !_c2_method_queue->is_empty()) {
1035     return false;
1036   } else if (_c1_method_queue != NULL && !_c1_method_queue->is_empty()) {
1037     return false;
1038   } else {
1039     int num_threads = _method_threads->length();
1040     for (int i=0; i<num_threads; i++) {
1041       if (_method_threads->at(i)->task() != NULL) {
1042         return false;
1043       }
1044     }
1045 
1046     // No pending or active compilations.
1047     return true;
1048   }
1049 }
1050 
1051 
1052 // ------------------------------------------------------------------
1053 // CompileBroker::compile_method
1054 //
1055 // Request compilation of a method.
1056 void CompileBroker::compile_method_base(methodHandle method,
1057                                         int osr_bci,
1058                                         int comp_level,
1059                                         methodHandle hot_method,
1060                                         int hot_count,
1061                                         const char* comment,
1062                                         Thread* thread) {
1063   // do nothing if compiler thread(s) is not available
1064   if (!_initialized ) {
1065     return;
1066   }
1067 
1068   guarantee(!method->is_abstract(), "cannot compile abstract methods");
1069   assert(method->method_holder()->oop_is_instance(),
1070          "sanity check");
1071   assert(!method->method_holder()->is_not_initialized(),
1072          "method holder must be initialized");


1534   {
1535     MutexLocker waiter(task->lock(), thread);
1536 
1537     while (!task->is_complete())
1538       task->lock()->wait();
1539   }
1540   // It is harmless to check this status without the lock, because
1541   // completion is a stable property (until the task object is recycled).
1542   assert(task->is_complete(), "Compilation should have completed");
1543   assert(task->code_handle() == NULL, "must be reset");
1544 
1545   thread->set_blocked_on_compilation(false);
1546 
1547   // By convention, the waiter is responsible for recycling a
1548   // blocking CompileTask. Since there is only one waiter ever
1549   // waiting on a CompileTask, we know that no one else will
1550   // be using this CompileTask; we can free it.
1551   free_task(task);
1552 }
1553 




























































































1554 // ------------------------------------------------------------------
1555 // CompileBroker::compiler_thread_loop
1556 //
1557 // The main loop run by a CompilerThread.
1558 void CompileBroker::compiler_thread_loop() {
1559   CompilerThread* thread = CompilerThread::current();
1560   CompileQueue* queue = thread->queue();
1561 
1562   // For the thread that initializes the ciObjectFactory
1563   // this resource mark holds all the shared objects
1564   ResourceMark rm;
1565 
1566   // First thread to get here will initialize the compiler interface
1567 
1568   if (!ciObjectFactory::is_initialized()) {
1569     ASSERT_IN_VM;
1570     MutexLocker only_one (CompileThread_lock, thread);
1571     if (!ciObjectFactory::is_initialized()) {
1572       ciObjectFactory::initialize();
1573     }
1574   }
1575 
1576   // Open a log.
1577   if (LogCompilation) {
1578     init_compiler_thread_log();
1579   }
1580   CompileLog* log = thread->log();
1581   if (log != NULL) {
1582     log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
1583                     thread->name(),
1584                     os::current_thread_id(),
1585                     os::current_process_id());
1586     log->stamp();
1587     log->end_elem();
1588   }
1589 
1590   while (true) {
1591     {









1592       // We need this HandleMark to avoid leaking VM handles.
1593       HandleMark hm(thread);
1594 
1595       if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) {
1596         // the code cache is really full
1597         handle_full_code_cache();
1598       }
1599 
1600       CompileTask* task = queue->get();
1601 
1602       // Give compiler threads an extra quanta.  They tend to be bursty and
1603       // this helps the compiler to finish up the job.
1604       if( CompilerThreadHintNoPreempt )
1605         os::hint_no_preempt();
1606 
1607       // trace per thread time and compile statistics
1608       CompilerCounters* counters = ((CompilerThread*)thread)->counters();
1609       PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter());
1610 
1611       // Assign the task to the current thread.  Mark this compilation


1625           if (CompilationRepeat != 0) {
1626             int compile_count = CompilationRepeat;
1627             while (compile_count > 0) {
1628               invoke_compiler_on_method(task);
1629               nmethod* nm = method->code();
1630               if (nm != NULL) {
1631                 nm->make_zombie();
1632                 method->clear_code();
1633               }
1634               compile_count--;
1635             }
1636           }
1637 #endif /* COMPILER1 */
1638           invoke_compiler_on_method(task);
1639         } else {
1640           // After compilation is disabled, remove remaining methods from queue
1641           method->clear_queued_for_compilation();
1642         }
1643       }
1644     }
1645   }
1646 }
1647 
1648 
1649 // ------------------------------------------------------------------
1650 // CompileBroker::init_compiler_thread_log
1651 //
1652 // Set up state required by +LogCompilation.
1653 void CompileBroker::init_compiler_thread_log() {
1654     CompilerThread* thread = CompilerThread::current();
1655     char  file_name[4*K];
1656     FILE* fp = NULL;
1657     intx thread_id = os::current_thread_id();
1658     for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) {
1659       const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL);
1660       if (dir == NULL) {
1661         jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log",
1662                      thread_id, os::current_process_id());
1663       } else {
1664         jio_snprintf(file_name, sizeof(file_name),
1665                      "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir,
1666                      os::file_separator(), thread_id, os::current_process_id());
1667       }




 169 elapsedTimer CompileBroker::_t_osr_compilation;
 170 elapsedTimer CompileBroker::_t_standard_compilation;
 171 
 172 int CompileBroker::_total_bailout_count          = 0;
 173 int CompileBroker::_total_invalidated_count      = 0;
 174 int CompileBroker::_total_compile_count          = 0;
 175 int CompileBroker::_total_osr_compile_count      = 0;
 176 int CompileBroker::_total_standard_compile_count = 0;
 177 
 178 int CompileBroker::_sum_osr_bytes_compiled       = 0;
 179 int CompileBroker::_sum_standard_bytes_compiled  = 0;
 180 int CompileBroker::_sum_nmethod_size             = 0;
 181 int CompileBroker::_sum_nmethod_code_size        = 0;
 182 
 183 long CompileBroker::_peak_compilation_time       = 0;
 184 
 185 CompileQueue* CompileBroker::_c2_method_queue    = NULL;
 186 CompileQueue* CompileBroker::_c1_method_queue    = NULL;
 187 CompileTask*  CompileBroker::_task_free_list     = NULL;
 188 
 189 GrowableArray<CompilerThread*>* CompileBroker::_compiler_threads = NULL;
 190 
 191 
 192 class CompilationLog : public StringEventLog {
 193  public:
 194   CompilationLog() : StringEventLog("Compilation events") {
 195   }
 196 
 197   void log_compile(JavaThread* thread, CompileTask* task) {
 198     StringLogMessage lm;
 199     stringStream sstr = lm.stream();
 200     // msg.time_stamp().update_to(tty->time_stamp().ticks());
 201     task->print_compilation(&sstr, NULL, true);
 202     log(thread, "%s", (const char*)lm);
 203   }
 204 
 205   void log_nmethod(JavaThread* thread, nmethod* nm) {
 206     log(thread, "nmethod %d%s " INTPTR_FORMAT " code ["INTPTR_FORMAT ", " INTPTR_FORMAT "]",
 207         nm->compile_id(), nm->is_osr_method() ? "%" : "",
 208         nm, nm->code_begin(), nm->code_end());
 209   }


 570                   _is_success, nm == NULL ? 0 : nm->content_size(),
 571                   method->invocation_count());
 572   int bec = method->backedge_count();
 573   if (bec != 0)  log->print(" backedge_count='%d'", bec);
 574   // Note:  "_is_complete" is about to be set, but is not.
 575   if (_num_inlined_bytecodes != 0) {
 576     log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);
 577   }
 578   log->stamp();
 579   log->end_elem();
 580   log->tail("task");
 581   log->clear_identities();   // next task will have different CI
 582   if (log->unflushed_count() > 2000) {
 583     log->flush();
 584   }
 585   log->mark_file_end();
 586 }
 587 
 588 
 589 



 590 // Add a CompileTask to a CompileQueue
 591 void CompileQueue::add(CompileTask* task) {
 592   assert(lock()->owned_by_self(), "must own lock");
 593 
 594   task->set_next(NULL);
 595   task->set_prev(NULL);
 596 
 597   if (_last == NULL) {
 598     // The compile queue is empty.
 599     assert(_first == NULL, "queue is empty");
 600     _first = task;
 601     _last = task;
 602   } else {
 603     // Append the task to the queue.
 604     assert(_last->next() == NULL, "not last");
 605     _last->set_next(task);
 606     task->set_prev(_last);
 607     _last = task;
 608   }
 609   ++_size;
 610 
 611   // Mark the method as being in the compile queue.
 612   task->method()->set_queued_for_compilation();
 613 
 614   if (CIPrintCompileQueue) {
 615     print();
 616   }
 617 
 618   if (LogCompilation && xtty != NULL) {
 619     task->log_task_queued();
 620   }
 621 
 622   // Notify CompilerThreads that a task is available.
 623   lock()->notify_all();
 624 }
 625 
 626 void CompileQueue::delete_all() {
 627   assert(lock()->owned_by_self(), "must own lock");
 628   if (_first != NULL) {
 629     for (CompileTask* task = _first; task != NULL; task = task->next()) {
 630       delete task;
 631     }
 632   _first = NULL;
 633   }
 634 }
 635 
 636 // ------------------------------------------------------------------
 637 // CompileQueue::get
 638 //
 639 // Get the next CompileTask from a CompileQueue
 640 CompileTask* CompileQueue::get() {
 641   NMethodSweeper::possibly_sweep();
 642 
 643   MutexLocker locker(lock());
 644   // If _first is NULL we have no more compile jobs. There are two reasons for
 645   // having no compile jobs: First, we compiled everything we wanted. Second,
 646   // we ran out of code cache so compilation has been disabled. In the latter
 647   // case we perform code cache sweeps to free memory such that we can re-enable
 648   // compilation.
 649   while (_first == NULL) {
 650     if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) {
 651       // Wait a certain amount of time to possibly do another sweep.
 652       // We must wait until stack scanning has happened so that we can
 653       // transition a method's state from 'not_entrant' to 'zombie'.
 654       long wait_time = NmethodSweepCheckInterval * 1000;
 655       if (FLAG_IS_DEFAULT(NmethodSweepCheckInterval)) {


 881                                               CHECK);
 882 
 883 
 884     _perf_last_failed_type =
 885              PerfDataManager::create_variable(SUN_CI, "lastFailedType",
 886                                               PerfData::U_None,
 887                                               (jlong)CompileBroker::no_compile,
 888                                               CHECK);
 889 
 890     _perf_last_invalidated_type =
 891          PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
 892                                           PerfData::U_None,
 893                                           (jlong)CompileBroker::no_compile,
 894                                           CHECK);
 895   }
 896 
 897   _initialized = true;
 898 }
 899 
 900 
 901 CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
 902                                                     AbstractCompiler* comp, TRAPS) {


 903   CompilerThread* compiler_thread = NULL;
 904 
 905   Klass* k =
 906     SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
 907                                       true, CHECK_0);
 908   instanceKlassHandle klass (THREAD, k);
 909   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
 910   Handle string = java_lang_String::create_from_str(name, CHECK_0);
 911 
 912   // Initialize thread_oop to put it into the system threadGroup
 913   Handle thread_group (THREAD,  Universe::system_thread_group());
 914   JavaValue result(T_VOID);
 915   JavaCalls::call_special(&result, thread_oop,
 916                        klass,
 917                        vmSymbols::object_initializer_name(),
 918                        vmSymbols::threadgroup_string_void_signature(),
 919                        thread_group,
 920                        string,
 921                        CHECK_0);
 922 


 949     java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
 950 
 951     // Note that we cannot call os::set_priority because it expects Java
 952     // priorities and we are *explicitly* using OS priorities so that it's
 953     // possible to set the compiler thread priority higher than any Java
 954     // thread.
 955 
 956     int native_prio = CompilerThreadPriority;
 957     if (native_prio == -1) {
 958       if (UseCriticalCompilerThreadPriority) {
 959         native_prio = os::java_to_os_priority[CriticalPriority];
 960       } else {
 961         native_prio = os::java_to_os_priority[NearMaxPriority];
 962       }
 963     }
 964     os::set_native_priority(compiler_thread, native_prio);
 965 
 966     java_lang_Thread::set_daemon(thread_oop());
 967 
 968     compiler_thread->set_threadObj(thread_oop());
 969     compiler_thread->set_compiler(comp);
 970     Threads::add(compiler_thread);
 971     Thread::start(compiler_thread);
 972   }
 973 
 974   // Let go of Threads_lock before yielding
 975   os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
 976 
 977   return compiler_thread;
 978 }
 979 
 980 




 981 void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) {
 982   EXCEPTION_MARK;
 983 #if !defined(ZERO) && !defined(SHARK)
 984   assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
 985 #endif // !ZERO && !SHARK
 986   // Initialize the compilation queue
 987   if (c2_compiler_count > 0) {
 988     _c2_method_queue  = new CompileQueue("C2MethodQueue",  MethodCompileQueue_lock);
 989     _compilers[1]->set_num_compiler_threads(c2_compiler_count);
 990   }
 991   if (c1_compiler_count > 0) {
 992     _c1_method_queue  = new CompileQueue("C1MethodQueue",  MethodCompileQueue_lock);
 993     _compilers[0]->set_num_compiler_threads(c1_compiler_count);
 994   }
 995 
 996   int compiler_count = c1_compiler_count + c2_compiler_count;
 997 
 998   _compiler_threads =
 999     new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true);
1000 
1001   char name_buffer[256];
1002   for (int i = 0; i < c2_compiler_count; i++) {
1003     // Create a name for our thread.
1004     sprintf(name_buffer, "C2 CompilerThread%d", i);
1005     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1006     // Shark and C2
1007     CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, _compilers[1], CHECK);
1008     _compiler_threads->append(new_thread);
1009   }
1010 
1011   for (int i = c2_compiler_count; i < compiler_count; i++) {
1012     // Create a name for our thread.
1013     sprintf(name_buffer, "C1 CompilerThread%d", i);
1014     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1015     // C1
1016     CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, _compilers[0], CHECK);
1017     _compiler_threads->append(new_thread);
1018   }
1019 
1020   if (UsePerfData) {
1021     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);

1022   }
1023 }
1024 
1025 
1026 // Set the methods on the stack as on_stack so that redefine classes doesn't
1027 // reclaim them
1028 void CompileBroker::mark_on_stack() {
1029   if (_c2_method_queue != NULL) {
1030     _c2_method_queue->mark_on_stack();
1031   }
1032   if (_c1_method_queue != NULL) {
1033     _c1_method_queue->mark_on_stack();
1034   }
1035 }
1036 
1037 // ------------------------------------------------------------------





















1038 // CompileBroker::compile_method
1039 //
1040 // Request compilation of a method.
1041 void CompileBroker::compile_method_base(methodHandle method,
1042                                         int osr_bci,
1043                                         int comp_level,
1044                                         methodHandle hot_method,
1045                                         int hot_count,
1046                                         const char* comment,
1047                                         Thread* thread) {
1048   // do nothing if compiler thread(s) is not available
1049   if (!_initialized ) {
1050     return;
1051   }
1052 
1053   guarantee(!method->is_abstract(), "cannot compile abstract methods");
1054   assert(method->method_holder()->oop_is_instance(),
1055          "sanity check");
1056   assert(!method->method_holder()->is_not_initialized(),
1057          "method holder must be initialized");


1519   {
1520     MutexLocker waiter(task->lock(), thread);
1521 
1522     while (!task->is_complete())
1523       task->lock()->wait();
1524   }
1525   // It is harmless to check this status without the lock, because
1526   // completion is a stable property (until the task object is recycled).
1527   assert(task->is_complete(), "Compilation should have completed");
1528   assert(task->code_handle() == NULL, "must be reset");
1529 
1530   thread->set_blocked_on_compilation(false);
1531 
1532   // By convention, the waiter is responsible for recycling a
1533   // blocking CompileTask. Since there is only one waiter ever
1534   // waiting on a CompileTask, we know that no one else will
1535   // be using this CompileTask; we can free it.
1536   free_task(task);
1537 }
1538 
1539 // Initialize compiler thread(s) + compiler object(s).
1540 bool CompileBroker::init_compiler_runtime() {
1541   CompilerThread* thread = CompilerThread::current();
1542   AbstractCompiler* comp = thread->compiler();
1543   // Final sanity check - the compiler object must exist
1544   guarantee(comp != NULL, "Compiler object must exist");
1545 
1546   int system_dictionary_modification_counter;
1547   {
1548     MutexLocker locker(Compile_lock, thread);
1549     system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1550   }
1551 
1552   {
1553     // Must switch to native to allocate ci_env
1554     ThreadToNativeFromVM ttn(thread);
1555     ciEnv ci_env(NULL, system_dictionary_modification_counter);
1556     // Cache Jvmti state
1557     ci_env.cache_jvmti_state();
1558     // Cache DTrace flags
1559     ci_env.cache_dtrace_flags();
1560 
1561     // Switch back to VM state to to compiler initialization
1562     ThreadInVMfromNative tv(thread);
1563     ResetNoHandleMark rnhm;
1564 
1565     if (!comp->is_shark()) {
1566       // Perform per-thread and global initializations
1567       comp->initialize();
1568     }
1569   }
1570 
1571   if (comp->is_state_failed()) {
1572     disable_compilation_forever();
1573     // If compiler initialization failed, no compiler thread that is specific to a
1574     // particular compiler runtime will ever start to compile methods.
1575 
1576     // Tiered: If C1 and/or C2 initialization failed, we shut down all compilation.
1577     // We do this to keep things simple. This can be changed if it ever turns out to be
1578     // a problem.
1579 
1580     if (comp->should_perform_shutdown()) {
1581       MutexLocker mu(MethodCompileQueue_lock, thread);
1582       CompileQueue* queue;
1583       if (_c1_method_queue != NULL) {
1584         _c1_method_queue->delete_all();
1585         queue = _c1_method_queue;
1586         _c1_method_queue = NULL;
1587         delete _c1_method_queue;
1588       }
1589 
1590       if (_c2_method_queue != NULL) {
1591         _c2_method_queue->delete_all();
1592         queue = _c2_method_queue;
1593         _c2_method_queue = NULL;
1594         delete _c2_method_queue;
1595       }
1596     }
1597     warning("Initialization of %s failed -- shutting down compiler(s)", comp->name());
1598 
1599     // Free buffer blob, if allocated
1600     if (thread->get_buffer_blob() != NULL) {
1601       CodeCache::free(thread->get_buffer_blob());
1602     }
1603 
1604     // Set state to shut down
1605     comp->set_shut_down();
1606 
1607     // Delete compiler runtimes
1608     MutexLocker mu(MethodCompileQueue_lock, thread);
1609     if ((comp->is_c1()) && (_compilers[0] != NULL)) {
1610       delete _compilers[0];
1611       _compilers[0] = NULL;
1612     }
1613 
1614     if ((comp->is_c2()) && (_compilers[1] != NULL)) {
1615       delete _compilers[1];
1616       _compilers[1] = NULL;
1617     }
1618 
1619     return false;
1620   }
1621 
1622    // C1 specific check
1623   if ((comp->is_c1()) && (thread->get_buffer_blob() == NULL)) {
1624     warning("Initialization of %s thread failed", comp->name());
1625     return false;
1626   }
1627 
1628   return true;
1629 }
1630 
1631 // ------------------------------------------------------------------
1632 // CompileBroker::compiler_thread_loop
1633 //
1634 // The main loop run by a CompilerThread.
1635 void CompileBroker::compiler_thread_loop() {
1636   CompilerThread* thread = CompilerThread::current();
1637   CompileQueue* queue = thread->queue();

1638   // For the thread that initializes the ciObjectFactory
1639   // this resource mark holds all the shared objects
1640   ResourceMark rm;
1641 
1642   // First thread to get here will initialize the compiler interface
1643 
1644   if (!ciObjectFactory::is_initialized()) {
1645     ASSERT_IN_VM;
1646     MutexLocker only_one (CompileThread_lock, thread);
1647     if (!ciObjectFactory::is_initialized()) {
1648       ciObjectFactory::initialize();
1649     }
1650   }
1651 
1652   // Open a log.
1653   if (LogCompilation) {
1654     init_compiler_thread_log();
1655   }
1656   CompileLog* log = thread->log();
1657   if (log != NULL) {
1658     log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
1659                     thread->name(),
1660                     os::current_thread_id(),
1661                     os::current_process_id());
1662     log->stamp();
1663     log->end_elem();
1664   }
1665 
1666   // If compiler thread/runtime initialization fails, exit the compiler thread
1667   if (!init_compiler_runtime()) {
1668     return;
1669   }
1670 
1671   // Poll for new compilation tasks as long as the JVM runs. Compilation
1672   // should only be disabled if something went wrong while initializing the
1673   // compiler runtimes. This, in turn, should not happen. The only known case
1674   // when compiler runtime initialization fails is if there is not enough free
1675   // space in the code cache to generate the necessary stubs, etc.
1676   while (!is_compilation_disabled_forever()) {
1677     // We need this HandleMark to avoid leaking VM handles.
1678     HandleMark hm(thread);
1679 
1680     if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) {
1681       // the code cache is really full
1682       handle_full_code_cache();
1683     }
1684 
1685     CompileTask* task = queue->get();
1686 
1687     // Give compiler threads an extra quanta.  They tend to be bursty and
1688     // this helps the compiler to finish up the job.
1689     if( CompilerThreadHintNoPreempt )
1690       os::hint_no_preempt();
1691 
1692     // trace per thread time and compile statistics
1693     CompilerCounters* counters = ((CompilerThread*)thread)->counters();
1694     PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter());
1695 
1696     // Assign the task to the current thread.  Mark this compilation


1710         if (CompilationRepeat != 0) {
1711           int compile_count = CompilationRepeat;
1712           while (compile_count > 0) {
1713             invoke_compiler_on_method(task);
1714             nmethod* nm = method->code();
1715             if (nm != NULL) {
1716               nm->make_zombie();
1717               method->clear_code();
1718             }
1719             compile_count--;
1720           }
1721         }
1722 #endif /* COMPILER1 */
1723         invoke_compiler_on_method(task);
1724       } else {
1725         // After compilation is disabled, remove remaining methods from queue
1726         method->clear_queued_for_compilation();
1727       }
1728     }
1729   }

1730 }

1731 
1732 // ------------------------------------------------------------------
1733 // CompileBroker::init_compiler_thread_log
1734 //
1735 // Set up state required by +LogCompilation.
1736 void CompileBroker::init_compiler_thread_log() {
1737     CompilerThread* thread = CompilerThread::current();
1738     char  file_name[4*K];
1739     FILE* fp = NULL;
1740     intx thread_id = os::current_thread_id();
1741     for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) {
1742       const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL);
1743       if (dir == NULL) {
1744         jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log",
1745                      thread_id, os::current_process_id());
1746       } else {
1747         jio_snprintf(file_name, sizeof(file_name),
1748                      "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir,
1749                      os::file_separator(), thread_id, os::current_process_id());
1750       }