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