139 elapsedTimer CompileBroker::_t_standard_compilation; 140 elapsedTimer CompileBroker::_t_invalidated_compilation; 141 elapsedTimer CompileBroker::_t_bailedout_compilation; 142 143 int CompileBroker::_total_bailout_count = 0; 144 int CompileBroker::_total_invalidated_count = 0; 145 int CompileBroker::_total_compile_count = 0; 146 int CompileBroker::_total_osr_compile_count = 0; 147 int CompileBroker::_total_standard_compile_count = 0; 148 149 int CompileBroker::_sum_osr_bytes_compiled = 0; 150 int CompileBroker::_sum_standard_bytes_compiled = 0; 151 int CompileBroker::_sum_nmethod_size = 0; 152 int CompileBroker::_sum_nmethod_code_size = 0; 153 154 long CompileBroker::_peak_compilation_time = 0; 155 156 CompileQueue* CompileBroker::_c2_compile_queue = NULL; 157 CompileQueue* CompileBroker::_c1_compile_queue = NULL; 158 159 GrowableArray<CompilerThread*>* CompileBroker::_compiler_threads = NULL; 160 161 162 class CompilationLog : public StringEventLog { 163 public: 164 CompilationLog() : StringEventLog("Compilation events") { 165 } 166 167 void log_compile(JavaThread* thread, CompileTask* task) { 168 StringLogMessage lm; 169 stringStream sstr = lm.stream(); 170 // msg.time_stamp().update_to(tty->time_stamp().ticks()); 171 task->print_compilation(&sstr, NULL, true, false); 172 log(thread, "%s", (const char*)lm); 173 } 174 175 void log_nmethod(JavaThread* thread, nmethod* nm) { 176 log(thread, "nmethod %d%s " INTPTR_FORMAT " code ["INTPTR_FORMAT ", " INTPTR_FORMAT "]", 177 nm->compile_id(), nm->is_osr_method() ? "%" : "", 178 p2i(nm), p2i(nm->code_begin()), p2i(nm->code_end())); 179 } 180 632 CompileTask* next = _first; 633 634 // Iterate over all tasks in the compile queue 635 while (next != NULL) { 636 CompileTask* current = next; 637 next = current->next(); 638 { 639 // Wake up thread that blocks on the compile task. 640 MutexLocker ct_lock(current->lock()); 641 current->lock()->notify(); 642 } 643 // Put the task back on the freelist. 644 CompileTask::free(current); 645 } 646 _first = NULL; 647 648 // Wake up all threads that block on the queue. 649 lock()->notify_all(); 650 } 651 652 // ------------------------------------------------------------------ 653 // CompileQueue::get 654 // 655 // Get the next CompileTask from a CompileQueue 656 CompileTask* CompileQueue::get() { 657 NMethodSweeper::possibly_sweep(); 658 659 MutexLocker locker(lock()); 660 // If _first is NULL we have no more compile jobs. There are two reasons for 661 // having no compile jobs: First, we compiled everything we wanted. Second, 662 // we ran out of code cache so compilation has been disabled. In the latter 663 // case we perform code cache sweeps to free memory such that we can re-enable 664 // compilation. 665 while (_first == NULL) { 666 // Exit loop if compilation is disabled forever 667 if (CompileBroker::is_compilation_disabled_forever()) { 668 return NULL; 669 } 670 671 if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) { 672 // Wait a certain amount of time to possibly do another sweep. 673 // We must wait until stack scanning has happened so that we can 674 // transition a method's state from 'not_entrant' to 'zombie'. 675 long wait_time = NmethodSweepCheckInterval * 1000; 676 if (FLAG_IS_DEFAULT(NmethodSweepCheckInterval)) { 677 // Only one thread at a time can do sweeping. Scale the 678 // wait time according to the number of compiler threads. 679 // As a result, the next sweep is likely to happen every 100ms 680 // with an arbitrary number of threads that do sweeping. 681 wait_time = 100 * CICompilerCount; 682 } 683 bool timeout = lock()->wait(!Mutex::_no_safepoint_check_flag, wait_time); 684 if (timeout) { 685 MutexUnlocker ul(lock()); 686 NMethodSweeper::possibly_sweep(); 687 } 688 } else { 689 // If there are no compilation tasks and we can compile new jobs 690 // (i.e., there is enough free space in the code cache) there is 691 // no need to invoke the sweeper. As a result, the hotness of methods 692 // remains unchanged. This behavior is desired, since we want to keep 693 // the stable state, i.e., we do not want to evict methods from the 694 // code cache if it is unnecessary. 695 // We need a timed wait here, since compiler threads can exit if compilation 696 // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads 697 // is not critical and we do not want idle compiler threads to wake up too often. 698 lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000); 699 } 700 } 701 702 if (CompileBroker::is_compilation_disabled_forever()) { 703 return NULL; 704 } 705 706 CompileTask* task; 707 { 708 No_Safepoint_Verifier nsv; 709 task = CompilationPolicy::policy()->select_task(this); 710 } 711 remove(task); 712 purge_stale_tasks(); // may temporarily release MCQ lock 713 return task; 714 } 715 716 // Clean & deallocate stale compile tasks. 717 // Temporarily releases MethodCompileQueue lock. 718 void CompileQueue::purge_stale_tasks() { 719 assert(lock()->owned_by_self(), "must own lock"); 720 if (_first_stale != NULL) { 869 int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization); 870 #ifdef COMPILER1 871 if (c1_count > 0) { 872 _compilers[0] = new Compiler(); 873 } 874 #endif // COMPILER1 875 876 #ifdef COMPILER2 877 if (c2_count > 0) { 878 _compilers[1] = new C2Compiler(); 879 } 880 #endif // COMPILER2 881 882 #else // SHARK 883 int c1_count = 0; 884 int c2_count = 1; 885 886 _compilers[1] = new SharkCompiler(); 887 #endif // SHARK 888 889 // Start the CompilerThreads 890 init_compiler_threads(c1_count, c2_count); 891 // totalTime performance counter is always created as it is required 892 // by the implementation of java.lang.management.CompilationMBean. 893 { 894 EXCEPTION_MARK; 895 _perf_total_compilation = 896 PerfDataManager::create_counter(JAVA_CI, "totalTime", 897 PerfData::U_Ticks, CHECK); 898 } 899 900 901 if (UsePerfData) { 902 903 EXCEPTION_MARK; 904 905 // create the jvmstat performance counters 906 _perf_osr_compilation = 907 PerfDataManager::create_counter(SUN_CI, "osrTime", 908 PerfData::U_Ticks, CHECK); 909 910 _perf_standard_compilation = 974 CHECK); 975 976 977 _perf_last_failed_type = 978 PerfDataManager::create_variable(SUN_CI, "lastFailedType", 979 PerfData::U_None, 980 (jlong)CompileBroker::no_compile, 981 CHECK); 982 983 _perf_last_invalidated_type = 984 PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType", 985 PerfData::U_None, 986 (jlong)CompileBroker::no_compile, 987 CHECK); 988 } 989 990 _initialized = true; 991 } 992 993 994 CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, 995 AbstractCompiler* comp, TRAPS) { 996 CompilerThread* compiler_thread = NULL; 997 998 Klass* k = 999 SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), 1000 true, CHECK_0); 1001 instanceKlassHandle klass (THREAD, k); 1002 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0); 1003 Handle string = java_lang_String::create_from_str(name, CHECK_0); 1004 1005 // Initialize thread_oop to put it into the system threadGroup 1006 Handle thread_group (THREAD, Universe::system_thread_group()); 1007 JavaValue result(T_VOID); 1008 JavaCalls::call_special(&result, thread_oop, 1009 klass, 1010 vmSymbols::object_initializer_name(), 1011 vmSymbols::threadgroup_string_void_signature(), 1012 thread_group, 1013 string, 1014 CHECK_0); 1015 1016 { 1017 MutexLocker mu(Threads_lock, THREAD); 1018 compiler_thread = new CompilerThread(queue, counters); 1019 // At this point the new CompilerThread data-races with this startup 1020 // thread (which I believe is the primoridal thread and NOT the VM 1021 // thread). This means Java bytecodes being executed at startup can 1022 // queue compile jobs which will run at whatever default priority the 1023 // newly created CompilerThread runs at. 1024 1025 1026 // At this point it may be possible that no osthread was created for the 1027 // JavaThread due to lack of memory. We would have to throw an exception 1028 // in that case. However, since this must work and we do not allow 1029 // exceptions anyway, check and abort if this fails. 1030 1031 if (compiler_thread == NULL || compiler_thread->osthread() == NULL){ 1032 vm_exit_during_initialization("java.lang.OutOfMemoryError", 1033 os::native_thread_creation_failed_msg()); 1034 } 1035 1036 java_lang_Thread::set_thread(thread_oop(), compiler_thread); 1037 1038 // Note that this only sets the JavaThread _priority field, which by 1039 // definition is limited to Java priorities and not OS priorities. 1040 // The os-priority is set in the CompilerThread startup code itself 1041 1042 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); 1043 1044 // Note that we cannot call os::set_priority because it expects Java 1045 // priorities and we are *explicitly* using OS priorities so that it's 1046 // possible to set the compiler thread priority higher than any Java 1047 // thread. 1048 1049 int native_prio = CompilerThreadPriority; 1050 if (native_prio == -1) { 1051 if (UseCriticalCompilerThreadPriority) { 1052 native_prio = os::java_to_os_priority[CriticalPriority]; 1053 } else { 1054 native_prio = os::java_to_os_priority[NearMaxPriority]; 1055 } 1056 } 1057 os::set_native_priority(compiler_thread, native_prio); 1058 1059 java_lang_Thread::set_daemon(thread_oop()); 1060 1061 compiler_thread->set_threadObj(thread_oop()); 1062 compiler_thread->set_compiler(comp); 1063 Threads::add(compiler_thread); 1064 Thread::start(compiler_thread); 1065 } 1066 1067 // Let go of Threads_lock before yielding 1068 os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS) 1069 1070 return compiler_thread; 1071 } 1072 1073 1074 void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) { 1075 EXCEPTION_MARK; 1076 #if !defined(ZERO) && !defined(SHARK) 1077 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); 1078 #endif // !ZERO && !SHARK 1079 // Initialize the compilation queue 1080 if (c2_compiler_count > 0) { 1081 _c2_compile_queue = new CompileQueue("C2 compile queue", MethodCompileQueue_lock); 1082 _compilers[1]->set_num_compiler_threads(c2_compiler_count); 1083 } 1084 if (c1_compiler_count > 0) { 1085 _c1_compile_queue = new CompileQueue("C1 compile queue", MethodCompileQueue_lock); 1086 _compilers[0]->set_num_compiler_threads(c1_compiler_count); 1087 } 1088 1089 int compiler_count = c1_compiler_count + c2_compiler_count; 1090 1091 _compiler_threads = 1092 new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true); 1093 1094 char name_buffer[256]; 1095 for (int i = 0; i < c2_compiler_count; i++) { 1096 // Create a name for our thread. 1097 sprintf(name_buffer, "C2 CompilerThread%d", i); 1098 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); 1099 // Shark and C2 1100 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], CHECK); 1101 _compiler_threads->append(new_thread); 1102 } 1103 1104 for (int i = c2_compiler_count; i < compiler_count; i++) { 1105 // Create a name for our thread. 1106 sprintf(name_buffer, "C1 CompilerThread%d", i); 1107 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); 1108 // C1 1109 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], CHECK); 1110 _compiler_threads->append(new_thread); 1111 } 1112 1113 if (UsePerfData) { 1114 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK); 1115 } 1116 } 1117 1118 1119 /** 1120 * Set the methods on the stack as on_stack so that redefine classes doesn't 1121 * reclaim them. This method is executed at a safepoint. 1122 */ 1123 void CompileBroker::mark_on_stack() { 1124 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 1125 // Since we are at a safepoint, we do not need a lock to access 1126 // the compile queues. 1127 if (_c2_compile_queue != NULL) { 1128 _c2_compile_queue->mark_on_stack(); 1129 } 1130 if (_c1_compile_queue != NULL) { 1131 _c1_compile_queue->mark_on_stack(); 1132 } 1133 } 1134 1135 // ------------------------------------------------------------------ 1742 os::current_thread_id(), 1743 os::current_process_id()); 1744 log->stamp(); 1745 log->end_elem(); 1746 } 1747 1748 // If compiler thread/runtime initialization fails, exit the compiler thread 1749 if (!init_compiler_runtime()) { 1750 return; 1751 } 1752 1753 // Poll for new compilation tasks as long as the JVM runs. Compilation 1754 // should only be disabled if something went wrong while initializing the 1755 // compiler runtimes. This, in turn, should not happen. The only known case 1756 // when compiler runtime initialization fails is if there is not enough free 1757 // space in the code cache to generate the necessary stubs, etc. 1758 while (!is_compilation_disabled_forever()) { 1759 // We need this HandleMark to avoid leaking VM handles. 1760 HandleMark hm(thread); 1761 1762 // Check if the CodeCache is full 1763 int code_blob_type = 0; 1764 if (CodeCache::is_full(&code_blob_type)) { 1765 // The CodeHeap for code_blob_type is really full 1766 handle_full_code_cache(code_blob_type); 1767 } 1768 1769 CompileTask* task = queue->get(); 1770 if (task == NULL) { 1771 continue; 1772 } 1773 1774 // Give compiler threads an extra quanta. They tend to be bursty and 1775 // this helps the compiler to finish up the job. 1776 if( CompilerThreadHintNoPreempt ) 1777 os::hint_no_preempt(); 1778 1779 // trace per thread time and compile statistics 1780 CompilerCounters* counters = ((CompilerThread*)thread)->counters(); 1781 PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter()); 1782 1783 // Assign the task to the current thread. Mark this compilation 1784 // thread as active for the profiler. 1785 CompileTaskWrapper ctw(task); 1786 nmethodLocker result_handle; // (handle for the nmethod produced by this task) 1787 task->set_code_handle(&result_handle); 1788 methodHandle method(thread, task->method()); 1789 1790 // Never compile a method if breakpoints are present in it 1791 if (method()->number_of_breakpoints() == 0) { 1792 // Compile the method. 1793 if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) { 1794 invoke_compiler_on_method(task); 1795 } else { 1796 // After compilation is disabled, remove remaining methods from queue 1797 method->clear_queued_for_compilation(); 2057 // Note that the queued_for_compilation bits are cleared without 2058 // protection of a mutex. [They were set by the requester thread, 2059 // when adding the task to the compile queue -- at which time the 2060 // compile queue lock was held. Subsequently, we acquired the compile 2061 // queue lock to get this task off the compile queue; thus (to belabour 2062 // the point somewhat) our clearing of the bits must be occurring 2063 // only after the setting of the bits. See also 14012000 above. 2064 method->clear_queued_for_compilation(); 2065 2066 #ifdef ASSERT 2067 if (CollectedHeap::fired_fake_oom()) { 2068 // The current compile received a fake OOM during compilation so 2069 // go ahead and exit the VM since the test apparently succeeded 2070 tty->print_cr("*** Shutting down VM after successful fake OOM"); 2071 vm_exit(0); 2072 } 2073 #endif 2074 } 2075 2076 /** 2077 * The CodeCache is full. Print out warning and disable compilation 2078 * or try code cache cleaning so compilation can continue later. 2079 */ 2080 void CompileBroker::handle_full_code_cache(int code_blob_type) { 2081 UseInterpreter = true; 2082 if (UseCompiler || AlwaysCompileLoopMethods ) { 2083 if (xtty != NULL) { 2084 ResourceMark rm; 2085 stringStream s; 2086 // Dump code cache state into a buffer before locking the tty, 2087 // because log_state() will use locks causing lock conflicts. 2088 CodeCache::log_state(&s); 2089 // Lock to prevent tearing 2090 ttyLocker ttyl; 2091 xtty->begin_elem("code_cache_full"); 2092 xtty->print("%s", s.as_string()); 2093 xtty->stamp(); 2094 xtty->end_elem(); 2095 } 2096 2097 #ifndef PRODUCT 2098 if (CompileTheWorld || ExitOnFullCodeCache) { 2099 codecache_print(/* detailed= */ true); 2100 before_exit(JavaThread::current()); 2101 exit_globals(); // will delete tty 2102 vm_direct_exit(CompileTheWorld ? 0 : 1); 2103 } 2104 #endif 2105 if (UseCodeCacheFlushing) { 2106 // Since code cache is full, immediately stop new compiles 2107 if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) { 2108 NMethodSweeper::log_sweep("disable_compiler"); 2109 } 2110 // Switch to 'vm_state'. This ensures that possibly_sweep() can be called 2111 // without having to consider the state in which the current thread is. 2112 ThreadInVMfromUnknown in_vm; 2113 NMethodSweeper::possibly_sweep(); 2114 } else { 2115 disable_compilation_forever(); 2116 } 2117 2118 CodeCache::report_codemem_full(code_blob_type, should_print_compiler_warning()); 2119 } 2120 } 2121 2122 // ------------------------------------------------------------------ 2123 // CompileBroker::set_last_compile 2124 // 2125 // Record this compilation for debugging purposes. 2126 void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) { 2127 ResourceMark rm; 2128 char* method_name = method->name()->as_C_string(); 2129 strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length); 2130 _last_method_compiled[CompileBroker::name_buffer_length - 1] = '\0'; // ensure null terminated 2131 char current_method[CompilerCounters::cmname_buffer_length]; 2132 size_t maxLen = CompilerCounters::cmname_buffer_length; 2133 | 139 elapsedTimer CompileBroker::_t_standard_compilation; 140 elapsedTimer CompileBroker::_t_invalidated_compilation; 141 elapsedTimer CompileBroker::_t_bailedout_compilation; 142 143 int CompileBroker::_total_bailout_count = 0; 144 int CompileBroker::_total_invalidated_count = 0; 145 int CompileBroker::_total_compile_count = 0; 146 int CompileBroker::_total_osr_compile_count = 0; 147 int CompileBroker::_total_standard_compile_count = 0; 148 149 int CompileBroker::_sum_osr_bytes_compiled = 0; 150 int CompileBroker::_sum_standard_bytes_compiled = 0; 151 int CompileBroker::_sum_nmethod_size = 0; 152 int CompileBroker::_sum_nmethod_code_size = 0; 153 154 long CompileBroker::_peak_compilation_time = 0; 155 156 CompileQueue* CompileBroker::_c2_compile_queue = NULL; 157 CompileQueue* CompileBroker::_c1_compile_queue = NULL; 158 159 160 class CompilationLog : public StringEventLog { 161 public: 162 CompilationLog() : StringEventLog("Compilation events") { 163 } 164 165 void log_compile(JavaThread* thread, CompileTask* task) { 166 StringLogMessage lm; 167 stringStream sstr = lm.stream(); 168 // msg.time_stamp().update_to(tty->time_stamp().ticks()); 169 task->print_compilation(&sstr, NULL, true, false); 170 log(thread, "%s", (const char*)lm); 171 } 172 173 void log_nmethod(JavaThread* thread, nmethod* nm) { 174 log(thread, "nmethod %d%s " INTPTR_FORMAT " code ["INTPTR_FORMAT ", " INTPTR_FORMAT "]", 175 nm->compile_id(), nm->is_osr_method() ? "%" : "", 176 p2i(nm), p2i(nm->code_begin()), p2i(nm->code_end())); 177 } 178 630 CompileTask* next = _first; 631 632 // Iterate over all tasks in the compile queue 633 while (next != NULL) { 634 CompileTask* current = next; 635 next = current->next(); 636 { 637 // Wake up thread that blocks on the compile task. 638 MutexLocker ct_lock(current->lock()); 639 current->lock()->notify(); 640 } 641 // Put the task back on the freelist. 642 CompileTask::free(current); 643 } 644 _first = NULL; 645 646 // Wake up all threads that block on the queue. 647 lock()->notify_all(); 648 } 649 650 /** 651 * Get the next CompileTask from a CompileQueue 652 */ 653 CompileTask* CompileQueue::get() { 654 MutexLocker locker(lock()); 655 // If _first is NULL we have no more compile jobs. There are two reasons for 656 // having no compile jobs: First, we compiled everything we wanted. Second, 657 // we ran out of code cache so compilation has been disabled. In the latter 658 // case we perform code cache sweeps to free memory such that we can re-enable 659 // compilation. 660 while (_first == NULL) { 661 // Exit loop if compilation is disabled forever 662 if (CompileBroker::is_compilation_disabled_forever()) { 663 return NULL; 664 } 665 666 // If there are no compilation tasks and we can compile new jobs 667 // (i.e., there is enough free space in the code cache) there is 668 // no need to invoke the sweeper. As a result, the hotness of methods 669 // remains unchanged. This behavior is desired, since we want to keep 670 // the stable state, i.e., we do not want to evict methods from the 671 // code cache if it is unnecessary. 672 // We need a timed wait here, since compiler threads can exit if compilation 673 // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads 674 // is not critical and we do not want idle compiler threads to wake up too often. 675 lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000); 676 } 677 678 if (CompileBroker::is_compilation_disabled_forever()) { 679 return NULL; 680 } 681 682 CompileTask* task; 683 { 684 No_Safepoint_Verifier nsv; 685 task = CompilationPolicy::policy()->select_task(this); 686 } 687 remove(task); 688 purge_stale_tasks(); // may temporarily release MCQ lock 689 return task; 690 } 691 692 // Clean & deallocate stale compile tasks. 693 // Temporarily releases MethodCompileQueue lock. 694 void CompileQueue::purge_stale_tasks() { 695 assert(lock()->owned_by_self(), "must own lock"); 696 if (_first_stale != NULL) { 845 int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization); 846 #ifdef COMPILER1 847 if (c1_count > 0) { 848 _compilers[0] = new Compiler(); 849 } 850 #endif // COMPILER1 851 852 #ifdef COMPILER2 853 if (c2_count > 0) { 854 _compilers[1] = new C2Compiler(); 855 } 856 #endif // COMPILER2 857 858 #else // SHARK 859 int c1_count = 0; 860 int c2_count = 1; 861 862 _compilers[1] = new SharkCompiler(); 863 #endif // SHARK 864 865 // Start the compiler thread(s) and the sweeper thread 866 init_compiler_sweeper_threads(c1_count, c2_count); 867 // totalTime performance counter is always created as it is required 868 // by the implementation of java.lang.management.CompilationMBean. 869 { 870 EXCEPTION_MARK; 871 _perf_total_compilation = 872 PerfDataManager::create_counter(JAVA_CI, "totalTime", 873 PerfData::U_Ticks, CHECK); 874 } 875 876 877 if (UsePerfData) { 878 879 EXCEPTION_MARK; 880 881 // create the jvmstat performance counters 882 _perf_osr_compilation = 883 PerfDataManager::create_counter(SUN_CI, "osrTime", 884 PerfData::U_Ticks, CHECK); 885 886 _perf_standard_compilation = 950 CHECK); 951 952 953 _perf_last_failed_type = 954 PerfDataManager::create_variable(SUN_CI, "lastFailedType", 955 PerfData::U_None, 956 (jlong)CompileBroker::no_compile, 957 CHECK); 958 959 _perf_last_invalidated_type = 960 PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType", 961 PerfData::U_None, 962 (jlong)CompileBroker::no_compile, 963 CHECK); 964 } 965 966 _initialized = true; 967 } 968 969 970 JavaThread* CompileBroker::make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, 971 AbstractCompiler* comp, bool compiler_thread, TRAPS) { 972 JavaThread* thread = NULL; 973 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_0); 974 instanceKlassHandle klass (THREAD, k); 975 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0); 976 Handle string = java_lang_String::create_from_str(name, CHECK_0); 977 978 // Initialize thread_oop to put it into the system threadGroup 979 Handle thread_group (THREAD, Universe::system_thread_group()); 980 JavaValue result(T_VOID); 981 JavaCalls::call_special(&result, thread_oop, 982 klass, 983 vmSymbols::object_initializer_name(), 984 vmSymbols::threadgroup_string_void_signature(), 985 thread_group, 986 string, 987 CHECK_0); 988 989 { 990 MutexLocker mu(Threads_lock, THREAD); 991 if (compiler_thread) { 992 thread = new CompilerThread(queue, counters); 993 } else { 994 thread = new CodeCacheSweeperThread(); 995 } 996 // At this point the new CompilerThread data-races with this startup 997 // thread (which I believe is the primoridal thread and NOT the VM 998 // thread). This means Java bytecodes being executed at startup can 999 // queue compile jobs which will run at whatever default priority the 1000 // newly created CompilerThread runs at. 1001 1002 1003 // At this point it may be possible that no osthread was created for the 1004 // JavaThread due to lack of memory. We would have to throw an exception 1005 // in that case. However, since this must work and we do not allow 1006 // exceptions anyway, check and abort if this fails. 1007 1008 if (thread == NULL || thread->osthread() == NULL) { 1009 vm_exit_during_initialization("java.lang.OutOfMemoryError", 1010 os::native_thread_creation_failed_msg()); 1011 } 1012 1013 java_lang_Thread::set_thread(thread_oop(), thread); 1014 1015 // Note that this only sets the JavaThread _priority field, which by 1016 // definition is limited to Java priorities and not OS priorities. 1017 // The os-priority is set in the CompilerThread startup code itself 1018 1019 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); 1020 1021 // Note that we cannot call os::set_priority because it expects Java 1022 // priorities and we are *explicitly* using OS priorities so that it's 1023 // possible to set the compiler thread priority higher than any Java 1024 // thread. 1025 1026 int native_prio = CompilerThreadPriority; 1027 if (native_prio == -1) { 1028 if (UseCriticalCompilerThreadPriority) { 1029 native_prio = os::java_to_os_priority[CriticalPriority]; 1030 } else { 1031 native_prio = os::java_to_os_priority[NearMaxPriority]; 1032 } 1033 } 1034 os::set_native_priority(thread, native_prio); 1035 1036 java_lang_Thread::set_daemon(thread_oop()); 1037 1038 thread->set_threadObj(thread_oop()); 1039 if (compiler_thread) { 1040 thread->as_CompilerThread()->set_compiler(comp); 1041 } 1042 Threads::add(thread); 1043 Thread::start(thread); 1044 } 1045 1046 // Let go of Threads_lock before yielding 1047 os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS) 1048 1049 return thread; 1050 } 1051 1052 1053 void CompileBroker::init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count) { 1054 EXCEPTION_MARK; 1055 #if !defined(ZERO) && !defined(SHARK) 1056 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); 1057 #endif // !ZERO && !SHARK 1058 // Initialize the compilation queue 1059 if (c2_compiler_count > 0) { 1060 _c2_compile_queue = new CompileQueue("C2 compile queue", MethodCompileQueue_lock); 1061 _compilers[1]->set_num_compiler_threads(c2_compiler_count); 1062 } 1063 if (c1_compiler_count > 0) { 1064 _c1_compile_queue = new CompileQueue("C1 compile queue", MethodCompileQueue_lock); 1065 _compilers[0]->set_num_compiler_threads(c1_compiler_count); 1066 } 1067 1068 int compiler_count = c1_compiler_count + c2_compiler_count; 1069 1070 char name_buffer[256]; 1071 const bool compiler_thread = true; 1072 for (int i = 0; i < c2_compiler_count; i++) { 1073 // Create a name for our thread. 1074 sprintf(name_buffer, "C2 CompilerThread%d", i); 1075 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); 1076 // Shark and C2 1077 make_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], compiler_thread, CHECK); 1078 } 1079 1080 for (int i = c2_compiler_count; i < compiler_count; i++) { 1081 // Create a name for our thread. 1082 sprintf(name_buffer, "C1 CompilerThread%d", i); 1083 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); 1084 // C1 1085 make_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], compiler_thread, CHECK); 1086 } 1087 1088 if (UsePerfData) { 1089 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK); 1090 } 1091 1092 if (MethodFlushing) { 1093 // Initialize the sweeper thread 1094 make_thread("Sweeper thread", NULL, NULL, NULL, false, CHECK); 1095 } 1096 } 1097 1098 1099 /** 1100 * Set the methods on the stack as on_stack so that redefine classes doesn't 1101 * reclaim them. This method is executed at a safepoint. 1102 */ 1103 void CompileBroker::mark_on_stack() { 1104 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 1105 // Since we are at a safepoint, we do not need a lock to access 1106 // the compile queues. 1107 if (_c2_compile_queue != NULL) { 1108 _c2_compile_queue->mark_on_stack(); 1109 } 1110 if (_c1_compile_queue != NULL) { 1111 _c1_compile_queue->mark_on_stack(); 1112 } 1113 } 1114 1115 // ------------------------------------------------------------------ 1722 os::current_thread_id(), 1723 os::current_process_id()); 1724 log->stamp(); 1725 log->end_elem(); 1726 } 1727 1728 // If compiler thread/runtime initialization fails, exit the compiler thread 1729 if (!init_compiler_runtime()) { 1730 return; 1731 } 1732 1733 // Poll for new compilation tasks as long as the JVM runs. Compilation 1734 // should only be disabled if something went wrong while initializing the 1735 // compiler runtimes. This, in turn, should not happen. The only known case 1736 // when compiler runtime initialization fails is if there is not enough free 1737 // space in the code cache to generate the necessary stubs, etc. 1738 while (!is_compilation_disabled_forever()) { 1739 // We need this HandleMark to avoid leaking VM handles. 1740 HandleMark hm(thread); 1741 1742 CompileTask* task = queue->get(); 1743 if (task == NULL) { 1744 continue; 1745 } 1746 1747 // Give compiler threads an extra quanta. They tend to be bursty and 1748 // this helps the compiler to finish up the job. 1749 if (CompilerThreadHintNoPreempt) { 1750 os::hint_no_preempt(); 1751 } 1752 1753 // trace per thread time and compile statistics 1754 CompilerCounters* counters = ((CompilerThread*)thread)->counters(); 1755 PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter()); 1756 1757 // Assign the task to the current thread. Mark this compilation 1758 // thread as active for the profiler. 1759 CompileTaskWrapper ctw(task); 1760 nmethodLocker result_handle; // (handle for the nmethod produced by this task) 1761 task->set_code_handle(&result_handle); 1762 methodHandle method(thread, task->method()); 1763 1764 // Never compile a method if breakpoints are present in it 1765 if (method()->number_of_breakpoints() == 0) { 1766 // Compile the method. 1767 if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) { 1768 invoke_compiler_on_method(task); 1769 } else { 1770 // After compilation is disabled, remove remaining methods from queue 1771 method->clear_queued_for_compilation(); 2031 // Note that the queued_for_compilation bits are cleared without 2032 // protection of a mutex. [They were set by the requester thread, 2033 // when adding the task to the compile queue -- at which time the 2034 // compile queue lock was held. Subsequently, we acquired the compile 2035 // queue lock to get this task off the compile queue; thus (to belabour 2036 // the point somewhat) our clearing of the bits must be occurring 2037 // only after the setting of the bits. See also 14012000 above. 2038 method->clear_queued_for_compilation(); 2039 2040 #ifdef ASSERT 2041 if (CollectedHeap::fired_fake_oom()) { 2042 // The current compile received a fake OOM during compilation so 2043 // go ahead and exit the VM since the test apparently succeeded 2044 tty->print_cr("*** Shutting down VM after successful fake OOM"); 2045 vm_exit(0); 2046 } 2047 #endif 2048 } 2049 2050 /** 2051 * The CodeCache is full. Print warning and disable compilation. 2052 * Schedule code cache cleaning so compilation can continue later. 2053 * This function needs to be called only from CodeCache::allocate(), 2054 * since we currently handle a full code cache uniformly. 2055 */ 2056 void CompileBroker::handle_full_code_cache(int code_blob_type) { 2057 UseInterpreter = true; 2058 if (UseCompiler || AlwaysCompileLoopMethods ) { 2059 if (xtty != NULL) { 2060 ResourceMark rm; 2061 stringStream s; 2062 // Dump code cache state into a buffer before locking the tty, 2063 // because log_state() will use locks causing lock conflicts. 2064 CodeCache::log_state(&s); 2065 // Lock to prevent tearing 2066 ttyLocker ttyl; 2067 xtty->begin_elem("code_cache_full"); 2068 xtty->print("%s", s.as_string()); 2069 xtty->stamp(); 2070 xtty->end_elem(); 2071 } 2072 2073 #ifndef PRODUCT 2074 if (CompileTheWorld || ExitOnFullCodeCache) { 2075 codecache_print(/* detailed= */ true); 2076 before_exit(JavaThread::current()); 2077 exit_globals(); // will delete tty 2078 vm_direct_exit(CompileTheWorld ? 0 : 1); 2079 } 2080 #endif 2081 if (UseCodeCacheFlushing) { 2082 // Since code cache is full, immediately stop new compiles 2083 if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) { 2084 NMethodSweeper::log_sweep("disable_compiler"); 2085 } 2086 } else { 2087 disable_compilation_forever(); 2088 } 2089 2090 CodeCache::report_codemem_full(code_blob_type, should_print_compiler_warning()); 2091 } 2092 } 2093 2094 // ------------------------------------------------------------------ 2095 // CompileBroker::set_last_compile 2096 // 2097 // Record this compilation for debugging purposes. 2098 void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) { 2099 ResourceMark rm; 2100 char* method_name = method->name()->as_C_string(); 2101 strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length); 2102 _last_method_compiled[CompileBroker::name_buffer_length - 1] = '\0'; // ensure null terminated 2103 char current_method[CompilerCounters::cmname_buffer_length]; 2104 size_t maxLen = CompilerCounters::cmname_buffer_length; 2105 |