duke@435: /* jiangli@4936: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "interpreter/invocationCounter.hpp" stefank@2314: #include "runtime/frame.hpp" stefank@2314: #include "runtime/handles.inline.hpp" duke@435: duke@435: duke@435: // Implementation of InvocationCounter duke@435: duke@435: void InvocationCounter::init() { duke@435: _counter = 0; // reset all the bits, including the sticky carry duke@435: reset(); duke@435: } duke@435: duke@435: void InvocationCounter::reset() { duke@435: // Only reset the state and don't make the method look like it's never duke@435: // been executed duke@435: set_state(wait_for_compile); duke@435: } duke@435: duke@435: void InvocationCounter::set_carry() { iveresov@2138: set_carry_flag(); duke@435: // The carry bit now indicates that this counter had achieved a very duke@435: // large value. Now reduce the value, so that the method can be duke@435: // executed many more times before re-entering the VM. duke@435: int old_count = count(); duke@435: int new_count = MIN2(old_count, (int) (CompileThreshold / 2)); coleenp@1089: // prevent from going to zero, to distinguish from never-executed methods coleenp@1089: if (new_count == 0) new_count = 1; duke@435: if (old_count != new_count) set(state(), new_count); duke@435: } duke@435: duke@435: void InvocationCounter::set_state(State state) { duke@435: assert(0 <= state && state < number_of_states, "illegal state"); duke@435: int init = _init[state]; duke@435: // prevent from going to zero, to distinguish from never-executed methods duke@435: if (init == 0 && count() > 0) init = 1; duke@435: int carry = (_counter & carry_mask); // the carry bit is sticky duke@435: _counter = (init << number_of_noncount_bits) | carry | state; duke@435: } duke@435: duke@435: duke@435: void InvocationCounter::print() { duke@435: tty->print_cr("invocation count: up = %d, limit = %d, carry = %s, state = %s", duke@435: count(), limit(), duke@435: carry() ? "true" : "false", duke@435: state_as_string(state())); duke@435: } duke@435: duke@435: void InvocationCounter::print_short() { duke@435: tty->print(" [%d%s;%s]", count(), carry()?"+carry":"", state_as_short_string(state())); duke@435: } duke@435: duke@435: // Initialization duke@435: duke@435: int InvocationCounter::_init [InvocationCounter::number_of_states]; duke@435: InvocationCounter::Action InvocationCounter::_action[InvocationCounter::number_of_states]; duke@435: int InvocationCounter::InterpreterInvocationLimit; duke@435: int InvocationCounter::InterpreterBackwardBranchLimit; duke@435: int InvocationCounter::InterpreterProfileLimit; duke@435: duke@435: duke@435: const char* InvocationCounter::state_as_string(State state) { duke@435: switch (state) { duke@435: case wait_for_nothing : return "wait_for_nothing"; duke@435: case wait_for_compile : return "wait_for_compile"; duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: duke@435: const char* InvocationCounter::state_as_short_string(State state) { duke@435: switch (state) { duke@435: case wait_for_nothing : return "not comp."; duke@435: case wait_for_compile : return "compileable"; duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: static address do_nothing(methodHandle method, TRAPS) { duke@435: // dummy action for inactive invocation counters jiangli@4936: MethodCounters* mcs = method->method_counters(); jiangli@4936: assert(mcs != NULL, ""); jiangli@4936: mcs->invocation_counter()->set_carry(); jiangli@4936: mcs->invocation_counter()->set_state(InvocationCounter::wait_for_nothing); duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: static address do_decay(methodHandle method, TRAPS) { duke@435: // decay invocation counters so compilation gets delayed jiangli@4936: MethodCounters* mcs = method->method_counters(); jiangli@4936: assert(mcs != NULL, ""); jiangli@4936: mcs->invocation_counter()->decay(); duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: void InvocationCounter::def(State state, int init, Action action) { duke@435: assert(0 <= state && state < number_of_states, "illegal state"); duke@435: assert(0 <= init && init < count_limit, "initial value out of range"); duke@435: _init [state] = init; duke@435: _action[state] = action; duke@435: } duke@435: duke@435: address dummy_invocation_counter_overflow(methodHandle m, TRAPS) { duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: duke@435: void InvocationCounter::reinitialize(bool delay_overflow) { duke@435: // define states duke@435: guarantee((int)number_of_states <= (int)state_limit, "adjust number_of_state_bits"); duke@435: def(wait_for_nothing, 0, do_nothing); duke@435: if (delay_overflow) { duke@435: def(wait_for_compile, 0, do_decay); duke@435: } else { duke@435: def(wait_for_compile, 0, dummy_invocation_counter_overflow); duke@435: } duke@435: duke@435: InterpreterInvocationLimit = CompileThreshold << number_of_noncount_bits; duke@435: InterpreterProfileLimit = ((CompileThreshold * InterpreterProfilePercentage) / 100)<< number_of_noncount_bits; duke@435: duke@435: // When methodData is collected, the backward branch limit is compared against a duke@435: // methodData counter, rather than an InvocationCounter. In the former case, we duke@435: // don't need the shift by number_of_noncount_bits, but we do need to adjust duke@435: // the factor by which we scale the threshold. duke@435: if (ProfileInterpreter) { duke@435: InterpreterBackwardBranchLimit = (CompileThreshold * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100; duke@435: } else { duke@435: InterpreterBackwardBranchLimit = ((CompileThreshold * OnStackReplacePercentage) / 100) << number_of_noncount_bits; duke@435: } duke@435: duke@435: assert(0 <= InterpreterBackwardBranchLimit, duke@435: "OSR threshold should be non-negative"); duke@435: assert(0 <= InterpreterProfileLimit && duke@435: InterpreterProfileLimit <= InterpreterInvocationLimit, duke@435: "profile threshold should be less than the compilation threshold " duke@435: "and non-negative"); duke@435: } duke@435: duke@435: void invocationCounter_init() { duke@435: InvocationCounter::reinitialize(DelayCompilationDuringStartup); duke@435: }