< 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   }

 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         ktd->iterate_comp_deps([&](CompileTrainingData* ctd) {
 187           if (ctd->init_deps_left_acquire() == 0) {
 188             MethodTrainingData* mtd = ctd->method();
 189             if (mtd->has_holder()) {
 190               const methodHandle mh(current, const_cast<Method*>(mtd->holder()));
 191               CompilationPolicy::maybe_compile_early(mh, current);
 192             }
 193           }
 194         });
 195       }
 196     }
 197   }
 198 }
 199 
 200 void CompilationPolicy::replay_training_at_init(InstanceKlass* klass, JavaThread* current) {
 201   assert(klass->is_initialized(), "");
 202   if (TrainingData::have_data() && klass->in_aot_cache()) {
 203     _training_replay_queue.push(klass, TrainingReplayQueue_lock, current);
 204   }

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

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

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

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

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

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

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

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