1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 "precompiled.hpp"
  26 #include "code/codeCache.hpp"
  27 #include "runtime/advancedThresholdPolicy.hpp"
  28 #include "runtime/simpleThresholdPolicy.inline.hpp"
  29 
  30 #ifdef TIERED
  31 // Print an event.
  32 void AdvancedThresholdPolicy::print_specific(EventType type, methodHandle mh, methodHandle imh,
  33                                              int bci, CompLevel level) {
  34   tty->print(" rate=");
  35   if (mh->prev_time() == 0) tty->print("n/a");
  36   else tty->print("%f", mh->rate());
  37 
  38   tty->print(" k=%.2lf,%.2lf", threshold_scale(CompLevel_full_profile, Tier3LoadFeedback),
  39                                threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback));
  40 
  41 }
  42 
  43 void AdvancedThresholdPolicy::initialize() {
  44   // Turn on ergonomic compiler count selection
  45   if (FLAG_IS_DEFAULT(CICompilerCountPerCPU) && FLAG_IS_DEFAULT(CICompilerCount)) {
  46     FLAG_SET_DEFAULT(CICompilerCountPerCPU, true);
  47   }
  48   int count = CICompilerCount;
  49   if (CICompilerCountPerCPU) {
  50     // Simple log n seems to grow too slowly for tiered, try something faster: log n * log log n
  51     int log_cpu = log2_intptr(os::active_processor_count());
  52     int loglog_cpu = log2_intptr(MAX2(log_cpu, 1));
  53     count = MAX2(log_cpu * loglog_cpu, 1) * 3 / 2;
  54   }
  55 
  56   set_c1_count(MAX2(count / 3, 1));
  57   set_c2_count(MAX2(count - c1_count(), 1));
  58   FLAG_SET_ERGO(intx, CICompilerCount, c1_count() + c2_count());
  59 
  60   // Some inlining tuning
  61 #ifdef X86
  62   if (FLAG_IS_DEFAULT(InlineSmallCode)) {
  63     FLAG_SET_DEFAULT(InlineSmallCode, 2000);
  64   }
  65 #endif
  66 
  67 #if defined SPARC || defined AARCH64
  68   if (FLAG_IS_DEFAULT(InlineSmallCode)) {
  69     FLAG_SET_DEFAULT(InlineSmallCode, 2500);
  70   }
  71 #endif
  72 
  73   set_increase_threshold_at_ratio();
  74   set_start_time(os::javaTimeMillis());
  75 }
  76 
  77 // update_rate() is called from select_task() while holding a compile queue lock.
  78 void AdvancedThresholdPolicy::update_rate(jlong t, Method* m) {
  79   // Skip update if counters are absent.
  80   // Can't allocate them since we are holding compile queue lock.
  81   if (m->method_counters() == NULL)  return;
  82 
  83   if (is_old(m)) {
  84     // We don't remove old methods from the queue,
  85     // so we can just zero the rate.
  86     m->set_rate(0);
  87     return;
  88   }
  89 
  90   // We don't update the rate if we've just came out of a safepoint.
  91   // delta_s is the time since last safepoint in milliseconds.
  92   jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint();
  93   jlong delta_t = t - (m->prev_time() != 0 ? m->prev_time() : start_time()); // milliseconds since the last measurement
  94   // How many events were there since the last time?
  95   int event_count = m->invocation_count() + m->backedge_count();
  96   int delta_e = event_count - m->prev_event_count();
  97 
  98   // We should be running for at least 1ms.
  99   if (delta_s >= TieredRateUpdateMinTime) {
 100     // And we must've taken the previous point at least 1ms before.
 101     if (delta_t >= TieredRateUpdateMinTime && delta_e > 0) {
 102       m->set_prev_time(t);
 103       m->set_prev_event_count(event_count);
 104       m->set_rate((float)delta_e / (float)delta_t); // Rate is events per millisecond
 105     } else {
 106       if (delta_t > TieredRateUpdateMaxTime && delta_e == 0) {
 107         // If nothing happened for 25ms, zero the rate. Don't modify prev values.
 108         m->set_rate(0);
 109       }
 110     }
 111   }
 112 }
 113 
 114 // Check if this method has been stale from a given number of milliseconds.
 115 // See select_task().
 116 bool AdvancedThresholdPolicy::is_stale(jlong t, jlong timeout, Method* m) {
 117   jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint();
 118   jlong delta_t = t - m->prev_time();
 119   if (delta_t > timeout && delta_s > timeout) {
 120     int event_count = m->invocation_count() + m->backedge_count();
 121     int delta_e = event_count - m->prev_event_count();
 122     // Return true if there were no events.
 123     return delta_e == 0;
 124   }
 125   return false;
 126 }
 127 
 128 // We don't remove old methods from the compile queue even if they have
 129 // very low activity. See select_task().
 130 bool AdvancedThresholdPolicy::is_old(Method* method) {
 131   return method->invocation_count() > 50000 || method->backedge_count() > 500000;
 132 }
 133 
 134 double AdvancedThresholdPolicy::weight(Method* method) {
 135   return (method->rate() + 1) * ((method->invocation_count() + 1) *  (method->backedge_count() + 1));
 136 }
 137 
 138 // Apply heuristics and return true if x should be compiled before y
 139 bool AdvancedThresholdPolicy::compare_methods(Method* x, Method* y) {
 140   if (x->highest_comp_level() > y->highest_comp_level()) {
 141     // recompilation after deopt
 142     return true;
 143   } else
 144     if (x->highest_comp_level() == y->highest_comp_level()) {
 145       if (weight(x) > weight(y)) {
 146         return true;
 147       }
 148     }
 149   return false;
 150 }
 151 
 152 // Is method profiled enough?
 153 bool AdvancedThresholdPolicy::is_method_profiled(Method* method) {
 154   MethodData* mdo = method->method_data();
 155   if (mdo != NULL) {
 156     int i = mdo->invocation_count_delta();
 157     int b = mdo->backedge_count_delta();
 158     return call_predicate_helper<CompLevel_full_profile>(i, b, 1, method);
 159   }
 160   return false;
 161 }
 162 
 163 // Called with the queue locked and with at least one element
 164 CompileTask* AdvancedThresholdPolicy::select_task(CompileQueue* compile_queue) {
 165   CompileTask *max_task = NULL;
 166   Method* max_method = NULL;
 167   jlong t = os::javaTimeMillis();
 168   // Iterate through the queue and find a method with a maximum rate.
 169   for (CompileTask* task = compile_queue->first(); task != NULL;) {
 170     CompileTask* next_task = task->next();
 171     Method* method = task->method();
 172     update_rate(t, method);
 173     if (max_task == NULL) {
 174       max_task = task;
 175       max_method = method;
 176     } else {
 177       // If a method has been stale for some time, remove it from the queue.
 178       if (is_stale(t, TieredCompileTaskTimeout, method) && !is_old(method)) {
 179         if (PrintTieredEvents) {
 180           print_event(REMOVE_FROM_QUEUE, method, method, task->osr_bci(), (CompLevel)task->comp_level());
 181         }
 182         compile_queue->remove_and_mark_stale(task);
 183         method->clear_queued_for_compilation();
 184         task = next_task;
 185         continue;
 186       }
 187 
 188       // Select a method with a higher rate
 189       if (compare_methods(method, max_method)) {
 190         max_task = task;
 191         max_method = method;
 192       }
 193     }
 194     task = next_task;
 195   }
 196 
 197   if (max_task->comp_level() == CompLevel_full_profile && TieredStopAtLevel > CompLevel_full_profile
 198       && is_method_profiled(max_method)) {
 199     max_task->set_comp_level(CompLevel_limited_profile);
 200     if (PrintTieredEvents) {
 201       print_event(UPDATE_IN_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level());
 202     }
 203   }
 204 
 205   return max_task;
 206 }
 207 
 208 double AdvancedThresholdPolicy::threshold_scale(CompLevel level, int feedback_k) {
 209   double queue_size = CompileBroker::queue_size(level);
 210   int comp_count = compiler_count(level);
 211   double k = queue_size / (feedback_k * comp_count) + 1;
 212 
 213   // Increase C1 compile threshold when the code cache is filled more
 214   // than specified by IncreaseFirstTierCompileThresholdAt percentage.
 215   // The main intention is to keep enough free space for C2 compiled code
 216   // to achieve peak performance if the code cache is under stress.
 217   if ((TieredStopAtLevel == CompLevel_full_optimization) && (level != CompLevel_full_optimization))  {
 218     double current_reverse_free_ratio = CodeCache::reverse_free_ratio(CodeCache::get_code_blob_type(level));
 219     if (current_reverse_free_ratio > _increase_threshold_at_ratio) {
 220       k *= exp(current_reverse_free_ratio - _increase_threshold_at_ratio);
 221     }
 222   }
 223   return k;
 224 }
 225 
 226 // Call and loop predicates determine whether a transition to a higher
 227 // compilation level should be performed (pointers to predicate functions
 228 // are passed to common()).
 229 // Tier?LoadFeedback is basically a coefficient that determines of
 230 // how many methods per compiler thread can be in the queue before
 231 // the threshold values double.
 232 bool AdvancedThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level, Method* method) {
 233   switch(cur_level) {
 234   case CompLevel_none:
 235   case CompLevel_limited_profile: {
 236     double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
 237     return loop_predicate_helper<CompLevel_none>(i, b, k, method);
 238   }
 239   case CompLevel_full_profile: {
 240     double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
 241     return loop_predicate_helper<CompLevel_full_profile>(i, b, k, method);
 242   }
 243   default:
 244     return true;
 245   }
 246 }
 247 
 248 bool AdvancedThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level, Method* method) {
 249   switch(cur_level) {
 250   case CompLevel_none:
 251   case CompLevel_limited_profile: {
 252     double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback);
 253     return call_predicate_helper<CompLevel_none>(i, b, k, method);
 254   }
 255   case CompLevel_full_profile: {
 256     double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback);
 257     return call_predicate_helper<CompLevel_full_profile>(i, b, k, method);
 258   }
 259   default:
 260     return true;
 261   }
 262 }
 263 
 264 // If a method is old enough and is still in the interpreter we would want to
 265 // start profiling without waiting for the compiled method to arrive.
 266 // We also take the load on compilers into the account.
 267 bool AdvancedThresholdPolicy::should_create_mdo(Method* method, CompLevel cur_level) {
 268   if (cur_level == CompLevel_none &&
 269       CompileBroker::queue_size(CompLevel_full_optimization) <=
 270       Tier3DelayOn * compiler_count(CompLevel_full_optimization)) {
 271     int i = method->invocation_count();
 272     int b = method->backedge_count();
 273     double k = Tier0ProfilingStartPercentage / 100.0;
 274     return call_predicate_helper<CompLevel_none>(i, b, k, method) || loop_predicate_helper<CompLevel_none>(i, b, k, method);
 275   }
 276   return false;
 277 }
 278 
 279 // Inlining control: if we're compiling a profiled method with C1 and the callee
 280 // is known to have OSRed in a C2 version, don't inline it.
 281 bool AdvancedThresholdPolicy::should_not_inline(ciEnv* env, ciMethod* callee) {
 282   CompLevel comp_level = (CompLevel)env->comp_level();
 283   if (comp_level == CompLevel_full_profile ||
 284       comp_level == CompLevel_limited_profile) {
 285     return callee->highest_osr_comp_level() == CompLevel_full_optimization;
 286   }
 287   return false;
 288 }
 289 
 290 // Create MDO if necessary.
 291 void AdvancedThresholdPolicy::create_mdo(methodHandle mh, JavaThread* THREAD) {
 292   if (mh->is_native() ||
 293       mh->is_abstract() ||
 294       mh->is_accessor() ||
 295       mh->is_constant_getter()) {
 296     return;
 297   }
 298   if (mh->method_data() == NULL) {
 299     Method::build_interpreter_method_data(mh, CHECK_AND_CLEAR);
 300   }
 301 }
 302 
 303 
 304 /*
 305  * Method states:
 306  *   0 - interpreter (CompLevel_none)
 307  *   1 - pure C1 (CompLevel_simple)
 308  *   2 - C1 with invocation and backedge counting (CompLevel_limited_profile)
 309  *   3 - C1 with full profiling (CompLevel_full_profile)
 310  *   4 - C2 (CompLevel_full_optimization)
 311  *
 312  * Common state transition patterns:
 313  * a. 0 -> 3 -> 4.
 314  *    The most common path. But note that even in this straightforward case
 315  *    profiling can start at level 0 and finish at level 3.
 316  *
 317  * b. 0 -> 2 -> 3 -> 4.
 318  *    This case occurs when the load on C2 is deemed too high. So, instead of transitioning
 319  *    into state 3 directly and over-profiling while a method is in the C2 queue we transition to
 320  *    level 2 and wait until the load on C2 decreases. This path is disabled for OSRs.
 321  *
 322  * c. 0 -> (3->2) -> 4.
 323  *    In this case we enqueue a method for compilation at level 3, but the C1 queue is long enough
 324  *    to enable the profiling to fully occur at level 0. In this case we change the compilation level
 325  *    of the method to 2 while the request is still in-queue, because it'll allow it to run much faster
 326  *    without full profiling while c2 is compiling.
 327  *
 328  * d. 0 -> 3 -> 1 or 0 -> 2 -> 1.
 329  *    After a method was once compiled with C1 it can be identified as trivial and be compiled to
 330  *    level 1. These transition can also occur if a method can't be compiled with C2 but can with C1.
 331  *
 332  * e. 0 -> 4.
 333  *    This can happen if a method fails C1 compilation (it will still be profiled in the interpreter)
 334  *    or because of a deopt that didn't require reprofiling (compilation won't happen in this case because
 335  *    the compiled version already exists).
 336  *
 337  * Note that since state 0 can be reached from any other state via deoptimization different loops
 338  * are possible.
 339  *
 340  */
 341 
 342 // Common transition function. Given a predicate determines if a method should transition to another level.
 343 CompLevel AdvancedThresholdPolicy::common(Predicate p, Method* method, CompLevel cur_level, bool disable_feedback) {
 344   CompLevel next_level = cur_level;
 345   int i = method->invocation_count();
 346   int b = method->backedge_count();
 347 
 348   if (is_trivial(method)) {
 349     next_level = CompLevel_simple;
 350   } else {
 351     switch(cur_level) {
 352     case CompLevel_none:
 353       // If we were at full profile level, would we switch to full opt?
 354       if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) {
 355         next_level = CompLevel_full_optimization;
 356       } else if ((this->*p)(i, b, cur_level, method)) {
 357         // C1-generated fully profiled code is about 30% slower than the limited profile
 358         // code that has only invocation and backedge counters. The observation is that
 359         // if C2 queue is large enough we can spend too much time in the fully profiled code
 360         // while waiting for C2 to pick the method from the queue. To alleviate this problem
 361         // we introduce a feedback on the C2 queue size. If the C2 queue is sufficiently long
 362         // we choose to compile a limited profiled version and then recompile with full profiling
 363         // when the load on C2 goes down.
 364         if (!disable_feedback && CompileBroker::queue_size(CompLevel_full_optimization) >
 365                                  Tier3DelayOn * compiler_count(CompLevel_full_optimization)) {
 366           next_level = CompLevel_limited_profile;
 367         } else {
 368           next_level = CompLevel_full_profile;
 369         }
 370       }
 371       break;
 372     case CompLevel_limited_profile:
 373       if (is_method_profiled(method)) {
 374         // Special case: we got here because this method was fully profiled in the interpreter.
 375         next_level = CompLevel_full_optimization;
 376       } else {
 377         MethodData* mdo = method->method_data();
 378         if (mdo != NULL) {
 379           if (mdo->would_profile()) {
 380             if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <=
 381                                      Tier3DelayOff * compiler_count(CompLevel_full_optimization) &&
 382                                      (this->*p)(i, b, cur_level, method))) {
 383               next_level = CompLevel_full_profile;
 384             }
 385           } else {
 386             next_level = CompLevel_full_optimization;
 387           }
 388         }
 389       }
 390       break;
 391     case CompLevel_full_profile:
 392       {
 393         MethodData* mdo = method->method_data();
 394         if (mdo != NULL) {
 395           if (mdo->would_profile()) {
 396             int mdo_i = mdo->invocation_count_delta();
 397             int mdo_b = mdo->backedge_count_delta();
 398             if ((this->*p)(mdo_i, mdo_b, cur_level, method)) {
 399               next_level = CompLevel_full_optimization;
 400             }
 401           } else {
 402             next_level = CompLevel_full_optimization;
 403           }
 404         }
 405       }
 406       break;
 407     }
 408   }
 409   return MIN2(next_level, (CompLevel)TieredStopAtLevel);
 410 }
 411 
 412 // Determine if a method should be compiled with a normal entry point at a different level.
 413 CompLevel AdvancedThresholdPolicy::call_event(Method* method, CompLevel cur_level) {
 414   CompLevel osr_level = MIN2((CompLevel) method->highest_osr_comp_level(),
 415                              common(&AdvancedThresholdPolicy::loop_predicate, method, cur_level, true));
 416   CompLevel next_level = common(&AdvancedThresholdPolicy::call_predicate, method, cur_level);
 417 
 418   // If OSR method level is greater than the regular method level, the levels should be
 419   // equalized by raising the regular method level in order to avoid OSRs during each
 420   // invocation of the method.
 421   if (osr_level == CompLevel_full_optimization && cur_level == CompLevel_full_profile) {
 422     MethodData* mdo = method->method_data();
 423     guarantee(mdo != NULL, "MDO should not be NULL");
 424     if (mdo->invocation_count() >= 1) {
 425       next_level = CompLevel_full_optimization;
 426     }
 427   } else {
 428     next_level = MAX2(osr_level, next_level);
 429   }
 430   return next_level;
 431 }
 432 
 433 // Determine if we should do an OSR compilation of a given method.
 434 CompLevel AdvancedThresholdPolicy::loop_event(Method* method, CompLevel cur_level) {
 435   CompLevel next_level = common(&AdvancedThresholdPolicy::loop_predicate, method, cur_level, true);
 436   if (cur_level == CompLevel_none) {
 437     // If there is a live OSR method that means that we deopted to the interpreter
 438     // for the transition.
 439     CompLevel osr_level = MIN2((CompLevel)method->highest_osr_comp_level(), next_level);
 440     if (osr_level > CompLevel_none) {
 441       return osr_level;
 442     }
 443   }
 444   return next_level;
 445 }
 446 
 447 // Update the rate and submit compile
 448 void AdvancedThresholdPolicy::submit_compile(methodHandle mh, int bci, CompLevel level, JavaThread* thread) {
 449   int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
 450   update_rate(os::javaTimeMillis(), mh());
 451   CompileBroker::compile_method(mh, bci, level, mh, hot_count, "tiered", thread);
 452 }
 453 
 454 // Handle the invocation event.
 455 void AdvancedThresholdPolicy::method_invocation_event(methodHandle mh, methodHandle imh,
 456                                                       CompLevel level, nmethod* nm, JavaThread* thread) {
 457   if (should_create_mdo(mh(), level)) {
 458     create_mdo(mh, thread);
 459   }
 460   if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh)) {
 461     CompLevel next_level = call_event(mh(), level);
 462     if (next_level != level) {
 463       compile(mh, InvocationEntryBci, next_level, thread);
 464     }
 465   }
 466 }
 467 
 468 // Handle the back branch event. Notice that we can compile the method
 469 // with a regular entry from here.
 470 void AdvancedThresholdPolicy::method_back_branch_event(methodHandle mh, methodHandle imh,
 471                                                        int bci, CompLevel level, nmethod* nm, JavaThread* thread) {
 472   if (should_create_mdo(mh(), level)) {
 473     create_mdo(mh, thread);
 474   }
 475   // Check if MDO should be created for the inlined method
 476   if (should_create_mdo(imh(), level)) {
 477     create_mdo(imh, thread);
 478   }
 479 
 480   if (is_compilation_enabled()) {
 481     CompLevel next_osr_level = loop_event(imh(), level);
 482     CompLevel max_osr_level = (CompLevel)imh->highest_osr_comp_level();
 483     // At the very least compile the OSR version
 484     if (!CompileBroker::compilation_is_in_queue(imh) && (next_osr_level != level)) {
 485       compile(imh, bci, next_osr_level, thread);
 486     }
 487 
 488     // Use loop event as an opportunity to also check if there's been
 489     // enough calls.
 490     CompLevel cur_level, next_level;
 491     if (mh() != imh()) { // If there is an enclosing method
 492       guarantee(nm != NULL, "Should have nmethod here");
 493       cur_level = comp_level(mh());
 494       next_level = call_event(mh(), cur_level);
 495 
 496       if (max_osr_level == CompLevel_full_optimization) {
 497         // The inlinee OSRed to full opt, we need to modify the enclosing method to avoid deopts
 498         bool make_not_entrant = false;
 499         if (nm->is_osr_method()) {
 500           // This is an osr method, just make it not entrant and recompile later if needed
 501           make_not_entrant = true;
 502         } else {
 503           if (next_level != CompLevel_full_optimization) {
 504             // next_level is not full opt, so we need to recompile the
 505             // enclosing method without the inlinee
 506             cur_level = CompLevel_none;
 507             make_not_entrant = true;
 508           }
 509         }
 510         if (make_not_entrant) {
 511           if (PrintTieredEvents) {
 512             int osr_bci = nm->is_osr_method() ? nm->osr_entry_bci() : InvocationEntryBci;
 513             print_event(MAKE_NOT_ENTRANT, mh(), mh(), osr_bci, level);
 514           }
 515           nm->make_not_entrant();
 516         }
 517       }
 518       if (!CompileBroker::compilation_is_in_queue(mh)) {
 519         // Fix up next_level if necessary to avoid deopts
 520         if (next_level == CompLevel_limited_profile && max_osr_level == CompLevel_full_profile) {
 521           next_level = CompLevel_full_profile;
 522         }
 523         if (cur_level != next_level) {
 524           compile(mh, InvocationEntryBci, next_level, thread);
 525         }
 526       }
 527     } else {
 528       cur_level = comp_level(imh());
 529       next_level = call_event(imh(), cur_level);
 530       if (!CompileBroker::compilation_is_in_queue(imh) && (next_level != cur_level)) {
 531         compile(imh, InvocationEntryBci, next_level, thread);
 532       }
 533     }
 534   }
 535 }
 536 
 537 #endif // TIERED