1 /*
   2  * Copyright (c) 2000, 2019, 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 "classfile/classLoaderDataGraph.inline.hpp"
  27 #include "code/compiledIC.hpp"
  28 #include "code/nmethod.hpp"
  29 #include "code/scopeDesc.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "oops/methodData.hpp"
  33 #include "oops/method.inline.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "prims/nativeLookup.hpp"
  36 #include "runtime/compilationPolicy.hpp"
  37 #include "runtime/frame.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "runtime/thread.hpp"
  41 #include "runtime/tieredThresholdPolicy.hpp"
  42 #include "runtime/vframe.hpp"
  43 #include "runtime/vmOperations.hpp"
  44 #include "utilities/events.inline.hpp"
  45 #include "utilities/globalDefinitions.hpp"
  46 
  47 #ifdef COMPILER1
  48 #include "c1/c1_Compiler.hpp"
  49 #endif
  50 #ifdef COMPILER2
  51 #include "opto/c2compiler.hpp"
  52 #endif
  53 
  54 CompilationPolicy* CompilationPolicy::_policy;
  55 
  56 // Determine compilation policy based on command line argument
  57 void compilationPolicy_init() {
  58   #ifdef TIERED
  59   if (TieredCompilation) {
  60     CompilationPolicy::set_policy(new TieredThresholdPolicy());
  61   } else {
  62     CompilationPolicy::set_policy(new SimpleCompPolicy());
  63   }
  64   #else
  65   CompilationPolicy::set_policy(new SimpleCompPolicy());
  66   #endif
  67 
  68   CompilationPolicy::policy()->initialize();
  69 }
  70 
  71 // Returns true if m must be compiled before executing it
  72 // This is intended to force compiles for methods (usually for
  73 // debugging) that would otherwise be interpreted for some reason.
  74 bool CompilationPolicy::must_be_compiled(const methodHandle& m, int comp_level) {
  75   // Don't allow Xcomp to cause compiles in replay mode
  76   if (ReplayCompiles) return false;
  77 
  78   if (m->has_compiled_code()) return false;       // already compiled
  79   if (!can_be_compiled(m, comp_level)) return false;
  80 
  81   return !UseInterpreter ||                                              // must compile all methods
  82          (UseCompiler && AlwaysCompileLoopMethods && m->has_loops() && CompileBroker::should_compile_new_jobs()); // eagerly compile loop methods
  83 }
  84 
  85 void CompilationPolicy::compile_if_required(const methodHandle& selected_method, TRAPS) {
  86   if (must_be_compiled(selected_method)) {
  87     // This path is unusual, mostly used by the '-Xcomp' stress test mode.
  88 
  89     // Note: with several active threads, the must_be_compiled may be true
  90     //       while can_be_compiled is false; remove assert
  91     // assert(CompilationPolicy::can_be_compiled(selected_method), "cannot compile");
  92     if (!THREAD->can_call_java() || THREAD->is_Compiler_thread()) {
  93       // don't force compilation, resolve was on behalf of compiler
  94       return;
  95     }
  96     if (selected_method->method_holder()->is_not_initialized()) {
  97       // 'is_not_initialized' means not only '!is_initialized', but also that
  98       // initialization has not been started yet ('!being_initialized')
  99       // Do not force compilation of methods in uninitialized classes.
 100       // Note that doing this would throw an assert later,
 101       // in CompileBroker::compile_method.
 102       // We sometimes use the link resolver to do reflective lookups
 103       // even before classes are initialized.
 104       return;
 105     }
 106     CompileBroker::compile_method(selected_method, InvocationEntryBci,
 107         CompilationPolicy::policy()->initial_compile_level(),
 108         methodHandle(), 0, CompileTask::Reason_MustBeCompiled, CHECK);
 109   }
 110 }
 111 
 112 // Returns true if m is allowed to be compiled
 113 bool CompilationPolicy::can_be_compiled(const methodHandle& m, int comp_level) {
 114   // allow any levels for WhiteBox
 115   assert(WhiteBoxAPI || comp_level == CompLevel_all || is_compile(comp_level), "illegal compilation level");
 116 
 117   if (m->is_abstract()) return false;
 118   if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false;
 119 
 120   // Math intrinsics should never be compiled as this can lead to
 121   // monotonicity problems because the interpreter will prefer the
 122   // compiled code to the intrinsic version.  This can't happen in
 123   // production because the invocation counter can't be incremented
 124   // but we shouldn't expose the system to this problem in testing
 125   // modes.
 126   if (!AbstractInterpreter::can_be_compiled(m)) {
 127     return false;
 128   }
 129   if (comp_level == CompLevel_all) {
 130     if (TieredCompilation) {
 131       // enough to be compilable at any level for tiered
 132       return !m->is_not_compilable(CompLevel_simple) || !m->is_not_compilable(CompLevel_full_optimization);
 133     } else {
 134       // must be compilable at available level for non-tiered
 135       return !m->is_not_compilable(CompLevel_highest_tier);
 136     }
 137   } else if (is_compile(comp_level)) {
 138     return !m->is_not_compilable(comp_level);
 139   }
 140   return false;
 141 }
 142 
 143 // Returns true if m is allowed to be osr compiled
 144 bool CompilationPolicy::can_be_osr_compiled(const methodHandle& m, int comp_level) {
 145   bool result = false;
 146   if (comp_level == CompLevel_all) {
 147     if (TieredCompilation) {
 148       // enough to be osr compilable at any level for tiered
 149       result = !m->is_not_osr_compilable(CompLevel_simple) || !m->is_not_osr_compilable(CompLevel_full_optimization);
 150     } else {
 151       // must be osr compilable at available level for non-tiered
 152       result = !m->is_not_osr_compilable(CompLevel_highest_tier);
 153     }
 154   } else if (is_compile(comp_level)) {
 155     result = !m->is_not_osr_compilable(comp_level);
 156   }
 157   return (result && can_be_compiled(m, comp_level));
 158 }
 159 
 160 bool CompilationPolicy::is_compilation_enabled() {
 161   // NOTE: CompileBroker::should_compile_new_jobs() checks for UseCompiler
 162   return CompileBroker::should_compile_new_jobs();
 163 }
 164 
 165 CompileTask* CompilationPolicy::select_task_helper(CompileQueue* compile_queue) {
 166   // Remove unloaded methods from the queue
 167   for (CompileTask* task = compile_queue->first(); task != NULL; ) {
 168     CompileTask* next = task->next();
 169     if (task->is_unloaded()) {
 170       compile_queue->remove_and_mark_stale(task);
 171     }
 172     task = next;
 173   }
 174 #if INCLUDE_JVMCI
 175   if (UseJVMCICompiler && !BackgroundCompilation) {
 176     /*
 177      * In blocking compilation mode, the CompileBroker will make
 178      * compilations submitted by a JVMCI compiler thread non-blocking. These
 179      * compilations should be scheduled after all blocking compilations
 180      * to service non-compiler related compilations sooner and reduce the
 181      * chance of such compilations timing out.
 182      */
 183     for (CompileTask* task = compile_queue->first(); task != NULL; task = task->next()) {
 184       if (task->is_blocking()) {
 185         return task;
 186       }
 187     }
 188   }
 189 #endif
 190   return compile_queue->first();
 191 }
 192 
 193 #ifndef PRODUCT
 194 void SimpleCompPolicy::trace_osr_completion(nmethod* osr_nm) {
 195   if (TraceOnStackReplacement) {
 196     if (osr_nm == NULL) tty->print_cr("compilation failed");
 197     else tty->print_cr("nmethod " INTPTR_FORMAT, p2i(osr_nm));
 198   }
 199 }
 200 #endif // !PRODUCT
 201 
 202 void SimpleCompPolicy::initialize() {
 203   // Setup the compiler thread numbers
 204   if (CICompilerCountPerCPU) {
 205     // Example: if CICompilerCountPerCPU is true, then we get
 206     // max(log2(8)-1,1) = 2 compiler threads on an 8-way machine.
 207     // May help big-app startup time.
 208     _compiler_count = MAX2(log2_int(os::active_processor_count())-1,1);
 209     // Make sure there is enough space in the code cache to hold all the compiler buffers
 210     size_t buffer_size = 1;
 211 #ifdef COMPILER1
 212     buffer_size = is_client_compilation_mode_vm() ? Compiler::code_buffer_size() : buffer_size;
 213 #endif
 214 #ifdef COMPILER2
 215     buffer_size = is_server_compilation_mode_vm() ? C2Compiler::initial_code_buffer_size() : buffer_size;
 216 #endif
 217     int max_count = (ReservedCodeCacheSize - (CodeCacheMinimumUseSpace DEBUG_ONLY(* 3))) / (int)buffer_size;
 218     if (_compiler_count > max_count) {
 219       // Lower the compiler count such that all buffers fit into the code cache
 220       _compiler_count = MAX2(max_count, 1);
 221     }
 222     FLAG_SET_ERGO(CICompilerCount, _compiler_count);
 223   } else {
 224     _compiler_count = CICompilerCount;
 225   }
 226 }
 227 
 228 // Note: this policy is used ONLY if TieredCompilation is off.
 229 // compiler_count() behaves the following way:
 230 // - with TIERED build (with both COMPILER1 and COMPILER2 defined) it should return
 231 //   zero for the c1 compilation levels in server compilation mode runs
 232 //   and c2 compilation levels in client compilation mode runs.
 233 // - with COMPILER2 not defined it should return zero for c2 compilation levels.
 234 // - with COMPILER1 not defined it should return zero for c1 compilation levels.
 235 // - if neither is defined - always return zero.
 236 int SimpleCompPolicy::compiler_count(CompLevel comp_level) {
 237   assert(!TieredCompilation, "This policy should not be used with TieredCompilation");
 238   if (COMPILER2_PRESENT(is_server_compilation_mode_vm() && is_c2_compile(comp_level) ||)
 239       is_client_compilation_mode_vm() && is_c1_compile(comp_level)) {
 240     return _compiler_count;
 241   }
 242   return 0;
 243 }
 244 
 245 void SimpleCompPolicy::reset_counter_for_invocation_event(const methodHandle& m) {
 246   // Make sure invocation and backedge counter doesn't overflow again right away
 247   // as would be the case for native methods.
 248 
 249   // BUT also make sure the method doesn't look like it was never executed.
 250   // Set carry bit and reduce counter's value to min(count, CompileThreshold/2).
 251   MethodCounters* mcs = m->method_counters();
 252   assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
 253   mcs->invocation_counter()->set_carry();
 254   mcs->backedge_counter()->set_carry();
 255 
 256   assert(!m->was_never_executed(), "don't reset to 0 -- could be mistaken for never-executed");
 257 }
 258 
 259 void SimpleCompPolicy::reset_counter_for_back_branch_event(const methodHandle& m) {
 260   // Delay next back-branch event but pump up invocation counter to trigger
 261   // whole method compilation.
 262   MethodCounters* mcs = m->method_counters();
 263   assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
 264   InvocationCounter* i = mcs->invocation_counter();
 265   InvocationCounter* b = mcs->backedge_counter();
 266 
 267   // Don't set invocation_counter's value too low otherwise the method will
 268   // look like immature (ic < ~5300) which prevents the inlining based on
 269   // the type profiling.
 270   i->set(i->state(), CompileThreshold);
 271   // Don't reset counter too low - it is used to check if OSR method is ready.
 272   b->set(b->state(), CompileThreshold / 2);
 273 }
 274 
 275 //
 276 // CounterDecay
 277 //
 278 // Iterates through invocation counters and decrements them. This
 279 // is done at each safepoint.
 280 //
 281 class CounterDecay : public AllStatic {
 282   static jlong _last_timestamp;
 283   static void do_method(Method* m) {
 284     MethodCounters* mcs = m->method_counters();
 285     if (mcs != NULL) {
 286       mcs->invocation_counter()->decay();
 287     }
 288   }
 289 public:
 290   static void decay();
 291   static bool is_decay_needed() {
 292     return (os::javaTimeMillis() - _last_timestamp) > CounterDecayMinIntervalLength;
 293   }
 294 };
 295 
 296 jlong CounterDecay::_last_timestamp = 0;
 297 
 298 void CounterDecay::decay() {
 299   _last_timestamp = os::javaTimeMillis();
 300 
 301   // This operation is going to be performed only at the end of a safepoint
 302   // and hence GC's will not be going on, all Java mutators are suspended
 303   // at this point and hence SystemDictionary_lock is also not needed.
 304   assert(SafepointSynchronize::is_at_safepoint(), "can only be executed at a safepoint");
 305   size_t nclasses = ClassLoaderDataGraph::num_instance_classes();
 306   size_t classes_per_tick = nclasses * (CounterDecayMinIntervalLength * 1e-3 /
 307                                         CounterHalfLifeTime);
 308   for (size_t i = 0; i < classes_per_tick; i++) {
 309     InstanceKlass* k = ClassLoaderDataGraph::try_get_next_class();
 310     if (k != NULL) {
 311       k->methods_do(do_method);
 312     }
 313   }
 314 }
 315 
 316 // Called at the end of the safepoint
 317 void SimpleCompPolicy::do_safepoint_work() {
 318   if(UseCounterDecay && CounterDecay::is_decay_needed()) {
 319     CounterDecay::decay();
 320   }
 321 }
 322 
 323 void SimpleCompPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
 324   ScopeDesc* sd = trap_scope;
 325   MethodCounters* mcs;
 326   InvocationCounter* c;
 327   for (; !sd->is_top(); sd = sd->sender()) {
 328     mcs = sd->method()->method_counters();
 329     if (mcs != NULL) {
 330       // Reset ICs of inlined methods, since they can trigger compilations also.
 331       mcs->invocation_counter()->reset();
 332     }
 333   }
 334   mcs = sd->method()->method_counters();
 335   if (mcs != NULL) {
 336     c = mcs->invocation_counter();
 337     if (is_osr) {
 338       // It was an OSR method, so bump the count higher.
 339       c->set(c->state(), CompileThreshold);
 340     } else {
 341       c->reset();
 342     }
 343     mcs->backedge_counter()->reset();
 344   }
 345 }
 346 
 347 // This method can be called by any component of the runtime to notify the policy
 348 // that it's recommended to delay the compilation of this method.
 349 void SimpleCompPolicy::delay_compilation(Method* method) {
 350   MethodCounters* mcs = method->method_counters();
 351   if (mcs != NULL) {
 352     mcs->invocation_counter()->decay();
 353     mcs->backedge_counter()->decay();
 354   }
 355 }
 356 
 357 void SimpleCompPolicy::disable_compilation(Method* method) {
 358   MethodCounters* mcs = method->method_counters();
 359   if (mcs != NULL) {
 360     mcs->invocation_counter()->set_state(InvocationCounter::wait_for_nothing);
 361     mcs->backedge_counter()->set_state(InvocationCounter::wait_for_nothing);
 362   }
 363 }
 364 
 365 CompileTask* SimpleCompPolicy::select_task(CompileQueue* compile_queue) {
 366   return select_task_helper(compile_queue);
 367 }
 368 
 369 bool SimpleCompPolicy::is_mature(Method* method) {
 370   MethodData* mdo = method->method_data();
 371   assert(mdo != NULL, "Should be");
 372   uint current = mdo->mileage_of(method);
 373   uint initial = mdo->creation_mileage();
 374   if (current < initial)
 375     return true;  // some sort of overflow
 376   uint target;
 377   if (ProfileMaturityPercentage <= 0)
 378     target = (uint) -ProfileMaturityPercentage;  // absolute value
 379   else
 380     target = (uint)( (ProfileMaturityPercentage * CompileThreshold) / 100 );
 381   return (current >= initial + target);
 382 }
 383 
 384 nmethod* SimpleCompPolicy::event(const methodHandle& method, const methodHandle& inlinee, int branch_bci,
 385                                     int bci, CompLevel comp_level, CompiledMethod* nm, JavaThread* thread) {
 386   assert(comp_level == CompLevel_none, "This should be only called from the interpreter");
 387   NOT_PRODUCT(trace_frequency_counter_overflow(method, branch_bci, bci));
 388   if (JvmtiExport::can_post_interpreter_events() && thread->is_interp_only_mode()) {
 389     // If certain JVMTI events (e.g. frame pop event) are requested then the
 390     // thread is forced to remain in interpreted code. This is
 391     // implemented partly by a check in the run_compiled_code
 392     // section of the interpreter whether we should skip running
 393     // compiled code, and partly by skipping OSR compiles for
 394     // interpreted-only threads.
 395     if (bci != InvocationEntryBci) {
 396       reset_counter_for_back_branch_event(method);
 397       return NULL;
 398     }
 399   }
 400   if (ReplayCompiles) {
 401     // Don't trigger other compiles in testing mode
 402     if (bci == InvocationEntryBci) {
 403       reset_counter_for_invocation_event(method);
 404     } else {
 405       reset_counter_for_back_branch_event(method);
 406     }
 407     return NULL;
 408   }
 409 
 410   if (bci == InvocationEntryBci) {
 411     // when code cache is full, compilation gets switched off, UseCompiler
 412     // is set to false
 413     if (!method->has_compiled_code() && UseCompiler) {
 414       method_invocation_event(method, thread);
 415     } else {
 416       // Force counter overflow on method entry, even if no compilation
 417       // happened.  (The method_invocation_event call does this also.)
 418       reset_counter_for_invocation_event(method);
 419     }
 420     // compilation at an invocation overflow no longer goes and retries test for
 421     // compiled method. We always run the loser of the race as interpreted.
 422     // so return NULL
 423     return NULL;
 424   } else {
 425     // counter overflow in a loop => try to do on-stack-replacement
 426     nmethod* osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
 427     NOT_PRODUCT(trace_osr_request(method, osr_nm, bci));
 428     // when code cache is full, we should not compile any more...
 429     if (osr_nm == NULL && UseCompiler) {
 430       method_back_branch_event(method, bci, thread);
 431       osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
 432     }
 433     if (osr_nm == NULL) {
 434       reset_counter_for_back_branch_event(method);
 435       return NULL;
 436     }
 437     return osr_nm;
 438   }
 439   return NULL;
 440 }
 441 
 442 #ifndef PRODUCT
 443 void SimpleCompPolicy::trace_frequency_counter_overflow(const methodHandle& m, int branch_bci, int bci) {
 444   if (TraceInvocationCounterOverflow) {
 445     MethodCounters* mcs = m->method_counters();
 446     assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
 447     InvocationCounter* ic = mcs->invocation_counter();
 448     InvocationCounter* bc = mcs->backedge_counter();
 449     ResourceMark rm;
 450     if (bci == InvocationEntryBci) {
 451       tty->print("comp-policy cntr ovfl @ %d in entry of ", bci);
 452     } else {
 453       tty->print("comp-policy cntr ovfl @ %d in loop of ", bci);
 454     }
 455     m->print_value();
 456     tty->cr();
 457     ic->print();
 458     bc->print();
 459     if (ProfileInterpreter) {
 460       if (bci != InvocationEntryBci) {
 461         MethodData* mdo = m->method_data();
 462         if (mdo != NULL) {
 463           ProfileData *pd = mdo->bci_to_data(branch_bci);
 464           if (pd == NULL) {
 465             tty->print_cr("back branch count = N/A (missing ProfileData)");
 466           } else {
 467             tty->print_cr("back branch count = %d", pd->as_JumpData()->taken());
 468           }
 469         }
 470       }
 471     }
 472   }
 473 }
 474 
 475 void SimpleCompPolicy::trace_osr_request(const methodHandle& method, nmethod* osr, int bci) {
 476   if (TraceOnStackReplacement) {
 477     ResourceMark rm;
 478     tty->print(osr != NULL ? "Reused OSR entry for " : "Requesting OSR entry for ");
 479     method->print_short_name(tty);
 480     tty->print_cr(" at bci %d", bci);
 481   }
 482 }
 483 #endif // !PRODUCT
 484 
 485 void SimpleCompPolicy::method_invocation_event(const methodHandle& m, JavaThread* thread) {
 486   const int comp_level = CompLevel_highest_tier;
 487   const int hot_count = m->invocation_count();
 488   reset_counter_for_invocation_event(m);
 489 
 490   if (is_compilation_enabled() && can_be_compiled(m, comp_level)) {
 491     CompiledMethod* nm = m->code();
 492     if (nm == NULL ) {
 493       CompileBroker::compile_method(m, InvocationEntryBci, comp_level, m, hot_count, CompileTask::Reason_InvocationCount, thread);
 494     }
 495   }
 496 }
 497 
 498 void SimpleCompPolicy::method_back_branch_event(const methodHandle& m, int bci, JavaThread* thread) {
 499   const int comp_level = CompLevel_highest_tier;
 500   const int hot_count = m->backedge_count();
 501 
 502   if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) {
 503     CompileBroker::compile_method(m, bci, comp_level, m, hot_count, CompileTask::Reason_BackedgeCount, thread);
 504     NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));)
 505   }
 506 }