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