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