154 long CompileBroker::_peak_compilation_time = 0; |
154 long CompileBroker::_peak_compilation_time = 0; |
155 |
155 |
156 CompileQueue* CompileBroker::_c2_compile_queue = NULL; |
156 CompileQueue* CompileBroker::_c2_compile_queue = NULL; |
157 CompileQueue* CompileBroker::_c1_compile_queue = NULL; |
157 CompileQueue* CompileBroker::_c1_compile_queue = NULL; |
158 |
158 |
159 GrowableArray<CompilerThread*>* CompileBroker::_compiler_threads = NULL; |
|
160 |
|
161 |
159 |
162 class CompilationLog : public StringEventLog { |
160 class CompilationLog : public StringEventLog { |
163 public: |
161 public: |
164 CompilationLog() : StringEventLog("Compilation events") { |
162 CompilationLog() : StringEventLog("Compilation events") { |
165 } |
163 } |
647 |
645 |
648 // Wake up all threads that block on the queue. |
646 // Wake up all threads that block on the queue. |
649 lock()->notify_all(); |
647 lock()->notify_all(); |
650 } |
648 } |
651 |
649 |
652 // ------------------------------------------------------------------ |
650 /** |
653 // CompileQueue::get |
651 * Get the next CompileTask from a CompileQueue |
654 // |
652 */ |
655 // Get the next CompileTask from a CompileQueue |
|
656 CompileTask* CompileQueue::get() { |
653 CompileTask* CompileQueue::get() { |
657 NMethodSweeper::possibly_sweep(); |
|
658 |
|
659 MutexLocker locker(lock()); |
654 MutexLocker locker(lock()); |
660 // If _first is NULL we have no more compile jobs. There are two reasons for |
655 // 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, |
656 // 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 |
657 // 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 |
658 // case we perform code cache sweeps to free memory such that we can re-enable |
666 // Exit loop if compilation is disabled forever |
661 // Exit loop if compilation is disabled forever |
667 if (CompileBroker::is_compilation_disabled_forever()) { |
662 if (CompileBroker::is_compilation_disabled_forever()) { |
668 return NULL; |
663 return NULL; |
669 } |
664 } |
670 |
665 |
671 if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) { |
666 // If there are no compilation tasks and we can compile new jobs |
672 // Wait a certain amount of time to possibly do another sweep. |
667 // (i.e., there is enough free space in the code cache) there is |
673 // We must wait until stack scanning has happened so that we can |
668 // no need to invoke the sweeper. As a result, the hotness of methods |
674 // transition a method's state from 'not_entrant' to 'zombie'. |
669 // remains unchanged. This behavior is desired, since we want to keep |
675 long wait_time = NmethodSweepCheckInterval * 1000; |
670 // the stable state, i.e., we do not want to evict methods from the |
676 if (FLAG_IS_DEFAULT(NmethodSweepCheckInterval)) { |
671 // code cache if it is unnecessary. |
677 // Only one thread at a time can do sweeping. Scale the |
672 // We need a timed wait here, since compiler threads can exit if compilation |
678 // wait time according to the number of compiler threads. |
673 // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads |
679 // As a result, the next sweep is likely to happen every 100ms |
674 // is not critical and we do not want idle compiler threads to wake up too often. |
680 // with an arbitrary number of threads that do sweeping. |
675 lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000); |
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 } |
676 } |
701 |
677 |
702 if (CompileBroker::is_compilation_disabled_forever()) { |
678 if (CompileBroker::is_compilation_disabled_forever()) { |
703 return NULL; |
679 return NULL; |
704 } |
680 } |
884 int c2_count = 1; |
860 int c2_count = 1; |
885 |
861 |
886 _compilers[1] = new SharkCompiler(); |
862 _compilers[1] = new SharkCompiler(); |
887 #endif // SHARK |
863 #endif // SHARK |
888 |
864 |
889 // Start the CompilerThreads |
865 // Start the compiler thread(s) and the sweeper thread |
890 init_compiler_threads(c1_count, c2_count); |
866 init_compiler_sweeper_threads(c1_count, c2_count); |
891 // totalTime performance counter is always created as it is required |
867 // totalTime performance counter is always created as it is required |
892 // by the implementation of java.lang.management.CompilationMBean. |
868 // by the implementation of java.lang.management.CompilationMBean. |
893 { |
869 { |
894 EXCEPTION_MARK; |
870 EXCEPTION_MARK; |
895 _perf_total_compilation = |
871 _perf_total_compilation = |
989 |
965 |
990 _initialized = true; |
966 _initialized = true; |
991 } |
967 } |
992 |
968 |
993 |
969 |
994 CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, |
970 JavaThread* CompileBroker::make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, |
995 AbstractCompiler* comp, TRAPS) { |
971 AbstractCompiler* comp, bool compiler_thread, TRAPS) { |
996 CompilerThread* compiler_thread = NULL; |
972 JavaThread* thread = NULL; |
997 |
973 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_0); |
998 Klass* k = |
|
999 SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), |
|
1000 true, CHECK_0); |
|
1001 instanceKlassHandle klass (THREAD, k); |
974 instanceKlassHandle klass (THREAD, k); |
1002 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0); |
975 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0); |
1003 Handle string = java_lang_String::create_from_str(name, CHECK_0); |
976 Handle string = java_lang_String::create_from_str(name, CHECK_0); |
1004 |
977 |
1005 // Initialize thread_oop to put it into the system threadGroup |
978 // Initialize thread_oop to put it into the system threadGroup |
1013 string, |
986 string, |
1014 CHECK_0); |
987 CHECK_0); |
1015 |
988 |
1016 { |
989 { |
1017 MutexLocker mu(Threads_lock, THREAD); |
990 MutexLocker mu(Threads_lock, THREAD); |
1018 compiler_thread = new CompilerThread(queue, counters); |
991 if (compiler_thread) { |
|
992 thread = new CompilerThread(queue, counters); |
|
993 } else { |
|
994 thread = new CodeCacheSweeperThread(); |
|
995 } |
1019 // At this point the new CompilerThread data-races with this startup |
996 // At this point the new CompilerThread data-races with this startup |
1020 // thread (which I believe is the primoridal thread and NOT the VM |
997 // thread (which I believe is the primoridal thread and NOT the VM |
1021 // thread). This means Java bytecodes being executed at startup can |
998 // thread). This means Java bytecodes being executed at startup can |
1022 // queue compile jobs which will run at whatever default priority the |
999 // queue compile jobs which will run at whatever default priority the |
1023 // newly created CompilerThread runs at. |
1000 // newly created CompilerThread runs at. |
1026 // At this point it may be possible that no osthread was created for the |
1003 // 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 |
1004 // 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 |
1005 // in that case. However, since this must work and we do not allow |
1029 // exceptions anyway, check and abort if this fails. |
1006 // exceptions anyway, check and abort if this fails. |
1030 |
1007 |
1031 if (compiler_thread == NULL || compiler_thread->osthread() == NULL){ |
1008 if (thread == NULL || thread->osthread() == NULL) { |
1032 vm_exit_during_initialization("java.lang.OutOfMemoryError", |
1009 vm_exit_during_initialization("java.lang.OutOfMemoryError", |
1033 os::native_thread_creation_failed_msg()); |
1010 os::native_thread_creation_failed_msg()); |
1034 } |
1011 } |
1035 |
1012 |
1036 java_lang_Thread::set_thread(thread_oop(), compiler_thread); |
1013 java_lang_Thread::set_thread(thread_oop(), thread); |
1037 |
1014 |
1038 // Note that this only sets the JavaThread _priority field, which by |
1015 // Note that this only sets the JavaThread _priority field, which by |
1039 // definition is limited to Java priorities and not OS priorities. |
1016 // definition is limited to Java priorities and not OS priorities. |
1040 // The os-priority is set in the CompilerThread startup code itself |
1017 // The os-priority is set in the CompilerThread startup code itself |
1041 |
1018 |
1052 native_prio = os::java_to_os_priority[CriticalPriority]; |
1029 native_prio = os::java_to_os_priority[CriticalPriority]; |
1053 } else { |
1030 } else { |
1054 native_prio = os::java_to_os_priority[NearMaxPriority]; |
1031 native_prio = os::java_to_os_priority[NearMaxPriority]; |
1055 } |
1032 } |
1056 } |
1033 } |
1057 os::set_native_priority(compiler_thread, native_prio); |
1034 os::set_native_priority(thread, native_prio); |
1058 |
1035 |
1059 java_lang_Thread::set_daemon(thread_oop()); |
1036 java_lang_Thread::set_daemon(thread_oop()); |
1060 |
1037 |
1061 compiler_thread->set_threadObj(thread_oop()); |
1038 thread->set_threadObj(thread_oop()); |
1062 compiler_thread->set_compiler(comp); |
1039 if (compiler_thread) { |
1063 Threads::add(compiler_thread); |
1040 thread->as_CompilerThread()->set_compiler(comp); |
1064 Thread::start(compiler_thread); |
1041 } |
|
1042 Threads::add(thread); |
|
1043 Thread::start(thread); |
1065 } |
1044 } |
1066 |
1045 |
1067 // Let go of Threads_lock before yielding |
1046 // Let go of Threads_lock before yielding |
1068 os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS) |
1047 os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS) |
1069 |
1048 |
1070 return compiler_thread; |
1049 return thread; |
1071 } |
1050 } |
1072 |
1051 |
1073 |
1052 |
1074 void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) { |
1053 void CompileBroker::init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count) { |
1075 EXCEPTION_MARK; |
1054 EXCEPTION_MARK; |
1076 #if !defined(ZERO) && !defined(SHARK) |
1055 #if !defined(ZERO) && !defined(SHARK) |
1077 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); |
1056 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); |
1078 #endif // !ZERO && !SHARK |
1057 #endif // !ZERO && !SHARK |
1079 // Initialize the compilation queue |
1058 // Initialize the compilation queue |
1086 _compilers[0]->set_num_compiler_threads(c1_compiler_count); |
1065 _compilers[0]->set_num_compiler_threads(c1_compiler_count); |
1087 } |
1066 } |
1088 |
1067 |
1089 int compiler_count = c1_compiler_count + c2_compiler_count; |
1068 int compiler_count = c1_compiler_count + c2_compiler_count; |
1090 |
1069 |
1091 _compiler_threads = |
|
1092 new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true); |
|
1093 |
|
1094 char name_buffer[256]; |
1070 char name_buffer[256]; |
|
1071 const bool compiler_thread = true; |
1095 for (int i = 0; i < c2_compiler_count; i++) { |
1072 for (int i = 0; i < c2_compiler_count; i++) { |
1096 // Create a name for our thread. |
1073 // Create a name for our thread. |
1097 sprintf(name_buffer, "C2 CompilerThread%d", i); |
1074 sprintf(name_buffer, "C2 CompilerThread%d", i); |
1098 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); |
1075 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); |
1099 // Shark and C2 |
1076 // Shark and C2 |
1100 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], CHECK); |
1077 make_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], compiler_thread, CHECK); |
1101 _compiler_threads->append(new_thread); |
|
1102 } |
1078 } |
1103 |
1079 |
1104 for (int i = c2_compiler_count; i < compiler_count; i++) { |
1080 for (int i = c2_compiler_count; i < compiler_count; i++) { |
1105 // Create a name for our thread. |
1081 // Create a name for our thread. |
1106 sprintf(name_buffer, "C1 CompilerThread%d", i); |
1082 sprintf(name_buffer, "C1 CompilerThread%d", i); |
1107 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); |
1083 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); |
1108 // C1 |
1084 // C1 |
1109 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], CHECK); |
1085 make_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], compiler_thread, CHECK); |
1110 _compiler_threads->append(new_thread); |
|
1111 } |
1086 } |
1112 |
1087 |
1113 if (UsePerfData) { |
1088 if (UsePerfData) { |
1114 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK); |
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); |
1115 } |
1095 } |
1116 } |
1096 } |
1117 |
1097 |
1118 |
1098 |
1119 /** |
1099 /** |
1757 // space in the code cache to generate the necessary stubs, etc. |
1737 // space in the code cache to generate the necessary stubs, etc. |
1758 while (!is_compilation_disabled_forever()) { |
1738 while (!is_compilation_disabled_forever()) { |
1759 // We need this HandleMark to avoid leaking VM handles. |
1739 // We need this HandleMark to avoid leaking VM handles. |
1760 HandleMark hm(thread); |
1740 HandleMark hm(thread); |
1761 |
1741 |
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(); |
1742 CompileTask* task = queue->get(); |
1770 if (task == NULL) { |
1743 if (task == NULL) { |
1771 continue; |
1744 continue; |
1772 } |
1745 } |
1773 |
1746 |
1774 // Give compiler threads an extra quanta. They tend to be bursty and |
1747 // Give compiler threads an extra quanta. They tend to be bursty and |
1775 // this helps the compiler to finish up the job. |
1748 // this helps the compiler to finish up the job. |
1776 if( CompilerThreadHintNoPreempt ) |
1749 if (CompilerThreadHintNoPreempt) { |
1777 os::hint_no_preempt(); |
1750 os::hint_no_preempt(); |
|
1751 } |
1778 |
1752 |
1779 // trace per thread time and compile statistics |
1753 // trace per thread time and compile statistics |
1780 CompilerCounters* counters = ((CompilerThread*)thread)->counters(); |
1754 CompilerCounters* counters = ((CompilerThread*)thread)->counters(); |
1781 PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter()); |
1755 PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter()); |
1782 |
1756 |
2072 } |
2046 } |
2073 #endif |
2047 #endif |
2074 } |
2048 } |
2075 |
2049 |
2076 /** |
2050 /** |
2077 * The CodeCache is full. Print out warning and disable compilation |
2051 * The CodeCache is full. Print warning and disable compilation. |
2078 * or try code cache cleaning so compilation can continue later. |
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. |
2079 */ |
2055 */ |
2080 void CompileBroker::handle_full_code_cache(int code_blob_type) { |
2056 void CompileBroker::handle_full_code_cache(int code_blob_type) { |
2081 UseInterpreter = true; |
2057 UseInterpreter = true; |
2082 if (UseCompiler || AlwaysCompileLoopMethods ) { |
2058 if (UseCompiler || AlwaysCompileLoopMethods ) { |
2083 if (xtty != NULL) { |
2059 if (xtty != NULL) { |
2105 if (UseCodeCacheFlushing) { |
2081 if (UseCodeCacheFlushing) { |
2106 // Since code cache is full, immediately stop new compiles |
2082 // Since code cache is full, immediately stop new compiles |
2107 if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) { |
2083 if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) { |
2108 NMethodSweeper::log_sweep("disable_compiler"); |
2084 NMethodSweeper::log_sweep("disable_compiler"); |
2109 } |
2085 } |
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 { |
2086 } else { |
2115 disable_compilation_forever(); |
2087 disable_compilation_forever(); |
2116 } |
2088 } |
2117 |
2089 |
2118 CodeCache::report_codemem_full(code_blob_type, should_print_compiler_warning()); |
2090 CodeCache::report_codemem_full(code_blob_type, should_print_compiler_warning()); |