Path: blob/master/src/hotspot/share/compiler/compileBroker.hpp
40930 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_COMPILER_COMPILEBROKER_HPP25#define SHARE_COMPILER_COMPILEBROKER_HPP2627#include "ci/compilerInterface.hpp"28#include "compiler/abstractCompiler.hpp"29#include "compiler/compileTask.hpp"30#include "compiler/compilerDirectives.hpp"31#include "compiler/compilerThread.hpp"32#include "runtime/atomic.hpp"33#include "runtime/perfDataTypes.hpp"34#include "utilities/stack.hpp"35#if INCLUDE_JVMCI36#include "jvmci/jvmciCompiler.hpp"37#endif3839class nmethod;40class nmethodLocker;4142// CompilerCounters43//44// Per Compiler Performance Counters.45//46class CompilerCounters : public CHeapObj<mtCompiler> {4748public:49enum {50cmname_buffer_length = 16051};5253private:5455char _current_method[cmname_buffer_length];56int _compile_type;5758public:59CompilerCounters();6061// these methods should be called in a thread safe context6263void set_current_method(const char* method) {64strncpy(_current_method, method, (size_t)cmname_buffer_length-1);65_current_method[cmname_buffer_length-1] = '\0';66}6768char* current_method() { return _current_method; }6970void set_compile_type(int compile_type) {71_compile_type = compile_type;72}7374int compile_type() { return _compile_type; }7576};7778// CompileQueue79//80// A list of CompileTasks.81class CompileQueue : public CHeapObj<mtCompiler> {82private:83const char* _name;8485CompileTask* _first;86CompileTask* _last;8788CompileTask* _first_stale;8990int _size;9192void purge_stale_tasks();93public:94CompileQueue(const char* name) {95_name = name;96_first = NULL;97_last = NULL;98_size = 0;99_first_stale = NULL;100}101102const char* name() const { return _name; }103104void add(CompileTask* task);105void remove(CompileTask* task);106void remove_and_mark_stale(CompileTask* task);107CompileTask* first() { return _first; }108CompileTask* last() { return _last; }109110CompileTask* get();111112bool is_empty() const { return _first == NULL; }113int size() const { return _size; }114115116// Redefine Classes support117void mark_on_stack();118void free_all();119void print_tty();120void print(outputStream* st = tty);121122~CompileQueue() {123assert (is_empty(), " Compile Queue must be empty");124}125};126127// CompileTaskWrapper128//129// Assign this task to the current thread. Deallocate the task130// when the compilation is complete.131class CompileTaskWrapper : StackObj {132public:133CompileTaskWrapper(CompileTask* task);134~CompileTaskWrapper();135};136137// Compilation138//139// The broker for all compilation requests.140class CompileBroker: AllStatic {141friend class Threads;142friend class CompileTaskWrapper;143144public:145enum {146name_buffer_length = 100147};148149// Compile type Information for print_last_compile() and CompilerCounters150enum { no_compile, normal_compile, osr_compile, native_compile };151static int assign_compile_id (const methodHandle& method, int osr_bci);152153154private:155static bool _initialized;156static volatile bool _should_block;157158// This flag can be used to stop compilation or turn it back on159static volatile jint _should_compile_new_jobs;160161// The installed compiler(s)162static AbstractCompiler* _compilers[2];163164// The maximum numbers of compiler threads to be determined during startup.165static int _c1_count, _c2_count;166167// An array of compiler thread Java objects168static jobject *_compiler1_objects, *_compiler2_objects;169170// An array of compiler logs171static CompileLog **_compiler1_logs, **_compiler2_logs;172173// These counters are used for assigning id's to each compilation174static volatile jint _compilation_id;175static volatile jint _osr_compilation_id;176177static CompileQueue* _c2_compile_queue;178static CompileQueue* _c1_compile_queue;179180// performance counters181static PerfCounter* _perf_total_compilation;182static PerfCounter* _perf_native_compilation;183static PerfCounter* _perf_osr_compilation;184static PerfCounter* _perf_standard_compilation;185186static PerfCounter* _perf_total_bailout_count;187static PerfCounter* _perf_total_invalidated_count;188static PerfCounter* _perf_total_compile_count;189static PerfCounter* _perf_total_native_compile_count;190static PerfCounter* _perf_total_osr_compile_count;191static PerfCounter* _perf_total_standard_compile_count;192193static PerfCounter* _perf_sum_osr_bytes_compiled;194static PerfCounter* _perf_sum_standard_bytes_compiled;195static PerfCounter* _perf_sum_nmethod_size;196static PerfCounter* _perf_sum_nmethod_code_size;197198static PerfStringVariable* _perf_last_method;199static PerfStringVariable* _perf_last_failed_method;200static PerfStringVariable* _perf_last_invalidated_method;201static PerfVariable* _perf_last_compile_type;202static PerfVariable* _perf_last_compile_size;203static PerfVariable* _perf_last_failed_type;204static PerfVariable* _perf_last_invalidated_type;205206// Timers and counters for generating statistics207static elapsedTimer _t_total_compilation;208static elapsedTimer _t_osr_compilation;209static elapsedTimer _t_standard_compilation;210static elapsedTimer _t_invalidated_compilation;211static elapsedTimer _t_bailedout_compilation;212213static int _total_compile_count;214static int _total_bailout_count;215static int _total_invalidated_count;216static int _total_native_compile_count;217static int _total_osr_compile_count;218static int _total_standard_compile_count;219static int _total_compiler_stopped_count;220static int _total_compiler_restarted_count;221static int _sum_osr_bytes_compiled;222static int _sum_standard_bytes_compiled;223static int _sum_nmethod_size;224static int _sum_nmethod_code_size;225static long _peak_compilation_time;226227static CompilerStatistics _stats_per_level[];228229static volatile int _print_compilation_warning;230231enum ThreadType {232compiler_t,233sweeper_t,234deoptimizer_t235};236237static Handle create_thread_oop(const char* name, TRAPS);238static JavaThread* make_thread(ThreadType type, jobject thread_oop, CompileQueue* queue, AbstractCompiler* comp, JavaThread* THREAD);239static void init_compiler_sweeper_threads();240static void possibly_add_compiler_threads(JavaThread* THREAD);241static bool compilation_is_prohibited(const methodHandle& method, int osr_bci, int comp_level, bool excluded);242243static CompileTask* create_compile_task(CompileQueue* queue,244int compile_id,245const methodHandle& method,246int osr_bci,247int comp_level,248const methodHandle& hot_method,249int hot_count,250CompileTask::CompileReason compile_reason,251bool blocking);252static void wait_for_completion(CompileTask* task);253#if INCLUDE_JVMCI254static bool wait_for_jvmci_completion(JVMCICompiler* comp, CompileTask* task, JavaThread* thread);255#endif256257static void invoke_compiler_on_method(CompileTask* task);258static void post_compile(CompilerThread* thread, CompileTask* task, bool success, ciEnv* ci_env,259int compilable, const char* failure_reason);260static void update_compile_perf_data(CompilerThread *thread, const methodHandle& method, bool is_osr);261262static void push_jni_handle_block();263static void pop_jni_handle_block();264static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);265266static void compile_method_base(const methodHandle& method,267int osr_bci,268int comp_level,269const methodHandle& hot_method,270int hot_count,271CompileTask::CompileReason compile_reason,272bool blocking,273Thread* thread);274275static CompileQueue* compile_queue(int comp_level);276static bool init_compiler_runtime();277static void shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread);278279public:280enum {281// The entry bci used for non-OSR compilations.282standard_entry_bci = InvocationEntryBci283};284285static AbstractCompiler* compiler(int comp_level) {286if (is_c2_compile(comp_level)) return _compilers[1]; // C2287if (is_c1_compile(comp_level)) return _compilers[0]; // C1288return NULL;289}290291static bool compilation_is_complete(const methodHandle& method, int osr_bci, int comp_level);292static bool compilation_is_in_queue(const methodHandle& method);293static void print_compile_queues(outputStream* st);294static int queue_size(int comp_level) {295CompileQueue *q = compile_queue(comp_level);296return q != NULL ? q->size() : 0;297}298static void compilation_init_phase1(JavaThread* THREAD);299static void compilation_init_phase2();300static void init_compiler_thread_log();301static nmethod* compile_method(const methodHandle& method,302int osr_bci,303int comp_level,304const methodHandle& hot_method,305int hot_count,306CompileTask::CompileReason compile_reason,307TRAPS);308309static nmethod* compile_method(const methodHandle& method,310int osr_bci,311int comp_level,312const methodHandle& hot_method,313int hot_count,314CompileTask::CompileReason compile_reason,315DirectiveSet* directive,316TRAPS);317318// Acquire any needed locks and assign a compile id319static uint assign_compile_id_unlocked(Thread* thread, const methodHandle& method, int osr_bci);320321static void compiler_thread_loop();322static uint get_compilation_id() { return _compilation_id; }323324// Set _should_block.325// Call this from the VM, with Threads_lock held and a safepoint requested.326static void set_should_block();327328// Call this from the compiler at convenient points, to poll for _should_block.329static void maybe_block();330331enum CompilerActivity {332// Flags for toggling compiler activity333stop_compilation = 0,334run_compilation = 1,335shutdown_compilation = 2336};337338static jint get_compilation_activity_mode() { return _should_compile_new_jobs; }339static bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); }340static bool set_should_compile_new_jobs(jint new_state) {341// Return success if the current caller set it342jint old = Atomic::cmpxchg(&_should_compile_new_jobs, 1-new_state, new_state);343bool success = (old == (1-new_state));344if (success) {345if (new_state == run_compilation) {346_total_compiler_restarted_count++;347} else {348_total_compiler_stopped_count++;349}350}351return success;352}353354static void disable_compilation_forever() {355UseCompiler = false;356AlwaysCompileLoopMethods = false;357Atomic::xchg(&_should_compile_new_jobs, jint(shutdown_compilation));358}359360static bool is_compilation_disabled_forever() {361return _should_compile_new_jobs == shutdown_compilation;362}363static void handle_full_code_cache(int code_blob_type);364// Ensures that warning is only printed once.365static bool should_print_compiler_warning() {366jint old = Atomic::cmpxchg(&_print_compilation_warning, 0, 1);367return old == 0;368}369// Return total compilation ticks370static jlong total_compilation_ticks();371372// Redefine Classes support373static void mark_on_stack();374375// Print curent compilation time stats for a given compiler376static void print_times(const char* name, CompilerStatistics* stats);377378// Print a detailed accounting of compilation time379static void print_times(bool per_compiler = true, bool aggregate = true);380381// compiler name for debugging382static const char* compiler_name(int comp_level);383384// Provide access to compiler thread Java objects385static jobject compiler1_object(int idx) {386assert(_compiler1_objects != NULL, "must be initialized");387assert(idx < _c1_count, "oob");388return _compiler1_objects[idx];389}390391static jobject compiler2_object(int idx) {392assert(_compiler2_objects != NULL, "must be initialized");393assert(idx < _c2_count, "oob");394return _compiler2_objects[idx];395}396397static AbstractCompiler* compiler1() { return _compilers[0]; }398static AbstractCompiler* compiler2() { return _compilers[1]; }399400static bool can_remove(CompilerThread *ct, bool do_it);401402static CompileLog* get_log(CompilerThread* ct);403404static int get_total_compile_count() { return _total_compile_count; }405static int get_total_bailout_count() { return _total_bailout_count; }406static int get_total_invalidated_count() { return _total_invalidated_count; }407static int get_total_native_compile_count() { return _total_native_compile_count; }408static int get_total_osr_compile_count() { return _total_osr_compile_count; }409static int get_total_standard_compile_count() { return _total_standard_compile_count; }410static int get_total_compiler_stopped_count() { return _total_compiler_stopped_count; }411static int get_total_compiler_restarted_count() { return _total_compiler_restarted_count; }412static int get_sum_osr_bytes_compiled() { return _sum_osr_bytes_compiled; }413static int get_sum_standard_bytes_compiled() { return _sum_standard_bytes_compiled; }414static int get_sum_nmethod_size() { return _sum_nmethod_size;}415static int get_sum_nmethod_code_size() { return _sum_nmethod_code_size; }416static long get_peak_compilation_time() { return _peak_compilation_time; }417static long get_total_compilation_time() { return _t_total_compilation.milliseconds(); }418419// Log that compilation profiling is skipped because metaspace is full.420static void log_metaspace_failure();421422// CodeHeap State Analytics.423static void print_info(outputStream *out);424static void print_heapinfo(outputStream *out, const char* function, size_t granularity);425};426427#endif // SHARE_COMPILER_COMPILEBROKER_HPP428429430