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