duke@435: /* sla@5237: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_COMPILER_COMPILEBROKER_HPP stefank@2314: #define SHARE_VM_COMPILER_COMPILEBROKER_HPP stefank@2314: stefank@2314: #include "ci/compilerInterface.hpp" stefank@2314: #include "compiler/abstractCompiler.hpp" stefank@2314: #include "runtime/perfData.hpp" stefank@2314: duke@435: class nmethod; duke@435: class nmethodLocker; duke@435: duke@435: // CompileTask duke@435: // duke@435: // An entry in the compile queue. It represents a pending or current duke@435: // compilation. zgu@3900: class CompileTask : public CHeapObj { never@3138: friend class VMStructs; never@3138: duke@435: private: duke@435: Monitor* _lock; duke@435: uint _compile_id; coleenp@4037: Method* _method; coleenp@4304: jobject _method_holder; duke@435: int _osr_bci; duke@435: bool _is_complete; duke@435: bool _is_success; duke@435: bool _is_blocking; duke@435: int _comp_level; duke@435: int _num_inlined_bytecodes; duke@435: nmethodLocker* _code_handle; // holder of eventual result iveresov@2138: CompileTask* _next, *_prev; duke@435: duke@435: // Fields used for logging why the compilation was initiated: duke@435: jlong _time_queued; // in units of os::elapsed_counter() coleenp@4037: Method* _hot_method; // which method actually triggered this task coleenp@4304: jobject _hot_method_holder; duke@435: int _hot_count; // information about its invocation counter duke@435: const char* _comment; // more info about the task duke@435: duke@435: public: duke@435: CompileTask() { duke@435: _lock = new Monitor(Mutex::nonleaf+2, "CompileTaskLock"); duke@435: } duke@435: duke@435: void initialize(int compile_id, methodHandle method, int osr_bci, int comp_level, duke@435: methodHandle hot_method, int hot_count, const char* comment, duke@435: bool is_blocking); duke@435: duke@435: void free(); duke@435: duke@435: int compile_id() const { return _compile_id; } coleenp@4037: Method* method() const { return _method; } duke@435: int osr_bci() const { return _osr_bci; } duke@435: bool is_complete() const { return _is_complete; } duke@435: bool is_blocking() const { return _is_blocking; } duke@435: bool is_success() const { return _is_success; } duke@435: duke@435: nmethodLocker* code_handle() const { return _code_handle; } duke@435: void set_code_handle(nmethodLocker* l) { _code_handle = l; } duke@435: nmethod* code() const; // _code_handle->code() duke@435: void set_code(nmethod* nm); // _code_handle->set_code(nm) duke@435: duke@435: Monitor* lock() const { return _lock; } duke@435: duke@435: void mark_complete() { _is_complete = true; } duke@435: void mark_success() { _is_success = true; } duke@435: duke@435: int comp_level() { return _comp_level;} duke@435: void set_comp_level(int comp_level) { _comp_level = comp_level;} duke@435: duke@435: int num_inlined_bytecodes() const { return _num_inlined_bytecodes; } duke@435: void set_num_inlined_bytecodes(int n) { _num_inlined_bytecodes = n; } duke@435: duke@435: CompileTask* next() const { return _next; } duke@435: void set_next(CompileTask* next) { _next = next; } iveresov@2138: CompileTask* prev() const { return _prev; } iveresov@2138: void set_prev(CompileTask* prev) { _prev = prev; } duke@435: twisti@2687: private: coleenp@4037: static void print_compilation_impl(outputStream* st, Method* method, int compile_id, int comp_level, never@3499: bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false, never@3499: const char* msg = NULL, bool short_form = false); twisti@2687: twisti@2687: public: twisti@4111: void print_compilation(outputStream* st = tty, const char* msg = NULL, bool short_form = false); twisti@3969: static void print_compilation(outputStream* st, const nmethod* nm, const char* msg = NULL, bool short_form = false) { never@3499: print_compilation_impl(st, nm->method(), nm->compile_id(), nm->comp_level(), never@3499: nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false, twisti@3969: msg, short_form); twisti@2687: } twisti@2687: twisti@2687: static void print_inlining(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg = NULL); twisti@2687: static void print_inlining(ciMethod* method, int inline_level, int bci, const char* msg = NULL) { twisti@2687: print_inlining(tty, method, inline_level, bci, msg); twisti@2687: } twisti@2687: coleenp@4037: // Redefine Classes support coleenp@4037: void mark_on_stack(); coleenp@4037: twisti@2687: static void print_inline_indent(int inline_level, outputStream* st = tty); twisti@2687: duke@435: void print(); duke@435: void print_line(); twisti@2687: void print_line_on_error(outputStream* st, char* buf, int buflen); iveresov@2138: duke@435: void log_task(xmlStream* log); duke@435: void log_task_queued(); duke@435: void log_task_start(CompileLog* log); duke@435: void log_task_done(CompileLog* log); duke@435: }; duke@435: duke@435: // CompilerCounters duke@435: // duke@435: // Per Compiler Performance Counters. duke@435: // zgu@3900: class CompilerCounters : public CHeapObj { duke@435: duke@435: public: duke@435: enum { duke@435: cmname_buffer_length = 160 duke@435: }; duke@435: duke@435: private: duke@435: duke@435: char _current_method[cmname_buffer_length]; duke@435: PerfStringVariable* _perf_current_method; duke@435: duke@435: int _compile_type; duke@435: PerfVariable* _perf_compile_type; duke@435: duke@435: PerfCounter* _perf_time; duke@435: PerfCounter* _perf_compiles; duke@435: duke@435: public: duke@435: CompilerCounters(const char* name, int instance, TRAPS); duke@435: duke@435: // these methods should be called in a thread safe context duke@435: duke@435: void set_current_method(const char* method) { duke@435: strncpy(_current_method, method, (size_t)cmname_buffer_length); duke@435: if (UsePerfData) _perf_current_method->set_value(method); duke@435: } duke@435: duke@435: char* current_method() { return _current_method; } duke@435: duke@435: void set_compile_type(int compile_type) { duke@435: _compile_type = compile_type; duke@435: if (UsePerfData) _perf_compile_type->set_value((jlong)compile_type); duke@435: } duke@435: duke@435: int compile_type() { return _compile_type; } duke@435: duke@435: PerfCounter* time_counter() { return _perf_time; } duke@435: PerfCounter* compile_counter() { return _perf_compiles; } duke@435: }; duke@435: duke@435: // CompileQueue duke@435: // duke@435: // A list of CompileTasks. zgu@3900: class CompileQueue : public CHeapObj { duke@435: private: duke@435: const char* _name; duke@435: Monitor* _lock; duke@435: duke@435: CompileTask* _first; duke@435: CompileTask* _last; duke@435: iveresov@2138: int _size; duke@435: public: duke@435: CompileQueue(const char* name, Monitor* lock) { duke@435: _name = name; duke@435: _lock = lock; duke@435: _first = NULL; duke@435: _last = NULL; iveresov@2138: _size = 0; duke@435: } duke@435: duke@435: const char* name() const { return _name; } duke@435: Monitor* lock() const { return _lock; } duke@435: duke@435: void add(CompileTask* task); iveresov@2138: void remove(CompileTask* task); iveresov@2138: CompileTask* first() { return _first; } iveresov@2138: CompileTask* last() { return _last; } duke@435: duke@435: CompileTask* get(); duke@435: duke@435: bool is_empty() const { return _first == NULL; } iveresov@2138: int size() const { return _size; } duke@435: coleenp@4037: // Redefine Classes support coleenp@4037: void mark_on_stack(); anoll@5919: void delete_all(); anoll@5919: void print(); coleenp@4037: anoll@5919: ~CompileQueue() { anoll@5919: assert (is_empty(), " Compile Queue must be empty"); anoll@5919: } duke@435: }; duke@435: iveresov@2138: // CompileTaskWrapper iveresov@2138: // iveresov@2138: // Assign this task to the current thread. Deallocate the task iveresov@2138: // when the compilation is complete. iveresov@2138: class CompileTaskWrapper : StackObj { iveresov@2138: public: iveresov@2138: CompileTaskWrapper(CompileTask* task); iveresov@2138: ~CompileTaskWrapper(); iveresov@2138: }; iveresov@2138: duke@435: duke@435: // Compilation duke@435: // duke@435: // The broker for all compilation requests. duke@435: class CompileBroker: AllStatic { duke@435: friend class Threads; duke@435: friend class CompileTaskWrapper; duke@435: duke@435: public: duke@435: enum { duke@435: name_buffer_length = 100 duke@435: }; duke@435: duke@435: // Compile type Information for print_last_compile() and CompilerCounters duke@435: enum { no_compile, normal_compile, osr_compile, native_compile }; anoll@6220: static int assign_compile_id (methodHandle method, int osr_bci); anoll@6220: duke@435: duke@435: private: duke@435: static bool _initialized; duke@435: static volatile bool _should_block; duke@435: kvn@1637: // This flag can be used to stop compilation or turn it back on kvn@1637: static volatile jint _should_compile_new_jobs; kvn@1637: duke@435: // The installed compiler(s) duke@435: static AbstractCompiler* _compilers[2]; duke@435: duke@435: // These counters are used for assigning id's to each compilation anoll@6220: static volatile jint _compilation_id; anoll@6220: static volatile jint _osr_compilation_id; duke@435: duke@435: static int _last_compile_type; duke@435: static int _last_compile_level; duke@435: static char _last_method_compiled[name_buffer_length]; duke@435: iveresov@2138: static CompileQueue* _c2_method_queue; iveresov@2138: static CompileQueue* _c1_method_queue; duke@435: static CompileTask* _task_free_list; duke@435: anoll@5919: static GrowableArray* _compiler_threads; duke@435: duke@435: // performance counters duke@435: static PerfCounter* _perf_total_compilation; duke@435: static PerfCounter* _perf_native_compilation; duke@435: static PerfCounter* _perf_osr_compilation; duke@435: static PerfCounter* _perf_standard_compilation; duke@435: duke@435: static PerfCounter* _perf_total_bailout_count; duke@435: static PerfCounter* _perf_total_invalidated_count; duke@435: static PerfCounter* _perf_total_compile_count; duke@435: static PerfCounter* _perf_total_native_compile_count; duke@435: static PerfCounter* _perf_total_osr_compile_count; duke@435: static PerfCounter* _perf_total_standard_compile_count; duke@435: duke@435: static PerfCounter* _perf_sum_osr_bytes_compiled; duke@435: static PerfCounter* _perf_sum_standard_bytes_compiled; duke@435: static PerfCounter* _perf_sum_nmethod_size; duke@435: static PerfCounter* _perf_sum_nmethod_code_size; duke@435: duke@435: static PerfStringVariable* _perf_last_method; duke@435: static PerfStringVariable* _perf_last_failed_method; duke@435: static PerfStringVariable* _perf_last_invalidated_method; duke@435: static PerfVariable* _perf_last_compile_type; duke@435: static PerfVariable* _perf_last_compile_size; duke@435: static PerfVariable* _perf_last_failed_type; duke@435: static PerfVariable* _perf_last_invalidated_type; duke@435: duke@435: // Timers and counters for generating statistics duke@435: static elapsedTimer _t_total_compilation; duke@435: static elapsedTimer _t_osr_compilation; duke@435: static elapsedTimer _t_standard_compilation; duke@435: sla@5237: static int _total_compile_count; duke@435: static int _total_bailout_count; duke@435: static int _total_invalidated_count; duke@435: static int _total_native_compile_count; duke@435: static int _total_osr_compile_count; duke@435: static int _total_standard_compile_count; duke@435: static int _sum_osr_bytes_compiled; duke@435: static int _sum_standard_bytes_compiled; duke@435: static int _sum_nmethod_size; duke@435: static int _sum_nmethod_code_size; sla@5237: static long _peak_compilation_time; duke@435: anoll@6099: static volatile jint _print_compilation_warning; anoll@6099: anoll@5919: static CompilerThread* make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, AbstractCompiler* comp, TRAPS); iveresov@2138: static void init_compiler_threads(int c1_compiler_count, int c2_compiler_count); duke@435: static bool compilation_is_complete (methodHandle method, int osr_bci, int comp_level); duke@435: static bool compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level); duke@435: static bool is_compile_blocking (methodHandle method, int osr_bci); duke@435: static void preload_classes (methodHandle method, TRAPS); duke@435: duke@435: static CompileTask* create_compile_task(CompileQueue* queue, duke@435: int compile_id, duke@435: methodHandle method, duke@435: int osr_bci, duke@435: int comp_level, duke@435: methodHandle hot_method, duke@435: int hot_count, duke@435: const char* comment, duke@435: bool blocking); duke@435: static CompileTask* allocate_task(); duke@435: static void free_task(CompileTask* task); duke@435: static void wait_for_completion(CompileTask* task); duke@435: duke@435: static void invoke_compiler_on_method(CompileTask* task); duke@435: static void set_last_compile(CompilerThread *thread, methodHandle method, bool is_osr, int comp_level); duke@435: static void push_jni_handle_block(); duke@435: static void pop_jni_handle_block(); duke@435: static bool check_break_at(methodHandle method, int compile_id, bool is_osr); duke@435: static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task); duke@435: duke@435: static void compile_method_base(methodHandle method, duke@435: int osr_bci, duke@435: int comp_level, duke@435: methodHandle hot_method, duke@435: int hot_count, duke@435: const char* comment, iveresov@3452: Thread* thread); iveresov@2138: static CompileQueue* compile_queue(int comp_level) { iveresov@2138: if (is_c2_compile(comp_level)) return _c2_method_queue; iveresov@2138: if (is_c1_compile(comp_level)) return _c1_method_queue; iveresov@2138: return NULL; iveresov@2138: } anoll@5919: static bool init_compiler_runtime(); anoll@5919: static void shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread); anoll@5919: duke@435: public: duke@435: enum { duke@435: // The entry bci used for non-OSR compilations. duke@435: standard_entry_bci = InvocationEntryBci duke@435: }; duke@435: iveresov@2138: static AbstractCompiler* compiler(int comp_level) { iveresov@2138: if (is_c2_compile(comp_level)) return _compilers[1]; // C2 iveresov@2138: if (is_c1_compile(comp_level)) return _compilers[0]; // C1 iveresov@2138: return NULL; duke@435: } duke@435: iveresov@2138: static bool compilation_is_in_queue(methodHandle method, int osr_bci); iveresov@2138: static int queue_size(int comp_level) { iveresov@2138: CompileQueue *q = compile_queue(comp_level); iveresov@2138: return q != NULL ? q->size() : 0; iveresov@2138: } duke@435: static void compilation_init(); duke@435: static void init_compiler_thread_log(); iveresov@2138: static nmethod* compile_method(methodHandle method, iveresov@2138: int osr_bci, iveresov@2138: int comp_level, iveresov@2138: methodHandle hot_method, iveresov@2138: int hot_count, iveresov@3452: const char* comment, Thread* thread); duke@435: duke@435: static void compiler_thread_loop(); kvn@1637: static uint get_compilation_id() { return _compilation_id; } duke@435: duke@435: // Set _should_block. duke@435: // Call this from the VM, with Threads_lock held and a safepoint requested. duke@435: static void set_should_block(); duke@435: duke@435: // Call this from the compiler at convenient points, to poll for _should_block. duke@435: static void maybe_block(); duke@435: kvn@1637: enum { kvn@1637: // Flags for toggling compiler activity anoll@5919: stop_compilation = 0, anoll@5919: run_compilation = 1, anoll@5919: shutdown_compilaton = 2 kvn@1637: }; kvn@1637: kvn@1637: static bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); } kvn@1637: static bool set_should_compile_new_jobs(jint new_state) { kvn@1637: // Return success if the current caller set it kvn@1637: jint old = Atomic::cmpxchg(new_state, &_should_compile_new_jobs, 1-new_state); kvn@1637: return (old == (1-new_state)); kvn@1637: } anoll@5919: anoll@5919: static void disable_compilation_forever() { anoll@5919: UseCompiler = false; anoll@5919: AlwaysCompileLoopMethods = false; anoll@5919: Atomic::xchg(shutdown_compilaton, &_should_compile_new_jobs); anoll@5919: } anoll@5919: anoll@5919: static bool is_compilation_disabled_forever() { anoll@5919: return _should_compile_new_jobs == shutdown_compilaton; anoll@5919: } kvn@1637: static void handle_full_code_cache(); anoll@6099: // Ensures that warning is only printed once. anoll@6099: static bool should_print_compiler_warning() { anoll@6099: jint old = Atomic::cmpxchg(1, &_print_compilation_warning, 0); anoll@6099: return old == 0; anoll@6099: } duke@435: // Return total compilation ticks duke@435: static jlong total_compilation_ticks() { duke@435: return _perf_total_compilation != NULL ? _perf_total_compilation->get_value() : 0; duke@435: } duke@435: coleenp@4037: // Redefine Classes support coleenp@4037: static void mark_on_stack(); coleenp@4037: duke@435: // Print a detailed accounting of compilation time duke@435: static void print_times(); duke@435: duke@435: // Debugging output for failure duke@435: static void print_last_compile(); duke@435: duke@435: static void print_compiler_threads_on(outputStream* st); morris@4771: morris@4771: // compiler name for debugging morris@4771: static const char* compiler_name(int comp_level); sla@5237: sla@5237: static int get_total_compile_count() { return _total_compile_count; } sla@5237: static int get_total_bailout_count() { return _total_bailout_count; } sla@5237: static int get_total_invalidated_count() { return _total_invalidated_count; } sla@5237: static int get_total_native_compile_count() { return _total_native_compile_count; } sla@5237: static int get_total_osr_compile_count() { return _total_osr_compile_count; } sla@5237: static int get_total_standard_compile_count() { return _total_standard_compile_count; } sla@5237: static int get_sum_osr_bytes_compiled() { return _sum_osr_bytes_compiled; } sla@5237: static int get_sum_standard_bytes_compiled() { return _sum_standard_bytes_compiled; } sla@5237: static int get_sum_nmethod_size() { return _sum_nmethod_size;} sla@5237: static int get_sum_nmethod_code_size() { return _sum_nmethod_code_size; } sla@5237: static long get_peak_compilation_time() { return _peak_compilation_time; } sla@5237: static long get_total_compilation_time() { return _t_total_compilation.milliseconds(); } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_COMPILER_COMPILEBROKER_HPP