aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, 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: #ifndef SHARE_VM_INTERPRETER_INVOCATIONCOUNTER_HPP aoqi@0: #define SHARE_VM_INTERPRETER_INVOCATIONCOUNTER_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "runtime/handles.hpp" aoqi@0: #include "utilities/exceptions.hpp" aoqi@0: aoqi@0: // InvocationCounters are used to trigger actions when a limit (threshold) is reached. aoqi@0: // For different states, different limits and actions can be defined in the initialization aoqi@0: // routine of InvocationCounters. aoqi@0: // aoqi@0: // Implementation notes: For space reasons, state & counter are both encoded in one word, aoqi@0: // The state is encoded using some of the least significant bits, the counter is using the aoqi@0: // more significant bits. The counter is incremented before a method is activated and an aoqi@0: // action is triggered when when count() > limit(). aoqi@0: aoqi@0: class InvocationCounter VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class VMStructs; aoqi@0: friend class ciReplay; aoqi@0: private: // bit no: |31 3| 2 | 1 0 | aoqi@0: unsigned int _counter; // format: [count|carry|state] aoqi@0: aoqi@0: enum PrivateConstants { aoqi@0: number_of_state_bits = 2, aoqi@0: number_of_carry_bits = 1, aoqi@0: number_of_noncount_bits = number_of_state_bits + number_of_carry_bits, aoqi@0: number_of_count_bits = BitsPerInt - number_of_noncount_bits, aoqi@0: state_limit = nth_bit(number_of_state_bits), aoqi@0: count_grain = nth_bit(number_of_state_bits + number_of_carry_bits), aoqi@0: carry_mask = right_n_bits(number_of_carry_bits) << number_of_state_bits, aoqi@0: state_mask = right_n_bits(number_of_state_bits), aoqi@0: status_mask = right_n_bits(number_of_state_bits + number_of_carry_bits), aoqi@0: count_mask = ((int)(-1) ^ status_mask) aoqi@0: }; aoqi@0: aoqi@0: public: aoqi@0: static int InterpreterInvocationLimit; // CompileThreshold scaled for interpreter use aoqi@0: static int InterpreterBackwardBranchLimit; // A separate threshold for on stack replacement aoqi@0: static int InterpreterProfileLimit; // Profiling threshold scaled for interpreter use aoqi@0: aoqi@0: typedef address (*Action)(methodHandle method, TRAPS); aoqi@0: aoqi@0: enum PublicConstants { aoqi@0: count_increment = count_grain, // use this value to increment the 32bit _counter word aoqi@0: count_mask_value = count_mask, // use this value to mask the backedge counter aoqi@0: count_shift = number_of_noncount_bits, aoqi@0: count_limit = nth_bit(number_of_count_bits - 1) aoqi@0: }; aoqi@0: aoqi@0: enum State { aoqi@0: wait_for_nothing, // do nothing when count() > limit() aoqi@0: wait_for_compile, // introduce nmethod when count() > limit() aoqi@0: number_of_states // must be <= state_limit aoqi@0: }; aoqi@0: aoqi@0: // Manipulation aoqi@0: void reset(); // sets state to wait state aoqi@0: void init(); // sets state into original state aoqi@0: void set_state(State state); // sets state and initializes counter correspondingly aoqi@0: inline void set(State state, int count); // sets state and counter aoqi@0: inline void decay(); // decay counter (divide by two) aoqi@0: void set_carry(); // set the sticky carry bit aoqi@0: void set_carry_flag() { _counter |= carry_mask; } aoqi@0: aoqi@0: int raw_counter() { return _counter; } aoqi@0: aoqi@0: // Accessors aoqi@0: State state() const { return (State)(_counter & state_mask); } aoqi@0: bool carry() const { return (_counter & carry_mask) != 0; } aoqi@0: int limit() const { return CompileThreshold; } aoqi@0: Action action() const { return _action[state()]; } aoqi@0: int count() const { return _counter >> number_of_noncount_bits; } aoqi@0: aoqi@0: int get_InvocationLimit() const { return InterpreterInvocationLimit >> number_of_noncount_bits; } aoqi@0: int get_BackwardBranchLimit() const { return InterpreterBackwardBranchLimit >> number_of_noncount_bits; } aoqi@0: int get_ProfileLimit() const { return InterpreterProfileLimit >> number_of_noncount_bits; } aoqi@0: aoqi@0: #ifdef CC_INTERP aoqi@0: // Test counter using scaled limits like the asm interpreter would do rather than doing aoqi@0: // the shifts to normalize the counter. aoqi@0: // Checks sum of invocation_counter and backedge_counter as the template interpreter does. aoqi@0: bool reached_InvocationLimit(InvocationCounter *back_edge_count) const { aoqi@0: return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >= aoqi@0: (unsigned int) InterpreterInvocationLimit; aoqi@0: } aoqi@0: bool reached_BackwardBranchLimit(InvocationCounter *back_edge_count) const { aoqi@0: return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >= aoqi@0: (unsigned int) InterpreterBackwardBranchLimit; aoqi@0: } aoqi@0: // Do this just like asm interpreter does for max speed. aoqi@0: bool reached_ProfileLimit(InvocationCounter *back_edge_count) const { aoqi@0: return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >= aoqi@0: (unsigned int) InterpreterProfileLimit; aoqi@0: } aoqi@0: #endif // CC_INTERP aoqi@0: aoqi@0: void increment() { _counter += count_increment; } aoqi@0: aoqi@0: aoqi@0: // Printing aoqi@0: void print(); aoqi@0: void print_short(); aoqi@0: aoqi@0: // Miscellaneous aoqi@0: static ByteSize counter_offset() { return byte_offset_of(InvocationCounter, _counter); } aoqi@0: static void reinitialize(bool delay_overflow); aoqi@0: aoqi@0: private: aoqi@0: static int _init [number_of_states]; // the counter limits aoqi@0: static Action _action[number_of_states]; // the actions aoqi@0: aoqi@0: static void def(State state, int init, Action action); aoqi@0: static const char* state_as_string(State state); aoqi@0: static const char* state_as_short_string(State state); aoqi@0: }; aoqi@0: aoqi@0: inline void InvocationCounter::set(State state, int count) { aoqi@0: assert(0 <= state && state < number_of_states, "illegal state"); aoqi@0: int carry = (_counter & carry_mask); // the carry bit is sticky aoqi@0: _counter = (count << number_of_noncount_bits) | carry | state; aoqi@0: } aoqi@0: aoqi@0: inline void InvocationCounter::decay() { aoqi@0: int c = count(); aoqi@0: int new_count = c >> 1; aoqi@0: // prevent from going to zero, to distinguish from never-executed methods aoqi@0: if (c > 0 && new_count == 0) new_count = 1; aoqi@0: set(state(), new_count); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #endif // SHARE_VM_INTERPRETER_INVOCATIONCOUNTER_HPP