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