@@ -1069,18 +1069,34 @@ void CompileBroker::init_compiler_sweeper_threads() {
10691069
10701070void CompileBroker::possibly_add_compiler_threads (JavaThread* THREAD) {
10711071
1072+ int old_c2_count = 0 , new_c2_count = 0 , old_c1_count = 0 , new_c1_count = 0 ;
1073+ const int c2_tasks_per_thread = 2 , c1_tasks_per_thread = 4 ;
1074+
1075+ // Quick check if we already have enough compiler threads without taking the lock.
1076+ // Numbers may change concurrently, so we read them again after we have the lock.
1077+ if (_c2_compile_queue != nullptr ) {
1078+ old_c2_count = get_c2_thread_count ();
1079+ new_c2_count = MIN2 (_c2_count, _c2_compile_queue->size () / c2_tasks_per_thread);
1080+ }
1081+ if (_c1_compile_queue != nullptr ) {
1082+ old_c1_count = get_c1_thread_count ();
1083+ new_c1_count = MIN2 (_c1_count, _c1_compile_queue->size () / c1_tasks_per_thread);
1084+ }
1085+ if (new_c2_count <= old_c2_count && new_c1_count <= old_c1_count) return ;
1086+
1087+ // Now, we do the more expensive operations.
10721088 julong available_memory = os::available_memory ();
10731089 // If SegmentedCodeCache is off, both values refer to the single heap (with type CodeBlobType::All).
1074- size_t available_cc_np = CodeCache::unallocated_capacity (CodeBlobType::MethodNonProfiled),
1075- available_cc_p = CodeCache::unallocated_capacity (CodeBlobType::MethodProfiled);
1090+ size_t available_cc_np = CodeCache::unallocated_capacity (CodeBlobType::MethodNonProfiled),
1091+ available_cc_p = CodeCache::unallocated_capacity (CodeBlobType::MethodProfiled);
10761092
1077- // Only do attempt to start additional threads if the lock is free.
1093+ // Only attempt to start additional threads if the lock is free.
10781094 if (!CompileThread_lock->try_lock ()) return ;
10791095
10801096 if (_c2_compile_queue != NULL ) {
1081- int old_c2_count = _compilers[ 1 ]-> num_compiler_threads ();
1082- int new_c2_count = MIN4 (_c2_count,
1083- _c2_compile_queue->size () / 2 ,
1097+ old_c2_count = get_c2_thread_count ();
1098+ new_c2_count = MIN4 (_c2_count,
1099+ _c2_compile_queue->size () / c2_tasks_per_thread ,
10841100 (int )(available_memory / (200 *M)),
10851101 (int )(available_cc_np / (128 *K)));
10861102
@@ -1114,7 +1130,7 @@ void CompileBroker::possibly_add_compiler_threads(JavaThread* THREAD) {
11141130 break ;
11151131 }
11161132 // Check if another thread has beaten us during the Java calls.
1117- if (_compilers[ 1 ]-> num_compiler_threads () != i) break ;
1133+ if (get_c2_thread_count () != i) break ;
11181134 jobject thread_handle = JNIHandles::make_global (thread_oop);
11191135 assert (compiler2_object (i) == NULL , " Old one must be released!" );
11201136 _compiler2_objects[i] = thread_handle;
@@ -1136,9 +1152,9 @@ void CompileBroker::possibly_add_compiler_threads(JavaThread* THREAD) {
11361152 }
11371153
11381154 if (_c1_compile_queue != NULL ) {
1139- int old_c1_count = _compilers[ 0 ]-> num_compiler_threads ();
1140- int new_c1_count = MIN4 (_c1_count,
1141- _c1_compile_queue->size () / 4 ,
1155+ old_c1_count = get_c1_thread_count ();
1156+ new_c1_count = MIN4 (_c1_count,
1157+ _c1_compile_queue->size () / c1_tasks_per_thread ,
11421158 (int )(available_memory / (100 *M)),
11431159 (int )(available_cc_p / (128 *K)));
11441160
0 commit comments