< prev index next >

src/hotspot/share/compiler/compilationPolicy.cpp

Print this page

   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 "cds/aotLinkedClassBulkLoader.hpp"

  26 #include "code/scopeDesc.hpp"
  27 #include "compiler/compilationPolicy.hpp"
  28 #include "compiler/compileBroker.hpp"
  29 #include "compiler/compilerDefinitions.inline.hpp"
  30 #include "compiler/compilerOracle.hpp"

  31 #include "memory/resourceArea.hpp"
  32 #include "oops/method.inline.hpp"
  33 #include "oops/methodData.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "oops/trainingData.hpp"
  36 #include "prims/jvmtiExport.hpp"
  37 #include "runtime/arguments.hpp"
  38 #include "runtime/deoptimization.hpp"
  39 #include "runtime/frame.hpp"
  40 #include "runtime/frame.inline.hpp"
  41 #include "runtime/globals_extension.hpp"
  42 #include "runtime/handles.inline.hpp"
  43 #include "runtime/safepoint.hpp"
  44 #include "runtime/safepointVerifiers.hpp"
  45 #ifdef COMPILER1
  46 #include "c1/c1_Compiler.hpp"
  47 #endif
  48 #ifdef COMPILER2
  49 #include "opto/c2compiler.hpp"
  50 #endif
  51 #if INCLUDE_JVMCI
  52 #include "jvmci/jvmci.hpp"
  53 #endif
  54 
  55 int64_t CompilationPolicy::_start_time = 0;
  56 int CompilationPolicy::_c1_count = 0;
  57 int CompilationPolicy::_c2_count = 0;

  58 double CompilationPolicy::_increase_threshold_at_ratio = 0;
  59 
  60 CompilationPolicy::TrainingReplayQueue CompilationPolicy::_training_replay_queue;
  61 
  62 void compilationPolicy_init() {
  63   CompilationPolicy::initialize();
  64 }
  65 
  66 int CompilationPolicy::compiler_count(CompLevel comp_level) {
  67   if (is_c1_compile(comp_level)) {
  68     return c1_count();
  69   } else if (is_c2_compile(comp_level)) {
  70     return c2_count();
  71   }
  72   return 0;
  73 }
  74 
  75 // Returns true if m must be compiled before executing it
  76 // This is intended to force compiles for methods (usually for
  77 // debugging) that would otherwise be interpreted for some reason.
  78 bool CompilationPolicy::must_be_compiled(const methodHandle& m, int comp_level) {
  79   // Don't allow Xcomp to cause compiles in replay mode
  80   if (ReplayCompiles) return false;
  81 
  82   if (m->has_compiled_code()) return false;       // already compiled
  83   if (!can_be_compiled(m, comp_level)) return false;
  84 
  85   return !UseInterpreter ||                                                                        // must compile all methods
  86          (AlwaysCompileLoopMethods && m->has_loops() && CompileBroker::should_compile_new_jobs()); // eagerly compile loop methods
  87 }
  88 
  89 void CompilationPolicy::maybe_compile_early(const methodHandle& m, TRAPS) {
  90   if (m->method_holder()->is_not_initialized()) {
  91     // 'is_not_initialized' means not only '!is_initialized', but also that
  92     // initialization has not been started yet ('!being_initialized')
  93     // Do not force compilation of methods in uninitialized classes.
  94     return;
  95   }
  96   if (!m->is_native() && MethodTrainingData::have_data()) {
  97     MethodTrainingData* mtd = MethodTrainingData::find_fast(m);
  98     if (mtd == nullptr) {
  99       return;              // there is no training data recorded for m
 100     }



 101     CompLevel cur_level = static_cast<CompLevel>(m->highest_comp_level());
 102     CompLevel next_level = trained_transition(m, cur_level, mtd, THREAD);
 103     if (next_level != cur_level && can_be_compiled(m, next_level) && !CompileBroker::compilation_is_in_queue(m)) {
















 104       if (PrintTieredEvents) {
 105         print_event(FORCE_COMPILE, m(), m(), InvocationEntryBci, next_level);
 106       }
 107       CompileBroker::compile_method(m, InvocationEntryBci, next_level, 0, CompileTask::Reason_MustBeCompiled, THREAD);
 108       if (HAS_PENDING_EXCEPTION) {
 109         CLEAR_PENDING_EXCEPTION;
 110       }
 111     }
 112   }
 113 }
 114 
 115 void CompilationPolicy::compile_if_required(const methodHandle& m, TRAPS) {
 116   if (!THREAD->can_call_java() || THREAD->is_Compiler_thread()) {
 117     // don't force compilation, resolve was on behalf of compiler
 118     return;
 119   }
 120   if (m->method_holder()->is_not_initialized()) {
 121     // 'is_not_initialized' means not only '!is_initialized', but also that
 122     // initialization has not been started yet ('!being_initialized')
 123     // Do not force compilation of methods in uninitialized classes.
 124     // Note that doing this would throw an assert later,
 125     // in CompileBroker::compile_method.
 126     // We sometimes use the link resolver to do reflective lookups
 127     // even before classes are initialized.
 128     return;
 129   }
 130 
 131   if (must_be_compiled(m)) {
 132     // This path is unusual, mostly used by the '-Xcomp' stress test mode.
 133     CompLevel level = initial_compile_level(m);
 134     if (PrintTieredEvents) {
 135       print_event(FORCE_COMPILE, m(), m(), InvocationEntryBci, level);
 136     }
 137     CompileBroker::compile_method(m, InvocationEntryBci, level, 0, CompileTask::Reason_MustBeCompiled, THREAD);











 138   }
 139 }
 140 
 141 void CompilationPolicy::replay_training_at_init_impl(InstanceKlass* klass, JavaThread* current) {
 142   if (!klass->has_init_deps_processed()) {
 143     ResourceMark rm;
 144     log_debug(training)("Replay training: %s", klass->external_name());
 145 
 146     KlassTrainingData* ktd = KlassTrainingData::find(klass);
 147     if (ktd != nullptr) {
 148       guarantee(ktd->has_holder(), "");
 149       ktd->notice_fully_initialized(); // sets klass->has_init_deps_processed bit
 150       assert(klass->has_init_deps_processed(), "");

 151       if (AOTCompileEagerly) {

 152         ktd->iterate_comp_deps([&](CompileTrainingData* ctd) {
 153           if (ctd->init_deps_left_acquire() == 0) {
 154             MethodTrainingData* mtd = ctd->method();
 155             if (mtd->has_holder()) {
 156               const methodHandle mh(current, const_cast<Method*>(mtd->holder()));
 157               CompilationPolicy::maybe_compile_early(mh, current);
 158             }
 159           }
 160         });





 161       }
 162     }
 163   }
 164 }
 165 
 166 void CompilationPolicy::replay_training_at_init(InstanceKlass* klass, JavaThread* current) {
 167   assert(klass->is_initialized(), "");
 168   if (TrainingData::have_data() && klass->in_aot_cache()) {
 169     _training_replay_queue.push(klass, TrainingReplayQueue_lock, current);
 170   }
 171 }
 172 
 173 // For TrainingReplayQueue
 174 template<>
 175 void CompilationPolicyUtils::Queue<InstanceKlass>::print_on(outputStream* st) {
 176   int pos = 0;
 177   for (QueueNode* cur = _head; cur != nullptr; cur = cur->next()) {
 178     ResourceMark rm;
 179     InstanceKlass* ik = cur->value();
 180     st->print_cr("%3d: " INTPTR_FORMAT " %s", ++pos, p2i(ik), ik->external_name());

 456 
 457 // Print an event.
 458 void CompilationPolicy::print_event_on(outputStream *st, EventType type, Method* m, Method* im, int bci, CompLevel level) {
 459   bool inlinee_event = m != im;
 460 
 461   st->print("%lf: [", os::elapsedTime());
 462 
 463   switch(type) {
 464   case CALL:
 465     st->print("call");
 466     break;
 467   case LOOP:
 468     st->print("loop");
 469     break;
 470   case COMPILE:
 471     st->print("compile");
 472     break;
 473   case FORCE_COMPILE:
 474     st->print("force-compile");
 475     break;



 476   case REMOVE_FROM_QUEUE:
 477     st->print("remove-from-queue");
 478     break;
 479   case UPDATE_IN_QUEUE:
 480     st->print("update-in-queue");
 481     break;
 482   case REPROFILE:
 483     st->print("reprofile");
 484     break;
 485   case MAKE_NOT_ENTRANT:
 486     st->print("make-not-entrant");
 487     break;
 488   default:
 489     st->print("unknown");
 490   }
 491 
 492   st->print(" level=%d ", level);
 493 
 494   ResourceMark rm;
 495   char *method_name = m->name_and_sig_as_C_string();
 496   st->print("[%s", method_name);
 497   if (inlinee_event) {
 498     char *inlinee_name = im->name_and_sig_as_C_string();
 499     st->print(" [%s]] ", inlinee_name);
 500   }
 501   else st->print("] ");
 502   st->print("@%d queues=%d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile),
 503                                      CompileBroker::queue_size(CompLevel_full_optimization));
 504 
 505   st->print(" rate=");
 506   if (m->prev_time() == 0) st->print("n/a");
 507   else st->print("%f", m->rate());
 508 


 509   st->print(" k=%.2lf,%.2lf", threshold_scale(CompLevel_full_profile, Tier3LoadFeedback),
 510                               threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback));
 511 
 512   if (type != COMPILE) {
 513     print_counters_on(st, "", m);
 514     if (inlinee_event) {
 515       print_counters_on(st, "inlinee ", im);
 516     }
 517     st->print(" compilable=");
 518     bool need_comma = false;
 519     if (!m->is_not_compilable(CompLevel_full_profile)) {
 520       st->print("c1");
 521       need_comma = true;
 522     }
 523     if (!m->is_not_osr_compilable(CompLevel_full_profile)) {
 524       if (need_comma) st->print(",");
 525       st->print("c1-osr");
 526       need_comma = true;
 527     }
 528     if (!m->is_not_compilable(CompLevel_full_optimization)) {

 547   st->print_cr("]");
 548 
 549 }
 550 
 551 void CompilationPolicy::print_event(EventType type, Method* m, Method* im, int bci, CompLevel level) {
 552   stringStream s;
 553   print_event_on(&s, type, m, im, bci, level);
 554   ResourceMark rm;
 555   tty->print("%s", s.as_string());
 556 }
 557 
 558 void CompilationPolicy::initialize() {
 559   if (!CompilerConfig::is_interpreter_only()) {
 560     int count = CICompilerCount;
 561     bool c1_only = CompilerConfig::is_c1_only();
 562     bool c2_only = CompilerConfig::is_c2_or_jvmci_compiler_only();
 563     int min_count = (c1_only || c2_only) ? 1 : 2;
 564 
 565 #ifdef _LP64
 566     // Turn on ergonomic compiler count selection










 567     if (FLAG_IS_DEFAULT(CICompilerCountPerCPU) && FLAG_IS_DEFAULT(CICompilerCount)) {
 568       FLAG_SET_DEFAULT(CICompilerCountPerCPU, true);
 569     }
 570     if (CICompilerCountPerCPU) {
 571       // Simple log n seems to grow too slowly for tiered, try something faster: log n * log log n
 572       int log_cpu = log2i(os::active_processor_count());
 573       int loglog_cpu = log2i(MAX2(log_cpu, 1));
 574       count = MAX2(log_cpu * loglog_cpu * 3 / 2, min_count);


 575       // Make sure there is enough space in the code cache to hold all the compiler buffers
 576       size_t c1_size = 0;
 577 #ifdef COMPILER1
 578       c1_size = Compiler::code_buffer_size();
 579 #endif
 580       size_t c2_size = 0;
 581 #ifdef COMPILER2
 582       c2_size = C2Compiler::initial_code_buffer_size();
 583 #endif
 584       size_t buffer_size = 0;
 585       if (c1_only) {
 586         buffer_size = c1_size;
 587       } else if (c2_only) {
 588         buffer_size = c2_size;
 589       } else {
 590         buffer_size = c1_size / 3 + 2 * c2_size / 3;
 591       }
 592       size_t max_count = (NonNMethodCodeHeapSize - (CodeCacheMinimumUseSpace DEBUG_ONLY(* 3))) / buffer_size;
 593       if ((size_t)count > max_count) {
 594         // Lower the compiler count such that all buffers fit into the code cache

 613     if (c1_only) {
 614       // No C2 compiler threads are needed
 615       set_c1_count(count);
 616     } else if (c2_only) {
 617       // No C1 compiler threads are needed
 618       set_c2_count(count);
 619     } else {
 620 #if INCLUDE_JVMCI
 621       if (UseJVMCICompiler && UseJVMCINativeLibrary) {
 622         int libjvmci_count = MAX2((int) (count * JVMCINativeLibraryThreadFraction), 1);
 623         int c1_count = MAX2(count - libjvmci_count, 1);
 624         set_c2_count(libjvmci_count);
 625         set_c1_count(c1_count);
 626       } else
 627 #endif
 628       {
 629         set_c1_count(MAX2(count / 3, 1));
 630         set_c2_count(MAX2(count - c1_count(), 1));
 631       }
 632     }



 633     assert(count == c1_count() + c2_count(), "inconsistent compiler thread count");
 634     set_increase_threshold_at_ratio();
 635   } else {
 636     // Interpreter mode creates no compilers
 637     FLAG_SET_ERGO(CICompilerCount, 0);
 638   }
 639   set_start_time(nanos_to_millis(os::javaTimeNanos()));
 640 }
 641 
 642 
 643 #ifdef ASSERT
 644 bool CompilationPolicy::verify_level(CompLevel level) {
 645   if (TieredCompilation && level > TieredStopAtLevel) {
 646     return false;
 647   }
 648   // Check if there is a compiler to process the requested level
 649   if (!CompilerConfig::is_c1_enabled() && is_c1_compile(level)) {
 650     return false;
 651   }
 652   if (!CompilerConfig::is_c2_or_jvmci_compiler_enabled() && is_c2_compile(level)) {

 755   }
 756 }
 757 
 758 // Called with the queue locked and with at least one element
 759 CompileTask* CompilationPolicy::select_task(CompileQueue* compile_queue, JavaThread* THREAD) {
 760   CompileTask *max_blocking_task = nullptr;
 761   CompileTask *max_task = nullptr;
 762   Method* max_method = nullptr;
 763 
 764   int64_t t = nanos_to_millis(os::javaTimeNanos());
 765   // Iterate through the queue and find a method with a maximum rate.
 766   for (CompileTask* task = compile_queue->first(); task != nullptr;) {
 767     CompileTask* next_task = task->next();
 768     // If a method was unloaded or has been stale for some time, remove it from the queue.
 769     // Blocking tasks and tasks submitted from whitebox API don't become stale
 770     if (task->is_unloaded()) {
 771       compile_queue->remove_and_mark_stale(task);
 772       task = next_task;
 773       continue;
 774     }





 775     if (task->is_blocking() && task->compile_reason() == CompileTask::Reason_Whitebox) {
 776       // CTW tasks, submitted as blocking Whitebox requests, do not participate in rate
 777       // selection and/or any level adjustments. Just return them in order.
 778       return task;
 779     }
 780     Method* method = task->method();
 781     methodHandle mh(THREAD, method);
 782     if (task->can_become_stale() && is_stale(t, TieredCompileTaskTimeout, mh) && !is_old(mh)) {
 783       if (PrintTieredEvents) {
 784         print_event(REMOVE_FROM_QUEUE, method, method, task->osr_bci(), (CompLevel) task->comp_level());
 785       }
 786       method->clear_queued_for_compilation();

 787       compile_queue->remove_and_mark_stale(task);
 788       task = next_task;
 789       continue;
 790     }
 791     update_rate(t, mh);
 792     if (max_task == nullptr || compare_methods(method, max_method)) {
 793       // Select a method with the highest rate
 794       max_task = task;
 795       max_method = method;
 796     }
 797 
 798     if (task->is_blocking()) {
 799       if (max_blocking_task == nullptr || compare_methods(method, max_blocking_task->method())) {
 800         max_blocking_task = task;
 801       }
 802     }
 803 
 804     task = next_task;
 805   }
 806 
 807   if (max_blocking_task != nullptr) {
 808     // In blocking compilation mode, the CompileBroker will make
 809     // compilations submitted by a JVMCI compiler thread non-blocking. These
 810     // compilations should be scheduled after all blocking compilations
 811     // to service non-compiler related compilations sooner and reduce the
 812     // chance of such compilations timing out.
 813     max_task = max_blocking_task;
 814     max_method = max_task->method();
 815   }
 816 
 817   methodHandle max_method_h(THREAD, max_method);
 818 
 819   if (max_task != nullptr && max_task->comp_level() == CompLevel_full_profile && TieredStopAtLevel > CompLevel_full_profile &&
 820       max_method != nullptr && is_method_profiled(max_method_h) && !Arguments::is_compiler_only()) {
 821     max_task->set_comp_level(CompLevel_limited_profile);
 822 
 823     if (CompileBroker::compilation_is_complete(max_method_h, max_task->osr_bci(), CompLevel_limited_profile)) {


 824       if (PrintTieredEvents) {
 825         print_event(REMOVE_FROM_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level());
 826       }
 827       compile_queue->remove_and_mark_stale(max_task);
 828       max_method->clear_queued_for_compilation();
 829       return nullptr;
 830     }
 831 
 832     if (PrintTieredEvents) {
 833       print_event(UPDATE_IN_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level());
 834     }
 835   }

 836   return max_task;
 837 }
 838 
 839 void CompilationPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
 840   for (ScopeDesc* sd = trap_scope;; sd = sd->sender()) {
 841     if (PrintTieredEvents) {
 842       print_event(REPROFILE, sd->method(), sd->method(), InvocationEntryBci, CompLevel_none);
 843     }
 844     MethodData* mdo = sd->method()->method_data();
 845     if (mdo != nullptr) {
 846       mdo->reset_start_counters();
 847     }
 848     if (sd->is_top()) break;
 849   }
 850 }
 851 
 852 nmethod* CompilationPolicy::event(const methodHandle& method, const methodHandle& inlinee,
 853                                       int branch_bci, int bci, CompLevel comp_level, nmethod* nm, TRAPS) {
 854   if (PrintTieredEvents) {
 855     print_event(bci == InvocationEntryBci ? CALL : LOOP, method(), inlinee(), bci, comp_level);

 934     if (!CompilationModeFlag::disable_intermediate() &&
 935         level == CompLevel_full_optimization && can_be_osr_compiled(mh, CompLevel_simple)) {
 936       nmethod* osr_nm = mh->lookup_osr_nmethod_for(bci, CompLevel_simple, false);
 937       if (osr_nm != nullptr && osr_nm->comp_level() > CompLevel_simple) {
 938         // Invalidate the existing OSR nmethod so that a compile at CompLevel_simple is permitted.
 939         osr_nm->make_not_entrant(nmethod::InvalidationReason::OSR_INVALIDATION_FOR_COMPILING_WITH_C1);
 940       }
 941       compile(mh, bci, CompLevel_simple, THREAD);
 942     }
 943     return;
 944   }
 945   if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) {
 946     return;
 947   }
 948   if (!CompileBroker::compilation_is_in_queue(mh)) {
 949     if (PrintTieredEvents) {
 950       print_event(COMPILE, mh(), mh(), bci, level);
 951     }
 952     int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
 953     update_rate(nanos_to_millis(os::javaTimeNanos()), mh);
 954     CompileBroker::compile_method(mh, bci, level, hot_count, CompileTask::Reason_Tiered, THREAD);










 955   }
 956 }
 957 
 958 // update_rate() is called from select_task() while holding a compile queue lock.
 959 void CompilationPolicy::update_rate(int64_t t, const methodHandle& method) {
 960   // Skip update if counters are absent.
 961   // Can't allocate them since we are holding compile queue lock.
 962   if (method->method_counters() == nullptr)  return;
 963 
 964   if (is_old(method)) {
 965     // We don't remove old methods from the queue,
 966     // so we can just zero the rate.
 967     method->set_rate(0);
 968     return;
 969   }
 970 
 971   // We don't update the rate if we've just came out of a safepoint.
 972   // delta_s is the time since last safepoint in milliseconds.
 973   int64_t delta_s = t - SafepointTracing::end_of_last_safepoint_ms();
 974   int64_t delta_t = t - (method->prev_time() != 0 ? method->prev_time() : start_time()); // milliseconds since the last measurement

1017 }
1018 
1019 double CompilationPolicy::weight(Method* method) {
1020   return (double)(method->rate() + 1) * (method->invocation_count() + 1) * (method->backedge_count() + 1);
1021 }
1022 
1023 // Apply heuristics and return true if x should be compiled before y
1024 bool CompilationPolicy::compare_methods(Method* x, Method* y) {
1025   if (x->highest_comp_level() > y->highest_comp_level()) {
1026     // recompilation after deopt
1027     return true;
1028   } else
1029     if (x->highest_comp_level() == y->highest_comp_level()) {
1030       if (weight(x) > weight(y)) {
1031         return true;
1032       }
1033     }
1034   return false;
1035 }
1036 








1037 // Is method profiled enough?
1038 bool CompilationPolicy::is_method_profiled(const methodHandle& method) {
1039   MethodData* mdo = method->method_data();
1040   if (mdo != nullptr) {
1041     int i = mdo->invocation_count_delta();
1042     int b = mdo->backedge_count_delta();
1043     return CallPredicate::apply_scaled(method, CompLevel_full_profile, i, b, 1);
1044   }
1045   return false;
1046 }
1047 
1048 
1049 // Determine is a method is mature.
1050 bool CompilationPolicy::is_mature(MethodData* mdo) {
1051   if (Arguments::is_compiler_only()) {
1052     // Always report profiles as immature with -Xcomp
1053     return false;
1054   }
1055   methodHandle mh(Thread::current(), mdo->method());
1056   if (mdo != nullptr) {

1063 }
1064 
1065 // If a method is old enough and is still in the interpreter we would want to
1066 // start profiling without waiting for the compiled method to arrive.
1067 // We also take the load on compilers into the account.
1068 bool CompilationPolicy::should_create_mdo(const methodHandle& method, CompLevel cur_level) {
1069   if (cur_level != CompLevel_none || force_comp_at_level_simple(method) || CompilationModeFlag::quick_only() || !ProfileInterpreter) {
1070     return false;
1071   }
1072 
1073   if (TrainingData::have_data()) {
1074     MethodTrainingData* mtd = MethodTrainingData::find_fast(method);
1075     if (mtd != nullptr && mtd->saw_level(CompLevel_full_optimization)) {
1076       return true;
1077     }
1078   }
1079 
1080   if (is_old(method)) {
1081     return true;
1082   }
1083 
1084   int i = method->invocation_count();
1085   int b = method->backedge_count();
1086   double k = Tier0ProfilingStartPercentage / 100.0;
1087 
1088   // If the top level compiler is not keeping up, delay profiling.
1089   if (CompileBroker::queue_size(CompLevel_full_optimization) <= Tier0Delay * compiler_count(CompLevel_full_optimization)) {
1090     return CallPredicate::apply_scaled(method, CompLevel_none, i, b, k) || LoopPredicate::apply_scaled(method, CompLevel_none, i, b, k);
1091   }
1092   return false;
1093 }
1094 
1095 // Inlining control: if we're compiling a profiled method with C1 and the callee
1096 // is known to have OSRed in a C2 version, don't inline it.
1097 bool CompilationPolicy::should_not_inline(ciEnv* env, ciMethod* callee) {
1098   CompLevel comp_level = (CompLevel)env->comp_level();
1099   if (comp_level == CompLevel_full_profile ||
1100       comp_level == CompLevel_limited_profile) {
1101     return callee->highest_osr_comp_level() == CompLevel_full_optimization;
1102   }
1103   return false;

   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 "cds/aotLinkedClassBulkLoader.hpp"
  26 #include "code/aotCodeCache.hpp"
  27 #include "code/scopeDesc.hpp"
  28 #include "compiler/compilationPolicy.hpp"
  29 #include "compiler/compileBroker.hpp"
  30 #include "compiler/compilerDefinitions.inline.hpp"
  31 #include "compiler/compilerOracle.hpp"
  32 #include "compiler/recompilationPolicy.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/method.inline.hpp"
  35 #include "oops/methodData.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "oops/trainingData.hpp"
  38 #include "prims/jvmtiExport.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/deoptimization.hpp"
  41 #include "runtime/frame.hpp"
  42 #include "runtime/frame.inline.hpp"
  43 #include "runtime/globals_extension.hpp"
  44 #include "runtime/handles.inline.hpp"
  45 #include "runtime/safepoint.hpp"
  46 #include "runtime/safepointVerifiers.hpp"
  47 #ifdef COMPILER1
  48 #include "c1/c1_Compiler.hpp"
  49 #endif
  50 #ifdef COMPILER2
  51 #include "opto/c2compiler.hpp"
  52 #endif
  53 #if INCLUDE_JVMCI
  54 #include "jvmci/jvmci.hpp"
  55 #endif
  56 
  57 int64_t CompilationPolicy::_start_time = 0;
  58 int CompilationPolicy::_c1_count = 0;
  59 int CompilationPolicy::_c2_count = 0;
  60 int CompilationPolicy::_ac_count = 0;
  61 double CompilationPolicy::_increase_threshold_at_ratio = 0;
  62 
  63 CompilationPolicy::TrainingReplayQueue CompilationPolicy::_training_replay_queue;
  64 
  65 void compilationPolicy_init() {
  66   CompilationPolicy::initialize();
  67 }
  68 
  69 int CompilationPolicy::compiler_count(CompLevel comp_level) {
  70   if (is_c1_compile(comp_level)) {
  71     return c1_count();
  72   } else if (is_c2_compile(comp_level)) {
  73     return c2_count();
  74   }
  75   return 0;
  76 }
  77 
  78 // Returns true if m must be compiled before executing it
  79 // This is intended to force compiles for methods (usually for
  80 // debugging) that would otherwise be interpreted for some reason.
  81 bool CompilationPolicy::must_be_compiled(const methodHandle& m, int comp_level) {
  82   // Don't allow Xcomp to cause compiles in replay mode
  83   if (ReplayCompiles) return false;
  84 
  85   if (m->has_compiled_code()) return false;       // already compiled
  86   if (!can_be_compiled(m, comp_level)) return false;
  87 
  88   return !UseInterpreter ||                                              // must compile all methods
  89          (AlwaysCompileLoopMethods && m->has_loops() && CompileBroker::should_compile_new_jobs()); // eagerly compile loop methods
  90 }
  91 
  92 void CompilationPolicy::maybe_compile_early(const methodHandle& m, TRAPS) {
  93   if (m->method_holder()->is_not_initialized()) {
  94     // 'is_not_initialized' means not only '!is_initialized', but also that
  95     // initialization has not been started yet ('!being_initialized')
  96     // Do not force compilation of methods in uninitialized classes.
  97     return;
  98   }
  99   if (!m->is_native() && MethodTrainingData::have_data()) {
 100     MethodTrainingData* mtd = MethodTrainingData::find_fast(m);
 101     if (mtd == nullptr) {
 102       return;              // there is no training data recorded for m
 103     }
 104     // Consider replacing conservatively compiled AOT Preload code with faster AOT code
 105     nmethod* nm = m->code();
 106     bool recompile = (nm != nullptr) && nm->preloaded();
 107     CompLevel cur_level = static_cast<CompLevel>(m->highest_comp_level());
 108     CompLevel next_level = trained_transition(m, cur_level, mtd, THREAD);
 109     if ((next_level != cur_level || recompile) && can_be_compiled(m, next_level) && !CompileBroker::compilation_is_in_queue(m)) {
 110       bool requires_online_compilation = false;
 111       CompileTrainingData* ctd = mtd->last_toplevel_compile(next_level);
 112       if (ctd != nullptr) {
 113         // Can't load normal AOT code - not all dependancies are ready,
 114         // request normal compilation
 115         requires_online_compilation = (ctd->init_deps_left_acquire() > 0);
 116       }
 117       if (requires_online_compilation && recompile) {
 118         // Wait when dependencies are ready to load normal AOT code
 119         // if AOT Preload code is used now.
 120         //
 121         // FIXME. We may never (or it take long time) get all dependencies
 122         // be ready to replace AOT Preload code. Consider using time and how many
 123         // dependencies left to allow normal JIT compilation for replacement.
 124         return;
 125       }
 126       if (PrintTieredEvents) {
 127         print_event(FORCE_COMPILE, m(), m(), InvocationEntryBci, next_level);
 128       }
 129       CompileBroker::compile_method(m, InvocationEntryBci, next_level, 0, requires_online_compilation, CompileTask::Reason_MustBeCompiled, THREAD);
 130       if (HAS_PENDING_EXCEPTION) {
 131         CLEAR_PENDING_EXCEPTION;
 132       }
 133     }
 134   }
 135 }
 136 
 137 void CompilationPolicy::compile_if_required(const methodHandle& m, TRAPS) {
 138   if (!THREAD->can_call_java() || THREAD->is_Compiler_thread()) {
 139     // don't force compilation, resolve was on behalf of compiler
 140     return;
 141   }
 142   if (m->method_holder()->is_not_initialized()) {
 143     // 'is_not_initialized' means not only '!is_initialized', but also that
 144     // initialization has not been started yet ('!being_initialized')
 145     // Do not force compilation of methods in uninitialized classes.
 146     // Note that doing this would throw an assert later,
 147     // in CompileBroker::compile_method.
 148     // We sometimes use the link resolver to do reflective lookups
 149     // even before classes are initialized.
 150     return;
 151   }
 152 
 153   if (must_be_compiled(m)) {
 154     // This path is unusual, mostly used by the '-Xcomp' stress test mode.
 155     CompLevel level = initial_compile_level(m);
 156     if (PrintTieredEvents) {
 157       print_event(FORCE_COMPILE, m(), m(), InvocationEntryBci, level);
 158     }
 159     // Test AOT code too
 160     bool requires_online_compilation = false;
 161     if (TrainingData::have_data()) {
 162       MethodTrainingData* mtd = MethodTrainingData::find_fast(m);
 163       if (mtd != nullptr) {
 164         CompileTrainingData* ctd = mtd->last_toplevel_compile(level);
 165         if (ctd != nullptr) {
 166           requires_online_compilation = (ctd->init_deps_left_acquire() > 0);
 167         }
 168       }
 169     }
 170     CompileBroker::compile_method(m, InvocationEntryBci, level, 0, requires_online_compilation, CompileTask::Reason_MustBeCompiled, THREAD);
 171   }
 172 }
 173 
 174 void CompilationPolicy::replay_training_at_init_impl(InstanceKlass* klass, JavaThread* current) {
 175   if (!klass->has_init_deps_processed()) {
 176     ResourceMark rm;
 177     log_debug(training)("Replay training: %s", klass->external_name());
 178 
 179     KlassTrainingData* ktd = KlassTrainingData::find(klass);
 180     if (ktd != nullptr) {
 181       guarantee(ktd->has_holder(), "");
 182       ktd->notice_fully_initialized(); // sets klass->has_init_deps_processed bit
 183       assert(klass->has_init_deps_processed(), "");
 184 
 185       if (AOTCompileEagerly) {
 186         GrowableArray<MethodTrainingData*> mtds;
 187         ktd->iterate_comp_deps([&](CompileTrainingData* ctd) {
 188           if (ctd->init_deps_left_acquire() == 0) {
 189             MethodTrainingData* mtd = ctd->method();
 190             if (mtd->has_holder()) {
 191               mtds.push(mtd);

 192             }
 193           }
 194         });
 195        for (int i = 0; i < mtds.length(); i++) {
 196           MethodTrainingData* mtd = mtds.at(i);
 197           const methodHandle mh(current, const_cast<Method*>(mtd->holder()));
 198           CompilationPolicy::maybe_compile_early(mh, current);
 199         }
 200       }
 201     }
 202   }
 203 }
 204 
 205 void CompilationPolicy::replay_training_at_init(InstanceKlass* klass, JavaThread* current) {
 206   assert(klass->is_initialized(), "");
 207   if (TrainingData::have_data() && klass->in_aot_cache()) {
 208     _training_replay_queue.push(klass, TrainingReplayQueue_lock, current);
 209   }
 210 }
 211 
 212 // For TrainingReplayQueue
 213 template<>
 214 void CompilationPolicyUtils::Queue<InstanceKlass>::print_on(outputStream* st) {
 215   int pos = 0;
 216   for (QueueNode* cur = _head; cur != nullptr; cur = cur->next()) {
 217     ResourceMark rm;
 218     InstanceKlass* ik = cur->value();
 219     st->print_cr("%3d: " INTPTR_FORMAT " %s", ++pos, p2i(ik), ik->external_name());

 495 
 496 // Print an event.
 497 void CompilationPolicy::print_event_on(outputStream *st, EventType type, Method* m, Method* im, int bci, CompLevel level) {
 498   bool inlinee_event = m != im;
 499 
 500   st->print("%lf: [", os::elapsedTime());
 501 
 502   switch(type) {
 503   case CALL:
 504     st->print("call");
 505     break;
 506   case LOOP:
 507     st->print("loop");
 508     break;
 509   case COMPILE:
 510     st->print("compile");
 511     break;
 512   case FORCE_COMPILE:
 513     st->print("force-compile");
 514     break;
 515   case FORCE_RECOMPILE:
 516     st->print("force-recompile");
 517     break;
 518   case REMOVE_FROM_QUEUE:
 519     st->print("remove-from-queue");
 520     break;
 521   case UPDATE_IN_QUEUE:
 522     st->print("update-in-queue");
 523     break;
 524   case REPROFILE:
 525     st->print("reprofile");
 526     break;
 527   case MAKE_NOT_ENTRANT:
 528     st->print("make-not-entrant");
 529     break;
 530   default:
 531     st->print("unknown");
 532   }
 533 
 534   st->print(" level=%d ", level);
 535 
 536   ResourceMark rm;
 537   char *method_name = m->name_and_sig_as_C_string();
 538   st->print("[%s", method_name);
 539   if (inlinee_event) {
 540     char *inlinee_name = im->name_and_sig_as_C_string();
 541     st->print(" [%s]] ", inlinee_name);
 542   }
 543   else st->print("] ");
 544   st->print("@%d queues=%d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile),
 545                                      CompileBroker::queue_size(CompLevel_full_optimization));
 546 
 547   st->print(" rate=");
 548   if (m->prev_time() == 0) st->print("n/a");
 549   else st->print("%f", m->rate());
 550 
 551   RecompilationPolicy::print_load_average(st);
 552 
 553   st->print(" k=%.2lf,%.2lf", threshold_scale(CompLevel_full_profile, Tier3LoadFeedback),
 554                               threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback));
 555 
 556   if (type != COMPILE) {
 557     print_counters_on(st, "", m);
 558     if (inlinee_event) {
 559       print_counters_on(st, "inlinee ", im);
 560     }
 561     st->print(" compilable=");
 562     bool need_comma = false;
 563     if (!m->is_not_compilable(CompLevel_full_profile)) {
 564       st->print("c1");
 565       need_comma = true;
 566     }
 567     if (!m->is_not_osr_compilable(CompLevel_full_profile)) {
 568       if (need_comma) st->print(",");
 569       st->print("c1-osr");
 570       need_comma = true;
 571     }
 572     if (!m->is_not_compilable(CompLevel_full_optimization)) {

 591   st->print_cr("]");
 592 
 593 }
 594 
 595 void CompilationPolicy::print_event(EventType type, Method* m, Method* im, int bci, CompLevel level) {
 596   stringStream s;
 597   print_event_on(&s, type, m, im, bci, level);
 598   ResourceMark rm;
 599   tty->print("%s", s.as_string());
 600 }
 601 
 602 void CompilationPolicy::initialize() {
 603   if (!CompilerConfig::is_interpreter_only()) {
 604     int count = CICompilerCount;
 605     bool c1_only = CompilerConfig::is_c1_only();
 606     bool c2_only = CompilerConfig::is_c2_or_jvmci_compiler_only();
 607     int min_count = (c1_only || c2_only) ? 1 : 2;
 608 
 609 #ifdef _LP64
 610     // Turn on ergonomic compiler count selection
 611     if (AOTCodeCache::maybe_dumping_code()) {
 612       // Assembly phase runs C1 and C2 compilation in separate phases,
 613       // and can use all the CPU threads it can reach. Adjust the common
 614       // options before policy starts overwriting them.
 615       FLAG_SET_ERGO_IF_DEFAULT(UseDynamicNumberOfCompilerThreads, false);
 616       FLAG_SET_ERGO_IF_DEFAULT(CICompilerCountPerCPU, false);
 617       if (FLAG_IS_DEFAULT(CICompilerCount)) {
 618         count =  MAX2(count, os::active_processor_count());
 619       }
 620     }
 621     if (FLAG_IS_DEFAULT(CICompilerCountPerCPU) && FLAG_IS_DEFAULT(CICompilerCount)) {
 622       FLAG_SET_DEFAULT(CICompilerCountPerCPU, true);
 623     }
 624     if (CICompilerCountPerCPU) {
 625       // Simple log n seems to grow too slowly for tiered, try something faster: log n * log log n
 626       int log_cpu = log2i(os::active_processor_count());
 627       int loglog_cpu = log2i(MAX2(log_cpu, 1));
 628       count = MAX2(log_cpu * loglog_cpu * 3 / 2, min_count);
 629     }
 630     if (FLAG_IS_DEFAULT(CICompilerCount)) {
 631       // Make sure there is enough space in the code cache to hold all the compiler buffers
 632       size_t c1_size = 0;
 633 #ifdef COMPILER1
 634       c1_size = Compiler::code_buffer_size();
 635 #endif
 636       size_t c2_size = 0;
 637 #ifdef COMPILER2
 638       c2_size = C2Compiler::initial_code_buffer_size();
 639 #endif
 640       size_t buffer_size = 0;
 641       if (c1_only) {
 642         buffer_size = c1_size;
 643       } else if (c2_only) {
 644         buffer_size = c2_size;
 645       } else {
 646         buffer_size = c1_size / 3 + 2 * c2_size / 3;
 647       }
 648       size_t max_count = (NonNMethodCodeHeapSize - (CodeCacheMinimumUseSpace DEBUG_ONLY(* 3))) / buffer_size;
 649       if ((size_t)count > max_count) {
 650         // Lower the compiler count such that all buffers fit into the code cache

 669     if (c1_only) {
 670       // No C2 compiler threads are needed
 671       set_c1_count(count);
 672     } else if (c2_only) {
 673       // No C1 compiler threads are needed
 674       set_c2_count(count);
 675     } else {
 676 #if INCLUDE_JVMCI
 677       if (UseJVMCICompiler && UseJVMCINativeLibrary) {
 678         int libjvmci_count = MAX2((int) (count * JVMCINativeLibraryThreadFraction), 1);
 679         int c1_count = MAX2(count - libjvmci_count, 1);
 680         set_c2_count(libjvmci_count);
 681         set_c1_count(c1_count);
 682       } else
 683 #endif
 684       {
 685         set_c1_count(MAX2(count / 3, 1));
 686         set_c2_count(MAX2(count - c1_count(), 1));
 687       }
 688     }
 689     if (AOTCodeCache::is_code_load_thread_on()) {
 690       set_ac_count((c1_only || c2_only) ? 1 : 2); // At minimum we need 2 threads to load C1 and C2 AOT code in parallel
 691     }
 692     assert(count == c1_count() + c2_count(), "inconsistent compiler thread count");
 693     set_increase_threshold_at_ratio();
 694   } else {
 695     // Interpreter mode creates no compilers
 696     FLAG_SET_ERGO(CICompilerCount, 0);
 697   }
 698   set_start_time(nanos_to_millis(os::javaTimeNanos()));
 699 }
 700 
 701 
 702 #ifdef ASSERT
 703 bool CompilationPolicy::verify_level(CompLevel level) {
 704   if (TieredCompilation && level > TieredStopAtLevel) {
 705     return false;
 706   }
 707   // Check if there is a compiler to process the requested level
 708   if (!CompilerConfig::is_c1_enabled() && is_c1_compile(level)) {
 709     return false;
 710   }
 711   if (!CompilerConfig::is_c2_or_jvmci_compiler_enabled() && is_c2_compile(level)) {

 814   }
 815 }
 816 
 817 // Called with the queue locked and with at least one element
 818 CompileTask* CompilationPolicy::select_task(CompileQueue* compile_queue, JavaThread* THREAD) {
 819   CompileTask *max_blocking_task = nullptr;
 820   CompileTask *max_task = nullptr;
 821   Method* max_method = nullptr;
 822 
 823   int64_t t = nanos_to_millis(os::javaTimeNanos());
 824   // Iterate through the queue and find a method with a maximum rate.
 825   for (CompileTask* task = compile_queue->first(); task != nullptr;) {
 826     CompileTask* next_task = task->next();
 827     // If a method was unloaded or has been stale for some time, remove it from the queue.
 828     // Blocking tasks and tasks submitted from whitebox API don't become stale
 829     if (task->is_unloaded()) {
 830       compile_queue->remove_and_mark_stale(task);
 831       task = next_task;
 832       continue;
 833     }
 834     if (task->is_aot_load()) {
 835       // AOTCodeCache tasks are on separate queue, and they should load fast. There is no need to walk
 836       // the rest of the queue, just take the task and go.
 837       return task;
 838     }
 839     if (task->is_blocking() && task->compile_reason() == CompileTask::Reason_Whitebox) {
 840       // CTW tasks, submitted as blocking Whitebox requests, do not participate in rate
 841       // selection and/or any level adjustments. Just return them in order.
 842       return task;
 843     }
 844     Method* method = task->method();
 845     methodHandle mh(THREAD, method);
 846     if (task->can_become_stale() && is_stale(t, TieredCompileTaskTimeout, mh) && !is_old(mh)) {
 847       if (PrintTieredEvents) {
 848         print_event(REMOVE_FROM_QUEUE, method, method, task->osr_bci(), (CompLevel) task->comp_level());
 849       }
 850       method->clear_queued_for_compilation();
 851       method->set_pending_queue_processed(false);
 852       compile_queue->remove_and_mark_stale(task);
 853       task = next_task;
 854       continue;
 855     }
 856     update_rate(t, mh);
 857     if (max_task == nullptr || compare_methods(method, max_method) || compare_tasks(task, max_task)) {
 858       // Select a method with the highest rate
 859       max_task = task;
 860       max_method = method;
 861     }
 862 
 863     if (task->is_blocking()) {
 864       if (max_blocking_task == nullptr || compare_methods(method, max_blocking_task->method())) {
 865         max_blocking_task = task;
 866       }
 867     }
 868 
 869     task = next_task;
 870   }
 871 
 872   if (max_blocking_task != nullptr) {
 873     // In blocking compilation mode, the CompileBroker will make
 874     // compilations submitted by a JVMCI compiler thread non-blocking. These
 875     // compilations should be scheduled after all blocking compilations
 876     // to service non-compiler related compilations sooner and reduce the
 877     // chance of such compilations timing out.
 878     max_task = max_blocking_task;
 879     max_method = max_task->method();
 880   }
 881 
 882   methodHandle max_method_h(THREAD, max_method);
 883 
 884   if (max_task != nullptr && max_task->comp_level() == CompLevel_full_profile && TieredStopAtLevel > CompLevel_full_profile &&
 885       max_method != nullptr && is_method_profiled(max_method_h) && !Arguments::is_compiler_only()) {
 886     max_task->set_comp_level(CompLevel_limited_profile);
 887 
 888     if (CompileBroker::compilation_is_complete(max_method_h(), max_task->osr_bci(), CompLevel_limited_profile,
 889                                                false /* requires_online_compilation */,
 890                                                CompileTask::Reason_None)) {
 891       if (PrintTieredEvents) {
 892         print_event(REMOVE_FROM_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level());
 893       }
 894       compile_queue->remove_and_mark_stale(max_task);
 895       max_method->clear_queued_for_compilation();
 896       return nullptr;
 897     }
 898 
 899     if (PrintTieredEvents) {
 900       print_event(UPDATE_IN_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level());
 901     }
 902   }
 903 
 904   return max_task;
 905 }
 906 
 907 void CompilationPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
 908   for (ScopeDesc* sd = trap_scope;; sd = sd->sender()) {
 909     if (PrintTieredEvents) {
 910       print_event(REPROFILE, sd->method(), sd->method(), InvocationEntryBci, CompLevel_none);
 911     }
 912     MethodData* mdo = sd->method()->method_data();
 913     if (mdo != nullptr) {
 914       mdo->reset_start_counters();
 915     }
 916     if (sd->is_top()) break;
 917   }
 918 }
 919 
 920 nmethod* CompilationPolicy::event(const methodHandle& method, const methodHandle& inlinee,
 921                                       int branch_bci, int bci, CompLevel comp_level, nmethod* nm, TRAPS) {
 922   if (PrintTieredEvents) {
 923     print_event(bci == InvocationEntryBci ? CALL : LOOP, method(), inlinee(), bci, comp_level);

1002     if (!CompilationModeFlag::disable_intermediate() &&
1003         level == CompLevel_full_optimization && can_be_osr_compiled(mh, CompLevel_simple)) {
1004       nmethod* osr_nm = mh->lookup_osr_nmethod_for(bci, CompLevel_simple, false);
1005       if (osr_nm != nullptr && osr_nm->comp_level() > CompLevel_simple) {
1006         // Invalidate the existing OSR nmethod so that a compile at CompLevel_simple is permitted.
1007         osr_nm->make_not_entrant(nmethod::InvalidationReason::OSR_INVALIDATION_FOR_COMPILING_WITH_C1);
1008       }
1009       compile(mh, bci, CompLevel_simple, THREAD);
1010     }
1011     return;
1012   }
1013   if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) {
1014     return;
1015   }
1016   if (!CompileBroker::compilation_is_in_queue(mh)) {
1017     if (PrintTieredEvents) {
1018       print_event(COMPILE, mh(), mh(), bci, level);
1019     }
1020     int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
1021     update_rate(nanos_to_millis(os::javaTimeNanos()), mh);
1022     bool requires_online_compilation = false;
1023     if (TrainingData::have_data()) {
1024       MethodTrainingData* mtd = MethodTrainingData::find_fast(mh);
1025       if (mtd != nullptr) {
1026         CompileTrainingData* ctd = mtd->last_toplevel_compile(level);
1027         if (ctd != nullptr) {
1028           requires_online_compilation = (ctd->init_deps_left_acquire() > 0);
1029         }
1030       }
1031     }
1032     CompileBroker::compile_method(mh, bci, level, hot_count, requires_online_compilation, CompileTask::Reason_Tiered, THREAD);
1033   }
1034 }
1035 
1036 // update_rate() is called from select_task() while holding a compile queue lock.
1037 void CompilationPolicy::update_rate(int64_t t, const methodHandle& method) {
1038   // Skip update if counters are absent.
1039   // Can't allocate them since we are holding compile queue lock.
1040   if (method->method_counters() == nullptr)  return;
1041 
1042   if (is_old(method)) {
1043     // We don't remove old methods from the queue,
1044     // so we can just zero the rate.
1045     method->set_rate(0);
1046     return;
1047   }
1048 
1049   // We don't update the rate if we've just came out of a safepoint.
1050   // delta_s is the time since last safepoint in milliseconds.
1051   int64_t delta_s = t - SafepointTracing::end_of_last_safepoint_ms();
1052   int64_t delta_t = t - (method->prev_time() != 0 ? method->prev_time() : start_time()); // milliseconds since the last measurement

1095 }
1096 
1097 double CompilationPolicy::weight(Method* method) {
1098   return (double)(method->rate() + 1) * (method->invocation_count() + 1) * (method->backedge_count() + 1);
1099 }
1100 
1101 // Apply heuristics and return true if x should be compiled before y
1102 bool CompilationPolicy::compare_methods(Method* x, Method* y) {
1103   if (x->highest_comp_level() > y->highest_comp_level()) {
1104     // recompilation after deopt
1105     return true;
1106   } else
1107     if (x->highest_comp_level() == y->highest_comp_level()) {
1108       if (weight(x) > weight(y)) {
1109         return true;
1110       }
1111     }
1112   return false;
1113 }
1114 
1115 bool CompilationPolicy::compare_tasks(CompileTask* x, CompileTask* y) {
1116   assert(!x->is_aot_load() && !y->is_aot_load(), "AOT code caching tasks are not expected here");
1117   if (x->compile_reason() != y->compile_reason() && y->compile_reason() == CompileTask::Reason_MustBeCompiled) {
1118     return true;
1119   }
1120   return false;
1121 }
1122 
1123 // Is method profiled enough?
1124 bool CompilationPolicy::is_method_profiled(const methodHandle& method) {
1125   MethodData* mdo = method->method_data();
1126   if (mdo != nullptr) {
1127     int i = mdo->invocation_count_delta();
1128     int b = mdo->backedge_count_delta();
1129     return CallPredicate::apply_scaled(method, CompLevel_full_profile, i, b, 1);
1130   }
1131   return false;
1132 }
1133 
1134 
1135 // Determine is a method is mature.
1136 bool CompilationPolicy::is_mature(MethodData* mdo) {
1137   if (Arguments::is_compiler_only()) {
1138     // Always report profiles as immature with -Xcomp
1139     return false;
1140   }
1141   methodHandle mh(Thread::current(), mdo->method());
1142   if (mdo != nullptr) {

1149 }
1150 
1151 // If a method is old enough and is still in the interpreter we would want to
1152 // start profiling without waiting for the compiled method to arrive.
1153 // We also take the load on compilers into the account.
1154 bool CompilationPolicy::should_create_mdo(const methodHandle& method, CompLevel cur_level) {
1155   if (cur_level != CompLevel_none || force_comp_at_level_simple(method) || CompilationModeFlag::quick_only() || !ProfileInterpreter) {
1156     return false;
1157   }
1158 
1159   if (TrainingData::have_data()) {
1160     MethodTrainingData* mtd = MethodTrainingData::find_fast(method);
1161     if (mtd != nullptr && mtd->saw_level(CompLevel_full_optimization)) {
1162       return true;
1163     }
1164   }
1165 
1166   if (is_old(method)) {
1167     return true;
1168   }

1169   int i = method->invocation_count();
1170   int b = method->backedge_count();
1171   double k = Tier0ProfilingStartPercentage / 100.0;
1172 
1173   // If the top level compiler is not keeping up, delay profiling.
1174   if (CompileBroker::queue_size(CompLevel_full_optimization) <= Tier0Delay * compiler_count(CompLevel_full_optimization)) {
1175     return CallPredicate::apply_scaled(method, CompLevel_none, i, b, k) || LoopPredicate::apply_scaled(method, CompLevel_none, i, b, k);
1176   }
1177   return false;
1178 }
1179 
1180 // Inlining control: if we're compiling a profiled method with C1 and the callee
1181 // is known to have OSRed in a C2 version, don't inline it.
1182 bool CompilationPolicy::should_not_inline(ciEnv* env, ciMethod* callee) {
1183   CompLevel comp_level = (CompLevel)env->comp_level();
1184   if (comp_level == CompLevel_full_profile ||
1185       comp_level == CompLevel_limited_profile) {
1186     return callee->highest_osr_comp_level() == CompLevel_full_optimization;
1187   }
1188   return false;
< prev index next >