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