Skip to content

Commit 3087c6c

Browse files
committed
8343205: CompileBroker::possibly_add_compiler_threads excessively polls available memory
Reviewed-by: phh Backport-of: 75801992a7c626d409f66e2491082dba84c6fe45
1 parent c1bd8d9 commit 3087c6c

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
@@ -1017,18 +1017,34 @@ void CompileBroker::init_compiler_threads() {
10171017

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

src/hotspot/share/compiler/compileBroker.hpp

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

8888
CompileTask* _first_stale;
8989

90-
int _size;
90+
volatile int _size;
9191

9292
void purge_stale_tasks();
9393
public:
@@ -399,6 +399,8 @@ class CompileBroker: AllStatic {
399399

400400
static CompileLog* get_log(CompilerThread* ct);
401401

402+
static int get_c1_thread_count() { return _compilers[0]->num_compiler_threads(); }
403+
static int get_c2_thread_count() { return _compilers[1]->num_compiler_threads(); }
402404
static int get_total_compile_count() { return _total_compile_count; }
403405
static int get_total_bailout_count() { return _total_bailout_count; }
404406
static int get_total_invalidated_count() { return _total_invalidated_count; }

0 commit comments

Comments
 (0)