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

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



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


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

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










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


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

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



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

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





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

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


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

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

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










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

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








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

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

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

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

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

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

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

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

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

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