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 MetaspaceObj { 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 static MethodCounters* allocate(methodHandle mh, TRAPS); 113 114 void deallocate_contents(ClassLoaderData* loader_data) {} 115 DEBUG_ONLY(bool on_stack() { return false; }) // for template 116 117 AOT_ONLY(Method* method() const { return _method; }) 118 119 static int size() { return sizeof(MethodCounters) / wordSize; } 120 121 bool is_klass() const { return false; } 122 123 void clear_counters(); 124 125 #if defined(COMPILER2) || INCLUDE_JVMCI 126 127 int interpreter_invocation_count() { 128 return _interpreter_invocation_count; 129 } 130 void set_interpreter_invocation_count(int count) { 131 _interpreter_invocation_count = count; 132 } 133 int increment_interpreter_invocation_count() { 134 return ++_interpreter_invocation_count; 135 } 136 137 void interpreter_throwout_increment() { 138 if (_interpreter_throwout_count < 65534) { 139 _interpreter_throwout_count++; 140 } 141 } 142 int interpreter_throwout_count() const { 143 return _interpreter_throwout_count; 144 } 145 void set_interpreter_throwout_count(int count) { 146 _interpreter_throwout_count = count; 147 } 148 149 #else // defined(COMPILER2) || INCLUDE_JVMCI 150 151 int interpreter_invocation_count() { 152 return 0; 153 } 154 void set_interpreter_invocation_count(int count) { 155 assert(count == 0, "count must be 0"); 156 } 157 158 int interpreter_throwout_count() const { 159 return 0; 160 } 161 void set_interpreter_throwout_count(int count) { 162 assert(count == 0, "count must be 0"); 163 } 164 165 #endif // defined(COMPILER2) || INCLUDE_JVMCI 166 167 #if INCLUDE_JVMTI 168 u2 number_of_breakpoints() const { return _number_of_breakpoints; } 169 void incr_number_of_breakpoints() { ++_number_of_breakpoints; } 170 void decr_number_of_breakpoints() { --_number_of_breakpoints; } 171 void clear_number_of_breakpoints() { _number_of_breakpoints = 0; } 172 #endif 173 174 #ifdef TIERED 175 jlong prev_time() const { return _prev_time; } 176 void set_prev_time(jlong time) { _prev_time = time; } 177 float rate() const { return _rate; } 178 void set_rate(float rate) { _rate = rate; } 179 #endif 180 181 int highest_comp_level() const; 182 void set_highest_comp_level(int level); 183 int highest_osr_comp_level() const; 184 void set_highest_osr_comp_level(int level); 185 186 // invocation counter 187 InvocationCounter* invocation_counter() { return &_invocation_counter; } 188 InvocationCounter* backedge_counter() { return &_backedge_counter; } 189 190 int nmethod_age() { 191 return _nmethod_age; 192 } 193 void set_nmethod_age(int age) { 194 _nmethod_age = age; 195 } 196 void reset_nmethod_age() { 197 set_nmethod_age(HotMethodDetectionLimit); 198 } 199 200 static bool is_nmethod_hot(int age) { return age <= 0; } 201 static bool is_nmethod_warm(int age) { return age < HotMethodDetectionLimit; } 202 static bool is_nmethod_age_unset(int age) { return age > HotMethodDetectionLimit; } 203 204 static ByteSize nmethod_age_offset() { 205 return byte_offset_of(MethodCounters, _nmethod_age); 206 } 207 208 #if defined(COMPILER2) || INCLUDE_JVMCI 209 210 static ByteSize interpreter_invocation_counter_offset() { 211 return byte_offset_of(MethodCounters, _interpreter_invocation_count); 212 } 213 214 static int interpreter_invocation_counter_offset_in_bytes() { 215 return offset_of(MethodCounters, _interpreter_invocation_count); 216 } 217 218 #else // defined(COMPILER2) || INCLUDE_JVMCI 219 220 static ByteSize interpreter_invocation_counter_offset() { 221 ShouldNotReachHere(); 222 return in_ByteSize(0); 223 } 224 225 #endif // defined(COMPILER2) || INCLUDE_JVMCI 226 227 static ByteSize invocation_counter_offset() { 228 return byte_offset_of(MethodCounters, _invocation_counter); 229 } 230 231 static ByteSize backedge_counter_offset() { 232 return byte_offset_of(MethodCounters, _backedge_counter); 233 } 234 235 static ByteSize interpreter_invocation_limit_offset() { 236 return byte_offset_of(MethodCounters, _interpreter_invocation_limit); 237 } 238 239 static ByteSize interpreter_backward_branch_limit_offset() { 240 return byte_offset_of(MethodCounters, _interpreter_backward_branch_limit); 241 } 242 243 static ByteSize interpreter_profile_limit_offset() { 244 return byte_offset_of(MethodCounters, _interpreter_profile_limit); 245 } 246 247 static ByteSize invoke_mask_offset() { 248 return byte_offset_of(MethodCounters, _invoke_mask); 249 } 250 251 static ByteSize backedge_mask_offset() { 252 return byte_offset_of(MethodCounters, _backedge_mask); 253 } 254 }; 255 #endif //SHARE_VM_OOPS_METHODCOUNTERS_HPP