aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "classfile/vmSymbols.hpp" aoqi@0: #include "code/codeCache.hpp" aoqi@0: #include "compiler/compileBroker.hpp" aoqi@0: #include "compiler/compileLog.hpp" aoqi@0: #include "compiler/compilerOracle.hpp" aoqi@0: #include "interpreter/linkResolver.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "oops/methodData.hpp" aoqi@0: #include "oops/method.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "prims/nativeLookup.hpp" aoqi@0: #include "runtime/arguments.hpp" aoqi@0: #include "runtime/compilationPolicy.hpp" aoqi@0: #include "runtime/init.hpp" aoqi@0: #include "runtime/interfaceSupport.hpp" aoqi@0: #include "runtime/javaCalls.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "runtime/sharedRuntime.hpp" aoqi@0: #include "runtime/sweeper.hpp" aoqi@0: #include "trace/tracing.hpp" aoqi@0: #include "utilities/dtrace.hpp" aoqi@0: #include "utilities/events.hpp" aoqi@0: #ifdef COMPILER1 aoqi@0: #include "c1/c1_Compiler.hpp" aoqi@0: #endif aoqi@0: #ifdef COMPILER2 aoqi@0: #include "opto/c2compiler.hpp" aoqi@0: #endif aoqi@0: #ifdef SHARK aoqi@0: #include "shark/sharkCompiler.hpp" aoqi@0: #endif aoqi@0: aoqi@0: #ifdef DTRACE_ENABLED aoqi@0: aoqi@0: // Only bother with this argument setup if dtrace is available aoqi@0: aoqi@0: #ifndef USDT2 aoqi@0: HS_DTRACE_PROBE_DECL8(hotspot, method__compile__begin, aoqi@0: char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t); aoqi@0: HS_DTRACE_PROBE_DECL9(hotspot, method__compile__end, aoqi@0: char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t, bool); aoqi@0: aoqi@0: #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name) \ aoqi@0: { \ aoqi@0: Symbol* klass_name = (method)->klass_name(); \ aoqi@0: Symbol* name = (method)->name(); \ aoqi@0: Symbol* signature = (method)->signature(); \ aoqi@0: HS_DTRACE_PROBE8(hotspot, method__compile__begin, \ aoqi@0: comp_name, strlen(comp_name), \ aoqi@0: klass_name->bytes(), klass_name->utf8_length(), \ aoqi@0: name->bytes(), name->utf8_length(), \ aoqi@0: signature->bytes(), signature->utf8_length()); \ aoqi@0: } aoqi@0: aoqi@0: #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success) \ aoqi@0: { \ aoqi@0: Symbol* klass_name = (method)->klass_name(); \ aoqi@0: Symbol* name = (method)->name(); \ aoqi@0: Symbol* signature = (method)->signature(); \ aoqi@0: HS_DTRACE_PROBE9(hotspot, method__compile__end, \ aoqi@0: comp_name, strlen(comp_name), \ aoqi@0: klass_name->bytes(), klass_name->utf8_length(), \ aoqi@0: name->bytes(), name->utf8_length(), \ aoqi@0: signature->bytes(), signature->utf8_length(), (success)); \ aoqi@0: } aoqi@0: aoqi@0: #else /* USDT2 */ aoqi@0: aoqi@0: #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name) \ aoqi@0: { \ aoqi@0: Symbol* klass_name = (method)->klass_name(); \ aoqi@0: Symbol* name = (method)->name(); \ aoqi@0: Symbol* signature = (method)->signature(); \ aoqi@0: HOTSPOT_METHOD_COMPILE_BEGIN( \ aoqi@0: comp_name, strlen(comp_name), \ aoqi@0: (char *) klass_name->bytes(), klass_name->utf8_length(), \ aoqi@0: (char *) name->bytes(), name->utf8_length(), \ aoqi@0: (char *) signature->bytes(), signature->utf8_length()); \ aoqi@0: } aoqi@0: aoqi@0: #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success) \ aoqi@0: { \ aoqi@0: Symbol* klass_name = (method)->klass_name(); \ aoqi@0: Symbol* name = (method)->name(); \ aoqi@0: Symbol* signature = (method)->signature(); \ aoqi@0: HOTSPOT_METHOD_COMPILE_END( \ aoqi@0: comp_name, strlen(comp_name), \ aoqi@0: (char *) klass_name->bytes(), klass_name->utf8_length(), \ aoqi@0: (char *) name->bytes(), name->utf8_length(), \ aoqi@0: (char *) signature->bytes(), signature->utf8_length(), (success)); \ aoqi@0: } aoqi@0: #endif /* USDT2 */ aoqi@0: aoqi@0: #else // ndef DTRACE_ENABLED aoqi@0: aoqi@0: #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name) aoqi@0: #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success) aoqi@0: aoqi@0: #endif // ndef DTRACE_ENABLED aoqi@0: aoqi@0: bool CompileBroker::_initialized = false; aoqi@0: volatile bool CompileBroker::_should_block = false; aoqi@0: volatile jint CompileBroker::_print_compilation_warning = 0; aoqi@0: volatile jint CompileBroker::_should_compile_new_jobs = run_compilation; aoqi@0: aoqi@0: // The installed compiler(s) aoqi@0: AbstractCompiler* CompileBroker::_compilers[2]; aoqi@0: aoqi@0: // These counters are used to assign an unique ID to each compilation. aoqi@0: volatile jint CompileBroker::_compilation_id = 0; aoqi@0: volatile jint CompileBroker::_osr_compilation_id = 0; aoqi@0: aoqi@0: // Debugging information aoqi@0: int CompileBroker::_last_compile_type = no_compile; aoqi@0: int CompileBroker::_last_compile_level = CompLevel_none; aoqi@0: char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length]; aoqi@0: aoqi@0: // Performance counters aoqi@0: PerfCounter* CompileBroker::_perf_total_compilation = NULL; aoqi@0: PerfCounter* CompileBroker::_perf_osr_compilation = NULL; aoqi@0: PerfCounter* CompileBroker::_perf_standard_compilation = NULL; aoqi@0: aoqi@0: PerfCounter* CompileBroker::_perf_total_bailout_count = NULL; aoqi@0: PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL; aoqi@0: PerfCounter* CompileBroker::_perf_total_compile_count = NULL; aoqi@0: PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL; aoqi@0: PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL; aoqi@0: aoqi@0: PerfCounter* CompileBroker::_perf_sum_osr_bytes_compiled = NULL; aoqi@0: PerfCounter* CompileBroker::_perf_sum_standard_bytes_compiled = NULL; aoqi@0: PerfCounter* CompileBroker::_perf_sum_nmethod_size = NULL; aoqi@0: PerfCounter* CompileBroker::_perf_sum_nmethod_code_size = NULL; aoqi@0: aoqi@0: PerfStringVariable* CompileBroker::_perf_last_method = NULL; aoqi@0: PerfStringVariable* CompileBroker::_perf_last_failed_method = NULL; aoqi@0: PerfStringVariable* CompileBroker::_perf_last_invalidated_method = NULL; aoqi@0: PerfVariable* CompileBroker::_perf_last_compile_type = NULL; aoqi@0: PerfVariable* CompileBroker::_perf_last_compile_size = NULL; aoqi@0: PerfVariable* CompileBroker::_perf_last_failed_type = NULL; aoqi@0: PerfVariable* CompileBroker::_perf_last_invalidated_type = NULL; aoqi@0: aoqi@0: // Timers and counters for generating statistics aoqi@0: elapsedTimer CompileBroker::_t_total_compilation; aoqi@0: elapsedTimer CompileBroker::_t_osr_compilation; aoqi@0: elapsedTimer CompileBroker::_t_standard_compilation; aoqi@0: aoqi@0: int CompileBroker::_total_bailout_count = 0; aoqi@0: int CompileBroker::_total_invalidated_count = 0; aoqi@0: int CompileBroker::_total_compile_count = 0; aoqi@0: int CompileBroker::_total_osr_compile_count = 0; aoqi@0: int CompileBroker::_total_standard_compile_count = 0; aoqi@0: aoqi@0: int CompileBroker::_sum_osr_bytes_compiled = 0; aoqi@0: int CompileBroker::_sum_standard_bytes_compiled = 0; aoqi@0: int CompileBroker::_sum_nmethod_size = 0; aoqi@0: int CompileBroker::_sum_nmethod_code_size = 0; aoqi@0: aoqi@0: long CompileBroker::_peak_compilation_time = 0; aoqi@0: aoqi@0: CompileQueue* CompileBroker::_c2_method_queue = NULL; aoqi@0: CompileQueue* CompileBroker::_c1_method_queue = NULL; aoqi@0: CompileTask* CompileBroker::_task_free_list = NULL; aoqi@0: aoqi@0: GrowableArray* CompileBroker::_compiler_threads = NULL; aoqi@0: aoqi@0: aoqi@0: class CompilationLog : public StringEventLog { aoqi@0: public: aoqi@0: CompilationLog() : StringEventLog("Compilation events") { aoqi@0: } aoqi@0: aoqi@0: void log_compile(JavaThread* thread, CompileTask* task) { aoqi@0: StringLogMessage lm; aoqi@0: stringStream sstr = lm.stream(); aoqi@0: // msg.time_stamp().update_to(tty->time_stamp().ticks()); aoqi@0: task->print_compilation(&sstr, NULL, true); aoqi@0: log(thread, "%s", (const char*)lm); aoqi@0: } aoqi@0: aoqi@0: void log_nmethod(JavaThread* thread, nmethod* nm) { aoqi@0: log(thread, "nmethod %d%s " INTPTR_FORMAT " code ["INTPTR_FORMAT ", " INTPTR_FORMAT "]", aoqi@0: nm->compile_id(), nm->is_osr_method() ? "%" : "", aoqi@0: p2i(nm), p2i(nm->code_begin()), p2i(nm->code_end())); aoqi@0: } aoqi@0: aoqi@0: void log_failure(JavaThread* thread, CompileTask* task, const char* reason, const char* retry_message) { aoqi@0: StringLogMessage lm; aoqi@0: lm.print("%4d COMPILE SKIPPED: %s", task->compile_id(), reason); aoqi@0: if (retry_message != NULL) { aoqi@0: lm.append(" (%s)", retry_message); aoqi@0: } aoqi@0: lm.print("\n"); aoqi@0: log(thread, "%s", (const char*)lm); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: static CompilationLog* _compilation_log = NULL; aoqi@0: aoqi@0: void compileBroker_init() { aoqi@0: if (LogEvents) { aoqi@0: _compilation_log = new CompilationLog(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) { aoqi@0: CompilerThread* thread = CompilerThread::current(); aoqi@0: thread->set_task(task); aoqi@0: CompileLog* log = thread->log(); aoqi@0: if (log != NULL) task->log_task_start(log); aoqi@0: } aoqi@0: aoqi@0: CompileTaskWrapper::~CompileTaskWrapper() { aoqi@0: CompilerThread* thread = CompilerThread::current(); aoqi@0: CompileTask* task = thread->task(); aoqi@0: CompileLog* log = thread->log(); aoqi@0: if (log != NULL) task->log_task_done(log); aoqi@0: thread->set_task(NULL); aoqi@0: task->set_code_handle(NULL); aoqi@0: thread->set_env(NULL); aoqi@0: if (task->is_blocking()) { aoqi@0: MutexLocker notifier(task->lock(), thread); aoqi@0: task->mark_complete(); aoqi@0: // Notify the waiting thread that the compilation has completed. aoqi@0: task->lock()->notify_all(); aoqi@0: } else { aoqi@0: task->mark_complete(); aoqi@0: aoqi@0: // By convention, the compiling thread is responsible for aoqi@0: // recycling a non-blocking CompileTask. aoqi@0: CompileBroker::free_task(task); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::initialize aoqi@0: void CompileTask::initialize(int compile_id, aoqi@0: methodHandle method, aoqi@0: int osr_bci, aoqi@0: int comp_level, aoqi@0: methodHandle hot_method, aoqi@0: int hot_count, aoqi@0: const char* comment, aoqi@0: bool is_blocking) { aoqi@0: assert(!_lock->is_locked(), "bad locking"); aoqi@0: aoqi@0: _compile_id = compile_id; aoqi@0: _method = method(); aoqi@0: _method_holder = JNIHandles::make_global(method->method_holder()->klass_holder()); aoqi@0: _osr_bci = osr_bci; aoqi@0: _is_blocking = is_blocking; aoqi@0: _comp_level = comp_level; aoqi@0: _num_inlined_bytecodes = 0; aoqi@0: aoqi@0: _is_complete = false; aoqi@0: _is_success = false; aoqi@0: _code_handle = NULL; aoqi@0: aoqi@0: _hot_method = NULL; aoqi@0: _hot_method_holder = NULL; aoqi@0: _hot_count = hot_count; aoqi@0: _time_queued = 0; // tidy aoqi@0: _comment = comment; aoqi@0: aoqi@0: if (LogCompilation) { aoqi@0: _time_queued = os::elapsed_counter(); aoqi@0: if (hot_method.not_null()) { aoqi@0: if (hot_method == method) { aoqi@0: _hot_method = _method; aoqi@0: } else { aoqi@0: _hot_method = hot_method(); aoqi@0: // only add loader or mirror if different from _method_holder aoqi@0: _hot_method_holder = JNIHandles::make_global(hot_method->method_holder()->klass_holder()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: _next = NULL; aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::code/set_code aoqi@0: nmethod* CompileTask::code() const { aoqi@0: if (_code_handle == NULL) return NULL; aoqi@0: return _code_handle->code(); aoqi@0: } aoqi@0: void CompileTask::set_code(nmethod* nm) { aoqi@0: if (_code_handle == NULL && nm == NULL) return; aoqi@0: guarantee(_code_handle != NULL, ""); aoqi@0: _code_handle->set_code(nm); aoqi@0: if (nm == NULL) _code_handle = NULL; // drop the handle also aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::free aoqi@0: void CompileTask::free() { aoqi@0: set_code(NULL); aoqi@0: assert(!_lock->is_locked(), "Should not be locked when freed"); aoqi@0: JNIHandles::destroy_global(_method_holder); aoqi@0: JNIHandles::destroy_global(_hot_method_holder); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CompileTask::mark_on_stack() { aoqi@0: // Mark these methods as something redefine classes cannot remove. aoqi@0: _method->set_on_stack(true); aoqi@0: if (_hot_method != NULL) { aoqi@0: _hot_method->set_on_stack(true); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::print aoqi@0: void CompileTask::print() { aoqi@0: tty->print("print("method="); aoqi@0: _method->print_name(tty); aoqi@0: tty->print_cr(" osr_bci=%d is_blocking=%s is_complete=%s is_success=%s>", aoqi@0: _osr_bci, bool_to_str(_is_blocking), aoqi@0: bool_to_str(_is_complete), bool_to_str(_is_success)); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::print_line_on_error aoqi@0: // aoqi@0: // This function is called by fatal error handler when the thread aoqi@0: // causing troubles is a compiler thread. aoqi@0: // aoqi@0: // Do not grab any lock, do not allocate memory. aoqi@0: // aoqi@0: // Otherwise it's the same as CompileTask::print_line() aoqi@0: // aoqi@0: void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) { aoqi@0: // print compiler name aoqi@0: st->print("%s:", CompileBroker::compiler_name(comp_level())); aoqi@0: print_compilation(st); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::print_line aoqi@0: void CompileTask::print_line() { aoqi@0: ttyLocker ttyl; // keep the following output all in one block aoqi@0: // print compiler name if requested aoqi@0: if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler_name(comp_level())); aoqi@0: print_compilation(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::print_compilation_impl aoqi@0: void CompileTask::print_compilation_impl(outputStream* st, Method* method, int compile_id, int comp_level, aoqi@0: bool is_osr_method, int osr_bci, bool is_blocking, aoqi@0: const char* msg, bool short_form) { aoqi@0: if (!short_form) { aoqi@0: st->print("%7d ", (int) st->time_stamp().milliseconds()); // print timestamp aoqi@0: } aoqi@0: st->print("%4d ", compile_id); // print compilation number aoqi@0: aoqi@0: // For unloaded methods the transition to zombie occurs after the aoqi@0: // method is cleared so it's impossible to report accurate aoqi@0: // information for that case. aoqi@0: bool is_synchronized = false; aoqi@0: bool has_exception_handler = false; aoqi@0: bool is_native = false; aoqi@0: if (method != NULL) { aoqi@0: is_synchronized = method->is_synchronized(); aoqi@0: has_exception_handler = method->has_exception_handler(); aoqi@0: is_native = method->is_native(); aoqi@0: } aoqi@0: // method attributes aoqi@0: const char compile_type = is_osr_method ? '%' : ' '; aoqi@0: const char sync_char = is_synchronized ? 's' : ' '; aoqi@0: const char exception_char = has_exception_handler ? '!' : ' '; aoqi@0: const char blocking_char = is_blocking ? 'b' : ' '; aoqi@0: const char native_char = is_native ? 'n' : ' '; aoqi@0: aoqi@0: // print method attributes aoqi@0: st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char); aoqi@0: aoqi@0: if (TieredCompilation) { aoqi@0: if (comp_level != -1) st->print("%d ", comp_level); aoqi@0: else st->print("- "); aoqi@0: } aoqi@0: st->print(" "); // more indent aoqi@0: aoqi@0: if (method == NULL) { aoqi@0: st->print("(method)"); aoqi@0: } else { aoqi@0: method->print_short_name(st); aoqi@0: if (is_osr_method) { aoqi@0: st->print(" @ %d", osr_bci); aoqi@0: } aoqi@0: if (method->is_native()) aoqi@0: st->print(" (native)"); aoqi@0: else aoqi@0: st->print(" (%d bytes)", method->code_size()); aoqi@0: } aoqi@0: aoqi@0: if (msg != NULL) { aoqi@0: st->print(" %s", msg); aoqi@0: } aoqi@0: if (!short_form) { aoqi@0: st->cr(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::print_inlining aoqi@0: void CompileTask::print_inlining(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) { aoqi@0: // 1234567 aoqi@0: st->print(" "); // print timestamp aoqi@0: // 1234 aoqi@0: st->print(" "); // print compilation number aoqi@0: aoqi@0: // method attributes aoqi@0: if (method->is_loaded()) { aoqi@0: const char sync_char = method->is_synchronized() ? 's' : ' '; aoqi@0: const char exception_char = method->has_exception_handlers() ? '!' : ' '; aoqi@0: const char monitors_char = method->has_monitor_bytecodes() ? 'm' : ' '; aoqi@0: aoqi@0: // print method attributes aoqi@0: st->print(" %c%c%c ", sync_char, exception_char, monitors_char); aoqi@0: } else { aoqi@0: // %s!bn aoqi@0: st->print(" "); // print method attributes aoqi@0: } aoqi@0: aoqi@0: if (TieredCompilation) { aoqi@0: st->print(" "); aoqi@0: } aoqi@0: st->print(" "); // more indent aoqi@0: st->print(" "); // initial inlining indent aoqi@0: aoqi@0: for (int i = 0; i < inline_level; i++) st->print(" "); aoqi@0: aoqi@0: st->print("@ %d ", bci); // print bci aoqi@0: method->print_short_name(st); aoqi@0: if (method->is_loaded()) aoqi@0: st->print(" (%d bytes)", method->code_size()); aoqi@0: else aoqi@0: st->print(" (not loaded)"); aoqi@0: aoqi@0: if (msg != NULL) { aoqi@0: st->print(" %s", msg); aoqi@0: } aoqi@0: st->cr(); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::print_inline_indent aoqi@0: void CompileTask::print_inline_indent(int inline_level, outputStream* st) { aoqi@0: // 1234567 aoqi@0: st->print(" "); // print timestamp aoqi@0: // 1234 aoqi@0: st->print(" "); // print compilation number aoqi@0: // %s!bn aoqi@0: st->print(" "); // print method attributes aoqi@0: if (TieredCompilation) { aoqi@0: st->print(" "); aoqi@0: } aoqi@0: st->print(" "); // more indent aoqi@0: st->print(" "); // initial inlining indent aoqi@0: for (int i = 0; i < inline_level; i++) st->print(" "); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::print_compilation aoqi@0: void CompileTask::print_compilation(outputStream* st, const char* msg, bool short_form) { aoqi@0: bool is_osr_method = osr_bci() != InvocationEntryBci; aoqi@0: print_compilation_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::log_task aoqi@0: void CompileTask::log_task(xmlStream* log) { aoqi@0: Thread* thread = Thread::current(); aoqi@0: methodHandle method(thread, this->method()); aoqi@0: ResourceMark rm(thread); aoqi@0: aoqi@0: // aoqi@0: log->print(" compile_id='%d'", _compile_id); aoqi@0: if (_osr_bci != CompileBroker::standard_entry_bci) { aoqi@0: log->print(" compile_kind='osr'"); // same as nmethod::compile_kind aoqi@0: } // else compile_kind='c2c' aoqi@0: if (!method.is_null()) log->method(method); aoqi@0: if (_osr_bci != CompileBroker::standard_entry_bci) { aoqi@0: log->print(" osr_bci='%d'", _osr_bci); aoqi@0: } aoqi@0: if (_comp_level != CompLevel_highest_tier) { aoqi@0: log->print(" level='%d'", _comp_level); aoqi@0: } aoqi@0: if (_is_blocking) { aoqi@0: log->print(" blocking='1'"); aoqi@0: } aoqi@0: log->stamp(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::log_task_queued aoqi@0: void CompileTask::log_task_queued() { aoqi@0: Thread* thread = Thread::current(); aoqi@0: ttyLocker ttyl; aoqi@0: ResourceMark rm(thread); aoqi@0: aoqi@0: xtty->begin_elem("task_queued"); aoqi@0: log_task(xtty); aoqi@0: if (_comment != NULL) { aoqi@0: xtty->print(" comment='%s'", _comment); aoqi@0: } aoqi@0: if (_hot_method != NULL) { aoqi@0: methodHandle hot(thread, _hot_method); aoqi@0: methodHandle method(thread, _method); aoqi@0: if (hot() != method()) { aoqi@0: xtty->method(hot); aoqi@0: } aoqi@0: } aoqi@0: if (_hot_count != 0) { aoqi@0: xtty->print(" hot_count='%d'", _hot_count); aoqi@0: } aoqi@0: xtty->end_elem(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::log_task_start aoqi@0: void CompileTask::log_task_start(CompileLog* log) { aoqi@0: log->begin_head("task"); aoqi@0: log_task(log); aoqi@0: log->end_head(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileTask::log_task_done aoqi@0: void CompileTask::log_task_done(CompileLog* log) { aoqi@0: Thread* thread = Thread::current(); aoqi@0: methodHandle method(thread, this->method()); aoqi@0: ResourceMark rm(thread); aoqi@0: aoqi@0: // aoqi@0: nmethod* nm = code(); aoqi@0: log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", aoqi@0: _is_success, nm == NULL ? 0 : nm->content_size(), aoqi@0: method->invocation_count()); aoqi@0: int bec = method->backedge_count(); aoqi@0: if (bec != 0) log->print(" backedge_count='%d'", bec); aoqi@0: // Note: "_is_complete" is about to be set, but is not. aoqi@0: if (_num_inlined_bytecodes != 0) { aoqi@0: log->print(" inlined_bytes='%d'", _num_inlined_bytecodes); aoqi@0: } aoqi@0: log->stamp(); aoqi@0: log->end_elem(); aoqi@0: log->tail("task"); aoqi@0: log->clear_identities(); // next task will have different CI aoqi@0: if (log->unflushed_count() > 2000) { aoqi@0: log->flush(); aoqi@0: } aoqi@0: log->mark_file_end(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: // Add a CompileTask to a CompileQueue aoqi@0: void CompileQueue::add(CompileTask* task) { aoqi@0: assert(lock()->owned_by_self(), "must own lock"); aoqi@0: aoqi@0: task->set_next(NULL); aoqi@0: task->set_prev(NULL); aoqi@0: aoqi@0: if (_last == NULL) { aoqi@0: // The compile queue is empty. aoqi@0: assert(_first == NULL, "queue is empty"); aoqi@0: _first = task; aoqi@0: _last = task; aoqi@0: } else { aoqi@0: // Append the task to the queue. aoqi@0: assert(_last->next() == NULL, "not last"); aoqi@0: _last->set_next(task); aoqi@0: task->set_prev(_last); aoqi@0: _last = task; aoqi@0: } aoqi@0: ++_size; aoqi@0: aoqi@0: // Mark the method as being in the compile queue. aoqi@0: task->method()->set_queued_for_compilation(); aoqi@0: aoqi@0: if (CIPrintCompileQueue) { aoqi@0: print(); aoqi@0: } aoqi@0: aoqi@0: if (LogCompilation && xtty != NULL) { aoqi@0: task->log_task_queued(); aoqi@0: } aoqi@0: aoqi@0: // Notify CompilerThreads that a task is available. aoqi@0: lock()->notify_all(); aoqi@0: } aoqi@0: aoqi@0: void CompileQueue::delete_all() { aoqi@0: assert(lock()->owned_by_self(), "must own lock"); aoqi@0: if (_first != NULL) { aoqi@0: for (CompileTask* task = _first; task != NULL; task = task->next()) { aoqi@0: delete task; aoqi@0: } aoqi@0: _first = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileQueue::get aoqi@0: // aoqi@0: // Get the next CompileTask from a CompileQueue aoqi@0: CompileTask* CompileQueue::get() { aoqi@0: NMethodSweeper::possibly_sweep(); aoqi@0: aoqi@0: MutexLocker locker(lock()); aoqi@0: // If _first is NULL we have no more compile jobs. There are two reasons for aoqi@0: // having no compile jobs: First, we compiled everything we wanted. Second, aoqi@0: // we ran out of code cache so compilation has been disabled. In the latter aoqi@0: // case we perform code cache sweeps to free memory such that we can re-enable aoqi@0: // compilation. aoqi@0: while (_first == NULL) { aoqi@0: // Exit loop if compilation is disabled forever aoqi@0: if (CompileBroker::is_compilation_disabled_forever()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) { aoqi@0: // Wait a certain amount of time to possibly do another sweep. aoqi@0: // We must wait until stack scanning has happened so that we can aoqi@0: // transition a method's state from 'not_entrant' to 'zombie'. aoqi@0: long wait_time = NmethodSweepCheckInterval * 1000; aoqi@0: if (FLAG_IS_DEFAULT(NmethodSweepCheckInterval)) { aoqi@0: // Only one thread at a time can do sweeping. Scale the aoqi@0: // wait time according to the number of compiler threads. aoqi@0: // As a result, the next sweep is likely to happen every 100ms aoqi@0: // with an arbitrary number of threads that do sweeping. aoqi@0: wait_time = 100 * CICompilerCount; aoqi@0: } aoqi@0: bool timeout = lock()->wait(!Mutex::_no_safepoint_check_flag, wait_time); aoqi@0: if (timeout) { aoqi@0: MutexUnlocker ul(lock()); aoqi@0: NMethodSweeper::possibly_sweep(); aoqi@0: } aoqi@0: } else { aoqi@0: // If there are no compilation tasks and we can compile new jobs aoqi@0: // (i.e., there is enough free space in the code cache) there is aoqi@0: // no need to invoke the sweeper. As a result, the hotness of methods aoqi@0: // remains unchanged. This behavior is desired, since we want to keep aoqi@0: // the stable state, i.e., we do not want to evict methods from the aoqi@0: // code cache if it is unnecessary. aoqi@0: // We need a timed wait here, since compiler threads can exit if compilation aoqi@0: // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads aoqi@0: // is not critical and we do not want idle compiler threads to wake up too often. aoqi@0: lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (CompileBroker::is_compilation_disabled_forever()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: CompileTask* task = CompilationPolicy::policy()->select_task(this); aoqi@0: remove(task); aoqi@0: return task; aoqi@0: } aoqi@0: aoqi@0: void CompileQueue::remove(CompileTask* task) aoqi@0: { aoqi@0: assert(lock()->owned_by_self(), "must own lock"); aoqi@0: if (task->prev() != NULL) { aoqi@0: task->prev()->set_next(task->next()); aoqi@0: } else { aoqi@0: // max is the first element aoqi@0: assert(task == _first, "Sanity"); aoqi@0: _first = task->next(); aoqi@0: } aoqi@0: aoqi@0: if (task->next() != NULL) { aoqi@0: task->next()->set_prev(task->prev()); aoqi@0: } else { aoqi@0: // max is the last element aoqi@0: assert(task == _last, "Sanity"); aoqi@0: _last = task->prev(); aoqi@0: } aoqi@0: --_size; aoqi@0: } aoqi@0: aoqi@0: // methods in the compile queue need to be marked as used on the stack aoqi@0: // so that they don't get reclaimed by Redefine Classes aoqi@0: void CompileQueue::mark_on_stack() { aoqi@0: CompileTask* task = _first; aoqi@0: while (task != NULL) { aoqi@0: task->mark_on_stack(); aoqi@0: task = task->next(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileQueue::print aoqi@0: void CompileQueue::print() { aoqi@0: tty->print_cr("Contents of %s", name()); aoqi@0: tty->print_cr("----------------------"); aoqi@0: CompileTask* task = _first; aoqi@0: while (task != NULL) { aoqi@0: task->print_line(); aoqi@0: task = task->next(); aoqi@0: } aoqi@0: tty->print_cr("----------------------"); aoqi@0: } aoqi@0: aoqi@0: CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) { aoqi@0: aoqi@0: _current_method[0] = '\0'; aoqi@0: _compile_type = CompileBroker::no_compile; aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: // create the thread instance name space string - don't create an aoqi@0: // instance subspace if instance is -1 - keeps the adapterThread aoqi@0: // counters from having a ".0" namespace. aoqi@0: const char* thread_i = (instance == -1) ? thread_name : aoqi@0: PerfDataManager::name_space(thread_name, instance); aoqi@0: aoqi@0: aoqi@0: char* name = PerfDataManager::counter_name(thread_i, "method"); aoqi@0: _perf_current_method = aoqi@0: PerfDataManager::create_string_variable(SUN_CI, name, aoqi@0: cmname_buffer_length, aoqi@0: _current_method, CHECK); aoqi@0: aoqi@0: name = PerfDataManager::counter_name(thread_i, "type"); aoqi@0: _perf_compile_type = PerfDataManager::create_variable(SUN_CI, name, aoqi@0: PerfData::U_None, aoqi@0: (jlong)_compile_type, aoqi@0: CHECK); aoqi@0: aoqi@0: name = PerfDataManager::counter_name(thread_i, "time"); aoqi@0: _perf_time = PerfDataManager::create_counter(SUN_CI, name, aoqi@0: PerfData::U_Ticks, CHECK); aoqi@0: aoqi@0: name = PerfDataManager::counter_name(thread_i, "compiles"); aoqi@0: _perf_compiles = PerfDataManager::create_counter(SUN_CI, name, aoqi@0: PerfData::U_Events, CHECK); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::compilation_init aoqi@0: // aoqi@0: // Initialize the Compilation object aoqi@0: void CompileBroker::compilation_init() { aoqi@0: _last_method_compiled[0] = '\0'; aoqi@0: aoqi@0: // No need to initialize compilation system if we do not use it. aoqi@0: if (!UseCompiler) { aoqi@0: return; aoqi@0: } aoqi@0: #ifndef SHARK aoqi@0: // Set the interface to the current compiler(s). aoqi@0: int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple); aoqi@0: int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization); aoqi@0: #ifdef COMPILER1 aoqi@0: if (c1_count > 0) { aoqi@0: _compilers[0] = new Compiler(); aoqi@0: } aoqi@0: #endif // COMPILER1 aoqi@0: aoqi@0: #ifdef COMPILER2 aoqi@0: if (c2_count > 0) { aoqi@0: _compilers[1] = new C2Compiler(); aoqi@0: } aoqi@0: #endif // COMPILER2 aoqi@0: aoqi@0: #else // SHARK aoqi@0: int c1_count = 0; aoqi@0: int c2_count = 1; aoqi@0: aoqi@0: _compilers[1] = new SharkCompiler(); aoqi@0: #endif // SHARK aoqi@0: aoqi@0: // Initialize the CompileTask free list aoqi@0: _task_free_list = NULL; aoqi@0: aoqi@0: // Start the CompilerThreads aoqi@0: init_compiler_threads(c1_count, c2_count); aoqi@0: // totalTime performance counter is always created as it is required aoqi@0: // by the implementation of java.lang.management.CompilationMBean. aoqi@0: { aoqi@0: EXCEPTION_MARK; aoqi@0: _perf_total_compilation = aoqi@0: PerfDataManager::create_counter(JAVA_CI, "totalTime", aoqi@0: PerfData::U_Ticks, CHECK); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: aoqi@0: EXCEPTION_MARK; aoqi@0: aoqi@0: // create the jvmstat performance counters aoqi@0: _perf_osr_compilation = aoqi@0: PerfDataManager::create_counter(SUN_CI, "osrTime", aoqi@0: PerfData::U_Ticks, CHECK); aoqi@0: aoqi@0: _perf_standard_compilation = aoqi@0: PerfDataManager::create_counter(SUN_CI, "standardTime", aoqi@0: PerfData::U_Ticks, CHECK); aoqi@0: aoqi@0: _perf_total_bailout_count = aoqi@0: PerfDataManager::create_counter(SUN_CI, "totalBailouts", aoqi@0: PerfData::U_Events, CHECK); aoqi@0: aoqi@0: _perf_total_invalidated_count = aoqi@0: PerfDataManager::create_counter(SUN_CI, "totalInvalidates", aoqi@0: PerfData::U_Events, CHECK); aoqi@0: aoqi@0: _perf_total_compile_count = aoqi@0: PerfDataManager::create_counter(SUN_CI, "totalCompiles", aoqi@0: PerfData::U_Events, CHECK); aoqi@0: _perf_total_osr_compile_count = aoqi@0: PerfDataManager::create_counter(SUN_CI, "osrCompiles", aoqi@0: PerfData::U_Events, CHECK); aoqi@0: aoqi@0: _perf_total_standard_compile_count = aoqi@0: PerfDataManager::create_counter(SUN_CI, "standardCompiles", aoqi@0: PerfData::U_Events, CHECK); aoqi@0: aoqi@0: _perf_sum_osr_bytes_compiled = aoqi@0: PerfDataManager::create_counter(SUN_CI, "osrBytes", aoqi@0: PerfData::U_Bytes, CHECK); aoqi@0: aoqi@0: _perf_sum_standard_bytes_compiled = aoqi@0: PerfDataManager::create_counter(SUN_CI, "standardBytes", aoqi@0: PerfData::U_Bytes, CHECK); aoqi@0: aoqi@0: _perf_sum_nmethod_size = aoqi@0: PerfDataManager::create_counter(SUN_CI, "nmethodSize", aoqi@0: PerfData::U_Bytes, CHECK); aoqi@0: aoqi@0: _perf_sum_nmethod_code_size = aoqi@0: PerfDataManager::create_counter(SUN_CI, "nmethodCodeSize", aoqi@0: PerfData::U_Bytes, CHECK); aoqi@0: aoqi@0: _perf_last_method = aoqi@0: PerfDataManager::create_string_variable(SUN_CI, "lastMethod", aoqi@0: CompilerCounters::cmname_buffer_length, aoqi@0: "", CHECK); aoqi@0: aoqi@0: _perf_last_failed_method = aoqi@0: PerfDataManager::create_string_variable(SUN_CI, "lastFailedMethod", aoqi@0: CompilerCounters::cmname_buffer_length, aoqi@0: "", CHECK); aoqi@0: aoqi@0: _perf_last_invalidated_method = aoqi@0: PerfDataManager::create_string_variable(SUN_CI, "lastInvalidatedMethod", aoqi@0: CompilerCounters::cmname_buffer_length, aoqi@0: "", CHECK); aoqi@0: aoqi@0: _perf_last_compile_type = aoqi@0: PerfDataManager::create_variable(SUN_CI, "lastType", aoqi@0: PerfData::U_None, aoqi@0: (jlong)CompileBroker::no_compile, aoqi@0: CHECK); aoqi@0: aoqi@0: _perf_last_compile_size = aoqi@0: PerfDataManager::create_variable(SUN_CI, "lastSize", aoqi@0: PerfData::U_Bytes, aoqi@0: (jlong)CompileBroker::no_compile, aoqi@0: CHECK); aoqi@0: aoqi@0: aoqi@0: _perf_last_failed_type = aoqi@0: PerfDataManager::create_variable(SUN_CI, "lastFailedType", aoqi@0: PerfData::U_None, aoqi@0: (jlong)CompileBroker::no_compile, aoqi@0: CHECK); aoqi@0: aoqi@0: _perf_last_invalidated_type = aoqi@0: PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType", aoqi@0: PerfData::U_None, aoqi@0: (jlong)CompileBroker::no_compile, aoqi@0: CHECK); aoqi@0: } aoqi@0: aoqi@0: _initialized = true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, aoqi@0: AbstractCompiler* comp, TRAPS) { aoqi@0: CompilerThread* compiler_thread = NULL; aoqi@0: aoqi@0: Klass* k = aoqi@0: SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), aoqi@0: true, CHECK_0); aoqi@0: instanceKlassHandle klass (THREAD, k); aoqi@0: instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0); aoqi@0: Handle string = java_lang_String::create_from_str(name, CHECK_0); aoqi@0: aoqi@0: // Initialize thread_oop to put it into the system threadGroup aoqi@0: Handle thread_group (THREAD, Universe::system_thread_group()); aoqi@0: JavaValue result(T_VOID); aoqi@0: JavaCalls::call_special(&result, thread_oop, aoqi@0: klass, aoqi@0: vmSymbols::object_initializer_name(), aoqi@0: vmSymbols::threadgroup_string_void_signature(), aoqi@0: thread_group, aoqi@0: string, aoqi@0: CHECK_0); aoqi@0: aoqi@0: { aoqi@0: MutexLocker mu(Threads_lock, THREAD); aoqi@0: compiler_thread = new CompilerThread(queue, counters); aoqi@0: // At this point the new CompilerThread data-races with this startup aoqi@0: // thread (which I believe is the primoridal thread and NOT the VM aoqi@0: // thread). This means Java bytecodes being executed at startup can aoqi@0: // queue compile jobs which will run at whatever default priority the aoqi@0: // newly created CompilerThread runs at. aoqi@0: aoqi@0: aoqi@0: // At this point it may be possible that no osthread was created for the aoqi@0: // JavaThread due to lack of memory. We would have to throw an exception aoqi@0: // in that case. However, since this must work and we do not allow aoqi@0: // exceptions anyway, check and abort if this fails. aoqi@0: aoqi@0: if (compiler_thread == NULL || compiler_thread->osthread() == NULL){ aoqi@0: vm_exit_during_initialization("java.lang.OutOfMemoryError", aoqi@0: "unable to create new native thread"); aoqi@0: } aoqi@0: aoqi@0: java_lang_Thread::set_thread(thread_oop(), compiler_thread); aoqi@0: aoqi@0: // Note that this only sets the JavaThread _priority field, which by aoqi@0: // definition is limited to Java priorities and not OS priorities. aoqi@0: // The os-priority is set in the CompilerThread startup code itself aoqi@0: aoqi@0: java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); aoqi@0: aoqi@0: // Note that we cannot call os::set_priority because it expects Java aoqi@0: // priorities and we are *explicitly* using OS priorities so that it's aoqi@0: // possible to set the compiler thread priority higher than any Java aoqi@0: // thread. aoqi@0: aoqi@0: int native_prio = CompilerThreadPriority; aoqi@0: if (native_prio == -1) { aoqi@0: if (UseCriticalCompilerThreadPriority) { aoqi@0: native_prio = os::java_to_os_priority[CriticalPriority]; aoqi@0: } else { aoqi@0: native_prio = os::java_to_os_priority[NearMaxPriority]; aoqi@0: } aoqi@0: } aoqi@0: os::set_native_priority(compiler_thread, native_prio); aoqi@0: aoqi@0: java_lang_Thread::set_daemon(thread_oop()); aoqi@0: aoqi@0: compiler_thread->set_threadObj(thread_oop()); aoqi@0: compiler_thread->set_compiler(comp); aoqi@0: Threads::add(compiler_thread); aoqi@0: Thread::start(compiler_thread); aoqi@0: } aoqi@0: aoqi@0: // Let go of Threads_lock before yielding aoqi@0: os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS) aoqi@0: aoqi@0: return compiler_thread; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) { aoqi@0: EXCEPTION_MARK; aoqi@0: #if !defined(ZERO) && !defined(SHARK) aoqi@0: assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); aoqi@0: #endif // !ZERO && !SHARK aoqi@0: // Initialize the compilation queue aoqi@0: if (c2_compiler_count > 0) { aoqi@0: _c2_method_queue = new CompileQueue("C2MethodQueue", MethodCompileQueue_lock); aoqi@0: _compilers[1]->set_num_compiler_threads(c2_compiler_count); aoqi@0: } aoqi@0: if (c1_compiler_count > 0) { aoqi@0: _c1_method_queue = new CompileQueue("C1MethodQueue", MethodCompileQueue_lock); aoqi@0: _compilers[0]->set_num_compiler_threads(c1_compiler_count); aoqi@0: } aoqi@0: aoqi@0: int compiler_count = c1_compiler_count + c2_compiler_count; aoqi@0: aoqi@0: _compiler_threads = aoqi@0: new (ResourceObj::C_HEAP, mtCompiler) GrowableArray(compiler_count, true); aoqi@0: aoqi@0: char name_buffer[256]; aoqi@0: for (int i = 0; i < c2_compiler_count; i++) { aoqi@0: // Create a name for our thread. aoqi@0: sprintf(name_buffer, "C2 CompilerThread%d", i); aoqi@0: CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); aoqi@0: // Shark and C2 aoqi@0: CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, _compilers[1], CHECK); aoqi@0: _compiler_threads->append(new_thread); aoqi@0: } aoqi@0: aoqi@0: for (int i = c2_compiler_count; i < compiler_count; i++) { aoqi@0: // Create a name for our thread. aoqi@0: sprintf(name_buffer, "C1 CompilerThread%d", i); aoqi@0: CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); aoqi@0: // C1 aoqi@0: CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, _compilers[0], CHECK); aoqi@0: _compiler_threads->append(new_thread); aoqi@0: } aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Set the methods on the stack as on_stack so that redefine classes doesn't aoqi@0: // reclaim them aoqi@0: void CompileBroker::mark_on_stack() { aoqi@0: if (_c2_method_queue != NULL) { aoqi@0: _c2_method_queue->mark_on_stack(); aoqi@0: } aoqi@0: if (_c1_method_queue != NULL) { aoqi@0: _c1_method_queue->mark_on_stack(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::compile_method aoqi@0: // aoqi@0: // Request compilation of a method. aoqi@0: void CompileBroker::compile_method_base(methodHandle method, aoqi@0: int osr_bci, aoqi@0: int comp_level, aoqi@0: methodHandle hot_method, aoqi@0: int hot_count, aoqi@0: const char* comment, aoqi@0: Thread* thread) { aoqi@0: // do nothing if compiler thread(s) is not available aoqi@0: if (!_initialized ) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: guarantee(!method->is_abstract(), "cannot compile abstract methods"); aoqi@0: assert(method->method_holder()->oop_is_instance(), aoqi@0: "sanity check"); aoqi@0: assert(!method->method_holder()->is_not_initialized(), aoqi@0: "method holder must be initialized"); aoqi@0: assert(!method->is_method_handle_intrinsic(), "do not enqueue these guys"); aoqi@0: aoqi@0: if (CIPrintRequests) { aoqi@0: tty->print("request: "); aoqi@0: method->print_short_name(tty); aoqi@0: if (osr_bci != InvocationEntryBci) { aoqi@0: tty->print(" osr_bci: %d", osr_bci); aoqi@0: } aoqi@0: tty->print(" comment: %s count: %d", comment, hot_count); aoqi@0: if (!hot_method.is_null()) { aoqi@0: tty->print(" hot: "); aoqi@0: if (hot_method() != method()) { aoqi@0: hot_method->print_short_name(tty); aoqi@0: } else { aoqi@0: tty->print("yes"); aoqi@0: } aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: // A request has been made for compilation. Before we do any aoqi@0: // real work, check to see if the method has been compiled aoqi@0: // in the meantime with a definitive result. aoqi@0: if (compilation_is_complete(method, osr_bci, comp_level)) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (osr_bci != -1 && !FLAG_IS_DEFAULT(OSROnlyBCI)) { aoqi@0: if ((OSROnlyBCI > 0) ? (OSROnlyBCI != osr_bci) : (-OSROnlyBCI == osr_bci)) { aoqi@0: // Positive OSROnlyBCI means only compile that bci. Negative means don't compile that BCI. aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: // If this method is already in the compile queue, then aoqi@0: // we do not block the current thread. aoqi@0: if (compilation_is_in_queue(method, osr_bci)) { aoqi@0: // We may want to decay our counter a bit here to prevent aoqi@0: // multiple denied requests for compilation. This is an aoqi@0: // open compilation policy issue. Note: The other possibility, aoqi@0: // in the case that this is a blocking compile request, is to have aoqi@0: // all subsequent blocking requesters wait for completion of aoqi@0: // ongoing compiles. Note that in this case we'll need a protocol aoqi@0: // for freeing the associated compile tasks. [Or we could have aoqi@0: // a single static monitor on which all these waiters sleep.] aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // If the requesting thread is holding the pending list lock aoqi@0: // then we just return. We can't risk blocking while holding aoqi@0: // the pending list lock or a 3-way deadlock may occur aoqi@0: // between the reference handler thread, a GC (instigated aoqi@0: // by a compiler thread), and compiled method registration. aoqi@0: if (InstanceRefKlass::owns_pending_list_lock(JavaThread::current())) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Outputs from the following MutexLocker block: aoqi@0: CompileTask* task = NULL; aoqi@0: bool blocking = false; aoqi@0: CompileQueue* queue = compile_queue(comp_level); aoqi@0: aoqi@0: // Acquire our lock. aoqi@0: { aoqi@0: MutexLocker locker(queue->lock(), thread); aoqi@0: aoqi@0: // Make sure the method has not slipped into the queues since aoqi@0: // last we checked; note that those checks were "fast bail-outs". aoqi@0: // Here we need to be more careful, see 14012000 below. aoqi@0: if (compilation_is_in_queue(method, osr_bci)) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // We need to check again to see if the compilation has aoqi@0: // completed. A previous compilation may have registered aoqi@0: // some result. aoqi@0: if (compilation_is_complete(method, osr_bci, comp_level)) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // We now know that this compilation is not pending, complete, aoqi@0: // or prohibited. Assign a compile_id to this compilation aoqi@0: // and check to see if it is in our [Start..Stop) range. aoqi@0: int compile_id = assign_compile_id(method, osr_bci); aoqi@0: if (compile_id == 0) { aoqi@0: // The compilation falls outside the allowed range. aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Should this thread wait for completion of the compile? aoqi@0: blocking = is_compile_blocking(method, osr_bci); aoqi@0: aoqi@0: // We will enter the compilation in the queue. aoqi@0: // 14012000: Note that this sets the queued_for_compile bits in aoqi@0: // the target method. We can now reason that a method cannot be aoqi@0: // queued for compilation more than once, as follows: aoqi@0: // Before a thread queues a task for compilation, it first acquires aoqi@0: // the compile queue lock, then checks if the method's queued bits aoqi@0: // are set or it has already been compiled. Thus there can not be two aoqi@0: // instances of a compilation task for the same method on the aoqi@0: // compilation queue. Consider now the case where the compilation aoqi@0: // thread has already removed a task for that method from the queue aoqi@0: // and is in the midst of compiling it. In this case, the aoqi@0: // queued_for_compile bits must be set in the method (and these aoqi@0: // will be visible to the current thread, since the bits were set aoqi@0: // under protection of the compile queue lock, which we hold now. aoqi@0: // When the compilation completes, the compiler thread first sets aoqi@0: // the compilation result and then clears the queued_for_compile aoqi@0: // bits. Neither of these actions are protected by a barrier (or done aoqi@0: // under the protection of a lock), so the only guarantee we have aoqi@0: // (on machines with TSO (Total Store Order)) is that these values aoqi@0: // will update in that order. As a result, the only combinations of aoqi@0: // these bits that the current thread will see are, in temporal order: aoqi@0: // : aoqi@0: // <0, 1> : in compile queue, but not yet compiled aoqi@0: // <1, 1> : compiled but queue bit not cleared aoqi@0: // <1, 0> : compiled and queue bit cleared aoqi@0: // Because we first check the queue bits then check the result bits, aoqi@0: // we are assured that we cannot introduce a duplicate task. aoqi@0: // Note that if we did the tests in the reverse order (i.e. check aoqi@0: // result then check queued bit), we could get the result bit before aoqi@0: // the compilation completed, and the queue bit after the compilation aoqi@0: // completed, and end up introducing a "duplicate" (redundant) task. aoqi@0: // In that case, the compiler thread should first check if a method aoqi@0: // has already been compiled before trying to compile it. aoqi@0: // NOTE: in the event that there are multiple compiler threads and aoqi@0: // there is de-optimization/recompilation, things will get hairy, aoqi@0: // and in that case it's best to protect both the testing (here) of aoqi@0: // these bits, and their updating (here and elsewhere) under a aoqi@0: // common lock. aoqi@0: task = create_compile_task(queue, aoqi@0: compile_id, method, aoqi@0: osr_bci, comp_level, aoqi@0: hot_method, hot_count, comment, aoqi@0: blocking); aoqi@0: } aoqi@0: aoqi@0: if (blocking) { aoqi@0: wait_for_completion(task); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: nmethod* CompileBroker::compile_method(methodHandle method, int osr_bci, aoqi@0: int comp_level, aoqi@0: methodHandle hot_method, int hot_count, aoqi@0: const char* comment, Thread* THREAD) { aoqi@0: // make sure arguments make sense aoqi@0: assert(method->method_holder()->oop_is_instance(), "not an instance method"); aoqi@0: assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range"); aoqi@0: assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods"); aoqi@0: assert(!method->method_holder()->is_not_initialized(), "method holder must be initialized"); aoqi@0: // allow any levels for WhiteBox aoqi@0: assert(WhiteBoxAPI || TieredCompilation || comp_level == CompLevel_highest_tier, "only CompLevel_highest_tier must be used in non-tiered"); aoqi@0: // return quickly if possible aoqi@0: aoqi@0: // lock, make sure that the compilation aoqi@0: // isn't prohibited in a straightforward way. aoqi@0: AbstractCompiler *comp = CompileBroker::compiler(comp_level); aoqi@0: if (comp == NULL || !comp->can_compile_method(method) || aoqi@0: compilation_is_prohibited(method, osr_bci, comp_level)) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: if (osr_bci == InvocationEntryBci) { aoqi@0: // standard compilation aoqi@0: nmethod* method_code = method->code(); aoqi@0: if (method_code != NULL) { aoqi@0: if (compilation_is_complete(method, osr_bci, comp_level)) { aoqi@0: return method_code; aoqi@0: } aoqi@0: } aoqi@0: if (method->is_not_compilable(comp_level)) { aoqi@0: return NULL; aoqi@0: } aoqi@0: } else { aoqi@0: // osr compilation aoqi@0: #ifndef TIERED aoqi@0: // seems like an assert of dubious value aoqi@0: assert(comp_level == CompLevel_highest_tier, aoqi@0: "all OSR compiles are assumed to be at a single compilation lavel"); aoqi@0: #endif // TIERED aoqi@0: // We accept a higher level osr method aoqi@0: nmethod* nm = method->lookup_osr_nmethod_for(osr_bci, comp_level, false); aoqi@0: if (nm != NULL) return nm; aoqi@0: if (method->is_not_osr_compilable(comp_level)) return NULL; aoqi@0: } aoqi@0: aoqi@0: assert(!HAS_PENDING_EXCEPTION, "No exception should be present"); aoqi@0: // some prerequisites that are compiler specific aoqi@0: if (comp->is_c2() || comp->is_shark()) { aoqi@0: method->constants()->resolve_string_constants(CHECK_AND_CLEAR_NULL); aoqi@0: // Resolve all classes seen in the signature of the method aoqi@0: // we are compiling. aoqi@0: Method::load_signature_classes(method, CHECK_AND_CLEAR_NULL); aoqi@0: } aoqi@0: aoqi@0: // If the method is native, do the lookup in the thread requesting aoqi@0: // the compilation. Native lookups can load code, which is not aoqi@0: // permitted during compilation. aoqi@0: // aoqi@0: // Note: A native method implies non-osr compilation which is aoqi@0: // checked with an assertion at the entry of this method. aoqi@0: if (method->is_native() && !method->is_method_handle_intrinsic()) { aoqi@0: bool in_base_library; aoqi@0: address adr = NativeLookup::lookup(method, in_base_library, THREAD); aoqi@0: if (HAS_PENDING_EXCEPTION) { aoqi@0: // In case of an exception looking up the method, we just forget aoqi@0: // about it. The interpreter will kick-in and throw the exception. aoqi@0: method->set_not_compilable(); // implies is_not_osr_compilable() aoqi@0: CLEAR_PENDING_EXCEPTION; aoqi@0: return NULL; aoqi@0: } aoqi@0: assert(method->has_native_function(), "must have native code by now"); aoqi@0: } aoqi@0: aoqi@0: // RedefineClasses() has replaced this method; just return aoqi@0: if (method->is_old()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // JVMTI -- post_compile_event requires jmethod_id() that may require aoqi@0: // a lock the compiling thread can not acquire. Prefetch it here. aoqi@0: if (JvmtiExport::should_post_compiled_method_load()) { aoqi@0: method->jmethod_id(); aoqi@0: } aoqi@0: aoqi@0: // do the compilation aoqi@0: if (method->is_native()) { aoqi@0: if (!PreferInterpreterNativeStubs || method->is_method_handle_intrinsic()) { aoqi@0: // To properly handle the appendix argument for out-of-line calls we are using a small trampoline that aoqi@0: // pops off the appendix argument and jumps to the target (see gen_special_dispatch in SharedRuntime). aoqi@0: // aoqi@0: // Since normal compiled-to-compiled calls are not able to handle such a thing we MUST generate an adapter aoqi@0: // in this case. If we can't generate one and use it we can not execute the out-of-line method handle calls. aoqi@0: AdapterHandlerLibrary::create_native_wrapper(method); aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: } else { aoqi@0: // If the compiler is shut off due to code cache getting full aoqi@0: // fail out now so blocking compiles dont hang the java thread aoqi@0: if (!should_compile_new_jobs()) { aoqi@0: CompilationPolicy::policy()->delay_compilation(method()); aoqi@0: return NULL; aoqi@0: } aoqi@0: compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, THREAD); aoqi@0: } aoqi@0: aoqi@0: // return requested nmethod aoqi@0: // We accept a higher level osr method aoqi@0: return osr_bci == InvocationEntryBci ? method->code() : method->lookup_osr_nmethod_for(osr_bci, comp_level, false); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::compilation_is_complete aoqi@0: // aoqi@0: // See if compilation of this method is already complete. aoqi@0: bool CompileBroker::compilation_is_complete(methodHandle method, aoqi@0: int osr_bci, aoqi@0: int comp_level) { aoqi@0: bool is_osr = (osr_bci != standard_entry_bci); aoqi@0: if (is_osr) { aoqi@0: if (method->is_not_osr_compilable(comp_level)) { aoqi@0: return true; aoqi@0: } else { aoqi@0: nmethod* result = method->lookup_osr_nmethod_for(osr_bci, comp_level, true); aoqi@0: return (result != NULL); aoqi@0: } aoqi@0: } else { aoqi@0: if (method->is_not_compilable(comp_level)) { aoqi@0: return true; aoqi@0: } else { aoqi@0: nmethod* result = method->code(); aoqi@0: if (result == NULL) return false; aoqi@0: return comp_level == result->comp_level(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::compilation_is_in_queue aoqi@0: // aoqi@0: // See if this compilation is already requested. aoqi@0: // aoqi@0: // Implementation note: there is only a single "is in queue" bit aoqi@0: // for each method. This means that the check below is overly aoqi@0: // conservative in the sense that an osr compilation in the queue aoqi@0: // will block a normal compilation from entering the queue (and vice aoqi@0: // versa). This can be remedied by a full queue search to disambiguate aoqi@0: // cases. If it is deemed profitible, this may be done. aoqi@0: bool CompileBroker::compilation_is_in_queue(methodHandle method, aoqi@0: int osr_bci) { aoqi@0: return method->queued_for_compilation(); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::compilation_is_prohibited aoqi@0: // aoqi@0: // See if this compilation is not allowed. aoqi@0: bool CompileBroker::compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level) { aoqi@0: bool is_native = method->is_native(); aoqi@0: // Some compilers may not support the compilation of natives. aoqi@0: AbstractCompiler *comp = compiler(comp_level); aoqi@0: if (is_native && aoqi@0: (!CICompileNatives || comp == NULL || !comp->supports_native())) { aoqi@0: method->set_not_compilable_quietly(comp_level); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: bool is_osr = (osr_bci != standard_entry_bci); aoqi@0: // Some compilers may not support on stack replacement. aoqi@0: if (is_osr && aoqi@0: (!CICompileOSR || comp == NULL || !comp->supports_osr())) { aoqi@0: method->set_not_osr_compilable(comp_level); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // The method may be explicitly excluded by the user. aoqi@0: bool quietly; aoqi@0: if (CompilerOracle::should_exclude(method, quietly)) { aoqi@0: if (!quietly) { aoqi@0: // This does not happen quietly... aoqi@0: ResourceMark rm; aoqi@0: tty->print("### Excluding %s:%s", aoqi@0: method->is_native() ? "generation of native wrapper" : "compile", aoqi@0: (method->is_static() ? " static" : "")); aoqi@0: method->print_short_name(tty); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: method->set_not_compilable(CompLevel_all, !quietly, "excluded by CompilerOracle"); aoqi@0: } aoqi@0: aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Generate serialized IDs for compilation requests. If certain debugging flags are used aoqi@0: * and the ID is not within the specified range, the method is not compiled and 0 is returned. aoqi@0: * The function also allows to generate separate compilation IDs for OSR compilations. aoqi@0: */ aoqi@0: int CompileBroker::assign_compile_id(methodHandle method, int osr_bci) { aoqi@0: #ifdef ASSERT aoqi@0: bool is_osr = (osr_bci != standard_entry_bci); aoqi@0: int id; aoqi@0: if (method->is_native()) { aoqi@0: assert(!is_osr, "can't be osr"); aoqi@0: // Adapters, native wrappers and method handle intrinsics aoqi@0: // should be generated always. aoqi@0: return Atomic::add(1, &_compilation_id); aoqi@0: } else if (CICountOSR && is_osr) { aoqi@0: id = Atomic::add(1, &_osr_compilation_id); aoqi@0: if (CIStartOSR <= id && id < CIStopOSR) { aoqi@0: return id; aoqi@0: } aoqi@0: } else { aoqi@0: id = Atomic::add(1, &_compilation_id); aoqi@0: if (CIStart <= id && id < CIStop) { aoqi@0: return id; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Method was not in the appropriate compilation range. aoqi@0: method->set_not_compilable_quietly(); aoqi@0: return 0; aoqi@0: #else aoqi@0: // CICountOSR is a develop flag and set to 'false' by default. In a product built, aoqi@0: // only _compilation_id is incremented. aoqi@0: return Atomic::add(1, &_compilation_id); aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::is_compile_blocking aoqi@0: // aoqi@0: // Should the current thread be blocked until this compilation request aoqi@0: // has been fulfilled? aoqi@0: bool CompileBroker::is_compile_blocking(methodHandle method, int osr_bci) { aoqi@0: assert(!InstanceRefKlass::owns_pending_list_lock(JavaThread::current()), "possible deadlock"); aoqi@0: return !BackgroundCompilation; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::preload_classes aoqi@0: void CompileBroker::preload_classes(methodHandle method, TRAPS) { aoqi@0: // Move this code over from c1_Compiler.cpp aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::create_compile_task aoqi@0: // aoqi@0: // Create a CompileTask object representing the current request for aoqi@0: // compilation. Add this task to the queue. aoqi@0: CompileTask* CompileBroker::create_compile_task(CompileQueue* queue, aoqi@0: int compile_id, aoqi@0: methodHandle method, aoqi@0: int osr_bci, aoqi@0: int comp_level, aoqi@0: methodHandle hot_method, aoqi@0: int hot_count, aoqi@0: const char* comment, aoqi@0: bool blocking) { aoqi@0: CompileTask* new_task = allocate_task(); aoqi@0: new_task->initialize(compile_id, method, osr_bci, comp_level, aoqi@0: hot_method, hot_count, comment, aoqi@0: blocking); aoqi@0: queue->add(new_task); aoqi@0: return new_task; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::allocate_task aoqi@0: // aoqi@0: // Allocate a CompileTask, from the free list if possible. aoqi@0: CompileTask* CompileBroker::allocate_task() { aoqi@0: MutexLocker locker(CompileTaskAlloc_lock); aoqi@0: CompileTask* task = NULL; aoqi@0: if (_task_free_list != NULL) { aoqi@0: task = _task_free_list; aoqi@0: _task_free_list = task->next(); aoqi@0: task->set_next(NULL); aoqi@0: } else { aoqi@0: task = new CompileTask(); aoqi@0: task->set_next(NULL); aoqi@0: } aoqi@0: return task; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::free_task aoqi@0: // aoqi@0: // Add a task to the free list. aoqi@0: void CompileBroker::free_task(CompileTask* task) { aoqi@0: MutexLocker locker(CompileTaskAlloc_lock); aoqi@0: task->free(); aoqi@0: task->set_next(_task_free_list); aoqi@0: _task_free_list = task; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::wait_for_completion aoqi@0: // aoqi@0: // Wait for the given method CompileTask to complete. aoqi@0: void CompileBroker::wait_for_completion(CompileTask* task) { aoqi@0: if (CIPrintCompileQueue) { aoqi@0: tty->print_cr("BLOCKING FOR COMPILE"); aoqi@0: } aoqi@0: aoqi@0: assert(task->is_blocking(), "can only wait on blocking task"); aoqi@0: aoqi@0: JavaThread *thread = JavaThread::current(); aoqi@0: thread->set_blocked_on_compilation(true); aoqi@0: aoqi@0: methodHandle method(thread, task->method()); aoqi@0: { aoqi@0: MutexLocker waiter(task->lock(), thread); aoqi@0: aoqi@0: while (!task->is_complete()) aoqi@0: task->lock()->wait(); aoqi@0: } aoqi@0: // It is harmless to check this status without the lock, because aoqi@0: // completion is a stable property (until the task object is recycled). aoqi@0: assert(task->is_complete(), "Compilation should have completed"); aoqi@0: assert(task->code_handle() == NULL, "must be reset"); aoqi@0: aoqi@0: thread->set_blocked_on_compilation(false); aoqi@0: aoqi@0: // By convention, the waiter is responsible for recycling a aoqi@0: // blocking CompileTask. Since there is only one waiter ever aoqi@0: // waiting on a CompileTask, we know that no one else will aoqi@0: // be using this CompileTask; we can free it. aoqi@0: free_task(task); aoqi@0: } aoqi@0: aoqi@0: // Initialize compiler thread(s) + compiler object(s). The postcondition aoqi@0: // of this function is that the compiler runtimes are initialized and that aoqi@0: //compiler threads can start compiling. aoqi@0: bool CompileBroker::init_compiler_runtime() { aoqi@0: CompilerThread* thread = CompilerThread::current(); aoqi@0: AbstractCompiler* comp = thread->compiler(); aoqi@0: // Final sanity check - the compiler object must exist aoqi@0: guarantee(comp != NULL, "Compiler object must exist"); aoqi@0: aoqi@0: int system_dictionary_modification_counter; aoqi@0: { aoqi@0: MutexLocker locker(Compile_lock, thread); aoqi@0: system_dictionary_modification_counter = SystemDictionary::number_of_modifications(); aoqi@0: } aoqi@0: aoqi@0: { aoqi@0: // Must switch to native to allocate ci_env aoqi@0: ThreadToNativeFromVM ttn(thread); aoqi@0: ciEnv ci_env(NULL, system_dictionary_modification_counter); aoqi@0: // Cache Jvmti state aoqi@0: ci_env.cache_jvmti_state(); aoqi@0: // Cache DTrace flags aoqi@0: ci_env.cache_dtrace_flags(); aoqi@0: aoqi@0: // Switch back to VM state to do compiler initialization aoqi@0: ThreadInVMfromNative tv(thread); aoqi@0: ResetNoHandleMark rnhm; aoqi@0: aoqi@0: aoqi@0: if (!comp->is_shark()) { aoqi@0: // Perform per-thread and global initializations aoqi@0: comp->initialize(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (comp->is_failed()) { aoqi@0: disable_compilation_forever(); aoqi@0: // If compiler initialization failed, no compiler thread that is specific to a aoqi@0: // particular compiler runtime will ever start to compile methods. aoqi@0: aoqi@0: shutdown_compiler_runtime(comp, thread); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // C1 specific check aoqi@0: if (comp->is_c1() && (thread->get_buffer_blob() == NULL)) { aoqi@0: warning("Initialization of %s thread failed (no space to run compilers)", thread->name()); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // If C1 and/or C2 initialization failed, we shut down all compilation. aoqi@0: // We do this to keep things simple. This can be changed if it ever turns out to be aoqi@0: // a problem. aoqi@0: void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) { aoqi@0: // Free buffer blob, if allocated aoqi@0: if (thread->get_buffer_blob() != NULL) { aoqi@0: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: CodeCache::free(thread->get_buffer_blob()); aoqi@0: } aoqi@0: aoqi@0: if (comp->should_perform_shutdown()) { aoqi@0: // There are two reasons for shutting down the compiler aoqi@0: // 1) compiler runtime initialization failed aoqi@0: // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing aoqi@0: warning("Shutting down compiler %s (no space to run compilers)", comp->name()); aoqi@0: aoqi@0: // Only one thread per compiler runtime object enters here aoqi@0: // Set state to shut down aoqi@0: comp->set_shut_down(); aoqi@0: aoqi@0: MutexLocker mu(MethodCompileQueue_lock, thread); aoqi@0: CompileQueue* queue; aoqi@0: if (_c1_method_queue != NULL) { aoqi@0: _c1_method_queue->delete_all(); aoqi@0: queue = _c1_method_queue; aoqi@0: _c1_method_queue = NULL; aoqi@0: delete _c1_method_queue; aoqi@0: } aoqi@0: aoqi@0: if (_c2_method_queue != NULL) { aoqi@0: _c2_method_queue->delete_all(); aoqi@0: queue = _c2_method_queue; aoqi@0: _c2_method_queue = NULL; aoqi@0: delete _c2_method_queue; aoqi@0: } aoqi@0: aoqi@0: // We could delete compiler runtimes also. However, there are references to aoqi@0: // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then aoqi@0: // fail. This can be done later if necessary. aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::compiler_thread_loop aoqi@0: // aoqi@0: // The main loop run by a CompilerThread. aoqi@0: void CompileBroker::compiler_thread_loop() { aoqi@0: CompilerThread* thread = CompilerThread::current(); aoqi@0: CompileQueue* queue = thread->queue(); aoqi@0: // For the thread that initializes the ciObjectFactory aoqi@0: // this resource mark holds all the shared objects aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: // First thread to get here will initialize the compiler interface aoqi@0: aoqi@0: if (!ciObjectFactory::is_initialized()) { aoqi@0: ASSERT_IN_VM; aoqi@0: MutexLocker only_one (CompileThread_lock, thread); aoqi@0: if (!ciObjectFactory::is_initialized()) { aoqi@0: ciObjectFactory::initialize(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Open a log. aoqi@0: if (LogCompilation) { aoqi@0: init_compiler_thread_log(); aoqi@0: } aoqi@0: CompileLog* log = thread->log(); aoqi@0: if (log != NULL) { aoqi@0: log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'", aoqi@0: thread->name(), aoqi@0: os::current_thread_id(), aoqi@0: os::current_process_id()); aoqi@0: log->stamp(); aoqi@0: log->end_elem(); aoqi@0: } aoqi@0: aoqi@0: // If compiler thread/runtime initialization fails, exit the compiler thread aoqi@0: if (!init_compiler_runtime()) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Poll for new compilation tasks as long as the JVM runs. Compilation aoqi@0: // should only be disabled if something went wrong while initializing the aoqi@0: // compiler runtimes. This, in turn, should not happen. The only known case aoqi@0: // when compiler runtime initialization fails is if there is not enough free aoqi@0: // space in the code cache to generate the necessary stubs, etc. aoqi@0: while (!is_compilation_disabled_forever()) { aoqi@0: // We need this HandleMark to avoid leaking VM handles. aoqi@0: HandleMark hm(thread); aoqi@0: aoqi@0: if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) { aoqi@0: // the code cache is really full aoqi@0: handle_full_code_cache(); aoqi@0: } aoqi@0: aoqi@0: CompileTask* task = queue->get(); aoqi@0: if (task == NULL) { aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: // Give compiler threads an extra quanta. They tend to be bursty and aoqi@0: // this helps the compiler to finish up the job. aoqi@0: if( CompilerThreadHintNoPreempt ) aoqi@0: os::hint_no_preempt(); aoqi@0: aoqi@0: // trace per thread time and compile statistics aoqi@0: CompilerCounters* counters = ((CompilerThread*)thread)->counters(); aoqi@0: PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter()); aoqi@0: aoqi@0: // Assign the task to the current thread. Mark this compilation aoqi@0: // thread as active for the profiler. aoqi@0: CompileTaskWrapper ctw(task); aoqi@0: nmethodLocker result_handle; // (handle for the nmethod produced by this task) aoqi@0: task->set_code_handle(&result_handle); aoqi@0: methodHandle method(thread, task->method()); aoqi@0: aoqi@0: // Never compile a method if breakpoints are present in it aoqi@0: if (method()->number_of_breakpoints() == 0) { aoqi@0: // Compile the method. aoqi@0: if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) { aoqi@0: #ifdef COMPILER1 aoqi@0: // Allow repeating compilations for the purpose of benchmarking aoqi@0: // compile speed. This is not useful for customers. aoqi@0: if (CompilationRepeat != 0) { aoqi@0: int compile_count = CompilationRepeat; aoqi@0: while (compile_count > 0) { aoqi@0: invoke_compiler_on_method(task); aoqi@0: nmethod* nm = method->code(); aoqi@0: if (nm != NULL) { aoqi@0: nm->make_zombie(); aoqi@0: method->clear_code(); aoqi@0: } aoqi@0: compile_count--; aoqi@0: } aoqi@0: } aoqi@0: #endif /* COMPILER1 */ aoqi@0: invoke_compiler_on_method(task); aoqi@0: } else { aoqi@0: // After compilation is disabled, remove remaining methods from queue aoqi@0: method->clear_queued_for_compilation(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Shut down compiler runtime aoqi@0: shutdown_compiler_runtime(thread->compiler(), thread); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::init_compiler_thread_log aoqi@0: // aoqi@0: // Set up state required by +LogCompilation. aoqi@0: void CompileBroker::init_compiler_thread_log() { aoqi@0: CompilerThread* thread = CompilerThread::current(); aoqi@0: char file_name[4*K]; aoqi@0: FILE* fp = NULL; aoqi@0: intx thread_id = os::current_thread_id(); aoqi@0: for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) { aoqi@0: const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL); aoqi@0: if (dir == NULL) { aoqi@0: jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log", aoqi@0: thread_id, os::current_process_id()); aoqi@0: } else { aoqi@0: jio_snprintf(file_name, sizeof(file_name), aoqi@0: "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir, aoqi@0: os::file_separator(), thread_id, os::current_process_id()); aoqi@0: } aoqi@0: aoqi@0: fp = fopen(file_name, "at"); aoqi@0: if (fp != NULL) { aoqi@0: if (LogCompilation && Verbose) { aoqi@0: tty->print_cr("Opening compilation log %s", file_name); aoqi@0: } aoqi@0: CompileLog* log = new(ResourceObj::C_HEAP, mtCompiler) CompileLog(file_name, fp, thread_id); aoqi@0: thread->init_log(log); aoqi@0: aoqi@0: if (xtty != NULL) { aoqi@0: ttyLocker ttyl; aoqi@0: // Record any per thread log files aoqi@0: xtty->elem("thread_logfile thread='" INTX_FORMAT "' filename='%s'", thread_id, file_name); aoqi@0: } aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: warning("Cannot open log file: %s", file_name); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::set_should_block aoqi@0: // aoqi@0: // Set _should_block. aoqi@0: // Call this from the VM, with Threads_lock held and a safepoint requested. aoqi@0: void CompileBroker::set_should_block() { aoqi@0: assert(Threads_lock->owner() == Thread::current(), "must have threads lock"); aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint already"); aoqi@0: #ifndef PRODUCT aoqi@0: if (PrintCompilation && (Verbose || WizardMode)) aoqi@0: tty->print_cr("notifying compiler thread pool to block"); aoqi@0: #endif aoqi@0: _should_block = true; aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::maybe_block aoqi@0: // aoqi@0: // Call this from the compiler at convenient points, to poll for _should_block. aoqi@0: void CompileBroker::maybe_block() { aoqi@0: if (_should_block) { aoqi@0: #ifndef PRODUCT aoqi@0: if (PrintCompilation && (Verbose || WizardMode)) aoqi@0: tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current())); aoqi@0: #endif aoqi@0: ThreadInVMfromNative tivfn(JavaThread::current()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // wrapper for CodeCache::print_summary() aoqi@0: static void codecache_print(bool detailed) aoqi@0: { aoqi@0: ResourceMark rm; aoqi@0: stringStream s; aoqi@0: // Dump code cache into a buffer before locking the tty, aoqi@0: { aoqi@0: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: CodeCache::print_summary(&s, detailed); aoqi@0: } aoqi@0: ttyLocker ttyl; aoqi@0: tty->print("%s", s.as_string()); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::invoke_compiler_on_method aoqi@0: // aoqi@0: // Compile a method. aoqi@0: // aoqi@0: void CompileBroker::invoke_compiler_on_method(CompileTask* task) { aoqi@0: if (PrintCompilation) { aoqi@0: ResourceMark rm; aoqi@0: task->print_line(); aoqi@0: } aoqi@0: elapsedTimer time; aoqi@0: aoqi@0: CompilerThread* thread = CompilerThread::current(); aoqi@0: ResourceMark rm(thread); aoqi@0: aoqi@0: if (LogEvents) { aoqi@0: _compilation_log->log_compile(thread, task); aoqi@0: } aoqi@0: aoqi@0: // Common flags. aoqi@0: uint compile_id = task->compile_id(); aoqi@0: int osr_bci = task->osr_bci(); aoqi@0: bool is_osr = (osr_bci != standard_entry_bci); aoqi@0: bool should_log = (thread->log() != NULL); aoqi@0: bool should_break = false; aoqi@0: int task_level = task->comp_level(); aoqi@0: { aoqi@0: // create the handle inside it's own block so it can't aoqi@0: // accidentally be referenced once the thread transitions to aoqi@0: // native. The NoHandleMark before the transition should catch aoqi@0: // any cases where this occurs in the future. aoqi@0: methodHandle method(thread, task->method()); aoqi@0: should_break = check_break_at(method, compile_id, is_osr); aoqi@0: if (should_log && !CompilerOracle::should_log(method)) { aoqi@0: should_log = false; aoqi@0: } aoqi@0: assert(!method->is_native(), "no longer compile natives"); aoqi@0: aoqi@0: // Save information about this method in case of failure. aoqi@0: set_last_compile(thread, method, is_osr, task_level); aoqi@0: aoqi@0: DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level)); aoqi@0: } aoqi@0: aoqi@0: // Allocate a new set of JNI handles. aoqi@0: push_jni_handle_block(); aoqi@0: Method* target_handle = task->method(); aoqi@0: int compilable = ciEnv::MethodCompilable; aoqi@0: { aoqi@0: int system_dictionary_modification_counter; aoqi@0: { aoqi@0: MutexLocker locker(Compile_lock, thread); aoqi@0: system_dictionary_modification_counter = SystemDictionary::number_of_modifications(); aoqi@0: } aoqi@0: aoqi@0: NoHandleMark nhm; aoqi@0: ThreadToNativeFromVM ttn(thread); aoqi@0: aoqi@0: ciEnv ci_env(task, system_dictionary_modification_counter); aoqi@0: if (should_break) { aoqi@0: ci_env.set_break_at_compile(true); aoqi@0: } aoqi@0: if (should_log) { aoqi@0: ci_env.set_log(thread->log()); aoqi@0: } aoqi@0: assert(thread->env() == &ci_env, "set by ci_env"); aoqi@0: // The thread-env() field is cleared in ~CompileTaskWrapper. aoqi@0: aoqi@0: // Cache Jvmti state aoqi@0: ci_env.cache_jvmti_state(); aoqi@0: aoqi@0: // Cache DTrace flags aoqi@0: ci_env.cache_dtrace_flags(); aoqi@0: aoqi@0: ciMethod* target = ci_env.get_method_from_handle(target_handle); aoqi@0: aoqi@0: TraceTime t1("compilation", &time); aoqi@0: EventCompilation event; aoqi@0: aoqi@0: AbstractCompiler *comp = compiler(task_level); aoqi@0: if (comp == NULL) { aoqi@0: ci_env.record_method_not_compilable("no compiler", !TieredCompilation); aoqi@0: } else { aoqi@0: comp->compile_method(&ci_env, target, osr_bci); aoqi@0: } aoqi@0: aoqi@0: if (!ci_env.failing() && task->code() == NULL) { aoqi@0: //assert(false, "compiler should always document failure"); aoqi@0: // The compiler elected, without comment, not to register a result. aoqi@0: // Do not attempt further compilations of this method. aoqi@0: ci_env.record_method_not_compilable("compile failed", !TieredCompilation); aoqi@0: } aoqi@0: aoqi@0: // Copy this bit to the enclosing block: aoqi@0: compilable = ci_env.compilable(); aoqi@0: aoqi@0: if (ci_env.failing()) { aoqi@0: const char* retry_message = ci_env.retry_message(); aoqi@0: if (_compilation_log != NULL) { aoqi@0: _compilation_log->log_failure(thread, task, ci_env.failure_reason(), retry_message); aoqi@0: } aoqi@0: if (PrintCompilation) { aoqi@0: FormatBufferResource msg = retry_message != NULL ? aoqi@0: err_msg_res("COMPILE SKIPPED: %s (%s)", ci_env.failure_reason(), retry_message) : aoqi@0: err_msg_res("COMPILE SKIPPED: %s", ci_env.failure_reason()); aoqi@0: task->print_compilation(tty, msg); aoqi@0: } aoqi@0: } else { aoqi@0: task->mark_success(); aoqi@0: task->set_num_inlined_bytecodes(ci_env.num_inlined_bytecodes()); aoqi@0: if (_compilation_log != NULL) { aoqi@0: nmethod* code = task->code(); aoqi@0: if (code != NULL) { aoqi@0: _compilation_log->log_nmethod(thread, code); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // simulate crash during compilation aoqi@0: assert(task->compile_id() != CICrashAt, "just as planned"); aoqi@0: if (event.should_commit()) { aoqi@0: event.set_method(target->get_Method()); aoqi@0: event.set_compileID(compile_id); aoqi@0: event.set_compileLevel(task->comp_level()); aoqi@0: event.set_succeded(task->is_success()); aoqi@0: event.set_isOsr(is_osr); aoqi@0: event.set_codeSize((task->code() == NULL) ? 0 : task->code()->total_size()); aoqi@0: event.set_inlinedBytes(task->num_inlined_bytecodes()); aoqi@0: event.commit(); aoqi@0: } aoqi@0: } aoqi@0: pop_jni_handle_block(); aoqi@0: aoqi@0: methodHandle method(thread, task->method()); aoqi@0: aoqi@0: DTRACE_METHOD_COMPILE_END_PROBE(method, compiler_name(task_level), task->is_success()); aoqi@0: aoqi@0: collect_statistics(thread, time, task); aoqi@0: aoqi@0: if (PrintCompilation && PrintCompilation2) { aoqi@0: tty->print("%7d ", (int) tty->time_stamp().milliseconds()); // print timestamp aoqi@0: tty->print("%4d ", compile_id); // print compilation number aoqi@0: tty->print("%s ", (is_osr ? "%" : " ")); aoqi@0: if (task->code() != NULL) { aoqi@0: tty->print("size: %d(%d) ", task->code()->total_size(), task->code()->insts_size()); aoqi@0: } aoqi@0: tty->print_cr("time: %d inlined: %d bytes", (int)time.milliseconds(), task->num_inlined_bytecodes()); aoqi@0: } aoqi@0: aoqi@0: if (PrintCodeCacheOnCompilation) aoqi@0: codecache_print(/* detailed= */ false); aoqi@0: aoqi@0: // Disable compilation, if required. aoqi@0: switch (compilable) { aoqi@0: case ciEnv::MethodCompilable_never: aoqi@0: if (is_osr) aoqi@0: method->set_not_osr_compilable_quietly(); aoqi@0: else aoqi@0: method->set_not_compilable_quietly(); aoqi@0: break; aoqi@0: case ciEnv::MethodCompilable_not_at_tier: aoqi@0: if (is_osr) aoqi@0: method->set_not_osr_compilable_quietly(task_level); aoqi@0: else aoqi@0: method->set_not_compilable_quietly(task_level); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: // Note that the queued_for_compilation bits are cleared without aoqi@0: // protection of a mutex. [They were set by the requester thread, aoqi@0: // when adding the task to the complie queue -- at which time the aoqi@0: // compile queue lock was held. Subsequently, we acquired the compile aoqi@0: // queue lock to get this task off the compile queue; thus (to belabour aoqi@0: // the point somewhat) our clearing of the bits must be occurring aoqi@0: // only after the setting of the bits. See also 14012000 above. aoqi@0: method->clear_queued_for_compilation(); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: if (CollectedHeap::fired_fake_oom()) { aoqi@0: // The current compile received a fake OOM during compilation so aoqi@0: // go ahead and exit the VM since the test apparently succeeded aoqi@0: tty->print_cr("*** Shutting down VM after successful fake OOM"); aoqi@0: vm_exit(0); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * The CodeCache is full. Print out warning and disable compilation aoqi@0: * or try code cache cleaning so compilation can continue later. aoqi@0: */ aoqi@0: void CompileBroker::handle_full_code_cache() { aoqi@0: UseInterpreter = true; aoqi@0: if (UseCompiler || AlwaysCompileLoopMethods ) { aoqi@0: if (xtty != NULL) { aoqi@0: ResourceMark rm; aoqi@0: stringStream s; aoqi@0: // Dump code cache state into a buffer before locking the tty, aoqi@0: // because log_state() will use locks causing lock conflicts. aoqi@0: CodeCache::log_state(&s); aoqi@0: // Lock to prevent tearing aoqi@0: ttyLocker ttyl; aoqi@0: xtty->begin_elem("code_cache_full"); aoqi@0: xtty->print("%s", s.as_string()); aoqi@0: xtty->stamp(); aoqi@0: xtty->end_elem(); aoqi@0: } aoqi@0: aoqi@0: CodeCache::report_codemem_full(); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (CompileTheWorld || ExitOnFullCodeCache) { aoqi@0: codecache_print(/* detailed= */ true); aoqi@0: before_exit(JavaThread::current()); aoqi@0: exit_globals(); // will delete tty aoqi@0: vm_direct_exit(CompileTheWorld ? 0 : 1); aoqi@0: } aoqi@0: #endif aoqi@0: if (UseCodeCacheFlushing) { aoqi@0: // Since code cache is full, immediately stop new compiles aoqi@0: if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) { aoqi@0: NMethodSweeper::log_sweep("disable_compiler"); aoqi@0: } aoqi@0: // Switch to 'vm_state'. This ensures that possibly_sweep() can be called aoqi@0: // without having to consider the state in which the current thread is. aoqi@0: ThreadInVMfromUnknown in_vm; aoqi@0: NMethodSweeper::possibly_sweep(); aoqi@0: } else { aoqi@0: disable_compilation_forever(); aoqi@0: } aoqi@0: aoqi@0: // Print warning only once aoqi@0: if (should_print_compiler_warning()) { aoqi@0: warning("CodeCache is full. Compiler has been disabled."); aoqi@0: warning("Try increasing the code cache size using -XX:ReservedCodeCacheSize="); aoqi@0: codecache_print(/* detailed= */ true); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::set_last_compile aoqi@0: // aoqi@0: // Record this compilation for debugging purposes. aoqi@0: void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) { aoqi@0: ResourceMark rm; aoqi@0: char* method_name = method->name()->as_C_string(); aoqi@0: strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length); aoqi@0: _last_method_compiled[CompileBroker::name_buffer_length - 1] = '\0'; // ensure null terminated aoqi@0: char current_method[CompilerCounters::cmname_buffer_length]; aoqi@0: size_t maxLen = CompilerCounters::cmname_buffer_length; aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: const char* class_name = method->method_holder()->name()->as_C_string(); aoqi@0: aoqi@0: size_t s1len = strlen(class_name); aoqi@0: size_t s2len = strlen(method_name); aoqi@0: aoqi@0: // check if we need to truncate the string aoqi@0: if (s1len + s2len + 2 > maxLen) { aoqi@0: aoqi@0: // the strategy is to lop off the leading characters of the aoqi@0: // class name and the trailing characters of the method name. aoqi@0: aoqi@0: if (s2len + 2 > maxLen) { aoqi@0: // lop of the entire class name string, let snprintf handle aoqi@0: // truncation of the method name. aoqi@0: class_name += s1len; // null string aoqi@0: } aoqi@0: else { aoqi@0: // lop off the extra characters from the front of the class name aoqi@0: class_name += ((s1len + s2len + 2) - maxLen); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: jio_snprintf(current_method, maxLen, "%s %s", class_name, method_name); aoqi@0: } aoqi@0: aoqi@0: if (CICountOSR && is_osr) { aoqi@0: _last_compile_type = osr_compile; aoqi@0: } else { aoqi@0: _last_compile_type = normal_compile; aoqi@0: } aoqi@0: _last_compile_level = comp_level; aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: CompilerCounters* counters = thread->counters(); aoqi@0: counters->set_current_method(current_method); aoqi@0: counters->set_compile_type((jlong)_last_compile_type); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::push_jni_handle_block aoqi@0: // aoqi@0: // Push on a new block of JNI handles. aoqi@0: void CompileBroker::push_jni_handle_block() { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: aoqi@0: // Allocate a new block for JNI handles. aoqi@0: // Inlined code from jni_PushLocalFrame() aoqi@0: JNIHandleBlock* java_handles = thread->active_handles(); aoqi@0: JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread); aoqi@0: assert(compile_handles != NULL && java_handles != NULL, "should not be NULL"); aoqi@0: compile_handles->set_pop_frame_link(java_handles); // make sure java handles get gc'd. aoqi@0: thread->set_active_handles(compile_handles); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::pop_jni_handle_block aoqi@0: // aoqi@0: // Pop off the current block of JNI handles. aoqi@0: void CompileBroker::pop_jni_handle_block() { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: aoqi@0: // Release our JNI handle block aoqi@0: JNIHandleBlock* compile_handles = thread->active_handles(); aoqi@0: JNIHandleBlock* java_handles = compile_handles->pop_frame_link(); aoqi@0: thread->set_active_handles(java_handles); aoqi@0: compile_handles->set_pop_frame_link(NULL); aoqi@0: JNIHandleBlock::release_block(compile_handles, thread); // may block aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::check_break_at aoqi@0: // aoqi@0: // Should the compilation break at the current compilation. aoqi@0: bool CompileBroker::check_break_at(methodHandle method, int compile_id, bool is_osr) { aoqi@0: if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) { aoqi@0: return true; aoqi@0: } else if( CompilerOracle::should_break_at(method) ) { // break when compiling aoqi@0: return true; aoqi@0: } else { aoqi@0: return (compile_id == CIBreakAt); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileBroker::collect_statistics aoqi@0: // aoqi@0: // Collect statistics about the compilation. aoqi@0: aoqi@0: void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) { aoqi@0: bool success = task->is_success(); aoqi@0: methodHandle method (thread, task->method()); aoqi@0: uint compile_id = task->compile_id(); aoqi@0: bool is_osr = (task->osr_bci() != standard_entry_bci); aoqi@0: nmethod* code = task->code(); aoqi@0: CompilerCounters* counters = thread->counters(); aoqi@0: aoqi@0: assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker"); aoqi@0: MutexLocker locker(CompileStatistics_lock); aoqi@0: aoqi@0: // _perf variables are production performance counters which are aoqi@0: // updated regardless of the setting of the CITime and CITimeEach flags aoqi@0: // aoqi@0: if (!success) { aoqi@0: _total_bailout_count++; aoqi@0: if (UsePerfData) { aoqi@0: _perf_last_failed_method->set_value(counters->current_method()); aoqi@0: _perf_last_failed_type->set_value(counters->compile_type()); aoqi@0: _perf_total_bailout_count->inc(); aoqi@0: } aoqi@0: } else if (code == NULL) { aoqi@0: if (UsePerfData) { aoqi@0: _perf_last_invalidated_method->set_value(counters->current_method()); aoqi@0: _perf_last_invalidated_type->set_value(counters->compile_type()); aoqi@0: _perf_total_invalidated_count->inc(); aoqi@0: } aoqi@0: _total_invalidated_count++; aoqi@0: } else { aoqi@0: // Compilation succeeded aoqi@0: aoqi@0: // update compilation ticks - used by the implementation of aoqi@0: // java.lang.management.CompilationMBean aoqi@0: _perf_total_compilation->inc(time.ticks()); aoqi@0: aoqi@0: _t_total_compilation.add(time); aoqi@0: _peak_compilation_time = time.milliseconds() > _peak_compilation_time ? time.milliseconds() : _peak_compilation_time; aoqi@0: aoqi@0: if (CITime) { aoqi@0: if (is_osr) { aoqi@0: _t_osr_compilation.add(time); aoqi@0: _sum_osr_bytes_compiled += method->code_size() + task->num_inlined_bytecodes(); aoqi@0: } else { aoqi@0: _t_standard_compilation.add(time); aoqi@0: _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: // save the name of the last method compiled aoqi@0: _perf_last_method->set_value(counters->current_method()); aoqi@0: _perf_last_compile_type->set_value(counters->compile_type()); aoqi@0: _perf_last_compile_size->set_value(method->code_size() + aoqi@0: task->num_inlined_bytecodes()); aoqi@0: if (is_osr) { aoqi@0: _perf_osr_compilation->inc(time.ticks()); aoqi@0: _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes()); aoqi@0: } else { aoqi@0: _perf_standard_compilation->inc(time.ticks()); aoqi@0: _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (CITimeEach) { aoqi@0: float bytes_per_sec = 1.0 * (method->code_size() + task->num_inlined_bytecodes()) / time.seconds(); aoqi@0: tty->print_cr("%3d seconds: %f bytes/sec : %f (bytes %d + %d inlined)", aoqi@0: compile_id, time.seconds(), bytes_per_sec, method->code_size(), task->num_inlined_bytecodes()); aoqi@0: } aoqi@0: aoqi@0: // Collect counts of successful compilations aoqi@0: _sum_nmethod_size += code->total_size(); aoqi@0: _sum_nmethod_code_size += code->insts_size(); aoqi@0: _total_compile_count++; aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: _perf_sum_nmethod_size->inc( code->total_size()); aoqi@0: _perf_sum_nmethod_code_size->inc(code->insts_size()); aoqi@0: _perf_total_compile_count->inc(); aoqi@0: } aoqi@0: aoqi@0: if (is_osr) { aoqi@0: if (UsePerfData) _perf_total_osr_compile_count->inc(); aoqi@0: _total_osr_compile_count++; aoqi@0: } else { aoqi@0: if (UsePerfData) _perf_total_standard_compile_count->inc(); aoqi@0: _total_standard_compile_count++; aoqi@0: } aoqi@0: } aoqi@0: // set the current method for the thread to null aoqi@0: if (UsePerfData) counters->set_current_method(""); aoqi@0: } aoqi@0: aoqi@0: const char* CompileBroker::compiler_name(int comp_level) { aoqi@0: AbstractCompiler *comp = CompileBroker::compiler(comp_level); aoqi@0: if (comp == NULL) { aoqi@0: return "no compiler"; aoqi@0: } else { aoqi@0: return (comp->name()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void CompileBroker::print_times() { aoqi@0: tty->cr(); aoqi@0: tty->print_cr("Accumulated compiler times (for compiled methods only)"); aoqi@0: tty->print_cr("------------------------------------------------"); aoqi@0: //0000000000111111111122222222223333333333444444444455555555556666666666 aoqi@0: //0123456789012345678901234567890123456789012345678901234567890123456789 aoqi@0: tty->print_cr(" Total compilation time : %6.3f s", CompileBroker::_t_total_compilation.seconds()); aoqi@0: tty->print_cr(" Standard compilation : %6.3f s, Average : %2.3f", aoqi@0: CompileBroker::_t_standard_compilation.seconds(), aoqi@0: CompileBroker::_t_standard_compilation.seconds() / CompileBroker::_total_standard_compile_count); aoqi@0: tty->print_cr(" On stack replacement : %6.3f s, Average : %2.3f", CompileBroker::_t_osr_compilation.seconds(), CompileBroker::_t_osr_compilation.seconds() / CompileBroker::_total_osr_compile_count); aoqi@0: aoqi@0: AbstractCompiler *comp = compiler(CompLevel_simple); aoqi@0: if (comp != NULL) { aoqi@0: comp->print_timers(); aoqi@0: } aoqi@0: comp = compiler(CompLevel_full_optimization); aoqi@0: if (comp != NULL) { aoqi@0: comp->print_timers(); aoqi@0: } aoqi@0: tty->cr(); aoqi@0: tty->print_cr(" Total compiled methods : %6d methods", CompileBroker::_total_compile_count); aoqi@0: tty->print_cr(" Standard compilation : %6d methods", CompileBroker::_total_standard_compile_count); aoqi@0: tty->print_cr(" On stack replacement : %6d methods", CompileBroker::_total_osr_compile_count); aoqi@0: int tcb = CompileBroker::_sum_osr_bytes_compiled + CompileBroker::_sum_standard_bytes_compiled; aoqi@0: tty->print_cr(" Total compiled bytecodes : %6d bytes", tcb); aoqi@0: tty->print_cr(" Standard compilation : %6d bytes", CompileBroker::_sum_standard_bytes_compiled); aoqi@0: tty->print_cr(" On stack replacement : %6d bytes", CompileBroker::_sum_osr_bytes_compiled); aoqi@0: int bps = (int)(tcb / CompileBroker::_t_total_compilation.seconds()); aoqi@0: tty->print_cr(" Average compilation speed: %6d bytes/s", bps); aoqi@0: tty->cr(); aoqi@0: tty->print_cr(" nmethod code size : %6d bytes", CompileBroker::_sum_nmethod_code_size); aoqi@0: tty->print_cr(" nmethod total size : %6d bytes", CompileBroker::_sum_nmethod_size); aoqi@0: } aoqi@0: aoqi@0: // Debugging output for failure aoqi@0: void CompileBroker::print_last_compile() { aoqi@0: if ( _last_compile_level != CompLevel_none && aoqi@0: compiler(_last_compile_level) != NULL && aoqi@0: _last_method_compiled != NULL && aoqi@0: _last_compile_type != no_compile) { aoqi@0: if (_last_compile_type == osr_compile) { aoqi@0: tty->print_cr("Last parse: [osr]%d+++(%d) %s", aoqi@0: _osr_compilation_id, _last_compile_level, _last_method_compiled); aoqi@0: } else { aoqi@0: tty->print_cr("Last parse: %d+++(%d) %s", aoqi@0: _compilation_id, _last_compile_level, _last_method_compiled); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CompileBroker::print_compiler_threads_on(outputStream* st) { aoqi@0: #ifndef PRODUCT aoqi@0: st->print_cr("Compiler thread printing unimplemented."); aoqi@0: st->cr(); aoqi@0: #endif aoqi@0: }