invocationCounter.hpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1997, 2006, 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// InvocationCounters are used to trigger actions when a limit (threshold) is reached.
26// For different states, different limits and actions can be defined in the initialization
27// routine of InvocationCounters.
28//
29// Implementation notes: For space reasons, state & counter are both encoded in one word,
30// The state is encoded using some of the least significant bits, the counter is using the
31// more significant bits. The counter is incremented before a method is activated and an
32// action is triggered when when count() > limit().
33
34class InvocationCounter VALUE_OBJ_CLASS_SPEC {
35  friend class VMStructs;
36 private:                             // bit no: |31  3|  2  | 1 0 |
37  unsigned int _counter;              // format: [count|carry|state]
38
39  enum PrivateConstants {
40    number_of_state_bits = 2,
41    number_of_carry_bits = 1,
42    number_of_noncount_bits = number_of_state_bits + number_of_carry_bits,
43    number_of_count_bits = BitsPerInt - number_of_noncount_bits,
44    state_limit          = nth_bit(number_of_state_bits),
45    count_grain          = nth_bit(number_of_state_bits + number_of_carry_bits),
46    count_limit          = nth_bit(number_of_count_bits - 1),
47    carry_mask           = right_n_bits(number_of_carry_bits) << number_of_state_bits,
48    state_mask           = right_n_bits(number_of_state_bits),
49    status_mask          = right_n_bits(number_of_state_bits + number_of_carry_bits),
50    count_mask           = ((int)(-1) ^ status_mask)
51  };
52
53 public:
54  static int InterpreterInvocationLimit;        // CompileThreshold scaled for interpreter use
55  static int Tier1InvocationLimit;              // CompileThreshold scaled for tier1 use
56  static int Tier1BackEdgeLimit;                // BackEdgeThreshold scaled for tier1 use
57
58  static int InterpreterBackwardBranchLimit;    // A separate threshold for on stack replacement
59
60  static int InterpreterProfileLimit;           // Profiling threshold scaled for interpreter use
61
62  typedef address (*Action)(methodHandle method, TRAPS);
63
64  enum PublicConstants {
65    count_increment      = count_grain,          // use this value to increment the 32bit _counter word
66    count_mask_value     = count_mask            // use this value to mask the backedge counter
67  };
68
69  enum State {
70    wait_for_nothing,                            // do nothing when count() > limit()
71    wait_for_compile,                            // introduce nmethod when count() > limit()
72    number_of_states                             // must be <= state_limit
73  };
74
75  // Manipulation
76  void reset();                                  // sets state to wait state
77  void init();                                   // sets state into original state
78  void set_state(State state);                   // sets state and initializes counter correspondingly
79  inline void set(State state, int count);       // sets state and counter
80  inline void decay();                           // decay counter (divide by two)
81  void set_carry();                              // set the sticky carry bit
82
83  // Accessors
84  State  state() const                           { return (State)(_counter & state_mask); }
85  bool   carry() const                           { return (_counter & carry_mask) != 0; }
86  int    limit() const                           { return CompileThreshold; }
87  Action action() const                          { return _action[state()]; }
88  int    count() const                           { return _counter >> number_of_noncount_bits; }
89
90  int   get_InvocationLimit() const              { return InterpreterInvocationLimit >> number_of_noncount_bits; }
91  int   get_BackwardBranchLimit() const          { return InterpreterBackwardBranchLimit >> number_of_noncount_bits; }
92  int   get_ProfileLimit() const                 { return InterpreterProfileLimit >> number_of_noncount_bits; }
93
94  // Test counter using scaled limits like the asm interpreter would do rather than doing
95  // the shifts to normalize the counter.
96
97  bool   reached_InvocationLimit() const         { return _counter >= (unsigned int) InterpreterInvocationLimit; }
98  bool   reached_BackwardBranchLimit() const     { return _counter >= (unsigned int) InterpreterBackwardBranchLimit; }
99
100  // Do this just like asm interpreter does for max speed
101  bool   reached_ProfileLimit(InvocationCounter *back_edge_count) const {
102    return (_counter && count_mask) + back_edge_count->_counter >= (unsigned int) InterpreterProfileLimit;
103  }
104
105  void increment()                               { _counter += count_increment; }
106
107
108  // Printing
109  void   print();
110  void   print_short();
111
112  // Miscellaneous
113  static ByteSize counter_offset()               { return byte_offset_of(InvocationCounter, _counter); }
114  static void reinitialize(bool delay_overflow);
115
116 private:
117  static int         _init  [number_of_states];  // the counter limits
118  static Action      _action[number_of_states];  // the actions
119
120  static void        def(State state, int init, Action action);
121  static const char* state_as_string(State state);
122  static const char* state_as_short_string(State state);
123};
124
125inline void InvocationCounter::set(State state, int count) {
126  assert(0 <= state && state < number_of_states, "illegal state");
127  int carry = (_counter & carry_mask);    // the carry bit is sticky
128  _counter = (count << number_of_noncount_bits) | carry | state;
129}
130
131inline 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(state(), new_count);
137}
138