< 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, MethodTrainingData* mtd, 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   // Consider replacing conservatively compiled AOT Preload code with faster AOT code
 100   nmethod* nm = m->code();
 101   bool recompile = (nm != nullptr) && nm->preloaded();
 102   CompLevel cur_level = static_cast<CompLevel>(m->highest_comp_level());
 103   CompLevel next_level = trained_transition(m, cur_level, mtd, THREAD);
 104   if ((next_level != cur_level || recompile) && can_be_compiled(m, next_level) && !CompileBroker::compilation_is_in_queue(m)) {
 105     // We are here becasue some of CTD have all init deps satisifed.
 106     CompileTrainingData* ctd = mtd->compile_data_for_aot_code(next_level);
 107     bool requires_online_compilation = true;
 108     if (ctd != nullptr) {
 109       // Can't load normal AOT code - not all dependancies are ready,
 110       // request normal compilation
 111       requires_online_compilation = (ctd->init_deps_left_acquire() > 0);
 112     }
 113     // Skip compilation if next_level doesn't have CDT or CDT
 114     // does not have all class init dependencies satisfied.
 115     if (requires_online_compilation) {
 116       return;
 117     }
 118     if (PrintTieredEvents) {
 119       print_event(FORCE_COMPILE, m(), m(), InvocationEntryBci, next_level);
 120     }
 121     CompileBroker::compile_method(m, InvocationEntryBci, next_level, 0, requires_online_compilation, CompileTask::Reason_MustBeCompiled, THREAD);
 122     if (HAS_PENDING_EXCEPTION) {
 123       CLEAR_PENDING_EXCEPTION;
 124     }
 125   }
 126 }
 127 
 128 void CompilationPolicy::compile_if_required(const methodHandle& m, TRAPS) {
 129   if (!THREAD->can_call_java() || THREAD->is_Compiler_thread()) {
 130     // don't force compilation, resolve was on behalf of compiler
 131     return;
 132   }
 133   if (m->method_holder()->is_not_initialized()) {
 134     // 'is_not_initialized' means not only '!is_initialized', but also that
 135     // initialization has not been started yet ('!being_initialized')
 136     // Do not force compilation of methods in uninitialized classes.
 137     // Note that doing this would throw an assert later,
 138     // in CompileBroker::compile_method.
 139     // We sometimes use the link resolver to do reflective lookups
 140     // even before classes are initialized.
 141     return;
 142   }
 143 
 144   if (must_be_compiled(m)) {
 145     // This path is unusual, mostly used by the '-Xcomp' stress test mode.
 146     CompLevel level = initial_compile_level(m);
 147     if (PrintTieredEvents) {
 148       print_event(FORCE_COMPILE, m(), m(), InvocationEntryBci, level);
 149     }
 150     // Test AOT code too
 151     bool requires_online_compilation = true;
 152     if (TrainingData::have_data()) {
 153       MethodTrainingData* mtd = MethodTrainingData::find_fast(m);
 154       if (mtd != nullptr) {
 155         CompileTrainingData* ctd = mtd->last_toplevel_compile(level);
 156         if (ctd != nullptr) {
 157           requires_online_compilation = (ctd->init_deps_left_acquire() > 0);
 158         }
 159       }
 160     }
 161     CompileBroker::compile_method(m, InvocationEntryBci, level, 0, requires_online_compilation, CompileTask::Reason_MustBeCompiled, THREAD);
 162   }
 163 }
 164 
 165 void CompilationPolicy::replay_training_at_init_impl(InstanceKlass* klass, JavaThread* current) {
 166   if (!klass->has_init_deps_processed()) {
 167     ResourceMark rm;
 168     log_debug(training)("Replay training: %s", klass->external_name());
 169 
 170     KlassTrainingData* ktd = KlassTrainingData::find(klass);
 171     if (ktd != nullptr) {
 172       guarantee(ktd->has_holder(), "");
 173       ktd->notice_fully_initialized(); // sets klass->has_init_deps_processed bit
 174       assert(klass->has_init_deps_processed(), "");
 175 
 176       if (AOTCompileEagerly) {
 177         GrowableArray<MethodTrainingData*> mtds;
 178         ktd->iterate_comp_deps([&](CompileTrainingData* ctd) {
 179           if (ctd->init_deps_left_acquire() == 0) {
 180             MethodTrainingData* mtd = ctd->method();
 181             if (mtd->has_holder()) {
 182               mtds.push(mtd);

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

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

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

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

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

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

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

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

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