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