1 /*
   2  * Copyright (c) 2010, 2017, 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 "compiler/compileBroker.hpp"
  27 #include "gc/shared/gcLocker.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "runtime/arguments.hpp"
  30 #include "runtime/handles.inline.hpp"
  31 #include "runtime/simpleThresholdPolicy.hpp"
  32 #include "runtime/simpleThresholdPolicy.inline.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #if INCLUDE_JVMCI
  35 #include "jvmci/jvmciRuntime.hpp"
  36 #endif
  37 
  38 #ifdef TIERED
  39 
  40 void SimpleThresholdPolicy::print_counters(const char* prefix, const methodHandle& mh) {
  41   int invocation_count = mh->invocation_count();
  42   int backedge_count = mh->backedge_count();
  43   MethodData* mdh = mh->method_data();
  44   int mdo_invocations = 0, mdo_backedges = 0;
  45   int mdo_invocations_start = 0, mdo_backedges_start = 0;
  46   if (mdh != NULL) {
  47     mdo_invocations = mdh->invocation_count();
  48     mdo_backedges = mdh->backedge_count();
  49     mdo_invocations_start = mdh->invocation_count_start();
  50     mdo_backedges_start = mdh->backedge_count_start();
  51   }
  52   tty->print(" %stotal=%d,%d %smdo=%d(%d),%d(%d)", prefix,
  53       invocation_count, backedge_count, prefix,
  54       mdo_invocations, mdo_invocations_start,
  55       mdo_backedges, mdo_backedges_start);
  56   tty->print(" %smax levels=%d,%d", prefix,
  57       mh->highest_comp_level(), mh->highest_osr_comp_level());
  58 }
  59 
  60 // Print an event.
  61 void SimpleThresholdPolicy::print_event(EventType type, const methodHandle& mh, const methodHandle& imh,
  62                                         int bci, CompLevel level) {
  63   bool inlinee_event = mh() != imh();
  64 
  65   ttyLocker tty_lock;
  66   tty->print("%lf: [", os::elapsedTime());
  67 
  68   switch(type) {
  69   case CALL:
  70     tty->print("call");
  71     break;
  72   case LOOP:
  73     tty->print("loop");
  74     break;
  75   case COMPILE:
  76     tty->print("compile");
  77     break;
  78   case REMOVE_FROM_QUEUE:
  79     tty->print("remove-from-queue");
  80     break;
  81   case UPDATE_IN_QUEUE:
  82     tty->print("update-in-queue");
  83     break;
  84   case REPROFILE:
  85     tty->print("reprofile");
  86     break;
  87   case MAKE_NOT_ENTRANT:
  88     tty->print("make-not-entrant");
  89     break;
  90   default:
  91     tty->print("unknown");
  92   }
  93 
  94   tty->print(" level=%d ", level);
  95 
  96   ResourceMark rm;
  97   char *method_name = mh->name_and_sig_as_C_string();
  98   tty->print("[%s", method_name);
  99   if (inlinee_event) {
 100     char *inlinee_name = imh->name_and_sig_as_C_string();
 101     tty->print(" [%s]] ", inlinee_name);
 102   }
 103   else tty->print("] ");
 104   tty->print("@%d queues=%d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile),
 105                                       CompileBroker::queue_size(CompLevel_full_optimization));
 106 
 107   print_specific(type, mh, imh, bci, level);
 108 
 109   if (type != COMPILE) {
 110     print_counters("", mh);
 111     if (inlinee_event) {
 112       print_counters("inlinee ", imh);
 113     }
 114     tty->print(" compilable=");
 115     bool need_comma = false;
 116     if (!mh->is_not_compilable(CompLevel_full_profile)) {
 117       tty->print("c1");
 118       need_comma = true;
 119     }
 120     if (!mh->is_not_osr_compilable(CompLevel_full_profile)) {
 121       if (need_comma) tty->print(",");
 122       tty->print("c1-osr");
 123       need_comma = true;
 124     }
 125     if (!mh->is_not_compilable(CompLevel_full_optimization)) {
 126       if (need_comma) tty->print(",");
 127       tty->print("c2");
 128       need_comma = true;
 129     }
 130     if (!mh->is_not_osr_compilable(CompLevel_full_optimization)) {
 131       if (need_comma) tty->print(",");
 132       tty->print("c2-osr");
 133     }
 134     tty->print(" status=");
 135     if (mh->queued_for_compilation()) {
 136       tty->print("in-queue");
 137     } else tty->print("idle");
 138   }
 139   tty->print_cr("]");
 140 }
 141 
 142 void SimpleThresholdPolicy::initialize() {
 143   if (FLAG_IS_DEFAULT(CICompilerCount)) {
 144     FLAG_SET_DEFAULT(CICompilerCount, 3);
 145   }
 146   int count = CICompilerCount;
 147 #ifdef _LP64
 148   // On 64-bit systems, scale the number of compiler threads with
 149   // the number of cores available on the system. Scaling is not
 150   // performed on 32-bit systems because it can lead to exhaustion
 151   // of the virtual memory address space available to the JVM.
 152   if (CICompilerCountPerCPU) {
 153     count = MAX2(log2_intptr(os::active_processor_count()) * 3 / 2, 2);
 154     FLAG_SET_ERGO(intx, CICompilerCount, count);
 155   }
 156 #endif
 157   if (TieredStopAtLevel < CompLevel_full_optimization) {
 158     // No C2 compiler thread required
 159     set_c1_count(count);
 160   } else {
 161     set_c1_count(MAX2(count / 3, 1));
 162     set_c2_count(MAX2(count - c1_count(), 1));
 163   }
 164   assert(count == c1_count() + c2_count(), "inconsistent compiler thread count");
 165 }
 166 
 167 void SimpleThresholdPolicy::set_carry_if_necessary(InvocationCounter *counter) {
 168   if (!counter->carry() && counter->count() > InvocationCounter::count_limit / 2) {
 169     counter->set_carry_flag();
 170   }
 171 }
 172 
 173 // Set carry flags on the counters if necessary
 174 void SimpleThresholdPolicy::handle_counter_overflow(Method* method) {
 175   MethodCounters *mcs = method->method_counters();
 176   if (mcs != NULL) {
 177     set_carry_if_necessary(mcs->invocation_counter());
 178     set_carry_if_necessary(mcs->backedge_counter());
 179   }
 180   MethodData* mdo = method->method_data();
 181   if (mdo != NULL) {
 182     set_carry_if_necessary(mdo->invocation_counter());
 183     set_carry_if_necessary(mdo->backedge_counter());
 184   }
 185 }
 186 
 187 // Called with the queue locked and with at least one element
 188 CompileTask* SimpleThresholdPolicy::select_task(CompileQueue* compile_queue) {
 189   return select_task_helper(compile_queue);
 190 }
 191 
 192 void SimpleThresholdPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
 193   for (ScopeDesc* sd = trap_scope;; sd = sd->sender()) {
 194     if (PrintTieredEvents) {
 195       methodHandle mh(sd->method());
 196       print_event(REPROFILE, mh, mh, InvocationEntryBci, CompLevel_none);
 197     }
 198     MethodData* mdo = sd->method()->method_data();
 199     if (mdo != NULL) {
 200       mdo->reset_start_counters();
 201     }
 202     if (sd->is_top()) break;
 203   }
 204 }
 205 
 206 nmethod* SimpleThresholdPolicy::event(const methodHandle& method, const methodHandle& inlinee,
 207                                       int branch_bci, int bci, CompLevel comp_level, CompiledMethod* nm, JavaThread* thread) {
 208   if (comp_level == CompLevel_none &&
 209       JvmtiExport::can_post_interpreter_events() &&
 210       thread->is_interp_only_mode()) {
 211     return NULL;
 212   }
 213   if (CompileTheWorld || ReplayCompiles) {
 214     // Don't trigger other compiles in testing mode
 215     return NULL;
 216   }
 217 
 218   handle_counter_overflow(method());
 219   if (method() != inlinee()) {
 220     handle_counter_overflow(inlinee());
 221   }
 222 
 223   if (PrintTieredEvents) {
 224     print_event(bci == InvocationEntryBci ? CALL : LOOP, method, inlinee, bci, comp_level);
 225   }
 226 
 227   if (bci == InvocationEntryBci) {
 228     method_invocation_event(method, inlinee, comp_level, nm, thread);
 229   } else {
 230     // method == inlinee if the event originated in the main method
 231     method_back_branch_event(method, inlinee, bci, comp_level, nm, thread);
 232     // Check if event led to a higher level OSR compilation
 233     nmethod* osr_nm = inlinee->lookup_osr_nmethod_for(bci, comp_level, false);
 234     if (osr_nm != NULL && osr_nm->comp_level() > comp_level) {
 235       // Perform OSR with new nmethod
 236       return osr_nm;
 237     }
 238   }
 239   return NULL;
 240 }
 241 
 242 // Check if the method can be compiled, change level if necessary
 243 void SimpleThresholdPolicy::compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
 244   assert(level <= TieredStopAtLevel, "Invalid compilation level");
 245   if (level == CompLevel_none) {
 246     return;
 247   }
 248   if (level == CompLevel_aot) {
 249     if (mh->has_aot_code()) {
 250       if (PrintTieredEvents) {
 251         print_event(COMPILE, mh, mh, bci, level);
 252       }
 253       MutexLocker ml(Compile_lock);
 254       NoSafepointVerifier nsv;
 255       if (mh->has_aot_code() && mh->code() != mh->aot_code()) {
 256         mh->aot_code()->make_entrant();
 257         if (mh->has_compiled_code()) {
 258           mh->code()->make_not_entrant();
 259         }
 260         Method::set_code(mh, mh->aot_code());
 261       }
 262     }
 263     return;
 264   }
 265 
 266   // Check if the method can be compiled. If it cannot be compiled with C1, continue profiling
 267   // in the interpreter and then compile with C2 (the transition function will request that,
 268   // see common() ). If the method cannot be compiled with C2 but still can with C1, compile it with
 269   // pure C1.
 270   if (!can_be_compiled(mh, level)) {
 271     if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) {
 272         compile(mh, bci, CompLevel_simple, thread);
 273     }
 274     return;
 275   }
 276   if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) {
 277     return;
 278   }
 279   if (!CompileBroker::compilation_is_in_queue(mh)) {
 280     if (PrintTieredEvents) {
 281       print_event(COMPILE, mh, mh, bci, level);
 282     }
 283     submit_compile(mh, bci, level, thread);
 284   }
 285 }
 286 
 287 // Tell the broker to compile the method
 288 void SimpleThresholdPolicy::submit_compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
 289   int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
 290   CompileBroker::compile_method(mh, bci, level, mh, hot_count, CompileTask::Reason_Tiered, thread);
 291 }
 292 
 293 // Call and loop predicates determine whether a transition to a higher
 294 // compilation level should be performed (pointers to predicate functions
 295 // are passed to common() transition function).
 296 bool SimpleThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level, Method* method) {
 297   switch(cur_level) {
 298   case CompLevel_aot: {
 299     return loop_predicate_helper<CompLevel_aot>(i, b, 1.0, method);
 300   }
 301   case CompLevel_none:
 302   case CompLevel_limited_profile: {
 303     return loop_predicate_helper<CompLevel_none>(i, b, 1.0, method);
 304   }
 305   case CompLevel_full_profile: {
 306     return loop_predicate_helper<CompLevel_full_profile>(i, b, 1.0, method);
 307   }
 308   default:
 309     return true;
 310   }
 311 }
 312 
 313 bool SimpleThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level, Method* method) {
 314   switch(cur_level) {
 315   case CompLevel_aot: {
 316     return call_predicate_helper<CompLevel_aot>(i, b, 1.0, method);
 317   }
 318   case CompLevel_none:
 319   case CompLevel_limited_profile: {
 320     return call_predicate_helper<CompLevel_none>(i, b, 1.0, method);
 321   }
 322   case CompLevel_full_profile: {
 323     return call_predicate_helper<CompLevel_full_profile>(i, b, 1.0, method);
 324   }
 325   default:
 326     return true;
 327   }
 328 }
 329 
 330 // Determine is a method is mature.
 331 bool SimpleThresholdPolicy::is_mature(Method* method) {
 332   if (is_trivial(method)) return true;
 333   MethodData* mdo = method->method_data();
 334   if (mdo != NULL) {
 335     int i = mdo->invocation_count();
 336     int b = mdo->backedge_count();
 337     double k = ProfileMaturityPercentage / 100.0;
 338     return call_predicate_helper<CompLevel_full_profile>(i, b, k, method) ||
 339            loop_predicate_helper<CompLevel_full_profile>(i, b, k, method);
 340   }
 341   return false;
 342 }
 343 
 344 // Common transition function. Given a predicate determines if a method should transition to another level.
 345 CompLevel SimpleThresholdPolicy::common(Predicate p, Method* method, CompLevel cur_level) {
 346   CompLevel next_level = cur_level;
 347   int i = method->invocation_count();
 348   int b = method->backedge_count();
 349 
 350   if (is_trivial(method) && cur_level != CompLevel_aot) {
 351     next_level = CompLevel_simple;
 352   } else {
 353     switch(cur_level) {
 354     case CompLevel_aot: {
 355       if ((this->*p)(i, b, cur_level, method)) {
 356         next_level = CompLevel_full_profile;
 357       }
 358     }
 359     break;
 360     case CompLevel_none:
 361       // If we were at full profile level, would we switch to full opt?
 362       if (common(p, method, CompLevel_full_profile) == CompLevel_full_optimization) {
 363         next_level = CompLevel_full_optimization;
 364       } else if ((this->*p)(i, b, cur_level, method)) {
 365         next_level = CompLevel_full_profile;
 366       }
 367       break;
 368     case CompLevel_limited_profile:
 369     case CompLevel_full_profile:
 370       {
 371         MethodData* mdo = method->method_data();
 372         if (mdo != NULL) {
 373           if (mdo->would_profile()) {
 374             int mdo_i = mdo->invocation_count_delta();
 375             int mdo_b = mdo->backedge_count_delta();
 376             if ((this->*p)(mdo_i, mdo_b, cur_level, method)) {
 377               next_level = CompLevel_full_optimization;
 378             }
 379           } else {
 380             next_level = CompLevel_full_optimization;
 381           }
 382         }
 383       }
 384       break;
 385     default:
 386       break;
 387     }
 388   }
 389   return MIN2(next_level, (CompLevel)TieredStopAtLevel);
 390 }
 391 
 392 // Determine if a method should be compiled with a normal entry point at a different level.
 393 CompLevel SimpleThresholdPolicy::call_event(Method* method,  CompLevel cur_level, JavaThread* thread) {
 394   CompLevel osr_level = MIN2((CompLevel) method->highest_osr_comp_level(),
 395                              common(&SimpleThresholdPolicy::loop_predicate, method, cur_level));
 396   CompLevel next_level = common(&SimpleThresholdPolicy::call_predicate, method, cur_level);
 397 
 398   // If OSR method level is greater than the regular method level, the levels should be
 399   // equalized by raising the regular method level in order to avoid OSRs during each
 400   // invocation of the method.
 401   if (osr_level == CompLevel_full_optimization && cur_level == CompLevel_full_profile) {
 402     MethodData* mdo = method->method_data();
 403     guarantee(mdo != NULL, "MDO should not be NULL");
 404     if (mdo->invocation_count() >= 1) {
 405       next_level = CompLevel_full_optimization;
 406     }
 407   } else {
 408     next_level = MAX2(osr_level, next_level);
 409   }
 410 #if INCLUDE_JVMCI
 411   if (UseJVMCICompiler) {
 412     next_level = JVMCIRuntime::adjust_comp_level(method, false, next_level, thread);
 413   }
 414 #endif
 415   return next_level;
 416 }
 417 
 418 // Determine if we should do an OSR compilation of a given method.
 419 CompLevel SimpleThresholdPolicy::loop_event(Method* method, CompLevel cur_level, JavaThread* thread) {
 420   CompLevel next_level = common(&SimpleThresholdPolicy::loop_predicate, method, cur_level);
 421   if (cur_level == CompLevel_none) {
 422     // If there is a live OSR method that means that we deopted to the interpreter
 423     // for the transition.
 424     CompLevel osr_level = MIN2((CompLevel)method->highest_osr_comp_level(), next_level);
 425     if (osr_level > CompLevel_none) {
 426       return osr_level;
 427     }
 428   }
 429 #if INCLUDE_JVMCI
 430   if (UseJVMCICompiler) {
 431     next_level = JVMCIRuntime::adjust_comp_level(method, true, next_level, thread);
 432   }
 433 #endif
 434   return next_level;
 435 }
 436 
 437 
 438 // Handle the invocation event.
 439 void SimpleThresholdPolicy::method_invocation_event(const methodHandle& mh, const methodHandle& imh,
 440                                               CompLevel level, CompiledMethod* nm, JavaThread* thread) {
 441   if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh)) {
 442     CompLevel next_level = call_event(mh(), level, thread);
 443     if (next_level != level) {
 444       compile(mh, InvocationEntryBci, next_level, thread);
 445     }
 446   }
 447 }
 448 
 449 // Handle the back branch event. Notice that we can compile the method
 450 // with a regular entry from here.
 451 void SimpleThresholdPolicy::method_back_branch_event(const methodHandle& mh, const methodHandle& imh,
 452                                                      int bci, CompLevel level, CompiledMethod* nm, JavaThread* thread) {
 453   // If the method is already compiling, quickly bail out.
 454   if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh)) {
 455     // Use loop event as an opportunity to also check there's been
 456     // enough calls.
 457     CompLevel cur_level = comp_level(mh());
 458     CompLevel next_level = call_event(mh(), cur_level, thread);
 459     CompLevel next_osr_level = loop_event(mh(), level, thread);
 460 
 461     next_level = MAX2(next_level,
 462                       next_osr_level < CompLevel_full_optimization ? next_osr_level : cur_level);
 463     bool is_compiling = false;
 464     if (next_level != cur_level) {
 465       compile(mh, InvocationEntryBci, next_level, thread);
 466       is_compiling = true;
 467     }
 468 
 469     // Do the OSR version
 470     if (!is_compiling && next_osr_level != level) {
 471       compile(mh, bci, next_osr_level, thread);
 472     }
 473   }
 474 }
 475 
 476 #endif