718 }
719
720 // Completes compiler initialization. Compilation requests submitted
721 // prior to this will be silently ignored.
722 void CompileBroker::compilation_init_phase2() {
723 _initialized = true;
724 }
725
726 Handle CompileBroker::create_thread_oop(const char* name, TRAPS) {
727 Handle string = java_lang_String::create_from_str(name, CHECK_NH);
728 Handle thread_group(THREAD, Universe::system_thread_group());
729 return JavaCalls::construct_new_instance(
730 SystemDictionary::Thread_klass(),
731 vmSymbols::threadgroup_string_void_signature(),
732 thread_group,
733 string,
734 CHECK_NH);
735 }
736
737
738 JavaThread* CompileBroker::make_thread(jobject thread_handle, CompileQueue* queue, AbstractCompiler* comp, TRAPS) {
739 JavaThread* thread = NULL;
740 {
741 MutexLocker mu(Threads_lock, THREAD);
742 if (comp != NULL) {
743 if (!InjectCompilerCreationFailure || comp->num_compiler_threads() == 0) {
744 CompilerCounters* counters = new CompilerCounters();
745 thread = new CompilerThread(queue, counters);
746 }
747 } else {
748 thread = new CodeCacheSweeperThread();
749 }
750 // At this point the new CompilerThread data-races with this startup
751 // thread (which I believe is the primoridal thread and NOT the VM
752 // thread). This means Java bytecodes being executed at startup can
753 // queue compile jobs which will run at whatever default priority the
754 // newly created CompilerThread runs at.
755
756
757 // At this point it may be possible that no osthread was created for the
758 // JavaThread due to lack of memory. We would have to throw an exception
759 // in that case. However, since this must work and we do not allow
760 // exceptions anyway, check and abort if this fails. But first release the
761 // lock.
762
763 if (thread != NULL && thread->osthread() != NULL) {
764
765 java_lang_Thread::set_thread(JNIHandles::resolve_non_null(thread_handle), thread);
766
767 // Note that this only sets the JavaThread _priority field, which by
768 // definition is limited to Java priorities and not OS priorities.
769 // The os-priority is set in the CompilerThread startup code itself
771 java_lang_Thread::set_priority(JNIHandles::resolve_non_null(thread_handle), NearMaxPriority);
772
773 // Note that we cannot call os::set_priority because it expects Java
774 // priorities and we are *explicitly* using OS priorities so that it's
775 // possible to set the compiler thread priority higher than any Java
776 // thread.
777
778 int native_prio = CompilerThreadPriority;
779 if (native_prio == -1) {
780 if (UseCriticalCompilerThreadPriority) {
781 native_prio = os::java_to_os_priority[CriticalPriority];
782 } else {
783 native_prio = os::java_to_os_priority[NearMaxPriority];
784 }
785 }
786 os::set_native_priority(thread, native_prio);
787
788 java_lang_Thread::set_daemon(JNIHandles::resolve_non_null(thread_handle));
789
790 thread->set_threadObj(JNIHandles::resolve_non_null(thread_handle));
791 if (comp != NULL) {
792 thread->as_CompilerThread()->set_compiler(comp);
793 }
794 Threads::add(thread);
795 Thread::start(thread);
796 }
797 }
798
799 // First release lock before aborting VM.
800 if (thread == NULL || thread->osthread() == NULL) {
801 if (UseDynamicNumberOfCompilerThreads && comp != NULL && comp->num_compiler_threads() > 0) {
802 if (thread != NULL) {
803 thread->smr_delete();
804 }
805 return NULL;
806 }
807 vm_exit_during_initialization("java.lang.OutOfMemoryError",
808 os::native_thread_creation_failed_msg());
809 }
810
811 // Let go of Threads_lock before yielding
812 os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
813
814 return thread;
815 }
816
817
818 void CompileBroker::init_compiler_sweeper_threads() {
819 EXCEPTION_MARK;
820 #if !defined(ZERO)
821 assert(_c2_count > 0 || _c1_count > 0, "No compilers?");
827 _compiler2_objects = NEW_C_HEAP_ARRAY(jobject, _c2_count, mtCompiler);
828 _compiler2_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c2_count, mtCompiler);
829 }
830 if (_c1_count > 0) {
831 _c1_compile_queue = new CompileQueue("C1 compile queue");
832 _compiler1_objects = NEW_C_HEAP_ARRAY(jobject, _c1_count, mtCompiler);
833 _compiler1_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c1_count, mtCompiler);
834 }
835
836 char name_buffer[256];
837
838 for (int i = 0; i < _c2_count; i++) {
839 // Create a name for our thread.
840 sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
841 Handle thread_oop = create_thread_oop(name_buffer, CHECK);
842 jobject thread_handle = JNIHandles::make_global(thread_oop);
843 _compiler2_objects[i] = thread_handle;
844 _compiler2_logs[i] = NULL;
845
846 if (!UseDynamicNumberOfCompilerThreads || i == 0) {
847 JavaThread *ct = make_thread(thread_handle, _c2_compile_queue, _compilers[1], CHECK);
848 assert(ct != NULL, "should have been handled for initial thread");
849 _compilers[1]->set_num_compiler_threads(i + 1);
850 if (TraceCompilerThreads) {
851 ResourceMark rm;
852 MutexLocker mu(Threads_lock);
853 tty->print_cr("Added initial compiler thread %s", ct->get_thread_name());
854 }
855 }
856 }
857
858 for (int i = 0; i < _c1_count; i++) {
859 // Create a name for our thread.
860 sprintf(name_buffer, "C1 CompilerThread%d", i);
861 Handle thread_oop = create_thread_oop(name_buffer, CHECK);
862 jobject thread_handle = JNIHandles::make_global(thread_oop);
863 _compiler1_objects[i] = thread_handle;
864 _compiler1_logs[i] = NULL;
865
866 if (!UseDynamicNumberOfCompilerThreads || i == 0) {
867 JavaThread *ct = make_thread(thread_handle, _c1_compile_queue, _compilers[0], CHECK);
868 assert(ct != NULL, "should have been handled for initial thread");
869 _compilers[0]->set_num_compiler_threads(i + 1);
870 if (TraceCompilerThreads) {
871 ResourceMark rm;
872 MutexLocker mu(Threads_lock);
873 tty->print_cr("Added initial compiler thread %s", ct->get_thread_name());
874 }
875 }
876 }
877
878 if (UsePerfData) {
879 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, _c1_count + _c2_count, CHECK);
880 }
881
882 if (MethodFlushing) {
883 // Initialize the sweeper thread
884 Handle thread_oop = create_thread_oop("Sweeper thread", CHECK);
885 jobject thread_handle = JNIHandles::make_local(THREAD, thread_oop());
886 make_thread(thread_handle, NULL, NULL, CHECK);
887 }
888 }
889
890 void CompileBroker::possibly_add_compiler_threads() {
891 EXCEPTION_MARK;
892
893 julong available_memory = os::available_memory();
894 // If SegmentedCodeCache is off, both values refer to the single heap (with type CodeBlobType::All).
895 size_t available_cc_np = CodeCache::unallocated_capacity(CodeBlobType::MethodNonProfiled),
896 available_cc_p = CodeCache::unallocated_capacity(CodeBlobType::MethodProfiled);
897
898 // Only do attempt to start additional threads if the lock is free.
899 if (!CompileThread_lock->try_lock()) return;
900
901 if (_c2_compile_queue != NULL) {
902 int old_c2_count = _compilers[1]->num_compiler_threads();
903 int new_c2_count = MIN4(_c2_count,
904 _c2_compile_queue->size() / 2,
905 (int)(available_memory / (200*M)),
906 (int)(available_cc_np / (128*K)));
907
908 for (int i = old_c2_count; i < new_c2_count; i++) {
909 JavaThread *ct = make_thread(compiler2_object(i), _c2_compile_queue, _compilers[1], CHECK);
910 if (ct == NULL) break;
911 _compilers[1]->set_num_compiler_threads(i + 1);
912 if (TraceCompilerThreads) {
913 ResourceMark rm;
914 MutexLocker mu(Threads_lock);
915 tty->print_cr("Added compiler thread %s (available memory: %dMB, available non-profiled code cache: %dMB)",
916 ct->get_thread_name(), (int)(available_memory/M), (int)(available_cc_np/M));
917 }
918 }
919 }
920
921 if (_c1_compile_queue != NULL) {
922 int old_c1_count = _compilers[0]->num_compiler_threads();
923 int new_c1_count = MIN4(_c1_count,
924 _c1_compile_queue->size() / 4,
925 (int)(available_memory / (100*M)),
926 (int)(available_cc_p / (128*K)));
927
928 for (int i = old_c1_count; i < new_c1_count; i++) {
929 JavaThread *ct = make_thread(compiler1_object(i), _c1_compile_queue, _compilers[0], CHECK);
930 if (ct == NULL) break;
931 _compilers[0]->set_num_compiler_threads(i + 1);
932 if (TraceCompilerThreads) {
933 ResourceMark rm;
934 MutexLocker mu(Threads_lock);
935 tty->print_cr("Added compiler thread %s (available memory: %dMB, available profiled code cache: %dMB)",
936 ct->get_thread_name(), (int)(available_memory/M), (int)(available_cc_p/M));
937 }
938 }
939 }
940
941 CompileThread_lock->unlock();
942 }
943
944
945 /**
946 * Set the methods on the stack as on_stack so that redefine classes doesn't
947 * reclaim them. This method is executed at a safepoint.
948 */
949 void CompileBroker::mark_on_stack() {
|
718 }
719
720 // Completes compiler initialization. Compilation requests submitted
721 // prior to this will be silently ignored.
722 void CompileBroker::compilation_init_phase2() {
723 _initialized = true;
724 }
725
726 Handle CompileBroker::create_thread_oop(const char* name, TRAPS) {
727 Handle string = java_lang_String::create_from_str(name, CHECK_NH);
728 Handle thread_group(THREAD, Universe::system_thread_group());
729 return JavaCalls::construct_new_instance(
730 SystemDictionary::Thread_klass(),
731 vmSymbols::threadgroup_string_void_signature(),
732 thread_group,
733 string,
734 CHECK_NH);
735 }
736
737
738 JavaThread* CompileBroker::make_thread(ThreadType type, jobject thread_handle, CompileQueue* queue, AbstractCompiler* comp, TRAPS) {
739 JavaThread* thread = NULL;
740 {
741 MutexLocker mu(Threads_lock, THREAD);
742 if (type == compiler_t) {
743 if (!InjectCompilerCreationFailure || comp->num_compiler_threads() == 0) {
744 CompilerCounters* counters = new CompilerCounters();
745 thread = new CompilerThread(queue, counters);
746 }
747 } else if (type == sweeper_t) {
748 thread = new CodeCacheSweeperThread();
749 }
750 #if defined(ASSERT) && COMPILER2_OR_JVMCI
751 else {
752 thread = new DeoptimizeObjectsALotThread();
753 }
754 #endif // ASSERT
755 // At this point the new CompilerThread data-races with this startup
756 // thread (which I believe is the primoridal thread and NOT the VM
757 // thread). This means Java bytecodes being executed at startup can
758 // queue compile jobs which will run at whatever default priority the
759 // newly created CompilerThread runs at.
760
761
762 // At this point it may be possible that no osthread was created for the
763 // JavaThread due to lack of memory. We would have to throw an exception
764 // in that case. However, since this must work and we do not allow
765 // exceptions anyway, check and abort if this fails. But first release the
766 // lock.
767
768 if (thread != NULL && thread->osthread() != NULL) {
769
770 java_lang_Thread::set_thread(JNIHandles::resolve_non_null(thread_handle), thread);
771
772 // Note that this only sets the JavaThread _priority field, which by
773 // definition is limited to Java priorities and not OS priorities.
774 // The os-priority is set in the CompilerThread startup code itself
776 java_lang_Thread::set_priority(JNIHandles::resolve_non_null(thread_handle), NearMaxPriority);
777
778 // Note that we cannot call os::set_priority because it expects Java
779 // priorities and we are *explicitly* using OS priorities so that it's
780 // possible to set the compiler thread priority higher than any Java
781 // thread.
782
783 int native_prio = CompilerThreadPriority;
784 if (native_prio == -1) {
785 if (UseCriticalCompilerThreadPriority) {
786 native_prio = os::java_to_os_priority[CriticalPriority];
787 } else {
788 native_prio = os::java_to_os_priority[NearMaxPriority];
789 }
790 }
791 os::set_native_priority(thread, native_prio);
792
793 java_lang_Thread::set_daemon(JNIHandles::resolve_non_null(thread_handle));
794
795 thread->set_threadObj(JNIHandles::resolve_non_null(thread_handle));
796 if (type == compiler_t) {
797 thread->as_CompilerThread()->set_compiler(comp);
798 }
799 Threads::add(thread);
800 Thread::start(thread);
801 }
802 }
803
804 // First release lock before aborting VM.
805 if (thread == NULL || thread->osthread() == NULL) {
806 if (UseDynamicNumberOfCompilerThreads && type == compiler_t && comp->num_compiler_threads() > 0) {
807 if (thread != NULL) {
808 thread->smr_delete();
809 }
810 return NULL;
811 }
812 vm_exit_during_initialization("java.lang.OutOfMemoryError",
813 os::native_thread_creation_failed_msg());
814 }
815
816 // Let go of Threads_lock before yielding
817 os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
818
819 return thread;
820 }
821
822
823 void CompileBroker::init_compiler_sweeper_threads() {
824 EXCEPTION_MARK;
825 #if !defined(ZERO)
826 assert(_c2_count > 0 || _c1_count > 0, "No compilers?");
832 _compiler2_objects = NEW_C_HEAP_ARRAY(jobject, _c2_count, mtCompiler);
833 _compiler2_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c2_count, mtCompiler);
834 }
835 if (_c1_count > 0) {
836 _c1_compile_queue = new CompileQueue("C1 compile queue");
837 _compiler1_objects = NEW_C_HEAP_ARRAY(jobject, _c1_count, mtCompiler);
838 _compiler1_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c1_count, mtCompiler);
839 }
840
841 char name_buffer[256];
842
843 for (int i = 0; i < _c2_count; i++) {
844 // Create a name for our thread.
845 sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
846 Handle thread_oop = create_thread_oop(name_buffer, CHECK);
847 jobject thread_handle = JNIHandles::make_global(thread_oop);
848 _compiler2_objects[i] = thread_handle;
849 _compiler2_logs[i] = NULL;
850
851 if (!UseDynamicNumberOfCompilerThreads || i == 0) {
852 JavaThread *ct = make_thread(compiler_t, thread_handle, _c2_compile_queue, _compilers[1], CHECK);
853 assert(ct != NULL, "should have been handled for initial thread");
854 _compilers[1]->set_num_compiler_threads(i + 1);
855 if (TraceCompilerThreads) {
856 ResourceMark rm;
857 MutexLocker mu(Threads_lock);
858 tty->print_cr("Added initial compiler thread %s", ct->get_thread_name());
859 }
860 }
861 }
862
863 for (int i = 0; i < _c1_count; i++) {
864 // Create a name for our thread.
865 sprintf(name_buffer, "C1 CompilerThread%d", i);
866 Handle thread_oop = create_thread_oop(name_buffer, CHECK);
867 jobject thread_handle = JNIHandles::make_global(thread_oop);
868 _compiler1_objects[i] = thread_handle;
869 _compiler1_logs[i] = NULL;
870
871 if (!UseDynamicNumberOfCompilerThreads || i == 0) {
872 JavaThread *ct = make_thread(compiler_t, thread_handle, _c1_compile_queue, _compilers[0], CHECK);
873 assert(ct != NULL, "should have been handled for initial thread");
874 _compilers[0]->set_num_compiler_threads(i + 1);
875 if (TraceCompilerThreads) {
876 ResourceMark rm;
877 MutexLocker mu(Threads_lock);
878 tty->print_cr("Added initial compiler thread %s", ct->get_thread_name());
879 }
880 }
881 }
882
883 if (UsePerfData) {
884 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, _c1_count + _c2_count, CHECK);
885 }
886
887 if (MethodFlushing) {
888 // Initialize the sweeper thread
889 Handle thread_oop = create_thread_oop("Sweeper thread", CHECK);
890 jobject thread_handle = JNIHandles::make_local(THREAD, thread_oop());
891 make_thread(sweeper_t, thread_handle, NULL, NULL, CHECK);
892 }
893
894 #if defined(ASSERT) && COMPILER2_OR_JVMCI
895 if (DeoptimizeObjectsALot == 2) {
896 // Initialize and start the object deoptimizer thread
897 Handle thread_oop = create_thread_oop("Deoptimize objects a lot thread", CHECK);
898 jobject thread_handle = JNIHandles::make_local(THREAD, thread_oop());
899 make_thread(deoptimizer_t, thread_handle, NULL, NULL, CHECK);
900 }
901 #endif // defined(ASSERT) && COMPILER2_OR_JVMCI
902 }
903
904 void CompileBroker::possibly_add_compiler_threads() {
905 EXCEPTION_MARK;
906
907 julong available_memory = os::available_memory();
908 // If SegmentedCodeCache is off, both values refer to the single heap (with type CodeBlobType::All).
909 size_t available_cc_np = CodeCache::unallocated_capacity(CodeBlobType::MethodNonProfiled),
910 available_cc_p = CodeCache::unallocated_capacity(CodeBlobType::MethodProfiled);
911
912 // Only do attempt to start additional threads if the lock is free.
913 if (!CompileThread_lock->try_lock()) return;
914
915 if (_c2_compile_queue != NULL) {
916 int old_c2_count = _compilers[1]->num_compiler_threads();
917 int new_c2_count = MIN4(_c2_count,
918 _c2_compile_queue->size() / 2,
919 (int)(available_memory / (200*M)),
920 (int)(available_cc_np / (128*K)));
921
922 for (int i = old_c2_count; i < new_c2_count; i++) {
923 JavaThread *ct = make_thread(compiler_t, compiler2_object(i), _c2_compile_queue, _compilers[1], CHECK);
924 if (ct == NULL) break;
925 _compilers[1]->set_num_compiler_threads(i + 1);
926 if (TraceCompilerThreads) {
927 ResourceMark rm;
928 MutexLocker mu(Threads_lock);
929 tty->print_cr("Added compiler thread %s (available memory: %dMB, available non-profiled code cache: %dMB)",
930 ct->get_thread_name(), (int)(available_memory/M), (int)(available_cc_np/M));
931 }
932 }
933 }
934
935 if (_c1_compile_queue != NULL) {
936 int old_c1_count = _compilers[0]->num_compiler_threads();
937 int new_c1_count = MIN4(_c1_count,
938 _c1_compile_queue->size() / 4,
939 (int)(available_memory / (100*M)),
940 (int)(available_cc_p / (128*K)));
941
942 for (int i = old_c1_count; i < new_c1_count; i++) {
943 JavaThread *ct = make_thread(compiler_t, compiler1_object(i), _c1_compile_queue, _compilers[0], CHECK);
944 if (ct == NULL) break;
945 _compilers[0]->set_num_compiler_threads(i + 1);
946 if (TraceCompilerThreads) {
947 ResourceMark rm;
948 MutexLocker mu(Threads_lock);
949 tty->print_cr("Added compiler thread %s (available memory: %dMB, available profiled code cache: %dMB)",
950 ct->get_thread_name(), (int)(available_memory/M), (int)(available_cc_p/M));
951 }
952 }
953 }
954
955 CompileThread_lock->unlock();
956 }
957
958
959 /**
960 * Set the methods on the stack as on_stack so that redefine classes doesn't
961 * reclaim them. This method is executed at a safepoint.
962 */
963 void CompileBroker::mark_on_stack() {
|