13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_VM_INTERPRETER_INVOCATIONCOUNTER_HPP
26 #define SHARE_VM_INTERPRETER_INVOCATIONCOUNTER_HPP
27
28 #include "memory/allocation.hpp"
29 #include "runtime/handles.hpp"
30 #include "utilities/exceptions.hpp"
31
32 // InvocationCounters are used to trigger actions when a limit (threshold) is reached.
33 // For different states, different limits and actions can be defined in the initialization
34 // routine of InvocationCounters.
35 //
36 // Implementation notes: For space reasons, state & counter are both encoded in one word,
37 // The state is encoded using some of the least significant bits, the counter is using the
38 // more significant bits. The counter is incremented before a method is activated and an
39 // action is triggered when when count() > limit().
40
41 class InvocationCounter VALUE_OBJ_CLASS_SPEC {
42 friend class VMStructs;
43 friend class ciReplay;
44 private: // bit no: |31 3| 2 | 1 0 |
45 unsigned int _counter; // format: [count|carry|state]
46
47 enum PrivateConstants {
48 number_of_state_bits = 2,
49 number_of_carry_bits = 1,
50 number_of_noncount_bits = number_of_state_bits + number_of_carry_bits,
51 number_of_count_bits = BitsPerInt - number_of_noncount_bits,
52 state_limit = nth_bit(number_of_state_bits),
53 count_grain = nth_bit(number_of_state_bits + number_of_carry_bits),
54 carry_mask = right_n_bits(number_of_carry_bits) << number_of_state_bits,
55 state_mask = right_n_bits(number_of_state_bits),
56 status_mask = right_n_bits(number_of_state_bits + number_of_carry_bits),
57 count_mask = ((int)(-1) ^ status_mask)
58 };
59
60 public:
61 static int InterpreterInvocationLimit; // CompileThreshold scaled for interpreter use
62 static int InterpreterBackwardBranchLimit; // A separate threshold for on stack replacement
63 static int InterpreterProfileLimit; // Profiling threshold scaled for interpreter use
64
65 typedef address (*Action)(methodHandle method, TRAPS);
66
67 enum PublicConstants {
68 count_increment = count_grain, // use this value to increment the 32bit _counter word
69 count_mask_value = count_mask, // use this value to mask the backedge counter
70 count_shift = number_of_noncount_bits,
71 count_limit = nth_bit(number_of_count_bits - 1)
72 };
73
74 enum State {
75 wait_for_nothing, // do nothing when count() > limit()
76 wait_for_compile, // introduce nmethod when count() > limit()
77 number_of_states // must be <= state_limit
78 };
79
80 // Manipulation
81 void reset(); // sets state to wait state
82 void init(); // sets state into original state
83 void set_state(State state); // sets state and initializes counter correspondingly
84 inline void set(State state, int count); // sets state and counter
85 inline void decay(); // decay counter (divide by two)
86 void set_carry(); // set the sticky carry bit
87 void set_carry_flag() { _counter |= carry_mask; }
88
89 int raw_counter() { return _counter; }
90
91 // Accessors
92 State state() const { return (State)(_counter & state_mask); }
93 bool carry() const { return (_counter & carry_mask) != 0; }
94 int limit() const { return CompileThreshold; }
95 Action action() const { return _action[state()]; }
96 int count() const { return _counter >> number_of_noncount_bits; }
97
98 int get_InvocationLimit() const { return InterpreterInvocationLimit >> number_of_noncount_bits; }
99 int get_BackwardBranchLimit() const { return InterpreterBackwardBranchLimit >> number_of_noncount_bits; }
100 int get_ProfileLimit() const { return InterpreterProfileLimit >> number_of_noncount_bits; }
101
102 #ifdef CC_INTERP
103 // Test counter using scaled limits like the asm interpreter would do rather than doing
104 // the shifts to normalize the counter.
105 // Checks sum of invocation_counter and backedge_counter as the template interpreter does.
106 bool reached_InvocationLimit(InvocationCounter *back_edge_count) const {
107 return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >=
108 (unsigned int) InterpreterInvocationLimit;
109 }
110 bool reached_BackwardBranchLimit(InvocationCounter *back_edge_count) const {
111 return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >=
112 (unsigned int) InterpreterBackwardBranchLimit;
113 }
114 // Do this just like asm interpreter does for max speed.
115 bool reached_ProfileLimit(InvocationCounter *back_edge_count) const {
116 return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >=
117 (unsigned int) InterpreterProfileLimit;
118 }
119 #endif // CC_INTERP
120
121 void increment() { _counter += count_increment; }
122
123
124 // Printing
125 void print();
126 void print_short();
127
128 // Miscellaneous
129 static ByteSize counter_offset() { return byte_offset_of(InvocationCounter, _counter); }
130 static void reinitialize(bool delay_overflow);
131
132 private:
133 static int _init [number_of_states]; // the counter limits
134 static Action _action[number_of_states]; // the actions
135
136 static void def(State state, int init, Action action);
137 static const char* state_as_string(State state);
138 static const char* state_as_short_string(State state);
139 };
140
141 inline void InvocationCounter::set(State state, int count) {
142 assert(0 <= state && state < number_of_states, "illegal state");
143 int carry = (_counter & carry_mask); // the carry bit is sticky
144 _counter = (count << number_of_noncount_bits) | carry | state;
145 }
146
147 inline void InvocationCounter::decay() {
148 int c = count();
149 int new_count = c >> 1;
150 // prevent from going to zero, to distinguish from never-executed methods
151 if (c > 0 && new_count == 0) new_count = 1;
152 set(state(), new_count);
153 }
154
155
156 #endif // SHARE_VM_INTERPRETER_INVOCATIONCOUNTER_HPP
|
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_VM_INTERPRETER_INVOCATIONCOUNTER_HPP
26 #define SHARE_VM_INTERPRETER_INVOCATIONCOUNTER_HPP
27
28 #include "memory/allocation.hpp"
29 #include "runtime/handles.hpp"
30 #include "utilities/exceptions.hpp"
31
32 // InvocationCounters are used to trigger actions when a limit (threshold) is reached.
33 //
34 // Implementation notes: For space reasons, counter & carry are both encoded in one word,
35 // The carry is encoded using the least significant bits the counter is using the
36 // more significant bits. The counter is incremented before a method is activated and an
37 // action is triggered when when count() > the limit for that counter type.
38
39 class InvocationCounter VALUE_OBJ_CLASS_SPEC {
40 friend class VMStructs;
41 friend class ciReplay;
42 private: // bit no: |31 1| 0 |
43 unsigned int _counter; // format: [count|carry|
44
45 enum PrivateConstants {
46 number_of_carry_bits = 1,
47 number_of_noncount_bits = number_of_carry_bits,
48 number_of_count_bits = BitsPerInt - number_of_noncount_bits,
49 count_grain = nth_bit(number_of_carry_bits),
50 carry_mask = right_n_bits(number_of_carry_bits),
51 count_mask = ((int)(-1) ^ carry_mask)
52 };
53
54 public:
55 static int InterpreterInvocationLimit; // CompileThreshold scaled for interpreter use
56 static int InterpreterBackwardBranchLimit; // A separate threshold for on stack replacement
57 static int InterpreterProfileLimit; // Profiling threshold scaled for interpreter use
58
59 enum PublicConstants {
60 count_increment = count_grain, // use this value to increment the 32bit _counter word
61 count_mask_value = count_mask, // use this value to mask the backedge counter
62 count_shift = number_of_noncount_bits,
63 count_limit = nth_bit(number_of_count_bits - 1)
64 };
65
66 // Manipulation
67 void reset(); // reset counter, but not to zero
68 void init(); // sets counters into original state
69 inline void set_count(int count); // sets counter
70 inline void decay(); // decay counter (divide by two)
71 void set_carry_and_reduce(); // set the sticky carry bit and reduce counter
72 void set_carry_if_necessary(); // set carry if counter is big enough
73 void increment() { _counter += count_increment; }
74
75 // primitive read and write
76 uint raw_counter() { return _counter; }
77 private:
78 void set_raw(uint counter) { _counter = counter; }
79
80 // operate on local counter without committing to memory
81 inline void set_count(uint* counter, int count);
82 void set_carry_flag(uint* counter) { *counter |= carry_mask; }
83 int count(uint counter) { return counter >> number_of_noncount_bits; }
84 bool carry(uint counter) { return (counter & carry_mask) != 0; }
85
86 public:
87 // Accessors
88 bool carry() const { return (_counter & carry_mask) != 0; }
89 int count() const { return _counter >> number_of_noncount_bits; }
90
91 int get_InvocationLimit() const { return InterpreterInvocationLimit >> number_of_noncount_bits; }
92 int get_BackwardBranchLimit() const { return InterpreterBackwardBranchLimit >> number_of_noncount_bits; }
93 int get_ProfileLimit() const { return InterpreterProfileLimit >> number_of_noncount_bits; }
94
95 #ifdef CC_INTERP
96 // Test counter using scaled limits like the asm interpreter would do rather than doing
97 // the shifts to normalize the counter.
98 // Checks sum of invocation_counter and backedge_counter as the template interpreter does.
99 bool reached_InvocationLimit(InvocationCounter *back_edge_count) const {
100 return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >=
101 (unsigned int) InterpreterInvocationLimit;
102 }
103 bool reached_BackwardBranchLimit(InvocationCounter *back_edge_count) const {
104 return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >=
105 (unsigned int) InterpreterBackwardBranchLimit;
106 }
107 // Do this just like asm interpreter does for max speed.
108 bool reached_ProfileLimit(InvocationCounter *back_edge_count) const {
109 return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >=
110 (unsigned int) InterpreterProfileLimit;
111 }
112 #endif // CC_INTERP
113
114 // Printing
115 void print();
116 void print_short();
117
118 // Miscellaneous
119 static ByteSize counter_offset() { return byte_offset_of(InvocationCounter, _counter); }
120 static void reinitialize(bool delay_overflow);
121 };
122
123 inline void InvocationCounter::set_count(int count) {
124 _counter = (count << number_of_noncount_bits) | (_counter & carry_mask);
125 }
126
127 inline void InvocationCounter::set_count(uint *counter, int count) {
128 *counter = (count << number_of_noncount_bits) | (*counter & carry_mask);
129 }
130
131 inline void InvocationCounter::decay() {
132 int c = count();
133 int new_count = c >> 1;
134 // prevent from going to zero, to distinguish from never-executed methods
135 if (c > 0 && new_count == 0) new_count = 1;
136 set_count(new_count);
137 }
138
139 #endif // SHARE_VM_INTERPRETER_INVOCATIONCOUNTER_HPP
|