1 /*
   2  * Copyright (c) 2013, 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_VM_OOPS_METHODCOUNTERS_HPP
  26 #define SHARE_VM_OOPS_METHODCOUNTERS_HPP
  27 
  28 #include "oops/metadata.hpp"
  29 #include "compiler/compilerOracle.hpp"
  30 #include "interpreter/invocationCounter.hpp"
  31 #include "runtime/arguments.hpp"
  32 
  33 class MethodCounters: public MetaspaceObj {
  34  friend class VMStructs;
  35  private:
  36   int               _interpreter_invocation_count; // Count of times invoked (reused as prev_event_count in tiered)
  37   u2                _interpreter_throwout_count; // Count of times method was exited via exception while interpreting
  38   u2                _number_of_breakpoints;      // fullspeed debugging support
  39   InvocationCounter _invocation_counter;         // Incremented before each activation of the method - used to trigger frequency-based optimizations
  40   InvocationCounter _backedge_counter;           // Incremented before each backedge taken - used to trigger frequencey-based optimizations
  41   // NMethod age is a counter for warm methods detection in the code cache sweeper.
  42   // The counter is reset by the sweeper and is decremented by some of the compiled
  43   // code. The counter values are interpreted as follows:
  44   // 1. (HotMethodDetection..INT_MAX] - initial value, no counters inserted
  45   // 2. (1..HotMethodDetectionLimit)  - the method is warm, the counter is used
  46   //                                    to figure out which methods can be flushed.
  47   // 3. (INT_MIN..0]                  - method is hot and will deopt and get
  48   //                                    recompiled without the counters
  49   int               _nmethod_age;
  50   int               _interpreter_invocation_limit;        // per-method InterpreterInvocationLimit
  51   int               _interpreter_backward_branch_limit;   // per-method InterpreterBackwardBranchLimit
  52   int               _interpreter_profile_limit;           // per-method InterpreterProfileLimit
  53   int               _invoke_mask;                         // per-method Tier0InvokeNotifyFreqLog
  54   int               _backedge_mask;                       // per-method Tier0BackedgeNotifyFreqLog
  55 #ifdef TIERED
  56   float             _rate;                        // Events (invocation and backedge counter increments) per millisecond
  57   jlong             _prev_time;                   // Previous time the rate was acquired
  58   u1                _highest_comp_level;          // Highest compile level this method has ever seen.
  59   u1                _highest_osr_comp_level;      // Same for OSR level
  60 #endif
  61 
  62   MethodCounters(methodHandle mh) : _interpreter_invocation_count(0),
  63                                     _interpreter_throwout_count(0),
  64                                     _number_of_breakpoints(0),
  65                                     _nmethod_age(INT_MAX)
  66 #ifdef TIERED
  67                                  , _rate(0),
  68                                    _prev_time(0),
  69                                    _highest_comp_level(0),
  70                                    _highest_osr_comp_level(0)
  71 #endif
  72   {
  73     invocation_counter()->init();
  74     backedge_counter()->init();
  75 
  76     if (StressCodeAging) {
  77       set_nmethod_age(HotMethodDetectionLimit);
  78     }
  79 
  80     // Set per-method thresholds.
  81     double scale = 1.0;
  82     CompilerOracle::has_option_value(mh, "CompileThresholdScaling", scale);
  83 
  84     int compile_threshold = Arguments::scaled_compile_threshold(CompileThreshold, scale);
  85     _interpreter_invocation_limit = compile_threshold << InvocationCounter::count_shift;
  86     if (ProfileInterpreter) {
  87       // If interpreter profiling is enabled, the backward branch limit
  88       // is compared against the method data counter rather than an invocation
  89       // counter, therefore no shifting of bits is required.
  90       _interpreter_backward_branch_limit = (compile_threshold * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100;
  91     } else {
  92       _interpreter_backward_branch_limit = ((compile_threshold * OnStackReplacePercentage) / 100) << InvocationCounter::count_shift;
  93     }
  94     _interpreter_profile_limit = ((compile_threshold * InterpreterProfilePercentage) / 100) << InvocationCounter::count_shift;
  95     _invoke_mask = right_n_bits(Arguments::scaled_freq_log(Tier0InvokeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
  96     _backedge_mask = right_n_bits(Arguments::scaled_freq_log(Tier0BackedgeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
  97   }
  98 
  99  public:
 100   static MethodCounters* allocate(methodHandle mh, TRAPS);
 101 
 102   void deallocate_contents(ClassLoaderData* loader_data) {}
 103   DEBUG_ONLY(bool on_stack() { return false; })  // for template
 104 
 105   static int size() { return sizeof(MethodCounters) / wordSize; }
 106 
 107   bool is_klass() const { return false; }
 108 
 109   void clear_counters();
 110 
 111   int interpreter_invocation_count() {
 112     return _interpreter_invocation_count;
 113   }
 114   void set_interpreter_invocation_count(int count) {
 115     _interpreter_invocation_count = count;
 116   }
 117   int increment_interpreter_invocation_count() {
 118     return ++_interpreter_invocation_count;
 119   }
 120 
 121   void interpreter_throwout_increment() {
 122     if (_interpreter_throwout_count < 65534) {
 123       _interpreter_throwout_count++;
 124     }
 125   }
 126   int  interpreter_throwout_count() const {
 127     return _interpreter_throwout_count;
 128   }
 129   void set_interpreter_throwout_count(int count) {
 130     _interpreter_throwout_count = count;
 131   }
 132 
 133   u2   number_of_breakpoints() const   { return _number_of_breakpoints; }
 134   void incr_number_of_breakpoints()    { ++_number_of_breakpoints; }
 135   void decr_number_of_breakpoints()    { --_number_of_breakpoints; }
 136   void clear_number_of_breakpoints()   { _number_of_breakpoints = 0; }
 137 
 138 #ifdef TIERED
 139   jlong prev_time() const                        { return _prev_time; }
 140   void set_prev_time(jlong time)                 { _prev_time = time; }
 141   float rate() const                             { return _rate; }
 142   void set_rate(float rate)                      { _rate = rate; }
 143 #endif
 144 
 145   int highest_comp_level() const;
 146   void set_highest_comp_level(int level);
 147   int highest_osr_comp_level() const;
 148   void set_highest_osr_comp_level(int level);
 149 
 150   // invocation counter
 151   InvocationCounter* invocation_counter() { return &_invocation_counter; }
 152   InvocationCounter* backedge_counter()   { return &_backedge_counter; }
 153 
 154   int nmethod_age() {
 155     return _nmethod_age;
 156   }
 157   void set_nmethod_age(int age) {
 158     _nmethod_age = age;
 159   }
 160   void reset_nmethod_age() {
 161     set_nmethod_age(HotMethodDetectionLimit);
 162   }
 163 
 164   static bool is_nmethod_hot(int age)       { return age <= 0; }
 165   static bool is_nmethod_warm(int age)      { return age < HotMethodDetectionLimit; }
 166   static bool is_nmethod_age_unset(int age) { return age > HotMethodDetectionLimit; }
 167 
 168   static ByteSize nmethod_age_offset() {
 169     return byte_offset_of(MethodCounters, _nmethod_age);
 170   }
 171 
 172   static ByteSize interpreter_invocation_counter_offset() {
 173     return byte_offset_of(MethodCounters, _interpreter_invocation_count);
 174   }
 175 
 176   static ByteSize invocation_counter_offset()    {
 177     return byte_offset_of(MethodCounters, _invocation_counter);
 178   }
 179 
 180   static ByteSize backedge_counter_offset()      {
 181     return byte_offset_of(MethodCounters, _backedge_counter);
 182   }
 183 
 184   static int interpreter_invocation_counter_offset_in_bytes() {
 185     return offset_of(MethodCounters, _interpreter_invocation_count);
 186   }
 187 
 188   static ByteSize interpreter_invocation_limit_offset() {
 189     return byte_offset_of(MethodCounters, _interpreter_invocation_limit);
 190   }
 191 
 192   static ByteSize interpreter_backward_branch_limit_offset() {
 193     return byte_offset_of(MethodCounters, _interpreter_backward_branch_limit);
 194   }
 195 
 196   static ByteSize interpreter_profile_limit_offset() {
 197     return byte_offset_of(MethodCounters, _interpreter_profile_limit);
 198   }
 199 
 200   static ByteSize invoke_mask_offset() {
 201     return byte_offset_of(MethodCounters, _invoke_mask);
 202   }
 203 
 204   static ByteSize backedge_mask_offset() {
 205     return byte_offset_of(MethodCounters, _backedge_mask);
 206   }
 207 };
 208 #endif //SHARE_VM_OOPS_METHODCOUNTERS_HPP