aoqi@0: /* aoqi@0: * Copyright (c) 2000, 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 "code/compiledIC.hpp" aoqi@0: #include "code/nmethod.hpp" aoqi@0: #include "code/scopeDesc.hpp" aoqi@0: #include "compiler/compilerOracle.hpp" aoqi@0: #include "interpreter/interpreter.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/advancedThresholdPolicy.hpp" aoqi@0: #include "runtime/compilationPolicy.hpp" aoqi@0: #include "runtime/frame.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/rframe.hpp" aoqi@0: #include "runtime/simpleThresholdPolicy.hpp" aoqi@0: #include "runtime/stubRoutines.hpp" aoqi@0: #include "runtime/thread.hpp" aoqi@0: #include "runtime/timer.hpp" aoqi@0: #include "runtime/vframe.hpp" aoqi@0: #include "runtime/vm_operations.hpp" aoqi@0: #include "utilities/events.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: aoqi@0: CompilationPolicy* CompilationPolicy::_policy; aoqi@0: elapsedTimer CompilationPolicy::_accumulated_time; aoqi@0: bool CompilationPolicy::_in_vm_startup; aoqi@0: aoqi@0: // Determine compilation policy based on command line argument aoqi@0: void compilationPolicy_init() { aoqi@0: CompilationPolicy::set_in_vm_startup(DelayCompilationDuringStartup); aoqi@0: aoqi@0: switch(CompilationPolicyChoice) { aoqi@0: case 0: aoqi@0: CompilationPolicy::set_policy(new SimpleCompPolicy()); aoqi@0: break; aoqi@0: aoqi@0: case 1: aoqi@0: #ifdef COMPILER2 aoqi@0: CompilationPolicy::set_policy(new StackWalkCompPolicy()); aoqi@0: #else aoqi@0: Unimplemented(); aoqi@0: #endif aoqi@0: break; aoqi@0: case 2: aoqi@0: #ifdef TIERED aoqi@0: CompilationPolicy::set_policy(new SimpleThresholdPolicy()); aoqi@0: #else aoqi@0: Unimplemented(); aoqi@0: #endif aoqi@0: break; aoqi@0: case 3: aoqi@0: #ifdef TIERED aoqi@0: CompilationPolicy::set_policy(new AdvancedThresholdPolicy()); aoqi@0: #else aoqi@0: Unimplemented(); aoqi@0: #endif aoqi@0: break; aoqi@0: default: aoqi@0: fatal("CompilationPolicyChoice must be in the range: [0-3]"); aoqi@0: } aoqi@0: CompilationPolicy::policy()->initialize(); aoqi@0: } aoqi@0: aoqi@0: void CompilationPolicy::completed_vm_startup() { aoqi@0: if (TraceCompilationPolicy) { aoqi@0: tty->print("CompilationPolicy: completed vm startup.\n"); aoqi@0: } aoqi@0: _in_vm_startup = false; aoqi@0: } aoqi@0: aoqi@0: // Returns true if m must be compiled before executing it aoqi@0: // This is intended to force compiles for methods (usually for aoqi@0: // debugging) that would otherwise be interpreted for some reason. aoqi@0: bool CompilationPolicy::must_be_compiled(methodHandle m, int comp_level) { aoqi@0: // Don't allow Xcomp to cause compiles in replay mode aoqi@0: if (ReplayCompiles) return false; aoqi@0: aoqi@0: if (m->has_compiled_code()) return false; // already compiled aoqi@0: if (!can_be_compiled(m, comp_level)) return false; aoqi@0: aoqi@0: return !UseInterpreter || // must compile all methods aoqi@0: (UseCompiler && AlwaysCompileLoopMethods && m->has_loops() && CompileBroker::should_compile_new_jobs()); // eagerly compile loop methods aoqi@0: } aoqi@0: aoqi@0: // Returns true if m is allowed to be compiled aoqi@0: bool CompilationPolicy::can_be_compiled(methodHandle m, int comp_level) { aoqi@0: // allow any levels for WhiteBox aoqi@0: assert(WhiteBoxAPI || comp_level == CompLevel_all || is_compile(comp_level), "illegal compilation level"); aoqi@0: aoqi@0: if (m->is_abstract()) return false; aoqi@0: if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false; aoqi@0: aoqi@0: // Math intrinsics should never be compiled as this can lead to aoqi@0: // monotonicity problems because the interpreter will prefer the aoqi@0: // compiled code to the intrinsic version. This can't happen in aoqi@0: // production because the invocation counter can't be incremented aoqi@0: // but we shouldn't expose the system to this problem in testing aoqi@0: // modes. aoqi@0: if (!AbstractInterpreter::can_be_compiled(m)) { aoqi@0: return false; aoqi@0: } aoqi@0: if (comp_level == CompLevel_all) { aoqi@0: if (TieredCompilation) { aoqi@0: // enough to be compilable at any level for tiered aoqi@0: return !m->is_not_compilable(CompLevel_simple) || !m->is_not_compilable(CompLevel_full_optimization); aoqi@0: } else { aoqi@0: // must be compilable at available level for non-tiered aoqi@0: return !m->is_not_compilable(CompLevel_highest_tier); aoqi@0: } aoqi@0: } else if (is_compile(comp_level)) { aoqi@0: return !m->is_not_compilable(comp_level); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // Returns true if m is allowed to be osr compiled aoqi@0: bool CompilationPolicy::can_be_osr_compiled(methodHandle m, int comp_level) { aoqi@0: bool result = false; aoqi@0: if (comp_level == CompLevel_all) { aoqi@0: if (TieredCompilation) { aoqi@0: // enough to be osr compilable at any level for tiered aoqi@0: result = !m->is_not_osr_compilable(CompLevel_simple) || !m->is_not_osr_compilable(CompLevel_full_optimization); aoqi@0: } else { aoqi@0: // must be osr compilable at available level for non-tiered aoqi@0: result = !m->is_not_osr_compilable(CompLevel_highest_tier); aoqi@0: } aoqi@0: } else if (is_compile(comp_level)) { aoqi@0: result = !m->is_not_osr_compilable(comp_level); aoqi@0: } aoqi@0: return (result && can_be_compiled(m, comp_level)); aoqi@0: } aoqi@0: aoqi@0: bool CompilationPolicy::is_compilation_enabled() { aoqi@0: // NOTE: CompileBroker::should_compile_new_jobs() checks for UseCompiler aoqi@0: return !delay_compilation_during_startup() && CompileBroker::should_compile_new_jobs(); aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void CompilationPolicy::print_time() { aoqi@0: tty->print_cr ("Accumulated compilationPolicy times:"); aoqi@0: tty->print_cr ("---------------------------"); aoqi@0: tty->print_cr (" Total: %3.3f sec.", _accumulated_time.seconds()); aoqi@0: } aoqi@0: aoqi@0: void NonTieredCompPolicy::trace_osr_completion(nmethod* osr_nm) { aoqi@0: if (TraceOnStackReplacement) { aoqi@0: if (osr_nm == NULL) tty->print_cr("compilation failed"); aoqi@0: else tty->print_cr("nmethod " INTPTR_FORMAT, p2i(osr_nm)); aoqi@0: } aoqi@0: } aoqi@0: #endif // !PRODUCT aoqi@0: aoqi@0: void NonTieredCompPolicy::initialize() { aoqi@0: // Setup the compiler thread numbers aoqi@0: if (CICompilerCountPerCPU) { aoqi@0: // Example: if CICompilerCountPerCPU is true, then we get aoqi@0: // max(log2(8)-1,1) = 2 compiler threads on an 8-way machine. aoqi@0: // May help big-app startup time. aoqi@0: _compiler_count = MAX2(log2_intptr(os::active_processor_count())-1,1); aoqi@0: FLAG_SET_ERGO(intx, CICompilerCount, _compiler_count); aoqi@0: } else { aoqi@0: _compiler_count = CICompilerCount; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Note: this policy is used ONLY if TieredCompilation is off. aoqi@0: // compiler_count() behaves the following way: aoqi@0: // - with TIERED build (with both COMPILER1 and COMPILER2 defined) it should return aoqi@0: // zero for the c1 compilation levels, hence the particular ordering of the aoqi@0: // statements. aoqi@0: // - the same should happen when COMPILER2 is defined and COMPILER1 is not aoqi@0: // (server build without TIERED defined). aoqi@0: // - if only COMPILER1 is defined (client build), zero should be returned for aoqi@0: // the c2 level. aoqi@0: // - if neither is defined - always return zero. aoqi@0: int NonTieredCompPolicy::compiler_count(CompLevel comp_level) { aoqi@0: assert(!TieredCompilation, "This policy should not be used with TieredCompilation"); aoqi@0: #ifdef COMPILER2 aoqi@0: if (is_c2_compile(comp_level)) { aoqi@0: return _compiler_count; aoqi@0: } else { aoqi@0: return 0; aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: #ifdef COMPILER1 aoqi@0: if (is_c1_compile(comp_level)) { aoqi@0: return _compiler_count; aoqi@0: } else { aoqi@0: return 0; aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: void NonTieredCompPolicy::reset_counter_for_invocation_event(methodHandle m) { aoqi@0: // Make sure invocation and backedge counter doesn't overflow again right away aoqi@0: // as would be the case for native methods. aoqi@0: aoqi@0: // BUT also make sure the method doesn't look like it was never executed. aoqi@0: // Set carry bit and reduce counter's value to min(count, CompileThreshold/2). aoqi@0: MethodCounters* mcs = m->method_counters(); aoqi@0: assert(mcs != NULL, "MethodCounters cannot be NULL for profiling"); aoqi@0: mcs->invocation_counter()->set_carry(); aoqi@0: mcs->backedge_counter()->set_carry(); aoqi@0: aoqi@0: assert(!m->was_never_executed(), "don't reset to 0 -- could be mistaken for never-executed"); aoqi@0: } aoqi@0: aoqi@0: void NonTieredCompPolicy::reset_counter_for_back_branch_event(methodHandle m) { aoqi@0: // Delay next back-branch event but pump up invocation counter to triger aoqi@0: // whole method compilation. aoqi@0: MethodCounters* mcs = m->method_counters(); aoqi@0: assert(mcs != NULL, "MethodCounters cannot be NULL for profiling"); aoqi@0: InvocationCounter* i = mcs->invocation_counter(); aoqi@0: InvocationCounter* b = mcs->backedge_counter(); aoqi@0: aoqi@0: // Don't set invocation_counter's value too low otherwise the method will aoqi@0: // look like immature (ic < ~5300) which prevents the inlining based on aoqi@0: // the type profiling. aoqi@0: i->set(i->state(), CompileThreshold); aoqi@0: // Don't reset counter too low - it is used to check if OSR method is ready. aoqi@0: b->set(b->state(), CompileThreshold / 2); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // CounterDecay aoqi@0: // aoqi@0: // Interates through invocation counters and decrements them. This aoqi@0: // is done at each safepoint. aoqi@0: // aoqi@0: class CounterDecay : public AllStatic { aoqi@0: static jlong _last_timestamp; aoqi@0: static void do_method(Method* m) { aoqi@0: MethodCounters* mcs = m->method_counters(); aoqi@0: if (mcs != NULL) { aoqi@0: mcs->invocation_counter()->decay(); aoqi@0: } aoqi@0: } aoqi@0: public: aoqi@0: static void decay(); aoqi@0: static bool is_decay_needed() { aoqi@0: return (os::javaTimeMillis() - _last_timestamp) > CounterDecayMinIntervalLength; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: jlong CounterDecay::_last_timestamp = 0; aoqi@0: aoqi@0: void CounterDecay::decay() { aoqi@0: _last_timestamp = os::javaTimeMillis(); aoqi@0: aoqi@0: // This operation is going to be performed only at the end of a safepoint aoqi@0: // and hence GC's will not be going on, all Java mutators are suspended aoqi@0: // at this point and hence SystemDictionary_lock is also not needed. aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "can only be executed at a safepoint"); aoqi@0: int nclasses = SystemDictionary::number_of_classes(); aoqi@0: double classes_per_tick = nclasses * (CounterDecayMinIntervalLength * 1e-3 / aoqi@0: CounterHalfLifeTime); aoqi@0: for (int i = 0; i < classes_per_tick; i++) { aoqi@0: Klass* k = SystemDictionary::try_get_next_class(); aoqi@0: if (k != NULL && k->oop_is_instance()) { aoqi@0: InstanceKlass::cast(k)->methods_do(do_method); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Called at the end of the safepoint aoqi@0: void NonTieredCompPolicy::do_safepoint_work() { aoqi@0: if(UseCounterDecay && CounterDecay::is_decay_needed()) { aoqi@0: CounterDecay::decay(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void NonTieredCompPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) { aoqi@0: ScopeDesc* sd = trap_scope; aoqi@0: MethodCounters* mcs; aoqi@0: InvocationCounter* c; aoqi@0: for (; !sd->is_top(); sd = sd->sender()) { aoqi@0: mcs = sd->method()->method_counters(); aoqi@0: if (mcs != NULL) { aoqi@0: // Reset ICs of inlined methods, since they can trigger compilations also. aoqi@0: mcs->invocation_counter()->reset(); aoqi@0: } aoqi@0: } aoqi@0: mcs = sd->method()->method_counters(); aoqi@0: if (mcs != NULL) { aoqi@0: c = mcs->invocation_counter(); aoqi@0: if (is_osr) { aoqi@0: // It was an OSR method, so bump the count higher. aoqi@0: c->set(c->state(), CompileThreshold); aoqi@0: } else { aoqi@0: c->reset(); aoqi@0: } aoqi@0: mcs->backedge_counter()->reset(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // This method can be called by any component of the runtime to notify the policy aoqi@0: // that it's recommended to delay the complation of this method. aoqi@0: void NonTieredCompPolicy::delay_compilation(Method* method) { aoqi@0: MethodCounters* mcs = method->method_counters(); aoqi@0: if (mcs != NULL) { aoqi@0: mcs->invocation_counter()->decay(); aoqi@0: mcs->backedge_counter()->decay(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void NonTieredCompPolicy::disable_compilation(Method* method) { aoqi@0: MethodCounters* mcs = method->method_counters(); aoqi@0: if (mcs != NULL) { aoqi@0: mcs->invocation_counter()->set_state(InvocationCounter::wait_for_nothing); aoqi@0: mcs->backedge_counter()->set_state(InvocationCounter::wait_for_nothing); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: CompileTask* NonTieredCompPolicy::select_task(CompileQueue* compile_queue) { aoqi@0: return compile_queue->first(); aoqi@0: } aoqi@0: aoqi@0: bool NonTieredCompPolicy::is_mature(Method* method) { aoqi@0: MethodData* mdo = method->method_data(); aoqi@0: assert(mdo != NULL, "Should be"); aoqi@0: uint current = mdo->mileage_of(method); aoqi@0: uint initial = mdo->creation_mileage(); aoqi@0: if (current < initial) aoqi@0: return true; // some sort of overflow aoqi@0: uint target; aoqi@0: if (ProfileMaturityPercentage <= 0) aoqi@0: target = (uint) -ProfileMaturityPercentage; // absolute value aoqi@0: else aoqi@0: target = (uint)( (ProfileMaturityPercentage * CompileThreshold) / 100 ); aoqi@0: return (current >= initial + target); aoqi@0: } aoqi@0: aoqi@0: nmethod* NonTieredCompPolicy::event(methodHandle method, methodHandle inlinee, int branch_bci, aoqi@0: int bci, CompLevel comp_level, nmethod* nm, JavaThread* thread) { aoqi@0: assert(comp_level == CompLevel_none, "This should be only called from the interpreter"); aoqi@0: NOT_PRODUCT(trace_frequency_counter_overflow(method, branch_bci, bci)); aoqi@0: if (JvmtiExport::can_post_interpreter_events() && thread->is_interp_only_mode()) { aoqi@0: // If certain JVMTI events (e.g. frame pop event) are requested then the aoqi@0: // thread is forced to remain in interpreted code. This is aoqi@0: // implemented partly by a check in the run_compiled_code aoqi@0: // section of the interpreter whether we should skip running aoqi@0: // compiled code, and partly by skipping OSR compiles for aoqi@0: // interpreted-only threads. aoqi@0: if (bci != InvocationEntryBci) { aoqi@0: reset_counter_for_back_branch_event(method); aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: if (CompileTheWorld || ReplayCompiles) { aoqi@0: // Don't trigger other compiles in testing mode aoqi@0: if (bci == InvocationEntryBci) { aoqi@0: reset_counter_for_invocation_event(method); aoqi@0: } else { aoqi@0: reset_counter_for_back_branch_event(method); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: if (bci == InvocationEntryBci) { aoqi@0: // when code cache is full, compilation gets switched off, UseCompiler aoqi@0: // is set to false aoqi@0: if (!method->has_compiled_code() && UseCompiler) { aoqi@0: method_invocation_event(method, thread); aoqi@0: } else { aoqi@0: // Force counter overflow on method entry, even if no compilation aoqi@0: // happened. (The method_invocation_event call does this also.) aoqi@0: reset_counter_for_invocation_event(method); aoqi@0: } aoqi@0: // compilation at an invocation overflow no longer goes and retries test for aoqi@0: // compiled method. We always run the loser of the race as interpreted. aoqi@0: // so return NULL aoqi@0: return NULL; aoqi@0: } else { aoqi@0: // counter overflow in a loop => try to do on-stack-replacement aoqi@0: nmethod* osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true); aoqi@0: NOT_PRODUCT(trace_osr_request(method, osr_nm, bci)); aoqi@0: // when code cache is full, we should not compile any more... aoqi@0: if (osr_nm == NULL && UseCompiler) { aoqi@0: method_back_branch_event(method, bci, thread); aoqi@0: osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true); aoqi@0: } aoqi@0: if (osr_nm == NULL) { aoqi@0: reset_counter_for_back_branch_event(method); aoqi@0: return NULL; aoqi@0: } aoqi@0: return osr_nm; aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL aoqi@0: void NonTieredCompPolicy::trace_frequency_counter_overflow(methodHandle m, int branch_bci, int bci) { aoqi@0: if (TraceInvocationCounterOverflow) { aoqi@0: MethodCounters* mcs = m->method_counters(); aoqi@0: assert(mcs != NULL, "MethodCounters cannot be NULL for profiling"); aoqi@0: InvocationCounter* ic = mcs->invocation_counter(); aoqi@0: InvocationCounter* bc = mcs->backedge_counter(); aoqi@0: ResourceMark rm; aoqi@0: const char* msg = aoqi@0: bci == InvocationEntryBci aoqi@0: ? "comp-policy cntr ovfl @ %d in entry of " aoqi@0: : "comp-policy cntr ovfl @ %d in loop of "; aoqi@0: PRAGMA_DIAG_PUSH aoqi@0: PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL aoqi@0: tty->print(msg, bci); aoqi@0: PRAGMA_DIAG_POP aoqi@0: m->print_value(); aoqi@0: tty->cr(); aoqi@0: ic->print(); aoqi@0: bc->print(); aoqi@0: if (ProfileInterpreter) { aoqi@0: if (bci != InvocationEntryBci) { aoqi@0: MethodData* mdo = m->method_data(); aoqi@0: if (mdo != NULL) { aoqi@0: int count = mdo->bci_to_data(branch_bci)->as_JumpData()->taken(); aoqi@0: tty->print_cr("back branch count = %d", count); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void NonTieredCompPolicy::trace_osr_request(methodHandle method, nmethod* osr, int bci) { aoqi@0: if (TraceOnStackReplacement) { aoqi@0: ResourceMark rm; aoqi@0: tty->print(osr != NULL ? "Reused OSR entry for " : "Requesting OSR entry for "); aoqi@0: method->print_short_name(tty); aoqi@0: tty->print_cr(" at bci %d", bci); aoqi@0: } aoqi@0: } aoqi@0: #endif // !PRODUCT aoqi@0: aoqi@0: // SimpleCompPolicy - compile current method aoqi@0: aoqi@0: void SimpleCompPolicy::method_invocation_event(methodHandle m, JavaThread* thread) { aoqi@0: const int comp_level = CompLevel_highest_tier; aoqi@0: const int hot_count = m->invocation_count(); aoqi@0: reset_counter_for_invocation_event(m); aoqi@0: const char* comment = "count"; aoqi@0: aoqi@0: if (is_compilation_enabled() && can_be_compiled(m, comp_level)) { aoqi@0: nmethod* nm = m->code(); aoqi@0: if (nm == NULL ) { aoqi@0: CompileBroker::compile_method(m, InvocationEntryBci, comp_level, m, hot_count, comment, thread); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void SimpleCompPolicy::method_back_branch_event(methodHandle m, int bci, JavaThread* thread) { aoqi@0: const int comp_level = CompLevel_highest_tier; aoqi@0: const int hot_count = m->backedge_count(); aoqi@0: const char* comment = "backedge_count"; aoqi@0: aoqi@0: if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) { aoqi@0: CompileBroker::compile_method(m, bci, comp_level, m, hot_count, comment, thread); aoqi@0: NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));) aoqi@0: } aoqi@0: } aoqi@0: // StackWalkCompPolicy - walk up stack to find a suitable method to compile aoqi@0: aoqi@0: #ifdef COMPILER2 aoqi@0: const char* StackWalkCompPolicy::_msg = NULL; aoqi@0: aoqi@0: aoqi@0: // Consider m for compilation aoqi@0: void StackWalkCompPolicy::method_invocation_event(methodHandle m, JavaThread* thread) { aoqi@0: const int comp_level = CompLevel_highest_tier; aoqi@0: const int hot_count = m->invocation_count(); aoqi@0: reset_counter_for_invocation_event(m); aoqi@0: const char* comment = "count"; aoqi@0: aoqi@0: if (is_compilation_enabled() && m->code() == NULL && can_be_compiled(m, comp_level)) { aoqi@0: ResourceMark rm(thread); aoqi@0: frame fr = thread->last_frame(); aoqi@0: assert(fr.is_interpreted_frame(), "must be interpreted"); aoqi@0: assert(fr.interpreter_frame_method() == m(), "bad method"); aoqi@0: aoqi@0: if (TraceCompilationPolicy) { aoqi@0: tty->print("method invocation trigger: "); aoqi@0: m->print_short_name(tty); aoqi@0: tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)m()), m->code_size()); aoqi@0: } aoqi@0: RegisterMap reg_map(thread, false); aoqi@0: javaVFrame* triggerVF = thread->last_java_vframe(®_map); aoqi@0: // triggerVF is the frame that triggered its counter aoqi@0: RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m); aoqi@0: aoqi@0: if (first->top_method()->code() != NULL) { aoqi@0: // called obsolete method/nmethod -- no need to recompile aoqi@0: if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, p2i(first->top_method()->code())); aoqi@0: } else { aoqi@0: if (TimeCompilationPolicy) accumulated_time()->start(); aoqi@0: GrowableArray* stack = new GrowableArray(50); aoqi@0: stack->push(first); aoqi@0: RFrame* top = findTopInlinableFrame(stack); aoqi@0: if (TimeCompilationPolicy) accumulated_time()->stop(); aoqi@0: assert(top != NULL, "findTopInlinableFrame returned null"); aoqi@0: if (TraceCompilationPolicy) top->print(); aoqi@0: CompileBroker::compile_method(top->top_method(), InvocationEntryBci, comp_level, aoqi@0: m, hot_count, comment, thread); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void StackWalkCompPolicy::method_back_branch_event(methodHandle m, int bci, JavaThread* thread) { aoqi@0: const int comp_level = CompLevel_highest_tier; aoqi@0: const int hot_count = m->backedge_count(); aoqi@0: const char* comment = "backedge_count"; aoqi@0: aoqi@0: if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) { aoqi@0: CompileBroker::compile_method(m, bci, comp_level, m, hot_count, comment, thread); aoqi@0: NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));) aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray* stack) { aoqi@0: // go up the stack until finding a frame that (probably) won't be inlined aoqi@0: // into its caller aoqi@0: RFrame* current = stack->at(0); // current choice for stopping aoqi@0: assert( current && !current->is_compiled(), "" ); aoqi@0: const char* msg = NULL; aoqi@0: aoqi@0: while (1) { aoqi@0: aoqi@0: // before going up the stack further, check if doing so would get us into aoqi@0: // compiled code aoqi@0: RFrame* next = senderOf(current, stack); aoqi@0: if( !next ) // No next frame up the stack? aoqi@0: break; // Then compile with current frame aoqi@0: aoqi@0: methodHandle m = current->top_method(); aoqi@0: methodHandle next_m = next->top_method(); aoqi@0: aoqi@0: if (TraceCompilationPolicy && Verbose) { aoqi@0: tty->print("[caller: "); aoqi@0: next_m->print_short_name(tty); aoqi@0: tty->print("] "); aoqi@0: } aoqi@0: aoqi@0: if( !Inline ) { // Inlining turned off aoqi@0: msg = "Inlining turned off"; aoqi@0: break; aoqi@0: } aoqi@0: if (next_m->is_not_compilable()) { // Did fail to compile this before/ aoqi@0: msg = "caller not compilable"; aoqi@0: break; aoqi@0: } aoqi@0: if (next->num() > MaxRecompilationSearchLength) { aoqi@0: // don't go up too high when searching for recompilees aoqi@0: msg = "don't go up any further: > MaxRecompilationSearchLength"; aoqi@0: break; aoqi@0: } aoqi@0: if (next->distance() > MaxInterpretedSearchLength) { aoqi@0: // don't go up too high when searching for recompilees aoqi@0: msg = "don't go up any further: next > MaxInterpretedSearchLength"; aoqi@0: break; aoqi@0: } aoqi@0: // Compiled frame above already decided not to inline; aoqi@0: // do not recompile him. aoqi@0: if (next->is_compiled()) { aoqi@0: msg = "not going up into optimized code"; aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: // Interpreted frame above us was already compiled. Do not force aoqi@0: // a recompile, although if the frame above us runs long enough an aoqi@0: // OSR might still happen. aoqi@0: if( current->is_interpreted() && next_m->has_compiled_code() ) { aoqi@0: msg = "not going up -- already compiled caller"; aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: // Compute how frequent this call site is. We have current method 'm'. aoqi@0: // We know next method 'next_m' is interpreted. Find the call site and aoqi@0: // check the various invocation counts. aoqi@0: int invcnt = 0; // Caller counts aoqi@0: if (ProfileInterpreter) { aoqi@0: invcnt = next_m->interpreter_invocation_count(); aoqi@0: } aoqi@0: int cnt = 0; // Call site counts aoqi@0: if (ProfileInterpreter && next_m->method_data() != NULL) { aoqi@0: ResourceMark rm; aoqi@0: int bci = next->top_vframe()->bci(); aoqi@0: ProfileData* data = next_m->method_data()->bci_to_data(bci); aoqi@0: if (data != NULL && data->is_CounterData()) aoqi@0: cnt = data->as_CounterData()->count(); aoqi@0: } aoqi@0: aoqi@0: // Caller counts / call-site counts; i.e. is this call site aoqi@0: // a hot call site for method next_m? aoqi@0: int freq = (invcnt) ? cnt/invcnt : cnt; aoqi@0: aoqi@0: // Check size and frequency limits aoqi@0: if ((msg = shouldInline(m, freq, cnt)) != NULL) { aoqi@0: break; aoqi@0: } aoqi@0: // Check inlining negative tests aoqi@0: if ((msg = shouldNotInline(m)) != NULL) { aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // If the caller method is too big or something then we do not want to aoqi@0: // compile it just to inline a method aoqi@0: if (!can_be_compiled(next_m, CompLevel_any)) { aoqi@0: msg = "caller cannot be compiled"; aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: if( next_m->name() == vmSymbols::class_initializer_name() ) { aoqi@0: msg = "do not compile class initializer (OSR ok)"; aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: if (TraceCompilationPolicy && Verbose) { aoqi@0: tty->print("\n\t check caller: "); aoqi@0: next_m->print_short_name(tty); aoqi@0: tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)next_m()), next_m->code_size()); aoqi@0: } aoqi@0: aoqi@0: current = next; aoqi@0: } aoqi@0: aoqi@0: assert( !current || !current->is_compiled(), "" ); aoqi@0: aoqi@0: if (TraceCompilationPolicy && msg) tty->print("(%s)\n", msg); aoqi@0: aoqi@0: return current; aoqi@0: } aoqi@0: aoqi@0: RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray* stack) { aoqi@0: RFrame* sender = rf->caller(); aoqi@0: if (sender && sender->num() == stack->length()) stack->push(sender); aoqi@0: return sender; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: const char* StackWalkCompPolicy::shouldInline(methodHandle m, float freq, int cnt) { aoqi@0: // Allows targeted inlining aoqi@0: // positive filter: should send be inlined? returns NULL (--> yes) aoqi@0: // or rejection msg aoqi@0: int max_size = MaxInlineSize; aoqi@0: int cost = m->code_size(); aoqi@0: aoqi@0: // Check for too many throws (and not too huge) aoqi@0: if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // bump the max size if the call is frequent aoqi@0: if ((freq >= InlineFrequencyRatio) || (cnt >= InlineFrequencyCount)) { aoqi@0: if (TraceFrequencyInlining) { aoqi@0: tty->print("(Inlined frequent method)\n"); aoqi@0: m->print(); aoqi@0: } aoqi@0: max_size = FreqInlineSize; aoqi@0: } aoqi@0: if (cost > max_size) { aoqi@0: return (_msg = "too big"); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: const char* StackWalkCompPolicy::shouldNotInline(methodHandle m) { aoqi@0: // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg aoqi@0: if (m->is_abstract()) return (_msg = "abstract method"); aoqi@0: // note: we allow ik->is_abstract() aoqi@0: if (!m->method_holder()->is_initialized()) return (_msg = "method holder not initialized"); aoqi@0: if (m->is_native()) return (_msg = "native method"); aoqi@0: nmethod* m_code = m->code(); aoqi@0: if (m_code != NULL && m_code->code_size() > InlineSmallCode) aoqi@0: return (_msg = "already compiled into a big method"); aoqi@0: aoqi@0: // use frequency-based objections only for non-trivial methods aoqi@0: if (m->code_size() <= MaxTrivialSize) return NULL; aoqi@0: if (UseInterpreter) { // don't use counts with -Xcomp aoqi@0: if ((m->code() == NULL) && m->was_never_executed()) return (_msg = "never executed"); aoqi@0: if (!m->was_executed_more_than(MIN2(MinInliningThreshold, CompileThreshold >> 1))) return (_msg = "executed < MinInliningThreshold times"); aoqi@0: } aoqi@0: if (Method::has_unloaded_classes_in_signature(m, JavaThread::current())) return (_msg = "unloaded signature classes"); aoqi@0: aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: #endif // COMPILER2