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