1 /*
   2  * Copyright (c) 2010, 2020, 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 "compiler/compileBroker.hpp"
  27 #include "compiler/compilerOracle.hpp"
  28 #include "compiler/tieredThresholdPolicy.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "runtime/arguments.hpp"
  31 #include "runtime/frame.inline.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/safepoint.hpp"
  34 #include "runtime/safepointVerifiers.hpp"
  35 #include "code/scopeDesc.hpp"
  36 #include "oops/method.inline.hpp"
  37 #if INCLUDE_JVMCI
  38 #include "jvmci/jvmci.hpp"
  39 #endif
  40 
  41 #ifdef TIERED
  42 
  43 #include "c1/c1_Compiler.hpp"
  44 #include "opto/c2compiler.hpp"
  45 
  46 bool TieredThresholdPolicy::call_predicate_helper(const methodHandle& method, CompLevel cur_level, int i, int b, double scale) {
  47   double threshold_scaling;
  48   if (CompilerOracle::has_option_value(method, "CompileThresholdScaling", threshold_scaling)) {
  49     scale *= threshold_scaling;
  50   }
  51   switch(cur_level) {
  52   case CompLevel_aot:
  53     if (CompilationModeFlag::disable_intermediate()) {
  54       return (i >= Tier0AOTInvocationThreshold * scale) ||
  55              (i >= Tier0AOTMinInvocationThreshold * scale && i + b >= Tier0AOTCompileThreshold * scale);
  56     } else {
  57       return (i >= Tier3AOTInvocationThreshold * scale) ||
  58              (i >= Tier3AOTMinInvocationThreshold * scale && i + b >= Tier3AOTCompileThreshold * scale);
  59     }
  60   case CompLevel_none:
  61     if (CompilationModeFlag::disable_intermediate()) {
  62       return (i >= Tier40InvocationThreshold * scale) ||
  63              (i >= Tier40MinInvocationThreshold * scale && i + b >= Tier40CompileThreshold * scale);
  64     }
  65     // Fall through
  66   case CompLevel_limited_profile:
  67     return (i >= Tier3InvocationThreshold * scale) ||
  68            (i >= Tier3MinInvocationThreshold * scale && i + b >= Tier3CompileThreshold * scale);
  69   case CompLevel_full_profile:
  70    return (i >= Tier4InvocationThreshold * scale) ||
  71           (i >= Tier4MinInvocationThreshold * scale && i + b >= Tier4CompileThreshold * scale);
  72   default:
  73    return true;
  74   }
  75 }
  76 
  77 bool TieredThresholdPolicy::loop_predicate_helper(const methodHandle& method, CompLevel cur_level, int i, int b, double scale) {
  78   double threshold_scaling;
  79   if (CompilerOracle::has_option_value(method, "CompileThresholdScaling", threshold_scaling)) {
  80     scale *= threshold_scaling;
  81   }
  82   switch(cur_level) {
  83   case CompLevel_aot:
  84     if (CompilationModeFlag::disable_intermediate()) {
  85       return b >= Tier0AOTBackEdgeThreshold * scale;
  86     } else {
  87       return b >= Tier3AOTBackEdgeThreshold * scale;
  88     }
  89   case CompLevel_none:
  90     if (CompilationModeFlag::disable_intermediate()) {
  91       return b >= Tier40BackEdgeThreshold * scale;
  92     }
  93     // Fall through
  94   case CompLevel_limited_profile:
  95     return b >= Tier3BackEdgeThreshold * scale;
  96   case CompLevel_full_profile:
  97     return b >= Tier4BackEdgeThreshold * scale;
  98   default:
  99     return true;
 100   }
 101 }
 102 
 103 // Simple methods are as good being compiled with C1 as C2.
 104 // Determine if a given method is such a case.
 105 bool TieredThresholdPolicy::is_trivial(Method* method) {
 106   if (method->is_accessor() ||
 107       method->is_constant_getter()) {
 108     return true;
 109   }
 110   return false;
 111 }
 112 
 113 bool TieredThresholdPolicy::force_comp_at_level_simple(const methodHandle& method) {
 114   if (CompilationModeFlag::quick_internal()) {
 115 #if INCLUDE_JVMCI
 116     if (UseJVMCICompiler) {
 117       AbstractCompiler* comp = CompileBroker::compiler(CompLevel_full_optimization);
 118       if (comp != NULL && comp->is_jvmci() && ((JVMCICompiler*) comp)->force_comp_at_level_simple(method)) {
 119         return true;
 120       }
 121     }
 122 #endif
 123   }
 124   return false;
 125 }
 126 
 127 CompLevel TieredThresholdPolicy::comp_level(Method* method) {
 128   CompiledMethod *nm = method->code();
 129   if (nm != NULL && nm->is_in_use()) {
 130     return (CompLevel)nm->comp_level();
 131   }
 132   return CompLevel_none;
 133 }
 134 
 135 void TieredThresholdPolicy::print_counters(const char* prefix, Method* m) {
 136   int invocation_count = m->invocation_count();
 137   int backedge_count = m->backedge_count();
 138   MethodData* mdh = m->method_data();
 139   int mdo_invocations = 0, mdo_backedges = 0;
 140   int mdo_invocations_start = 0, mdo_backedges_start = 0;
 141   if (mdh != NULL) {
 142     mdo_invocations = mdh->invocation_count();
 143     mdo_backedges = mdh->backedge_count();
 144     mdo_invocations_start = mdh->invocation_count_start();
 145     mdo_backedges_start = mdh->backedge_count_start();
 146   }
 147   tty->print(" %stotal=%d,%d %smdo=%d(%d),%d(%d)", prefix,
 148       invocation_count, backedge_count, prefix,
 149       mdo_invocations, mdo_invocations_start,
 150       mdo_backedges, mdo_backedges_start);
 151   tty->print(" %smax levels=%d,%d", prefix,
 152       m->highest_comp_level(), m->highest_osr_comp_level());
 153 }
 154 
 155 // Print an event.
 156 void TieredThresholdPolicy::print_event(EventType type, Method* m, Method* im,
 157                                         int bci, CompLevel level) {
 158   bool inlinee_event = m != im;
 159 
 160   ttyLocker tty_lock;
 161   tty->print("%lf: [", os::elapsedTime());
 162 
 163   switch(type) {
 164   case CALL:
 165     tty->print("call");
 166     break;
 167   case LOOP:
 168     tty->print("loop");
 169     break;
 170   case COMPILE:
 171     tty->print("compile");
 172     break;
 173   case REMOVE_FROM_QUEUE:
 174     tty->print("remove-from-queue");
 175     break;
 176   case UPDATE_IN_QUEUE:
 177     tty->print("update-in-queue");
 178     break;
 179   case REPROFILE:
 180     tty->print("reprofile");
 181     break;
 182   case MAKE_NOT_ENTRANT:
 183     tty->print("make-not-entrant");
 184     break;
 185   default:
 186     tty->print("unknown");
 187   }
 188 
 189   tty->print(" level=%d ", level);
 190 
 191   ResourceMark rm;
 192   char *method_name = m->name_and_sig_as_C_string();
 193   tty->print("[%s", method_name);
 194   if (inlinee_event) {
 195     char *inlinee_name = im->name_and_sig_as_C_string();
 196     tty->print(" [%s]] ", inlinee_name);
 197   }
 198   else tty->print("] ");
 199   tty->print("@%d queues=%d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile),
 200                                       CompileBroker::queue_size(CompLevel_full_optimization));
 201 
 202   tty->print(" rate=");
 203   if (m->prev_time() == 0) tty->print("n/a");
 204   else tty->print("%f", m->rate());
 205 
 206   tty->print(" k=%.2lf,%.2lf", threshold_scale(CompLevel_full_profile, Tier3LoadFeedback),
 207                                threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback));
 208 
 209   if (type != COMPILE) {
 210     print_counters("", m);
 211     if (inlinee_event) {
 212       print_counters("inlinee ", im);
 213     }
 214     tty->print(" compilable=");
 215     bool need_comma = false;
 216     if (!m->is_not_compilable(CompLevel_full_profile)) {
 217       tty->print("c1");
 218       need_comma = true;
 219     }
 220     if (!m->is_not_osr_compilable(CompLevel_full_profile)) {
 221       if (need_comma) tty->print(",");
 222       tty->print("c1-osr");
 223       need_comma = true;
 224     }
 225     if (!m->is_not_compilable(CompLevel_full_optimization)) {
 226       if (need_comma) tty->print(",");
 227       tty->print("c2");
 228       need_comma = true;
 229     }
 230     if (!m->is_not_osr_compilable(CompLevel_full_optimization)) {
 231       if (need_comma) tty->print(",");
 232       tty->print("c2-osr");
 233     }
 234     tty->print(" status=");
 235     if (m->queued_for_compilation()) {
 236       tty->print("in-queue");
 237     } else tty->print("idle");
 238   }
 239   tty->print_cr("]");
 240 }
 241 
 242 
 243 void TieredThresholdPolicy::initialize() {
 244   int count = CICompilerCount;
 245   bool c1_only = TieredStopAtLevel < CompLevel_full_optimization || CompilationModeFlag::quick_only();
 246   bool c2_only = CompilationModeFlag::high_only();
 247 #ifdef _LP64
 248   // Turn on ergonomic compiler count selection
 249   if (FLAG_IS_DEFAULT(CICompilerCountPerCPU) && FLAG_IS_DEFAULT(CICompilerCount)) {
 250     FLAG_SET_DEFAULT(CICompilerCountPerCPU, true);
 251   }
 252   if (CICompilerCountPerCPU) {
 253     // Simple log n seems to grow too slowly for tiered, try something faster: log n * log log n
 254     int log_cpu = log2_int(os::active_processor_count());
 255     int loglog_cpu = log2_int(MAX2(log_cpu, 1));
 256     count = MAX2(log_cpu * loglog_cpu * 3 / 2, 2);
 257     // Make sure there is enough space in the code cache to hold all the compiler buffers
 258     size_t c1_size = Compiler::code_buffer_size();
 259     size_t c2_size = C2Compiler::initial_code_buffer_size();
 260     size_t buffer_size = c1_only ? c1_size : (c1_size/3 + 2*c2_size/3);
 261     int max_count = (ReservedCodeCacheSize - (CodeCacheMinimumUseSpace DEBUG_ONLY(* 3))) / (int)buffer_size;
 262     if (count > max_count) {
 263       // Lower the compiler count such that all buffers fit into the code cache
 264       count = MAX2(max_count, c1_only ? 1 : 2);
 265     }
 266     FLAG_SET_ERGO(CICompilerCount, count);
 267   }
 268 #else
 269   // On 32-bit systems, the number of compiler threads is limited to 3.
 270   // On these systems, the virtual address space available to the JVM
 271   // is usually limited to 2-4 GB (the exact value depends on the platform).
 272   // As the compilers (especially C2) can consume a large amount of
 273   // memory, scaling the number of compiler threads with the number of
 274   // available cores can result in the exhaustion of the address space
 275   /// available to the VM and thus cause the VM to crash.
 276   if (FLAG_IS_DEFAULT(CICompilerCount)) {
 277     count = 3;
 278     FLAG_SET_ERGO(CICompilerCount, count);
 279   }
 280 #endif
 281 
 282   if (c1_only) {
 283     // No C2 compiler thread required
 284     set_c1_count(count);
 285   } else if (c2_only) {
 286     set_c2_count(count);
 287   } else {
 288     set_c1_count(MAX2(count / 3, 1));
 289     set_c2_count(MAX2(count - c1_count(), 1));
 290   }
 291   assert(count == c1_count() + c2_count(), "inconsistent compiler thread count");
 292 
 293   // Some inlining tuning
 294 #ifdef X86
 295   if (FLAG_IS_DEFAULT(InlineSmallCode)) {
 296     FLAG_SET_DEFAULT(InlineSmallCode, 2000);
 297   }
 298 #endif
 299 
 300 #if defined AARCH64
 301   if (FLAG_IS_DEFAULT(InlineSmallCode)) {
 302     FLAG_SET_DEFAULT(InlineSmallCode, 2500);
 303   }
 304 #endif
 305 
 306   set_increase_threshold_at_ratio();
 307   set_start_time(nanos_to_millis(os::javaTimeNanos()));
 308 }
 309 
 310 
 311 #ifdef ASSERT
 312 bool TieredThresholdPolicy::verify_level(CompLevel level) {
 313   // AOT and interpreter levels are always valid.
 314   if (level == CompLevel_aot || level == CompLevel_none) {
 315     return true;
 316   }
 317   if (CompilationModeFlag::normal()) {
 318     return true;
 319   } else if (CompilationModeFlag::quick_only()) {
 320     return level == CompLevel_simple;
 321   } else if (CompilationModeFlag::high_only()) {
 322     return level == CompLevel_full_optimization;
 323   } else if (CompilationModeFlag::high_only_quick_internal()) {
 324     return level == CompLevel_full_optimization || level == CompLevel_simple;
 325   }
 326   return false;
 327 }
 328 #endif
 329 
 330 
 331 CompLevel TieredThresholdPolicy::limit_level(CompLevel level) {
 332   if (CompilationModeFlag::quick_only()) {
 333     level = MIN2(level, CompLevel_simple);
 334   }
 335   assert(verify_level(level), "Invalid compilation level %d", level);
 336   if (level <= TieredStopAtLevel) {
 337     return level;
 338   }
 339   // Some compilation levels are not valid depending on a compilation mode:
 340   // a) quick_only - levels 2,3,4 are invalid; levels -1,0,1 are valid;
 341   // b) high_only - levels 1,2,3 are invalid; levels -1,0,4 are valid;
 342   // c) high_only_quick_internal - levels 2,3 are invalid; levels -1,0,1,4 are valid.
 343   // The invalid levels are actually sequential so a single comparison is sufficient.
 344   // Down here we already have (level > TieredStopAtLevel), which also implies that
 345   // (TieredStopAtLevel < Highest Possible Level), so we need to return a level that is:
 346   // a) a max level that is strictly less than the highest for a given compilation mode
 347   // b) less or equal to TieredStopAtLevel
 348   if (CompilationModeFlag::normal() || CompilationModeFlag::quick_only()) {
 349     return (CompLevel)TieredStopAtLevel;
 350   }
 351 
 352   if (CompilationModeFlag::high_only() || CompilationModeFlag::high_only_quick_internal()) {
 353     return MIN2(CompLevel_none, (CompLevel)TieredStopAtLevel);
 354   }
 355 
 356   ShouldNotReachHere();
 357   return CompLevel_any;
 358 }
 359 
 360 CompLevel TieredThresholdPolicy::initial_compile_level_helper(const methodHandle& method) {
 361   if (CompilationModeFlag::normal()) {
 362     return CompLevel_full_profile;
 363   } else if (CompilationModeFlag::quick_only()) {
 364     return CompLevel_simple;
 365   } else if (CompilationModeFlag::high_only()) {
 366     return CompLevel_full_optimization;
 367   } else if (CompilationModeFlag::high_only_quick_internal()) {
 368     if (force_comp_at_level_simple(method)) {
 369       return CompLevel_simple;
 370     } else {
 371       return CompLevel_full_optimization;
 372     }
 373   }
 374   ShouldNotReachHere();
 375   return CompLevel_any;
 376 }
 377 
 378 CompLevel TieredThresholdPolicy::initial_compile_level(const methodHandle& method) {
 379   return limit_level(initial_compile_level_helper(method));
 380 }
 381 
 382 // Set carry flags on the counters if necessary
 383 void TieredThresholdPolicy::handle_counter_overflow(Method* method) {
 384   MethodCounters *mcs = method->method_counters();
 385   if (mcs != NULL) {
 386     mcs->invocation_counter()->set_carry_on_overflow();
 387     mcs->backedge_counter()->set_carry_on_overflow();
 388   }
 389   MethodData* mdo = method->method_data();
 390   if (mdo != NULL) {
 391     mdo->invocation_counter()->set_carry_on_overflow();
 392     mdo->backedge_counter()->set_carry_on_overflow();
 393   }
 394 }
 395 
 396 // Called with the queue locked and with at least one element
 397 CompileTask* TieredThresholdPolicy::select_task(CompileQueue* compile_queue) {
 398   CompileTask *max_blocking_task = NULL;
 399   CompileTask *max_task = NULL;
 400   Method* max_method = NULL;
 401   jlong t = nanos_to_millis(os::javaTimeNanos());
 402   // Iterate through the queue and find a method with a maximum rate.
 403   for (CompileTask* task = compile_queue->first(); task != NULL;) {
 404     CompileTask* next_task = task->next();
 405     Method* method = task->method();
 406     // If a method was unloaded or has been stale for some time, remove it from the queue.
 407     // Blocking tasks and tasks submitted from whitebox API don't become stale
 408     if (task->is_unloaded() || (task->can_become_stale() && is_stale(t, TieredCompileTaskTimeout, method) && !is_old(method))) {
 409       if (!task->is_unloaded()) {
 410         if (PrintTieredEvents) {
 411           print_event(REMOVE_FROM_QUEUE, method, method, task->osr_bci(), (CompLevel) task->comp_level());
 412         }
 413         method->clear_queued_for_compilation();
 414       }
 415       compile_queue->remove_and_mark_stale(task);
 416       task = next_task;
 417       continue;
 418     }
 419     update_rate(t, method);
 420     if (max_task == NULL || compare_methods(method, max_method)) {
 421       // Select a method with the highest rate
 422       max_task = task;
 423       max_method = method;
 424     }
 425 
 426     if (task->is_blocking()) {
 427       if (max_blocking_task == NULL || compare_methods(method, max_blocking_task->method())) {
 428         max_blocking_task = task;
 429       }
 430     }
 431 
 432     task = next_task;
 433   }
 434 
 435   if (max_blocking_task != NULL) {
 436     // In blocking compilation mode, the CompileBroker will make
 437     // compilations submitted by a JVMCI compiler thread non-blocking. These
 438     // compilations should be scheduled after all blocking compilations
 439     // to service non-compiler related compilations sooner and reduce the
 440     // chance of such compilations timing out.
 441     max_task = max_blocking_task;
 442     max_method = max_task->method();
 443   }
 444 
 445   methodHandle max_method_h(Thread::current(), max_method);
 446 
 447   if (max_task != NULL && max_task->comp_level() == CompLevel_full_profile &&
 448       TieredStopAtLevel > CompLevel_full_profile &&
 449       max_method != NULL && is_method_profiled(max_method_h)) {
 450     max_task->set_comp_level(CompLevel_limited_profile);
 451 
 452     if (CompileBroker::compilation_is_complete(max_method_h, max_task->osr_bci(), CompLevel_limited_profile)) {
 453       if (PrintTieredEvents) {
 454         print_event(REMOVE_FROM_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level());
 455       }
 456       compile_queue->remove_and_mark_stale(max_task);
 457       max_method->clear_queued_for_compilation();
 458       return NULL;
 459     }
 460 
 461     if (PrintTieredEvents) {
 462       print_event(UPDATE_IN_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level());
 463     }
 464   }
 465 
 466   return max_task;
 467 }
 468 
 469 void TieredThresholdPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
 470   for (ScopeDesc* sd = trap_scope;; sd = sd->sender()) {
 471     if (PrintTieredEvents) {
 472       print_event(REPROFILE, sd->method(), sd->method(), InvocationEntryBci, CompLevel_none);
 473     }
 474     MethodData* mdo = sd->method()->method_data();
 475     if (mdo != NULL) {
 476       mdo->reset_start_counters();
 477     }
 478     if (sd->is_top()) break;
 479   }
 480 }
 481 
 482 nmethod* TieredThresholdPolicy::event(const methodHandle& method, const methodHandle& inlinee,
 483                                       int branch_bci, int bci, CompLevel comp_level, CompiledMethod* nm, JavaThread* thread) {
 484   if (comp_level == CompLevel_none &&
 485       JvmtiExport::can_post_interpreter_events() &&
 486       thread->is_interp_only_mode()) {
 487     return NULL;
 488   }
 489   if (ReplayCompiles) {
 490     // Don't trigger other compiles in testing mode
 491     return NULL;
 492   }
 493 
 494   handle_counter_overflow(method());
 495   if (method() != inlinee()) {
 496     handle_counter_overflow(inlinee());
 497   }
 498 
 499   if (PrintTieredEvents) {
 500     print_event(bci == InvocationEntryBci ? CALL : LOOP, method(), inlinee(), bci, comp_level);
 501   }
 502 
 503   if (bci == InvocationEntryBci) {
 504     method_invocation_event(method, inlinee, comp_level, nm, thread);
 505   } else {
 506     // method == inlinee if the event originated in the main method
 507     method_back_branch_event(method, inlinee, bci, comp_level, nm, thread);
 508     // Check if event led to a higher level OSR compilation
 509     CompLevel expected_comp_level = comp_level;
 510     if (!CompilationModeFlag::disable_intermediate() && inlinee->is_not_osr_compilable(expected_comp_level)) {
 511       // It's not possble to reach the expected level so fall back to simple.
 512       expected_comp_level = CompLevel_simple;
 513     }
 514     nmethod* osr_nm = inlinee->lookup_osr_nmethod_for(bci, expected_comp_level, false);
 515     assert(osr_nm == NULL || osr_nm->comp_level() >= expected_comp_level, "lookup_osr_nmethod_for is broken");
 516     if (osr_nm != NULL) {
 517       // Perform OSR with new nmethod
 518       return osr_nm;
 519     }
 520   }
 521   return NULL;
 522 }
 523 
 524 // Check if the method can be compiled, change level if necessary
 525 void TieredThresholdPolicy::compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
 526   assert(verify_level(level) && level <= TieredStopAtLevel, "Invalid compilation level %d", level);
 527 
 528   if (level == CompLevel_none) {
 529     if (mh->has_compiled_code()) {
 530       // Happens when we switch from AOT to interpreter to profile.
 531       MutexLocker ml(Compile_lock);
 532       NoSafepointVerifier nsv;
 533       if (mh->has_compiled_code()) {
 534         mh->code()->make_not_used();
 535       }
 536       // Deoptimize immediately (we don't have to wait for a compile).
 537       RegisterMap map(thread, false);
 538       frame fr = thread->last_frame().sender(&map);
 539       Deoptimization::deoptimize_frame(thread, fr.id());
 540     }
 541     return;
 542   }
 543   if (level == CompLevel_aot) {
 544     if (mh->has_aot_code()) {
 545       if (PrintTieredEvents) {
 546         print_event(COMPILE, mh(), mh(), bci, level);
 547       }
 548       MutexLocker ml(Compile_lock);
 549       NoSafepointVerifier nsv;
 550       if (mh->has_aot_code() && mh->code() != mh->aot_code()) {
 551         mh->aot_code()->make_entrant();
 552         if (mh->has_compiled_code()) {
 553           mh->code()->make_not_entrant();
 554         }
 555         MutexLocker pl(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
 556         Method::set_code(mh, mh->aot_code());
 557       }
 558     }
 559     return;
 560   }
 561 
 562   if (!CompilationModeFlag::disable_intermediate()) {
 563     // Check if the method can be compiled. If it cannot be compiled with C1, continue profiling
 564     // in the interpreter and then compile with C2 (the transition function will request that,
 565     // see common() ). If the method cannot be compiled with C2 but still can with C1, compile it with
 566     // pure C1.
 567     if ((bci == InvocationEntryBci && !can_be_compiled(mh, level))) {
 568       if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) {
 569         compile(mh, bci, CompLevel_simple, thread);
 570       }
 571       return;
 572     }
 573     if ((bci != InvocationEntryBci && !can_be_osr_compiled(mh, level))) {
 574       if (level == CompLevel_full_optimization && can_be_osr_compiled(mh, CompLevel_simple)) {
 575         nmethod* osr_nm = mh->lookup_osr_nmethod_for(bci, CompLevel_simple, false);
 576         if (osr_nm != NULL && osr_nm->comp_level() > CompLevel_simple) {
 577           // Invalidate the existing OSR nmethod so that a compile at CompLevel_simple is permitted.
 578           osr_nm->make_not_entrant();
 579         }
 580         compile(mh, bci, CompLevel_simple, thread);
 581       }
 582       return;
 583     }
 584   }
 585   if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) {
 586     return;
 587   }
 588   if (!CompileBroker::compilation_is_in_queue(mh)) {
 589     if (PrintTieredEvents) {
 590       print_event(COMPILE, mh(), mh(), bci, level);
 591     }
 592     int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
 593     update_rate(nanos_to_millis(os::javaTimeNanos()), mh());
 594     CompileBroker::compile_method(mh, bci, level, mh, hot_count, CompileTask::Reason_Tiered, thread);
 595   }
 596 }
 597 
 598 // update_rate() is called from select_task() while holding a compile queue lock.
 599 void TieredThresholdPolicy::update_rate(jlong t, Method* m) {
 600   // Skip update if counters are absent.
 601   // Can't allocate them since we are holding compile queue lock.
 602   if (m->method_counters() == NULL)  return;
 603 
 604   if (is_old(m)) {
 605     // We don't remove old methods from the queue,
 606     // so we can just zero the rate.
 607     m->set_rate(0);
 608     return;
 609   }
 610 
 611   // We don't update the rate if we've just came out of a safepoint.
 612   // delta_s is the time since last safepoint in milliseconds.
 613   jlong delta_s = t - SafepointTracing::end_of_last_safepoint_ms();
 614   jlong delta_t = t - (m->prev_time() != 0 ? m->prev_time() : start_time()); // milliseconds since the last measurement
 615   // How many events were there since the last time?
 616   int event_count = m->invocation_count() + m->backedge_count();
 617   int delta_e = event_count - m->prev_event_count();
 618 
 619   // We should be running for at least 1ms.
 620   if (delta_s >= TieredRateUpdateMinTime) {
 621     // And we must've taken the previous point at least 1ms before.
 622     if (delta_t >= TieredRateUpdateMinTime && delta_e > 0) {
 623       m->set_prev_time(t);
 624       m->set_prev_event_count(event_count);
 625       m->set_rate((float)delta_e / (float)delta_t); // Rate is events per millisecond
 626     } else {
 627       if (delta_t > TieredRateUpdateMaxTime && delta_e == 0) {
 628         // If nothing happened for 25ms, zero the rate. Don't modify prev values.
 629         m->set_rate(0);
 630       }
 631     }
 632   }
 633 }
 634 
 635 // Check if this method has been stale for a given number of milliseconds.
 636 // See select_task().
 637 bool TieredThresholdPolicy::is_stale(jlong t, jlong timeout, Method* m) {
 638   jlong delta_s = t - SafepointTracing::end_of_last_safepoint_ms();
 639   jlong delta_t = t - m->prev_time();
 640   if (delta_t > timeout && delta_s > timeout) {
 641     int event_count = m->invocation_count() + m->backedge_count();
 642     int delta_e = event_count - m->prev_event_count();
 643     // Return true if there were no events.
 644     return delta_e == 0;
 645   }
 646   return false;
 647 }
 648 
 649 // We don't remove old methods from the compile queue even if they have
 650 // very low activity. See select_task().
 651 bool TieredThresholdPolicy::is_old(Method* method) {
 652   return method->invocation_count() > 50000 || method->backedge_count() > 500000;
 653 }
 654 
 655 double TieredThresholdPolicy::weight(Method* method) {
 656   return (double)(method->rate() + 1) *
 657     (method->invocation_count() + 1) * (method->backedge_count() + 1);
 658 }
 659 
 660 // Apply heuristics and return true if x should be compiled before y
 661 bool TieredThresholdPolicy::compare_methods(Method* x, Method* y) {
 662   if (x->highest_comp_level() > y->highest_comp_level()) {
 663     // recompilation after deopt
 664     return true;
 665   } else
 666     if (x->highest_comp_level() == y->highest_comp_level()) {
 667       if (weight(x) > weight(y)) {
 668         return true;
 669       }
 670     }
 671   return false;
 672 }
 673 
 674 // Is method profiled enough?
 675 bool TieredThresholdPolicy::is_method_profiled(const methodHandle& method) {
 676   MethodData* mdo = method->method_data();
 677   if (mdo != NULL) {
 678     int i = mdo->invocation_count_delta();
 679     int b = mdo->backedge_count_delta();
 680     return call_predicate_helper(method, CompilationModeFlag::disable_intermediate() ? CompLevel_none : CompLevel_full_profile, i, b, 1);
 681   }
 682   return false;
 683 }
 684 
 685 double TieredThresholdPolicy::threshold_scale(CompLevel level, int feedback_k) {
 686   int comp_count = compiler_count(level);
 687   if (comp_count > 0) {
 688     double queue_size = CompileBroker::queue_size(level);
 689     double k = queue_size / (feedback_k * comp_count) + 1;
 690 
 691     // Increase C1 compile threshold when the code cache is filled more
 692     // than specified by IncreaseFirstTierCompileThresholdAt percentage.
 693     // The main intention is to keep enough free space for C2 compiled code
 694     // to achieve peak performance if the code cache is under stress.
 695     if (!CompilationModeFlag::disable_intermediate() && TieredStopAtLevel == CompLevel_full_optimization && level != CompLevel_full_optimization)  {
 696       double current_reverse_free_ratio = CodeCache::reverse_free_ratio(CodeCache::get_code_blob_type(level));
 697       if (current_reverse_free_ratio > _increase_threshold_at_ratio) {
 698         k *= exp(current_reverse_free_ratio - _increase_threshold_at_ratio);
 699       }
 700     }
 701     return k;
 702   }
 703   return 1;
 704 }
 705 
 706 // Call and loop predicates determine whether a transition to a higher
 707 // compilation level should be performed (pointers to predicate functions
 708 // are passed to common()).
 709 // Tier?LoadFeedback is basically a coefficient that determines of
 710 // how many methods per compiler thread can be in the queue before
 711 // the threshold values double.
 712 bool TieredThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level, const methodHandle& method) {
 713   double k = 1;
 714   switch(cur_level) {
 715   case CompLevel_aot: {
 716     k = CompilationModeFlag::disable_intermediate() ? 1 : threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
 717     break;
 718   }
 719   case CompLevel_none: {
 720     if (CompilationModeFlag::disable_intermediate()) {
 721       k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
 722       break;
 723     }
 724   }
 725   // Fall through
 726   case CompLevel_limited_profile: {
 727     k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
 728     break;
 729   }
 730   case CompLevel_full_profile: {
 731     k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
 732     break;
 733   }
 734   default:
 735     return true;
 736   }
 737   return loop_predicate_helper(method, cur_level, i, b, k);
 738 }
 739 
 740 bool TieredThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level, const methodHandle& method) {
 741   double k = 1;
 742   switch(cur_level) {
 743   case CompLevel_aot: {
 744     k = CompilationModeFlag::disable_intermediate() ? 1 : threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
 745     break;
 746   }
 747   case CompLevel_none: {
 748     if (CompilationModeFlag::disable_intermediate()) {
 749       k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
 750       break;
 751     }
 752   }
 753   // Fall through
 754   case CompLevel_limited_profile: {
 755     k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
 756     break;
 757   }
 758   case CompLevel_full_profile: {
 759     k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
 760     break;
 761   }
 762   default:
 763     return true;
 764   }
 765   return call_predicate_helper(method, cur_level, i, b, k);
 766 }
 767 
 768 // Determine is a method is mature.
 769 bool TieredThresholdPolicy::is_mature(Method* method) {
 770   methodHandle mh(Thread::current(), method);
 771   if (is_trivial(method) || force_comp_at_level_simple(mh)) return true;
 772   MethodData* mdo = method->method_data();
 773   if (mdo != NULL) {
 774     int i = mdo->invocation_count();
 775     int b = mdo->backedge_count();
 776     double k = ProfileMaturityPercentage / 100.0;
 777     CompLevel main_profile_level = CompilationModeFlag::disable_intermediate() ? CompLevel_none : CompLevel_full_profile;
 778     return call_predicate_helper(mh, main_profile_level, i, b, k) || loop_predicate_helper(mh, main_profile_level, i, b, k);
 779   }
 780   return false;
 781 }
 782 
 783 // If a method is old enough and is still in the interpreter we would want to
 784 // start profiling without waiting for the compiled method to arrive.
 785 // We also take the load on compilers into the account.
 786 bool TieredThresholdPolicy::should_create_mdo(const methodHandle& method, CompLevel cur_level) {
 787   if (cur_level != CompLevel_none || force_comp_at_level_simple(method)) {
 788     return false;
 789   }
 790   int i = method->invocation_count();
 791   int b = method->backedge_count();
 792   double k = Tier0ProfilingStartPercentage / 100.0;
 793 
 794   // If the top level compiler is not keeping up, delay profiling.
 795   if (CompileBroker::queue_size(CompLevel_full_optimization) <=  (CompilationModeFlag::disable_intermediate() ? Tier0Delay : Tier3DelayOn) * compiler_count(CompLevel_full_optimization)) {
 796     return call_predicate_helper(method, CompLevel_none, i, b, k) || loop_predicate_helper(method, CompLevel_none, i, b, k);
 797   }
 798   return false;
 799 }
 800 
 801 // Inlining control: if we're compiling a profiled method with C1 and the callee
 802 // is known to have OSRed in a C2 version, don't inline it.
 803 bool TieredThresholdPolicy::should_not_inline(ciEnv* env, ciMethod* callee) {
 804   CompLevel comp_level = (CompLevel)env->comp_level();
 805   if (comp_level == CompLevel_full_profile ||
 806       comp_level == CompLevel_limited_profile) {
 807     return callee->highest_osr_comp_level() == CompLevel_full_optimization;
 808   }
 809   return false;
 810 }
 811 
 812 // Create MDO if necessary.
 813 void TieredThresholdPolicy::create_mdo(const methodHandle& mh, JavaThread* THREAD) {
 814   if (mh->is_native() ||
 815       mh->is_abstract() ||
 816       mh->is_accessor() ||
 817       mh->is_constant_getter()) {
 818     return;
 819   }
 820   if (mh->method_data() == NULL) {
 821     Method::build_interpreter_method_data(mh, CHECK_AND_CLEAR);
 822   }
 823 }
 824 
 825 
 826 /*
 827  * Method states:
 828  *   0 - interpreter (CompLevel_none)
 829  *   1 - pure C1 (CompLevel_simple)
 830  *   2 - C1 with invocation and backedge counting (CompLevel_limited_profile)
 831  *   3 - C1 with full profiling (CompLevel_full_profile)
 832  *   4 - C2 or Graal (CompLevel_full_optimization)
 833  *
 834  * Common state transition patterns:
 835  * a. 0 -> 3 -> 4.
 836  *    The most common path. But note that even in this straightforward case
 837  *    profiling can start at level 0 and finish at level 3.
 838  *
 839  * b. 0 -> 2 -> 3 -> 4.
 840  *    This case occurs when the load on C2 is deemed too high. So, instead of transitioning
 841  *    into state 3 directly and over-profiling while a method is in the C2 queue we transition to
 842  *    level 2 and wait until the load on C2 decreases. This path is disabled for OSRs.
 843  *
 844  * c. 0 -> (3->2) -> 4.
 845  *    In this case we enqueue a method for compilation at level 3, but the C1 queue is long enough
 846  *    to enable the profiling to fully occur at level 0. In this case we change the compilation level
 847  *    of the method to 2 while the request is still in-queue, because it'll allow it to run much faster
 848  *    without full profiling while c2 is compiling.
 849  *
 850  * d. 0 -> 3 -> 1 or 0 -> 2 -> 1.
 851  *    After a method was once compiled with C1 it can be identified as trivial and be compiled to
 852  *    level 1. These transition can also occur if a method can't be compiled with C2 but can with C1.
 853  *
 854  * e. 0 -> 4.
 855  *    This can happen if a method fails C1 compilation (it will still be profiled in the interpreter)
 856  *    or because of a deopt that didn't require reprofiling (compilation won't happen in this case because
 857  *    the compiled version already exists).
 858  *
 859  * Note that since state 0 can be reached from any other state via deoptimization different loops
 860  * are possible.
 861  *
 862  */
 863 
 864 // Common transition function. Given a predicate determines if a method should transition to another level.
 865 CompLevel TieredThresholdPolicy::common(Predicate p, const methodHandle& method, CompLevel cur_level, bool disable_feedback) {
 866   CompLevel next_level = cur_level;
 867   int i = method->invocation_count();
 868   int b = method->backedge_count();
 869 
 870   if (force_comp_at_level_simple(method)) {
 871     next_level = CompLevel_simple;
 872   } else {
 873     if (!CompilationModeFlag::disable_intermediate() && is_trivial(method())) {
 874       next_level = CompLevel_simple;
 875     } else {
 876       switch(cur_level) {
 877       default: break;
 878       case CompLevel_aot:
 879         if (CompilationModeFlag::disable_intermediate()) {
 880           if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
 881                                    Tier0Delay * compiler_count(CompLevel_full_optimization) &&
 882                                   (this->*p)(i, b, cur_level, method))) {
 883             next_level = CompLevel_none;
 884           }
 885         } else {
 886           // If we were at full profile level, would we switch to full opt?
 887           if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) {
 888             next_level = CompLevel_full_optimization;
 889           } else if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
 890                                           Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
 891                                          (this->*p)(i, b, cur_level, method))) {
 892             next_level = CompLevel_full_profile;
 893           }
 894         }
 895         break;
 896       case CompLevel_none:
 897         if (CompilationModeFlag::disable_intermediate()) {
 898           MethodData* mdo = method->method_data();
 899           if (mdo != NULL) {
 900             // If mdo exists that means we are in a normal profiling mode.
 901             int mdo_i = mdo->invocation_count_delta();
 902             int mdo_b = mdo->backedge_count_delta();
 903             if ((this->*p)(mdo_i, mdo_b, cur_level, method)) {
 904               next_level = CompLevel_full_optimization;
 905             }
 906           }
 907         } else {
 908           // If we were at full profile level, would we switch to full opt?
 909           if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) {
 910             next_level = CompLevel_full_optimization;
 911           } else if ((this->*p)(i, b, cur_level, method)) {
 912   #if INCLUDE_JVMCI
 913             if (EnableJVMCI && UseJVMCICompiler) {
 914               // Since JVMCI takes a while to warm up, its queue inevitably backs up during
 915               // early VM execution. As of 2014-06-13, JVMCI's inliner assumes that the root
 916               // compilation method and all potential inlinees have mature profiles (which
 917               // includes type profiling). If it sees immature profiles, JVMCI's inliner
 918               // can perform pathologically bad (e.g., causing OutOfMemoryErrors due to
 919               // exploring/inlining too many graphs). Since a rewrite of the inliner is
 920               // in progress, we simply disable the dialing back heuristic for now and will
 921               // revisit this decision once the new inliner is completed.
 922               next_level = CompLevel_full_profile;
 923             } else
 924   #endif
 925             {
 926               // C1-generated fully profiled code is about 30% slower than the limited profile
 927               // code that has only invocation and backedge counters. The observation is that
 928               // if C2 queue is large enough we can spend too much time in the fully profiled code
 929               // while waiting for C2 to pick the method from the queue. To alleviate this problem
 930               // we introduce a feedback on the C2 queue size. If the C2 queue is sufficiently long
 931               // we choose to compile a limited profiled version and then recompile with full profiling
 932               // when the load on C2 goes down.
 933               if (!disable_feedback && CompileBroker::queue_size(CompLevel_full_optimization) >
 934                   Tier3DelayOn * compiler_count(CompLevel_full_optimization)) {
 935                 next_level = CompLevel_limited_profile;
 936               } else {
 937                 next_level = CompLevel_full_profile;
 938               }
 939             }
 940           }
 941         }
 942         break;
 943       case CompLevel_limited_profile:
 944         if (is_method_profiled(method)) {
 945           // Special case: we got here because this method was fully profiled in the interpreter.
 946           next_level = CompLevel_full_optimization;
 947         } else {
 948           MethodData* mdo = method->method_data();
 949           if (mdo != NULL) {
 950             if (mdo->would_profile()) {
 951               if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
 952                                        Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
 953                                        (this->*p)(i, b, cur_level, method))) {
 954                 next_level = CompLevel_full_profile;
 955               }
 956             } else {
 957               next_level = CompLevel_full_optimization;
 958             }
 959           } else {
 960             // If there is no MDO we need to profile
 961             if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
 962                                      Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
 963                                      (this->*p)(i, b, cur_level, method))) {
 964               next_level = CompLevel_full_profile;
 965             }
 966           }
 967         }
 968         break;
 969       case CompLevel_full_profile:
 970         {
 971           MethodData* mdo = method->method_data();
 972           if (mdo != NULL) {
 973             if (mdo->would_profile()) {
 974               int mdo_i = mdo->invocation_count_delta();
 975               int mdo_b = mdo->backedge_count_delta();
 976               if ((this->*p)(mdo_i, mdo_b, cur_level, method)) {
 977                 next_level = CompLevel_full_optimization;
 978               }
 979             } else {
 980               next_level = CompLevel_full_optimization;
 981             }
 982           }
 983         }
 984         break;
 985       }
 986     }
 987   }
 988   return limit_level(next_level);
 989 }
 990 
 991 
 992 
 993 // Determine if a method should be compiled with a normal entry point at a different level.
 994 CompLevel TieredThresholdPolicy::call_event(const methodHandle& method, CompLevel cur_level, JavaThread* thread) {
 995   CompLevel osr_level = MIN2((CompLevel) method->highest_osr_comp_level(),
 996                              common(&TieredThresholdPolicy::loop_predicate, method, cur_level, true));
 997   CompLevel next_level = common(&TieredThresholdPolicy::call_predicate, method, cur_level);
 998 
 999   // If OSR method level is greater than the regular method level, the levels should be
1000   // equalized by raising the regular method level in order to avoid OSRs during each
1001   // invocation of the method.
1002   if (osr_level == CompLevel_full_optimization && cur_level == CompLevel_full_profile) {
1003     MethodData* mdo = method->method_data();
1004     guarantee(mdo != NULL, "MDO should not be NULL");
1005     if (mdo->invocation_count() >= 1) {
1006       next_level = CompLevel_full_optimization;
1007     }
1008   } else {
1009     next_level = MAX2(osr_level, next_level);
1010   }
1011   return next_level;
1012 }
1013 
1014 // Determine if we should do an OSR compilation of a given method.
1015 CompLevel TieredThresholdPolicy::loop_event(const methodHandle& method, CompLevel cur_level, JavaThread* thread) {
1016   CompLevel next_level = common(&TieredThresholdPolicy::loop_predicate, method, cur_level, true);
1017   if (cur_level == CompLevel_none) {
1018     // If there is a live OSR method that means that we deopted to the interpreter
1019     // for the transition.
1020     CompLevel osr_level = MIN2((CompLevel)method->highest_osr_comp_level(), next_level);
1021     if (osr_level > CompLevel_none) {
1022       return osr_level;
1023     }
1024   }
1025   return next_level;
1026 }
1027 
1028 bool TieredThresholdPolicy::maybe_switch_to_aot(const methodHandle& mh, CompLevel cur_level, CompLevel next_level, JavaThread* thread) {
1029   if (UseAOT) {
1030     if (cur_level == CompLevel_full_profile || cur_level == CompLevel_none) {
1031       // If the current level is full profile or interpreter and we're switching to any other level,
1032       // activate the AOT code back first so that we won't waste time overprofiling.
1033       compile(mh, InvocationEntryBci, CompLevel_aot, thread);
1034       // Fall through for JIT compilation.
1035     }
1036     if (next_level == CompLevel_limited_profile && cur_level != CompLevel_aot && mh->has_aot_code()) {
1037       // If the next level is limited profile, use the aot code (if there is any),
1038       // since it's essentially the same thing.
1039       compile(mh, InvocationEntryBci, CompLevel_aot, thread);
1040       // Not need to JIT, we're done.
1041       return true;
1042     }
1043   }
1044   return false;
1045 }
1046 
1047 
1048 // Handle the invocation event.
1049 void TieredThresholdPolicy::method_invocation_event(const methodHandle& mh, const methodHandle& imh,
1050                                                       CompLevel level, CompiledMethod* nm, JavaThread* thread) {
1051   if (should_create_mdo(mh, level)) {
1052     create_mdo(mh, thread);
1053   }
1054   CompLevel next_level = call_event(mh, level, thread);
1055   if (next_level != level) {
1056     if (maybe_switch_to_aot(mh, level, next_level, thread)) {
1057       // No JITting necessary
1058       return;
1059     }
1060     if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh)) {
1061       compile(mh, InvocationEntryBci, next_level, thread);
1062     }
1063   }
1064 }
1065 
1066 // Handle the back branch event. Notice that we can compile the method
1067 // with a regular entry from here.
1068 void TieredThresholdPolicy::method_back_branch_event(const methodHandle& mh, const methodHandle& imh,
1069                                                      int bci, CompLevel level, CompiledMethod* nm, JavaThread* thread) {
1070   if (should_create_mdo(mh, level)) {
1071     create_mdo(mh, thread);
1072   }
1073   // Check if MDO should be created for the inlined method
1074   if (should_create_mdo(imh, level)) {
1075     create_mdo(imh, thread);
1076   }
1077 
1078   if (is_compilation_enabled()) {
1079     CompLevel next_osr_level = loop_event(imh, level, thread);
1080     CompLevel max_osr_level = (CompLevel)imh->highest_osr_comp_level();
1081     // At the very least compile the OSR version
1082     if (!CompileBroker::compilation_is_in_queue(imh) && (next_osr_level != level)) {
1083       compile(imh, bci, next_osr_level, thread);
1084     }
1085 
1086     // Use loop event as an opportunity to also check if there's been
1087     // enough calls.
1088     CompLevel cur_level, next_level;
1089     if (mh() != imh()) { // If there is an enclosing method
1090       if (level == CompLevel_aot) {
1091         // Recompile the enclosing method to prevent infinite OSRs. Stay at AOT level while it's compiling.
1092         if (max_osr_level != CompLevel_none && !CompileBroker::compilation_is_in_queue(mh)) {
1093           CompLevel enclosing_level = limit_level(CompLevel_full_profile);
1094           compile(mh, InvocationEntryBci, enclosing_level, thread);
1095         }
1096       } else {
1097         // Current loop event level is not AOT
1098         guarantee(nm != NULL, "Should have nmethod here");
1099         cur_level = comp_level(mh());
1100         next_level = call_event(mh, cur_level, thread);
1101 
1102         if (max_osr_level == CompLevel_full_optimization) {
1103           // The inlinee OSRed to full opt, we need to modify the enclosing method to avoid deopts
1104           bool make_not_entrant = false;
1105           if (nm->is_osr_method()) {
1106             // This is an osr method, just make it not entrant and recompile later if needed
1107             make_not_entrant = true;
1108           } else {
1109             if (next_level != CompLevel_full_optimization) {
1110               // next_level is not full opt, so we need to recompile the
1111               // enclosing method without the inlinee
1112               cur_level = CompLevel_none;
1113               make_not_entrant = true;
1114             }
1115           }
1116           if (make_not_entrant) {
1117             if (PrintTieredEvents) {
1118               int osr_bci = nm->is_osr_method() ? nm->osr_entry_bci() : InvocationEntryBci;
1119               print_event(MAKE_NOT_ENTRANT, mh(), mh(), osr_bci, level);
1120             }
1121             nm->make_not_entrant();
1122           }
1123         }
1124         // Fix up next_level if necessary to avoid deopts
1125         if (next_level == CompLevel_limited_profile && max_osr_level == CompLevel_full_profile) {
1126           next_level = CompLevel_full_profile;
1127         }
1128         if (cur_level != next_level) {
1129           if (!maybe_switch_to_aot(mh, cur_level, next_level, thread) && !CompileBroker::compilation_is_in_queue(mh)) {
1130             compile(mh, InvocationEntryBci, next_level, thread);
1131           }
1132         }
1133       }
1134     } else {
1135       cur_level = comp_level(mh());
1136       next_level = call_event(mh, cur_level, thread);
1137       if (next_level != cur_level) {
1138         if (!maybe_switch_to_aot(mh, cur_level, next_level, thread) && !CompileBroker::compilation_is_in_queue(mh)) {
1139           compile(mh, InvocationEntryBci, next_level, thread);
1140         }
1141       }
1142     }
1143   }
1144 }
1145 
1146 #endif