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)) {
 649         // Only one thread at a time can do sweeping. Scale the
 650         // wait time according to the number of compiler threads.
 651         // As a result, the next sweep is likely to happen every 100ms
 652         // with an arbitrary number of threads that do sweeping.
 653         wait_time = 100 * CICompilerCount;
 654       }
 655       bool timeout = lock()->wait(!Mutex::_no_safepoint_check_flag, wait_time);
 656       if (timeout) {
 657         MutexUnlocker ul(lock());
 658         NMethodSweeper::possibly_sweep();
 659       }
 660     } else {
 661       // If there are no compilation tasks and we can compile new jobs
 662       // (i.e., there is enough free space in the code cache) there is
 663       // no need to invoke the sweeper. As a result, the hotness of methods
 664       // remains unchanged. This behavior is desired, since we want to keep
 665       // the stable state, i.e., we do not want to evict methods from the
 666       // code cache if it is unnecessary.
 667       lock()->wait();
 668     }
 669   }





 670   CompileTask* task = CompilationPolicy::policy()->select_task(this);
 671   remove(task);
 672   return task;
 673 }
 674 
 675 void CompileQueue::remove(CompileTask* task)
 676 {
 677    assert(lock()->owned_by_self(), "must own lock");
 678   if (task->prev() != NULL) {
 679     task->prev()->set_next(task->next());
 680   } else {
 681     // max is the first element
 682     assert(task == _first, "Sanity");
 683     _first = task->next();
 684   }
 685 
 686   if (task->next() != NULL) {
 687     task->next()->set_prev(task->prev());
 688   } else {
 689     // max is the last element


 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
1612       // thread as active for the profiler.
1613       CompileTaskWrapper ctw(task);
1614       nmethodLocker result_handle;  // (handle for the nmethod produced by this task)
1615       task->set_code_handle(&result_handle);
1616       methodHandle method(thread, task->method());
1617 
1618       // Never compile a method if breakpoints are present in it
1619       if (method()->number_of_breakpoints() == 0) {
1620         // Compile the method.


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       }


1939     warning("Try increasing the code cache size using -XX:ReservedCodeCacheSize=");
1940 
1941     CodeCache::report_codemem_full();
1942 
1943 
1944 #ifndef PRODUCT
1945     if (CompileTheWorld || ExitOnFullCodeCache) {
1946       codecache_print(/* detailed= */ true);
1947       before_exit(JavaThread::current());
1948       exit_globals(); // will delete tty
1949       vm_direct_exit(CompileTheWorld ? 0 : 1);
1950     }
1951 #endif
1952     if (UseCodeCacheFlushing) {
1953       // Since code cache is full, immediately stop new compiles
1954       if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) {
1955         NMethodSweeper::log_sweep("disable_compiler");
1956         NMethodSweeper::possibly_sweep();
1957       }
1958     } else {
1959       UseCompiler               = false;
1960       AlwaysCompileLoopMethods  = false;
1961     }
1962   }
1963   codecache_print(/* detailed= */ true);
1964 }
1965 
1966 // ------------------------------------------------------------------
1967 // CompileBroker::set_last_compile
1968 //
1969 // Record this compilation for debugging purposes.
1970 void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) {
1971   ResourceMark rm;
1972   char* method_name = method->name()->as_C_string();
1973   strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length);
1974   char current_method[CompilerCounters::cmname_buffer_length];
1975   size_t maxLen = CompilerCounters::cmname_buffer_length;
1976 
1977   if (UsePerfData) {
1978     const char* class_name = method->method_holder()->name()->as_C_string();
1979 
1980     size_t s1len = strlen(class_name);




 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     // Exit loop if compilation is disabled forever
 651     if (CompileBroker::is_compilation_disabled_forever()) {
 652       return NULL;
 653     }
 654 
 655     if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) {
 656       // Wait a certain amount of time to possibly do another sweep.
 657       // We must wait until stack scanning has happened so that we can
 658       // transition a method's state from 'not_entrant' to 'zombie'.
 659       long wait_time = NmethodSweepCheckInterval * 1000;
 660       if (FLAG_IS_DEFAULT(NmethodSweepCheckInterval)) {
 661         // Only one thread at a time can do sweeping. Scale the
 662         // wait time according to the number of compiler threads.
 663         // As a result, the next sweep is likely to happen every 100ms
 664         // with an arbitrary number of threads that do sweeping.
 665         wait_time = 100 * CICompilerCount;
 666       }
 667       bool timeout = lock()->wait(!Mutex::_no_safepoint_check_flag, wait_time);
 668       if (timeout) {
 669         MutexUnlocker ul(lock());
 670         NMethodSweeper::possibly_sweep();
 671       }
 672     } else {
 673       // If there are no compilation tasks and we can compile new jobs
 674       // (i.e., there is enough free space in the code cache) there is
 675       // no need to invoke the sweeper. As a result, the hotness of methods
 676       // remains unchanged. This behavior is desired, since we want to keep
 677       // the stable state, i.e., we do not want to evict methods from the
 678       // code cache if it is unnecessary.
 679       lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
 680     }
 681   }
 682 
 683   if (CompileBroker::is_compilation_disabled_forever()) {
 684     return NULL;
 685   }
 686 
 687   CompileTask* task = CompilationPolicy::policy()->select_task(this);
 688   remove(task);
 689   return task;
 690 }
 691 
 692 void CompileQueue::remove(CompileTask* task)
 693 {
 694    assert(lock()->owned_by_self(), "must own lock");
 695   if (task->prev() != NULL) {
 696     task->prev()->set_next(task->next());
 697   } else {
 698     // max is the first element
 699     assert(task == _first, "Sanity");
 700     _first = task->next();
 701   }
 702 
 703   if (task->next() != NULL) {
 704     task->next()->set_prev(task->prev());
 705   } else {
 706     // max is the last element


 891                                               CHECK);
 892 
 893 
 894     _perf_last_failed_type =
 895              PerfDataManager::create_variable(SUN_CI, "lastFailedType",
 896                                               PerfData::U_None,
 897                                               (jlong)CompileBroker::no_compile,
 898                                               CHECK);
 899 
 900     _perf_last_invalidated_type =
 901          PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
 902                                           PerfData::U_None,
 903                                           (jlong)CompileBroker::no_compile,
 904                                           CHECK);
 905   }
 906 
 907   _initialized = true;
 908 }
 909 
 910 
 911 CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
 912                                                     AbstractCompiler* comp, TRAPS) {


 913   CompilerThread* compiler_thread = NULL;
 914 
 915   Klass* k =
 916     SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
 917                                       true, CHECK_0);
 918   instanceKlassHandle klass (THREAD, k);
 919   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
 920   Handle string = java_lang_String::create_from_str(name, CHECK_0);
 921 
 922   // Initialize thread_oop to put it into the system threadGroup
 923   Handle thread_group (THREAD,  Universe::system_thread_group());
 924   JavaValue result(T_VOID);
 925   JavaCalls::call_special(&result, thread_oop,
 926                        klass,
 927                        vmSymbols::object_initializer_name(),
 928                        vmSymbols::threadgroup_string_void_signature(),
 929                        thread_group,
 930                        string,
 931                        CHECK_0);
 932 


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




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

1032   }
1033 }
1034 
1035 
1036 // Set the methods on the stack as on_stack so that redefine classes doesn't
1037 // reclaim them
1038 void CompileBroker::mark_on_stack() {
1039   if (_c2_method_queue != NULL) {
1040     _c2_method_queue->mark_on_stack();
1041   }
1042   if (_c1_method_queue != NULL) {
1043     _c1_method_queue->mark_on_stack();
1044   }
1045 }
1046 
1047 // ------------------------------------------------------------------





















1048 // CompileBroker::compile_method
1049 //
1050 // Request compilation of a method.
1051 void CompileBroker::compile_method_base(methodHandle method,
1052                                         int osr_bci,
1053                                         int comp_level,
1054                                         methodHandle hot_method,
1055                                         int hot_count,
1056                                         const char* comment,
1057                                         Thread* thread) {
1058   // do nothing if compiler thread(s) is not available
1059   if (!_initialized ) {
1060     return;
1061   }
1062 
1063   guarantee(!method->is_abstract(), "cannot compile abstract methods");
1064   assert(method->method_holder()->oop_is_instance(),
1065          "sanity check");
1066   assert(!method->method_holder()->is_not_initialized(),
1067          "method holder must be initialized");


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

1655   // For the thread that initializes the ciObjectFactory
1656   // this resource mark holds all the shared objects
1657   ResourceMark rm;
1658 
1659   // First thread to get here will initialize the compiler interface
1660 
1661   if (!ciObjectFactory::is_initialized()) {
1662     ASSERT_IN_VM;
1663     MutexLocker only_one (CompileThread_lock, thread);
1664     if (!ciObjectFactory::is_initialized()) {
1665       ciObjectFactory::initialize();
1666     }
1667   }
1668 
1669   // Open a log.
1670   if (LogCompilation) {
1671     init_compiler_thread_log();
1672   }
1673   CompileLog* log = thread->log();
1674   if (log != NULL) {
1675     log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
1676                     thread->name(),
1677                     os::current_thread_id(),
1678                     os::current_process_id());
1679     log->stamp();
1680     log->end_elem();
1681   }
1682 
1683   // If compiler thread/runtime initialization fails, exit the compiler thread
1684   if (!init_compiler_runtime()) {
1685     return;
1686   }
1687 
1688   // Poll for new compilation tasks as long as the JVM runs. Compilation
1689   // should only be disabled if something went wrong while initializing the
1690   // compiler runtimes. This, in turn, should not happen. The only known case
1691   // when compiler runtime initialization fails is if there is not enough free
1692   // space in the code cache to generate the necessary stubs, etc.
1693   while (!is_compilation_disabled_forever()) {
1694     // We need this HandleMark to avoid leaking VM handles.
1695     HandleMark hm(thread);
1696 
1697     if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) {
1698       // the code cache is really full
1699       handle_full_code_cache();
1700     }
1701 
1702     CompileTask* task = queue->get();
1703     if (task == NULL) {
1704       continue;
1705     }
1706 
1707     // Give compiler threads an extra quanta.  They tend to be bursty and
1708     // this helps the compiler to finish up the job.
1709     if( CompilerThreadHintNoPreempt )
1710       os::hint_no_preempt();
1711 
1712     // trace per thread time and compile statistics
1713     CompilerCounters* counters = ((CompilerThread*)thread)->counters();
1714     PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter());
1715 
1716     // Assign the task to the current thread.  Mark this compilation
1717     // thread as active for the profiler.
1718     CompileTaskWrapper ctw(task);
1719     nmethodLocker result_handle;  // (handle for the nmethod produced by this task)
1720     task->set_code_handle(&result_handle);
1721     methodHandle method(thread, task->method());
1722 
1723     // Never compile a method if breakpoints are present in it
1724     if (method()->number_of_breakpoints() == 0) {
1725       // Compile the method.


1730         if (CompilationRepeat != 0) {
1731           int compile_count = CompilationRepeat;
1732           while (compile_count > 0) {
1733             invoke_compiler_on_method(task);
1734             nmethod* nm = method->code();
1735             if (nm != NULL) {
1736               nm->make_zombie();
1737               method->clear_code();
1738             }
1739             compile_count--;
1740           }
1741         }
1742 #endif /* COMPILER1 */
1743         invoke_compiler_on_method(task);
1744       } else {
1745         // After compilation is disabled, remove remaining methods from queue
1746         method->clear_queued_for_compilation();
1747       }
1748     }
1749   }


1750 
1751   // Shut down compiler runtime
1752   shutdown_compiler_runtime(thread->compiler(), thread);
1753 }
1754 
1755 // ------------------------------------------------------------------
1756 // CompileBroker::init_compiler_thread_log
1757 //
1758 // Set up state required by +LogCompilation.
1759 void CompileBroker::init_compiler_thread_log() {
1760     CompilerThread* thread = CompilerThread::current();
1761     char  file_name[4*K];
1762     FILE* fp = NULL;
1763     intx thread_id = os::current_thread_id();
1764     for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) {
1765       const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL);
1766       if (dir == NULL) {
1767         jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log",
1768                      thread_id, os::current_process_id());
1769       } else {
1770         jio_snprintf(file_name, sizeof(file_name),
1771                      "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir,
1772                      os::file_separator(), thread_id, os::current_process_id());
1773       }


2045     warning("Try increasing the code cache size using -XX:ReservedCodeCacheSize=");
2046 
2047     CodeCache::report_codemem_full();
2048 
2049 
2050 #ifndef PRODUCT
2051     if (CompileTheWorld || ExitOnFullCodeCache) {
2052       codecache_print(/* detailed= */ true);
2053       before_exit(JavaThread::current());
2054       exit_globals(); // will delete tty
2055       vm_direct_exit(CompileTheWorld ? 0 : 1);
2056     }
2057 #endif
2058     if (UseCodeCacheFlushing) {
2059       // Since code cache is full, immediately stop new compiles
2060       if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) {
2061         NMethodSweeper::log_sweep("disable_compiler");
2062         NMethodSweeper::possibly_sweep();
2063       }
2064     } else {
2065       disable_compilation_forever();

2066     }
2067   }
2068   codecache_print(/* detailed= */ true);
2069 }
2070 
2071 // ------------------------------------------------------------------
2072 // CompileBroker::set_last_compile
2073 //
2074 // Record this compilation for debugging purposes.
2075 void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) {
2076   ResourceMark rm;
2077   char* method_name = method->name()->as_C_string();
2078   strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length);
2079   char current_method[CompilerCounters::cmname_buffer_length];
2080   size_t maxLen = CompilerCounters::cmname_buffer_length;
2081 
2082   if (UsePerfData) {
2083     const char* class_name = method->method_holder()->name()->as_C_string();
2084 
2085     size_t s1len = strlen(class_name);