1 /*
   2  * Copyright (c) 1999, 2024, 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/javaClasses.inline.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/vmClasses.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/codeCache.hpp"
  31 #include "code/codeHeapState.hpp"
  32 #include "code/dependencyContext.hpp"
  33 #include "compiler/compilationLog.hpp"
  34 #include "compiler/compilationMemoryStatistic.hpp"
  35 #include "compiler/compilationPolicy.hpp"
  36 #include "compiler/compileBroker.hpp"
  37 #include "compiler/compileLog.hpp"
  38 #include "compiler/compilerEvent.hpp"
  39 #include "compiler/compilerOracle.hpp"
  40 #include "compiler/directivesParser.hpp"
  41 #include "gc/shared/memAllocator.hpp"
  42 #include "interpreter/linkResolver.hpp"
  43 #include "jvm.h"
  44 #include "jfr/jfrEvents.hpp"
  45 #include "logging/log.hpp"
  46 #include "logging/logStream.hpp"
  47 #include "memory/allocation.inline.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "memory/universe.hpp"
  50 #include "oops/methodData.hpp"
  51 #include "oops/method.inline.hpp"
  52 #include "oops/oop.inline.hpp"
  53 #include "prims/jvmtiExport.hpp"
  54 #include "prims/nativeLookup.hpp"
  55 #include "prims/whitebox.hpp"
  56 #include "runtime/atomic.hpp"
  57 #include "runtime/escapeBarrier.hpp"
  58 #include "runtime/globals_extension.hpp"
  59 #include "runtime/handles.inline.hpp"
  60 #include "runtime/init.hpp"
  61 #include "runtime/interfaceSupport.inline.hpp"
  62 #include "runtime/java.hpp"
  63 #include "runtime/javaCalls.hpp"
  64 #include "runtime/jniHandles.inline.hpp"
  65 #include "runtime/os.hpp"
  66 #include "runtime/perfData.hpp"
  67 #include "runtime/safepointVerifiers.hpp"
  68 #include "runtime/sharedRuntime.hpp"
  69 #include "runtime/threads.hpp"
  70 #include "runtime/threadSMR.hpp"
  71 #include "runtime/timerTrace.hpp"
  72 #include "runtime/vframe.inline.hpp"
  73 #include "utilities/debug.hpp"
  74 #include "utilities/dtrace.hpp"
  75 #include "utilities/events.hpp"
  76 #include "utilities/formatBuffer.hpp"
  77 #include "utilities/macros.hpp"
  78 #ifdef COMPILER1
  79 #include "c1/c1_Compiler.hpp"
  80 #endif
  81 #ifdef COMPILER2
  82 #include "opto/c2compiler.hpp"
  83 #endif
  84 #if INCLUDE_JVMCI
  85 #include "jvmci/jvmciEnv.hpp"
  86 #include "jvmci/jvmciRuntime.hpp"
  87 #endif
  88 
  89 #ifdef DTRACE_ENABLED
  90 
  91 // Only bother with this argument setup if dtrace is available
  92 
  93 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)             \
  94   {                                                                      \
  95     Symbol* klass_name = (method)->klass_name();                         \
  96     Symbol* name = (method)->name();                                     \
  97     Symbol* signature = (method)->signature();                           \
  98     HOTSPOT_METHOD_COMPILE_BEGIN(                                        \
  99       (char *) comp_name, strlen(comp_name),                             \
 100       (char *) klass_name->bytes(), klass_name->utf8_length(),           \
 101       (char *) name->bytes(), name->utf8_length(),                       \
 102       (char *) signature->bytes(), signature->utf8_length());            \
 103   }
 104 
 105 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)      \
 106   {                                                                      \
 107     Symbol* klass_name = (method)->klass_name();                         \
 108     Symbol* name = (method)->name();                                     \
 109     Symbol* signature = (method)->signature();                           \
 110     HOTSPOT_METHOD_COMPILE_END(                                          \
 111       (char *) comp_name, strlen(comp_name),                             \
 112       (char *) klass_name->bytes(), klass_name->utf8_length(),           \
 113       (char *) name->bytes(), name->utf8_length(),                       \
 114       (char *) signature->bytes(), signature->utf8_length(), (success)); \
 115   }
 116 
 117 #else //  ndef DTRACE_ENABLED
 118 
 119 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
 120 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
 121 
 122 #endif // ndef DTRACE_ENABLED
 123 
 124 bool CompileBroker::_initialized = false;
 125 volatile bool CompileBroker::_should_block = false;
 126 volatile int  CompileBroker::_print_compilation_warning = 0;
 127 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
 128 
 129 // The installed compiler(s)
 130 AbstractCompiler* CompileBroker::_compilers[2];
 131 
 132 // The maximum numbers of compiler threads to be determined during startup.
 133 int CompileBroker::_c1_count = 0;
 134 int CompileBroker::_c2_count = 0;
 135 
 136 // An array of compiler names as Java String objects
 137 jobject* CompileBroker::_compiler1_objects = nullptr;
 138 jobject* CompileBroker::_compiler2_objects = nullptr;
 139 
 140 CompileLog** CompileBroker::_compiler1_logs = nullptr;
 141 CompileLog** CompileBroker::_compiler2_logs = nullptr;
 142 
 143 // These counters are used to assign an unique ID to each compilation.
 144 volatile jint CompileBroker::_compilation_id     = 0;
 145 volatile jint CompileBroker::_osr_compilation_id = 0;
 146 volatile jint CompileBroker::_native_compilation_id = 0;
 147 
 148 // Performance counters
 149 PerfCounter* CompileBroker::_perf_total_compilation = nullptr;
 150 PerfCounter* CompileBroker::_perf_osr_compilation = nullptr;
 151 PerfCounter* CompileBroker::_perf_standard_compilation = nullptr;
 152 
 153 PerfCounter* CompileBroker::_perf_total_bailout_count = nullptr;
 154 PerfCounter* CompileBroker::_perf_total_invalidated_count = nullptr;
 155 PerfCounter* CompileBroker::_perf_total_compile_count = nullptr;
 156 PerfCounter* CompileBroker::_perf_total_osr_compile_count = nullptr;
 157 PerfCounter* CompileBroker::_perf_total_standard_compile_count = nullptr;
 158 
 159 PerfCounter* CompileBroker::_perf_sum_osr_bytes_compiled = nullptr;
 160 PerfCounter* CompileBroker::_perf_sum_standard_bytes_compiled = nullptr;
 161 PerfCounter* CompileBroker::_perf_sum_nmethod_size = nullptr;
 162 PerfCounter* CompileBroker::_perf_sum_nmethod_code_size = nullptr;
 163 
 164 PerfStringVariable* CompileBroker::_perf_last_method = nullptr;
 165 PerfStringVariable* CompileBroker::_perf_last_failed_method = nullptr;
 166 PerfStringVariable* CompileBroker::_perf_last_invalidated_method = nullptr;
 167 PerfVariable*       CompileBroker::_perf_last_compile_type = nullptr;
 168 PerfVariable*       CompileBroker::_perf_last_compile_size = nullptr;
 169 PerfVariable*       CompileBroker::_perf_last_failed_type = nullptr;
 170 PerfVariable*       CompileBroker::_perf_last_invalidated_type = nullptr;
 171 
 172 // Timers and counters for generating statistics
 173 elapsedTimer CompileBroker::_t_total_compilation;
 174 elapsedTimer CompileBroker::_t_osr_compilation;
 175 elapsedTimer CompileBroker::_t_standard_compilation;
 176 elapsedTimer CompileBroker::_t_invalidated_compilation;
 177 elapsedTimer CompileBroker::_t_bailedout_compilation;
 178 
 179 uint CompileBroker::_total_bailout_count            = 0;
 180 uint CompileBroker::_total_invalidated_count        = 0;
 181 uint CompileBroker::_total_compile_count            = 0;
 182 uint CompileBroker::_total_osr_compile_count        = 0;
 183 uint CompileBroker::_total_standard_compile_count   = 0;
 184 uint CompileBroker::_total_compiler_stopped_count   = 0;
 185 uint CompileBroker::_total_compiler_restarted_count = 0;
 186 
 187 uint CompileBroker::_sum_osr_bytes_compiled         = 0;
 188 uint CompileBroker::_sum_standard_bytes_compiled    = 0;
 189 uint CompileBroker::_sum_nmethod_size               = 0;
 190 uint CompileBroker::_sum_nmethod_code_size          = 0;
 191 
 192 jlong CompileBroker::_peak_compilation_time        = 0;
 193 
 194 CompilerStatistics CompileBroker::_stats_per_level[CompLevel_full_optimization];
 195 
 196 CompileQueue* CompileBroker::_c2_compile_queue     = nullptr;
 197 CompileQueue* CompileBroker::_c1_compile_queue     = nullptr;
 198 
 199 bool compileBroker_init() {
 200   if (LogEvents) {
 201     CompilationLog::init();
 202   }
 203 
 204   // init directives stack, adding default directive
 205   DirectivesStack::init();
 206 
 207   if (DirectivesParser::has_file()) {
 208     return DirectivesParser::parse_from_flag();
 209   } else if (CompilerDirectivesPrint) {
 210     // Print default directive even when no other was added
 211     DirectivesStack::print(tty);
 212   }
 213 
 214   return true;
 215 }
 216 
 217 CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) {
 218   CompilerThread* thread = CompilerThread::current();
 219   thread->set_task(task);
 220   CompileLog*     log  = thread->log();
 221   if (log != nullptr && !task->is_unloaded())  task->log_task_start(log);
 222 }
 223 
 224 CompileTaskWrapper::~CompileTaskWrapper() {
 225   CompilerThread* thread = CompilerThread::current();
 226   CompileTask* task = thread->task();
 227   CompileLog*  log  = thread->log();
 228   if (log != nullptr && !task->is_unloaded())  task->log_task_done(log);
 229   thread->set_task(nullptr);
 230   thread->set_env(nullptr);
 231   if (task->is_blocking()) {
 232     bool free_task = false;
 233     {
 234       MutexLocker notifier(thread, task->lock());
 235       task->mark_complete();
 236 #if INCLUDE_JVMCI
 237       if (CompileBroker::compiler(task->comp_level())->is_jvmci()) {
 238         if (!task->has_waiter()) {
 239           // The waiting thread timed out and thus did not free the task.
 240           free_task = true;
 241         }
 242         task->set_blocking_jvmci_compile_state(nullptr);
 243       }
 244 #endif
 245       if (!free_task) {
 246         // Notify the waiting thread that the compilation has completed
 247         // so that it can free the task.
 248         task->lock()->notify_all();
 249       }
 250     }
 251     if (free_task) {
 252       // The task can only be freed once the task lock is released.
 253       CompileTask::free(task);
 254     }
 255   } else {
 256     task->mark_complete();
 257 
 258     // By convention, the compiling thread is responsible for
 259     // recycling a non-blocking CompileTask.
 260     CompileTask::free(task);
 261   }
 262 }
 263 
 264 /**
 265  * Check if a CompilerThread can be removed and update count if requested.
 266  */
 267 bool CompileBroker::can_remove(CompilerThread *ct, bool do_it) {
 268   assert(UseDynamicNumberOfCompilerThreads, "or shouldn't be here");
 269   if (!ReduceNumberOfCompilerThreads) return false;
 270 
 271   AbstractCompiler *compiler = ct->compiler();
 272   int compiler_count = compiler->num_compiler_threads();
 273   bool c1 = compiler->is_c1();
 274 
 275   // Keep at least 1 compiler thread of each type.
 276   if (compiler_count < 2) return false;
 277 
 278   // Keep thread alive for at least some time.
 279   if (ct->idle_time_millis() < (c1 ? 500 : 100)) return false;
 280 
 281 #if INCLUDE_JVMCI
 282   if (compiler->is_jvmci() && !UseJVMCINativeLibrary) {
 283     // Handles for JVMCI thread objects may get released concurrently.
 284     if (do_it) {
 285       assert(CompileThread_lock->owner() == ct, "must be holding lock");
 286     } else {
 287       // Skip check if it's the last thread and let caller check again.
 288       return true;
 289     }
 290   }
 291 #endif
 292 
 293   // We only allow the last compiler thread of each type to get removed.
 294   jobject last_compiler = c1 ? compiler1_object(compiler_count - 1)
 295                              : compiler2_object(compiler_count - 1);
 296   if (ct->threadObj() == JNIHandles::resolve_non_null(last_compiler)) {
 297     if (do_it) {
 298       assert_locked_or_safepoint(CompileThread_lock); // Update must be consistent.
 299       compiler->set_num_compiler_threads(compiler_count - 1);
 300 #if INCLUDE_JVMCI
 301       if (compiler->is_jvmci() && !UseJVMCINativeLibrary) {
 302         // Old j.l.Thread object can die when no longer referenced elsewhere.
 303         JNIHandles::destroy_global(compiler2_object(compiler_count - 1));
 304         _compiler2_objects[compiler_count - 1] = nullptr;
 305       }
 306 #endif
 307     }
 308     return true;
 309   }
 310   return false;
 311 }
 312 
 313 /**
 314  * Add a CompileTask to a CompileQueue.
 315  */
 316 void CompileQueue::add(CompileTask* task) {
 317   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 318 
 319   task->set_next(nullptr);
 320   task->set_prev(nullptr);
 321 
 322   if (_last == nullptr) {
 323     // The compile queue is empty.
 324     assert(_first == nullptr, "queue is empty");
 325     _first = task;
 326     _last = task;
 327   } else {
 328     // Append the task to the queue.
 329     assert(_last->next() == nullptr, "not last");
 330     _last->set_next(task);
 331     task->set_prev(_last);
 332     _last = task;
 333   }
 334   ++_size;
 335   ++_total_added;
 336   if (_size > _peak_size) {
 337     _peak_size = _size;
 338   }
 339 
 340   // Mark the method as being in the compile queue.
 341   task->method()->set_queued_for_compilation();
 342 
 343   if (CIPrintCompileQueue) {
 344     print_tty();
 345   }
 346 
 347   if (LogCompilation && xtty != nullptr) {
 348     task->log_task_queued();
 349   }
 350 
 351   // Notify CompilerThreads that a task is available.
 352   MethodCompileQueue_lock->notify_all();
 353 }
 354 
 355 /**
 356  * Empties compilation queue by putting all compilation tasks onto
 357  * a freelist. Furthermore, the method wakes up all threads that are
 358  * waiting on a compilation task to finish. This can happen if background
 359  * compilation is disabled.
 360  */
 361 void CompileQueue::free_all() {
 362   MutexLocker mu(MethodCompileQueue_lock);
 363   CompileTask* next = _first;
 364 
 365   // Iterate over all tasks in the compile queue
 366   while (next != nullptr) {
 367     CompileTask* current = next;
 368     next = current->next();
 369     {
 370       // Wake up thread that blocks on the compile task.
 371       MutexLocker ct_lock(current->lock());
 372       current->lock()->notify();
 373     }
 374     // Put the task back on the freelist.
 375     CompileTask::free(current);
 376   }
 377   _first = nullptr;
 378   _last = nullptr;
 379 
 380   // Wake up all threads that block on the queue.
 381   MethodCompileQueue_lock->notify_all();
 382 }
 383 
 384 /**
 385  * Get the next CompileTask from a CompileQueue
 386  */
 387 CompileTask* CompileQueue::get(CompilerThread* thread) {
 388   // save methods from RedefineClasses across safepoint
 389   // across MethodCompileQueue_lock below.
 390   methodHandle save_method;
 391   methodHandle save_hot_method;
 392 
 393   MonitorLocker locker(MethodCompileQueue_lock);
 394   // If _first is null we have no more compile jobs. There are two reasons for
 395   // having no compile jobs: First, we compiled everything we wanted. Second,
 396   // we ran out of code cache so compilation has been disabled. In the latter
 397   // case we perform code cache sweeps to free memory such that we can re-enable
 398   // compilation.
 399   while (_first == nullptr) {
 400     // Exit loop if compilation is disabled forever
 401     if (CompileBroker::is_compilation_disabled_forever()) {
 402       return nullptr;
 403     }
 404 
 405     AbstractCompiler* compiler = thread->compiler();
 406     guarantee(compiler != nullptr, "Compiler object must exist");
 407     compiler->on_empty_queue(this, thread);
 408     if (_first != nullptr) {
 409       // The call to on_empty_queue may have temporarily unlocked the MCQ lock
 410       // so check again whether any tasks were added to the queue.
 411       break;
 412     }
 413 
 414     // If there are no compilation tasks and we can compile new jobs
 415     // (i.e., there is enough free space in the code cache) there is
 416     // no need to invoke the GC.
 417     // We need a timed wait here, since compiler threads can exit if compilation
 418     // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
 419     // is not critical and we do not want idle compiler threads to wake up too often.
 420     locker.wait(5*1000);
 421 
 422     if (UseDynamicNumberOfCompilerThreads && _first == nullptr) {
 423       // Still nothing to compile. Give caller a chance to stop this thread.
 424       if (CompileBroker::can_remove(CompilerThread::current(), false)) return nullptr;
 425     }
 426   }
 427 
 428   if (CompileBroker::is_compilation_disabled_forever()) {
 429     return nullptr;
 430   }
 431 
 432   CompileTask* task;
 433   {
 434     NoSafepointVerifier nsv;
 435     task = CompilationPolicy::select_task(this);
 436     if (task != nullptr) {
 437       task = task->select_for_compilation();
 438     }
 439   }
 440 
 441   if (task != nullptr) {
 442     // Save method pointers across unlock safepoint.  The task is removed from
 443     // the compilation queue, which is walked during RedefineClasses.
 444     Thread* thread = Thread::current();
 445     save_method = methodHandle(thread, task->method());
 446     save_hot_method = methodHandle(thread, task->hot_method());
 447 
 448     remove(task);
 449   }
 450   purge_stale_tasks(); // may temporarily release MCQ lock
 451   return task;
 452 }
 453 
 454 // Clean & deallocate stale compile tasks.
 455 // Temporarily releases MethodCompileQueue lock.
 456 void CompileQueue::purge_stale_tasks() {
 457   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 458   if (_first_stale != nullptr) {
 459     // Stale tasks are purged when MCQ lock is released,
 460     // but _first_stale updates are protected by MCQ lock.
 461     // Once task processing starts and MCQ lock is released,
 462     // other compiler threads can reuse _first_stale.
 463     CompileTask* head = _first_stale;
 464     _first_stale = nullptr;
 465     {
 466       MutexUnlocker ul(MethodCompileQueue_lock);
 467       for (CompileTask* task = head; task != nullptr; ) {
 468         CompileTask* next_task = task->next();
 469         CompileTaskWrapper ctw(task); // Frees the task
 470         task->set_failure_reason("stale task");
 471         task = next_task;
 472       }
 473     }
 474   }
 475 }
 476 
 477 void CompileQueue::remove(CompileTask* task) {
 478   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 479   if (task->prev() != nullptr) {
 480     task->prev()->set_next(task->next());
 481   } else {
 482     // max is the first element
 483     assert(task == _first, "Sanity");
 484     _first = task->next();
 485   }
 486 
 487   if (task->next() != nullptr) {
 488     task->next()->set_prev(task->prev());
 489   } else {
 490     // max is the last element
 491     assert(task == _last, "Sanity");
 492     _last = task->prev();
 493   }
 494   --_size;
 495   ++_total_removed;
 496 }
 497 
 498 void CompileQueue::remove_and_mark_stale(CompileTask* task) {
 499   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 500   remove(task);
 501 
 502   // Enqueue the task for reclamation (should be done outside MCQ lock)
 503   task->set_next(_first_stale);
 504   task->set_prev(nullptr);
 505   _first_stale = task;
 506 }
 507 
 508 // methods in the compile queue need to be marked as used on the stack
 509 // so that they don't get reclaimed by Redefine Classes
 510 void CompileQueue::mark_on_stack() {
 511   CompileTask* task = _first;
 512   while (task != nullptr) {
 513     task->mark_on_stack();
 514     task = task->next();
 515   }
 516 }
 517 
 518 
 519 CompileQueue* CompileBroker::compile_queue(int comp_level) {
 520   if (is_c2_compile(comp_level)) return _c2_compile_queue;
 521   if (is_c1_compile(comp_level)) return _c1_compile_queue;
 522   return nullptr;
 523 }
 524 
 525 CompileQueue* CompileBroker::c1_compile_queue() {
 526   return _c1_compile_queue;
 527 }
 528 
 529 CompileQueue* CompileBroker::c2_compile_queue() {
 530   return _c2_compile_queue;
 531 }
 532 
 533 void CompileBroker::print_compile_queues(outputStream* st) {
 534   st->print_cr("Current compiles: ");
 535 
 536   char buf[2000];
 537   int buflen = sizeof(buf);
 538   Threads::print_threads_compiling(st, buf, buflen, /* short_form = */ true);
 539 
 540   st->cr();
 541   if (_c1_compile_queue != nullptr) {
 542     _c1_compile_queue->print(st);
 543   }
 544   if (_c2_compile_queue != nullptr) {
 545     _c2_compile_queue->print(st);
 546   }
 547 }
 548 
 549 void CompileQueue::print(outputStream* st) {
 550   assert_locked_or_safepoint(MethodCompileQueue_lock);
 551   st->print_cr("%s:", name());
 552   CompileTask* task = _first;
 553   if (task == nullptr) {
 554     st->print_cr("Empty");
 555   } else {
 556     while (task != nullptr) {
 557       task->print(st, nullptr, true, true);
 558       task = task->next();
 559     }
 560   }
 561   st->cr();
 562 }
 563 
 564 void CompileQueue::print_tty() {
 565   stringStream ss;
 566   // Dump the compile queue into a buffer before locking the tty
 567   print(&ss);
 568   {
 569     ttyLocker ttyl;
 570     tty->print("%s", ss.freeze());
 571   }
 572 }
 573 
 574 CompilerCounters::CompilerCounters() {
 575   _current_method[0] = '\0';
 576   _compile_type = CompileBroker::no_compile;
 577 }
 578 
 579 #if INCLUDE_JFR && COMPILER2_OR_JVMCI
 580 // It appends new compiler phase names to growable array phase_names(a new CompilerPhaseType mapping
 581 // in compiler/compilerEvent.cpp) and registers it with its serializer.
 582 //
 583 // c2 uses explicit CompilerPhaseType idToPhase mapping in opto/phasetype.hpp,
 584 // so if c2 is used, it should be always registered first.
 585 // This function is called during vm initialization.
 586 static void register_jfr_phasetype_serializer(CompilerType compiler_type) {
 587   ResourceMark rm;
 588   static bool first_registration = true;
 589   if (compiler_type == compiler_jvmci) {
 590     CompilerEvent::PhaseEvent::get_phase_id("NOT_A_PHASE_NAME", false, false, false);
 591     first_registration = false;
 592 #ifdef COMPILER2
 593   } else if (compiler_type == compiler_c2) {
 594     assert(first_registration, "invariant"); // c2 must be registered first.
 595     for (int i = 0; i < PHASE_NUM_TYPES; i++) {
 596       const char* phase_name = CompilerPhaseTypeHelper::to_description((CompilerPhaseType) i);
 597       CompilerEvent::PhaseEvent::get_phase_id(phase_name, false, false, false);
 598     }
 599     first_registration = false;
 600 #endif // COMPILER2
 601   }
 602 }
 603 #endif // INCLUDE_JFR && COMPILER2_OR_JVMCI
 604 
 605 // ------------------------------------------------------------------
 606 // CompileBroker::compilation_init
 607 //
 608 // Initialize the Compilation object
 609 void CompileBroker::compilation_init(JavaThread* THREAD) {
 610   // No need to initialize compilation system if we do not use it.
 611   if (!UseCompiler) {
 612     return;
 613   }
 614   // Set the interface to the current compiler(s).
 615   _c1_count = CompilationPolicy::c1_count();
 616   _c2_count = CompilationPolicy::c2_count();
 617 
 618 #if INCLUDE_JVMCI
 619   if (EnableJVMCI) {
 620     // This is creating a JVMCICompiler singleton.
 621     JVMCICompiler* jvmci = new JVMCICompiler();
 622 
 623     if (UseJVMCICompiler) {
 624       _compilers[1] = jvmci;
 625       if (FLAG_IS_DEFAULT(JVMCIThreads)) {
 626         if (BootstrapJVMCI) {
 627           // JVMCI will bootstrap so give it more threads
 628           _c2_count = MIN2(32, os::active_processor_count());
 629         }
 630       } else {
 631         _c2_count = JVMCIThreads;
 632       }
 633       if (FLAG_IS_DEFAULT(JVMCIHostThreads)) {
 634       } else {
 635 #ifdef COMPILER1
 636         _c1_count = JVMCIHostThreads;
 637 #endif // COMPILER1
 638       }
 639     }
 640   }
 641 #endif // INCLUDE_JVMCI
 642 
 643 #ifdef COMPILER1
 644   if (_c1_count > 0) {
 645     _compilers[0] = new Compiler();
 646   }
 647 #endif // COMPILER1
 648 
 649 #ifdef COMPILER2
 650   if (true JVMCI_ONLY( && !UseJVMCICompiler)) {
 651     if (_c2_count > 0) {
 652       _compilers[1] = new C2Compiler();
 653       // Register c2 first as c2 CompilerPhaseType idToPhase mapping is explicit.
 654       // idToPhase mapping for c2 is in opto/phasetype.hpp
 655       JFR_ONLY(register_jfr_phasetype_serializer(compiler_c2);)
 656     }
 657   }
 658 #endif // COMPILER2
 659 
 660 #if INCLUDE_JVMCI
 661    // Register after c2 registration.
 662    // JVMCI CompilerPhaseType idToPhase mapping is dynamic.
 663    if (EnableJVMCI) {
 664      JFR_ONLY(register_jfr_phasetype_serializer(compiler_jvmci);)
 665    }
 666 #endif // INCLUDE_JVMCI
 667 
 668   if (CompilerOracle::should_collect_memstat()) {
 669     CompilationMemoryStatistic::initialize();
 670   }
 671 
 672   // Start the compiler thread(s)
 673   init_compiler_threads();
 674   // totalTime performance counter is always created as it is required
 675   // by the implementation of java.lang.management.CompilationMXBean.
 676   {
 677     // Ensure OOM leads to vm_exit_during_initialization.
 678     EXCEPTION_MARK;
 679     _perf_total_compilation =
 680                  PerfDataManager::create_counter(JAVA_CI, "totalTime",
 681                                                  PerfData::U_Ticks, CHECK);
 682   }
 683 
 684   if (UsePerfData) {
 685 
 686     EXCEPTION_MARK;
 687 
 688     // create the jvmstat performance counters
 689     _perf_osr_compilation =
 690                  PerfDataManager::create_counter(SUN_CI, "osrTime",
 691                                                  PerfData::U_Ticks, CHECK);
 692 
 693     _perf_standard_compilation =
 694                  PerfDataManager::create_counter(SUN_CI, "standardTime",
 695                                                  PerfData::U_Ticks, CHECK);
 696 
 697     _perf_total_bailout_count =
 698                  PerfDataManager::create_counter(SUN_CI, "totalBailouts",
 699                                                  PerfData::U_Events, CHECK);
 700 
 701     _perf_total_invalidated_count =
 702                  PerfDataManager::create_counter(SUN_CI, "totalInvalidates",
 703                                                  PerfData::U_Events, CHECK);
 704 
 705     _perf_total_compile_count =
 706                  PerfDataManager::create_counter(SUN_CI, "totalCompiles",
 707                                                  PerfData::U_Events, CHECK);
 708     _perf_total_osr_compile_count =
 709                  PerfDataManager::create_counter(SUN_CI, "osrCompiles",
 710                                                  PerfData::U_Events, CHECK);
 711 
 712     _perf_total_standard_compile_count =
 713                  PerfDataManager::create_counter(SUN_CI, "standardCompiles",
 714                                                  PerfData::U_Events, CHECK);
 715 
 716     _perf_sum_osr_bytes_compiled =
 717                  PerfDataManager::create_counter(SUN_CI, "osrBytes",
 718                                                  PerfData::U_Bytes, CHECK);
 719 
 720     _perf_sum_standard_bytes_compiled =
 721                  PerfDataManager::create_counter(SUN_CI, "standardBytes",
 722                                                  PerfData::U_Bytes, CHECK);
 723 
 724     _perf_sum_nmethod_size =
 725                  PerfDataManager::create_counter(SUN_CI, "nmethodSize",
 726                                                  PerfData::U_Bytes, CHECK);
 727 
 728     _perf_sum_nmethod_code_size =
 729                  PerfDataManager::create_counter(SUN_CI, "nmethodCodeSize",
 730                                                  PerfData::U_Bytes, CHECK);
 731 
 732     _perf_last_method =
 733                  PerfDataManager::create_string_variable(SUN_CI, "lastMethod",
 734                                        CompilerCounters::cmname_buffer_length,
 735                                        "", CHECK);
 736 
 737     _perf_last_failed_method =
 738             PerfDataManager::create_string_variable(SUN_CI, "lastFailedMethod",
 739                                        CompilerCounters::cmname_buffer_length,
 740                                        "", CHECK);
 741 
 742     _perf_last_invalidated_method =
 743         PerfDataManager::create_string_variable(SUN_CI, "lastInvalidatedMethod",
 744                                      CompilerCounters::cmname_buffer_length,
 745                                      "", CHECK);
 746 
 747     _perf_last_compile_type =
 748              PerfDataManager::create_variable(SUN_CI, "lastType",
 749                                               PerfData::U_None,
 750                                               (jlong)CompileBroker::no_compile,
 751                                               CHECK);
 752 
 753     _perf_last_compile_size =
 754              PerfDataManager::create_variable(SUN_CI, "lastSize",
 755                                               PerfData::U_Bytes,
 756                                               (jlong)CompileBroker::no_compile,
 757                                               CHECK);
 758 
 759 
 760     _perf_last_failed_type =
 761              PerfDataManager::create_variable(SUN_CI, "lastFailedType",
 762                                               PerfData::U_None,
 763                                               (jlong)CompileBroker::no_compile,
 764                                               CHECK);
 765 
 766     _perf_last_invalidated_type =
 767          PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
 768                                           PerfData::U_None,
 769                                           (jlong)CompileBroker::no_compile,
 770                                           CHECK);
 771   }
 772 
 773   _initialized = true;
 774 }
 775 
 776 #if defined(ASSERT) && COMPILER2_OR_JVMCI
 777 // Stress testing. Dedicated threads revert optimizations based on escape analysis concurrently to
 778 // the running java application.  Configured with vm options DeoptimizeObjectsALot*.
 779 class DeoptimizeObjectsALotThread : public JavaThread {
 780 
 781   static void deopt_objs_alot_thread_entry(JavaThread* thread, TRAPS);
 782   void deoptimize_objects_alot_loop_single();
 783   void deoptimize_objects_alot_loop_all();
 784 
 785 public:
 786   DeoptimizeObjectsALotThread() : JavaThread(&deopt_objs_alot_thread_entry) { }
 787 
 788   bool is_hidden_from_external_view() const      { return true; }
 789 };
 790 
 791 // Entry for DeoptimizeObjectsALotThread. The threads are started in
 792 // CompileBroker::init_compiler_threads() iff DeoptimizeObjectsALot is enabled
 793 void DeoptimizeObjectsALotThread::deopt_objs_alot_thread_entry(JavaThread* thread, TRAPS) {
 794     DeoptimizeObjectsALotThread* dt = ((DeoptimizeObjectsALotThread*) thread);
 795     bool enter_single_loop;
 796     {
 797       MonitorLocker ml(dt, EscapeBarrier_lock, Mutex::_no_safepoint_check_flag);
 798       static int single_thread_count = 0;
 799       enter_single_loop = single_thread_count++ < DeoptimizeObjectsALotThreadCountSingle;
 800     }
 801     if (enter_single_loop) {
 802       dt->deoptimize_objects_alot_loop_single();
 803     } else {
 804       dt->deoptimize_objects_alot_loop_all();
 805     }
 806   }
 807 
 808 // Execute EscapeBarriers in an endless loop to revert optimizations based on escape analysis. Each
 809 // barrier targets a single thread which is selected round robin.
 810 void DeoptimizeObjectsALotThread::deoptimize_objects_alot_loop_single() {
 811   HandleMark hm(this);
 812   while (true) {
 813     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *deoptee_thread = jtiwh.next(); ) {
 814       { // Begin new scope for escape barrier
 815         HandleMarkCleaner hmc(this);
 816         ResourceMark rm(this);
 817         EscapeBarrier eb(true, this, deoptee_thread);
 818         eb.deoptimize_objects(100);
 819       }
 820       // Now sleep after the escape barriers destructor resumed deoptee_thread.
 821       sleep(DeoptimizeObjectsALotInterval);
 822     }
 823   }
 824 }
 825 
 826 // Execute EscapeBarriers in an endless loop to revert optimizations based on escape analysis. Each
 827 // barrier targets all java threads in the vm at once.
 828 void DeoptimizeObjectsALotThread::deoptimize_objects_alot_loop_all() {
 829   HandleMark hm(this);
 830   while (true) {
 831     { // Begin new scope for escape barrier
 832       HandleMarkCleaner hmc(this);
 833       ResourceMark rm(this);
 834       EscapeBarrier eb(true, this);
 835       eb.deoptimize_objects_all_threads();
 836     }
 837     // Now sleep after the escape barriers destructor resumed the java threads.
 838     sleep(DeoptimizeObjectsALotInterval);
 839   }
 840 }
 841 #endif // defined(ASSERT) && COMPILER2_OR_JVMCI
 842 
 843 
 844 JavaThread* CompileBroker::make_thread(ThreadType type, jobject thread_handle, CompileQueue* queue, AbstractCompiler* comp, JavaThread* THREAD) {
 845   Handle thread_oop(THREAD, JNIHandles::resolve_non_null(thread_handle));
 846 
 847   if (java_lang_Thread::thread(thread_oop()) != nullptr) {
 848     assert(type == compiler_t, "should only happen with reused compiler threads");
 849     // The compiler thread hasn't actually exited yet so don't try to reuse it
 850     return nullptr;
 851   }
 852 
 853   JavaThread* new_thread = nullptr;
 854   switch (type) {
 855     case compiler_t:
 856       assert(comp != nullptr, "Compiler instance missing.");
 857       if (!InjectCompilerCreationFailure || comp->num_compiler_threads() == 0) {
 858         CompilerCounters* counters = new CompilerCounters();
 859         new_thread = new CompilerThread(queue, counters);
 860       }
 861       break;
 862 #if defined(ASSERT) && COMPILER2_OR_JVMCI
 863     case deoptimizer_t:
 864       new_thread = new DeoptimizeObjectsALotThread();
 865       break;
 866 #endif // ASSERT
 867     default:
 868       ShouldNotReachHere();
 869   }
 870 
 871   // At this point the new CompilerThread data-races with this startup
 872   // thread (which is the main thread and NOT the VM thread).
 873   // This means Java bytecodes being executed at startup can
 874   // queue compile jobs which will run at whatever default priority the
 875   // newly created CompilerThread runs at.
 876 
 877 
 878   // At this point it may be possible that no osthread was created for the
 879   // JavaThread due to lack of resources. We will handle that failure below.
 880   // Also check new_thread so that static analysis is happy.
 881   if (new_thread != nullptr && new_thread->osthread() != nullptr) {
 882 
 883     if (type == compiler_t) {
 884       CompilerThread::cast(new_thread)->set_compiler(comp);
 885     }
 886 
 887     // Note that we cannot call os::set_priority because it expects Java
 888     // priorities and we are *explicitly* using OS priorities so that it's
 889     // possible to set the compiler thread priority higher than any Java
 890     // thread.
 891 
 892     int native_prio = CompilerThreadPriority;
 893     if (native_prio == -1) {
 894       if (UseCriticalCompilerThreadPriority) {
 895         native_prio = os::java_to_os_priority[CriticalPriority];
 896       } else {
 897         native_prio = os::java_to_os_priority[NearMaxPriority];
 898       }
 899     }
 900     os::set_native_priority(new_thread, native_prio);
 901 
 902     // Note that this only sets the JavaThread _priority field, which by
 903     // definition is limited to Java priorities and not OS priorities.
 904     JavaThread::start_internal_daemon(THREAD, new_thread, thread_oop, NearMaxPriority);
 905 
 906   } else { // osthread initialization failure
 907     if (UseDynamicNumberOfCompilerThreads && type == compiler_t
 908         && comp->num_compiler_threads() > 0) {
 909       // The new thread is not known to Thread-SMR yet so we can just delete.
 910       delete new_thread;
 911       return nullptr;
 912     } else {
 913       vm_exit_during_initialization("java.lang.OutOfMemoryError",
 914                                     os::native_thread_creation_failed_msg());
 915     }
 916   }
 917 
 918   os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
 919 
 920   return new_thread;
 921 }
 922 
 923 static bool trace_compiler_threads() {
 924   LogTarget(Debug, jit, thread) lt;
 925   return TraceCompilerThreads || lt.is_enabled();
 926 }
 927 
 928 static jobject create_compiler_thread(AbstractCompiler* compiler, int i, TRAPS) {
 929   char name_buffer[256];
 930   os::snprintf_checked(name_buffer, sizeof(name_buffer), "%s CompilerThread%d", compiler->name(), i);
 931   Handle thread_oop = JavaThread::create_system_thread_object(name_buffer, CHECK_NULL);
 932   return JNIHandles::make_global(thread_oop);
 933 }
 934 
 935 static void print_compiler_threads(stringStream& msg) {
 936   if (TraceCompilerThreads) {
 937     tty->print_cr("%7d %s", (int)tty->time_stamp().milliseconds(), msg.as_string());
 938   }
 939   LogTarget(Debug, jit, thread) lt;
 940   if (lt.is_enabled()) {
 941     LogStream ls(lt);
 942     ls.print_cr("%s", msg.as_string());
 943   }
 944 }
 945 
 946 void CompileBroker::init_compiler_threads() {
 947   // Ensure any exceptions lead to vm_exit_during_initialization.
 948   EXCEPTION_MARK;
 949 #if !defined(ZERO)
 950   assert(_c2_count > 0 || _c1_count > 0, "No compilers?");
 951 #endif // !ZERO
 952   // Initialize the compilation queue
 953   if (_c2_count > 0) {
 954     const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue";
 955     _c2_compile_queue  = new CompileQueue(name);
 956     _compiler2_objects = NEW_C_HEAP_ARRAY(jobject, _c2_count, mtCompiler);
 957     _compiler2_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c2_count, mtCompiler);
 958   }
 959   if (_c1_count > 0) {
 960     _c1_compile_queue  = new CompileQueue("C1 compile queue");
 961     _compiler1_objects = NEW_C_HEAP_ARRAY(jobject, _c1_count, mtCompiler);
 962     _compiler1_logs = NEW_C_HEAP_ARRAY(CompileLog*, _c1_count, mtCompiler);
 963   }
 964 
 965   for (int i = 0; i < _c2_count; i++) {
 966     // Create a name for our thread.
 967     jobject thread_handle = create_compiler_thread(_compilers[1], i, CHECK);
 968     _compiler2_objects[i] = thread_handle;
 969     _compiler2_logs[i] = nullptr;
 970 
 971     if (!UseDynamicNumberOfCompilerThreads || i == 0) {
 972       JavaThread *ct = make_thread(compiler_t, thread_handle, _c2_compile_queue, _compilers[1], THREAD);
 973       assert(ct != nullptr, "should have been handled for initial thread");
 974       _compilers[1]->set_num_compiler_threads(i + 1);
 975       if (trace_compiler_threads()) {
 976         ResourceMark rm;
 977         ThreadsListHandle tlh;  // name() depends on the TLH.
 978         assert(tlh.includes(ct), "ct=" INTPTR_FORMAT " exited unexpectedly.", p2i(ct));
 979         stringStream msg;
 980         msg.print("Added initial compiler thread %s", ct->name());
 981         print_compiler_threads(msg);
 982       }
 983     }
 984   }
 985 
 986   for (int i = 0; i < _c1_count; i++) {
 987     // Create a name for our thread.
 988     jobject thread_handle = create_compiler_thread(_compilers[0], i, CHECK);
 989     _compiler1_objects[i] = thread_handle;
 990     _compiler1_logs[i] = nullptr;
 991 
 992     if (!UseDynamicNumberOfCompilerThreads || i == 0) {
 993       JavaThread *ct = make_thread(compiler_t, thread_handle, _c1_compile_queue, _compilers[0], THREAD);
 994       assert(ct != nullptr, "should have been handled for initial thread");
 995       _compilers[0]->set_num_compiler_threads(i + 1);
 996       if (trace_compiler_threads()) {
 997         ResourceMark rm;
 998         ThreadsListHandle tlh;  // name() depends on the TLH.
 999         assert(tlh.includes(ct), "ct=" INTPTR_FORMAT " exited unexpectedly.", p2i(ct));
1000         stringStream msg;
1001         msg.print("Added initial compiler thread %s", ct->name());
1002         print_compiler_threads(msg);
1003       }
1004     }
1005   }
1006 
1007   if (UsePerfData) {
1008     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, _c1_count + _c2_count, CHECK);
1009   }
1010 
1011 #if defined(ASSERT) && COMPILER2_OR_JVMCI
1012   if (DeoptimizeObjectsALot) {
1013     // Initialize and start the object deoptimizer threads
1014     const int total_count = DeoptimizeObjectsALotThreadCountSingle + DeoptimizeObjectsALotThreadCountAll;
1015     for (int count = 0; count < total_count; count++) {
1016       Handle thread_oop = JavaThread::create_system_thread_object("Deoptimize objects a lot single mode", CHECK);
1017       jobject thread_handle = JNIHandles::make_local(THREAD, thread_oop());
1018       make_thread(deoptimizer_t, thread_handle, nullptr, nullptr, THREAD);
1019     }
1020   }
1021 #endif // defined(ASSERT) && COMPILER2_OR_JVMCI
1022 }
1023 
1024 void CompileBroker::possibly_add_compiler_threads(JavaThread* THREAD) {
1025 
1026   julong free_memory = os::free_memory();
1027   // If SegmentedCodeCache is off, both values refer to the single heap (with type CodeBlobType::All).
1028   size_t available_cc_np  = CodeCache::unallocated_capacity(CodeBlobType::MethodNonProfiled),
1029          available_cc_p   = CodeCache::unallocated_capacity(CodeBlobType::MethodProfiled);
1030 
1031   // Only do attempt to start additional threads if the lock is free.
1032   if (!CompileThread_lock->try_lock()) return;
1033 
1034   if (_c2_compile_queue != nullptr) {
1035     int old_c2_count = _compilers[1]->num_compiler_threads();
1036     int new_c2_count = MIN4(_c2_count,
1037         _c2_compile_queue->size() / 2,
1038         (int)(free_memory / (200*M)),
1039         (int)(available_cc_np / (128*K)));
1040 
1041     for (int i = old_c2_count; i < new_c2_count; i++) {
1042 #if INCLUDE_JVMCI
1043       if (UseJVMCICompiler && !UseJVMCINativeLibrary && _compiler2_objects[i] == nullptr) {
1044         // Native compiler threads as used in C1/C2 can reuse the j.l.Thread objects as their
1045         // existence is completely hidden from the rest of the VM (and those compiler threads can't
1046         // call Java code to do the creation anyway).
1047         //
1048         // For pure Java JVMCI we have to create new j.l.Thread objects as they are visible and we
1049         // can see unexpected thread lifecycle transitions if we bind them to new JavaThreads.  For
1050         // native library JVMCI it's preferred to use the C1/C2 strategy as this avoids unnecessary
1051         // coupling with Java.
1052         if (!THREAD->can_call_java()) break;
1053         char name_buffer[256];
1054         os::snprintf_checked(name_buffer, sizeof(name_buffer), "%s CompilerThread%d", _compilers[1]->name(), i);
1055         Handle thread_oop;
1056         {
1057           // We have to give up the lock temporarily for the Java calls.
1058           MutexUnlocker mu(CompileThread_lock);
1059           thread_oop = JavaThread::create_system_thread_object(name_buffer, THREAD);
1060         }
1061         if (HAS_PENDING_EXCEPTION) {
1062           if (trace_compiler_threads()) {
1063             ResourceMark rm;
1064             stringStream msg;
1065             msg.print_cr("JVMCI compiler thread creation failed:");
1066             PENDING_EXCEPTION->print_on(&msg);
1067             print_compiler_threads(msg);
1068           }
1069           CLEAR_PENDING_EXCEPTION;
1070           break;
1071         }
1072         // Check if another thread has beaten us during the Java calls.
1073         if (_compilers[1]->num_compiler_threads() != i) break;
1074         jobject thread_handle = JNIHandles::make_global(thread_oop);
1075         assert(compiler2_object(i) == nullptr, "Old one must be released!");
1076         _compiler2_objects[i] = thread_handle;
1077       }
1078 #endif
1079       guarantee(compiler2_object(i) != nullptr, "Thread oop must exist");
1080       JavaThread *ct = make_thread(compiler_t, compiler2_object(i), _c2_compile_queue, _compilers[1], THREAD);
1081       if (ct == nullptr) break;
1082       _compilers[1]->set_num_compiler_threads(i + 1);
1083       if (trace_compiler_threads()) {
1084         ResourceMark rm;
1085         ThreadsListHandle tlh;  // name() depends on the TLH.
1086         assert(tlh.includes(ct), "ct=" INTPTR_FORMAT " exited unexpectedly.", p2i(ct));
1087         stringStream msg;
1088         msg.print("Added compiler thread %s (free memory: %dMB, available non-profiled code cache: %dMB)",
1089                   ct->name(), (int)(free_memory/M), (int)(available_cc_np/M));
1090         print_compiler_threads(msg);
1091       }
1092     }
1093   }
1094 
1095   if (_c1_compile_queue != nullptr) {
1096     int old_c1_count = _compilers[0]->num_compiler_threads();
1097     int new_c1_count = MIN4(_c1_count,
1098         _c1_compile_queue->size() / 4,
1099         (int)(free_memory / (100*M)),
1100         (int)(available_cc_p / (128*K)));
1101 
1102     for (int i = old_c1_count; i < new_c1_count; i++) {
1103       JavaThread *ct = make_thread(compiler_t, compiler1_object(i), _c1_compile_queue, _compilers[0], THREAD);
1104       if (ct == nullptr) break;
1105       _compilers[0]->set_num_compiler_threads(i + 1);
1106       if (trace_compiler_threads()) {
1107         ResourceMark rm;
1108         ThreadsListHandle tlh;  // name() depends on the TLH.
1109         assert(tlh.includes(ct), "ct=" INTPTR_FORMAT " exited unexpectedly.", p2i(ct));
1110         stringStream msg;
1111         msg.print("Added compiler thread %s (free memory: %dMB, available profiled code cache: %dMB)",
1112                   ct->name(), (int)(free_memory/M), (int)(available_cc_p/M));
1113         print_compiler_threads(msg);
1114       }
1115     }
1116   }
1117 
1118   CompileThread_lock->unlock();
1119 }
1120 
1121 
1122 /**
1123  * Set the methods on the stack as on_stack so that redefine classes doesn't
1124  * reclaim them. This method is executed at a safepoint.
1125  */
1126 void CompileBroker::mark_on_stack() {
1127   assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
1128   // Since we are at a safepoint, we do not need a lock to access
1129   // the compile queues.
1130   if (_c2_compile_queue != nullptr) {
1131     _c2_compile_queue->mark_on_stack();
1132   }
1133   if (_c1_compile_queue != nullptr) {
1134     _c1_compile_queue->mark_on_stack();
1135   }
1136 }
1137 
1138 // ------------------------------------------------------------------
1139 // CompileBroker::compile_method
1140 //
1141 // Request compilation of a method.
1142 void CompileBroker::compile_method_base(const methodHandle& method,
1143                                         int osr_bci,
1144                                         int comp_level,
1145                                         const methodHandle& hot_method,
1146                                         int hot_count,
1147                                         CompileTask::CompileReason compile_reason,
1148                                         bool blocking,
1149                                         Thread* thread) {
1150   guarantee(!method->is_abstract(), "cannot compile abstract methods");
1151   assert(method->method_holder()->is_instance_klass(),
1152          "sanity check");
1153   assert(!method->method_holder()->is_not_initialized(),
1154          "method holder must be initialized");
1155   assert(!method->is_method_handle_intrinsic(), "do not enqueue these guys");
1156 
1157   if (CIPrintRequests) {
1158     tty->print("request: ");
1159     method->print_short_name(tty);
1160     if (osr_bci != InvocationEntryBci) {
1161       tty->print(" osr_bci: %d", osr_bci);
1162     }
1163     tty->print(" level: %d comment: %s count: %d", comp_level, CompileTask::reason_name(compile_reason), hot_count);
1164     if (!hot_method.is_null()) {
1165       tty->print(" hot: ");
1166       if (hot_method() != method()) {
1167           hot_method->print_short_name(tty);
1168       } else {
1169         tty->print("yes");
1170       }
1171     }
1172     tty->cr();
1173   }
1174 
1175   // A request has been made for compilation.  Before we do any
1176   // real work, check to see if the method has been compiled
1177   // in the meantime with a definitive result.
1178   if (compilation_is_complete(method, osr_bci, comp_level)) {
1179     return;
1180   }
1181 
1182 #ifndef PRODUCT
1183   if (osr_bci != -1 && !FLAG_IS_DEFAULT(OSROnlyBCI)) {
1184     if ((OSROnlyBCI > 0) ? (OSROnlyBCI != osr_bci) : (-OSROnlyBCI == osr_bci)) {
1185       // Positive OSROnlyBCI means only compile that bci.  Negative means don't compile that BCI.
1186       return;
1187     }
1188   }
1189 #endif
1190 
1191   // If this method is already in the compile queue, then
1192   // we do not block the current thread.
1193   if (compilation_is_in_queue(method)) {
1194     // We may want to decay our counter a bit here to prevent
1195     // multiple denied requests for compilation.  This is an
1196     // open compilation policy issue. Note: The other possibility,
1197     // in the case that this is a blocking compile request, is to have
1198     // all subsequent blocking requesters wait for completion of
1199     // ongoing compiles. Note that in this case we'll need a protocol
1200     // for freeing the associated compile tasks. [Or we could have
1201     // a single static monitor on which all these waiters sleep.]
1202     return;
1203   }
1204 
1205   // Tiered policy requires MethodCounters to exist before adding a method to
1206   // the queue. Create if we don't have them yet.
1207   method->get_method_counters(thread);
1208 
1209   // Outputs from the following MutexLocker block:
1210   CompileTask* task     = nullptr;
1211   CompileQueue* queue  = compile_queue(comp_level);
1212 
1213   // Acquire our lock.
1214   {
1215     MutexLocker locker(thread, MethodCompileQueue_lock);
1216 
1217     // Make sure the method has not slipped into the queues since
1218     // last we checked; note that those checks were "fast bail-outs".
1219     // Here we need to be more careful, see 14012000 below.
1220     if (compilation_is_in_queue(method)) {
1221       return;
1222     }
1223 
1224     // We need to check again to see if the compilation has
1225     // completed.  A previous compilation may have registered
1226     // some result.
1227     if (compilation_is_complete(method, osr_bci, comp_level)) {
1228       return;
1229     }
1230 
1231     // We now know that this compilation is not pending, complete,
1232     // or prohibited.  Assign a compile_id to this compilation
1233     // and check to see if it is in our [Start..Stop) range.
1234     int compile_id = assign_compile_id(method, osr_bci);
1235     if (compile_id == 0) {
1236       // The compilation falls outside the allowed range.
1237       return;
1238     }
1239 
1240 #if INCLUDE_JVMCI
1241     if (UseJVMCICompiler && blocking) {
1242       // Don't allow blocking compiles for requests triggered by JVMCI.
1243       if (thread->is_Compiler_thread()) {
1244         blocking = false;
1245       }
1246 
1247       // In libjvmci, JVMCI initialization should not deadlock with other threads
1248       if (!UseJVMCINativeLibrary) {
1249         // Don't allow blocking compiles if inside a class initializer or while performing class loading
1250         vframeStream vfst(JavaThread::cast(thread));
1251         for (; !vfst.at_end(); vfst.next()) {
1252           if (vfst.method()->is_static_initializer() ||
1253               (vfst.method()->method_holder()->is_subclass_of(vmClasses::ClassLoader_klass()) &&
1254                   vfst.method()->name() == vmSymbols::loadClass_name())) {
1255             blocking = false;
1256             break;
1257           }
1258         }
1259 
1260         // Don't allow blocking compilation requests to JVMCI
1261         // if JVMCI itself is not yet initialized
1262         if (!JVMCI::is_compiler_initialized() && compiler(comp_level)->is_jvmci()) {
1263           blocking = false;
1264         }
1265       }
1266 
1267       // Don't allow blocking compilation requests if we are in JVMCIRuntime::shutdown
1268       // to avoid deadlock between compiler thread(s) and threads run at shutdown
1269       // such as the DestroyJavaVM thread.
1270       if (JVMCI::in_shutdown()) {
1271         blocking = false;
1272       }
1273     }
1274 #endif // INCLUDE_JVMCI
1275 
1276     // We will enter the compilation in the queue.
1277     // 14012000: Note that this sets the queued_for_compile bits in
1278     // the target method. We can now reason that a method cannot be
1279     // queued for compilation more than once, as follows:
1280     // Before a thread queues a task for compilation, it first acquires
1281     // the compile queue lock, then checks if the method's queued bits
1282     // are set or it has already been compiled. Thus there can not be two
1283     // instances of a compilation task for the same method on the
1284     // compilation queue. Consider now the case where the compilation
1285     // thread has already removed a task for that method from the queue
1286     // and is in the midst of compiling it. In this case, the
1287     // queued_for_compile bits must be set in the method (and these
1288     // will be visible to the current thread, since the bits were set
1289     // under protection of the compile queue lock, which we hold now.
1290     // When the compilation completes, the compiler thread first sets
1291     // the compilation result and then clears the queued_for_compile
1292     // bits. Neither of these actions are protected by a barrier (or done
1293     // under the protection of a lock), so the only guarantee we have
1294     // (on machines with TSO (Total Store Order)) is that these values
1295     // will update in that order. As a result, the only combinations of
1296     // these bits that the current thread will see are, in temporal order:
1297     // <RESULT, QUEUE> :
1298     //     <0, 1> : in compile queue, but not yet compiled
1299     //     <1, 1> : compiled but queue bit not cleared
1300     //     <1, 0> : compiled and queue bit cleared
1301     // Because we first check the queue bits then check the result bits,
1302     // we are assured that we cannot introduce a duplicate task.
1303     // Note that if we did the tests in the reverse order (i.e. check
1304     // result then check queued bit), we could get the result bit before
1305     // the compilation completed, and the queue bit after the compilation
1306     // completed, and end up introducing a "duplicate" (redundant) task.
1307     // In that case, the compiler thread should first check if a method
1308     // has already been compiled before trying to compile it.
1309     // NOTE: in the event that there are multiple compiler threads and
1310     // there is de-optimization/recompilation, things will get hairy,
1311     // and in that case it's best to protect both the testing (here) of
1312     // these bits, and their updating (here and elsewhere) under a
1313     // common lock.
1314     task = create_compile_task(queue,
1315                                compile_id, method,
1316                                osr_bci, comp_level,
1317                                hot_method, hot_count, compile_reason,
1318                                blocking);
1319   }
1320 
1321   if (blocking) {
1322     wait_for_completion(task);
1323   }
1324 }
1325 
1326 nmethod* CompileBroker::compile_method(const methodHandle& method, int osr_bci,
1327                                        int comp_level,
1328                                        const methodHandle& hot_method, int hot_count,
1329                                        CompileTask::CompileReason compile_reason,
1330                                        TRAPS) {
1331   // Do nothing if compilebroker is not initialized or compiles are submitted on level none
1332   if (!_initialized || comp_level == CompLevel_none) {
1333     return nullptr;
1334   }
1335 
1336   AbstractCompiler *comp = CompileBroker::compiler(comp_level);
1337   assert(comp != nullptr, "Ensure we have a compiler");
1338 
1339 #if INCLUDE_JVMCI
1340   if (comp->is_jvmci() && !JVMCI::can_initialize_JVMCI()) {
1341     // JVMCI compilation is not yet initializable.
1342     return nullptr;
1343   }
1344 #endif
1345 
1346   DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, comp);
1347   // CompileBroker::compile_method can trap and can have pending async exception.
1348   nmethod* nm = CompileBroker::compile_method(method, osr_bci, comp_level, hot_method, hot_count, compile_reason, directive, THREAD);
1349   DirectivesStack::release(directive);
1350   return nm;
1351 }
1352 
1353 nmethod* CompileBroker::compile_method(const methodHandle& method, int osr_bci,
1354                                          int comp_level,
1355                                          const methodHandle& hot_method, int hot_count,
1356                                          CompileTask::CompileReason compile_reason,
1357                                          DirectiveSet* directive,
1358                                          TRAPS) {
1359 
1360   // make sure arguments make sense
1361   assert(method->method_holder()->is_instance_klass(), "not an instance method");
1362   assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range");
1363   assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods");
1364   assert(!method->method_holder()->is_not_initialized(), "method holder must be initialized");
1365   // return quickly if possible
1366 
1367   // lock, make sure that the compilation
1368   // isn't prohibited in a straightforward way.
1369   AbstractCompiler* comp = CompileBroker::compiler(comp_level);
1370   if (comp == nullptr || compilation_is_prohibited(method, osr_bci, comp_level, directive->ExcludeOption)) {
1371     return nullptr;
1372   }
1373 
1374   if (osr_bci == InvocationEntryBci) {
1375     // standard compilation
1376     nmethod* method_code = method->code();
1377     if (method_code != nullptr) {
1378       if (compilation_is_complete(method, osr_bci, comp_level)) {
1379         return method_code;
1380       }
1381     }
1382     if (method->is_not_compilable(comp_level)) {
1383       return nullptr;
1384     }
1385   } else {
1386     // osr compilation
1387     // We accept a higher level osr method
1388     nmethod* nm = method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
1389     if (nm != nullptr) return nm;
1390     if (method->is_not_osr_compilable(comp_level)) return nullptr;
1391   }
1392 
1393   assert(!HAS_PENDING_EXCEPTION, "No exception should be present");
1394   // some prerequisites that are compiler specific
1395   if (comp->is_c2() || comp->is_jvmci()) {
1396     InternalOOMEMark iom(THREAD);
1397     method->constants()->resolve_string_constants(CHECK_AND_CLEAR_NONASYNC_NULL);
1398     // Resolve all classes seen in the signature of the method
1399     // we are compiling.
1400     Method::load_signature_classes(method, CHECK_AND_CLEAR_NONASYNC_NULL);
1401   }
1402 
1403   // If the method is native, do the lookup in the thread requesting
1404   // the compilation. Native lookups can load code, which is not
1405   // permitted during compilation.
1406   //
1407   // Note: A native method implies non-osr compilation which is
1408   //       checked with an assertion at the entry of this method.
1409   if (method->is_native() && !method->is_method_handle_intrinsic()) {
1410     address adr = NativeLookup::lookup(method, THREAD);
1411     if (HAS_PENDING_EXCEPTION) {
1412       // In case of an exception looking up the method, we just forget
1413       // about it. The interpreter will kick-in and throw the exception.
1414       method->set_not_compilable("NativeLookup::lookup failed"); // implies is_not_osr_compilable()
1415       CLEAR_PENDING_EXCEPTION;
1416       return nullptr;
1417     }
1418     assert(method->has_native_function(), "must have native code by now");
1419   }
1420 
1421   // RedefineClasses() has replaced this method; just return
1422   if (method->is_old()) {
1423     return nullptr;
1424   }
1425 
1426   // JVMTI -- post_compile_event requires jmethod_id() that may require
1427   // a lock the compiling thread can not acquire. Prefetch it here.
1428   if (JvmtiExport::should_post_compiled_method_load()) {
1429     method->jmethod_id();
1430   }
1431 
1432   // do the compilation
1433   if (method->is_native()) {
1434     if (!PreferInterpreterNativeStubs || method->is_method_handle_intrinsic()) {
1435 #if defined(X86) && !defined(ZERO)
1436       // The following native methods:
1437       //
1438       // java.lang.Float.intBitsToFloat
1439       // java.lang.Float.floatToRawIntBits
1440       // java.lang.Double.longBitsToDouble
1441       // java.lang.Double.doubleToRawLongBits
1442       //
1443       // are called through the interpreter even if interpreter native stubs
1444       // are not preferred (i.e., calling through adapter handlers is preferred).
1445       // The reason is that on x86_32 signaling NaNs (sNaNs) are not preserved
1446       // if the version of the methods from the native libraries is called.
1447       // As the interpreter and the C2-intrinsified version of the methods preserves
1448       // sNaNs, that would result in an inconsistent way of handling of sNaNs.
1449       if ((UseSSE >= 1 &&
1450           (method->intrinsic_id() == vmIntrinsics::_intBitsToFloat ||
1451            method->intrinsic_id() == vmIntrinsics::_floatToRawIntBits)) ||
1452           (UseSSE >= 2 &&
1453            (method->intrinsic_id() == vmIntrinsics::_longBitsToDouble ||
1454             method->intrinsic_id() == vmIntrinsics::_doubleToRawLongBits))) {
1455         return nullptr;
1456       }
1457 #endif // X86 && !ZERO
1458 
1459       // To properly handle the appendix argument for out-of-line calls we are using a small trampoline that
1460       // pops off the appendix argument and jumps to the target (see gen_special_dispatch in SharedRuntime).
1461       //
1462       // Since normal compiled-to-compiled calls are not able to handle such a thing we MUST generate an adapter
1463       // in this case.  If we can't generate one and use it we can not execute the out-of-line method handle calls.
1464       AdapterHandlerLibrary::create_native_wrapper(method);
1465     } else {
1466       return nullptr;
1467     }
1468   } else {
1469     // If the compiler is shut off due to code cache getting full
1470     // fail out now so blocking compiles dont hang the java thread
1471     if (!should_compile_new_jobs()) {
1472       return nullptr;
1473     }
1474     bool is_blocking = !directive->BackgroundCompilationOption || ReplayCompiles;
1475     compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, compile_reason, is_blocking, THREAD);
1476   }
1477 
1478   // return requested nmethod
1479   // We accept a higher level osr method
1480   if (osr_bci == InvocationEntryBci) {
1481     return method->code();
1482   }
1483   return method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
1484 }
1485 
1486 
1487 // ------------------------------------------------------------------
1488 // CompileBroker::compilation_is_complete
1489 //
1490 // See if compilation of this method is already complete.
1491 bool CompileBroker::compilation_is_complete(const methodHandle& method,
1492                                             int                 osr_bci,
1493                                             int                 comp_level) {
1494   bool is_osr = (osr_bci != standard_entry_bci);
1495   if (is_osr) {
1496     if (method->is_not_osr_compilable(comp_level)) {
1497       return true;
1498     } else {
1499       nmethod* result = method->lookup_osr_nmethod_for(osr_bci, comp_level, true);
1500       return (result != nullptr);
1501     }
1502   } else {
1503     if (method->is_not_compilable(comp_level)) {
1504       return true;
1505     } else {
1506       nmethod* result = method->code();
1507       if (result == nullptr) return false;
1508       return comp_level == result->comp_level();
1509     }
1510   }
1511 }
1512 
1513 
1514 /**
1515  * See if this compilation is already requested.
1516  *
1517  * Implementation note: there is only a single "is in queue" bit
1518  * for each method.  This means that the check below is overly
1519  * conservative in the sense that an osr compilation in the queue
1520  * will block a normal compilation from entering the queue (and vice
1521  * versa).  This can be remedied by a full queue search to disambiguate
1522  * cases.  If it is deemed profitable, this may be done.
1523  */
1524 bool CompileBroker::compilation_is_in_queue(const methodHandle& method) {
1525   return method->queued_for_compilation();
1526 }
1527 
1528 // ------------------------------------------------------------------
1529 // CompileBroker::compilation_is_prohibited
1530 //
1531 // See if this compilation is not allowed.
1532 bool CompileBroker::compilation_is_prohibited(const methodHandle& method, int osr_bci, int comp_level, bool excluded) {
1533   bool is_native = method->is_native();
1534   // Some compilers may not support the compilation of natives.
1535   AbstractCompiler *comp = compiler(comp_level);
1536   if (is_native && (!CICompileNatives || comp == nullptr)) {
1537     method->set_not_compilable_quietly("native methods not supported", comp_level);
1538     return true;
1539   }
1540 
1541   bool is_osr = (osr_bci != standard_entry_bci);
1542   // Some compilers may not support on stack replacement.
1543   if (is_osr && (!CICompileOSR || comp == nullptr)) {
1544     method->set_not_osr_compilable("OSR not supported", comp_level);
1545     return true;
1546   }
1547 
1548   // The method may be explicitly excluded by the user.
1549   double scale;
1550   if (excluded || (CompilerOracle::has_option_value(method, CompileCommandEnum::CompileThresholdScaling, scale) && scale == 0)) {
1551     bool quietly = CompilerOracle::be_quiet();
1552     if (PrintCompilation && !quietly) {
1553       // This does not happen quietly...
1554       ResourceMark rm;
1555       tty->print("### Excluding %s:%s",
1556                  method->is_native() ? "generation of native wrapper" : "compile",
1557                  (method->is_static() ? " static" : ""));
1558       method->print_short_name(tty);
1559       tty->cr();
1560     }
1561     method->set_not_compilable("excluded by CompileCommand", comp_level, !quietly);
1562   }
1563 
1564   return false;
1565 }
1566 
1567 /**
1568  * Generate serialized IDs for compilation requests. If certain debugging flags are used
1569  * and the ID is not within the specified range, the method is not compiled and 0 is returned.
1570  * The function also allows to generate separate compilation IDs for OSR compilations.
1571  */
1572 int CompileBroker::assign_compile_id(const methodHandle& method, int osr_bci) {
1573 #ifdef ASSERT
1574   bool is_osr = (osr_bci != standard_entry_bci);
1575   int id;
1576   if (method->is_native()) {
1577     assert(!is_osr, "can't be osr");
1578     // Adapters, native wrappers and method handle intrinsics
1579     // should be generated always.
1580     return Atomic::add(CICountNative ? &_native_compilation_id : &_compilation_id, 1);
1581   } else if (CICountOSR && is_osr) {
1582     id = Atomic::add(&_osr_compilation_id, 1);
1583     if (CIStartOSR <= id && id < CIStopOSR) {
1584       return id;
1585     }
1586   } else {
1587     id = Atomic::add(&_compilation_id, 1);
1588     if (CIStart <= id && id < CIStop) {
1589       return id;
1590     }
1591   }
1592 
1593   // Method was not in the appropriate compilation range.
1594   method->set_not_compilable_quietly("Not in requested compile id range");
1595   return 0;
1596 #else
1597   // CICountOSR is a develop flag and set to 'false' by default. In a product built,
1598   // only _compilation_id is incremented.
1599   return Atomic::add(&_compilation_id, 1);
1600 #endif
1601 }
1602 
1603 // ------------------------------------------------------------------
1604 // CompileBroker::assign_compile_id_unlocked
1605 //
1606 // Public wrapper for assign_compile_id that acquires the needed locks
1607 int CompileBroker::assign_compile_id_unlocked(Thread* thread, const methodHandle& method, int osr_bci) {
1608   MutexLocker locker(thread, MethodCompileQueue_lock);
1609   return assign_compile_id(method, osr_bci);
1610 }
1611 
1612 // ------------------------------------------------------------------
1613 // CompileBroker::create_compile_task
1614 //
1615 // Create a CompileTask object representing the current request for
1616 // compilation.  Add this task to the queue.
1617 CompileTask* CompileBroker::create_compile_task(CompileQueue*       queue,
1618                                                 int                 compile_id,
1619                                                 const methodHandle& method,
1620                                                 int                 osr_bci,
1621                                                 int                 comp_level,
1622                                                 const methodHandle& hot_method,
1623                                                 int                 hot_count,
1624                                                 CompileTask::CompileReason compile_reason,
1625                                                 bool                blocking) {
1626   CompileTask* new_task = CompileTask::allocate();
1627   new_task->initialize(compile_id, method, osr_bci, comp_level,
1628                        hot_method, hot_count, compile_reason,
1629                        blocking);
1630   queue->add(new_task);
1631   return new_task;
1632 }
1633 
1634 #if INCLUDE_JVMCI
1635 // The number of milliseconds to wait before checking if
1636 // JVMCI compilation has made progress.
1637 static const long JVMCI_COMPILATION_PROGRESS_WAIT_TIMESLICE = 1000;
1638 
1639 // The number of JVMCI compilation progress checks that must fail
1640 // before unblocking a thread waiting for a blocking compilation.
1641 static const int JVMCI_COMPILATION_PROGRESS_WAIT_ATTEMPTS = 10;
1642 
1643 /**
1644  * Waits for a JVMCI compiler to complete a given task. This thread
1645  * waits until either the task completes or it sees no JVMCI compilation
1646  * progress for N consecutive milliseconds where N is
1647  * JVMCI_COMPILATION_PROGRESS_WAIT_TIMESLICE *
1648  * JVMCI_COMPILATION_PROGRESS_WAIT_ATTEMPTS.
1649  *
1650  * @return true if this thread needs to free/recycle the task
1651  */
1652 bool CompileBroker::wait_for_jvmci_completion(JVMCICompiler* jvmci, CompileTask* task, JavaThread* thread) {
1653   assert(UseJVMCICompiler, "sanity");
1654   MonitorLocker ml(thread, task->lock());
1655   int progress_wait_attempts = 0;
1656   jint thread_jvmci_compilation_ticks = 0;
1657   jint global_jvmci_compilation_ticks = jvmci->global_compilation_ticks();
1658   while (!task->is_complete() && !is_compilation_disabled_forever() &&
1659          ml.wait(JVMCI_COMPILATION_PROGRESS_WAIT_TIMESLICE)) {
1660     JVMCICompileState* jvmci_compile_state = task->blocking_jvmci_compile_state();
1661 
1662     bool progress;
1663     if (jvmci_compile_state != nullptr) {
1664       jint ticks = jvmci_compile_state->compilation_ticks();
1665       progress = (ticks - thread_jvmci_compilation_ticks) != 0;
1666       JVMCI_event_1("waiting on compilation %d [ticks=%d]", task->compile_id(), ticks);
1667       thread_jvmci_compilation_ticks = ticks;
1668     } else {
1669       // Still waiting on JVMCI compiler queue. This thread may be holding a lock
1670       // that all JVMCI compiler threads are blocked on. We use the global JVMCI
1671       // compilation ticks to determine whether JVMCI compilation
1672       // is still making progress through the JVMCI compiler queue.
1673       jint ticks = jvmci->global_compilation_ticks();
1674       progress = (ticks - global_jvmci_compilation_ticks) != 0;
1675       JVMCI_event_1("waiting on compilation %d to be queued [ticks=%d]", task->compile_id(), ticks);
1676       global_jvmci_compilation_ticks = ticks;
1677     }
1678 
1679     if (!progress) {
1680       if (++progress_wait_attempts == JVMCI_COMPILATION_PROGRESS_WAIT_ATTEMPTS) {
1681         if (PrintCompilation) {
1682           task->print(tty, "wait for blocking compilation timed out");
1683         }
1684         JVMCI_event_1("waiting on compilation %d timed out", task->compile_id());
1685         break;
1686       }
1687     } else {
1688       progress_wait_attempts = 0;
1689     }
1690   }
1691   task->clear_waiter();
1692   return task->is_complete();
1693 }
1694 #endif
1695 
1696 /**
1697  *  Wait for the compilation task to complete.
1698  */
1699 void CompileBroker::wait_for_completion(CompileTask* task) {
1700   if (CIPrintCompileQueue) {
1701     ttyLocker ttyl;
1702     tty->print_cr("BLOCKING FOR COMPILE");
1703   }
1704 
1705   assert(task->is_blocking(), "can only wait on blocking task");
1706 
1707   JavaThread* thread = JavaThread::current();
1708 
1709   methodHandle method(thread, task->method());
1710   bool free_task;
1711 #if INCLUDE_JVMCI
1712   AbstractCompiler* comp = compiler(task->comp_level());
1713   if (!UseJVMCINativeLibrary && comp->is_jvmci() && !task->should_wait_for_compilation()) {
1714     // It may return before compilation is completed.
1715     // Note that libjvmci should not pre-emptively unblock
1716     // a thread waiting for a compilation as it does not call
1717     // Java code and so is not deadlock prone like jarjvmci.
1718     free_task = wait_for_jvmci_completion((JVMCICompiler*) comp, task, thread);
1719   } else
1720 #endif
1721   {
1722     MonitorLocker ml(thread, task->lock());
1723     free_task = true;
1724     while (!task->is_complete() && !is_compilation_disabled_forever()) {
1725       ml.wait();
1726     }
1727   }
1728 
1729   if (free_task) {
1730     if (is_compilation_disabled_forever()) {
1731       CompileTask::free(task);
1732       return;
1733     }
1734 
1735     // It is harmless to check this status without the lock, because
1736     // completion is a stable property (until the task object is recycled).
1737     assert(task->is_complete(), "Compilation should have completed");
1738 
1739     // By convention, the waiter is responsible for recycling a
1740     // blocking CompileTask. Since there is only one waiter ever
1741     // waiting on a CompileTask, we know that no one else will
1742     // be using this CompileTask; we can free it.
1743     CompileTask::free(task);
1744   }
1745 }
1746 
1747 /**
1748  * Initialize compiler thread(s) + compiler object(s). The postcondition
1749  * of this function is that the compiler runtimes are initialized and that
1750  * compiler threads can start compiling.
1751  */
1752 bool CompileBroker::init_compiler_runtime() {
1753   CompilerThread* thread = CompilerThread::current();
1754   AbstractCompiler* comp = thread->compiler();
1755   // Final sanity check - the compiler object must exist
1756   guarantee(comp != nullptr, "Compiler object must exist");
1757 
1758   {
1759     // Must switch to native to allocate ci_env
1760     ThreadToNativeFromVM ttn(thread);
1761     ciEnv ci_env((CompileTask*)nullptr);
1762     // Cache Jvmti state
1763     ci_env.cache_jvmti_state();
1764     // Cache DTrace flags
1765     ci_env.cache_dtrace_flags();
1766 
1767     // Switch back to VM state to do compiler initialization
1768     ThreadInVMfromNative tv(thread);
1769 
1770     // Perform per-thread and global initializations
1771     comp->initialize();
1772   }
1773 
1774   if (comp->is_failed()) {
1775     disable_compilation_forever();
1776     // If compiler initialization failed, no compiler thread that is specific to a
1777     // particular compiler runtime will ever start to compile methods.
1778     shutdown_compiler_runtime(comp, thread);
1779     return false;
1780   }
1781 
1782   // C1 specific check
1783   if (comp->is_c1() && (thread->get_buffer_blob() == nullptr)) {
1784     warning("Initialization of %s thread failed (no space to run compilers)", thread->name());
1785     return false;
1786   }
1787 
1788   return true;
1789 }
1790 
1791 void CompileBroker::free_buffer_blob_if_allocated(CompilerThread* thread) {
1792   BufferBlob* blob = thread->get_buffer_blob();
1793   if (blob != nullptr) {
1794     blob->purge();
1795     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1796     CodeCache::free(blob);
1797   }
1798 }
1799 
1800 /**
1801  * If C1 and/or C2 initialization failed, we shut down all compilation.
1802  * We do this to keep things simple. This can be changed if it ever turns
1803  * out to be a problem.
1804  */
1805 void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) {
1806   free_buffer_blob_if_allocated(thread);
1807 
1808   if (comp->should_perform_shutdown()) {
1809     // There are two reasons for shutting down the compiler
1810     // 1) compiler runtime initialization failed
1811     // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing
1812     warning("%s initialization failed. Shutting down all compilers", comp->name());
1813 
1814     // Only one thread per compiler runtime object enters here
1815     // Set state to shut down
1816     comp->set_shut_down();
1817 
1818     // Delete all queued compilation tasks to make compiler threads exit faster.
1819     if (_c1_compile_queue != nullptr) {
1820       _c1_compile_queue->free_all();
1821     }
1822 
1823     if (_c2_compile_queue != nullptr) {
1824       _c2_compile_queue->free_all();
1825     }
1826 
1827     // Set flags so that we continue execution with using interpreter only.
1828     UseCompiler    = false;
1829     UseInterpreter = true;
1830 
1831     // We could delete compiler runtimes also. However, there are references to
1832     // the compiler runtime(s) (e.g.,  nmethod::is_compiled_by_c1()) which then
1833     // fail. This can be done later if necessary.
1834   }
1835 }
1836 
1837 /**
1838  * Helper function to create new or reuse old CompileLog.
1839  */
1840 CompileLog* CompileBroker::get_log(CompilerThread* ct) {
1841   if (!LogCompilation) return nullptr;
1842 
1843   AbstractCompiler *compiler = ct->compiler();
1844   bool c1 = compiler->is_c1();
1845   jobject* compiler_objects = c1 ? _compiler1_objects : _compiler2_objects;
1846   assert(compiler_objects != nullptr, "must be initialized at this point");
1847   CompileLog** logs = c1 ? _compiler1_logs : _compiler2_logs;
1848   assert(logs != nullptr, "must be initialized at this point");
1849   int count = c1 ? _c1_count : _c2_count;
1850 
1851   // Find Compiler number by its threadObj.
1852   oop compiler_obj = ct->threadObj();
1853   int compiler_number = 0;
1854   bool found = false;
1855   for (; compiler_number < count; compiler_number++) {
1856     if (JNIHandles::resolve_non_null(compiler_objects[compiler_number]) == compiler_obj) {
1857       found = true;
1858       break;
1859     }
1860   }
1861   assert(found, "Compiler must exist at this point");
1862 
1863   // Determine pointer for this thread's log.
1864   CompileLog** log_ptr = &logs[compiler_number];
1865 
1866   // Return old one if it exists.
1867   CompileLog* log = *log_ptr;
1868   if (log != nullptr) {
1869     ct->init_log(log);
1870     return log;
1871   }
1872 
1873   // Create a new one and remember it.
1874   init_compiler_thread_log();
1875   log = ct->log();
1876   *log_ptr = log;
1877   return log;
1878 }
1879 
1880 // ------------------------------------------------------------------
1881 // CompileBroker::compiler_thread_loop
1882 //
1883 // The main loop run by a CompilerThread.
1884 void CompileBroker::compiler_thread_loop() {
1885   CompilerThread* thread = CompilerThread::current();
1886   CompileQueue* queue = thread->queue();
1887   // For the thread that initializes the ciObjectFactory
1888   // this resource mark holds all the shared objects
1889   ResourceMark rm;
1890 
1891   // First thread to get here will initialize the compiler interface
1892 
1893   {
1894     ASSERT_IN_VM;
1895     MutexLocker only_one (thread, CompileThread_lock);
1896     if (!ciObjectFactory::is_initialized()) {
1897       ciObjectFactory::initialize();
1898     }
1899   }
1900 
1901   // Open a log.
1902   CompileLog* log = get_log(thread);
1903   if (log != nullptr) {
1904     log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
1905                     thread->name(),
1906                     os::current_thread_id(),
1907                     os::current_process_id());
1908     log->stamp();
1909     log->end_elem();
1910   }
1911 
1912   // If compiler thread/runtime initialization fails, exit the compiler thread
1913   if (!init_compiler_runtime()) {
1914     return;
1915   }
1916 
1917   thread->start_idle_timer();
1918 
1919   // Poll for new compilation tasks as long as the JVM runs. Compilation
1920   // should only be disabled if something went wrong while initializing the
1921   // compiler runtimes. This, in turn, should not happen. The only known case
1922   // when compiler runtime initialization fails is if there is not enough free
1923   // space in the code cache to generate the necessary stubs, etc.
1924   while (!is_compilation_disabled_forever()) {
1925     // We need this HandleMark to avoid leaking VM handles.
1926     HandleMark hm(thread);
1927 
1928     CompileTask* task = queue->get(thread);
1929     if (task == nullptr) {
1930       if (UseDynamicNumberOfCompilerThreads) {
1931         // Access compiler_count under lock to enforce consistency.
1932         MutexLocker only_one(CompileThread_lock);
1933         if (can_remove(thread, true)) {
1934           if (trace_compiler_threads()) {
1935             ResourceMark rm;
1936             stringStream msg;
1937             msg.print("Removing compiler thread %s after " JLONG_FORMAT " ms idle time",
1938                       thread->name(), thread->idle_time_millis());
1939             print_compiler_threads(msg);
1940           }
1941 
1942           // Notify compiler that the compiler thread is about to stop
1943           thread->compiler()->stopping_compiler_thread(thread);
1944 
1945           free_buffer_blob_if_allocated(thread);
1946           return; // Stop this thread.
1947         }
1948       }
1949     } else {
1950       // Assign the task to the current thread.  Mark this compilation
1951       // thread as active for the profiler.
1952       // CompileTaskWrapper also keeps the Method* from being deallocated if redefinition
1953       // occurs after fetching the compile task off the queue.
1954       CompileTaskWrapper ctw(task);
1955       methodHandle method(thread, task->method());
1956 
1957       // Never compile a method if breakpoints are present in it
1958       if (method()->number_of_breakpoints() == 0) {
1959         // Compile the method.
1960         if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
1961           invoke_compiler_on_method(task);
1962           thread->start_idle_timer();
1963         } else {
1964           // After compilation is disabled, remove remaining methods from queue
1965           method->clear_queued_for_compilation();
1966           task->set_failure_reason("compilation is disabled");
1967         }
1968       } else {
1969         task->set_failure_reason("breakpoints are present");
1970       }
1971 
1972       if (UseDynamicNumberOfCompilerThreads) {
1973         possibly_add_compiler_threads(thread);
1974         assert(!thread->has_pending_exception(), "should have been handled");
1975       }
1976     }
1977   }
1978 
1979   // Shut down compiler runtime
1980   shutdown_compiler_runtime(thread->compiler(), thread);
1981 }
1982 
1983 // ------------------------------------------------------------------
1984 // CompileBroker::init_compiler_thread_log
1985 //
1986 // Set up state required by +LogCompilation.
1987 void CompileBroker::init_compiler_thread_log() {
1988     CompilerThread* thread = CompilerThread::current();
1989     char  file_name[4*K];
1990     FILE* fp = nullptr;
1991     intx thread_id = os::current_thread_id();
1992     for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) {
1993       const char* dir = (try_temp_dir ? os::get_temp_directory() : nullptr);
1994       if (dir == nullptr) {
1995         jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log",
1996                      thread_id, os::current_process_id());
1997       } else {
1998         jio_snprintf(file_name, sizeof(file_name),
1999                      "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir,
2000                      os::file_separator(), thread_id, os::current_process_id());
2001       }
2002 
2003       fp = os::fopen(file_name, "wt");
2004       if (fp != nullptr) {
2005         if (LogCompilation && Verbose) {
2006           tty->print_cr("Opening compilation log %s", file_name);
2007         }
2008         CompileLog* log = new(mtCompiler) CompileLog(file_name, fp, thread_id);
2009         if (log == nullptr) {
2010           fclose(fp);
2011           return;
2012         }
2013         thread->init_log(log);
2014 
2015         if (xtty != nullptr) {
2016           ttyLocker ttyl;
2017           // Record any per thread log files
2018           xtty->elem("thread_logfile thread='" INTX_FORMAT "' filename='%s'", thread_id, file_name);
2019         }
2020         return;
2021       }
2022     }
2023     warning("Cannot open log file: %s", file_name);
2024 }
2025 
2026 void CompileBroker::log_metaspace_failure() {
2027   const char* message = "some methods may not be compiled because metaspace "
2028                         "is out of memory";
2029   if (CompilationLog::log() != nullptr) {
2030     CompilationLog::log()->log_metaspace_failure(message);
2031   }
2032   if (PrintCompilation) {
2033     tty->print_cr("COMPILE PROFILING SKIPPED: %s", message);
2034   }
2035 }
2036 
2037 
2038 // ------------------------------------------------------------------
2039 // CompileBroker::set_should_block
2040 //
2041 // Set _should_block.
2042 // Call this from the VM, with Threads_lock held and a safepoint requested.
2043 void CompileBroker::set_should_block() {
2044   assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
2045   assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint already");
2046 #ifndef PRODUCT
2047   if (PrintCompilation && (Verbose || WizardMode))
2048     tty->print_cr("notifying compiler thread pool to block");
2049 #endif
2050   _should_block = true;
2051 }
2052 
2053 // ------------------------------------------------------------------
2054 // CompileBroker::maybe_block
2055 //
2056 // Call this from the compiler at convenient points, to poll for _should_block.
2057 void CompileBroker::maybe_block() {
2058   if (_should_block) {
2059 #ifndef PRODUCT
2060     if (PrintCompilation && (Verbose || WizardMode))
2061       tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current()));
2062 #endif
2063     ThreadInVMfromNative tivfn(JavaThread::current());
2064   }
2065 }
2066 
2067 // wrapper for CodeCache::print_summary()
2068 static void codecache_print(bool detailed)
2069 {
2070   stringStream s;
2071   // Dump code cache  into a buffer before locking the tty,
2072   {
2073     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
2074     CodeCache::print_summary(&s, detailed);
2075   }
2076   ttyLocker ttyl;
2077   tty->print("%s", s.freeze());
2078 }
2079 
2080 // wrapper for CodeCache::print_summary() using outputStream
2081 static void codecache_print(outputStream* out, bool detailed) {
2082   stringStream s;
2083 
2084   // Dump code cache into a buffer
2085   {
2086     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
2087     CodeCache::print_summary(&s, detailed);
2088   }
2089 
2090   char* remaining_log = s.as_string();
2091   while (*remaining_log != '\0') {
2092     char* eol = strchr(remaining_log, '\n');
2093     if (eol == nullptr) {
2094       out->print_cr("%s", remaining_log);
2095       remaining_log = remaining_log + strlen(remaining_log);
2096     } else {
2097       *eol = '\0';
2098       out->print_cr("%s", remaining_log);
2099       remaining_log = eol + 1;
2100     }
2101   }
2102 }
2103 
2104 void CompileBroker::handle_compile_error(CompilerThread* thread, CompileTask* task, ciEnv* ci_env,
2105                                          int compilable, const char* failure_reason) {
2106   if (!AbortVMOnCompilationFailure) {
2107     return;
2108   }
2109   if (compilable == ciEnv::MethodCompilable_not_at_tier) {
2110     fatal("Not compilable at tier %d: %s", task->comp_level(), failure_reason);
2111   }
2112   if (compilable == ciEnv::MethodCompilable_never) {
2113     fatal("Never compilable: %s", failure_reason);
2114   }
2115 }
2116 
2117 static void post_compilation_event(EventCompilation& event, CompileTask* task) {
2118   assert(task != nullptr, "invariant");
2119   CompilerEvent::CompilationEvent::post(event,
2120                                         task->compile_id(),
2121                                         task->compiler()->type(),
2122                                         task->method(),
2123                                         task->comp_level(),
2124                                         task->is_success(),
2125                                         task->osr_bci() != CompileBroker::standard_entry_bci,
2126                                         task->nm_total_size(),
2127                                         task->num_inlined_bytecodes(),
2128                                         task->arena_bytes());
2129 }
2130 
2131 int DirectivesStack::_depth = 0;
2132 CompilerDirectives* DirectivesStack::_top = nullptr;
2133 CompilerDirectives* DirectivesStack::_bottom = nullptr;
2134 
2135 // Acquires Compilation_lock and waits for it to be notified
2136 // as long as WhiteBox::compilation_locked is true.
2137 static void whitebox_lock_compilation() {
2138   MonitorLocker locker(Compilation_lock, Mutex::_no_safepoint_check_flag);
2139   while (WhiteBox::compilation_locked) {
2140     locker.wait();
2141   }
2142 }
2143 
2144 // ------------------------------------------------------------------
2145 // CompileBroker::invoke_compiler_on_method
2146 //
2147 // Compile a method.
2148 //
2149 void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
2150   task->print_ul();
2151   elapsedTimer time;
2152 
2153   DirectiveSet* directive = task->directive();
2154   if (directive->PrintCompilationOption) {
2155     ResourceMark rm;
2156     task->print_tty();
2157   }
2158 
2159   CompilerThread* thread = CompilerThread::current();
2160   ResourceMark rm(thread);
2161 
2162   if (CompilationLog::log() != nullptr) {
2163     CompilationLog::log()->log_compile(thread, task);
2164   }
2165 
2166   // Common flags.
2167   int compile_id = task->compile_id();
2168   int osr_bci = task->osr_bci();
2169   bool is_osr = (osr_bci != standard_entry_bci);
2170   bool should_log = (thread->log() != nullptr);
2171   bool should_break = false;
2172   const int task_level = task->comp_level();
2173   AbstractCompiler* comp = task->compiler();
2174   {
2175     // create the handle inside it's own block so it can't
2176     // accidentally be referenced once the thread transitions to
2177     // native.  The NoHandleMark before the transition should catch
2178     // any cases where this occurs in the future.
2179     methodHandle method(thread, task->method());
2180 
2181     assert(!method->is_native(), "no longer compile natives");
2182 
2183     // Update compile information when using perfdata.
2184     if (UsePerfData) {
2185       update_compile_perf_data(thread, method, is_osr);
2186     }
2187 
2188     DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level));
2189   }
2190 
2191   should_break = directive->BreakAtCompileOption || task->check_break_at_flags();
2192   if (should_log && !directive->LogOption) {
2193     should_log = false;
2194   }
2195 
2196   // Allocate a new set of JNI handles.
2197   JNIHandleMark jhm(thread);
2198   Method* target_handle = task->method();
2199   int compilable = ciEnv::MethodCompilable;
2200   const char* failure_reason = nullptr;
2201   bool failure_reason_on_C_heap = false;
2202   const char* retry_message = nullptr;
2203 
2204 #if INCLUDE_JVMCI
2205   if (UseJVMCICompiler && comp != nullptr && comp->is_jvmci()) {
2206     JVMCICompiler* jvmci = (JVMCICompiler*) comp;
2207 
2208     TraceTime t1("compilation", &time);
2209     EventCompilation event;
2210     JVMCICompileState compile_state(task, jvmci);
2211     JVMCIRuntime *runtime = nullptr;
2212 
2213     if (JVMCI::in_shutdown()) {
2214       failure_reason = "in JVMCI shutdown";
2215       retry_message = "not retryable";
2216       compilable = ciEnv::MethodCompilable_never;
2217     } else if (compile_state.target_method_is_old()) {
2218       // Skip redefined methods
2219       failure_reason = "redefined method";
2220       retry_message = "not retryable";
2221       compilable = ciEnv::MethodCompilable_never;
2222     } else {
2223       JVMCIEnv env(thread, &compile_state, __FILE__, __LINE__);
2224       if (env.init_error() != JNI_OK) {
2225         const char* msg = env.init_error_msg();
2226         failure_reason = os::strdup(err_msg("Error attaching to libjvmci (err: %d, %s)",
2227                                     env.init_error(), msg == nullptr ? "unknown" : msg), mtJVMCI);
2228         bool reason_on_C_heap = true;
2229         // In case of JNI_ENOMEM, there's a good chance a subsequent attempt to create libjvmci or attach to it
2230         // might succeed. Other errors most likely indicate a non-recoverable error in the JVMCI runtime.
2231         bool retryable = env.init_error() == JNI_ENOMEM;
2232         compile_state.set_failure(retryable, failure_reason, reason_on_C_heap);
2233       }
2234       if (failure_reason == nullptr) {
2235         if (WhiteBoxAPI && WhiteBox::compilation_locked) {
2236           // Must switch to native to block
2237           ThreadToNativeFromVM ttn(thread);
2238           whitebox_lock_compilation();
2239         }
2240         methodHandle method(thread, target_handle);
2241         runtime = env.runtime();
2242         runtime->compile_method(&env, jvmci, method, osr_bci);
2243 
2244         failure_reason = compile_state.failure_reason();
2245         failure_reason_on_C_heap = compile_state.failure_reason_on_C_heap();
2246         if (!compile_state.retryable()) {
2247           retry_message = "not retryable";
2248           compilable = ciEnv::MethodCompilable_not_at_tier;
2249         }
2250         if (!task->is_success()) {
2251           assert(failure_reason != nullptr, "must specify failure_reason");
2252         }
2253       }
2254     }
2255     if (!task->is_success() && !JVMCI::in_shutdown()) {
2256       handle_compile_error(thread, task, nullptr, compilable, failure_reason);
2257     }
2258     if (event.should_commit()) {
2259       post_compilation_event(event, task);
2260     }
2261 
2262     if (runtime != nullptr) {
2263       runtime->post_compile(thread);
2264     }
2265   } else
2266 #endif // INCLUDE_JVMCI
2267   {
2268     NoHandleMark  nhm;
2269     ThreadToNativeFromVM ttn(thread);
2270 
2271     ciEnv ci_env(task);
2272     if (should_break) {
2273       ci_env.set_break_at_compile(true);
2274     }
2275     if (should_log) {
2276       ci_env.set_log(thread->log());
2277     }
2278     assert(thread->env() == &ci_env, "set by ci_env");
2279     // The thread-env() field is cleared in ~CompileTaskWrapper.
2280 
2281     // Cache Jvmti state
2282     bool method_is_old = ci_env.cache_jvmti_state();
2283 
2284     // Skip redefined methods
2285     if (method_is_old) {
2286       ci_env.record_method_not_compilable("redefined method", true);
2287     }
2288 
2289     // Cache DTrace flags
2290     ci_env.cache_dtrace_flags();
2291 
2292     ciMethod* target = ci_env.get_method_from_handle(target_handle);
2293 
2294     TraceTime t1("compilation", &time);
2295     EventCompilation event;
2296 
2297     if (comp == nullptr) {
2298       ci_env.record_method_not_compilable("no compiler");
2299     } else if (!ci_env.failing()) {
2300       if (WhiteBoxAPI && WhiteBox::compilation_locked) {
2301         whitebox_lock_compilation();
2302       }
2303       comp->compile_method(&ci_env, target, osr_bci, true, directive);
2304 
2305       /* Repeat compilation without installing code for profiling purposes */
2306       int repeat_compilation_count = directive->RepeatCompilationOption;
2307       while (repeat_compilation_count > 0) {
2308         ResourceMark rm(thread);
2309         task->print_ul("NO CODE INSTALLED");
2310         comp->compile_method(&ci_env, target, osr_bci, false, directive);
2311         repeat_compilation_count--;
2312       }
2313     }
2314 
2315     DirectivesStack::release(directive);
2316 
2317     if (!ci_env.failing() && !task->is_success()) {
2318       assert(ci_env.failure_reason() != nullptr, "expect failure reason");
2319       assert(false, "compiler should always document failure: %s", ci_env.failure_reason());
2320       // The compiler elected, without comment, not to register a result.
2321       // Do not attempt further compilations of this method.
2322       ci_env.record_method_not_compilable("compile failed");
2323     }
2324 
2325     // Copy this bit to the enclosing block:
2326     compilable = ci_env.compilable();
2327 
2328     if (ci_env.failing()) {
2329       // Duplicate the failure reason string, so that it outlives ciEnv
2330       failure_reason = os::strdup(ci_env.failure_reason(), mtCompiler);
2331       failure_reason_on_C_heap = true;
2332       retry_message = ci_env.retry_message();
2333       ci_env.report_failure(failure_reason);
2334     }
2335 
2336     if (ci_env.failing()) {
2337       handle_compile_error(thread, task, &ci_env, compilable, failure_reason);
2338     }
2339     if (event.should_commit()) {
2340       post_compilation_event(event, task);
2341     }
2342   }
2343 
2344   if (failure_reason != nullptr) {
2345     task->set_failure_reason(failure_reason, failure_reason_on_C_heap);
2346     if (CompilationLog::log() != nullptr) {
2347       CompilationLog::log()->log_failure(thread, task, failure_reason, retry_message);
2348     }
2349     if (PrintCompilation) {
2350       FormatBufferResource msg = retry_message != nullptr ?
2351         FormatBufferResource("COMPILE SKIPPED: %s (%s)", failure_reason, retry_message) :
2352         FormatBufferResource("COMPILE SKIPPED: %s",      failure_reason);
2353       task->print(tty, msg);
2354     }
2355   }
2356 
2357   methodHandle method(thread, task->method());
2358 
2359   DTRACE_METHOD_COMPILE_END_PROBE(method, compiler_name(task_level), task->is_success());
2360 
2361   collect_statistics(thread, time, task);
2362 
2363   if (PrintCompilation && PrintCompilation2) {
2364     tty->print("%7d ", (int) tty->time_stamp().milliseconds());  // print timestamp
2365     tty->print("%4d ", compile_id);    // print compilation number
2366     tty->print("%s ", (is_osr ? "%" : " "));
2367     if (task->is_success()) {
2368       tty->print("size: %d(%d) ", task->nm_total_size(), task->nm_insts_size());
2369     }
2370     tty->print_cr("time: %d inlined: %d bytes", (int)time.milliseconds(), task->num_inlined_bytecodes());
2371   }
2372 
2373   Log(compilation, codecache) log;
2374   if (log.is_debug()) {
2375     LogStream ls(log.debug());
2376     codecache_print(&ls, /* detailed= */ false);
2377   }
2378   if (PrintCodeCacheOnCompilation) {
2379     codecache_print(/* detailed= */ false);
2380   }
2381   // Disable compilation, if required.
2382   switch (compilable) {
2383   case ciEnv::MethodCompilable_never:
2384     if (is_osr)
2385       method->set_not_osr_compilable_quietly("MethodCompilable_never");
2386     else
2387       method->set_not_compilable_quietly("MethodCompilable_never");
2388     break;
2389   case ciEnv::MethodCompilable_not_at_tier:
2390     if (is_osr)
2391       method->set_not_osr_compilable_quietly("MethodCompilable_not_at_tier", task_level);
2392     else
2393       method->set_not_compilable_quietly("MethodCompilable_not_at_tier", task_level);
2394     break;
2395   }
2396 
2397   // Note that the queued_for_compilation bits are cleared without
2398   // protection of a mutex. [They were set by the requester thread,
2399   // when adding the task to the compile queue -- at which time the
2400   // compile queue lock was held. Subsequently, we acquired the compile
2401   // queue lock to get this task off the compile queue; thus (to belabour
2402   // the point somewhat) our clearing of the bits must be occurring
2403   // only after the setting of the bits. See also 14012000 above.
2404   method->clear_queued_for_compilation();
2405 }
2406 
2407 /**
2408  * The CodeCache is full. Print warning and disable compilation.
2409  * Schedule code cache cleaning so compilation can continue later.
2410  * This function needs to be called only from CodeCache::allocate(),
2411  * since we currently handle a full code cache uniformly.
2412  */
2413 void CompileBroker::handle_full_code_cache(CodeBlobType code_blob_type) {
2414   UseInterpreter = true;
2415   if (UseCompiler || AlwaysCompileLoopMethods ) {
2416     if (xtty != nullptr) {
2417       stringStream s;
2418       // Dump code cache state into a buffer before locking the tty,
2419       // because log_state() will use locks causing lock conflicts.
2420       CodeCache::log_state(&s);
2421       // Lock to prevent tearing
2422       ttyLocker ttyl;
2423       xtty->begin_elem("code_cache_full");
2424       xtty->print("%s", s.freeze());
2425       xtty->stamp();
2426       xtty->end_elem();
2427     }
2428 
2429 #ifndef PRODUCT
2430     if (ExitOnFullCodeCache) {
2431       codecache_print(/* detailed= */ true);
2432       before_exit(JavaThread::current());
2433       exit_globals(); // will delete tty
2434       vm_direct_exit(1);
2435     }
2436 #endif
2437     if (UseCodeCacheFlushing) {
2438       // Since code cache is full, immediately stop new compiles
2439       if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) {
2440         log_info(codecache)("Code cache is full - disabling compilation");
2441       }
2442     } else {
2443       disable_compilation_forever();
2444     }
2445 
2446     CodeCache::report_codemem_full(code_blob_type, should_print_compiler_warning());
2447   }
2448 }
2449 
2450 // ------------------------------------------------------------------
2451 // CompileBroker::update_compile_perf_data
2452 //
2453 // Record this compilation for debugging purposes.
2454 void CompileBroker::update_compile_perf_data(CompilerThread* thread, const methodHandle& method, bool is_osr) {
2455   ResourceMark rm;
2456   char* method_name = method->name()->as_C_string();
2457   char current_method[CompilerCounters::cmname_buffer_length];
2458   size_t maxLen = CompilerCounters::cmname_buffer_length;
2459 
2460   const char* class_name = method->method_holder()->name()->as_C_string();
2461 
2462   size_t s1len = strlen(class_name);
2463   size_t s2len = strlen(method_name);
2464 
2465   // check if we need to truncate the string
2466   if (s1len + s2len + 2 > maxLen) {
2467 
2468     // the strategy is to lop off the leading characters of the
2469     // class name and the trailing characters of the method name.
2470 
2471     if (s2len + 2 > maxLen) {
2472       // lop of the entire class name string, let snprintf handle
2473       // truncation of the method name.
2474       class_name += s1len; // null string
2475     }
2476     else {
2477       // lop off the extra characters from the front of the class name
2478       class_name += ((s1len + s2len + 2) - maxLen);
2479     }
2480   }
2481 
2482   jio_snprintf(current_method, maxLen, "%s %s", class_name, method_name);
2483 
2484   int last_compile_type = normal_compile;
2485   if (CICountOSR && is_osr) {
2486     last_compile_type = osr_compile;
2487   } else if (CICountNative && method->is_native()) {
2488     last_compile_type = native_compile;
2489   }
2490 
2491   CompilerCounters* counters = thread->counters();
2492   counters->set_current_method(current_method);
2493   counters->set_compile_type((jlong) last_compile_type);
2494 }
2495 
2496 // ------------------------------------------------------------------
2497 // CompileBroker::collect_statistics
2498 //
2499 // Collect statistics about the compilation.
2500 
2501 void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) {
2502   bool success = task->is_success();
2503   methodHandle method (thread, task->method());
2504   int compile_id = task->compile_id();
2505   bool is_osr = (task->osr_bci() != standard_entry_bci);
2506   const int comp_level = task->comp_level();
2507   CompilerCounters* counters = thread->counters();
2508 
2509   MutexLocker locker(CompileStatistics_lock);
2510 
2511   // _perf variables are production performance counters which are
2512   // updated regardless of the setting of the CITime and CITimeEach flags
2513   //
2514 
2515   // account all time, including bailouts and failures in this counter;
2516   // C1 and C2 counters are counting both successful and unsuccessful compiles
2517   _t_total_compilation.add(time);
2518 
2519   if (!success) {
2520     _total_bailout_count++;
2521     if (UsePerfData) {
2522       _perf_last_failed_method->set_value(counters->current_method());
2523       _perf_last_failed_type->set_value(counters->compile_type());
2524       _perf_total_bailout_count->inc();
2525     }
2526     _t_bailedout_compilation.add(time);
2527   } else if (!task->is_success()) {
2528     if (UsePerfData) {
2529       _perf_last_invalidated_method->set_value(counters->current_method());
2530       _perf_last_invalidated_type->set_value(counters->compile_type());
2531       _perf_total_invalidated_count->inc();
2532     }
2533     _total_invalidated_count++;
2534     _t_invalidated_compilation.add(time);
2535   } else {
2536     // Compilation succeeded
2537 
2538     // update compilation ticks - used by the implementation of
2539     // java.lang.management.CompilationMXBean
2540     _perf_total_compilation->inc(time.ticks());
2541     _peak_compilation_time = time.milliseconds() > _peak_compilation_time ? time.milliseconds() : _peak_compilation_time;
2542 
2543     if (CITime) {
2544       int bytes_compiled = method->code_size() + task->num_inlined_bytecodes();
2545       if (is_osr) {
2546         _t_osr_compilation.add(time);
2547         _sum_osr_bytes_compiled += bytes_compiled;
2548       } else {
2549         _t_standard_compilation.add(time);
2550         _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();
2551       }
2552 
2553       // Collect statistic per compilation level
2554       if (comp_level > CompLevel_none && comp_level <= CompLevel_full_optimization) {
2555         CompilerStatistics* stats = &_stats_per_level[comp_level-1];
2556         if (is_osr) {
2557           stats->_osr.update(time, bytes_compiled);
2558         } else {
2559           stats->_standard.update(time, bytes_compiled);
2560         }
2561         stats->_nmethods_size += task->nm_total_size();
2562         stats->_nmethods_code_size += task->nm_insts_size();
2563       } else {
2564         assert(false, "CompilerStatistics object does not exist for compilation level %d", comp_level);
2565       }
2566 
2567       // Collect statistic per compiler
2568       AbstractCompiler* comp = compiler(comp_level);
2569       if (comp) {
2570         CompilerStatistics* stats = comp->stats();
2571         if (is_osr) {
2572           stats->_osr.update(time, bytes_compiled);
2573         } else {
2574           stats->_standard.update(time, bytes_compiled);
2575         }
2576         stats->_nmethods_size += task->nm_total_size();
2577         stats->_nmethods_code_size += task->nm_insts_size();
2578       } else { // if (!comp)
2579         assert(false, "Compiler object must exist");
2580       }
2581     }
2582 
2583     if (UsePerfData) {
2584       // save the name of the last method compiled
2585       _perf_last_method->set_value(counters->current_method());
2586       _perf_last_compile_type->set_value(counters->compile_type());
2587       _perf_last_compile_size->set_value(method->code_size() +
2588                                          task->num_inlined_bytecodes());
2589       if (is_osr) {
2590         _perf_osr_compilation->inc(time.ticks());
2591         _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
2592       } else {
2593         _perf_standard_compilation->inc(time.ticks());
2594         _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
2595       }
2596     }
2597 
2598     if (CITimeEach) {
2599       double compile_time = time.seconds();
2600       double bytes_per_sec = compile_time == 0.0 ? 0.0 : (double)(method->code_size() + task->num_inlined_bytecodes()) / compile_time;
2601       tty->print_cr("%3d   seconds: %6.3f bytes/sec : %f (bytes %d + %d inlined)",
2602                     compile_id, compile_time, bytes_per_sec, method->code_size(), task->num_inlined_bytecodes());
2603     }
2604 
2605     // Collect counts of successful compilations
2606     _sum_nmethod_size      += task->nm_total_size();
2607     _sum_nmethod_code_size += task->nm_insts_size();
2608     _total_compile_count++;
2609 
2610     if (UsePerfData) {
2611       _perf_sum_nmethod_size->inc(     task->nm_total_size());
2612       _perf_sum_nmethod_code_size->inc(task->nm_insts_size());
2613       _perf_total_compile_count->inc();
2614     }
2615 
2616     if (is_osr) {
2617       if (UsePerfData) _perf_total_osr_compile_count->inc();
2618       _total_osr_compile_count++;
2619     } else {
2620       if (UsePerfData) _perf_total_standard_compile_count->inc();
2621       _total_standard_compile_count++;
2622     }
2623   }
2624   // set the current method for the thread to null
2625   if (UsePerfData) counters->set_current_method("");
2626 }
2627 
2628 const char* CompileBroker::compiler_name(int comp_level) {
2629   AbstractCompiler *comp = CompileBroker::compiler(comp_level);
2630   if (comp == nullptr) {
2631     return "no compiler";
2632   } else {
2633     return (comp->name());
2634   }
2635 }
2636 
2637 jlong CompileBroker::total_compilation_ticks() {
2638   return _perf_total_compilation != nullptr ? _perf_total_compilation->get_value() : 0;
2639 }
2640 
2641 void CompileBroker::print_times(const char* name, CompilerStatistics* stats) {
2642   tty->print_cr("  %s {speed: %6.3f bytes/s; standard: %6.3f s, %u bytes, %u methods; osr: %6.3f s, %u bytes, %u methods; nmethods_size: %u bytes; nmethods_code_size: %u bytes}",
2643                 name, stats->bytes_per_second(),
2644                 stats->_standard._time.seconds(), stats->_standard._bytes, stats->_standard._count,
2645                 stats->_osr._time.seconds(), stats->_osr._bytes, stats->_osr._count,
2646                 stats->_nmethods_size, stats->_nmethods_code_size);
2647 }
2648 
2649 void CompileBroker::print_times(bool per_compiler, bool aggregate) {
2650   if (per_compiler) {
2651     if (aggregate) {
2652       tty->cr();
2653       tty->print_cr("Individual compiler times (for compiled methods only)");
2654       tty->print_cr("------------------------------------------------");
2655       tty->cr();
2656     }
2657     for (unsigned int i = 0; i < sizeof(_compilers) / sizeof(AbstractCompiler*); i++) {
2658       AbstractCompiler* comp = _compilers[i];
2659       if (comp != nullptr) {
2660         print_times(comp->name(), comp->stats());
2661       }
2662     }
2663     if (aggregate) {
2664       tty->cr();
2665       tty->print_cr("Individual compilation Tier times (for compiled methods only)");
2666       tty->print_cr("------------------------------------------------");
2667       tty->cr();
2668     }
2669     char tier_name[256];
2670     for (int tier = CompLevel_simple; tier <= CompilationPolicy::highest_compile_level(); tier++) {
2671       CompilerStatistics* stats = &_stats_per_level[tier-1];
2672       os::snprintf_checked(tier_name, sizeof(tier_name), "Tier%d", tier);
2673       print_times(tier_name, stats);
2674     }
2675   }
2676 
2677   if (!aggregate) {
2678     return;
2679   }
2680 
2681   elapsedTimer standard_compilation = CompileBroker::_t_standard_compilation;
2682   elapsedTimer osr_compilation = CompileBroker::_t_osr_compilation;
2683   elapsedTimer total_compilation = CompileBroker::_t_total_compilation;
2684 
2685   uint standard_bytes_compiled = CompileBroker::_sum_standard_bytes_compiled;
2686   uint osr_bytes_compiled = CompileBroker::_sum_osr_bytes_compiled;
2687 
2688   uint standard_compile_count = CompileBroker::_total_standard_compile_count;
2689   uint osr_compile_count = CompileBroker::_total_osr_compile_count;
2690   uint total_compile_count = CompileBroker::_total_compile_count;
2691   uint total_bailout_count = CompileBroker::_total_bailout_count;
2692   uint total_invalidated_count = CompileBroker::_total_invalidated_count;
2693 
2694   uint nmethods_code_size = CompileBroker::_sum_nmethod_code_size;
2695   uint nmethods_size = CompileBroker::_sum_nmethod_size;
2696 
2697   tty->cr();
2698   tty->print_cr("Accumulated compiler times");
2699   tty->print_cr("----------------------------------------------------------");
2700                //0000000000111111111122222222223333333333444444444455555555556666666666
2701                //0123456789012345678901234567890123456789012345678901234567890123456789
2702   tty->print_cr("  Total compilation time   : %7.3f s", total_compilation.seconds());
2703   tty->print_cr("    Standard compilation   : %7.3f s, Average : %2.3f s",
2704                 standard_compilation.seconds(),
2705                 standard_compile_count == 0 ? 0.0 : standard_compilation.seconds() / standard_compile_count);
2706   tty->print_cr("    Bailed out compilation : %7.3f s, Average : %2.3f s",
2707                 CompileBroker::_t_bailedout_compilation.seconds(),
2708                 total_bailout_count == 0 ? 0.0 : CompileBroker::_t_bailedout_compilation.seconds() / total_bailout_count);
2709   tty->print_cr("    On stack replacement   : %7.3f s, Average : %2.3f s",
2710                 osr_compilation.seconds(),
2711                 osr_compile_count == 0 ? 0.0 : osr_compilation.seconds() / osr_compile_count);
2712   tty->print_cr("    Invalidated            : %7.3f s, Average : %2.3f s",
2713                 CompileBroker::_t_invalidated_compilation.seconds(),
2714                 total_invalidated_count == 0 ? 0.0 : CompileBroker::_t_invalidated_compilation.seconds() / total_invalidated_count);
2715 
2716   AbstractCompiler *comp = compiler(CompLevel_simple);
2717   if (comp != nullptr) {
2718     tty->cr();
2719     comp->print_timers();
2720   }
2721   comp = compiler(CompLevel_full_optimization);
2722   if (comp != nullptr) {
2723     tty->cr();
2724     comp->print_timers();
2725   }
2726 #if INCLUDE_JVMCI
2727   if (EnableJVMCI) {
2728     JVMCICompiler *jvmci_comp = JVMCICompiler::instance(false, JavaThread::current_or_null());
2729     if (jvmci_comp != nullptr && jvmci_comp != comp) {
2730       tty->cr();
2731       jvmci_comp->print_timers();
2732     }
2733   }
2734 #endif
2735 
2736   tty->cr();
2737   tty->print_cr("  Total compiled methods    : %8u methods", total_compile_count);
2738   tty->print_cr("    Standard compilation    : %8u methods", standard_compile_count);
2739   tty->print_cr("    On stack replacement    : %8u methods", osr_compile_count);
2740   uint tcb = osr_bytes_compiled + standard_bytes_compiled;
2741   tty->print_cr("  Total compiled bytecodes  : %8u bytes", tcb);
2742   tty->print_cr("    Standard compilation    : %8u bytes", standard_bytes_compiled);
2743   tty->print_cr("    On stack replacement    : %8u bytes", osr_bytes_compiled);
2744   double tcs = total_compilation.seconds();
2745   uint bps = tcs == 0.0 ? 0 : (uint)(tcb / tcs);
2746   tty->print_cr("  Average compilation speed : %8u bytes/s", bps);
2747   tty->cr();
2748   tty->print_cr("  nmethod code size         : %8u bytes", nmethods_code_size);
2749   tty->print_cr("  nmethod total size        : %8u bytes", nmethods_size);
2750 }
2751 
2752 // Print general/accumulated JIT information.
2753 void CompileBroker::print_info(outputStream *out) {
2754   if (out == nullptr) out = tty;
2755   out->cr();
2756   out->print_cr("======================");
2757   out->print_cr("   General JIT info   ");
2758   out->print_cr("======================");
2759   out->cr();
2760   out->print_cr("            JIT is : %7s",     should_compile_new_jobs() ? "on" : "off");
2761   out->print_cr("  Compiler threads : %7d",     (int)CICompilerCount);
2762   out->cr();
2763   out->print_cr("CodeCache overview");
2764   out->print_cr("--------------------------------------------------------");
2765   out->cr();
2766   out->print_cr("         Reserved size : " SIZE_FORMAT_W(7) " KB", CodeCache::max_capacity() / K);
2767   out->print_cr("        Committed size : " SIZE_FORMAT_W(7) " KB", CodeCache::capacity() / K);
2768   out->print_cr("  Unallocated capacity : " SIZE_FORMAT_W(7) " KB", CodeCache::unallocated_capacity() / K);
2769   out->cr();
2770 }
2771 
2772 // Note: tty_lock must not be held upon entry to this function.
2773 //       Print functions called from herein do "micro-locking" on tty_lock.
2774 //       That's a tradeoff which keeps together important blocks of output.
2775 //       At the same time, continuous tty_lock hold time is kept in check,
2776 //       preventing concurrently printing threads from stalling a long time.
2777 void CompileBroker::print_heapinfo(outputStream* out, const char* function, size_t granularity) {
2778   TimeStamp ts_total;
2779   TimeStamp ts_global;
2780   TimeStamp ts;
2781 
2782   bool allFun = !strcmp(function, "all");
2783   bool aggregate = !strcmp(function, "aggregate") || !strcmp(function, "analyze") || allFun;
2784   bool usedSpace = !strcmp(function, "UsedSpace") || allFun;
2785   bool freeSpace = !strcmp(function, "FreeSpace") || allFun;
2786   bool methodCount = !strcmp(function, "MethodCount") || allFun;
2787   bool methodSpace = !strcmp(function, "MethodSpace") || allFun;
2788   bool methodAge = !strcmp(function, "MethodAge") || allFun;
2789   bool methodNames = !strcmp(function, "MethodNames") || allFun;
2790   bool discard = !strcmp(function, "discard") || allFun;
2791 
2792   if (out == nullptr) {
2793     out = tty;
2794   }
2795 
2796   if (!(aggregate || usedSpace || freeSpace || methodCount || methodSpace || methodAge || methodNames || discard)) {
2797     out->print_cr("\n__ CodeHeapStateAnalytics: Function %s is not supported", function);
2798     out->cr();
2799     return;
2800   }
2801 
2802   ts_total.update(); // record starting point
2803 
2804   if (aggregate) {
2805     print_info(out);
2806   }
2807 
2808   // We hold the CodeHeapStateAnalytics_lock all the time, from here until we leave this function.
2809   // That prevents other threads from destroying (making inconsistent) our view on the CodeHeap.
2810   // When we request individual parts of the analysis via the jcmd interface, it is possible
2811   // that in between another thread (another jcmd user or the vm running into CodeCache OOM)
2812   // updated the aggregated data. We will then see a modified, but again consistent, view
2813   // on the CodeHeap. That's a tolerable tradeoff we have to accept because we can't hold
2814   // a lock across user interaction.
2815 
2816   // We should definitely acquire this lock before acquiring Compile_lock and CodeCache_lock.
2817   // CodeHeapStateAnalytics_lock may be held by a concurrent thread for a long time,
2818   // leading to an unnecessarily long hold time of the other locks we acquired before.
2819   ts.update(); // record starting point
2820   MutexLocker mu0(CodeHeapStateAnalytics_lock, Mutex::_safepoint_check_flag);
2821   out->print_cr("\n__ CodeHeapStateAnalytics lock wait took %10.3f seconds _________\n", ts.seconds());
2822 
2823   // Holding the CodeCache_lock protects from concurrent alterations of the CodeCache.
2824   // Unfortunately, such protection is not sufficient:
2825   // When a new nmethod is created via ciEnv::register_method(), the
2826   // Compile_lock is taken first. After some initializations,
2827   // nmethod::new_nmethod() takes over, grabbing the CodeCache_lock
2828   // immediately (after finalizing the oop references). To lock out concurrent
2829   // modifiers, we have to grab both locks as well in the described sequence.
2830   //
2831   // If we serve an "allFun" call, it is beneficial to hold CodeCache_lock and Compile_lock
2832   // for the entire duration of aggregation and printing. That makes sure we see
2833   // a consistent picture and do not run into issues caused by concurrent alterations.
2834   bool should_take_Compile_lock   = !SafepointSynchronize::is_at_safepoint() &&
2835                                     !Compile_lock->owned_by_self();
2836   bool should_take_CodeCache_lock = !SafepointSynchronize::is_at_safepoint() &&
2837                                     !CodeCache_lock->owned_by_self();
2838   bool take_global_lock_1   =  allFun && should_take_Compile_lock;
2839   bool take_global_lock_2   =  allFun && should_take_CodeCache_lock;
2840   bool take_function_lock_1 = !allFun && should_take_Compile_lock;
2841   bool take_function_lock_2 = !allFun && should_take_CodeCache_lock;
2842   bool take_global_locks    = take_global_lock_1 || take_global_lock_2;
2843   bool take_function_locks  = take_function_lock_1 || take_function_lock_2;
2844 
2845   ts_global.update(); // record starting point
2846 
2847   ConditionalMutexLocker mu1(Compile_lock, take_global_lock_1, Mutex::_safepoint_check_flag);
2848   ConditionalMutexLocker mu2(CodeCache_lock, take_global_lock_2, Mutex::_no_safepoint_check_flag);
2849   if (take_global_locks) {
2850     out->print_cr("\n__ Compile & CodeCache (global) lock wait took %10.3f seconds _________\n", ts_global.seconds());
2851     ts_global.update(); // record starting point
2852   }
2853 
2854   if (aggregate) {
2855     ts.update(); // record starting point
2856     ConditionalMutexLocker mu11(Compile_lock, take_function_lock_1,  Mutex::_safepoint_check_flag);
2857     ConditionalMutexLocker mu22(CodeCache_lock, take_function_lock_2, Mutex::_no_safepoint_check_flag);
2858     if (take_function_locks) {
2859       out->print_cr("\n__ Compile & CodeCache (function) lock wait took %10.3f seconds _________\n", ts.seconds());
2860     }
2861 
2862     ts.update(); // record starting point
2863     CodeCache::aggregate(out, granularity);
2864     if (take_function_locks) {
2865       out->print_cr("\n__ Compile & CodeCache (function) lock hold took %10.3f seconds _________\n", ts.seconds());
2866     }
2867   }
2868 
2869   if (usedSpace) CodeCache::print_usedSpace(out);
2870   if (freeSpace) CodeCache::print_freeSpace(out);
2871   if (methodCount) CodeCache::print_count(out);
2872   if (methodSpace) CodeCache::print_space(out);
2873   if (methodAge) CodeCache::print_age(out);
2874   if (methodNames) {
2875     if (allFun) {
2876       // print_names() can only be used safely if the locks have been continuously held
2877       // since aggregation begin. That is true only for function "all".
2878       CodeCache::print_names(out);
2879     } else {
2880       out->print_cr("\nCodeHeapStateAnalytics: Function 'MethodNames' is only available as part of function 'all'");
2881     }
2882   }
2883   if (discard) CodeCache::discard(out);
2884 
2885   if (take_global_locks) {
2886     out->print_cr("\n__ Compile & CodeCache (global) lock hold took %10.3f seconds _________\n", ts_global.seconds());
2887   }
2888   out->print_cr("\n__ CodeHeapStateAnalytics total duration %10.3f seconds _________\n", ts_total.seconds());
2889 }