src/hotspot/share/compiler/tieredThresholdPolicy.cpp
changeset 58894 b95bead30957
parent 58545 725244418646
child 59056 15936b142f86
equal deleted inserted replaced
58893:ec954ef6caf1 58894:b95bead30957
    26 #include "compiler/compileBroker.hpp"
    26 #include "compiler/compileBroker.hpp"
    27 #include "compiler/compilerOracle.hpp"
    27 #include "compiler/compilerOracle.hpp"
    28 #include "compiler/tieredThresholdPolicy.hpp"
    28 #include "compiler/tieredThresholdPolicy.hpp"
    29 #include "memory/resourceArea.hpp"
    29 #include "memory/resourceArea.hpp"
    30 #include "runtime/arguments.hpp"
    30 #include "runtime/arguments.hpp"
       
    31 #include "runtime/frame.inline.hpp"
    31 #include "runtime/handles.inline.hpp"
    32 #include "runtime/handles.inline.hpp"
    32 #include "runtime/safepoint.hpp"
    33 #include "runtime/safepoint.hpp"
    33 #include "runtime/safepointVerifiers.hpp"
    34 #include "runtime/safepointVerifiers.hpp"
    34 #include "code/scopeDesc.hpp"
    35 #include "code/scopeDesc.hpp"
    35 #include "oops/method.inline.hpp"
    36 #include "oops/method.inline.hpp"
    40 #ifdef TIERED
    41 #ifdef TIERED
    41 
    42 
    42 #include "c1/c1_Compiler.hpp"
    43 #include "c1/c1_Compiler.hpp"
    43 #include "opto/c2compiler.hpp"
    44 #include "opto/c2compiler.hpp"
    44 
    45 
    45 template<CompLevel level>
    46 bool TieredThresholdPolicy::call_predicate_helper(Method* method, CompLevel cur_level, int i, int b, double scale) {
    46 bool TieredThresholdPolicy::call_predicate_helper(int i, int b, double scale, Method* method) {
       
    47   double threshold_scaling;
    47   double threshold_scaling;
    48   if (CompilerOracle::has_option_value(method, "CompileThresholdScaling", threshold_scaling)) {
    48   if (CompilerOracle::has_option_value(method, "CompileThresholdScaling", threshold_scaling)) {
    49     scale *= threshold_scaling;
    49     scale *= threshold_scaling;
    50   }
    50   }
    51   switch(level) {
    51   switch(cur_level) {
    52   case CompLevel_aot:
    52   case CompLevel_aot:
    53     return (i >= Tier3AOTInvocationThreshold * scale) ||
    53     if (CompilationModeFlag::disable_intermediate()) {
    54            (i >= Tier3AOTMinInvocationThreshold * scale && i + b >= Tier3AOTCompileThreshold * scale);
    54       return (i >= Tier0AOTInvocationThreshold * scale) ||
       
    55              (i >= Tier0AOTMinInvocationThreshold * scale && i + b >= Tier0AOTCompileThreshold * scale);
       
    56     } else {
       
    57       return (i >= Tier3AOTInvocationThreshold * scale) ||
       
    58              (i >= Tier3AOTMinInvocationThreshold * scale && i + b >= Tier3AOTCompileThreshold * scale);
       
    59     }
    55   case CompLevel_none:
    60   case CompLevel_none:
       
    61     if (CompilationModeFlag::disable_intermediate()) {
       
    62       return (i >= Tier40InvocationThreshold * scale) ||
       
    63              (i >= Tier40MinInvocationThreshold * scale && i + b >= Tier40CompileThreshold * scale);
       
    64     }
       
    65     // Fall through
    56   case CompLevel_limited_profile:
    66   case CompLevel_limited_profile:
    57     return (i >= Tier3InvocationThreshold * scale) ||
    67     return (i >= Tier3InvocationThreshold * scale) ||
    58            (i >= Tier3MinInvocationThreshold * scale && i + b >= Tier3CompileThreshold * scale);
    68            (i >= Tier3MinInvocationThreshold * scale && i + b >= Tier3CompileThreshold * scale);
    59   case CompLevel_full_profile:
    69   case CompLevel_full_profile:
    60    return (i >= Tier4InvocationThreshold * scale) ||
    70    return (i >= Tier4InvocationThreshold * scale) ||
    61           (i >= Tier4MinInvocationThreshold * scale && i + b >= Tier4CompileThreshold * scale);
    71           (i >= Tier4MinInvocationThreshold * scale && i + b >= Tier4CompileThreshold * scale);
    62   }
    72   default:
    63   return true;
    73    return true;
    64 }
    74   }
    65 
    75 }
    66 template<CompLevel level>
    76 
    67 bool TieredThresholdPolicy::loop_predicate_helper(int i, int b, double scale, Method* method) {
    77 bool TieredThresholdPolicy::loop_predicate_helper(Method* method, CompLevel cur_level, int i, int b, double scale) {
    68   double threshold_scaling;
    78   double threshold_scaling;
    69   if (CompilerOracle::has_option_value(method, "CompileThresholdScaling", threshold_scaling)) {
    79   if (CompilerOracle::has_option_value(method, "CompileThresholdScaling", threshold_scaling)) {
    70     scale *= threshold_scaling;
    80     scale *= threshold_scaling;
    71   }
    81   }
    72   switch(level) {
    82   switch(cur_level) {
    73   case CompLevel_aot:
    83   case CompLevel_aot:
    74     return b >= Tier3AOTBackEdgeThreshold * scale;
    84     if (CompilationModeFlag::disable_intermediate()) {
       
    85       return b >= Tier0AOTBackEdgeThreshold * scale;
       
    86     } else {
       
    87       return b >= Tier3AOTBackEdgeThreshold * scale;
       
    88     }
    75   case CompLevel_none:
    89   case CompLevel_none:
       
    90     if (CompilationModeFlag::disable_intermediate()) {
       
    91       return b >= Tier40BackEdgeThreshold * scale;
       
    92     }
       
    93     // Fall through
    76   case CompLevel_limited_profile:
    94   case CompLevel_limited_profile:
    77     return b >= Tier3BackEdgeThreshold * scale;
    95     return b >= Tier3BackEdgeThreshold * scale;
    78   case CompLevel_full_profile:
    96   case CompLevel_full_profile:
    79     return b >= Tier4BackEdgeThreshold * scale;
    97     return b >= Tier4BackEdgeThreshold * scale;
    80   }
    98   default:
    81   return true;
    99     return true;
       
   100   }
    82 }
   101 }
    83 
   102 
    84 // Simple methods are as good being compiled with C1 as C2.
   103 // Simple methods are as good being compiled with C1 as C2.
    85 // Determine if a given method is such a case.
   104 // Determine if a given method is such a case.
    86 bool TieredThresholdPolicy::is_trivial(Method* method) {
   105 bool TieredThresholdPolicy::is_trivial(Method* method) {
    89     return true;
   108     return true;
    90   }
   109   }
    91   return false;
   110   return false;
    92 }
   111 }
    93 
   112 
    94 bool TieredThresholdPolicy::should_compile_at_level_simple(Method* method) {
   113 bool TieredThresholdPolicy::force_comp_at_level_simple(Method* method) {
    95   if (TieredThresholdPolicy::is_trivial(method)) {
   114   if (CompilationModeFlag::quick_internal()) {
    96     return true;
       
    97   }
       
    98 #if INCLUDE_JVMCI
   115 #if INCLUDE_JVMCI
    99   if (UseJVMCICompiler) {
   116     if (UseJVMCICompiler) {
   100     AbstractCompiler* comp = CompileBroker::compiler(CompLevel_full_optimization);
   117       AbstractCompiler* comp = CompileBroker::compiler(CompLevel_full_optimization);
   101     if (comp != NULL && comp->is_jvmci() && ((JVMCICompiler*) comp)->force_comp_at_level_simple(method)) {
   118       if (comp != NULL && comp->is_jvmci() && ((JVMCICompiler*) comp)->force_comp_at_level_simple(method)) {
   102       return true;
   119         return true;
   103     }
   120       }
   104   }
   121     }
   105 #endif
   122 #endif
       
   123   }
   106   return false;
   124   return false;
   107 }
   125 }
   108 
   126 
   109 CompLevel TieredThresholdPolicy::comp_level(Method* method) {
   127 CompLevel TieredThresholdPolicy::comp_level(Method* method) {
   110   CompiledMethod *nm = method->code();
   128   CompiledMethod *nm = method->code();
   179   }
   197   }
   180   else tty->print("] ");
   198   else tty->print("] ");
   181   tty->print("@%d queues=%d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile),
   199   tty->print("@%d queues=%d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile),
   182                                       CompileBroker::queue_size(CompLevel_full_optimization));
   200                                       CompileBroker::queue_size(CompLevel_full_optimization));
   183 
   201 
   184   print_specific(type, mh, imh, bci, level);
   202   tty->print(" rate=");
       
   203   if (mh->prev_time() == 0) tty->print("n/a");
       
   204   else tty->print("%f", mh->rate());
       
   205 
       
   206   tty->print(" k=%.2lf,%.2lf", threshold_scale(CompLevel_full_profile, Tier3LoadFeedback),
       
   207                                threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback));
   185 
   208 
   186   if (type != COMPILE) {
   209   if (type != COMPILE) {
   187     print_counters("", mh);
   210     print_counters("", mh);
   188     if (inlinee_event) {
   211     if (inlinee_event) {
   189       print_counters("inlinee ", imh);
   212       print_counters("inlinee ", imh);
   214     } else tty->print("idle");
   237     } else tty->print("idle");
   215   }
   238   }
   216   tty->print_cr("]");
   239   tty->print_cr("]");
   217 }
   240 }
   218 
   241 
       
   242 
   219 void TieredThresholdPolicy::initialize() {
   243 void TieredThresholdPolicy::initialize() {
   220   int count = CICompilerCount;
   244   int count = CICompilerCount;
   221   bool c1_only = TieredStopAtLevel < CompLevel_full_optimization;
   245   bool c1_only = TieredStopAtLevel < CompLevel_full_optimization || CompilationModeFlag::quick_only();
       
   246   bool c2_only = CompilationModeFlag::high_only();
   222 #ifdef _LP64
   247 #ifdef _LP64
   223   // Turn on ergonomic compiler count selection
   248   // Turn on ergonomic compiler count selection
   224   if (FLAG_IS_DEFAULT(CICompilerCountPerCPU) && FLAG_IS_DEFAULT(CICompilerCount)) {
   249   if (FLAG_IS_DEFAULT(CICompilerCountPerCPU) && FLAG_IS_DEFAULT(CICompilerCount)) {
   225     FLAG_SET_DEFAULT(CICompilerCountPerCPU, true);
   250     FLAG_SET_DEFAULT(CICompilerCountPerCPU, true);
   226   }
   251   }
   255 #endif
   280 #endif
   256 
   281 
   257   if (c1_only) {
   282   if (c1_only) {
   258     // No C2 compiler thread required
   283     // No C2 compiler thread required
   259     set_c1_count(count);
   284     set_c1_count(count);
       
   285   } else if (c2_only) {
       
   286     set_c2_count(count);
   260   } else {
   287   } else {
   261     set_c1_count(MAX2(count / 3, 1));
   288     set_c1_count(MAX2(count / 3, 1));
   262     set_c2_count(MAX2(count - c1_count(), 1));
   289     set_c2_count(MAX2(count - c1_count(), 1));
   263   }
   290   }
   264   assert(count == c1_count() + c2_count(), "inconsistent compiler thread count");
   291   assert(count == c1_count() + c2_count(), "inconsistent compiler thread count");
   411   } else {
   438   } else {
   412     // method == inlinee if the event originated in the main method
   439     // method == inlinee if the event originated in the main method
   413     method_back_branch_event(method, inlinee, bci, comp_level, nm, thread);
   440     method_back_branch_event(method, inlinee, bci, comp_level, nm, thread);
   414     // Check if event led to a higher level OSR compilation
   441     // Check if event led to a higher level OSR compilation
   415     CompLevel expected_comp_level = comp_level;
   442     CompLevel expected_comp_level = comp_level;
   416     if (inlinee->is_not_osr_compilable(expected_comp_level)) {
   443     if (!CompilationModeFlag::disable_intermediate() && inlinee->is_not_osr_compilable(expected_comp_level)) {
   417       // It's not possble to reach the expected level so fall back to simple.
   444       // It's not possble to reach the expected level so fall back to simple.
   418       expected_comp_level = CompLevel_simple;
   445       expected_comp_level = CompLevel_simple;
   419     }
   446     }
   420     nmethod* osr_nm = inlinee->lookup_osr_nmethod_for(bci, expected_comp_level, false);
   447     nmethod* osr_nm = inlinee->lookup_osr_nmethod_for(bci, expected_comp_level, false);
   421     assert(osr_nm == NULL || osr_nm->comp_level() >= expected_comp_level, "lookup_osr_nmethod_for is broken");
   448     assert(osr_nm == NULL || osr_nm->comp_level() >= expected_comp_level, "lookup_osr_nmethod_for is broken");
   428 }
   455 }
   429 
   456 
   430 // Check if the method can be compiled, change level if necessary
   457 // Check if the method can be compiled, change level if necessary
   431 void TieredThresholdPolicy::compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
   458 void TieredThresholdPolicy::compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
   432   assert(level <= TieredStopAtLevel, "Invalid compilation level");
   459   assert(level <= TieredStopAtLevel, "Invalid compilation level");
       
   460   if (CompilationModeFlag::quick_only()) {
       
   461     assert(level <= CompLevel_simple, "Invalid compilation level");
       
   462   } else if (CompilationModeFlag::disable_intermediate()) {
       
   463     assert(level != CompLevel_full_profile && level != CompLevel_limited_profile, "C1 profiling levels shouldn't be used with intermediate levels disabled");
       
   464   }
       
   465 
   433   if (level == CompLevel_none) {
   466   if (level == CompLevel_none) {
       
   467     if (mh->has_compiled_code()) {
       
   468       // Happens when we switch from AOT to interpreter to profile.
       
   469       MutexLocker ml(Compile_lock);
       
   470       NoSafepointVerifier nsv;
       
   471       if (mh->has_compiled_code()) {
       
   472         mh->code()->make_not_used();
       
   473       }
       
   474       // Deoptimize immediately (we don't have to wait for a compile).
       
   475       RegisterMap map(thread, false);
       
   476       frame fr = thread->last_frame().sender(&map);
       
   477       Deoptimization::deoptimize_frame(thread, fr.id());
       
   478     }
   434     return;
   479     return;
   435   }
   480   }
   436   if (level == CompLevel_aot) {
   481   if (level == CompLevel_aot) {
   437     if (mh->has_aot_code()) {
   482     if (mh->has_aot_code()) {
   438       if (PrintTieredEvents) {
   483       if (PrintTieredEvents) {
   450       }
   495       }
   451     }
   496     }
   452     return;
   497     return;
   453   }
   498   }
   454 
   499 
   455   // Check if the method can be compiled. If it cannot be compiled with C1, continue profiling
   500   if (!CompilationModeFlag::disable_intermediate()) {
   456   // in the interpreter and then compile with C2 (the transition function will request that,
   501     // Check if the method can be compiled. If it cannot be compiled with C1, continue profiling
   457   // see common() ). If the method cannot be compiled with C2 but still can with C1, compile it with
   502     // in the interpreter and then compile with C2 (the transition function will request that,
   458   // pure C1.
   503     // see common() ). If the method cannot be compiled with C2 but still can with C1, compile it with
   459   if ((bci == InvocationEntryBci && !can_be_compiled(mh, level))) {
   504     // pure C1.
   460     if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) {
   505     if ((bci == InvocationEntryBci && !can_be_compiled(mh, level))) {
   461       compile(mh, bci, CompLevel_simple, thread);
   506       if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) {
   462     }
   507         compile(mh, bci, CompLevel_simple, thread);
   463     return;
   508       }
   464   }
   509       return;
   465   if ((bci != InvocationEntryBci && !can_be_osr_compiled(mh, level))) {
   510     }
   466     if (level == CompLevel_full_optimization && can_be_osr_compiled(mh, CompLevel_simple)) {
   511     if ((bci != InvocationEntryBci && !can_be_osr_compiled(mh, level))) {
   467       nmethod* osr_nm = mh->lookup_osr_nmethod_for(bci, CompLevel_simple, false);
   512       if (level == CompLevel_full_optimization && can_be_osr_compiled(mh, CompLevel_simple)) {
   468       if (osr_nm != NULL && osr_nm->comp_level() > CompLevel_simple) {
   513         nmethod* osr_nm = mh->lookup_osr_nmethod_for(bci, CompLevel_simple, false);
   469         // Invalidate the existing OSR nmethod so that a compile at CompLevel_simple is permitted.
   514         if (osr_nm != NULL && osr_nm->comp_level() > CompLevel_simple) {
   470         osr_nm->make_not_entrant();
   515           // Invalidate the existing OSR nmethod so that a compile at CompLevel_simple is permitted.
   471       }
   516           osr_nm->make_not_entrant();
   472       compile(mh, bci, CompLevel_simple, thread);
   517         }
   473     }
   518         compile(mh, bci, CompLevel_simple, thread);
   474     return;
   519       }
       
   520       return;
       
   521     }
   475   }
   522   }
   476   if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) {
   523   if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) {
   477     return;
   524     return;
   478   }
   525   }
   479   if (!CompileBroker::compilation_is_in_queue(mh)) {
   526   if (!CompileBroker::compilation_is_in_queue(mh)) {
   480     if (PrintTieredEvents) {
   527     if (PrintTieredEvents) {
   481       print_event(COMPILE, mh, mh, bci, level);
   528       print_event(COMPILE, mh, mh, bci, level);
   482     }
   529     }
   483     submit_compile(mh, bci, level, thread);
   530     int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
   484   }
   531     update_rate(os::javaTimeMillis(), mh());
   485 }
   532     CompileBroker::compile_method(mh, bci, level, mh, hot_count, CompileTask::Reason_Tiered, thread);
   486 
   533   }
   487 // Update the rate and submit compile
       
   488 void TieredThresholdPolicy::submit_compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
       
   489   int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
       
   490   update_rate(os::javaTimeMillis(), mh());
       
   491   CompileBroker::compile_method(mh, bci, level, mh, hot_count, CompileTask::Reason_Tiered, thread);
       
   492 }
       
   493 
       
   494 // Print an event.
       
   495 void TieredThresholdPolicy::print_specific(EventType type, const methodHandle& mh, const methodHandle& imh,
       
   496                                              int bci, CompLevel level) {
       
   497   tty->print(" rate=");
       
   498   if (mh->prev_time() == 0) tty->print("n/a");
       
   499   else tty->print("%f", mh->rate());
       
   500 
       
   501   tty->print(" k=%.2lf,%.2lf", threshold_scale(CompLevel_full_profile, Tier3LoadFeedback),
       
   502                                threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback));
       
   503 
       
   504 }
   534 }
   505 
   535 
   506 // update_rate() is called from select_task() while holding a compile queue lock.
   536 // update_rate() is called from select_task() while holding a compile queue lock.
   507 void TieredThresholdPolicy::update_rate(jlong t, Method* m) {
   537 void TieredThresholdPolicy::update_rate(jlong t, Method* m) {
   508   // Skip update if counters are absent.
   538   // Skip update if counters are absent.
   583 bool TieredThresholdPolicy::is_method_profiled(Method* method) {
   613 bool TieredThresholdPolicy::is_method_profiled(Method* method) {
   584   MethodData* mdo = method->method_data();
   614   MethodData* mdo = method->method_data();
   585   if (mdo != NULL) {
   615   if (mdo != NULL) {
   586     int i = mdo->invocation_count_delta();
   616     int i = mdo->invocation_count_delta();
   587     int b = mdo->backedge_count_delta();
   617     int b = mdo->backedge_count_delta();
   588     return call_predicate_helper<CompLevel_full_profile>(i, b, 1, method);
   618     return call_predicate_helper(method, CompilationModeFlag::disable_intermediate() ? CompLevel_none : CompLevel_full_profile, i, b, 1);
   589   }
   619   }
   590   return false;
   620   return false;
   591 }
   621 }
   592 
   622 
   593 double TieredThresholdPolicy::threshold_scale(CompLevel level, int feedback_k) {
   623 double TieredThresholdPolicy::threshold_scale(CompLevel level, int feedback_k) {
   594   double queue_size = CompileBroker::queue_size(level);
       
   595   int comp_count = compiler_count(level);
   624   int comp_count = compiler_count(level);
   596   double k = queue_size / (feedback_k * comp_count) + 1;
   625   if (comp_count > 0) {
   597 
   626     double queue_size = CompileBroker::queue_size(level);
   598   // Increase C1 compile threshold when the code cache is filled more
   627     double k = queue_size / (feedback_k * comp_count) + 1;
   599   // than specified by IncreaseFirstTierCompileThresholdAt percentage.
   628 
   600   // The main intention is to keep enough free space for C2 compiled code
   629     // Increase C1 compile threshold when the code cache is filled more
   601   // to achieve peak performance if the code cache is under stress.
   630     // than specified by IncreaseFirstTierCompileThresholdAt percentage.
   602   if ((TieredStopAtLevel == CompLevel_full_optimization) && (level != CompLevel_full_optimization))  {
   631     // The main intention is to keep enough free space for C2 compiled code
   603     double current_reverse_free_ratio = CodeCache::reverse_free_ratio(CodeCache::get_code_blob_type(level));
   632     // to achieve peak performance if the code cache is under stress.
   604     if (current_reverse_free_ratio > _increase_threshold_at_ratio) {
   633     if (!CompilationModeFlag::disable_intermediate() && TieredStopAtLevel == CompLevel_full_optimization && level != CompLevel_full_optimization)  {
   605       k *= exp(current_reverse_free_ratio - _increase_threshold_at_ratio);
   634       double current_reverse_free_ratio = CodeCache::reverse_free_ratio(CodeCache::get_code_blob_type(level));
   606     }
   635       if (current_reverse_free_ratio > _increase_threshold_at_ratio) {
   607   }
   636         k *= exp(current_reverse_free_ratio - _increase_threshold_at_ratio);
   608   return k;
   637       }
       
   638     }
       
   639     return k;
       
   640   }
       
   641   return 1;
   609 }
   642 }
   610 
   643 
   611 // Call and loop predicates determine whether a transition to a higher
   644 // Call and loop predicates determine whether a transition to a higher
   612 // compilation level should be performed (pointers to predicate functions
   645 // compilation level should be performed (pointers to predicate functions
   613 // are passed to common()).
   646 // are passed to common()).
   614 // Tier?LoadFeedback is basically a coefficient that determines of
   647 // Tier?LoadFeedback is basically a coefficient that determines of
   615 // how many methods per compiler thread can be in the queue before
   648 // how many methods per compiler thread can be in the queue before
   616 // the threshold values double.
   649 // the threshold values double.
   617 bool TieredThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level, Method* method) {
   650 bool TieredThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level, Method* method) {
       
   651   double k = 1;
   618   switch(cur_level) {
   652   switch(cur_level) {
   619   case CompLevel_aot: {
   653   case CompLevel_aot: {
   620     double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
   654     k = CompilationModeFlag::disable_intermediate() ? 1 : threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
   621     return loop_predicate_helper<CompLevel_aot>(i, b, k, method);
   655     break;
   622   }
   656   }
   623   case CompLevel_none:
   657   case CompLevel_none: {
       
   658     if (CompilationModeFlag::disable_intermediate()) {
       
   659       k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
       
   660       break;
       
   661     }
       
   662   }
       
   663   // Fall through
   624   case CompLevel_limited_profile: {
   664   case CompLevel_limited_profile: {
   625     double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
   665     k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
   626     return loop_predicate_helper<CompLevel_none>(i, b, k, method);
   666     break;
   627   }
   667   }
   628   case CompLevel_full_profile: {
   668   case CompLevel_full_profile: {
   629     double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
   669     k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
   630     return loop_predicate_helper<CompLevel_full_profile>(i, b, k, method);
   670     break;
   631   }
   671   }
   632   default:
   672   default:
   633     return true;
   673     return true;
   634   }
   674   }
       
   675  return loop_predicate_helper(method, cur_level, i, b, k);
   635 }
   676 }
   636 
   677 
   637 bool TieredThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level, Method* method) {
   678 bool TieredThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level, Method* method) {
       
   679   double k = 1;
   638   switch(cur_level) {
   680   switch(cur_level) {
   639   case CompLevel_aot: {
   681   case CompLevel_aot: {
   640     double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
   682     k = CompilationModeFlag::disable_intermediate() ? 1 : threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
   641     return call_predicate_helper<CompLevel_aot>(i, b, k, method);
   683     break;
   642   }
   684   }
   643   case CompLevel_none:
   685   case CompLevel_none: {
       
   686     if (CompilationModeFlag::disable_intermediate()) {
       
   687       k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
       
   688       break;
       
   689     }
       
   690   }
       
   691   // Fall through
   644   case CompLevel_limited_profile: {
   692   case CompLevel_limited_profile: {
   645     double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
   693     k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
   646     return call_predicate_helper<CompLevel_none>(i, b, k, method);
   694     break;
   647   }
   695   }
   648   case CompLevel_full_profile: {
   696   case CompLevel_full_profile: {
   649     double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
   697     k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
   650     return call_predicate_helper<CompLevel_full_profile>(i, b, k, method);
   698     break;
   651   }
   699   }
   652   default:
   700   default:
   653     return true;
   701     return true;
   654   }
   702   }
       
   703   return call_predicate_helper(method, cur_level, i, b, k);
   655 }
   704 }
   656 
   705 
   657 // Determine is a method is mature.
   706 // Determine is a method is mature.
   658 bool TieredThresholdPolicy::is_mature(Method* method) {
   707 bool TieredThresholdPolicy::is_mature(Method* method) {
   659   if (should_compile_at_level_simple(method)) return true;
   708   if (is_trivial(method) || force_comp_at_level_simple(method)) return true;
   660   MethodData* mdo = method->method_data();
   709   MethodData* mdo = method->method_data();
   661   if (mdo != NULL) {
   710   if (mdo != NULL) {
   662     int i = mdo->invocation_count();
   711     int i = mdo->invocation_count();
   663     int b = mdo->backedge_count();
   712     int b = mdo->backedge_count();
   664     double k = ProfileMaturityPercentage / 100.0;
   713     double k = ProfileMaturityPercentage / 100.0;
   665     return call_predicate_helper<CompLevel_full_profile>(i, b, k, method) ||
   714     CompLevel main_profile_level = CompilationModeFlag::disable_intermediate() ? CompLevel_none : CompLevel_full_profile;
   666            loop_predicate_helper<CompLevel_full_profile>(i, b, k, method);
   715     return call_predicate_helper(method, main_profile_level, i, b, k) || loop_predicate_helper(method, main_profile_level, i, b, k);
   667   }
   716   }
   668   return false;
   717   return false;
   669 }
   718 }
   670 
   719 
   671 // If a method is old enough and is still in the interpreter we would want to
   720 // If a method is old enough and is still in the interpreter we would want to
   672 // start profiling without waiting for the compiled method to arrive.
   721 // start profiling without waiting for the compiled method to arrive.
   673 // We also take the load on compilers into the account.
   722 // We also take the load on compilers into the account.
   674 bool TieredThresholdPolicy::should_create_mdo(Method* method, CompLevel cur_level) {
   723 bool TieredThresholdPolicy::should_create_mdo(Method* method, CompLevel cur_level) {
   675   if (cur_level == CompLevel_none &&
   724   if (cur_level != CompLevel_none || force_comp_at_level_simple(method)) {
   676       CompileBroker::queue_size(CompLevel_full_optimization) <=
   725     return false;
   677       Tier3DelayOn * compiler_count(CompLevel_full_optimization)) {
   726   }
   678     int i = method->invocation_count();
   727   int i = method->invocation_count();
   679     int b = method->backedge_count();
   728   int b = method->backedge_count();
   680     double k = Tier0ProfilingStartPercentage / 100.0;
   729   double k = Tier0ProfilingStartPercentage / 100.0;
   681     return call_predicate_helper<CompLevel_none>(i, b, k, method) || loop_predicate_helper<CompLevel_none>(i, b, k, method);
   730 
       
   731   // If the top level compiler is not keeping up, delay profiling.
       
   732   if (CompileBroker::queue_size(CompLevel_full_optimization) <=  (CompilationModeFlag::disable_intermediate() ? Tier0Delay : Tier3DelayOn) * compiler_count(CompLevel_full_optimization)) {
       
   733     return call_predicate_helper(method, CompLevel_none, i, b, k) || loop_predicate_helper(method, CompLevel_none, i, b, k);
   682   }
   734   }
   683   return false;
   735   return false;
   684 }
   736 }
   685 
   737 
   686 // Inlining control: if we're compiling a profiled method with C1 and the callee
   738 // Inlining control: if we're compiling a profiled method with C1 and the callee
   712  * Method states:
   764  * Method states:
   713  *   0 - interpreter (CompLevel_none)
   765  *   0 - interpreter (CompLevel_none)
   714  *   1 - pure C1 (CompLevel_simple)
   766  *   1 - pure C1 (CompLevel_simple)
   715  *   2 - C1 with invocation and backedge counting (CompLevel_limited_profile)
   767  *   2 - C1 with invocation and backedge counting (CompLevel_limited_profile)
   716  *   3 - C1 with full profiling (CompLevel_full_profile)
   768  *   3 - C1 with full profiling (CompLevel_full_profile)
   717  *   4 - C2 (CompLevel_full_optimization)
   769  *   4 - C2 or Graal (CompLevel_full_optimization)
   718  *
   770  *
   719  * Common state transition patterns:
   771  * Common state transition patterns:
   720  * a. 0 -> 3 -> 4.
   772  * a. 0 -> 3 -> 4.
   721  *    The most common path. But note that even in this straightforward case
   773  *    The most common path. But note that even in this straightforward case
   722  *    profiling can start at level 0 and finish at level 3.
   774  *    profiling can start at level 0 and finish at level 3.
   750 CompLevel TieredThresholdPolicy::common(Predicate p, Method* method, CompLevel cur_level, bool disable_feedback) {
   802 CompLevel TieredThresholdPolicy::common(Predicate p, Method* method, CompLevel cur_level, bool disable_feedback) {
   751   CompLevel next_level = cur_level;
   803   CompLevel next_level = cur_level;
   752   int i = method->invocation_count();
   804   int i = method->invocation_count();
   753   int b = method->backedge_count();
   805   int b = method->backedge_count();
   754 
   806 
   755   if (should_compile_at_level_simple(method)) {
   807   if (force_comp_at_level_simple(method)) {
   756     next_level = CompLevel_simple;
   808     next_level = CompLevel_simple;
   757   } else {
   809   } else {
   758     switch(cur_level) {
   810     if (!CompilationModeFlag::disable_intermediate() && is_trivial(method)) {
       
   811       next_level = CompLevel_simple;
       
   812     } else {
       
   813       switch(cur_level) {
   759       default: break;
   814       default: break;
   760       case CompLevel_aot: {
   815       case CompLevel_aot:
   761       // If we were at full profile level, would we switch to full opt?
   816         if (CompilationModeFlag::disable_intermediate()) {
   762       if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) {
   817           if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
   763         next_level = CompLevel_full_optimization;
   818                                    Tier0Delay * compiler_count(CompLevel_full_optimization) &&
   764       } else if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
   819                                   (this->*p)(i, b, cur_level, method))) {
   765                                Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
   820             next_level = CompLevel_none;
   766                                (this->*p)(i, b, cur_level, method))) {
   821           }
   767         next_level = CompLevel_full_profile;
   822         } else {
   768       }
   823           // If we were at full profile level, would we switch to full opt?
   769     }
   824           if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) {
   770     break;
   825             next_level = CompLevel_full_optimization;
   771     case CompLevel_none:
   826           } else if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
   772       // If we were at full profile level, would we switch to full opt?
   827                                           Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
   773       if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) {
   828                                          (this->*p)(i, b, cur_level, method))) {
   774         next_level = CompLevel_full_optimization;
       
   775       } else if ((this->*p)(i, b, cur_level, method)) {
       
   776 #if INCLUDE_JVMCI
       
   777         if (EnableJVMCI && UseJVMCICompiler) {
       
   778           // Since JVMCI takes a while to warm up, its queue inevitably backs up during
       
   779           // early VM execution. As of 2014-06-13, JVMCI's inliner assumes that the root
       
   780           // compilation method and all potential inlinees have mature profiles (which
       
   781           // includes type profiling). If it sees immature profiles, JVMCI's inliner
       
   782           // can perform pathologically bad (e.g., causing OutOfMemoryErrors due to
       
   783           // exploring/inlining too many graphs). Since a rewrite of the inliner is
       
   784           // in progress, we simply disable the dialing back heuristic for now and will
       
   785           // revisit this decision once the new inliner is completed.
       
   786           next_level = CompLevel_full_profile;
       
   787         } else
       
   788 #endif
       
   789         {
       
   790           // C1-generated fully profiled code is about 30% slower than the limited profile
       
   791           // code that has only invocation and backedge counters. The observation is that
       
   792           // if C2 queue is large enough we can spend too much time in the fully profiled code
       
   793           // while waiting for C2 to pick the method from the queue. To alleviate this problem
       
   794           // we introduce a feedback on the C2 queue size. If the C2 queue is sufficiently long
       
   795           // we choose to compile a limited profiled version and then recompile with full profiling
       
   796           // when the load on C2 goes down.
       
   797           if (!disable_feedback && CompileBroker::queue_size(CompLevel_full_optimization) >
       
   798               Tier3DelayOn * compiler_count(CompLevel_full_optimization)) {
       
   799             next_level = CompLevel_limited_profile;
       
   800           } else {
       
   801             next_level = CompLevel_full_profile;
   829             next_level = CompLevel_full_profile;
   802           }
   830           }
   803         }
   831         }
   804       }
   832         break;
   805       break;
   833       case CompLevel_none:
   806     case CompLevel_limited_profile:
   834         if (CompilationModeFlag::disable_intermediate()) {
   807       if (is_method_profiled(method)) {
   835           MethodData* mdo = method->method_data();
   808         // Special case: we got here because this method was fully profiled in the interpreter.
   836           if (mdo != NULL) {
   809         next_level = CompLevel_full_optimization;
   837             // If mdo exists that means we are in a normal profiling mode.
   810       } else {
   838             int mdo_i = mdo->invocation_count_delta();
   811         MethodData* mdo = method->method_data();
   839             int mdo_b = mdo->backedge_count_delta();
   812         if (mdo != NULL) {
   840             if ((this->*p)(mdo_i, mdo_b, cur_level, method)) {
   813           if (mdo->would_profile()) {
   841               next_level = CompLevel_full_optimization;
       
   842             }
       
   843           }
       
   844         } else {
       
   845           // If we were at full profile level, would we switch to full opt?
       
   846           if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) {
       
   847             next_level = CompLevel_full_optimization;
       
   848           } else if ((this->*p)(i, b, cur_level, method)) {
       
   849   #if INCLUDE_JVMCI
       
   850             if (EnableJVMCI && UseJVMCICompiler) {
       
   851               // Since JVMCI takes a while to warm up, its queue inevitably backs up during
       
   852               // early VM execution. As of 2014-06-13, JVMCI's inliner assumes that the root
       
   853               // compilation method and all potential inlinees have mature profiles (which
       
   854               // includes type profiling). If it sees immature profiles, JVMCI's inliner
       
   855               // can perform pathologically bad (e.g., causing OutOfMemoryErrors due to
       
   856               // exploring/inlining too many graphs). Since a rewrite of the inliner is
       
   857               // in progress, we simply disable the dialing back heuristic for now and will
       
   858               // revisit this decision once the new inliner is completed.
       
   859               next_level = CompLevel_full_profile;
       
   860             } else
       
   861   #endif
       
   862             {
       
   863               // C1-generated fully profiled code is about 30% slower than the limited profile
       
   864               // code that has only invocation and backedge counters. The observation is that
       
   865               // if C2 queue is large enough we can spend too much time in the fully profiled code
       
   866               // while waiting for C2 to pick the method from the queue. To alleviate this problem
       
   867               // we introduce a feedback on the C2 queue size. If the C2 queue is sufficiently long
       
   868               // we choose to compile a limited profiled version and then recompile with full profiling
       
   869               // when the load on C2 goes down.
       
   870               if (!disable_feedback && CompileBroker::queue_size(CompLevel_full_optimization) >
       
   871                   Tier3DelayOn * compiler_count(CompLevel_full_optimization)) {
       
   872                 next_level = CompLevel_limited_profile;
       
   873               } else {
       
   874                 next_level = CompLevel_full_profile;
       
   875               }
       
   876             }
       
   877           }
       
   878         }
       
   879         break;
       
   880       case CompLevel_limited_profile:
       
   881         if (is_method_profiled(method)) {
       
   882           // Special case: we got here because this method was fully profiled in the interpreter.
       
   883           next_level = CompLevel_full_optimization;
       
   884         } else {
       
   885           MethodData* mdo = method->method_data();
       
   886           if (mdo != NULL) {
       
   887             if (mdo->would_profile()) {
       
   888               if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
       
   889                                        Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
       
   890                                        (this->*p)(i, b, cur_level, method))) {
       
   891                 next_level = CompLevel_full_profile;
       
   892               }
       
   893             } else {
       
   894               next_level = CompLevel_full_optimization;
       
   895             }
       
   896           } else {
       
   897             // If there is no MDO we need to profile
   814             if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
   898             if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
   815                                      Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
   899                                      Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
   816                                      (this->*p)(i, b, cur_level, method))) {
   900                                      (this->*p)(i, b, cur_level, method))) {
   817               next_level = CompLevel_full_profile;
   901               next_level = CompLevel_full_profile;
   818             }
   902             }
   819           } else {
       
   820             next_level = CompLevel_full_optimization;
       
   821           }
   903           }
   822         } else {
   904         }
   823           // If there is no MDO we need to profile
   905         break;
   824           if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
   906       case CompLevel_full_profile:
   825                                    Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
   907         {
   826                                    (this->*p)(i, b, cur_level, method))) {
   908           MethodData* mdo = method->method_data();
   827             next_level = CompLevel_full_profile;
   909           if (mdo != NULL) {
   828           }
   910             if (mdo->would_profile()) {
   829         }
   911               int mdo_i = mdo->invocation_count_delta();
   830       }
   912               int mdo_b = mdo->backedge_count_delta();
   831       break;
   913               if ((this->*p)(mdo_i, mdo_b, cur_level, method)) {
   832     case CompLevel_full_profile:
   914                 next_level = CompLevel_full_optimization;
   833       {
   915               }
   834         MethodData* mdo = method->method_data();
   916             } else {
   835         if (mdo != NULL) {
       
   836           if (mdo->would_profile()) {
       
   837             int mdo_i = mdo->invocation_count_delta();
       
   838             int mdo_b = mdo->backedge_count_delta();
       
   839             if ((this->*p)(mdo_i, mdo_b, cur_level, method)) {
       
   840               next_level = CompLevel_full_optimization;
   917               next_level = CompLevel_full_optimization;
   841             }
   918             }
   842           } else {
       
   843             next_level = CompLevel_full_optimization;
       
   844           }
   919           }
   845         }
   920         }
   846       }
   921         break;
   847       break;
   922       }
   848     }
   923     }
   849   }
   924   }
   850   return MIN2(next_level, (CompLevel)TieredStopAtLevel);
   925   return MIN2(next_level, CompilationModeFlag::quick_only() ? CompLevel_simple : (CompLevel)TieredStopAtLevel);
   851 }
   926 }
   852 
   927 
   853 // Determine if a method should be compiled with a normal entry point at a different level.
   928 // Determine if a method should be compiled with a normal entry point at a different level.
   854 CompLevel TieredThresholdPolicy::call_event(Method* method, CompLevel cur_level, JavaThread * thread) {
   929 CompLevel TieredThresholdPolicy::call_event(Method* method, CompLevel cur_level, JavaThread* thread) {
   855   CompLevel osr_level = MIN2((CompLevel) method->highest_osr_comp_level(),
   930   CompLevel osr_level = MIN2((CompLevel) method->highest_osr_comp_level(),
   856                              common(&TieredThresholdPolicy::loop_predicate, method, cur_level, true));
   931                              common(&TieredThresholdPolicy::loop_predicate, method, cur_level, true));
   857   CompLevel next_level = common(&TieredThresholdPolicy::call_predicate, method, cur_level);
   932   CompLevel next_level = common(&TieredThresholdPolicy::call_predicate, method, cur_level);
   858 
   933 
   859   // If OSR method level is greater than the regular method level, the levels should be
   934   // If OSR method level is greater than the regular method level, the levels should be
   948     CompLevel cur_level, next_level;
  1023     CompLevel cur_level, next_level;
   949     if (mh() != imh()) { // If there is an enclosing method
  1024     if (mh() != imh()) { // If there is an enclosing method
   950       if (level == CompLevel_aot) {
  1025       if (level == CompLevel_aot) {
   951         // Recompile the enclosing method to prevent infinite OSRs. Stay at AOT level while it's compiling.
  1026         // Recompile the enclosing method to prevent infinite OSRs. Stay at AOT level while it's compiling.
   952         if (max_osr_level != CompLevel_none && !CompileBroker::compilation_is_in_queue(mh)) {
  1027         if (max_osr_level != CompLevel_none && !CompileBroker::compilation_is_in_queue(mh)) {
   953           compile(mh, InvocationEntryBci, MIN2((CompLevel)TieredStopAtLevel, CompLevel_full_profile), thread);
  1028           CompLevel enclosing_level = MIN2(CompilationModeFlag::quick_only() ? CompLevel_simple : (CompLevel)TieredStopAtLevel, CompLevel_full_profile);
       
  1029           compile(mh, InvocationEntryBci, enclosing_level, thread);
   954         }
  1030         }
   955       } else {
  1031       } else {
   956         // Current loop event level is not AOT
  1032         // Current loop event level is not AOT
   957         guarantee(nm != NULL, "Should have nmethod here");
  1033         guarantee(nm != NULL, "Should have nmethod here");
   958         cur_level = comp_level(mh());
  1034         cur_level = comp_level(mh());