285 CompileTask::free(task); |
297 CompileTask::free(task); |
286 } |
298 } |
287 } |
299 } |
288 |
300 |
289 /** |
301 /** |
|
302 * Check if a CompilerThread can be removed and update count if requested. |
|
303 */ |
|
304 static bool can_remove(CompilerThread *ct, bool do_it) { |
|
305 assert(UseDynamicNumberOfCompilerThreads, "or shouldn't be here"); |
|
306 if (!ReduceNumberOfCompilerThreads) return false; |
|
307 |
|
308 AbstractCompiler *compiler = ct->compiler(); |
|
309 int compiler_count = compiler->num_compiler_threads(); |
|
310 bool c1 = compiler->is_c1(); |
|
311 |
|
312 // Keep at least 1 compiler thread of each type. |
|
313 if (compiler_count < 2) return false; |
|
314 |
|
315 // Keep thread alive for at least some time. |
|
316 if (ct->idle_time_millis() < (c1 ? 500 : 100)) return false; |
|
317 |
|
318 // We only allow the last compiler thread of each type to get removed. |
|
319 jobject last_compiler = c1 ? CompileBroker::compiler1_object(compiler_count - 1) |
|
320 : CompileBroker::compiler2_object(compiler_count - 1); |
|
321 if (oopDesc::equals(ct->threadObj(), JNIHandles::resolve_non_null(last_compiler))) { |
|
322 if (do_it) { |
|
323 assert_locked_or_safepoint(CompileThread_lock); // Update must be consistent. |
|
324 compiler->set_num_compiler_threads(compiler_count - 1); |
|
325 } |
|
326 return true; |
|
327 } |
|
328 return false; |
|
329 } |
|
330 |
|
331 /** |
290 * Add a CompileTask to a CompileQueue. |
332 * Add a CompileTask to a CompileQueue. |
291 */ |
333 */ |
292 void CompileQueue::add(CompileTask* task) { |
334 void CompileQueue::add(CompileTask* task) { |
293 assert(MethodCompileQueue_lock->owned_by_self(), "must own lock"); |
335 assert(MethodCompileQueue_lock->owned_by_self(), "must own lock"); |
294 |
336 |
543 if (UseJVMCICompiler) { |
590 if (UseJVMCICompiler) { |
544 _compilers[1] = jvmci; |
591 _compilers[1] = jvmci; |
545 if (FLAG_IS_DEFAULT(JVMCIThreads)) { |
592 if (FLAG_IS_DEFAULT(JVMCIThreads)) { |
546 if (BootstrapJVMCI) { |
593 if (BootstrapJVMCI) { |
547 // JVMCI will bootstrap so give it more threads |
594 // JVMCI will bootstrap so give it more threads |
548 c2_count = MIN2(32, os::active_processor_count()); |
595 _c2_count = MIN2(32, os::active_processor_count()); |
549 } |
596 } |
550 } else { |
597 } else { |
551 c2_count = JVMCIThreads; |
598 _c2_count = JVMCIThreads; |
552 } |
599 } |
553 if (FLAG_IS_DEFAULT(JVMCIHostThreads)) { |
600 if (FLAG_IS_DEFAULT(JVMCIHostThreads)) { |
554 } else { |
601 } else { |
555 c1_count = JVMCIHostThreads; |
602 _c1_count = JVMCIHostThreads; |
556 } |
603 } |
557 } |
604 } |
558 } |
605 } |
559 #endif // INCLUDE_JVMCI |
606 #endif // INCLUDE_JVMCI |
560 |
607 |
561 #ifdef COMPILER1 |
608 #ifdef COMPILER1 |
562 if (c1_count > 0) { |
609 if (_c1_count > 0) { |
563 _compilers[0] = new Compiler(); |
610 _compilers[0] = new Compiler(); |
564 } |
611 } |
565 #endif // COMPILER1 |
612 #endif // COMPILER1 |
566 |
613 |
567 #ifdef COMPILER2 |
614 #ifdef COMPILER2 |
568 if (true JVMCI_ONLY( && !UseJVMCICompiler)) { |
615 if (true JVMCI_ONLY( && !UseJVMCICompiler)) { |
569 if (c2_count > 0) { |
616 if (_c2_count > 0) { |
570 _compilers[1] = new C2Compiler(); |
617 _compilers[1] = new C2Compiler(); |
571 } |
618 } |
572 } |
619 } |
573 #endif // COMPILER2 |
620 #endif // COMPILER2 |
574 |
621 |
575 // Start the compiler thread(s) and the sweeper thread |
622 // Start the compiler thread(s) and the sweeper thread |
576 init_compiler_sweeper_threads(c1_count, c2_count); |
623 init_compiler_sweeper_threads(); |
577 // totalTime performance counter is always created as it is required |
624 // totalTime performance counter is always created as it is required |
578 // by the implementation of java.lang.management.CompilationMBean. |
625 // by the implementation of java.lang.management.CompilationMBean. |
579 { |
626 { |
580 EXCEPTION_MARK; |
627 EXCEPTION_MARK; |
581 _perf_total_compilation = |
628 _perf_total_compilation = |
677 // prior to this will be silently ignored. |
724 // prior to this will be silently ignored. |
678 void CompileBroker::compilation_init_phase2() { |
725 void CompileBroker::compilation_init_phase2() { |
679 _initialized = true; |
726 _initialized = true; |
680 } |
727 } |
681 |
728 |
682 JavaThread* CompileBroker::make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, |
729 Handle CompileBroker::create_thread_oop(const char* name, TRAPS) { |
683 AbstractCompiler* comp, bool compiler_thread, TRAPS) { |
730 Klass* k = SystemDictionary::find(vmSymbols::java_lang_Thread(), Handle(), Handle(), CHECK_NH); |
684 JavaThread* thread = NULL; |
731 assert(k != NULL, "must be initialized"); |
685 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_0); |
|
686 InstanceKlass* klass = InstanceKlass::cast(k); |
732 InstanceKlass* klass = InstanceKlass::cast(k); |
687 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0); |
733 instanceHandle thread_handle = klass->allocate_instance_handle(CHECK_NH); |
688 Handle string = java_lang_String::create_from_str(name, CHECK_0); |
734 Handle string = java_lang_String::create_from_str(name, CHECK_NH); |
689 |
735 |
690 // Initialize thread_oop to put it into the system threadGroup |
736 // Initialize thread_oop to put it into the system threadGroup |
691 Handle thread_group (THREAD, Universe::system_thread_group()); |
737 Handle thread_group(THREAD, Universe::system_thread_group()); |
692 JavaValue result(T_VOID); |
738 JavaValue result(T_VOID); |
693 JavaCalls::call_special(&result, thread_oop, |
739 JavaCalls::call_special(&result, thread_handle, |
694 klass, |
740 klass, |
695 vmSymbols::object_initializer_name(), |
741 vmSymbols::object_initializer_name(), |
696 vmSymbols::threadgroup_string_void_signature(), |
742 vmSymbols::threadgroup_string_void_signature(), |
697 thread_group, |
743 thread_group, |
698 string, |
744 string, |
699 CHECK_0); |
745 CHECK_NH); |
700 |
746 |
|
747 return thread_handle; |
|
748 } |
|
749 |
|
750 |
|
751 JavaThread* CompileBroker::make_thread(jobject thread_handle, CompileQueue* queue, |
|
752 AbstractCompiler* comp, bool compiler_thread, TRAPS) { |
|
753 JavaThread* thread = NULL; |
701 { |
754 { |
702 MutexLocker mu(Threads_lock, THREAD); |
755 MutexLocker mu(Threads_lock, THREAD); |
703 if (compiler_thread) { |
756 if (compiler_thread) { |
704 thread = new CompilerThread(queue, counters); |
757 if (!InjectCompilerCreationFailure || comp->num_compiler_threads() == 0) { |
|
758 CompilerCounters* counters = new CompilerCounters(); |
|
759 thread = new CompilerThread(queue, counters); |
|
760 } |
705 } else { |
761 } else { |
706 thread = new CodeCacheSweeperThread(); |
762 thread = new CodeCacheSweeperThread(); |
707 } |
763 } |
708 // At this point the new CompilerThread data-races with this startup |
764 // At this point the new CompilerThread data-races with this startup |
709 // thread (which I believe is the primoridal thread and NOT the VM |
765 // thread (which I believe is the primoridal thread and NOT the VM |
741 native_prio = os::java_to_os_priority[NearMaxPriority]; |
797 native_prio = os::java_to_os_priority[NearMaxPriority]; |
742 } |
798 } |
743 } |
799 } |
744 os::set_native_priority(thread, native_prio); |
800 os::set_native_priority(thread, native_prio); |
745 |
801 |
746 java_lang_Thread::set_daemon(thread_oop()); |
802 java_lang_Thread::set_daemon(JNIHandles::resolve_non_null(thread_handle)); |
747 |
803 |
748 thread->set_threadObj(thread_oop()); |
804 thread->set_threadObj(JNIHandles::resolve_non_null(thread_handle)); |
749 if (compiler_thread) { |
805 if (compiler_thread) { |
750 thread->as_CompilerThread()->set_compiler(comp); |
806 thread->as_CompilerThread()->set_compiler(comp); |
751 } |
807 } |
752 Threads::add(thread); |
808 Threads::add(thread); |
753 Thread::start(thread); |
809 Thread::start(thread); |
754 } |
810 } |
755 } |
811 } |
756 |
812 |
757 // First release lock before aborting VM. |
813 // First release lock before aborting VM. |
758 if (thread == NULL || thread->osthread() == NULL) { |
814 if (thread == NULL || thread->osthread() == NULL) { |
|
815 if (UseDynamicNumberOfCompilerThreads && comp->num_compiler_threads() > 0) { |
|
816 if (thread != NULL) { |
|
817 thread->smr_delete(); |
|
818 } |
|
819 return NULL; |
|
820 } |
759 vm_exit_during_initialization("java.lang.OutOfMemoryError", |
821 vm_exit_during_initialization("java.lang.OutOfMemoryError", |
760 os::native_thread_creation_failed_msg()); |
822 os::native_thread_creation_failed_msg()); |
761 } |
823 } |
762 |
824 |
763 // Let go of Threads_lock before yielding |
825 // Let go of Threads_lock before yielding |
765 |
827 |
766 return thread; |
828 return thread; |
767 } |
829 } |
768 |
830 |
769 |
831 |
770 void CompileBroker::init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count) { |
832 void CompileBroker::init_compiler_sweeper_threads() { |
771 EXCEPTION_MARK; |
833 EXCEPTION_MARK; |
772 #if !defined(ZERO) |
834 #if !defined(ZERO) |
773 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); |
835 assert(_c2_count > 0 || _c1_count > 0, "No compilers?"); |
774 #endif // !ZERO |
836 #endif // !ZERO |
775 // Initialize the compilation queue |
837 // Initialize the compilation queue |
776 if (c2_compiler_count > 0) { |
838 if (_c2_count > 0) { |
777 const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue"; |
839 const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue"; |
778 _c2_compile_queue = new CompileQueue(name); |
840 _c2_compile_queue = new CompileQueue(name); |
779 _compilers[1]->set_num_compiler_threads(c2_compiler_count); |
841 _compiler2_objects = NEW_C_HEAP_ARRAY(jobject, _c2_count, mtCompiler); |
780 } |
842 _compiler2_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c2_count, mtCompiler); |
781 if (c1_compiler_count > 0) { |
843 } |
|
844 if (_c1_count > 0) { |
782 _c1_compile_queue = new CompileQueue("C1 compile queue"); |
845 _c1_compile_queue = new CompileQueue("C1 compile queue"); |
783 _compilers[0]->set_num_compiler_threads(c1_compiler_count); |
846 _compiler1_objects = NEW_C_HEAP_ARRAY(jobject, _c1_count, mtCompiler); |
784 } |
847 _compiler1_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c1_count, mtCompiler); |
785 |
848 } |
786 int compiler_count = c1_compiler_count + c2_compiler_count; |
|
787 |
849 |
788 char name_buffer[256]; |
850 char name_buffer[256]; |
789 const bool compiler_thread = true; |
851 |
790 for (int i = 0; i < c2_compiler_count; i++) { |
852 for (int i = 0; i < _c2_count; i++) { |
791 // Create a name for our thread. |
853 // Create a name for our thread. |
792 sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i); |
854 sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i); |
793 CompilerCounters* counters = new CompilerCounters(); |
855 jobject thread_handle = JNIHandles::make_global(create_thread_oop(name_buffer, THREAD)); |
794 make_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], compiler_thread, CHECK); |
856 _compiler2_objects[i] = thread_handle; |
795 } |
857 _compiler2_logs[i] = NULL; |
796 |
858 |
797 for (int i = c2_compiler_count; i < compiler_count; i++) { |
859 if (!UseDynamicNumberOfCompilerThreads || i == 0) { |
|
860 JavaThread *ct = make_thread(thread_handle, _c2_compile_queue, _compilers[1], /* compiler_thread */ true, CHECK); |
|
861 assert(ct != NULL, "should have been handled for initial thread"); |
|
862 _compilers[1]->set_num_compiler_threads(i + 1); |
|
863 if (TraceCompilerThreads) { |
|
864 ResourceMark rm; |
|
865 MutexLocker mu(Threads_lock); |
|
866 tty->print_cr("Added initial compiler thread %s", ct->get_thread_name()); |
|
867 } |
|
868 } |
|
869 } |
|
870 |
|
871 for (int i = 0; i < _c1_count; i++) { |
798 // Create a name for our thread. |
872 // Create a name for our thread. |
799 sprintf(name_buffer, "C1 CompilerThread%d", i); |
873 sprintf(name_buffer, "C1 CompilerThread%d", i); |
800 CompilerCounters* counters = new CompilerCounters(); |
874 jobject thread_handle = JNIHandles::make_global(create_thread_oop(name_buffer, THREAD)); |
801 // C1 |
875 _compiler1_objects[i] = thread_handle; |
802 make_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], compiler_thread, CHECK); |
876 _compiler1_logs[i] = NULL; |
|
877 |
|
878 if (!UseDynamicNumberOfCompilerThreads || i == 0) { |
|
879 JavaThread *ct = make_thread(thread_handle, _c1_compile_queue, _compilers[0], /* compiler_thread */ true, CHECK); |
|
880 assert(ct != NULL, "should have been handled for initial thread"); |
|
881 _compilers[0]->set_num_compiler_threads(i + 1); |
|
882 if (TraceCompilerThreads) { |
|
883 ResourceMark rm; |
|
884 MutexLocker mu(Threads_lock); |
|
885 tty->print_cr("Added initial compiler thread %s", ct->get_thread_name()); |
|
886 } |
|
887 } |
803 } |
888 } |
804 |
889 |
805 if (UsePerfData) { |
890 if (UsePerfData) { |
806 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK); |
891 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, _c1_count + _c2_count, CHECK); |
807 } |
892 } |
808 |
893 |
809 if (MethodFlushing) { |
894 if (MethodFlushing) { |
810 // Initialize the sweeper thread |
895 // Initialize the sweeper thread |
811 make_thread("Sweeper thread", NULL, NULL, NULL, false, CHECK); |
896 jobject thread_handle = JNIHandles::make_local(THREAD, create_thread_oop("Sweeper thread", THREAD)()); |
812 } |
897 make_thread(thread_handle, NULL, NULL, /* compiler_thread */ false, CHECK); |
|
898 } |
|
899 } |
|
900 |
|
901 void CompileBroker::possibly_add_compiler_threads() { |
|
902 EXCEPTION_MARK; |
|
903 |
|
904 julong available_memory = os::available_memory(); |
|
905 // Only do attempt to start additional threads if the lock is free. |
|
906 if (!CompileThread_lock->try_lock()) return; |
|
907 |
|
908 if (_c2_compile_queue != NULL) { |
|
909 int old_c2_count = _compilers[1]->num_compiler_threads(); |
|
910 int new_c2_count = MIN3(_c2_count, |
|
911 _c2_compile_queue->size() / 2, |
|
912 (int)(available_memory / 200*M)); |
|
913 |
|
914 for (int i = old_c2_count; i < new_c2_count; i++) { |
|
915 JavaThread *ct = make_thread(compiler2_object(i), _c2_compile_queue, _compilers[1], true, CHECK); |
|
916 if (ct == NULL) break; |
|
917 _compilers[1]->set_num_compiler_threads(i + 1); |
|
918 if (TraceCompilerThreads) { |
|
919 ResourceMark rm; |
|
920 MutexLocker mu(Threads_lock); |
|
921 tty->print_cr("Added compiler thread %s (available memory: %dMB)", |
|
922 ct->get_thread_name(), (int)(available_memory/M)); |
|
923 } |
|
924 } |
|
925 } |
|
926 |
|
927 if (_c1_compile_queue != NULL) { |
|
928 int old_c1_count = _compilers[0]->num_compiler_threads(); |
|
929 int new_c1_count = MIN3(_c1_count, |
|
930 _c1_compile_queue->size() / 4, |
|
931 (int)(available_memory / 100*M)); |
|
932 |
|
933 for (int i = old_c1_count; i < new_c1_count; i++) { |
|
934 JavaThread *ct = make_thread(compiler1_object(i), _c1_compile_queue, _compilers[0], true, CHECK); |
|
935 if (ct == NULL) break; |
|
936 _compilers[0]->set_num_compiler_threads(i + 1); |
|
937 if (TraceCompilerThreads) { |
|
938 ResourceMark rm; |
|
939 MutexLocker mu(Threads_lock); |
|
940 tty->print_cr("Added compiler thread %s (available memory: %dMB)", |
|
941 ct->get_thread_name(), (int)(available_memory/M)); |
|
942 } |
|
943 } |
|
944 } |
|
945 |
|
946 CompileThread_lock->unlock(); |
813 } |
947 } |
814 |
948 |
815 |
949 |
816 /** |
950 /** |
817 * Set the methods on the stack as on_stack so that redefine classes doesn't |
951 * Set the methods on the stack as on_stack so that redefine classes doesn't |
1544 // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then |
1678 // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then |
1545 // fail. This can be done later if necessary. |
1679 // fail. This can be done later if necessary. |
1546 } |
1680 } |
1547 } |
1681 } |
1548 |
1682 |
|
1683 /** |
|
1684 * Helper function to create new or reuse old CompileLog. |
|
1685 */ |
|
1686 CompileLog* CompileBroker::get_log(CompilerThread* ct) { |
|
1687 if (!LogCompilation) return NULL; |
|
1688 |
|
1689 AbstractCompiler *compiler = ct->compiler(); |
|
1690 bool c1 = compiler->is_c1(); |
|
1691 jobject* compiler_objects = c1 ? _compiler1_objects : _compiler2_objects; |
|
1692 assert(compiler_objects != NULL, "must be initialized at this point"); |
|
1693 CompileLog** logs = c1 ? _compiler1_logs : _compiler2_logs; |
|
1694 assert(logs != NULL, "must be initialized at this point"); |
|
1695 int count = c1 ? _c1_count : _c2_count; |
|
1696 |
|
1697 // Find Compiler number by its threadObj. |
|
1698 oop compiler_obj = ct->threadObj(); |
|
1699 int compiler_number = 0; |
|
1700 bool found = false; |
|
1701 for (; compiler_number < count; compiler_number++) { |
|
1702 if (oopDesc::equals(JNIHandles::resolve_non_null(compiler_objects[compiler_number]), compiler_obj)) { |
|
1703 found = true; |
|
1704 break; |
|
1705 } |
|
1706 } |
|
1707 assert(found, "Compiler must exist at this point"); |
|
1708 |
|
1709 // Determine pointer for this thread's log. |
|
1710 CompileLog** log_ptr = &logs[compiler_number]; |
|
1711 |
|
1712 // Return old one if it exists. |
|
1713 CompileLog* log = *log_ptr; |
|
1714 if (log != NULL) { |
|
1715 ct->init_log(log); |
|
1716 return log; |
|
1717 } |
|
1718 |
|
1719 // Create a new one and remember it. |
|
1720 init_compiler_thread_log(); |
|
1721 log = ct->log(); |
|
1722 *log_ptr = log; |
|
1723 return log; |
|
1724 } |
|
1725 |
1549 // ------------------------------------------------------------------ |
1726 // ------------------------------------------------------------------ |
1550 // CompileBroker::compiler_thread_loop |
1727 // CompileBroker::compiler_thread_loop |
1551 // |
1728 // |
1552 // The main loop run by a CompilerThread. |
1729 // The main loop run by a CompilerThread. |
1553 void CompileBroker::compiler_thread_loop() { |
1730 void CompileBroker::compiler_thread_loop() { |