Skip to content

Commit a320a4b

Browse files
committed
Backport 3087c6c74d742b7b5eaf28e4c886b5dc1811ea6f
1 parent 131a032 commit a320a4b

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/hotspot/share/compiler/compileBroker.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,18 +1069,34 @@ void CompileBroker::init_compiler_sweeper_threads() {
10691069

10701070
void 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

src/hotspot/share/compiler/compileBroker.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class CompileQueue : public CHeapObj<mtCompiler> {
8888

8989
CompileTask* _first_stale;
9090

91-
int _size;
91+
volatile int _size;
9292

9393
void purge_stale_tasks();
9494
public:
@@ -402,6 +402,8 @@ class CompileBroker: AllStatic {
402402

403403
static CompileLog* get_log(CompilerThread* ct);
404404

405+
static int get_c1_thread_count() { return _compilers[0]->num_compiler_threads(); }
406+
static int get_c2_thread_count() { return _compilers[1]->num_compiler_threads(); }
405407
static int get_total_compile_count() { return _total_compile_count; }
406408
static int get_total_bailout_count() { return _total_bailout_count; }
407409
static int get_total_invalidated_count() { return _total_invalidated_count; }

0 commit comments

Comments
 (0)