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