From 13cfc1a2343e4adafae12614eb39721c36f91266 Mon Sep 17 00:00:00 2001 From: Boris Ulasevich Date: Wed, 24 Apr 2024 06:23:55 +0000 Subject: [PATCH] release C1 scratch buffer after compilation --- src/hotspot/share/c1/c1_Compiler.cpp | 31 ++++++++++++++++---- src/hotspot/share/c1/c1_Compiler.hpp | 1 + src/hotspot/share/compiler/compileBroker.cpp | 6 ---- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/hotspot/share/c1/c1_Compiler.cpp b/src/hotspot/share/c1/c1_Compiler.cpp index bdbeb39f89a4a..fae904c515729 100644 --- a/src/hotspot/share/c1/c1_Compiler.cpp +++ b/src/hotspot/share/c1/c1_Compiler.cpp @@ -62,10 +62,9 @@ void Compiler::init_c1_runtime() { void Compiler::initialize() { - // Buffer blob must be allocated per C1 compiler thread at startup - BufferBlob* buffer_blob = init_buffer_blob(); - + // MonitorLocker only_one(CompileThread_lock); if (should_perform_init()) { + BufferBlob* buffer_blob = init_buffer_blob(); if (buffer_blob == nullptr) { // When we come here we are in state 'initializing'; entire C1 compilation // can be shut down. @@ -74,6 +73,8 @@ void Compiler::initialize() { init_c1_runtime(); set_state(initialized); } + tty->print_cr("## Compiler::initialize -> c1_init"); + release_buffer_blob(); } } @@ -82,8 +83,6 @@ uint Compiler::code_buffer_size() { } BufferBlob* Compiler::init_buffer_blob() { - // Allocate buffer blob once at startup since allocation for each - // compilation seems to be too expensive (at least on Intel win32). assert (CompilerThread::current()->get_buffer_blob() == nullptr, "Should initialize only once"); // setup CodeBuffer. Preallocate a BufferBlob of size @@ -96,6 +95,18 @@ BufferBlob* Compiler::init_buffer_blob() { return buffer_blob; } +void Compiler::release_buffer_blob() { + BufferBlob* buffer_blob = CompilerThread::current()->get_buffer_blob(); + if (buffer_blob != nullptr) { + CompilerThread::current()->set_buffer_blob(nullptr); + // Note. + // free_buffer_blob_if_allocated does the following. Why? + // blob->purge(true /* free_code_cache_data */, true /* unregister_nmethod */); + // MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); + BufferBlob::free(buffer_blob); + } +} + bool Compiler::is_intrinsic_supported(const methodHandle& method) { vmIntrinsics::ID id = method->intrinsic_id(); assert(id != vmIntrinsics::_none, "must be a VM intrinsic"); @@ -247,7 +258,13 @@ bool Compiler::is_intrinsic_supported(vmIntrinsics::ID id) { void Compiler::compile_method(ciEnv* env, ciMethod* method, int entry_bci, bool install_code, DirectiveSet* directive) { BufferBlob* buffer_blob = CompilerThread::current()->get_buffer_blob(); - assert(buffer_blob != nullptr, "Must exist"); + if (buffer_blob == nullptr) { buffer_blob = init_buffer_blob(); } + + if (buffer_blob == nullptr) { + warning("no space to run C1 compiler"); + return; + } + // invoke compilation { // We are nested here because we need for the destructor @@ -256,6 +273,8 @@ void Compiler::compile_method(ciEnv* env, ciMethod* method, int entry_bci, bool ResourceMark rm; Compilation c(this, env, method, entry_bci, buffer_blob, install_code, directive); } + + release_buffer_blob(); } diff --git a/src/hotspot/share/c1/c1_Compiler.hpp b/src/hotspot/share/c1/c1_Compiler.hpp index 0160ee6357471..c5d77571f28c4 100644 --- a/src/hotspot/share/c1/c1_Compiler.hpp +++ b/src/hotspot/share/c1/c1_Compiler.hpp @@ -35,6 +35,7 @@ class Compiler: public AbstractCompiler { private: static void init_c1_runtime(); BufferBlob* init_buffer_blob(); + void release_buffer_blob(); public: // Creation diff --git a/src/hotspot/share/compiler/compileBroker.cpp b/src/hotspot/share/compiler/compileBroker.cpp index 7adb4dfc587e1..b29453a4d75ef 100644 --- a/src/hotspot/share/compiler/compileBroker.cpp +++ b/src/hotspot/share/compiler/compileBroker.cpp @@ -1787,12 +1787,6 @@ bool CompileBroker::init_compiler_runtime() { return false; } - // C1 specific check - if (comp->is_c1() && (thread->get_buffer_blob() == nullptr)) { - warning("Initialization of %s thread failed (no space to run compilers)", thread->name()); - return false; - } - return true; }