duke@435: /* twisti@2563: * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "classfile/vmSymbols.hpp" stefank@2314: #include "code/codeCache.hpp" stefank@2314: #include "compiler/compileBroker.hpp" stefank@2314: #include "compiler/compileLog.hpp" stefank@2314: #include "compiler/compilerOracle.hpp" stefank@2314: #include "interpreter/linkResolver.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "oops/methodDataOop.hpp" stefank@2314: #include "oops/methodOop.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "prims/nativeLookup.hpp" stefank@2314: #include "runtime/arguments.hpp" stefank@2314: #include "runtime/compilationPolicy.hpp" stefank@2314: #include "runtime/init.hpp" stefank@2314: #include "runtime/interfaceSupport.hpp" stefank@2314: #include "runtime/javaCalls.hpp" stefank@2314: #include "runtime/os.hpp" stefank@2314: #include "runtime/sharedRuntime.hpp" stefank@2314: #include "runtime/sweeper.hpp" stefank@2314: #include "utilities/dtrace.hpp" stefank@2314: #ifdef COMPILER1 stefank@2314: #include "c1/c1_Compiler.hpp" stefank@2314: #endif stefank@2314: #ifdef COMPILER2 stefank@2314: #include "opto/c2compiler.hpp" stefank@2314: #endif stefank@2314: #ifdef SHARK stefank@2314: #include "shark/sharkCompiler.hpp" stefank@2314: #endif duke@435: duke@435: #ifdef DTRACE_ENABLED duke@435: duke@435: // Only bother with this argument setup if dtrace is available duke@435: duke@435: HS_DTRACE_PROBE_DECL8(hotspot, method__compile__begin, duke@435: char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t); duke@435: HS_DTRACE_PROBE_DECL9(hotspot, method__compile__end, duke@435: char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t, bool); duke@435: duke@435: #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(compiler, method) \ duke@435: { \ duke@435: char* comp_name = (char*)(compiler)->name(); \ coleenp@2497: Symbol* klass_name = (method)->klass_name(); \ coleenp@2497: Symbol* name = (method)->name(); \ coleenp@2497: Symbol* signature = (method)->signature(); \ duke@435: HS_DTRACE_PROBE8(hotspot, method__compile__begin, \ duke@435: comp_name, strlen(comp_name), \ duke@435: klass_name->bytes(), klass_name->utf8_length(), \ duke@435: name->bytes(), name->utf8_length(), \ duke@435: signature->bytes(), signature->utf8_length()); \ duke@435: } duke@435: duke@435: #define DTRACE_METHOD_COMPILE_END_PROBE(compiler, method, success) \ duke@435: { \ duke@435: char* comp_name = (char*)(compiler)->name(); \ coleenp@2497: Symbol* klass_name = (method)->klass_name(); \ coleenp@2497: Symbol* name = (method)->name(); \ coleenp@2497: Symbol* signature = (method)->signature(); \ duke@435: HS_DTRACE_PROBE9(hotspot, method__compile__end, \ duke@435: comp_name, strlen(comp_name), \ duke@435: klass_name->bytes(), klass_name->utf8_length(), \ duke@435: name->bytes(), name->utf8_length(), \ duke@435: signature->bytes(), signature->utf8_length(), (success)); \ duke@435: } duke@435: duke@435: #else // ndef DTRACE_ENABLED duke@435: duke@435: #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(compiler, method) duke@435: #define DTRACE_METHOD_COMPILE_END_PROBE(compiler, method, success) duke@435: duke@435: #endif // ndef DTRACE_ENABLED duke@435: duke@435: bool CompileBroker::_initialized = false; duke@435: volatile bool CompileBroker::_should_block = false; kvn@1637: volatile jint CompileBroker::_should_compile_new_jobs = run_compilation; duke@435: duke@435: // The installed compiler(s) duke@435: AbstractCompiler* CompileBroker::_compilers[2]; duke@435: duke@435: // These counters are used for assigning id's to each compilation duke@435: uint CompileBroker::_compilation_id = 0; duke@435: uint CompileBroker::_osr_compilation_id = 0; duke@435: duke@435: // Debugging information duke@435: int CompileBroker::_last_compile_type = no_compile; duke@435: int CompileBroker::_last_compile_level = CompLevel_none; duke@435: char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length]; duke@435: duke@435: // Performance counters duke@435: PerfCounter* CompileBroker::_perf_total_compilation = NULL; duke@435: PerfCounter* CompileBroker::_perf_osr_compilation = NULL; duke@435: PerfCounter* CompileBroker::_perf_standard_compilation = NULL; duke@435: duke@435: PerfCounter* CompileBroker::_perf_total_bailout_count = NULL; duke@435: PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL; duke@435: PerfCounter* CompileBroker::_perf_total_compile_count = NULL; duke@435: PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL; duke@435: PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL; duke@435: duke@435: PerfCounter* CompileBroker::_perf_sum_osr_bytes_compiled = NULL; duke@435: PerfCounter* CompileBroker::_perf_sum_standard_bytes_compiled = NULL; duke@435: PerfCounter* CompileBroker::_perf_sum_nmethod_size = NULL; duke@435: PerfCounter* CompileBroker::_perf_sum_nmethod_code_size = NULL; duke@435: duke@435: PerfStringVariable* CompileBroker::_perf_last_method = NULL; duke@435: PerfStringVariable* CompileBroker::_perf_last_failed_method = NULL; duke@435: PerfStringVariable* CompileBroker::_perf_last_invalidated_method = NULL; duke@435: PerfVariable* CompileBroker::_perf_last_compile_type = NULL; duke@435: PerfVariable* CompileBroker::_perf_last_compile_size = NULL; duke@435: PerfVariable* CompileBroker::_perf_last_failed_type = NULL; duke@435: PerfVariable* CompileBroker::_perf_last_invalidated_type = NULL; duke@435: duke@435: // Timers and counters for generating statistics duke@435: elapsedTimer CompileBroker::_t_total_compilation; duke@435: elapsedTimer CompileBroker::_t_osr_compilation; duke@435: elapsedTimer CompileBroker::_t_standard_compilation; duke@435: duke@435: int CompileBroker::_total_bailout_count = 0; duke@435: int CompileBroker::_total_invalidated_count = 0; duke@435: int CompileBroker::_total_compile_count = 0; duke@435: int CompileBroker::_total_osr_compile_count = 0; duke@435: int CompileBroker::_total_standard_compile_count = 0; duke@435: duke@435: int CompileBroker::_sum_osr_bytes_compiled = 0; duke@435: int CompileBroker::_sum_standard_bytes_compiled = 0; duke@435: int CompileBroker::_sum_nmethod_size = 0; duke@435: int CompileBroker::_sum_nmethod_code_size = 0; duke@435: iveresov@2138: CompileQueue* CompileBroker::_c2_method_queue = NULL; iveresov@2138: CompileQueue* CompileBroker::_c1_method_queue = NULL; duke@435: CompileTask* CompileBroker::_task_free_list = NULL; duke@435: duke@435: GrowableArray* CompileBroker::_method_threads = NULL; duke@435: duke@435: duke@435: CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) { duke@435: CompilerThread* thread = CompilerThread::current(); duke@435: thread->set_task(task); duke@435: CompileLog* log = thread->log(); duke@435: if (log != NULL) task->log_task_start(log); duke@435: } duke@435: duke@435: CompileTaskWrapper::~CompileTaskWrapper() { duke@435: CompilerThread* thread = CompilerThread::current(); duke@435: CompileTask* task = thread->task(); duke@435: CompileLog* log = thread->log(); duke@435: if (log != NULL) task->log_task_done(log); duke@435: thread->set_task(NULL); duke@435: task->set_code_handle(NULL); duke@435: DEBUG_ONLY(thread->set_env((ciEnv*)badAddress)); duke@435: if (task->is_blocking()) { duke@435: MutexLocker notifier(task->lock(), thread); duke@435: task->mark_complete(); duke@435: // Notify the waiting thread that the compilation has completed. duke@435: task->lock()->notify_all(); duke@435: } else { duke@435: task->mark_complete(); duke@435: duke@435: // By convention, the compiling thread is responsible for duke@435: // recycling a non-blocking CompileTask. duke@435: CompileBroker::free_task(task); duke@435: } duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::initialize duke@435: void CompileTask::initialize(int compile_id, duke@435: methodHandle method, duke@435: int osr_bci, duke@435: int comp_level, duke@435: methodHandle hot_method, duke@435: int hot_count, duke@435: const char* comment, duke@435: bool is_blocking) { duke@435: assert(!_lock->is_locked(), "bad locking"); duke@435: duke@435: _compile_id = compile_id; duke@435: _method = JNIHandles::make_global(method); duke@435: _osr_bci = osr_bci; duke@435: _is_blocking = is_blocking; duke@435: _comp_level = comp_level; duke@435: _num_inlined_bytecodes = 0; duke@435: duke@435: _is_complete = false; duke@435: _is_success = false; duke@435: _code_handle = NULL; duke@435: duke@435: _hot_method = NULL; duke@435: _hot_count = hot_count; duke@435: _time_queued = 0; // tidy duke@435: _comment = comment; duke@435: duke@435: if (LogCompilation) { duke@435: _time_queued = os::elapsed_counter(); duke@435: if (hot_method.not_null()) { duke@435: if (hot_method == method) { duke@435: _hot_method = _method; duke@435: } else { duke@435: _hot_method = JNIHandles::make_global(hot_method); duke@435: } duke@435: } duke@435: } duke@435: duke@435: _next = NULL; duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::code/set_code duke@435: nmethod* CompileTask::code() const { duke@435: if (_code_handle == NULL) return NULL; duke@435: return _code_handle->code(); duke@435: } duke@435: void CompileTask::set_code(nmethod* nm) { duke@435: if (_code_handle == NULL && nm == NULL) return; duke@435: guarantee(_code_handle != NULL, ""); duke@435: _code_handle->set_code(nm); duke@435: if (nm == NULL) _code_handle = NULL; // drop the handle also duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::free duke@435: void CompileTask::free() { duke@435: set_code(NULL); duke@435: assert(!_lock->is_locked(), "Should not be locked when freed"); duke@435: if (_hot_method != NULL && _hot_method != _method) { duke@435: JNIHandles::destroy_global(_hot_method); duke@435: } duke@435: JNIHandles::destroy_global(_method); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::print duke@435: void CompileTask::print() { duke@435: tty->print("print("method="); duke@435: ((methodOop)JNIHandles::resolve(_method))->print_name(tty); duke@435: tty->print_cr(" osr_bci=%d is_blocking=%s is_complete=%s is_success=%s>", duke@435: _osr_bci, bool_to_str(_is_blocking), duke@435: bool_to_str(_is_complete), bool_to_str(_is_success)); duke@435: } duke@435: iveresov@2138: iveresov@2138: void CompileTask::print_compilation(outputStream *st, methodOop method, char* method_name) { iveresov@2138: nmethod::print_compilation(st, method_name,/*title*/ NULL, method, iveresov@2138: is_blocking(), compile_id(), osr_bci(), comp_level()); iveresov@2138: } iveresov@2138: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::print_line_on_error duke@435: // duke@435: // This function is called by fatal error handler when the thread duke@435: // causing troubles is a compiler thread. duke@435: // duke@435: // Do not grab any lock, do not allocate memory. duke@435: // duke@435: // Otherwise it's the same as CompileTask::print_line() duke@435: // duke@435: void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) { duke@435: methodOop method = (methodOop)JNIHandles::resolve(_method); duke@435: // print compiler name duke@435: st->print("%s:", CompileBroker::compiler(comp_level())->name()); iveresov@2138: char* method_name = NULL; iveresov@2138: if (method != NULL) { iveresov@2138: method_name = method->name_and_sig_as_C_string(buf, buflen); duke@435: } iveresov@2138: print_compilation(st, method, method_name); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::print_line duke@435: void CompileTask::print_line() { duke@435: Thread *thread = Thread::current(); duke@435: methodHandle method(thread, duke@435: (methodOop)JNIHandles::resolve(method_handle())); duke@435: ResourceMark rm(thread); duke@435: duke@435: ttyLocker ttyl; // keep the following output all in one block duke@435: duke@435: // print compiler name if requested duke@435: if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler(comp_level())->name()); iveresov@2138: print_compilation(tty, method(), NULL); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::log_task duke@435: void CompileTask::log_task(xmlStream* log) { duke@435: Thread* thread = Thread::current(); duke@435: methodHandle method(thread, duke@435: (methodOop)JNIHandles::resolve(method_handle())); duke@435: ResourceMark rm(thread); duke@435: duke@435: // duke@435: if (_compile_id != 0) log->print(" compile_id='%d'", _compile_id); duke@435: if (_osr_bci != CompileBroker::standard_entry_bci) { duke@435: log->print(" compile_kind='osr'"); // same as nmethod::compile_kind duke@435: } // else compile_kind='c2c' duke@435: if (!method.is_null()) log->method(method); duke@435: if (_osr_bci != CompileBroker::standard_entry_bci) { duke@435: log->print(" osr_bci='%d'", _osr_bci); duke@435: } duke@435: if (_comp_level != CompLevel_highest_tier) { duke@435: log->print(" level='%d'", _comp_level); duke@435: } duke@435: if (_is_blocking) { duke@435: log->print(" blocking='1'"); duke@435: } duke@435: log->stamp(); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::log_task_queued duke@435: void CompileTask::log_task_queued() { duke@435: Thread* thread = Thread::current(); duke@435: ttyLocker ttyl; duke@435: ResourceMark rm(thread); duke@435: duke@435: xtty->begin_elem("task_queued"); duke@435: log_task(xtty); duke@435: if (_comment != NULL) { duke@435: xtty->print(" comment='%s'", _comment); duke@435: } duke@435: if (_hot_method != NULL) { duke@435: methodHandle hot(thread, duke@435: (methodOop)JNIHandles::resolve(_hot_method)); duke@435: methodHandle method(thread, duke@435: (methodOop)JNIHandles::resolve(_method)); duke@435: if (hot() != method()) { duke@435: xtty->method(hot); duke@435: } duke@435: } duke@435: if (_hot_count != 0) { duke@435: xtty->print(" hot_count='%d'", _hot_count); duke@435: } duke@435: xtty->end_elem(); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::log_task_start duke@435: void CompileTask::log_task_start(CompileLog* log) { duke@435: log->begin_head("task"); duke@435: log_task(log); duke@435: log->end_head(); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileTask::log_task_done duke@435: void CompileTask::log_task_done(CompileLog* log) { duke@435: Thread* thread = Thread::current(); duke@435: methodHandle method(thread, duke@435: (methodOop)JNIHandles::resolve(method_handle())); duke@435: ResourceMark rm(thread); duke@435: duke@435: // duke@435: nmethod* nm = code(); duke@435: log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", twisti@2103: _is_success, nm == NULL ? 0 : nm->content_size(), duke@435: method->invocation_count()); duke@435: int bec = method->backedge_count(); duke@435: if (bec != 0) log->print(" backedge_count='%d'", bec); duke@435: // Note: "_is_complete" is about to be set, but is not. duke@435: if (_num_inlined_bytecodes != 0) { duke@435: log->print(" inlined_bytes='%d'", _num_inlined_bytecodes); duke@435: } duke@435: log->stamp(); duke@435: log->end_elem(); duke@435: log->tail("task"); duke@435: log->clear_identities(); // next task will have different CI duke@435: if (log->unflushed_count() > 2000) { duke@435: log->flush(); duke@435: } duke@435: log->mark_file_end(); duke@435: } duke@435: duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileQueue::add duke@435: // duke@435: // Add a CompileTask to a CompileQueue duke@435: void CompileQueue::add(CompileTask* task) { duke@435: assert(lock()->owned_by_self(), "must own lock"); duke@435: duke@435: task->set_next(NULL); iveresov@2138: task->set_prev(NULL); duke@435: duke@435: if (_last == NULL) { duke@435: // The compile queue is empty. duke@435: assert(_first == NULL, "queue is empty"); duke@435: _first = task; duke@435: _last = task; duke@435: } else { duke@435: // Append the task to the queue. duke@435: assert(_last->next() == NULL, "not last"); duke@435: _last->set_next(task); iveresov@2138: task->set_prev(_last); duke@435: _last = task; duke@435: } iveresov@2138: ++_size; duke@435: duke@435: // Mark the method as being in the compile queue. duke@435: ((methodOop)JNIHandles::resolve(task->method_handle()))->set_queued_for_compilation(); duke@435: duke@435: if (CIPrintCompileQueue) { duke@435: print(); duke@435: } duke@435: duke@435: if (LogCompilation && xtty != NULL) { duke@435: task->log_task_queued(); duke@435: } duke@435: duke@435: // Notify CompilerThreads that a task is available. iveresov@2138: lock()->notify_all(); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileQueue::get duke@435: // duke@435: // Get the next CompileTask from a CompileQueue duke@435: CompileTask* CompileQueue::get() { never@1893: NMethodSweeper::possibly_sweep(); never@1893: duke@435: MutexLocker locker(lock()); duke@435: // Wait for an available CompileTask. duke@435: while (_first == NULL) { duke@435: // There is no work to be done right now. Wait. never@1893: if (UseCodeCacheFlushing && (!CompileBroker::should_compile_new_jobs() || CodeCache::needs_flushing())) { never@1893: // During the emergency sweeping periods, wake up and sweep occasionally never@1893: bool timedout = lock()->wait(!Mutex::_no_safepoint_check_flag, NmethodSweepCheckInterval*1000); never@1893: if (timedout) { never@1893: MutexUnlocker ul(lock()); never@1893: // When otherwise not busy, run nmethod sweeping never@1893: NMethodSweeper::possibly_sweep(); never@1893: } never@1893: } else { never@1893: // During normal operation no need to wake up on timer never@1893: lock()->wait(); never@1893: } duke@435: } iveresov@2138: CompileTask* task = CompilationPolicy::policy()->select_task(this); iveresov@2138: remove(task); iveresov@2138: return task; iveresov@2138: } duke@435: iveresov@2138: void CompileQueue::remove(CompileTask* task) iveresov@2138: { iveresov@2138: assert(lock()->owned_by_self(), "must own lock"); iveresov@2138: if (task->prev() != NULL) { iveresov@2138: task->prev()->set_next(task->next()); iveresov@2138: } else { iveresov@2138: // max is the first element iveresov@2138: assert(task == _first, "Sanity"); iveresov@2138: _first = task->next(); duke@435: } duke@435: iveresov@2138: if (task->next() != NULL) { iveresov@2138: task->next()->set_prev(task->prev()); iveresov@2138: } else { iveresov@2138: // max is the last element iveresov@2138: assert(task == _last, "Sanity"); iveresov@2138: _last = task->prev(); iveresov@2138: } iveresov@2138: --_size; duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileQueue::print duke@435: void CompileQueue::print() { duke@435: tty->print_cr("Contents of %s", name()); duke@435: tty->print_cr("----------------------"); duke@435: CompileTask* task = _first; duke@435: while (task != NULL) { duke@435: task->print_line(); duke@435: task = task->next(); duke@435: } duke@435: tty->print_cr("----------------------"); duke@435: } duke@435: duke@435: CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) { duke@435: duke@435: _current_method[0] = '\0'; duke@435: _compile_type = CompileBroker::no_compile; duke@435: duke@435: if (UsePerfData) { duke@435: ResourceMark rm; duke@435: duke@435: // create the thread instance name space string - don't create an duke@435: // instance subspace if instance is -1 - keeps the adapterThread duke@435: // counters from having a ".0" namespace. duke@435: const char* thread_i = (instance == -1) ? thread_name : duke@435: PerfDataManager::name_space(thread_name, instance); duke@435: duke@435: duke@435: char* name = PerfDataManager::counter_name(thread_i, "method"); duke@435: _perf_current_method = duke@435: PerfDataManager::create_string_variable(SUN_CI, name, duke@435: cmname_buffer_length, duke@435: _current_method, CHECK); duke@435: duke@435: name = PerfDataManager::counter_name(thread_i, "type"); duke@435: _perf_compile_type = PerfDataManager::create_variable(SUN_CI, name, duke@435: PerfData::U_None, duke@435: (jlong)_compile_type, duke@435: CHECK); duke@435: duke@435: name = PerfDataManager::counter_name(thread_i, "time"); duke@435: _perf_time = PerfDataManager::create_counter(SUN_CI, name, duke@435: PerfData::U_Ticks, CHECK); duke@435: duke@435: name = PerfDataManager::counter_name(thread_i, "compiles"); duke@435: _perf_compiles = PerfDataManager::create_counter(SUN_CI, name, duke@435: PerfData::U_Events, CHECK); duke@435: } duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::compilation_init duke@435: // duke@435: // Initialize the Compilation object duke@435: void CompileBroker::compilation_init() { duke@435: _last_method_compiled[0] = '\0'; duke@435: twisti@2312: #ifndef SHARK duke@435: // Set the interface to the current compiler(s). iveresov@2138: int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple); iveresov@2138: int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization); duke@435: #ifdef COMPILER1 iveresov@2138: if (c1_count > 0) { iveresov@2138: _compilers[0] = new Compiler(); iveresov@2138: } duke@435: #endif // COMPILER1 duke@435: duke@435: #ifdef COMPILER2 iveresov@2138: if (c2_count > 0) { iveresov@2138: _compilers[1] = new C2Compiler(); iveresov@2138: } duke@435: #endif // COMPILER2 duke@435: twisti@2312: #else // SHARK twisti@2312: int c1_count = 0; twisti@2312: int c2_count = 1; twisti@2312: twisti@2312: _compilers[1] = new SharkCompiler(); twisti@2312: #endif // SHARK twisti@2047: duke@435: // Initialize the CompileTask free list duke@435: _task_free_list = NULL; duke@435: duke@435: // Start the CompilerThreads iveresov@2138: init_compiler_threads(c1_count, c2_count); duke@435: // totalTime performance counter is always created as it is required duke@435: // by the implementation of java.lang.management.CompilationMBean. duke@435: { duke@435: EXCEPTION_MARK; duke@435: _perf_total_compilation = duke@435: PerfDataManager::create_counter(JAVA_CI, "totalTime", duke@435: PerfData::U_Ticks, CHECK); duke@435: } duke@435: duke@435: duke@435: if (UsePerfData) { duke@435: duke@435: EXCEPTION_MARK; duke@435: duke@435: // create the jvmstat performance counters duke@435: _perf_osr_compilation = duke@435: PerfDataManager::create_counter(SUN_CI, "osrTime", duke@435: PerfData::U_Ticks, CHECK); duke@435: duke@435: _perf_standard_compilation = duke@435: PerfDataManager::create_counter(SUN_CI, "standardTime", duke@435: PerfData::U_Ticks, CHECK); duke@435: duke@435: _perf_total_bailout_count = duke@435: PerfDataManager::create_counter(SUN_CI, "totalBailouts", duke@435: PerfData::U_Events, CHECK); duke@435: duke@435: _perf_total_invalidated_count = duke@435: PerfDataManager::create_counter(SUN_CI, "totalInvalidates", duke@435: PerfData::U_Events, CHECK); duke@435: duke@435: _perf_total_compile_count = duke@435: PerfDataManager::create_counter(SUN_CI, "totalCompiles", duke@435: PerfData::U_Events, CHECK); duke@435: _perf_total_osr_compile_count = duke@435: PerfDataManager::create_counter(SUN_CI, "osrCompiles", duke@435: PerfData::U_Events, CHECK); duke@435: duke@435: _perf_total_standard_compile_count = duke@435: PerfDataManager::create_counter(SUN_CI, "standardCompiles", duke@435: PerfData::U_Events, CHECK); duke@435: duke@435: _perf_sum_osr_bytes_compiled = duke@435: PerfDataManager::create_counter(SUN_CI, "osrBytes", duke@435: PerfData::U_Bytes, CHECK); duke@435: duke@435: _perf_sum_standard_bytes_compiled = duke@435: PerfDataManager::create_counter(SUN_CI, "standardBytes", duke@435: PerfData::U_Bytes, CHECK); duke@435: duke@435: _perf_sum_nmethod_size = duke@435: PerfDataManager::create_counter(SUN_CI, "nmethodSize", duke@435: PerfData::U_Bytes, CHECK); duke@435: duke@435: _perf_sum_nmethod_code_size = duke@435: PerfDataManager::create_counter(SUN_CI, "nmethodCodeSize", duke@435: PerfData::U_Bytes, CHECK); duke@435: duke@435: _perf_last_method = duke@435: PerfDataManager::create_string_variable(SUN_CI, "lastMethod", duke@435: CompilerCounters::cmname_buffer_length, duke@435: "", CHECK); duke@435: duke@435: _perf_last_failed_method = duke@435: PerfDataManager::create_string_variable(SUN_CI, "lastFailedMethod", duke@435: CompilerCounters::cmname_buffer_length, duke@435: "", CHECK); duke@435: duke@435: _perf_last_invalidated_method = duke@435: PerfDataManager::create_string_variable(SUN_CI, "lastInvalidatedMethod", duke@435: CompilerCounters::cmname_buffer_length, duke@435: "", CHECK); duke@435: duke@435: _perf_last_compile_type = duke@435: PerfDataManager::create_variable(SUN_CI, "lastType", duke@435: PerfData::U_None, duke@435: (jlong)CompileBroker::no_compile, duke@435: CHECK); duke@435: duke@435: _perf_last_compile_size = duke@435: PerfDataManager::create_variable(SUN_CI, "lastSize", duke@435: PerfData::U_Bytes, duke@435: (jlong)CompileBroker::no_compile, duke@435: CHECK); duke@435: duke@435: duke@435: _perf_last_failed_type = duke@435: PerfDataManager::create_variable(SUN_CI, "lastFailedType", duke@435: PerfData::U_None, duke@435: (jlong)CompileBroker::no_compile, duke@435: CHECK); duke@435: duke@435: _perf_last_invalidated_type = duke@435: PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType", duke@435: PerfData::U_None, duke@435: (jlong)CompileBroker::no_compile, duke@435: CHECK); duke@435: } duke@435: duke@435: _initialized = true; duke@435: } duke@435: duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::make_compiler_thread duke@435: CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, TRAPS) { duke@435: CompilerThread* compiler_thread = NULL; duke@435: duke@435: klassOop k = coleenp@2497: SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), duke@435: true, CHECK_0); duke@435: instanceKlassHandle klass (THREAD, k); duke@435: instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0); duke@435: Handle string = java_lang_String::create_from_str(name, CHECK_0); duke@435: duke@435: // Initialize thread_oop to put it into the system threadGroup duke@435: Handle thread_group (THREAD, Universe::system_thread_group()); duke@435: JavaValue result(T_VOID); duke@435: JavaCalls::call_special(&result, thread_oop, duke@435: klass, coleenp@2497: vmSymbols::object_initializer_name(), coleenp@2497: vmSymbols::threadgroup_string_void_signature(), duke@435: thread_group, duke@435: string, duke@435: CHECK_0); duke@435: duke@435: { duke@435: MutexLocker mu(Threads_lock, THREAD); duke@435: compiler_thread = new CompilerThread(queue, counters); duke@435: // At this point the new CompilerThread data-races with this startup duke@435: // thread (which I believe is the primoridal thread and NOT the VM duke@435: // thread). This means Java bytecodes being executed at startup can duke@435: // queue compile jobs which will run at whatever default priority the duke@435: // newly created CompilerThread runs at. duke@435: duke@435: duke@435: // At this point it may be possible that no osthread was created for the duke@435: // JavaThread due to lack of memory. We would have to throw an exception duke@435: // in that case. However, since this must work and we do not allow duke@435: // exceptions anyway, check and abort if this fails. duke@435: duke@435: if (compiler_thread == NULL || compiler_thread->osthread() == NULL){ duke@435: vm_exit_during_initialization("java.lang.OutOfMemoryError", duke@435: "unable to create new native thread"); duke@435: } duke@435: duke@435: java_lang_Thread::set_thread(thread_oop(), compiler_thread); duke@435: duke@435: // Note that this only sets the JavaThread _priority field, which by duke@435: // definition is limited to Java priorities and not OS priorities. duke@435: // The os-priority is set in the CompilerThread startup code itself duke@435: java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); duke@435: // CLEANUP PRIORITIES: This -if- statement hids a bug whereby the compiler duke@435: // threads never have their OS priority set. The assumption here is to duke@435: // enable the Performance group to do flag tuning, figure out a suitable duke@435: // CompilerThreadPriority, and then remove this 'if' statement (and duke@435: // comment) and unconditionally set the priority. duke@435: duke@435: // Compiler Threads should be at the highest Priority duke@435: if ( CompilerThreadPriority != -1 ) duke@435: os::set_native_priority( compiler_thread, CompilerThreadPriority ); duke@435: else duke@435: os::set_native_priority( compiler_thread, os::java_to_os_priority[NearMaxPriority]); duke@435: duke@435: // Note that I cannot call os::set_priority because it expects Java duke@435: // priorities and I am *explicitly* using OS priorities so that it's duke@435: // possible to set the compiler thread priority higher than any Java duke@435: // thread. duke@435: duke@435: java_lang_Thread::set_daemon(thread_oop()); duke@435: duke@435: compiler_thread->set_threadObj(thread_oop()); duke@435: Threads::add(compiler_thread); duke@435: Thread::start(compiler_thread); duke@435: } duke@435: // Let go of Threads_lock before yielding duke@435: os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS) duke@435: duke@435: return compiler_thread; duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::init_compiler_threads duke@435: // duke@435: // Initialize the compilation queue iveresov@2138: void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) { duke@435: EXCEPTION_MARK; twisti@2563: #ifndef ZERO iveresov@2138: assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); twisti@2563: #endif // !ZERO iveresov@2138: if (c2_compiler_count > 0) { iveresov@2138: _c2_method_queue = new CompileQueue("C2MethodQueue", MethodCompileQueue_lock); iveresov@2138: } iveresov@2138: if (c1_compiler_count > 0) { iveresov@2138: _c1_method_queue = new CompileQueue("C1MethodQueue", MethodCompileQueue_lock); iveresov@2138: } duke@435: iveresov@2138: int compiler_count = c1_compiler_count + c2_compiler_count; iveresov@2138: duke@435: _method_threads = duke@435: new (ResourceObj::C_HEAP) GrowableArray(compiler_count, true); duke@435: duke@435: char name_buffer[256]; iveresov@2138: for (int i = 0; i < c2_compiler_count; i++) { duke@435: // Create a name for our thread. iveresov@2138: sprintf(name_buffer, "C2 CompilerThread%d", i); duke@435: CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); iveresov@2138: CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, CHECK); duke@435: _method_threads->append(new_thread); duke@435: } iveresov@2138: iveresov@2138: for (int i = c2_compiler_count; i < compiler_count; i++) { iveresov@2138: // Create a name for our thread. iveresov@2138: sprintf(name_buffer, "C1 CompilerThread%d", i); iveresov@2138: CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); iveresov@2138: CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, CHECK); iveresov@2138: _method_threads->append(new_thread); iveresov@2138: } iveresov@2138: duke@435: if (UsePerfData) { duke@435: PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, duke@435: compiler_count, CHECK); duke@435: } duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::is_idle duke@435: bool CompileBroker::is_idle() { iveresov@2138: if (_c2_method_queue != NULL && !_c2_method_queue->is_empty()) { iveresov@2138: return false; iveresov@2138: } else if (_c1_method_queue != NULL && !_c1_method_queue->is_empty()) { duke@435: return false; duke@435: } else { duke@435: int num_threads = _method_threads->length(); duke@435: for (int i=0; iat(i)->task() != NULL) { duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: // No pending or active compilations. duke@435: return true; duke@435: } duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::compile_method duke@435: // duke@435: // Request compilation of a method. duke@435: void CompileBroker::compile_method_base(methodHandle method, duke@435: int osr_bci, duke@435: int comp_level, duke@435: methodHandle hot_method, duke@435: int hot_count, duke@435: const char* comment, duke@435: TRAPS) { duke@435: // do nothing if compiler thread(s) is not available duke@435: if (!_initialized ) { duke@435: return; duke@435: } duke@435: duke@435: guarantee(!method->is_abstract(), "cannot compile abstract methods"); duke@435: assert(method->method_holder()->klass_part()->oop_is_instance(), duke@435: "sanity check"); duke@435: assert(!instanceKlass::cast(method->method_holder())->is_not_initialized(), duke@435: "method holder must be initialized"); duke@435: duke@435: if (CIPrintRequests) { duke@435: tty->print("request: "); duke@435: method->print_short_name(tty); duke@435: if (osr_bci != InvocationEntryBci) { duke@435: tty->print(" osr_bci: %d", osr_bci); duke@435: } duke@435: tty->print(" comment: %s count: %d", comment, hot_count); duke@435: if (!hot_method.is_null()) { duke@435: tty->print(" hot: "); duke@435: if (hot_method() != method()) { duke@435: hot_method->print_short_name(tty); duke@435: } else { duke@435: tty->print("yes"); duke@435: } duke@435: } duke@435: tty->cr(); duke@435: } duke@435: duke@435: // A request has been made for compilation. Before we do any duke@435: // real work, check to see if the method has been compiled duke@435: // in the meantime with a definitive result. duke@435: if (compilation_is_complete(method, osr_bci, comp_level)) { duke@435: return; duke@435: } duke@435: iveresov@2138: duke@435: // If this method is already in the compile queue, then duke@435: // we do not block the current thread. duke@435: if (compilation_is_in_queue(method, osr_bci)) { duke@435: // We may want to decay our counter a bit here to prevent duke@435: // multiple denied requests for compilation. This is an duke@435: // open compilation policy issue. Note: The other possibility, duke@435: // in the case that this is a blocking compile request, is to have duke@435: // all subsequent blocking requesters wait for completion of duke@435: // ongoing compiles. Note that in this case we'll need a protocol duke@435: // for freeing the associated compile tasks. [Or we could have duke@435: // a single static monitor on which all these waiters sleep.] duke@435: return; duke@435: } duke@435: duke@435: // Outputs from the following MutexLocker block: duke@435: CompileTask* task = NULL; duke@435: bool blocking = false; iveresov@2138: CompileQueue* queue = compile_queue(comp_level); duke@435: duke@435: // Acquire our lock. duke@435: { iveresov@2138: MutexLocker locker(queue->lock(), THREAD); duke@435: duke@435: // Make sure the method has not slipped into the queues since duke@435: // last we checked; note that those checks were "fast bail-outs". duke@435: // Here we need to be more careful, see 14012000 below. duke@435: if (compilation_is_in_queue(method, osr_bci)) { duke@435: return; duke@435: } duke@435: duke@435: // We need to check again to see if the compilation has duke@435: // completed. A previous compilation may have registered duke@435: // some result. duke@435: if (compilation_is_complete(method, osr_bci, comp_level)) { duke@435: return; duke@435: } duke@435: duke@435: // We now know that this compilation is not pending, complete, duke@435: // or prohibited. Assign a compile_id to this compilation duke@435: // and check to see if it is in our [Start..Stop) range. duke@435: uint compile_id = assign_compile_id(method, osr_bci); duke@435: if (compile_id == 0) { duke@435: // The compilation falls outside the allowed range. duke@435: return; duke@435: } duke@435: duke@435: // Should this thread wait for completion of the compile? duke@435: blocking = is_compile_blocking(method, osr_bci); duke@435: duke@435: // We will enter the compilation in the queue. duke@435: // 14012000: Note that this sets the queued_for_compile bits in duke@435: // the target method. We can now reason that a method cannot be duke@435: // queued for compilation more than once, as follows: duke@435: // Before a thread queues a task for compilation, it first acquires duke@435: // the compile queue lock, then checks if the method's queued bits duke@435: // are set or it has already been compiled. Thus there can not be two duke@435: // instances of a compilation task for the same method on the duke@435: // compilation queue. Consider now the case where the compilation duke@435: // thread has already removed a task for that method from the queue duke@435: // and is in the midst of compiling it. In this case, the duke@435: // queued_for_compile bits must be set in the method (and these duke@435: // will be visible to the current thread, since the bits were set duke@435: // under protection of the compile queue lock, which we hold now. duke@435: // When the compilation completes, the compiler thread first sets duke@435: // the compilation result and then clears the queued_for_compile duke@435: // bits. Neither of these actions are protected by a barrier (or done duke@435: // under the protection of a lock), so the only guarantee we have duke@435: // (on machines with TSO (Total Store Order)) is that these values duke@435: // will update in that order. As a result, the only combinations of duke@435: // these bits that the current thread will see are, in temporal order: duke@435: // : duke@435: // <0, 1> : in compile queue, but not yet compiled duke@435: // <1, 1> : compiled but queue bit not cleared duke@435: // <1, 0> : compiled and queue bit cleared duke@435: // Because we first check the queue bits then check the result bits, duke@435: // we are assured that we cannot introduce a duplicate task. duke@435: // Note that if we did the tests in the reverse order (i.e. check duke@435: // result then check queued bit), we could get the result bit before duke@435: // the compilation completed, and the queue bit after the compilation duke@435: // completed, and end up introducing a "duplicate" (redundant) task. duke@435: // In that case, the compiler thread should first check if a method duke@435: // has already been compiled before trying to compile it. duke@435: // NOTE: in the event that there are multiple compiler threads and duke@435: // there is de-optimization/recompilation, things will get hairy, duke@435: // and in that case it's best to protect both the testing (here) of duke@435: // these bits, and their updating (here and elsewhere) under a duke@435: // common lock. iveresov@2138: task = create_compile_task(queue, duke@435: compile_id, method, duke@435: osr_bci, comp_level, duke@435: hot_method, hot_count, comment, duke@435: blocking); duke@435: } duke@435: duke@435: if (blocking) { duke@435: wait_for_completion(task); duke@435: } duke@435: } duke@435: duke@435: duke@435: nmethod* CompileBroker::compile_method(methodHandle method, int osr_bci, iveresov@2138: int comp_level, duke@435: methodHandle hot_method, int hot_count, duke@435: const char* comment, TRAPS) { duke@435: // make sure arguments make sense duke@435: assert(method->method_holder()->klass_part()->oop_is_instance(), "not an instance method"); duke@435: assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range"); duke@435: assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods"); duke@435: assert(!instanceKlass::cast(method->method_holder())->is_not_initialized(), "method holder must be initialized"); duke@435: iveresov@2138: if (!TieredCompilation) { iveresov@2138: comp_level = CompLevel_highest_tier; duke@435: } duke@435: duke@435: // return quickly if possible duke@435: duke@435: // lock, make sure that the compilation duke@435: // isn't prohibited in a straightforward way. duke@435: duke@435: if (compiler(comp_level) == NULL || compilation_is_prohibited(method, osr_bci, comp_level)) { duke@435: return NULL; duke@435: } duke@435: duke@435: if (osr_bci == InvocationEntryBci) { duke@435: // standard compilation duke@435: nmethod* method_code = method->code(); iveresov@2138: if (method_code != NULL) { iveresov@2138: if (compilation_is_complete(method, osr_bci, comp_level)) { iveresov@2138: return method_code; iveresov@2138: } duke@435: } duke@435: if (method->is_not_compilable(comp_level)) return NULL; kvn@1637: kvn@1735: if (UseCodeCacheFlushing) { kvn@1735: nmethod* saved = CodeCache::find_and_remove_saved_code(method()); kvn@1735: if (saved != NULL) { kvn@1735: method->set_code(method, saved); kvn@1735: return saved; kvn@1735: } kvn@1637: } kvn@1637: duke@435: } else { duke@435: // osr compilation duke@435: #ifndef TIERED duke@435: // seems like an assert of dubious value iveresov@2138: assert(comp_level == CompLevel_highest_tier, duke@435: "all OSR compiles are assumed to be at a single compilation lavel"); duke@435: #endif // TIERED iveresov@2138: // We accept a higher level osr method iveresov@2138: nmethod* nm = method->lookup_osr_nmethod_for(osr_bci, comp_level, false); duke@435: if (nm != NULL) return nm; duke@435: if (method->is_not_osr_compilable()) return NULL; duke@435: } duke@435: duke@435: assert(!HAS_PENDING_EXCEPTION, "No exception should be present"); duke@435: // some prerequisites that are compiler specific duke@435: if (compiler(comp_level)->is_c2()) { duke@435: method->constants()->resolve_string_constants(CHECK_0); duke@435: // Resolve all classes seen in the signature of the method duke@435: // we are compiling. duke@435: methodOopDesc::load_signature_classes(method, CHECK_0); duke@435: } duke@435: duke@435: // If the method is native, do the lookup in the thread requesting duke@435: // the compilation. Native lookups can load code, which is not duke@435: // permitted during compilation. duke@435: // duke@435: // Note: A native method implies non-osr compilation which is duke@435: // checked with an assertion at the entry of this method. duke@435: if (method->is_native()) { duke@435: bool in_base_library; duke@435: address adr = NativeLookup::lookup(method, in_base_library, THREAD); duke@435: if (HAS_PENDING_EXCEPTION) { duke@435: // In case of an exception looking up the method, we just forget duke@435: // about it. The interpreter will kick-in and throw the exception. duke@435: method->set_not_compilable(); // implies is_not_osr_compilable() duke@435: CLEAR_PENDING_EXCEPTION; duke@435: return NULL; duke@435: } duke@435: assert(method->has_native_function(), "must have native code by now"); duke@435: } duke@435: duke@435: // RedefineClasses() has replaced this method; just return duke@435: if (method->is_old()) { duke@435: return NULL; duke@435: } duke@435: duke@435: // JVMTI -- post_compile_event requires jmethod_id() that may require duke@435: // a lock the compiling thread can not acquire. Prefetch it here. duke@435: if (JvmtiExport::should_post_compiled_method_load()) { duke@435: method->jmethod_id(); duke@435: } duke@435: kvn@1637: // If the compiler is shut off due to code cache flushing or otherwise, kvn@1637: // fail out now so blocking compiles dont hang the java thread kvn@1637: if (!should_compile_new_jobs() || (UseCodeCacheFlushing && CodeCache::needs_flushing())) { iveresov@2138: CompilationPolicy::policy()->delay_compilation(method()); kvn@1637: return NULL; kvn@1637: } kvn@1637: duke@435: // do the compilation duke@435: if (method->is_native()) { duke@435: if (!PreferInterpreterNativeStubs) { duke@435: (void) AdapterHandlerLibrary::create_native_wrapper(method); duke@435: } else { duke@435: return NULL; duke@435: } duke@435: } else { duke@435: compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, CHECK_0); duke@435: } duke@435: duke@435: // return requested nmethod iveresov@2138: // We accept a higher level osr method iveresov@2138: return osr_bci == InvocationEntryBci ? method->code() : method->lookup_osr_nmethod_for(osr_bci, comp_level, false); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::compilation_is_complete duke@435: // duke@435: // See if compilation of this method is already complete. duke@435: bool CompileBroker::compilation_is_complete(methodHandle method, duke@435: int osr_bci, duke@435: int comp_level) { duke@435: bool is_osr = (osr_bci != standard_entry_bci); duke@435: if (is_osr) { duke@435: if (method->is_not_osr_compilable()) { duke@435: return true; duke@435: } else { iveresov@2138: nmethod* result = method->lookup_osr_nmethod_for(osr_bci, comp_level, true); duke@435: return (result != NULL); duke@435: } duke@435: } else { duke@435: if (method->is_not_compilable(comp_level)) { duke@435: return true; duke@435: } else { duke@435: nmethod* result = method->code(); duke@435: if (result == NULL) return false; iveresov@2138: return comp_level == result->comp_level(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::compilation_is_in_queue duke@435: // duke@435: // See if this compilation is already requested. duke@435: // duke@435: // Implementation note: there is only a single "is in queue" bit duke@435: // for each method. This means that the check below is overly duke@435: // conservative in the sense that an osr compilation in the queue duke@435: // will block a normal compilation from entering the queue (and vice duke@435: // versa). This can be remedied by a full queue search to disambiguate duke@435: // cases. If it is deemed profitible, this may be done. duke@435: bool CompileBroker::compilation_is_in_queue(methodHandle method, iveresov@2138: int osr_bci) { duke@435: return method->queued_for_compilation(); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::compilation_is_prohibited duke@435: // duke@435: // See if this compilation is not allowed. duke@435: bool CompileBroker::compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level) { duke@435: bool is_native = method->is_native(); duke@435: // Some compilers may not support the compilation of natives. duke@435: if (is_native && duke@435: (!CICompileNatives || !compiler(comp_level)->supports_native())) { iveresov@2138: method->set_not_compilable_quietly(comp_level); duke@435: return true; duke@435: } duke@435: duke@435: bool is_osr = (osr_bci != standard_entry_bci); duke@435: // Some compilers may not support on stack replacement. duke@435: if (is_osr && duke@435: (!CICompileOSR || !compiler(comp_level)->supports_osr())) { duke@435: method->set_not_osr_compilable(); duke@435: return true; duke@435: } duke@435: duke@435: // The method may be explicitly excluded by the user. duke@435: bool quietly; duke@435: if (CompilerOracle::should_exclude(method, quietly)) { duke@435: if (!quietly) { duke@435: // This does not happen quietly... duke@435: ResourceMark rm; duke@435: tty->print("### Excluding %s:%s", duke@435: method->is_native() ? "generation of native wrapper" : "compile", duke@435: (method->is_static() ? " static" : "")); duke@435: method->print_short_name(tty); duke@435: tty->cr(); duke@435: } kvn@1643: method->set_not_compilable_quietly(); duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::assign_compile_id duke@435: // duke@435: // Assign a serialized id number to this compilation request. If the duke@435: // number falls out of the allowed range, return a 0. OSR duke@435: // compilations may be numbered separately from regular compilations duke@435: // if certain debugging flags are used. duke@435: uint CompileBroker::assign_compile_id(methodHandle method, int osr_bci) { iveresov@2138: assert(MethodCompileQueue_lock->owner() == Thread::current(), duke@435: "must hold the compilation queue lock"); duke@435: bool is_osr = (osr_bci != standard_entry_bci); duke@435: assert(!method->is_native(), "no longer compile natives"); duke@435: uint id; duke@435: if (CICountOSR && is_osr) { duke@435: id = ++_osr_compilation_id; duke@435: if ((uint)CIStartOSR <= id && id < (uint)CIStopOSR) { duke@435: return id; duke@435: } duke@435: } else { duke@435: id = ++_compilation_id; duke@435: if ((uint)CIStart <= id && id < (uint)CIStop) { duke@435: return id; duke@435: } duke@435: } duke@435: duke@435: // Method was not in the appropriate compilation range. kvn@1643: method->set_not_compilable_quietly(); duke@435: return 0; duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::is_compile_blocking duke@435: // duke@435: // Should the current thread be blocked until this compilation request duke@435: // has been fulfilled? duke@435: bool CompileBroker::is_compile_blocking(methodHandle method, int osr_bci) { duke@435: return !BackgroundCompilation; duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::preload_classes duke@435: void CompileBroker::preload_classes(methodHandle method, TRAPS) { duke@435: // Move this code over from c1_Compiler.cpp duke@435: ShouldNotReachHere(); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::create_compile_task duke@435: // duke@435: // Create a CompileTask object representing the current request for duke@435: // compilation. Add this task to the queue. duke@435: CompileTask* CompileBroker::create_compile_task(CompileQueue* queue, duke@435: int compile_id, duke@435: methodHandle method, duke@435: int osr_bci, duke@435: int comp_level, duke@435: methodHandle hot_method, duke@435: int hot_count, duke@435: const char* comment, duke@435: bool blocking) { duke@435: CompileTask* new_task = allocate_task(); duke@435: new_task->initialize(compile_id, method, osr_bci, comp_level, duke@435: hot_method, hot_count, comment, duke@435: blocking); duke@435: queue->add(new_task); duke@435: return new_task; duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::allocate_task duke@435: // duke@435: // Allocate a CompileTask, from the free list if possible. duke@435: CompileTask* CompileBroker::allocate_task() { duke@435: MutexLocker locker(CompileTaskAlloc_lock); duke@435: CompileTask* task = NULL; duke@435: if (_task_free_list != NULL) { duke@435: task = _task_free_list; duke@435: _task_free_list = task->next(); duke@435: task->set_next(NULL); duke@435: } else { duke@435: task = new CompileTask(); duke@435: task->set_next(NULL); duke@435: } duke@435: return task; duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::free_task duke@435: // duke@435: // Add a task to the free list. duke@435: void CompileBroker::free_task(CompileTask* task) { duke@435: MutexLocker locker(CompileTaskAlloc_lock); duke@435: task->free(); duke@435: task->set_next(_task_free_list); duke@435: _task_free_list = task; duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::wait_for_completion duke@435: // duke@435: // Wait for the given method CompileTask to complete. duke@435: void CompileBroker::wait_for_completion(CompileTask* task) { duke@435: if (CIPrintCompileQueue) { duke@435: tty->print_cr("BLOCKING FOR COMPILE"); duke@435: } duke@435: duke@435: assert(task->is_blocking(), "can only wait on blocking task"); duke@435: duke@435: JavaThread *thread = JavaThread::current(); duke@435: thread->set_blocked_on_compilation(true); duke@435: duke@435: methodHandle method(thread, duke@435: (methodOop)JNIHandles::resolve(task->method_handle())); duke@435: { duke@435: MutexLocker waiter(task->lock(), thread); duke@435: duke@435: while (!task->is_complete()) duke@435: task->lock()->wait(); duke@435: } duke@435: // It is harmless to check this status without the lock, because duke@435: // completion is a stable property (until the task object is recycled). duke@435: assert(task->is_complete(), "Compilation should have completed"); duke@435: assert(task->code_handle() == NULL, "must be reset"); duke@435: duke@435: thread->set_blocked_on_compilation(false); duke@435: duke@435: // By convention, the waiter is responsible for recycling a duke@435: // blocking CompileTask. Since there is only one waiter ever duke@435: // waiting on a CompileTask, we know that no one else will duke@435: // be using this CompileTask; we can free it. duke@435: free_task(task); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::compiler_thread_loop duke@435: // duke@435: // The main loop run by a CompilerThread. duke@435: void CompileBroker::compiler_thread_loop() { duke@435: CompilerThread* thread = CompilerThread::current(); duke@435: CompileQueue* queue = thread->queue(); duke@435: duke@435: // For the thread that initializes the ciObjectFactory duke@435: // this resource mark holds all the shared objects duke@435: ResourceMark rm; duke@435: duke@435: // First thread to get here will initialize the compiler interface duke@435: duke@435: if (!ciObjectFactory::is_initialized()) { duke@435: ASSERT_IN_VM; duke@435: MutexLocker only_one (CompileThread_lock, thread); duke@435: if (!ciObjectFactory::is_initialized()) { duke@435: ciObjectFactory::initialize(); duke@435: } duke@435: } duke@435: duke@435: // Open a log. duke@435: if (LogCompilation) { duke@435: init_compiler_thread_log(); duke@435: } duke@435: CompileLog* log = thread->log(); duke@435: if (log != NULL) { duke@435: log->begin_elem("start_compile_thread thread='" UINTX_FORMAT "' process='%d'", duke@435: os::current_thread_id(), duke@435: os::current_process_id()); duke@435: log->stamp(); duke@435: log->end_elem(); duke@435: } duke@435: duke@435: while (true) { duke@435: { duke@435: // We need this HandleMark to avoid leaking VM handles. duke@435: HandleMark hm(thread); kvn@1637: duke@435: if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) { kvn@1637: // the code cache is really full kvn@1637: handle_full_code_cache(); kvn@1637: } else if (UseCodeCacheFlushing && CodeCache::needs_flushing()) { kvn@1637: // Attempt to start cleaning the code cache while there is still a little headroom kvn@1637: NMethodSweeper::handle_full_code_cache(false); duke@435: } duke@435: duke@435: CompileTask* task = queue->get(); duke@435: duke@435: // Give compiler threads an extra quanta. They tend to be bursty and duke@435: // this helps the compiler to finish up the job. duke@435: if( CompilerThreadHintNoPreempt ) duke@435: os::hint_no_preempt(); duke@435: duke@435: // trace per thread time and compile statistics duke@435: CompilerCounters* counters = ((CompilerThread*)thread)->counters(); duke@435: PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter()); duke@435: duke@435: // Assign the task to the current thread. Mark this compilation duke@435: // thread as active for the profiler. duke@435: CompileTaskWrapper ctw(task); duke@435: nmethodLocker result_handle; // (handle for the nmethod produced by this task) duke@435: task->set_code_handle(&result_handle); duke@435: methodHandle method(thread, duke@435: (methodOop)JNIHandles::resolve(task->method_handle())); duke@435: duke@435: // Never compile a method if breakpoints are present in it duke@435: if (method()->number_of_breakpoints() == 0) { duke@435: // Compile the method. kvn@1637: if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) { duke@435: #ifdef COMPILER1 duke@435: // Allow repeating compilations for the purpose of benchmarking duke@435: // compile speed. This is not useful for customers. duke@435: if (CompilationRepeat != 0) { duke@435: int compile_count = CompilationRepeat; duke@435: while (compile_count > 0) { duke@435: invoke_compiler_on_method(task); duke@435: nmethod* nm = method->code(); duke@435: if (nm != NULL) { duke@435: nm->make_zombie(); duke@435: method->clear_code(); duke@435: } duke@435: compile_count--; duke@435: } duke@435: } duke@435: #endif /* COMPILER1 */ duke@435: invoke_compiler_on_method(task); duke@435: } else { duke@435: // After compilation is disabled, remove remaining methods from queue duke@435: method->clear_queued_for_compilation(); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::init_compiler_thread_log duke@435: // duke@435: // Set up state required by +LogCompilation. duke@435: void CompileBroker::init_compiler_thread_log() { duke@435: CompilerThread* thread = CompilerThread::current(); duke@435: char fileBuf[4*K]; duke@435: FILE* fp = NULL; duke@435: char* file = NULL; duke@435: intx thread_id = os::current_thread_id(); duke@435: for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) { duke@435: const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL); coleenp@1788: if (dir == NULL) { coleenp@1788: jio_snprintf(fileBuf, sizeof(fileBuf), "hs_c" UINTX_FORMAT "_pid%u.log", coleenp@1788: thread_id, os::current_process_id()); coleenp@1788: } else { coleenp@1788: jio_snprintf(fileBuf, sizeof(fileBuf), coleenp@1788: "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir, coleenp@1788: os::file_separator(), thread_id, os::current_process_id()); coleenp@1788: } duke@435: fp = fopen(fileBuf, "at"); duke@435: if (fp != NULL) { duke@435: file = NEW_C_HEAP_ARRAY(char, strlen(fileBuf)+1); duke@435: strcpy(file, fileBuf); duke@435: break; duke@435: } duke@435: } duke@435: if (fp == NULL) { duke@435: warning("Cannot open log file: %s", fileBuf); duke@435: } else { duke@435: if (LogCompilation && Verbose) duke@435: tty->print_cr("Opening compilation log %s", file); duke@435: CompileLog* log = new(ResourceObj::C_HEAP) CompileLog(file, fp, thread_id); duke@435: thread->init_log(log); duke@435: duke@435: if (xtty != NULL) { duke@435: ttyLocker ttyl; duke@435: duke@435: // Record any per thread log files duke@435: xtty->elem("thread_logfile thread='%d' filename='%s'", thread_id, file); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::set_should_block duke@435: // duke@435: // Set _should_block. duke@435: // Call this from the VM, with Threads_lock held and a safepoint requested. duke@435: void CompileBroker::set_should_block() { duke@435: assert(Threads_lock->owner() == Thread::current(), "must have threads lock"); duke@435: assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint already"); duke@435: #ifndef PRODUCT duke@435: if (PrintCompilation && (Verbose || WizardMode)) duke@435: tty->print_cr("notifying compiler thread pool to block"); duke@435: #endif duke@435: _should_block = true; duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::maybe_block duke@435: // duke@435: // Call this from the compiler at convenient points, to poll for _should_block. duke@435: void CompileBroker::maybe_block() { duke@435: if (_should_block) { duke@435: #ifndef PRODUCT duke@435: if (PrintCompilation && (Verbose || WizardMode)) duke@435: tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", Thread::current()); duke@435: #endif duke@435: ThreadInVMfromNative tivfn(JavaThread::current()); duke@435: } duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::invoke_compiler_on_method duke@435: // duke@435: // Compile a method. duke@435: // duke@435: void CompileBroker::invoke_compiler_on_method(CompileTask* task) { duke@435: if (PrintCompilation) { duke@435: ResourceMark rm; duke@435: task->print_line(); duke@435: } duke@435: elapsedTimer time; duke@435: duke@435: CompilerThread* thread = CompilerThread::current(); duke@435: ResourceMark rm(thread); duke@435: duke@435: // Common flags. duke@435: uint compile_id = task->compile_id(); duke@435: int osr_bci = task->osr_bci(); duke@435: bool is_osr = (osr_bci != standard_entry_bci); duke@435: bool should_log = (thread->log() != NULL); duke@435: bool should_break = false; duke@435: { duke@435: // create the handle inside it's own block so it can't duke@435: // accidentally be referenced once the thread transitions to duke@435: // native. The NoHandleMark before the transition should catch duke@435: // any cases where this occurs in the future. duke@435: methodHandle method(thread, duke@435: (methodOop)JNIHandles::resolve(task->method_handle())); duke@435: should_break = check_break_at(method, compile_id, is_osr); duke@435: if (should_log && !CompilerOracle::should_log(method)) { duke@435: should_log = false; duke@435: } duke@435: assert(!method->is_native(), "no longer compile natives"); duke@435: duke@435: // Save information about this method in case of failure. duke@435: set_last_compile(thread, method, is_osr, task->comp_level()); duke@435: duke@435: DTRACE_METHOD_COMPILE_BEGIN_PROBE(compiler(task->comp_level()), method); duke@435: } duke@435: duke@435: // Allocate a new set of JNI handles. duke@435: push_jni_handle_block(); duke@435: jobject target_handle = JNIHandles::make_local(thread, JNIHandles::resolve(task->method_handle())); duke@435: int compilable = ciEnv::MethodCompilable; duke@435: { duke@435: int system_dictionary_modification_counter; duke@435: { duke@435: MutexLocker locker(Compile_lock, thread); duke@435: system_dictionary_modification_counter = SystemDictionary::number_of_modifications(); duke@435: } duke@435: duke@435: NoHandleMark nhm; duke@435: ThreadToNativeFromVM ttn(thread); duke@435: duke@435: ciEnv ci_env(task, system_dictionary_modification_counter); duke@435: if (should_break) { duke@435: ci_env.set_break_at_compile(true); duke@435: } duke@435: if (should_log) { duke@435: ci_env.set_log(thread->log()); duke@435: } duke@435: assert(thread->env() == &ci_env, "set by ci_env"); duke@435: // The thread-env() field is cleared in ~CompileTaskWrapper. duke@435: kvn@1215: // Cache Jvmti state kvn@1215: ci_env.cache_jvmti_state(); kvn@1215: kvn@1215: // Cache DTrace flags kvn@1215: ci_env.cache_dtrace_flags(); kvn@1215: duke@435: ciMethod* target = ci_env.get_method_from_handle(target_handle); duke@435: duke@435: TraceTime t1("compilation", &time); duke@435: duke@435: compiler(task->comp_level())->compile_method(&ci_env, target, osr_bci); duke@435: duke@435: if (!ci_env.failing() && task->code() == NULL) { duke@435: //assert(false, "compiler should always document failure"); duke@435: // The compiler elected, without comment, not to register a result. duke@435: // Do not attempt further compilations of this method. iveresov@2306: ci_env.record_method_not_compilable("compile failed", !TieredCompilation); duke@435: } duke@435: duke@435: if (ci_env.failing()) { duke@435: // Copy this bit to the enclosing block: duke@435: compilable = ci_env.compilable(); duke@435: if (PrintCompilation) { duke@435: const char* reason = ci_env.failure_reason(); duke@435: if (compilable == ciEnv::MethodCompilable_not_at_tier) { duke@435: tty->print_cr("%3d COMPILE SKIPPED: %s (retry at different tier)", compile_id, reason); iveresov@2306: } else if (compilable == ciEnv::MethodCompilable_never) { duke@435: tty->print_cr("%3d COMPILE SKIPPED: %s (not retryable)", compile_id, reason); duke@435: } else if (compilable == ciEnv::MethodCompilable) { duke@435: tty->print_cr("%3d COMPILE SKIPPED: %s", compile_id, reason); duke@435: } duke@435: } duke@435: } else { duke@435: task->mark_success(); duke@435: task->set_num_inlined_bytecodes(ci_env.num_inlined_bytecodes()); duke@435: } duke@435: } duke@435: pop_jni_handle_block(); duke@435: duke@435: methodHandle method(thread, duke@435: (methodOop)JNIHandles::resolve(task->method_handle())); duke@435: duke@435: DTRACE_METHOD_COMPILE_END_PROBE(compiler(task->comp_level()), method, task->is_success()); duke@435: duke@435: collect_statistics(thread, time, task); duke@435: duke@435: if (compilable == ciEnv::MethodCompilable_never) { duke@435: if (is_osr) { duke@435: method->set_not_osr_compilable(); duke@435: } else { kvn@1643: method->set_not_compilable_quietly(); duke@435: } duke@435: } else if (compilable == ciEnv::MethodCompilable_not_at_tier) { kvn@1643: method->set_not_compilable_quietly(task->comp_level()); duke@435: } duke@435: duke@435: // Note that the queued_for_compilation bits are cleared without duke@435: // protection of a mutex. [They were set by the requester thread, duke@435: // when adding the task to the complie queue -- at which time the duke@435: // compile queue lock was held. Subsequently, we acquired the compile duke@435: // queue lock to get this task off the compile queue; thus (to belabour duke@435: // the point somewhat) our clearing of the bits must be occurring duke@435: // only after the setting of the bits. See also 14012000 above. duke@435: method->clear_queued_for_compilation(); duke@435: duke@435: #ifdef ASSERT duke@435: if (CollectedHeap::fired_fake_oom()) { duke@435: // The current compile received a fake OOM during compilation so duke@435: // go ahead and exit the VM since the test apparently succeeded duke@435: tty->print_cr("*** Shutting down VM after successful fake OOM"); duke@435: vm_exit(0); duke@435: } duke@435: #endif duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ kvn@1637: // CompileBroker::handle_full_code_cache kvn@1637: // kvn@1637: // The CodeCache is full. Print out warning and disable compilation or kvn@1637: // try code cache cleaning so compilation can continue later. kvn@1637: void CompileBroker::handle_full_code_cache() { kvn@1637: UseInterpreter = true; kvn@1637: if (UseCompiler || AlwaysCompileLoopMethods ) { never@2083: if (xtty != NULL) { never@2083: xtty->begin_elem("code_cache_full"); never@2083: xtty->stamp(); never@2083: xtty->end_elem(); kvn@1637: } never@1933: warning("CodeCache is full. Compiler has been disabled."); never@1933: warning("Try increasing the code cache size using -XX:ReservedCodeCacheSize="); never@1933: #ifndef PRODUCT kvn@1637: if (CompileTheWorld || ExitOnFullCodeCache) { kvn@1637: before_exit(JavaThread::current()); kvn@1637: exit_globals(); // will delete tty kvn@1637: vm_direct_exit(CompileTheWorld ? 0 : 1); kvn@1637: } never@1933: #endif kvn@1637: if (UseCodeCacheFlushing) { kvn@1637: NMethodSweeper::handle_full_code_cache(true); kvn@1637: } else { kvn@1637: UseCompiler = false; kvn@1637: AlwaysCompileLoopMethods = false; kvn@1637: } kvn@1637: } kvn@1637: } kvn@1637: kvn@1637: // ------------------------------------------------------------------ duke@435: // CompileBroker::set_last_compile duke@435: // duke@435: // Record this compilation for debugging purposes. duke@435: void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) { duke@435: ResourceMark rm; duke@435: char* method_name = method->name()->as_C_string(); duke@435: strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length); duke@435: char current_method[CompilerCounters::cmname_buffer_length]; duke@435: size_t maxLen = CompilerCounters::cmname_buffer_length; duke@435: duke@435: if (UsePerfData) { duke@435: const char* class_name = method->method_holder()->klass_part()->name()->as_C_string(); duke@435: duke@435: size_t s1len = strlen(class_name); duke@435: size_t s2len = strlen(method_name); duke@435: duke@435: // check if we need to truncate the string duke@435: if (s1len + s2len + 2 > maxLen) { duke@435: duke@435: // the strategy is to lop off the leading characters of the duke@435: // class name and the trailing characters of the method name. duke@435: duke@435: if (s2len + 2 > maxLen) { duke@435: // lop of the entire class name string, let snprintf handle duke@435: // truncation of the method name. duke@435: class_name += s1len; // null string duke@435: } duke@435: else { duke@435: // lop off the extra characters from the front of the class name duke@435: class_name += ((s1len + s2len + 2) - maxLen); duke@435: } duke@435: } duke@435: duke@435: jio_snprintf(current_method, maxLen, "%s %s", class_name, method_name); duke@435: } duke@435: duke@435: if (CICountOSR && is_osr) { duke@435: _last_compile_type = osr_compile; duke@435: } else { duke@435: _last_compile_type = normal_compile; duke@435: } duke@435: _last_compile_level = comp_level; duke@435: duke@435: if (UsePerfData) { duke@435: CompilerCounters* counters = thread->counters(); duke@435: counters->set_current_method(current_method); duke@435: counters->set_compile_type((jlong)_last_compile_type); duke@435: } duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::push_jni_handle_block duke@435: // duke@435: // Push on a new block of JNI handles. duke@435: void CompileBroker::push_jni_handle_block() { duke@435: JavaThread* thread = JavaThread::current(); duke@435: duke@435: // Allocate a new block for JNI handles. duke@435: // Inlined code from jni_PushLocalFrame() duke@435: JNIHandleBlock* java_handles = thread->active_handles(); duke@435: JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread); duke@435: assert(compile_handles != NULL && java_handles != NULL, "should not be NULL"); duke@435: compile_handles->set_pop_frame_link(java_handles); // make sure java handles get gc'd. duke@435: thread->set_active_handles(compile_handles); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::pop_jni_handle_block duke@435: // duke@435: // Pop off the current block of JNI handles. duke@435: void CompileBroker::pop_jni_handle_block() { duke@435: JavaThread* thread = JavaThread::current(); duke@435: duke@435: // Release our JNI handle block duke@435: JNIHandleBlock* compile_handles = thread->active_handles(); duke@435: JNIHandleBlock* java_handles = compile_handles->pop_frame_link(); duke@435: thread->set_active_handles(java_handles); duke@435: compile_handles->set_pop_frame_link(NULL); duke@435: JNIHandleBlock::release_block(compile_handles, thread); // may block duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::check_break_at duke@435: // duke@435: // Should the compilation break at the current compilation. duke@435: bool CompileBroker::check_break_at(methodHandle method, int compile_id, bool is_osr) { duke@435: if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) { duke@435: return true; duke@435: } else if( CompilerOracle::should_break_at(method) ) { // break when compiling duke@435: return true; duke@435: } else { duke@435: return (compile_id == CIBreakAt); duke@435: } duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // CompileBroker::collect_statistics duke@435: // duke@435: // Collect statistics about the compilation. duke@435: duke@435: void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) { duke@435: bool success = task->is_success(); duke@435: methodHandle method (thread, (methodOop)JNIHandles::resolve(task->method_handle())); duke@435: uint compile_id = task->compile_id(); duke@435: bool is_osr = (task->osr_bci() != standard_entry_bci); duke@435: nmethod* code = task->code(); duke@435: CompilerCounters* counters = thread->counters(); duke@435: duke@435: assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker"); duke@435: MutexLocker locker(CompileStatistics_lock); duke@435: duke@435: // _perf variables are production performance counters which are duke@435: // updated regardless of the setting of the CITime and CITimeEach flags duke@435: // duke@435: if (!success) { duke@435: _total_bailout_count++; duke@435: if (UsePerfData) { duke@435: _perf_last_failed_method->set_value(counters->current_method()); duke@435: _perf_last_failed_type->set_value(counters->compile_type()); duke@435: _perf_total_bailout_count->inc(); duke@435: } duke@435: } else if (code == NULL) { duke@435: if (UsePerfData) { duke@435: _perf_last_invalidated_method->set_value(counters->current_method()); duke@435: _perf_last_invalidated_type->set_value(counters->compile_type()); duke@435: _perf_total_invalidated_count->inc(); duke@435: } duke@435: _total_invalidated_count++; duke@435: } else { duke@435: // Compilation succeeded duke@435: duke@435: // update compilation ticks - used by the implementation of duke@435: // java.lang.management.CompilationMBean duke@435: _perf_total_compilation->inc(time.ticks()); duke@435: duke@435: if (CITime) { duke@435: _t_total_compilation.add(time); duke@435: if (is_osr) { duke@435: _t_osr_compilation.add(time); duke@435: _sum_osr_bytes_compiled += method->code_size() + task->num_inlined_bytecodes(); duke@435: } else { duke@435: _t_standard_compilation.add(time); duke@435: _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes(); duke@435: } duke@435: } duke@435: duke@435: if (UsePerfData) { duke@435: // save the name of the last method compiled duke@435: _perf_last_method->set_value(counters->current_method()); duke@435: _perf_last_compile_type->set_value(counters->compile_type()); duke@435: _perf_last_compile_size->set_value(method->code_size() + duke@435: task->num_inlined_bytecodes()); duke@435: if (is_osr) { duke@435: _perf_osr_compilation->inc(time.ticks()); duke@435: _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes()); duke@435: } else { duke@435: _perf_standard_compilation->inc(time.ticks()); duke@435: _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes()); duke@435: } duke@435: } duke@435: duke@435: if (CITimeEach) { duke@435: float bytes_per_sec = 1.0 * (method->code_size() + task->num_inlined_bytecodes()) / time.seconds(); duke@435: tty->print_cr("%3d seconds: %f bytes/sec : %f (bytes %d + %d inlined)", duke@435: compile_id, time.seconds(), bytes_per_sec, method->code_size(), task->num_inlined_bytecodes()); duke@435: } duke@435: duke@435: // Collect counts of successful compilations twisti@2103: _sum_nmethod_size += code->total_size(); twisti@2103: _sum_nmethod_code_size += code->insts_size(); duke@435: _total_compile_count++; duke@435: duke@435: if (UsePerfData) { twisti@2103: _perf_sum_nmethod_size->inc( code->total_size()); twisti@2103: _perf_sum_nmethod_code_size->inc(code->insts_size()); duke@435: _perf_total_compile_count->inc(); duke@435: } duke@435: duke@435: if (is_osr) { duke@435: if (UsePerfData) _perf_total_osr_compile_count->inc(); duke@435: _total_osr_compile_count++; duke@435: } else { duke@435: if (UsePerfData) _perf_total_standard_compile_count->inc(); duke@435: _total_standard_compile_count++; duke@435: } duke@435: } duke@435: // set the current method for the thread to null duke@435: if (UsePerfData) counters->set_current_method(""); duke@435: } duke@435: duke@435: duke@435: duke@435: void CompileBroker::print_times() { duke@435: tty->cr(); duke@435: tty->print_cr("Accumulated compiler times (for compiled methods only)"); duke@435: tty->print_cr("------------------------------------------------"); duke@435: //0000000000111111111122222222223333333333444444444455555555556666666666 duke@435: //0123456789012345678901234567890123456789012345678901234567890123456789 duke@435: tty->print_cr(" Total compilation time : %6.3f s", CompileBroker::_t_total_compilation.seconds()); duke@435: tty->print_cr(" Standard compilation : %6.3f s, Average : %2.3f", duke@435: CompileBroker::_t_standard_compilation.seconds(), duke@435: CompileBroker::_t_standard_compilation.seconds() / CompileBroker::_total_standard_compile_count); duke@435: 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); twisti@1566: iveresov@2138: if (compiler(CompLevel_simple) != NULL) { iveresov@2138: compiler(CompLevel_simple)->print_timers(); duke@435: } iveresov@2138: if (compiler(CompLevel_full_optimization) != NULL) { iveresov@2138: compiler(CompLevel_full_optimization)->print_timers(); iveresov@2138: } duke@435: tty->cr(); duke@435: int tcb = CompileBroker::_sum_osr_bytes_compiled + CompileBroker::_sum_standard_bytes_compiled; duke@435: tty->print_cr(" Total compiled bytecodes : %6d bytes", tcb); duke@435: tty->print_cr(" Standard compilation : %6d bytes", CompileBroker::_sum_standard_bytes_compiled); duke@435: tty->print_cr(" On stack replacement : %6d bytes", CompileBroker::_sum_osr_bytes_compiled); duke@435: int bps = (int)(tcb / CompileBroker::_t_total_compilation.seconds()); duke@435: tty->print_cr(" Average compilation speed: %6d bytes/s", bps); duke@435: tty->cr(); duke@435: tty->print_cr(" nmethod code size : %6d bytes", CompileBroker::_sum_nmethod_code_size); duke@435: tty->print_cr(" nmethod total size : %6d bytes", CompileBroker::_sum_nmethod_size); duke@435: } duke@435: duke@435: duke@435: // Debugging output for failure duke@435: void CompileBroker::print_last_compile() { duke@435: if ( _last_compile_level != CompLevel_none && duke@435: compiler(_last_compile_level) != NULL && duke@435: _last_method_compiled != NULL && duke@435: _last_compile_type != no_compile) { duke@435: if (_last_compile_type == osr_compile) { duke@435: tty->print_cr("Last parse: [osr]%d+++(%d) %s", duke@435: _osr_compilation_id, _last_compile_level, _last_method_compiled); duke@435: } else { duke@435: tty->print_cr("Last parse: %d+++(%d) %s", duke@435: _compilation_id, _last_compile_level, _last_method_compiled); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void CompileBroker::print_compiler_threads_on(outputStream* st) { duke@435: #ifndef PRODUCT duke@435: st->print_cr("Compiler thread printing unimplemented."); duke@435: st->cr(); duke@435: #endif duke@435: }