1 /*
   2  * Copyright (c) 2013, 2017, 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 #include "utilities/align.hpp"
  33 
  34 class MethodCounters : public Metadata {
  35  friend class VMStructs;
  36  friend class JVMCIVMStructs;
  37  private:
  38 #if INCLUDE_AOT
  39   Method*           _method;                     // Back link to Method
  40 #endif
  41 #if defined(COMPILER2) || INCLUDE_JVMCI
  42   int               _interpreter_invocation_count; // Count of times invoked (reused as prev_event_count in tiered)
  43   u2                _interpreter_throwout_count; // Count of times method was exited via exception while interpreting
  44 #endif
  45 #if INCLUDE_JVMTI
  46   u2                _number_of_breakpoints;      // fullspeed debugging support
  47 #endif
  48   InvocationCounter _invocation_counter;         // Incremented before each activation of the method - used to trigger frequency-based optimizations
  49   InvocationCounter _backedge_counter;           // Incremented before each backedge taken - used to trigger frequencey-based optimizations
  50   // NMethod age is a counter for warm methods detection in the code cache sweeper.
  51   // The counter is reset by the sweeper and is decremented by some of the compiled
  52   // code. The counter values are interpreted as follows:
  53   // 1. (HotMethodDetection..INT_MAX] - initial value, no counters inserted
  54   // 2. [1..HotMethodDetectionLimit)  - the method is warm, the counter is used
  55   //                                    to figure out which methods can be flushed.
  56   // 3. (INT_MIN..0]                  - method is hot and will deopt and get
  57   //                                    recompiled without the counters
  58   int               _nmethod_age;
  59   int               _interpreter_invocation_limit;        // per-method InterpreterInvocationLimit
  60   int               _interpreter_backward_branch_limit;   // per-method InterpreterBackwardBranchLimit
  61   int               _interpreter_profile_limit;           // per-method InterpreterProfileLimit
  62   int               _invoke_mask;                         // per-method Tier0InvokeNotifyFreqLog
  63   int               _backedge_mask;                       // per-method Tier0BackedgeNotifyFreqLog
  64 #ifdef TIERED
  65   float             _rate;                        // Events (invocation and backedge counter increments) per millisecond
  66   jlong             _prev_time;                   // Previous time the rate was acquired
  67   u1                _highest_comp_level;          // Highest compile level this method has ever seen.
  68   u1                _highest_osr_comp_level;      // Same for OSR level
  69 #endif
  70 
  71   MethodCounters(methodHandle mh) :
  72 #if INCLUDE_AOT
  73                                     _method(mh()),
  74 #endif
  75                                     _nmethod_age(INT_MAX)
  76 #ifdef TIERED
  77                                  , _rate(0),
  78                                    _prev_time(0),
  79                                    _highest_comp_level(0),
  80                                    _highest_osr_comp_level(0)
  81 #endif
  82   {
  83     set_interpreter_invocation_count(0);
  84     set_interpreter_throwout_count(0);
  85     JVMTI_ONLY(clear_number_of_breakpoints());
  86     invocation_counter()->init();
  87     backedge_counter()->init();
  88 
  89     if (StressCodeAging) {
  90       set_nmethod_age(HotMethodDetectionLimit);
  91     }
  92 
  93     // Set per-method thresholds.
  94     double scale = 1.0;
  95     CompilerOracle::has_option_value(mh, "CompileThresholdScaling", scale);
  96 
  97     int compile_threshold = Arguments::scaled_compile_threshold(CompileThreshold, scale);
  98     _interpreter_invocation_limit = compile_threshold << InvocationCounter::count_shift;
  99     if (ProfileInterpreter) {
 100       // If interpreter profiling is enabled, the backward branch limit
 101       // is compared against the method data counter rather than an invocation
 102       // counter, therefore no shifting of bits is required.
 103       _interpreter_backward_branch_limit = (compile_threshold * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100;
 104     } else {
 105       _interpreter_backward_branch_limit = ((compile_threshold * OnStackReplacePercentage) / 100) << InvocationCounter::count_shift;
 106     }
 107     _interpreter_profile_limit = ((compile_threshold * InterpreterProfilePercentage) / 100) << InvocationCounter::count_shift;
 108     _invoke_mask = right_n_bits(Arguments::scaled_freq_log(Tier0InvokeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
 109     _backedge_mask = right_n_bits(Arguments::scaled_freq_log(Tier0BackedgeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
 110   }
 111 
 112  public:
 113   virtual bool is_methodCounters() const volatile { return true; }
 114 
 115   static MethodCounters* allocate(methodHandle mh, TRAPS);
 116 
 117   void deallocate_contents(ClassLoaderData* loader_data) {}
 118 
 119   AOT_ONLY(Method* method() const { return _method; })
 120 
 121   static int method_counters_size() {
 122     return align_up((int)sizeof(MethodCounters), wordSize) / wordSize;
 123   }
 124   virtual int size() const {
 125     return method_counters_size();
 126   }
 127   void metaspace_pointers_do(MetaspaceClosure* it);
 128   MetaspaceObj::Type type() const { return MethodCountersType; }
 129   void clear_counters();
 130 
 131 #if defined(COMPILER2) || INCLUDE_JVMCI
 132 
 133   int interpreter_invocation_count() {
 134     return _interpreter_invocation_count;
 135   }
 136   void set_interpreter_invocation_count(int count) {
 137     _interpreter_invocation_count = count;
 138   }
 139   int increment_interpreter_invocation_count() {
 140     return ++_interpreter_invocation_count;
 141   }
 142 
 143   void interpreter_throwout_increment() {
 144     if (_interpreter_throwout_count < 65534) {
 145       _interpreter_throwout_count++;
 146     }
 147   }
 148   int  interpreter_throwout_count() const {
 149     return _interpreter_throwout_count;
 150   }
 151   void set_interpreter_throwout_count(int count) {
 152     _interpreter_throwout_count = count;
 153   }
 154 
 155 #else // defined(COMPILER2) || INCLUDE_JVMCI
 156 
 157   int interpreter_invocation_count() {
 158     return 0;
 159   }
 160   void set_interpreter_invocation_count(int count) {
 161     assert(count == 0, "count must be 0");
 162   }
 163 
 164   int  interpreter_throwout_count() const {
 165     return 0;
 166   }
 167   void set_interpreter_throwout_count(int count) {
 168     assert(count == 0, "count must be 0");
 169   }
 170 
 171 #endif // defined(COMPILER2) || INCLUDE_JVMCI
 172 
 173 #if INCLUDE_JVMTI
 174   u2   number_of_breakpoints() const   { return _number_of_breakpoints; }
 175   void incr_number_of_breakpoints()    { ++_number_of_breakpoints; }
 176   void decr_number_of_breakpoints()    { --_number_of_breakpoints; }
 177   void clear_number_of_breakpoints()   { _number_of_breakpoints = 0; }
 178 #endif
 179 
 180 #ifdef TIERED
 181   jlong prev_time() const                        { return _prev_time; }
 182   void set_prev_time(jlong time)                 { _prev_time = time; }
 183   float rate() const                             { return _rate; }
 184   void set_rate(float rate)                      { _rate = rate; }
 185 #endif
 186 
 187   int highest_comp_level() const;
 188   void set_highest_comp_level(int level);
 189   int highest_osr_comp_level() const;
 190   void set_highest_osr_comp_level(int level);
 191 
 192   // invocation counter
 193   InvocationCounter* invocation_counter() { return &_invocation_counter; }
 194   InvocationCounter* backedge_counter()   { return &_backedge_counter; }
 195 
 196   int nmethod_age() {
 197     return _nmethod_age;
 198   }
 199   void set_nmethod_age(int age) {
 200     _nmethod_age = age;
 201   }
 202   void reset_nmethod_age() {
 203     set_nmethod_age(HotMethodDetectionLimit);
 204   }
 205 
 206   static bool is_nmethod_hot(int age)       { return age <= 0; }
 207   static bool is_nmethod_warm(int age)      { return age < HotMethodDetectionLimit; }
 208   static bool is_nmethod_age_unset(int age) { return age > HotMethodDetectionLimit; }
 209 
 210   static ByteSize nmethod_age_offset() {
 211     return byte_offset_of(MethodCounters, _nmethod_age);
 212   }
 213 
 214 #if defined(COMPILER2) || INCLUDE_JVMCI
 215 
 216   static ByteSize interpreter_invocation_counter_offset() {
 217     return byte_offset_of(MethodCounters, _interpreter_invocation_count);
 218   }
 219 
 220   static int interpreter_invocation_counter_offset_in_bytes() {
 221     return offset_of(MethodCounters, _interpreter_invocation_count);
 222   }
 223 
 224 #else // defined(COMPILER2) || INCLUDE_JVMCI
 225 
 226   static ByteSize interpreter_invocation_counter_offset() {
 227     ShouldNotReachHere();
 228     return in_ByteSize(0);
 229   }
 230 
 231 #endif // defined(COMPILER2) || INCLUDE_JVMCI
 232 
 233   static ByteSize invocation_counter_offset()    {
 234     return byte_offset_of(MethodCounters, _invocation_counter);
 235   }
 236 
 237   static ByteSize backedge_counter_offset()      {
 238     return byte_offset_of(MethodCounters, _backedge_counter);
 239   }
 240 
 241   static ByteSize interpreter_invocation_limit_offset() {
 242     return byte_offset_of(MethodCounters, _interpreter_invocation_limit);
 243   }
 244 
 245   static ByteSize interpreter_backward_branch_limit_offset() {
 246     return byte_offset_of(MethodCounters, _interpreter_backward_branch_limit);
 247   }
 248 
 249   static ByteSize interpreter_profile_limit_offset() {
 250     return byte_offset_of(MethodCounters, _interpreter_profile_limit);
 251   }
 252 
 253   static ByteSize invoke_mask_offset() {
 254     return byte_offset_of(MethodCounters, _invoke_mask);
 255   }
 256 
 257   static ByteSize backedge_mask_offset() {
 258     return byte_offset_of(MethodCounters, _backedge_mask);
 259   }
 260 
 261   virtual const char* internal_name() const { return "{method counters}"; }
 262   virtual void print_value_on(outputStream* st) const;
 263 
 264 };
 265 #endif //SHARE_VM_OOPS_METHODCOUNTERS_HPP