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 // We need a timed wait here, since compiler threads can exit if compilation
680 // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
681 // is not critical and we do not want idle compiler threads to wake up too often.
682 lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
683 }
684 }
685
686 if (CompileBroker::is_compilation_disabled_forever()) {
687 return NULL;
688 }
689
690 CompileTask* task = CompilationPolicy::policy()->select_task(this);
691 remove(task);
692 return task;
693 }
694
695 void CompileQueue::remove(CompileTask* task)
696 {
697 assert(lock()->owned_by_self(), "must own lock");
698 if (task->prev() != NULL) {
699 task->prev()->set_next(task->next());
700 } else {
701 // max is the first element
702 assert(task == _first, "Sanity");
703 _first = task->next();
704 }
705
706 if (task->next() != NULL) {
707 task->next()->set_prev(task->prev());
708 } else {
709 // max is the last element
894 CHECK);
895
896
897 _perf_last_failed_type =
898 PerfDataManager::create_variable(SUN_CI, "lastFailedType",
899 PerfData::U_None,
900 (jlong)CompileBroker::no_compile,
901 CHECK);
902
903 _perf_last_invalidated_type =
904 PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
905 PerfData::U_None,
906 (jlong)CompileBroker::no_compile,
907 CHECK);
908 }
909
910 _initialized = true;
911 }
912
913
914 CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
915 AbstractCompiler* comp, TRAPS) {
916 CompilerThread* compiler_thread = NULL;
917
918 Klass* k =
919 SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
920 true, CHECK_0);
921 instanceKlassHandle klass (THREAD, k);
922 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
923 Handle string = java_lang_String::create_from_str(name, CHECK_0);
924
925 // Initialize thread_oop to put it into the system threadGroup
926 Handle thread_group (THREAD, Universe::system_thread_group());
927 JavaValue result(T_VOID);
928 JavaCalls::call_special(&result, thread_oop,
929 klass,
930 vmSymbols::object_initializer_name(),
931 vmSymbols::threadgroup_string_void_signature(),
932 thread_group,
933 string,
934 CHECK_0);
935
962 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
963
964 // Note that we cannot call os::set_priority because it expects Java
965 // priorities and we are *explicitly* using OS priorities so that it's
966 // possible to set the compiler thread priority higher than any Java
967 // thread.
968
969 int native_prio = CompilerThreadPriority;
970 if (native_prio == -1) {
971 if (UseCriticalCompilerThreadPriority) {
972 native_prio = os::java_to_os_priority[CriticalPriority];
973 } else {
974 native_prio = os::java_to_os_priority[NearMaxPriority];
975 }
976 }
977 os::set_native_priority(compiler_thread, native_prio);
978
979 java_lang_Thread::set_daemon(thread_oop());
980
981 compiler_thread->set_threadObj(thread_oop());
982 compiler_thread->set_compiler(comp);
983 Threads::add(compiler_thread);
984 Thread::start(compiler_thread);
985 }
986
987 // Let go of Threads_lock before yielding
988 os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
989
990 return compiler_thread;
991 }
992
993
994 void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) {
995 EXCEPTION_MARK;
996 #if !defined(ZERO) && !defined(SHARK)
997 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
998 #endif // !ZERO && !SHARK
999 // Initialize the compilation queue
1000 if (c2_compiler_count > 0) {
1001 _c2_method_queue = new CompileQueue("C2MethodQueue", MethodCompileQueue_lock);
1002 _compilers[1]->set_num_compiler_threads(c2_compiler_count);
1003 }
1004 if (c1_compiler_count > 0) {
1005 _c1_method_queue = new CompileQueue("C1MethodQueue", MethodCompileQueue_lock);
1006 _compilers[0]->set_num_compiler_threads(c1_compiler_count);
1007 }
1008
1009 int compiler_count = c1_compiler_count + c2_compiler_count;
1010
1011 _compiler_threads =
1012 new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true);
1013
1014 char name_buffer[256];
1015 for (int i = 0; i < c2_compiler_count; i++) {
1016 // Create a name for our thread.
1017 sprintf(name_buffer, "C2 CompilerThread%d", i);
1018 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1019 // Shark and C2
1020 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, _compilers[1], CHECK);
1021 _compiler_threads->append(new_thread);
1022 }
1023
1024 for (int i = c2_compiler_count; i < compiler_count; i++) {
1025 // Create a name for our thread.
1026 sprintf(name_buffer, "C1 CompilerThread%d", i);
1027 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1028 // C1
1029 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, _compilers[0], CHECK);
1030 _compiler_threads->append(new_thread);
1031 }
1032
1033 if (UsePerfData) {
1034 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
1035 }
1036 }
1037
1038
1039 // Set the methods on the stack as on_stack so that redefine classes doesn't
1040 // reclaim them
1041 void CompileBroker::mark_on_stack() {
1042 if (_c2_method_queue != NULL) {
1043 _c2_method_queue->mark_on_stack();
1044 }
1045 if (_c1_method_queue != NULL) {
1046 _c1_method_queue->mark_on_stack();
1047 }
1048 }
1049
1050 // ------------------------------------------------------------------
1051 // CompileBroker::compile_method
1052 //
1053 // Request compilation of a method.
1054 void CompileBroker::compile_method_base(methodHandle method,
1055 int osr_bci,
1056 int comp_level,
1057 methodHandle hot_method,
1058 int hot_count,
1059 const char* comment,
1060 Thread* thread) {
1061 // do nothing if compiler thread(s) is not available
1062 if (!_initialized ) {
1063 return;
1064 }
1065
1066 guarantee(!method->is_abstract(), "cannot compile abstract methods");
1067 assert(method->method_holder()->oop_is_instance(),
1068 "sanity check");
1069 assert(!method->method_holder()->is_not_initialized(),
1070 "method holder must be initialized");
1532 {
1533 MutexLocker waiter(task->lock(), thread);
1534
1535 while (!task->is_complete())
1536 task->lock()->wait();
1537 }
1538 // It is harmless to check this status without the lock, because
1539 // completion is a stable property (until the task object is recycled).
1540 assert(task->is_complete(), "Compilation should have completed");
1541 assert(task->code_handle() == NULL, "must be reset");
1542
1543 thread->set_blocked_on_compilation(false);
1544
1545 // By convention, the waiter is responsible for recycling a
1546 // blocking CompileTask. Since there is only one waiter ever
1547 // waiting on a CompileTask, we know that no one else will
1548 // be using this CompileTask; we can free it.
1549 free_task(task);
1550 }
1551
1552 // Initialize compiler thread(s) + compiler object(s). The postcondition
1553 // of this function is that the compiler runtimes are initialized and that
1554 //compiler threads can start compiling.
1555 bool CompileBroker::init_compiler_runtime() {
1556 CompilerThread* thread = CompilerThread::current();
1557 AbstractCompiler* comp = thread->compiler();
1558 // Final sanity check - the compiler object must exist
1559 guarantee(comp != NULL, "Compiler object must exist");
1560
1561 int system_dictionary_modification_counter;
1562 {
1563 MutexLocker locker(Compile_lock, thread);
1564 system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1565 }
1566
1567 {
1568 // Must switch to native to allocate ci_env
1569 ThreadToNativeFromVM ttn(thread);
1570 ciEnv ci_env(NULL, system_dictionary_modification_counter);
1571 // Cache Jvmti state
1572 ci_env.cache_jvmti_state();
1573 // Cache DTrace flags
1574 ci_env.cache_dtrace_flags();
1575
1576 // Switch back to VM state to do compiler initialization
1577 ThreadInVMfromNative tv(thread);
1578 ResetNoHandleMark rnhm;
1579
1580
1581 if (!comp->is_shark()) {
1582 // Perform per-thread and global initializations
1583 comp->initialize();
1584 }
1585 }
1586
1587 if (comp->is_failed()) {
1588 disable_compilation_forever();
1589 // If compiler initialization failed, no compiler thread that is specific to a
1590 // particular compiler runtime will ever start to compile methods.
1591
1592 shutdown_compiler_runtime(comp, thread);
1593 return false;
1594 }
1595
1596 // C1 specific check
1597 if (comp->is_c1() && (thread->get_buffer_blob() == NULL)) {
1598 warning("Initialization of %s thread failed (no space to run compilers)", thread->name());
1599 return false;
1600 }
1601
1602 return true;
1603 }
1604
1605 // If C1 and/or C2 initialization failed, we shut down all compilation.
1606 // We do this to keep things simple. This can be changed if it ever turns out to be
1607 // a problem.
1608 void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) {
1609 // Free buffer blob, if allocated
1610 if (thread->get_buffer_blob() != NULL) {
1611 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1612 CodeCache::free(thread->get_buffer_blob());
1613 }
1614
1615 if (comp->should_perform_shutdown()) {
1616 // There are two reasons for shutting down the compiler
1617 // 1) compiler runtime initialization failed
1618 // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing
1619 warning("Shutting down compiler %s (no space to run compilers)", comp->name());
1620
1621 // Only one thread per compiler runtime object enters here
1622 // Set state to shut down
1623 comp->set_shut_down();
1624
1625 MutexLocker mu(MethodCompileQueue_lock, thread);
1626 CompileQueue* queue;
1627 if (_c1_method_queue != NULL) {
1628 _c1_method_queue->delete_all();
1629 queue = _c1_method_queue;
1630 _c1_method_queue = NULL;
1631 delete _c1_method_queue;
1632 }
1633
1634 if (_c2_method_queue != NULL) {
1635 _c2_method_queue->delete_all();
1636 queue = _c2_method_queue;
1637 _c2_method_queue = NULL;
1638 delete _c2_method_queue;
1639 }
1640
1641 // We could delete compiler runtimes also. However, there are references to
1642 // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then
1643 // fail. This can be done later if necessary.
1644 }
1645 }
1646
1647 // ------------------------------------------------------------------
1648 // CompileBroker::compiler_thread_loop
1649 //
1650 // The main loop run by a CompilerThread.
1651 void CompileBroker::compiler_thread_loop() {
1652 CompilerThread* thread = CompilerThread::current();
1653 CompileQueue* queue = thread->queue();
1654 // For the thread that initializes the ciObjectFactory
1655 // this resource mark holds all the shared objects
1656 ResourceMark rm;
1657
1658 // First thread to get here will initialize the compiler interface
1659
1660 if (!ciObjectFactory::is_initialized()) {
1661 ASSERT_IN_VM;
1662 MutexLocker only_one (CompileThread_lock, thread);
1663 if (!ciObjectFactory::is_initialized()) {
1664 ciObjectFactory::initialize();
1665 }
1666 }
1667
1668 // Open a log.
1669 if (LogCompilation) {
1670 init_compiler_thread_log();
1671 }
1672 CompileLog* log = thread->log();
1673 if (log != NULL) {
1674 log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
1675 thread->name(),
1676 os::current_thread_id(),
1677 os::current_process_id());
1678 log->stamp();
1679 log->end_elem();
1680 }
1681
1682 // If compiler thread/runtime initialization fails, exit the compiler thread
1683 if (!init_compiler_runtime()) {
1684 return;
1685 }
1686
1687 // Poll for new compilation tasks as long as the JVM runs. Compilation
1688 // should only be disabled if something went wrong while initializing the
1689 // compiler runtimes. This, in turn, should not happen. The only known case
1690 // when compiler runtime initialization fails is if there is not enough free
1691 // space in the code cache to generate the necessary stubs, etc.
1692 while (!is_compilation_disabled_forever()) {
1693 // We need this HandleMark to avoid leaking VM handles.
1694 HandleMark hm(thread);
1695
1696 if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) {
1697 // the code cache is really full
1698 handle_full_code_cache();
1699 }
1700
1701 CompileTask* task = queue->get();
1702 if (task == NULL) {
1703 continue;
1704 }
1705
1706 // Give compiler threads an extra quanta. They tend to be bursty and
1707 // this helps the compiler to finish up the job.
1708 if( CompilerThreadHintNoPreempt )
1709 os::hint_no_preempt();
1710
1711 // trace per thread time and compile statistics
1712 CompilerCounters* counters = ((CompilerThread*)thread)->counters();
1713 PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter());
1714
1715 // Assign the task to the current thread. Mark this compilation
1716 // thread as active for the profiler.
1717 CompileTaskWrapper ctw(task);
1718 nmethodLocker result_handle; // (handle for the nmethod produced by this task)
1719 task->set_code_handle(&result_handle);
1720 methodHandle method(thread, task->method());
1721
1722 // Never compile a method if breakpoints are present in it
1723 if (method()->number_of_breakpoints() == 0) {
1724 // Compile the method.
1729 if (CompilationRepeat != 0) {
1730 int compile_count = CompilationRepeat;
1731 while (compile_count > 0) {
1732 invoke_compiler_on_method(task);
1733 nmethod* nm = method->code();
1734 if (nm != NULL) {
1735 nm->make_zombie();
1736 method->clear_code();
1737 }
1738 compile_count--;
1739 }
1740 }
1741 #endif /* COMPILER1 */
1742 invoke_compiler_on_method(task);
1743 } else {
1744 // After compilation is disabled, remove remaining methods from queue
1745 method->clear_queued_for_compilation();
1746 }
1747 }
1748 }
1749
1750 // Shut down compiler runtime
1751 shutdown_compiler_runtime(thread->compiler(), thread);
1752 }
1753
1754 // ------------------------------------------------------------------
1755 // CompileBroker::init_compiler_thread_log
1756 //
1757 // Set up state required by +LogCompilation.
1758 void CompileBroker::init_compiler_thread_log() {
1759 CompilerThread* thread = CompilerThread::current();
1760 char file_name[4*K];
1761 FILE* fp = NULL;
1762 intx thread_id = os::current_thread_id();
1763 for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) {
1764 const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL);
1765 if (dir == NULL) {
1766 jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log",
1767 thread_id, os::current_process_id());
1768 } else {
1769 jio_snprintf(file_name, sizeof(file_name),
1770 "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir,
1771 os::file_separator(), thread_id, os::current_process_id());
1772 }
2044 warning("Try increasing the code cache size using -XX:ReservedCodeCacheSize=");
2045
2046 CodeCache::report_codemem_full();
2047
2048
2049 #ifndef PRODUCT
2050 if (CompileTheWorld || ExitOnFullCodeCache) {
2051 codecache_print(/* detailed= */ true);
2052 before_exit(JavaThread::current());
2053 exit_globals(); // will delete tty
2054 vm_direct_exit(CompileTheWorld ? 0 : 1);
2055 }
2056 #endif
2057 if (UseCodeCacheFlushing) {
2058 // Since code cache is full, immediately stop new compiles
2059 if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) {
2060 NMethodSweeper::log_sweep("disable_compiler");
2061 NMethodSweeper::possibly_sweep();
2062 }
2063 } else {
2064 disable_compilation_forever();
2065 }
2066 }
2067 codecache_print(/* detailed= */ true);
2068 }
2069
2070 // ------------------------------------------------------------------
2071 // CompileBroker::set_last_compile
2072 //
2073 // Record this compilation for debugging purposes.
2074 void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) {
2075 ResourceMark rm;
2076 char* method_name = method->name()->as_C_string();
2077 strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length);
2078 char current_method[CompilerCounters::cmname_buffer_length];
2079 size_t maxLen = CompilerCounters::cmname_buffer_length;
2080
2081 if (UsePerfData) {
2082 const char* class_name = method->method_holder()->name()->as_C_string();
2083
2084 size_t s1len = strlen(class_name);
|