@@ -1017,18 +1017,34 @@ void CompileBroker::init_compiler_threads() {
10171017
10181018void CompileBroker::possibly_add_compiler_threads (JavaThread* THREAD) {
10191019
1020+ int old_c2_count = 0 , new_c2_count = 0 , old_c1_count = 0 , new_c1_count = 0 ;
1021+ const int c2_tasks_per_thread = 2 , c1_tasks_per_thread = 4 ;
1022+
1023+ // Quick check if we already have enough compiler threads without taking the lock.
1024+ // Numbers may change concurrently, so we read them again after we have the lock.
1025+ if (_c2_compile_queue != nullptr ) {
1026+ old_c2_count = get_c2_thread_count ();
1027+ new_c2_count = MIN2 (_c2_count, _c2_compile_queue->size () / c2_tasks_per_thread);
1028+ }
1029+ if (_c1_compile_queue != nullptr ) {
1030+ old_c1_count = get_c1_thread_count ();
1031+ new_c1_count = MIN2 (_c1_count, _c1_compile_queue->size () / c1_tasks_per_thread);
1032+ }
1033+ if (new_c2_count <= old_c2_count && new_c1_count <= old_c1_count) return ;
1034+
1035+ // Now, we do the more expensive operations.
10201036 julong free_memory = os::free_memory ();
10211037 // If SegmentedCodeCache is off, both values refer to the single heap (with type CodeBlobType::All).
1022- size_t available_cc_np = CodeCache::unallocated_capacity (CodeBlobType::MethodNonProfiled),
1023- available_cc_p = CodeCache::unallocated_capacity (CodeBlobType::MethodProfiled);
1038+ size_t available_cc_np = CodeCache::unallocated_capacity (CodeBlobType::MethodNonProfiled),
1039+ available_cc_p = CodeCache::unallocated_capacity (CodeBlobType::MethodProfiled);
10241040
1025- // Only do attempt to start additional threads if the lock is free.
1041+ // Only attempt to start additional threads if the lock is free.
10261042 if (!CompileThread_lock->try_lock ()) return ;
10271043
10281044 if (_c2_compile_queue != nullptr ) {
1029- int old_c2_count = _compilers[ 1 ]-> num_compiler_threads ();
1030- int new_c2_count = MIN4 (_c2_count,
1031- _c2_compile_queue->size () / 2 ,
1045+ old_c2_count = get_c2_thread_count ();
1046+ new_c2_count = MIN4 (_c2_count,
1047+ _c2_compile_queue->size () / c2_tasks_per_thread ,
10321048 (int )(free_memory / (200 *M)),
10331049 (int )(available_cc_np / (128 *K)));
10341050
@@ -1062,7 +1078,7 @@ void CompileBroker::possibly_add_compiler_threads(JavaThread* THREAD) {
10621078 break ;
10631079 }
10641080 // Check if another thread has beaten us during the Java calls.
1065- if (_compilers[ 1 ]-> num_compiler_threads () != i) break ;
1081+ if (get_c2_thread_count () != i) break ;
10661082 jobject thread_handle = JNIHandles::make_global (thread_oop);
10671083 assert (compiler2_object (i) == nullptr , " Old one must be released!" );
10681084 _compiler2_objects[i] = thread_handle;
@@ -1084,9 +1100,9 @@ void CompileBroker::possibly_add_compiler_threads(JavaThread* THREAD) {
10841100 }
10851101
10861102 if (_c1_compile_queue != nullptr ) {
1087- int old_c1_count = _compilers[ 0 ]-> num_compiler_threads ();
1088- int new_c1_count = MIN4 (_c1_count,
1089- _c1_compile_queue->size () / 4 ,
1103+ old_c1_count = get_c1_thread_count ();
1104+ new_c1_count = MIN4 (_c1_count,
1105+ _c1_compile_queue->size () / c1_tasks_per_thread ,
10901106 (int )(free_memory / (100 *M)),
10911107 (int )(available_cc_p / (128 *K)));
10921108
0 commit comments