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