1/*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
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_INTERPRETER_INVOCATIONCOUNTER_HPP
26#define SHARE_INTERPRETER_INVOCATIONCOUNTER_HPP
27
28#include "runtime/handles.hpp"
29#include "utilities/exceptions.hpp"
30
31// InvocationCounters are used to trigger actions when a limit (threshold) is reached.
32// For different states, different limits and actions can be defined in the initialization
33// routine of InvocationCounters.
34//
35// Implementation notes: For space reasons, state & counter are both encoded in one word,
36// The state is encoded using some of the least significant bits, the counter is using the
37// more significant bits. The counter is incremented before a method is activated and an
38// action is triggered when count() > limit().
39
40class InvocationCounter {
41 friend class VMStructs;
42 friend class JVMCIVMStructs;
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 state_limit = nth_bit(number_of_state_bits),
52 count_grain = nth_bit(number_of_state_bits + number_of_carry_bits),
53 carry_mask = right_n_bits(number_of_carry_bits) << number_of_state_bits,
54 state_mask = right_n_bits(number_of_state_bits),
55 status_mask = right_n_bits(number_of_state_bits + number_of_carry_bits),
56 count_mask = ((int)(-1) ^ status_mask)
57 };
58
59 public:
60 typedef address (*Action)(const methodHandle& method, TRAPS);
61
62 enum PublicConstants {
63 count_increment = count_grain, // use this value to increment the 32bit _counter word
64 count_mask_value = count_mask, // use this value to mask the backedge counter
65 count_shift = number_of_noncount_bits,
66 number_of_count_bits = BitsPerInt - number_of_noncount_bits,
67 count_limit = nth_bit(number_of_count_bits - 1)
68 };
69
70 enum State {
71 wait_for_nothing, // do nothing when count() > limit()
72 wait_for_compile, // introduce nmethod when count() > limit()
73 number_of_states // must be <= state_limit
74 };
75
76 // Manipulation
77 void reset(); // sets state to wait state
78 void init(); // sets state into original state
79 void set_state(State state); // sets state and initializes counter correspondingly
80 inline void set(State state, int count); // sets state and counter
81 inline void decay(); // decay counter (divide by two)
82 void set_carry(); // set the sticky carry bit
83 void set_carry_flag() { _counter |= carry_mask; }
84
85 int raw_counter() { return _counter; }
86
87 // Accessors
88 State state() const { return (State)(_counter & state_mask); }
89 bool carry() const { return (_counter & carry_mask) != 0; }
90 int limit() const { return CompileThreshold; }
91 Action action() const { return _action[state()]; }
92 int count() const { return _counter >> number_of_noncount_bits; }
93
94#ifdef CC_INTERP
95 static int InterpreterInvocationLimit; // CompileThreshold scaled for interpreter use
96 static int InterpreterBackwardBranchLimit; // A separate threshold for on stack replacement
97
98 // Test counter using scaled limits like the asm interpreter would do rather than doing
99 // the shifts to normalize the counter.
100 // Checks sum of invocation_counter and backedge_counter as the template interpreter does.
101 bool reached_InvocationLimit(InvocationCounter *back_edge_count) const {
102 return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >=
103 (unsigned int) InterpreterInvocationLimit;
104 }
105 bool reached_BackwardBranchLimit(InvocationCounter *back_edge_count) const {
106 return (_counter & count_mask) + (back_edge_count->_counter & count_mask) >=
107 (unsigned int) InterpreterBackwardBranchLimit;
108 }
109#endif // CC_INTERP
110
111 void increment() { _counter += count_increment; }
112
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();
121
122 private:
123 static int _init [number_of_states]; // the counter limits
124 static Action _action[number_of_states]; // the actions
125
126 static void def(State state, int init, Action action);
127 static const char* state_as_string(State state);
128 static const char* state_as_short_string(State state);
129};
130
131inline void InvocationCounter::set(State state, int count) {
132 assert(0 <= state && state < number_of_states, "illegal state");
133 int carry = (_counter & carry_mask); // the carry bit is sticky
134 _counter = (count << number_of_noncount_bits) | carry | state;
135}
136
137inline void InvocationCounter::decay() {
138 int c = count();
139 int new_count = c >> 1;
140 // prevent from going to zero, to distinguish from never-executed methods
141 if (c > 0 && new_count == 0) new_count = 1;
142 set(state(), new_count);
143}
144
145
146#endif // SHARE_INTERPRETER_INVOCATIONCOUNTER_HPP
147