src/share/vm/runtime/compilationPolicy.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 6876
710a3c8b516e
parent 9614
bb44c0e88235
permissions
-rw-r--r--

Merge

duke@435 1 /*
drchase@6680 2 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "code/compiledIC.hpp"
stefank@2314 27 #include "code/nmethod.hpp"
stefank@2314 28 #include "code/scopeDesc.hpp"
stefank@2314 29 #include "compiler/compilerOracle.hpp"
stefank@2314 30 #include "interpreter/interpreter.hpp"
coleenp@4037 31 #include "oops/methodData.hpp"
coleenp@4037 32 #include "oops/method.hpp"
stefank@2314 33 #include "oops/oop.inline.hpp"
stefank@2314 34 #include "prims/nativeLookup.hpp"
iveresov@2630 35 #include "runtime/advancedThresholdPolicy.hpp"
stefank@2314 36 #include "runtime/compilationPolicy.hpp"
stefank@2314 37 #include "runtime/frame.hpp"
stefank@2314 38 #include "runtime/handles.inline.hpp"
stefank@2314 39 #include "runtime/rframe.hpp"
stefank@2314 40 #include "runtime/simpleThresholdPolicy.hpp"
stefank@2314 41 #include "runtime/stubRoutines.hpp"
stefank@2314 42 #include "runtime/thread.hpp"
stefank@2314 43 #include "runtime/timer.hpp"
stefank@2314 44 #include "runtime/vframe.hpp"
stefank@2314 45 #include "runtime/vm_operations.hpp"
stefank@2314 46 #include "utilities/events.hpp"
stefank@2314 47 #include "utilities/globalDefinitions.hpp"
duke@435 48
duke@435 49 CompilationPolicy* CompilationPolicy::_policy;
duke@435 50 elapsedTimer CompilationPolicy::_accumulated_time;
duke@435 51 bool CompilationPolicy::_in_vm_startup;
duke@435 52
duke@435 53 // Determine compilation policy based on command line argument
duke@435 54 void compilationPolicy_init() {
duke@435 55 CompilationPolicy::set_in_vm_startup(DelayCompilationDuringStartup);
duke@435 56
duke@435 57 switch(CompilationPolicyChoice) {
duke@435 58 case 0:
duke@435 59 CompilationPolicy::set_policy(new SimpleCompPolicy());
duke@435 60 break;
duke@435 61
duke@435 62 case 1:
duke@435 63 #ifdef COMPILER2
duke@435 64 CompilationPolicy::set_policy(new StackWalkCompPolicy());
duke@435 65 #else
duke@435 66 Unimplemented();
duke@435 67 #endif
duke@435 68 break;
iveresov@2138 69 case 2:
iveresov@2138 70 #ifdef TIERED
iveresov@2138 71 CompilationPolicy::set_policy(new SimpleThresholdPolicy());
iveresov@2138 72 #else
iveresov@2138 73 Unimplemented();
iveresov@2138 74 #endif
iveresov@2138 75 break;
iveresov@2630 76 case 3:
iveresov@2630 77 #ifdef TIERED
iveresov@2630 78 CompilationPolicy::set_policy(new AdvancedThresholdPolicy());
iveresov@2630 79 #else
iveresov@2630 80 Unimplemented();
iveresov@2630 81 #endif
iveresov@2630 82 break;
duke@435 83 default:
iveresov@2630 84 fatal("CompilationPolicyChoice must be in the range: [0-3]");
duke@435 85 }
iveresov@2138 86 CompilationPolicy::policy()->initialize();
duke@435 87 }
duke@435 88
duke@435 89 void CompilationPolicy::completed_vm_startup() {
duke@435 90 if (TraceCompilationPolicy) {
duke@435 91 tty->print("CompilationPolicy: completed vm startup.\n");
duke@435 92 }
duke@435 93 _in_vm_startup = false;
duke@435 94 }
duke@435 95
duke@435 96 // Returns true if m must be compiled before executing it
duke@435 97 // This is intended to force compiles for methods (usually for
duke@435 98 // debugging) that would otherwise be interpreted for some reason.
iveresov@2138 99 bool CompilationPolicy::must_be_compiled(methodHandle m, int comp_level) {
minqi@4267 100 // Don't allow Xcomp to cause compiles in replay mode
minqi@4267 101 if (ReplayCompiles) return false;
minqi@4267 102
duke@435 103 if (m->has_compiled_code()) return false; // already compiled
iveresov@2138 104 if (!can_be_compiled(m, comp_level)) return false;
duke@435 105
duke@435 106 return !UseInterpreter || // must compile all methods
kvn@1637 107 (UseCompiler && AlwaysCompileLoopMethods && m->has_loops() && CompileBroker::should_compile_new_jobs()); // eagerly compile loop methods
duke@435 108 }
duke@435 109
duke@435 110 // Returns true if m is allowed to be compiled
iveresov@2138 111 bool CompilationPolicy::can_be_compiled(methodHandle m, int comp_level) {
iignatyev@5032 112 // allow any levels for WhiteBox
iignatyev@5032 113 assert(WhiteBoxAPI || comp_level == CompLevel_all || is_compile(comp_level), "illegal compilation level");
iignatyev@5032 114
duke@435 115 if (m->is_abstract()) return false;
duke@435 116 if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false;
duke@435 117
never@1609 118 // Math intrinsics should never be compiled as this can lead to
never@1609 119 // monotonicity problems because the interpreter will prefer the
never@1609 120 // compiled code to the intrinsic version. This can't happen in
never@1609 121 // production because the invocation counter can't be incremented
never@1609 122 // but we shouldn't expose the system to this problem in testing
never@1609 123 // modes.
never@1609 124 if (!AbstractInterpreter::can_be_compiled(m)) {
never@1609 125 return false;
never@1609 126 }
iveresov@2138 127 if (comp_level == CompLevel_all) {
iignatyev@5032 128 if (TieredCompilation) {
iignatyev@5032 129 // enough to be compilable at any level for tiered
iignatyev@5032 130 return !m->is_not_compilable(CompLevel_simple) || !m->is_not_compilable(CompLevel_full_optimization);
iignatyev@5032 131 } else {
iignatyev@5032 132 // must be compilable at available level for non-tiered
iignatyev@5032 133 return !m->is_not_compilable(CompLevel_highest_tier);
iignatyev@5032 134 }
iignatyev@4908 135 } else if (is_compile(comp_level)) {
iveresov@2138 136 return !m->is_not_compilable(comp_level);
iveresov@2138 137 }
iignatyev@4908 138 return false;
iveresov@2138 139 }
never@1609 140
iignatyev@5541 141 // Returns true if m is allowed to be osr compiled
iignatyev@5541 142 bool CompilationPolicy::can_be_osr_compiled(methodHandle m, int comp_level) {
iignatyev@5541 143 bool result = false;
iignatyev@5541 144 if (comp_level == CompLevel_all) {
iignatyev@5541 145 if (TieredCompilation) {
iignatyev@5541 146 // enough to be osr compilable at any level for tiered
iignatyev@5541 147 result = !m->is_not_osr_compilable(CompLevel_simple) || !m->is_not_osr_compilable(CompLevel_full_optimization);
iignatyev@5541 148 } else {
iignatyev@5541 149 // must be osr compilable at available level for non-tiered
iignatyev@5541 150 result = !m->is_not_osr_compilable(CompLevel_highest_tier);
iignatyev@5541 151 }
iignatyev@5541 152 } else if (is_compile(comp_level)) {
iignatyev@5541 153 result = !m->is_not_osr_compilable(comp_level);
iignatyev@5541 154 }
iignatyev@5541 155 return (result && can_be_compiled(m, comp_level));
iignatyev@5541 156 }
iignatyev@5541 157
iveresov@2138 158 bool CompilationPolicy::is_compilation_enabled() {
iveresov@2138 159 // NOTE: CompileBroker::should_compile_new_jobs() checks for UseCompiler
iveresov@2138 160 return !delay_compilation_during_startup() && CompileBroker::should_compile_new_jobs();
duke@435 161 }
duke@435 162
duke@435 163 #ifndef PRODUCT
duke@435 164 void CompilationPolicy::print_time() {
duke@435 165 tty->print_cr ("Accumulated compilationPolicy times:");
duke@435 166 tty->print_cr ("---------------------------");
duke@435 167 tty->print_cr (" Total: %3.3f sec.", _accumulated_time.seconds());
duke@435 168 }
duke@435 169
iveresov@2138 170 void NonTieredCompPolicy::trace_osr_completion(nmethod* osr_nm) {
duke@435 171 if (TraceOnStackReplacement) {
duke@435 172 if (osr_nm == NULL) tty->print_cr("compilation failed");
drchase@6680 173 else tty->print_cr("nmethod " INTPTR_FORMAT, p2i(osr_nm));
duke@435 174 }
duke@435 175 }
duke@435 176 #endif // !PRODUCT
duke@435 177
iveresov@2138 178 void NonTieredCompPolicy::initialize() {
iveresov@2138 179 // Setup the compiler thread numbers
iveresov@2138 180 if (CICompilerCountPerCPU) {
iveresov@2138 181 // Example: if CICompilerCountPerCPU is true, then we get
iveresov@2138 182 // max(log2(8)-1,1) = 2 compiler threads on an 8-way machine.
iveresov@2138 183 // May help big-app startup time.
roland@9614 184 _compiler_count = MAX2(log2_int(os::active_processor_count())-1,1);
anoll@6649 185 FLAG_SET_ERGO(intx, CICompilerCount, _compiler_count);
iveresov@2138 186 } else {
iveresov@2138 187 _compiler_count = CICompilerCount;
iveresov@2138 188 }
iveresov@2138 189 }
iveresov@2138 190
iveresov@2176 191 // Note: this policy is used ONLY if TieredCompilation is off.
iveresov@2176 192 // compiler_count() behaves the following way:
iveresov@2176 193 // - with TIERED build (with both COMPILER1 and COMPILER2 defined) it should return
iveresov@2176 194 // zero for the c1 compilation levels, hence the particular ordering of the
iveresov@2176 195 // statements.
iveresov@2176 196 // - the same should happen when COMPILER2 is defined and COMPILER1 is not
iveresov@2176 197 // (server build without TIERED defined).
iveresov@2176 198 // - if only COMPILER1 is defined (client build), zero should be returned for
iveresov@2176 199 // the c2 level.
iveresov@2176 200 // - if neither is defined - always return zero.
iveresov@2138 201 int NonTieredCompPolicy::compiler_count(CompLevel comp_level) {
iveresov@2176 202 assert(!TieredCompilation, "This policy should not be used with TieredCompilation");
iveresov@2176 203 #ifdef COMPILER2
iveresov@2176 204 if (is_c2_compile(comp_level)) {
iveresov@2176 205 return _compiler_count;
iveresov@2176 206 } else {
iveresov@2176 207 return 0;
iveresov@2176 208 }
iveresov@2176 209 #endif
iveresov@2176 210
iveresov@2138 211 #ifdef COMPILER1
iveresov@2138 212 if (is_c1_compile(comp_level)) {
iveresov@2138 213 return _compiler_count;
iveresov@2176 214 } else {
iveresov@2176 215 return 0;
iveresov@2138 216 }
iveresov@2138 217 #endif
iveresov@2138 218
iveresov@2138 219 return 0;
iveresov@2138 220 }
iveresov@2138 221
iveresov@2138 222 void NonTieredCompPolicy::reset_counter_for_invocation_event(methodHandle m) {
duke@435 223 // Make sure invocation and backedge counter doesn't overflow again right away
duke@435 224 // as would be the case for native methods.
duke@435 225
duke@435 226 // BUT also make sure the method doesn't look like it was never executed.
duke@435 227 // Set carry bit and reduce counter's value to min(count, CompileThreshold/2).
jiangli@4936 228 MethodCounters* mcs = m->method_counters();
jiangli@4936 229 assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
jiangli@4936 230 mcs->invocation_counter()->set_carry();
jiangli@4936 231 mcs->backedge_counter()->set_carry();
duke@435 232
duke@435 233 assert(!m->was_never_executed(), "don't reset to 0 -- could be mistaken for never-executed");
duke@435 234 }
duke@435 235
iveresov@2138 236 void NonTieredCompPolicy::reset_counter_for_back_branch_event(methodHandle m) {
duke@435 237 // Delay next back-branch event but pump up invocation counter to triger
duke@435 238 // whole method compilation.
jiangli@4936 239 MethodCounters* mcs = m->method_counters();
jiangli@4936 240 assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
jiangli@4936 241 InvocationCounter* i = mcs->invocation_counter();
jiangli@4936 242 InvocationCounter* b = mcs->backedge_counter();
duke@435 243
duke@435 244 // Don't set invocation_counter's value too low otherwise the method will
duke@435 245 // look like immature (ic < ~5300) which prevents the inlining based on
duke@435 246 // the type profiling.
duke@435 247 i->set(i->state(), CompileThreshold);
duke@435 248 // Don't reset counter too low - it is used to check if OSR method is ready.
duke@435 249 b->set(b->state(), CompileThreshold / 2);
duke@435 250 }
duke@435 251
iveresov@2138 252 //
iveresov@2138 253 // CounterDecay
iveresov@2138 254 //
iveresov@2138 255 // Interates through invocation counters and decrements them. This
iveresov@2138 256 // is done at each safepoint.
iveresov@2138 257 //
iveresov@2138 258 class CounterDecay : public AllStatic {
iveresov@2138 259 static jlong _last_timestamp;
coleenp@4037 260 static void do_method(Method* m) {
jiangli@4936 261 MethodCounters* mcs = m->method_counters();
jiangli@4936 262 if (mcs != NULL) {
jiangli@4936 263 mcs->invocation_counter()->decay();
jiangli@4936 264 }
iveresov@2138 265 }
iveresov@2138 266 public:
iveresov@2138 267 static void decay();
iveresov@2138 268 static bool is_decay_needed() {
iveresov@2138 269 return (os::javaTimeMillis() - _last_timestamp) > CounterDecayMinIntervalLength;
iveresov@2138 270 }
iveresov@2138 271 };
iveresov@2138 272
iveresov@2138 273 jlong CounterDecay::_last_timestamp = 0;
iveresov@2138 274
iveresov@2138 275 void CounterDecay::decay() {
iveresov@2138 276 _last_timestamp = os::javaTimeMillis();
iveresov@2138 277
iveresov@2138 278 // This operation is going to be performed only at the end of a safepoint
iveresov@2138 279 // and hence GC's will not be going on, all Java mutators are suspended
iveresov@2138 280 // at this point and hence SystemDictionary_lock is also not needed.
iveresov@2138 281 assert(SafepointSynchronize::is_at_safepoint(), "can only be executed at a safepoint");
iveresov@2138 282 int nclasses = SystemDictionary::number_of_classes();
iveresov@2138 283 double classes_per_tick = nclasses * (CounterDecayMinIntervalLength * 1e-3 /
iveresov@2138 284 CounterHalfLifeTime);
iveresov@2138 285 for (int i = 0; i < classes_per_tick; i++) {
coleenp@4037 286 Klass* k = SystemDictionary::try_get_next_class();
coleenp@4037 287 if (k != NULL && k->oop_is_instance()) {
coleenp@4037 288 InstanceKlass::cast(k)->methods_do(do_method);
iveresov@2138 289 }
iveresov@2138 290 }
iveresov@2138 291 }
iveresov@2138 292
iveresov@2138 293 // Called at the end of the safepoint
iveresov@2138 294 void NonTieredCompPolicy::do_safepoint_work() {
iveresov@2138 295 if(UseCounterDecay && CounterDecay::is_decay_needed()) {
iveresov@2138 296 CounterDecay::decay();
iveresov@2138 297 }
iveresov@2138 298 }
iveresov@2138 299
iveresov@2138 300 void NonTieredCompPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
iveresov@2138 301 ScopeDesc* sd = trap_scope;
jiangli@4936 302 MethodCounters* mcs;
jiangli@4936 303 InvocationCounter* c;
iveresov@2138 304 for (; !sd->is_top(); sd = sd->sender()) {
jiangli@4936 305 mcs = sd->method()->method_counters();
jiangli@4936 306 if (mcs != NULL) {
jiangli@4936 307 // Reset ICs of inlined methods, since they can trigger compilations also.
jiangli@4936 308 mcs->invocation_counter()->reset();
jiangli@4936 309 }
iveresov@2138 310 }
jiangli@4936 311 mcs = sd->method()->method_counters();
jiangli@4936 312 if (mcs != NULL) {
jiangli@4936 313 c = mcs->invocation_counter();
jiangli@4936 314 if (is_osr) {
jiangli@4936 315 // It was an OSR method, so bump the count higher.
jiangli@4936 316 c->set(c->state(), CompileThreshold);
jiangli@4936 317 } else {
jiangli@4936 318 c->reset();
jiangli@4936 319 }
jiangli@4936 320 mcs->backedge_counter()->reset();
iveresov@2138 321 }
iveresov@2138 322 }
iveresov@2138 323
iveresov@2138 324 // This method can be called by any component of the runtime to notify the policy
iveresov@2138 325 // that it's recommended to delay the complation of this method.
coleenp@4037 326 void NonTieredCompPolicy::delay_compilation(Method* method) {
jiangli@4936 327 MethodCounters* mcs = method->method_counters();
jiangli@4937 328 if (mcs != NULL) {
jiangli@4937 329 mcs->invocation_counter()->decay();
jiangli@4937 330 mcs->backedge_counter()->decay();
jiangli@4937 331 }
iveresov@2138 332 }
iveresov@2138 333
coleenp@4037 334 void NonTieredCompPolicy::disable_compilation(Method* method) {
jiangli@4936 335 MethodCounters* mcs = method->method_counters();
jiangli@4936 336 if (mcs != NULL) {
jiangli@4936 337 mcs->invocation_counter()->set_state(InvocationCounter::wait_for_nothing);
jiangli@4936 338 mcs->backedge_counter()->set_state(InvocationCounter::wait_for_nothing);
jiangli@4936 339 }
iveresov@2138 340 }
iveresov@2138 341
iveresov@2138 342 CompileTask* NonTieredCompPolicy::select_task(CompileQueue* compile_queue) {
iveresov@2138 343 return compile_queue->first();
iveresov@2138 344 }
iveresov@2138 345
coleenp@4037 346 bool NonTieredCompPolicy::is_mature(Method* method) {
coleenp@4037 347 MethodData* mdo = method->method_data();
iveresov@2138 348 assert(mdo != NULL, "Should be");
iveresov@2138 349 uint current = mdo->mileage_of(method);
iveresov@2138 350 uint initial = mdo->creation_mileage();
iveresov@2138 351 if (current < initial)
iveresov@2138 352 return true; // some sort of overflow
iveresov@2138 353 uint target;
iveresov@2138 354 if (ProfileMaturityPercentage <= 0)
iveresov@2138 355 target = (uint) -ProfileMaturityPercentage; // absolute value
iveresov@2138 356 else
iveresov@2138 357 target = (uint)( (ProfileMaturityPercentage * CompileThreshold) / 100 );
iveresov@2138 358 return (current >= initial + target);
iveresov@2138 359 }
iveresov@2138 360
iveresov@3452 361 nmethod* NonTieredCompPolicy::event(methodHandle method, methodHandle inlinee, int branch_bci,
iveresov@3452 362 int bci, CompLevel comp_level, nmethod* nm, JavaThread* thread) {
iveresov@2138 363 assert(comp_level == CompLevel_none, "This should be only called from the interpreter");
iveresov@2138 364 NOT_PRODUCT(trace_frequency_counter_overflow(method, branch_bci, bci));
iveresov@3452 365 if (JvmtiExport::can_post_interpreter_events() && thread->is_interp_only_mode()) {
iveresov@3452 366 // If certain JVMTI events (e.g. frame pop event) are requested then the
iveresov@3452 367 // thread is forced to remain in interpreted code. This is
iveresov@3452 368 // implemented partly by a check in the run_compiled_code
iveresov@3452 369 // section of the interpreter whether we should skip running
iveresov@3452 370 // compiled code, and partly by skipping OSR compiles for
iveresov@3452 371 // interpreted-only threads.
iveresov@3452 372 if (bci != InvocationEntryBci) {
iveresov@3452 373 reset_counter_for_back_branch_event(method);
iveresov@3452 374 return NULL;
iveresov@2138 375 }
iveresov@2138 376 }
minqi@4267 377 if (CompileTheWorld || ReplayCompiles) {
minqi@4267 378 // Don't trigger other compiles in testing mode
minqi@4267 379 if (bci == InvocationEntryBci) {
minqi@4267 380 reset_counter_for_invocation_event(method);
minqi@4267 381 } else {
minqi@4267 382 reset_counter_for_back_branch_event(method);
minqi@4267 383 }
minqi@4267 384 return NULL;
minqi@4267 385 }
minqi@4267 386
iveresov@2138 387 if (bci == InvocationEntryBci) {
iveresov@2138 388 // when code cache is full, compilation gets switched off, UseCompiler
iveresov@2138 389 // is set to false
iveresov@2138 390 if (!method->has_compiled_code() && UseCompiler) {
iveresov@3452 391 method_invocation_event(method, thread);
iveresov@2138 392 } else {
iveresov@2138 393 // Force counter overflow on method entry, even if no compilation
iveresov@2138 394 // happened. (The method_invocation_event call does this also.)
iveresov@2138 395 reset_counter_for_invocation_event(method);
iveresov@2138 396 }
iveresov@2138 397 // compilation at an invocation overflow no longer goes and retries test for
iveresov@2138 398 // compiled method. We always run the loser of the race as interpreted.
iveresov@2138 399 // so return NULL
iveresov@2138 400 return NULL;
iveresov@2138 401 } else {
iveresov@2138 402 // counter overflow in a loop => try to do on-stack-replacement
iveresov@2138 403 nmethod* osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
iveresov@2138 404 NOT_PRODUCT(trace_osr_request(method, osr_nm, bci));
iveresov@2138 405 // when code cache is full, we should not compile any more...
iveresov@2138 406 if (osr_nm == NULL && UseCompiler) {
iveresov@3452 407 method_back_branch_event(method, bci, thread);
iveresov@2138 408 osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
iveresov@2138 409 }
iveresov@2138 410 if (osr_nm == NULL) {
iveresov@2138 411 reset_counter_for_back_branch_event(method);
iveresov@2138 412 return NULL;
iveresov@2138 413 }
iveresov@2138 414 return osr_nm;
iveresov@2138 415 }
iveresov@2138 416 return NULL;
iveresov@2138 417 }
iveresov@2138 418
iveresov@2138 419 #ifndef PRODUCT
drchase@6680 420 PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL
iveresov@2138 421 void NonTieredCompPolicy::trace_frequency_counter_overflow(methodHandle m, int branch_bci, int bci) {
iveresov@2138 422 if (TraceInvocationCounterOverflow) {
jiangli@4936 423 MethodCounters* mcs = m->method_counters();
jiangli@4936 424 assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
jiangli@4936 425 InvocationCounter* ic = mcs->invocation_counter();
jiangli@4936 426 InvocationCounter* bc = mcs->backedge_counter();
iveresov@2138 427 ResourceMark rm;
iveresov@2138 428 const char* msg =
iveresov@2138 429 bci == InvocationEntryBci
iveresov@2138 430 ? "comp-policy cntr ovfl @ %d in entry of "
iveresov@2138 431 : "comp-policy cntr ovfl @ %d in loop of ";
drchase@6680 432 PRAGMA_DIAG_PUSH
drchase@6680 433 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL
iveresov@2138 434 tty->print(msg, bci);
drchase@6680 435 PRAGMA_DIAG_POP
iveresov@2138 436 m->print_value();
iveresov@2138 437 tty->cr();
iveresov@2138 438 ic->print();
iveresov@2138 439 bc->print();
iveresov@2138 440 if (ProfileInterpreter) {
iveresov@2138 441 if (bci != InvocationEntryBci) {
coleenp@4037 442 MethodData* mdo = m->method_data();
iveresov@2138 443 if (mdo != NULL) {
iveresov@2138 444 int count = mdo->bci_to_data(branch_bci)->as_JumpData()->taken();
iveresov@2138 445 tty->print_cr("back branch count = %d", count);
iveresov@2138 446 }
iveresov@2138 447 }
iveresov@2138 448 }
iveresov@2138 449 }
iveresov@2138 450 }
iveresov@2138 451
iveresov@2138 452 void NonTieredCompPolicy::trace_osr_request(methodHandle method, nmethod* osr, int bci) {
iveresov@2138 453 if (TraceOnStackReplacement) {
iveresov@2138 454 ResourceMark rm;
iveresov@2138 455 tty->print(osr != NULL ? "Reused OSR entry for " : "Requesting OSR entry for ");
iveresov@2138 456 method->print_short_name(tty);
iveresov@2138 457 tty->print_cr(" at bci %d", bci);
iveresov@2138 458 }
iveresov@2138 459 }
iveresov@2138 460 #endif // !PRODUCT
iveresov@2138 461
duke@435 462 // SimpleCompPolicy - compile current method
duke@435 463
iveresov@3452 464 void SimpleCompPolicy::method_invocation_event(methodHandle m, JavaThread* thread) {
twisti@4111 465 const int comp_level = CompLevel_highest_tier;
twisti@4111 466 const int hot_count = m->invocation_count();
duke@435 467 reset_counter_for_invocation_event(m);
duke@435 468 const char* comment = "count";
duke@435 469
iignatyev@5032 470 if (is_compilation_enabled() && can_be_compiled(m, comp_level)) {
duke@435 471 nmethod* nm = m->code();
duke@435 472 if (nm == NULL ) {
twisti@4111 473 CompileBroker::compile_method(m, InvocationEntryBci, comp_level, m, hot_count, comment, thread);
duke@435 474 }
duke@435 475 }
duke@435 476 }
duke@435 477
iveresov@3452 478 void SimpleCompPolicy::method_back_branch_event(methodHandle m, int bci, JavaThread* thread) {
twisti@4111 479 const int comp_level = CompLevel_highest_tier;
twisti@4111 480 const int hot_count = m->backedge_count();
duke@435 481 const char* comment = "backedge_count";
duke@435 482
iignatyev@5541 483 if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) {
twisti@4111 484 CompileBroker::compile_method(m, bci, comp_level, m, hot_count, comment, thread);
twisti@4111 485 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));)
duke@435 486 }
duke@435 487 }
duke@435 488 // StackWalkCompPolicy - walk up stack to find a suitable method to compile
duke@435 489
duke@435 490 #ifdef COMPILER2
duke@435 491 const char* StackWalkCompPolicy::_msg = NULL;
duke@435 492
duke@435 493
duke@435 494 // Consider m for compilation
iveresov@3452 495 void StackWalkCompPolicy::method_invocation_event(methodHandle m, JavaThread* thread) {
twisti@4111 496 const int comp_level = CompLevel_highest_tier;
twisti@4111 497 const int hot_count = m->invocation_count();
duke@435 498 reset_counter_for_invocation_event(m);
duke@435 499 const char* comment = "count";
duke@435 500
iignatyev@5032 501 if (is_compilation_enabled() && m->code() == NULL && can_be_compiled(m, comp_level)) {
iveresov@3452 502 ResourceMark rm(thread);
duke@435 503 frame fr = thread->last_frame();
duke@435 504 assert(fr.is_interpreted_frame(), "must be interpreted");
duke@435 505 assert(fr.interpreter_frame_method() == m(), "bad method");
duke@435 506
duke@435 507 if (TraceCompilationPolicy) {
duke@435 508 tty->print("method invocation trigger: ");
duke@435 509 m->print_short_name(tty);
drchase@6680 510 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)m()), m->code_size());
duke@435 511 }
duke@435 512 RegisterMap reg_map(thread, false);
duke@435 513 javaVFrame* triggerVF = thread->last_java_vframe(&reg_map);
duke@435 514 // triggerVF is the frame that triggered its counter
duke@435 515 RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m);
duke@435 516
duke@435 517 if (first->top_method()->code() != NULL) {
duke@435 518 // called obsolete method/nmethod -- no need to recompile
drchase@6680 519 if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, p2i(first->top_method()->code()));
duke@435 520 } else {
duke@435 521 if (TimeCompilationPolicy) accumulated_time()->start();
duke@435 522 GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
duke@435 523 stack->push(first);
duke@435 524 RFrame* top = findTopInlinableFrame(stack);
duke@435 525 if (TimeCompilationPolicy) accumulated_time()->stop();
duke@435 526 assert(top != NULL, "findTopInlinableFrame returned null");
duke@435 527 if (TraceCompilationPolicy) top->print();
twisti@4111 528 CompileBroker::compile_method(top->top_method(), InvocationEntryBci, comp_level,
iveresov@3452 529 m, hot_count, comment, thread);
duke@435 530 }
duke@435 531 }
duke@435 532 }
duke@435 533
iveresov@3452 534 void StackWalkCompPolicy::method_back_branch_event(methodHandle m, int bci, JavaThread* thread) {
twisti@4111 535 const int comp_level = CompLevel_highest_tier;
twisti@4111 536 const int hot_count = m->backedge_count();
duke@435 537 const char* comment = "backedge_count";
duke@435 538
iignatyev@5541 539 if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) {
twisti@4111 540 CompileBroker::compile_method(m, bci, comp_level, m, hot_count, comment, thread);
twisti@4111 541 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));)
duke@435 542 }
duke@435 543 }
duke@435 544
duke@435 545 RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray<RFrame*>* stack) {
duke@435 546 // go up the stack until finding a frame that (probably) won't be inlined
duke@435 547 // into its caller
duke@435 548 RFrame* current = stack->at(0); // current choice for stopping
duke@435 549 assert( current && !current->is_compiled(), "" );
duke@435 550 const char* msg = NULL;
duke@435 551
duke@435 552 while (1) {
duke@435 553
duke@435 554 // before going up the stack further, check if doing so would get us into
duke@435 555 // compiled code
duke@435 556 RFrame* next = senderOf(current, stack);
duke@435 557 if( !next ) // No next frame up the stack?
duke@435 558 break; // Then compile with current frame
duke@435 559
duke@435 560 methodHandle m = current->top_method();
duke@435 561 methodHandle next_m = next->top_method();
duke@435 562
duke@435 563 if (TraceCompilationPolicy && Verbose) {
duke@435 564 tty->print("[caller: ");
duke@435 565 next_m->print_short_name(tty);
duke@435 566 tty->print("] ");
duke@435 567 }
duke@435 568
duke@435 569 if( !Inline ) { // Inlining turned off
duke@435 570 msg = "Inlining turned off";
duke@435 571 break;
duke@435 572 }
duke@435 573 if (next_m->is_not_compilable()) { // Did fail to compile this before/
duke@435 574 msg = "caller not compilable";
duke@435 575 break;
duke@435 576 }
duke@435 577 if (next->num() > MaxRecompilationSearchLength) {
duke@435 578 // don't go up too high when searching for recompilees
duke@435 579 msg = "don't go up any further: > MaxRecompilationSearchLength";
duke@435 580 break;
duke@435 581 }
duke@435 582 if (next->distance() > MaxInterpretedSearchLength) {
duke@435 583 // don't go up too high when searching for recompilees
duke@435 584 msg = "don't go up any further: next > MaxInterpretedSearchLength";
duke@435 585 break;
duke@435 586 }
duke@435 587 // Compiled frame above already decided not to inline;
duke@435 588 // do not recompile him.
duke@435 589 if (next->is_compiled()) {
duke@435 590 msg = "not going up into optimized code";
duke@435 591 break;
duke@435 592 }
duke@435 593
duke@435 594 // Interpreted frame above us was already compiled. Do not force
duke@435 595 // a recompile, although if the frame above us runs long enough an
duke@435 596 // OSR might still happen.
duke@435 597 if( current->is_interpreted() && next_m->has_compiled_code() ) {
duke@435 598 msg = "not going up -- already compiled caller";
duke@435 599 break;
duke@435 600 }
duke@435 601
duke@435 602 // Compute how frequent this call site is. We have current method 'm'.
duke@435 603 // We know next method 'next_m' is interpreted. Find the call site and
duke@435 604 // check the various invocation counts.
duke@435 605 int invcnt = 0; // Caller counts
duke@435 606 if (ProfileInterpreter) {
duke@435 607 invcnt = next_m->interpreter_invocation_count();
duke@435 608 }
duke@435 609 int cnt = 0; // Call site counts
duke@435 610 if (ProfileInterpreter && next_m->method_data() != NULL) {
duke@435 611 ResourceMark rm;
duke@435 612 int bci = next->top_vframe()->bci();
duke@435 613 ProfileData* data = next_m->method_data()->bci_to_data(bci);
duke@435 614 if (data != NULL && data->is_CounterData())
duke@435 615 cnt = data->as_CounterData()->count();
duke@435 616 }
duke@435 617
duke@435 618 // Caller counts / call-site counts; i.e. is this call site
duke@435 619 // a hot call site for method next_m?
duke@435 620 int freq = (invcnt) ? cnt/invcnt : cnt;
duke@435 621
duke@435 622 // Check size and frequency limits
duke@435 623 if ((msg = shouldInline(m, freq, cnt)) != NULL) {
duke@435 624 break;
duke@435 625 }
duke@435 626 // Check inlining negative tests
duke@435 627 if ((msg = shouldNotInline(m)) != NULL) {
duke@435 628 break;
duke@435 629 }
duke@435 630
duke@435 631
duke@435 632 // If the caller method is too big or something then we do not want to
duke@435 633 // compile it just to inline a method
iignatyev@5032 634 if (!can_be_compiled(next_m, CompLevel_any)) {
duke@435 635 msg = "caller cannot be compiled";
duke@435 636 break;
duke@435 637 }
duke@435 638
duke@435 639 if( next_m->name() == vmSymbols::class_initializer_name() ) {
duke@435 640 msg = "do not compile class initializer (OSR ok)";
duke@435 641 break;
duke@435 642 }
duke@435 643
duke@435 644 if (TraceCompilationPolicy && Verbose) {
duke@435 645 tty->print("\n\t check caller: ");
duke@435 646 next_m->print_short_name(tty);
drchase@6680 647 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)next_m()), next_m->code_size());
duke@435 648 }
duke@435 649
duke@435 650 current = next;
duke@435 651 }
duke@435 652
duke@435 653 assert( !current || !current->is_compiled(), "" );
duke@435 654
duke@435 655 if (TraceCompilationPolicy && msg) tty->print("(%s)\n", msg);
duke@435 656
duke@435 657 return current;
duke@435 658 }
duke@435 659
duke@435 660 RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray<RFrame*>* stack) {
duke@435 661 RFrame* sender = rf->caller();
duke@435 662 if (sender && sender->num() == stack->length()) stack->push(sender);
duke@435 663 return sender;
duke@435 664 }
duke@435 665
duke@435 666
duke@435 667 const char* StackWalkCompPolicy::shouldInline(methodHandle m, float freq, int cnt) {
duke@435 668 // Allows targeted inlining
duke@435 669 // positive filter: should send be inlined? returns NULL (--> yes)
duke@435 670 // or rejection msg
duke@435 671 int max_size = MaxInlineSize;
duke@435 672 int cost = m->code_size();
duke@435 673
duke@435 674 // Check for too many throws (and not too huge)
duke@435 675 if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) {
duke@435 676 return NULL;
duke@435 677 }
duke@435 678
duke@435 679 // bump the max size if the call is frequent
duke@435 680 if ((freq >= InlineFrequencyRatio) || (cnt >= InlineFrequencyCount)) {
duke@435 681 if (TraceFrequencyInlining) {
duke@435 682 tty->print("(Inlined frequent method)\n");
duke@435 683 m->print();
duke@435 684 }
duke@435 685 max_size = FreqInlineSize;
duke@435 686 }
duke@435 687 if (cost > max_size) {
duke@435 688 return (_msg = "too big");
duke@435 689 }
duke@435 690 return NULL;
duke@435 691 }
duke@435 692
duke@435 693
duke@435 694 const char* StackWalkCompPolicy::shouldNotInline(methodHandle m) {
duke@435 695 // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg
duke@435 696 if (m->is_abstract()) return (_msg = "abstract method");
duke@435 697 // note: we allow ik->is_abstract()
coleenp@4251 698 if (!m->method_holder()->is_initialized()) return (_msg = "method holder not initialized");
duke@435 699 if (m->is_native()) return (_msg = "native method");
duke@435 700 nmethod* m_code = m->code();
twisti@2103 701 if (m_code != NULL && m_code->code_size() > InlineSmallCode)
duke@435 702 return (_msg = "already compiled into a big method");
duke@435 703
duke@435 704 // use frequency-based objections only for non-trivial methods
duke@435 705 if (m->code_size() <= MaxTrivialSize) return NULL;
duke@435 706 if (UseInterpreter) { // don't use counts with -Xcomp
duke@435 707 if ((m->code() == NULL) && m->was_never_executed()) return (_msg = "never executed");
duke@435 708 if (!m->was_executed_more_than(MIN2(MinInliningThreshold, CompileThreshold >> 1))) return (_msg = "executed < MinInliningThreshold times");
duke@435 709 }
coleenp@4037 710 if (Method::has_unloaded_classes_in_signature(m, JavaThread::current())) return (_msg = "unloaded signature classes");
duke@435 711
duke@435 712 return NULL;
duke@435 713 }
duke@435 714
duke@435 715
duke@435 716
duke@435 717 #endif // COMPILER2

mercurial