@@ -1023,18 +1023,34 @@ void CompileBroker::init_compiler_threads() {
10231023
10241024void CompileBroker::possibly_add_compiler_threads (JavaThread* THREAD) {
10251025
1026+ int old_c2_count = 0 , new_c2_count = 0 , old_c1_count = 0 , new_c1_count = 0 ;
1027+ const int c2_tasks_per_thread = 2 , c1_tasks_per_thread = 4 ;
1028+
1029+ // Quick check if we already have enough compiler threads without taking the lock.
1030+ // Numbers may change concurrently, so we read them again after we have the lock.
1031+ if (_c2_compile_queue != nullptr ) {
1032+ old_c2_count = get_c2_thread_count ();
1033+ new_c2_count = MIN2 (_c2_count, _c2_compile_queue->size () / c2_tasks_per_thread);
1034+ }
1035+ if (_c1_compile_queue != nullptr ) {
1036+ old_c1_count = get_c1_thread_count ();
1037+ new_c1_count = MIN2 (_c1_count, _c1_compile_queue->size () / c1_tasks_per_thread);
1038+ }
1039+ if (new_c2_count <= old_c2_count && new_c1_count <= old_c1_count) return ;
1040+
1041+ // Now, we do the more expensive operations.
10261042 julong free_memory = os::free_memory ();
10271043 // If SegmentedCodeCache is off, both values refer to the single heap (with type CodeBlobType::All).
1028- size_t available_cc_np = CodeCache::unallocated_capacity (CodeBlobType::MethodNonProfiled),
1029- available_cc_p = CodeCache::unallocated_capacity (CodeBlobType::MethodProfiled);
1044+ size_t available_cc_np = CodeCache::unallocated_capacity (CodeBlobType::MethodNonProfiled),
1045+ available_cc_p = CodeCache::unallocated_capacity (CodeBlobType::MethodProfiled);
10301046
1031- // Only do attempt to start additional threads if the lock is free.
1047+ // Only attempt to start additional threads if the lock is free.
10321048 if (!CompileThread_lock->try_lock ()) return ;
10331049
10341050 if (_c2_compile_queue != nullptr ) {
1035- int old_c2_count = _compilers[ 1 ]-> num_compiler_threads ();
1036- int new_c2_count = MIN4 (_c2_count,
1037- _c2_compile_queue->size () / 2 ,
1051+ old_c2_count = get_c2_thread_count ();
1052+ new_c2_count = MIN4 (_c2_count,
1053+ _c2_compile_queue->size () / c2_tasks_per_thread ,
10381054 (int )(free_memory / (200 *M)),
10391055 (int )(available_cc_np / (128 *K)));
10401056
@@ -1070,7 +1086,7 @@ void CompileBroker::possibly_add_compiler_threads(JavaThread* THREAD) {
10701086 break ;
10711087 }
10721088 // Check if another thread has beaten us during the Java calls.
1073- if (_compilers[ 1 ]-> num_compiler_threads () != i) break ;
1089+ if (get_c2_thread_count () != i) break ;
10741090 jobject thread_handle = JNIHandles::make_global (thread_oop);
10751091 assert (compiler2_object (i) == nullptr , " Old one must be released!" );
10761092 _compiler2_objects[i] = thread_handle;
@@ -1093,9 +1109,9 @@ void CompileBroker::possibly_add_compiler_threads(JavaThread* THREAD) {
10931109 }
10941110
10951111 if (_c1_compile_queue != nullptr ) {
1096- int old_c1_count = _compilers[ 0 ]-> num_compiler_threads ();
1097- int new_c1_count = MIN4 (_c1_count,
1098- _c1_compile_queue->size () / 4 ,
1112+ old_c1_count = get_c1_thread_count ();
1113+ new_c1_count = MIN4 (_c1_count,
1114+ _c1_compile_queue->size () / c1_tasks_per_thread ,
10991115 (int )(free_memory / (100 *M)),
11001116 (int )(available_cc_p / (128 *K)));
11011117
0 commit comments