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