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   }


 857                                               CHECK);
 858 
 859 
 860     _perf_last_failed_type =
 861              PerfDataManager::create_variable(SUN_CI, "lastFailedType",
 862                                               PerfData::U_None,
 863                                               (jlong)CompileBroker::no_compile,
 864                                               CHECK);
 865 
 866     _perf_last_invalidated_type =
 867          PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
 868                                           PerfData::U_None,
 869                                           (jlong)CompileBroker::no_compile,
 870                                           CHECK);
 871   }
 872 
 873   _initialized = true;
 874 }
 875 
 876 
 877 
 878 // ------------------------------------------------------------------
 879 // CompileBroker::make_compiler_thread
 880 CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, TRAPS) {
 881   CompilerThread* compiler_thread = NULL;
 882 
 883   Klass* k =
 884     SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
 885                                       true, CHECK_0);
 886   instanceKlassHandle klass (THREAD, k);
 887   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
 888   Handle string = java_lang_String::create_from_str(name, CHECK_0);
 889 
 890   // Initialize thread_oop to put it into the system threadGroup
 891   Handle thread_group (THREAD,  Universe::system_thread_group());
 892   JavaValue result(T_VOID);
 893   JavaCalls::call_special(&result, thread_oop,
 894                        klass,
 895                        vmSymbols::object_initializer_name(),
 896                        vmSymbols::threadgroup_string_void_signature(),
 897                        thread_group,
 898                        string,
 899                        CHECK_0);
 900 


 927     java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
 928 
 929     // Note that we cannot call os::set_priority because it expects Java
 930     // priorities and we are *explicitly* using OS priorities so that it's
 931     // possible to set the compiler thread priority higher than any Java
 932     // thread.
 933 
 934     int native_prio = CompilerThreadPriority;
 935     if (native_prio == -1) {
 936       if (UseCriticalCompilerThreadPriority) {
 937         native_prio = os::java_to_os_priority[CriticalPriority];
 938       } else {
 939         native_prio = os::java_to_os_priority[NearMaxPriority];
 940       }
 941     }
 942     os::set_native_priority(compiler_thread, native_prio);
 943 
 944     java_lang_Thread::set_daemon(thread_oop());
 945 
 946     compiler_thread->set_threadObj(thread_oop());

 947     Threads::add(compiler_thread);
 948     Thread::start(compiler_thread);
 949   }
 950 
 951   // Let go of Threads_lock before yielding
 952   os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
 953 
 954   return compiler_thread;
 955 }
 956 
 957 
 958 // ------------------------------------------------------------------
 959 // CompileBroker::init_compiler_threads
 960 //
 961 // Initialize the compilation queue
 962 void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) {
 963   EXCEPTION_MARK;
 964 #if !defined(ZERO) && !defined(SHARK)
 965   assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
 966 #endif // !ZERO && !SHARK

 967   if (c2_compiler_count > 0) {
 968     _c2_method_queue  = new CompileQueue("C2MethodQueue",  MethodCompileQueue_lock);
 969   }
 970   if (c1_compiler_count > 0) {
 971     _c1_method_queue  = new CompileQueue("C1MethodQueue",  MethodCompileQueue_lock);
 972   }
 973 
 974   int compiler_count = c1_compiler_count + c2_compiler_count;
 975 
 976   _method_threads =
 977     new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true);
 978 
 979   char name_buffer[256];
 980   for (int i = 0; i < c2_compiler_count; i++) {
 981     // Create a name for our thread.
 982     sprintf(name_buffer, "C2 CompilerThread%d", i);
 983     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
 984     CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, CHECK);
 985     _method_threads->append(new_thread);

 986   }
 987 
 988   for (int i = c2_compiler_count; i < compiler_count; i++) {
 989     // Create a name for our thread.
 990     sprintf(name_buffer, "C1 CompilerThread%d", i);
 991     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
 992     CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, CHECK);
 993     _method_threads->append(new_thread);

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


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













































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





1579 
1580   while (true) {
1581     {
1582       // We need this HandleMark to avoid leaking VM handles.
1583       HandleMark hm(thread);
1584 
1585       if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) {
1586         // the code cache is really full
1587         handle_full_code_cache();
1588       } else if (UseCodeCacheFlushing && CodeCache::needs_flushing()) {
1589         // Attempt to start cleaning the code cache while there is still a little headroom
1590         NMethodSweeper::handle_full_code_cache(false);
1591       }
1592 
1593       CompileTask* task = queue->get();
1594 
1595       // Give compiler threads an extra quanta.  They tend to be bursty and
1596       // this helps the compiler to finish up the job.
1597       if( CompilerThreadHintNoPreempt )
1598         os::hint_no_preempt();




 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   }


 857                                               CHECK);
 858 
 859 
 860     _perf_last_failed_type =
 861              PerfDataManager::create_variable(SUN_CI, "lastFailedType",
 862                                               PerfData::U_None,
 863                                               (jlong)CompileBroker::no_compile,
 864                                               CHECK);
 865 
 866     _perf_last_invalidated_type =
 867          PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
 868                                           PerfData::U_None,
 869                                           (jlong)CompileBroker::no_compile,
 870                                           CHECK);
 871   }
 872 
 873   _initialized = true;
 874 }
 875 
 876 
 877 CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
 878                                                     AbstractCompiler* comp, TRAPS) {


 879   CompilerThread* compiler_thread = NULL;
 880 
 881   Klass* k =
 882     SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
 883                                       true, CHECK_0);
 884   instanceKlassHandle klass (THREAD, k);
 885   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
 886   Handle string = java_lang_String::create_from_str(name, CHECK_0);
 887 
 888   // Initialize thread_oop to put it into the system threadGroup
 889   Handle thread_group (THREAD,  Universe::system_thread_group());
 890   JavaValue result(T_VOID);
 891   JavaCalls::call_special(&result, thread_oop,
 892                        klass,
 893                        vmSymbols::object_initializer_name(),
 894                        vmSymbols::threadgroup_string_void_signature(),
 895                        thread_group,
 896                        string,
 897                        CHECK_0);
 898 


 925     java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
 926 
 927     // Note that we cannot call os::set_priority because it expects Java
 928     // priorities and we are *explicitly* using OS priorities so that it's
 929     // possible to set the compiler thread priority higher than any Java
 930     // thread.
 931 
 932     int native_prio = CompilerThreadPriority;
 933     if (native_prio == -1) {
 934       if (UseCriticalCompilerThreadPriority) {
 935         native_prio = os::java_to_os_priority[CriticalPriority];
 936       } else {
 937         native_prio = os::java_to_os_priority[NearMaxPriority];
 938       }
 939     }
 940     os::set_native_priority(compiler_thread, native_prio);
 941 
 942     java_lang_Thread::set_daemon(thread_oop());
 943 
 944     compiler_thread->set_threadObj(thread_oop());
 945     compiler_thread->set_compiler(comp);
 946     Threads::add(compiler_thread);
 947     Thread::start(compiler_thread);
 948   }
 949 
 950   // Let go of Threads_lock before yielding
 951   os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
 952 
 953   return compiler_thread;
 954 }
 955 
 956 




 957 void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) {
 958   EXCEPTION_MARK;
 959 #if !defined(ZERO) && !defined(SHARK)
 960   assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
 961 #endif // !ZERO && !SHARK
 962   // Initialize the compilation queue
 963   if (c2_compiler_count > 0) {
 964     _c2_method_queue  = new CompileQueue("C2MethodQueue",  MethodCompileQueue_lock);
 965   }
 966   if (c1_compiler_count > 0) {
 967     _c1_method_queue  = new CompileQueue("C1MethodQueue",  MethodCompileQueue_lock);
 968   }
 969 
 970   int compiler_count = c1_compiler_count + c2_compiler_count;
 971 
 972   _compiler_threads =
 973     new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true);
 974 
 975   char name_buffer[256];
 976   for (int i = 0; i < c2_compiler_count; i++) {
 977     // Create a name for our thread.
 978     sprintf(name_buffer, "C2 CompilerThread%d", i);
 979     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
 980     // Shark and C2
 981     CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, _compilers[1], CHECK);
 982     _compiler_threads->append(new_thread);
 983   }
 984 
 985   for (int i = c2_compiler_count; i < compiler_count; i++) {
 986     // Create a name for our thread.
 987     sprintf(name_buffer, "C1 CompilerThread%d", i);
 988     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
 989     // C1
 990     CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, _compilers[0], CHECK);
 991     _compiler_threads->append(new_thread);
 992   }
 993 
 994   if (UsePerfData) {
 995     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);

 996   }
 997 }
 998 
 999 
1000 // Set the methods on the stack as on_stack so that redefine classes doesn't
1001 // reclaim them
1002 void CompileBroker::mark_on_stack() {
1003   if (_c2_method_queue != NULL) {
1004     _c2_method_queue->mark_on_stack();
1005   }
1006   if (_c1_method_queue != NULL) {
1007     _c1_method_queue->mark_on_stack();
1008   }
1009 }
1010 
1011 // ------------------------------------------------------------------





















1012 // CompileBroker::compile_method
1013 //
1014 // Request compilation of a method.
1015 void CompileBroker::compile_method_base(methodHandle method,
1016                                         int osr_bci,
1017                                         int comp_level,
1018                                         methodHandle hot_method,
1019                                         int hot_count,
1020                                         const char* comment,
1021                                         Thread* thread) {
1022   // do nothing if compiler thread(s) is not available
1023   if (!_initialized ) {
1024     return;
1025   }
1026 
1027   guarantee(!method->is_abstract(), "cannot compile abstract methods");
1028   assert(method->method_holder()->oop_is_instance(),
1029          "sanity check");
1030   assert(!method->method_holder()->is_not_initialized(),
1031          "method holder must be initialized");


1500   {
1501     MutexLocker waiter(task->lock(), thread);
1502 
1503     while (!task->is_complete())
1504       task->lock()->wait();
1505   }
1506   // It is harmless to check this status without the lock, because
1507   // completion is a stable property (until the task object is recycled).
1508   assert(task->is_complete(), "Compilation should have completed");
1509   assert(task->code_handle() == NULL, "must be reset");
1510 
1511   thread->set_blocked_on_compilation(false);
1512 
1513   // By convention, the waiter is responsible for recycling a
1514   // blocking CompileTask. Since there is only one waiter ever
1515   // waiting on a CompileTask, we know that no one else will
1516   // be using this CompileTask; we can free it.
1517   free_task(task);
1518 }
1519 
1520 // Initialize compiler thread(s) + compiler object(s).
1521 bool CompileBroker::init_compiler_runtime() {
1522   CompilerThread* thread = CompilerThread::current();
1523   AbstractCompiler* comp = thread->compiler();
1524   // Final sanity check - the compiler object must exist
1525   guarantee(comp != NULL, "Compiler object must exist");
1526 
1527   int system_dictionary_modification_counter;
1528   {
1529     MutexLocker locker(Compile_lock, thread);
1530     system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1531   }
1532 
1533   {
1534     // Must switch to native to allocate ci_env
1535     ThreadToNativeFromVM ttn(thread);
1536     ciEnv ci_env(NULL, system_dictionary_modification_counter);
1537     // Cache Jvmti state
1538     ci_env.cache_jvmti_state();
1539     // Cache DTrace flags
1540     ci_env.cache_dtrace_flags();
1541 
1542     // Switch back to VM state to to compiler initialization
1543     CompilerThread* thread = CompilerThread::current();
1544     ThreadInVMfromNative tv(thread);
1545     ResetNoHandleMark rnhm;
1546 
1547     if (!comp->is_shark()) {
1548       // Perform per-thread and global initializations
1549       comp->initialize();
1550       if ((thread->compiler()->is_c1()) && (thread->get_buffer_blob() == NULL)) {
1551         warning("Initialization of %s thread failed", comp->name());
1552         return false;
1553       }
1554     }
1555   }
1556 
1557   if (comp->is_state_failed()) {
1558     warning("Initialization of %s thread failed", comp->name());
1559     return false;
1560   }
1561 
1562   return true;
1563 }
1564 
1565 // ------------------------------------------------------------------
1566 // CompileBroker::compiler_thread_loop
1567 //
1568 // The main loop run by a CompilerThread.
1569 void CompileBroker::compiler_thread_loop() {
1570   CompilerThread* thread = CompilerThread::current();
1571   CompileQueue* queue = thread->queue();

1572   // For the thread that initializes the ciObjectFactory
1573   // this resource mark holds all the shared objects
1574   ResourceMark rm;
1575 
1576   // First thread to get here will initialize the compiler interface
1577 
1578   if (!ciObjectFactory::is_initialized()) {
1579     ASSERT_IN_VM;
1580     MutexLocker only_one (CompileThread_lock, thread);
1581     if (!ciObjectFactory::is_initialized()) {
1582       ciObjectFactory::initialize();
1583     }
1584   }
1585 
1586   // Open a log.
1587   if (LogCompilation) {
1588     init_compiler_thread_log();
1589   }
1590   CompileLog* log = thread->log();
1591   if (log != NULL) {
1592     log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
1593                     thread->name(),
1594                     os::current_thread_id(),
1595                     os::current_process_id());
1596     log->stamp();
1597     log->end_elem();
1598   }
1599 
1600  // If compiler thread/runtime initialization fails, exit the compiler thread
1601  if (!init_compiler_runtime()) {
1602    return;
1603  }
1604 
1605   while (true) {
1606     {
1607       // We need this HandleMark to avoid leaking VM handles.
1608       HandleMark hm(thread);
1609 
1610       if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) {
1611         // the code cache is really full
1612         handle_full_code_cache();
1613       } else if (UseCodeCacheFlushing && CodeCache::needs_flushing()) {
1614         // Attempt to start cleaning the code cache while there is still a little headroom
1615         NMethodSweeper::handle_full_code_cache(false);
1616       }
1617 
1618       CompileTask* task = queue->get();
1619 
1620       // Give compiler threads an extra quanta.  They tend to be bursty and
1621       // this helps the compiler to finish up the job.
1622       if( CompilerThreadHintNoPreempt )
1623         os::hint_no_preempt();