1 /*
2  * Copyright (c) 1997, 2021, 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 //
33 // The counter is incremented before a method is activated and an
34 // action is triggered when count() > limit().
35 
36 class InvocationCounter {
37   friend class VMStructs;
38   friend class JVMCIVMStructs;
39   friend class ciReplay;
40  private:              // bit no: |31  1|  0  |
41   uint _counter;       // format: [count|carry|
42 
43   enum PrivateConstants {
44     number_of_carry_bits    = 1,
45     number_of_noncount_bits = number_of_carry_bits,
46     count_grain             = nth_bit(number_of_carry_bits),
47     carry_mask              = right_n_bits(number_of_carry_bits),
48     count_mask              = ((int)(-1) ^ carry_mask)
49   };
50 
51  public:
52   enum PublicConstants {
53     count_increment      = count_grain,          // use this value to increment the 32bit _counter word
54     count_mask_value     = count_mask,           // use this value to mask the backedge counter
55     count_shift          = number_of_noncount_bits,
56     number_of_count_bits = BitsPerInt - number_of_noncount_bits,
57     count_limit          = nth_bit(number_of_count_bits - 1)
58   };
59 
60   // Manipulation
61   void reset();
62   void init();
63   void decay();                                  // decay counter (divide by two)
64   void set_carry_on_overflow();
65   void set(uint count);
increment()66   void increment()                 { _counter += count_increment; }
67 
68   // Accessors
carry() const69   bool carry() const               { return (_counter & carry_mask) != 0; }
count() const70   uint count() const               { return _counter >> number_of_noncount_bits; }
limit() const71   uint limit() const               { return CompileThreshold; }
raw_counter() const72   uint raw_counter() const         { return _counter; }
73 
74   void print();
75 
76 private:
set_carry()77   void set_carry()                   {  _counter |= carry_mask; }
extract_carry(uint raw) const78   uint extract_carry(uint raw) const { return (raw & carry_mask); }
extract_count(uint raw) const79   uint extract_count(uint raw) const { return raw >> number_of_noncount_bits; }
80   void update(uint new_count);
81   void set(uint count, uint carry);
82 
83 public:
84 
85   // Miscellaneous
counter_offset()86   static ByteSize counter_offset()               { return byte_offset_of(InvocationCounter, _counter); }
87 };
88 
89 #endif // SHARE_INTERPRETER_INVOCATIONCOUNTER_HPP
90