1 /*
   2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/codeCache.hpp"
  31 #include "code/dependencyContext.hpp"
  32 #include "compiler/compileBroker.hpp"
  33 #include "compiler/compileLog.hpp"
  34 #include "compiler/compilerOracle.hpp"
  35 #include "compiler/directivesParser.hpp"
  36 #include "interpreter/linkResolver.hpp"
  37 #include "logging/log.hpp"
  38 #include "logging/logStream.hpp"
  39 #include "memory/allocation.inline.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "oops/methodData.hpp"
  42 #include "oops/method.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "prims/nativeLookup.hpp"
  45 #include "prims/whitebox.hpp"
  46 #include "runtime/arguments.hpp"
  47 #include "runtime/atomic.hpp"
  48 #include "runtime/compilationPolicy.hpp"
  49 #include "runtime/init.hpp"
  50 #include "runtime/interfaceSupport.hpp"
  51 #include "runtime/javaCalls.hpp"
  52 #include "runtime/os.hpp"
  53 #include "runtime/sharedRuntime.hpp"
  54 #include "runtime/sweeper.hpp"
  55 #include "runtime/timerTrace.hpp"
  56 #include "trace/tracing.hpp"
  57 #include "utilities/debug.hpp"
  58 #include "utilities/dtrace.hpp"
  59 #include "utilities/events.hpp"
  60 #include "utilities/formatBuffer.hpp"
  61 #ifdef COMPILER1
  62 #include "c1/c1_Compiler.hpp"
  63 #endif
  64 #if INCLUDE_JVMCI
  65 #include "jvmci/jvmciCompiler.hpp"
  66 #include "jvmci/jvmciRuntime.hpp"
  67 #include "jvmci/jvmciJavaClasses.hpp"
  68 #include "runtime/vframe.hpp"
  69 #endif
  70 #ifdef COMPILER2
  71 #include "opto/c2compiler.hpp"
  72 #endif
  73 
  74 #ifdef DTRACE_ENABLED
  75 
  76 // Only bother with this argument setup if dtrace is available
  77 
  78 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)             \
  79   {                                                                      \
  80     Symbol* klass_name = (method)->klass_name();                         \
  81     Symbol* name = (method)->name();                                     \
  82     Symbol* signature = (method)->signature();                           \
  83     HOTSPOT_METHOD_COMPILE_BEGIN(                                        \
  84       (char *) comp_name, strlen(comp_name),                             \
  85       (char *) klass_name->bytes(), klass_name->utf8_length(),           \
  86       (char *) name->bytes(), name->utf8_length(),                       \
  87       (char *) signature->bytes(), signature->utf8_length());            \
  88   }
  89 
  90 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)      \
  91   {                                                                      \
  92     Symbol* klass_name = (method)->klass_name();                         \
  93     Symbol* name = (method)->name();                                     \
  94     Symbol* signature = (method)->signature();                           \
  95     HOTSPOT_METHOD_COMPILE_END(                                          \
  96       (char *) comp_name, strlen(comp_name),                             \
  97       (char *) klass_name->bytes(), klass_name->utf8_length(),           \
  98       (char *) name->bytes(), name->utf8_length(),                       \
  99       (char *) signature->bytes(), signature->utf8_length(), (success)); \
 100   }
 101 
 102 #else //  ndef DTRACE_ENABLED
 103 
 104 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
 105 #define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
 106 
 107 #endif // ndef DTRACE_ENABLED
 108 
 109 bool CompileBroker::_initialized = false;
 110 volatile bool CompileBroker::_should_block = false;
 111 volatile int  CompileBroker::_print_compilation_warning = 0;
 112 volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
 113 
 114 // The installed compiler(s)
 115 AbstractCompiler* CompileBroker::_compilers[2];
 116 
 117 // These counters are used to assign an unique ID to each compilation.
 118 volatile jint CompileBroker::_compilation_id     = 0;
 119 volatile jint CompileBroker::_osr_compilation_id = 0;
 120 
 121 // Debugging information
 122 int  CompileBroker::_last_compile_type     = no_compile;
 123 int  CompileBroker::_last_compile_level    = CompLevel_none;
 124 char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];
 125 
 126 // Performance counters
 127 PerfCounter* CompileBroker::_perf_total_compilation = NULL;
 128 PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
 129 PerfCounter* CompileBroker::_perf_standard_compilation = NULL;
 130 
 131 PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
 132 PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
 133 PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
 134 PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
 135 PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;
 136 
 137 PerfCounter* CompileBroker::_perf_sum_osr_bytes_compiled = NULL;
 138 PerfCounter* CompileBroker::_perf_sum_standard_bytes_compiled = NULL;
 139 PerfCounter* CompileBroker::_perf_sum_nmethod_size = NULL;
 140 PerfCounter* CompileBroker::_perf_sum_nmethod_code_size = NULL;
 141 
 142 PerfStringVariable* CompileBroker::_perf_last_method = NULL;
 143 PerfStringVariable* CompileBroker::_perf_last_failed_method = NULL;
 144 PerfStringVariable* CompileBroker::_perf_last_invalidated_method = NULL;
 145 PerfVariable*       CompileBroker::_perf_last_compile_type = NULL;
 146 PerfVariable*       CompileBroker::_perf_last_compile_size = NULL;
 147 PerfVariable*       CompileBroker::_perf_last_failed_type = NULL;
 148 PerfVariable*       CompileBroker::_perf_last_invalidated_type = NULL;
 149 
 150 // Timers and counters for generating statistics
 151 elapsedTimer CompileBroker::_t_total_compilation;
 152 elapsedTimer CompileBroker::_t_osr_compilation;
 153 elapsedTimer CompileBroker::_t_standard_compilation;
 154 elapsedTimer CompileBroker::_t_invalidated_compilation;
 155 elapsedTimer CompileBroker::_t_bailedout_compilation;
 156 
 157 int CompileBroker::_total_bailout_count          = 0;
 158 int CompileBroker::_total_invalidated_count      = 0;
 159 int CompileBroker::_total_compile_count          = 0;
 160 int CompileBroker::_total_osr_compile_count      = 0;
 161 int CompileBroker::_total_standard_compile_count = 0;
 162 
 163 int CompileBroker::_sum_osr_bytes_compiled       = 0;
 164 int CompileBroker::_sum_standard_bytes_compiled  = 0;
 165 int CompileBroker::_sum_nmethod_size             = 0;
 166 int CompileBroker::_sum_nmethod_code_size        = 0;
 167 
 168 long CompileBroker::_peak_compilation_time       = 0;
 169 
 170 CompileQueue* CompileBroker::_c2_compile_queue   = NULL;
 171 CompileQueue* CompileBroker::_c1_compile_queue   = NULL;
 172 
 173 
 174 
 175 class CompilationLog : public StringEventLog {
 176  public:
 177   CompilationLog() : StringEventLog("Compilation events") {
 178   }
 179 
 180   void log_compile(JavaThread* thread, CompileTask* task) {
 181     StringLogMessage lm;
 182     stringStream sstr = lm.stream();
 183     // msg.time_stamp().update_to(tty->time_stamp().ticks());
 184     task->print(&sstr, NULL, true, false);
 185     log(thread, "%s", (const char*)lm);
 186   }
 187 
 188   void log_nmethod(JavaThread* thread, nmethod* nm) {
 189     log(thread, "nmethod %d%s " INTPTR_FORMAT " code [" INTPTR_FORMAT ", " INTPTR_FORMAT "]",
 190         nm->compile_id(), nm->is_osr_method() ? "%" : "",
 191         p2i(nm), p2i(nm->code_begin()), p2i(nm->code_end()));
 192   }
 193 
 194   void log_failure(JavaThread* thread, CompileTask* task, const char* reason, const char* retry_message) {
 195     StringLogMessage lm;
 196     lm.print("%4d   COMPILE SKIPPED: %s", task->compile_id(), reason);
 197     if (retry_message != NULL) {
 198       lm.append(" (%s)", retry_message);
 199     }
 200     lm.print("\n");
 201     log(thread, "%s", (const char*)lm);
 202   }
 203 
 204   void log_metaspace_failure(const char* reason) {
 205     ResourceMark rm;
 206     StringLogMessage lm;
 207     lm.print("%4d   COMPILE PROFILING SKIPPED: %s", -1, reason);
 208     lm.print("\n");
 209     log(JavaThread::current(), "%s", (const char*)lm);
 210   }
 211 };
 212 
 213 static CompilationLog* _compilation_log = NULL;
 214 
 215 bool compileBroker_init() {
 216   if (LogEvents) {
 217     _compilation_log = new CompilationLog();
 218   }
 219 
 220   // init directives stack, adding default directive
 221   DirectivesStack::init();
 222 
 223   if (DirectivesParser::has_file()) {
 224     return DirectivesParser::parse_from_flag();
 225   } else if (CompilerDirectivesPrint) {
 226     // Print default directive even when no other was added
 227     DirectivesStack::print(tty);
 228   }
 229 
 230   return true;
 231 }
 232 
 233 CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) {
 234   CompilerThread* thread = CompilerThread::current();
 235   thread->set_task(task);
 236 #if INCLUDE_JVMCI
 237   if (task->is_blocking() && CompileBroker::compiler(task->comp_level())->is_jvmci()) {
 238     task->set_jvmci_compiler_thread(thread);
 239   }
 240 #endif
 241   CompileLog*     log  = thread->log();
 242   if (log != NULL)  task->log_task_start(log);
 243 }
 244 
 245 CompileTaskWrapper::~CompileTaskWrapper() {
 246   CompilerThread* thread = CompilerThread::current();
 247   CompileTask* task = thread->task();
 248   CompileLog*  log  = thread->log();
 249   if (log != NULL)  task->log_task_done(log);
 250   thread->set_task(NULL);
 251   task->set_code_handle(NULL);
 252   thread->set_env(NULL);
 253   if (task->is_blocking()) {
 254     bool free_task = false;
 255     {
 256       MutexLocker notifier(task->lock(), thread);
 257       task->mark_complete();
 258 #if INCLUDE_JVMCI
 259       if (CompileBroker::compiler(task->comp_level())->is_jvmci()) {
 260         if (!task->has_waiter()) {
 261           // The waiting thread timed out and thus did not free the task.
 262           free_task = true;
 263         }
 264         task->set_jvmci_compiler_thread(NULL);
 265       }
 266 #endif
 267       if (!free_task) {
 268         // Notify the waiting thread that the compilation has completed
 269         // so that it can free the task.
 270         task->lock()->notify_all();
 271       }
 272     }
 273     if (free_task) {
 274       // The task can only be freed once the task lock is released.
 275       CompileTask::free(task);
 276     }
 277   } else {
 278     task->mark_complete();
 279 
 280     // By convention, the compiling thread is responsible for
 281     // recycling a non-blocking CompileTask.
 282     CompileTask::free(task);
 283   }
 284 }
 285 
 286 /**
 287  * Add a CompileTask to a CompileQueue.
 288  */
 289 void CompileQueue::add(CompileTask* task) {
 290   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 291 
 292   task->set_next(NULL);
 293   task->set_prev(NULL);
 294 
 295   if (_last == NULL) {
 296     // The compile queue is empty.
 297     assert(_first == NULL, "queue is empty");
 298     _first = task;
 299     _last = task;
 300   } else {
 301     // Append the task to the queue.
 302     assert(_last->next() == NULL, "not last");
 303     _last->set_next(task);
 304     task->set_prev(_last);
 305     _last = task;
 306   }
 307   ++_size;
 308 
 309   // Mark the method as being in the compile queue.
 310   task->method()->set_queued_for_compilation();
 311 
 312   if (CIPrintCompileQueue) {
 313     print_tty();
 314   }
 315 
 316   if (LogCompilation && xtty != NULL) {
 317     task->log_task_queued();
 318   }
 319 
 320   // Notify CompilerThreads that a task is available.
 321   MethodCompileQueue_lock->notify_all();
 322 }
 323 
 324 /**
 325  * Empties compilation queue by putting all compilation tasks onto
 326  * a freelist. Furthermore, the method wakes up all threads that are
 327  * waiting on a compilation task to finish. This can happen if background
 328  * compilation is disabled.
 329  */
 330 void CompileQueue::free_all() {
 331   MutexLocker mu(MethodCompileQueue_lock);
 332   CompileTask* next = _first;
 333 
 334   // Iterate over all tasks in the compile queue
 335   while (next != NULL) {
 336     CompileTask* current = next;
 337     next = current->next();
 338     {
 339       // Wake up thread that blocks on the compile task.
 340       MutexLocker ct_lock(current->lock());
 341       current->lock()->notify();
 342     }
 343     // Put the task back on the freelist.
 344     CompileTask::free(current);
 345   }
 346   _first = NULL;
 347 
 348   // Wake up all threads that block on the queue.
 349   MethodCompileQueue_lock->notify_all();
 350 }
 351 
 352 /**
 353  * Get the next CompileTask from a CompileQueue
 354  */
 355 CompileTask* CompileQueue::get() {
 356   // save methods from RedefineClasses across safepoint
 357   // across MethodCompileQueue_lock below.
 358   methodHandle save_method;
 359   methodHandle save_hot_method;
 360 
 361   MutexLocker locker(MethodCompileQueue_lock);
 362   // If _first is NULL we have no more compile jobs. There are two reasons for
 363   // having no compile jobs: First, we compiled everything we wanted. Second,
 364   // we ran out of code cache so compilation has been disabled. In the latter
 365   // case we perform code cache sweeps to free memory such that we can re-enable
 366   // compilation.
 367   while (_first == NULL) {
 368     // Exit loop if compilation is disabled forever
 369     if (CompileBroker::is_compilation_disabled_forever()) {
 370       return NULL;
 371     }
 372 
 373     // If there are no compilation tasks and we can compile new jobs
 374     // (i.e., there is enough free space in the code cache) there is
 375     // no need to invoke the sweeper. As a result, the hotness of methods
 376     // remains unchanged. This behavior is desired, since we want to keep
 377     // the stable state, i.e., we do not want to evict methods from the
 378     // code cache if it is unnecessary.
 379     // We need a timed wait here, since compiler threads can exit if compilation
 380     // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
 381     // is not critical and we do not want idle compiler threads to wake up too often.
 382     MethodCompileQueue_lock->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
 383   }
 384 
 385   if (CompileBroker::is_compilation_disabled_forever()) {
 386     return NULL;
 387   }
 388 
 389   CompileTask* task;
 390   {
 391     NoSafepointVerifier nsv;
 392     task = CompilationPolicy::policy()->select_task(this);
 393   }
 394 
 395   if (task != NULL) {
 396     // Save method pointers across unlock safepoint.  The task is removed from
 397     // the compilation queue, which is walked during RedefineClasses.
 398     save_method = methodHandle(task->method());
 399     save_hot_method = methodHandle(task->hot_method());
 400 
 401     remove(task);
 402     purge_stale_tasks(); // may temporarily release MCQ lock
 403   }
 404 
 405   return task;
 406 }
 407 
 408 // Clean & deallocate stale compile tasks.
 409 // Temporarily releases MethodCompileQueue lock.
 410 void CompileQueue::purge_stale_tasks() {
 411   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 412   if (_first_stale != NULL) {
 413     // Stale tasks are purged when MCQ lock is released,
 414     // but _first_stale updates are protected by MCQ lock.
 415     // Once task processing starts and MCQ lock is released,
 416     // other compiler threads can reuse _first_stale.
 417     CompileTask* head = _first_stale;
 418     _first_stale = NULL;
 419     {
 420       MutexUnlocker ul(MethodCompileQueue_lock);
 421       for (CompileTask* task = head; task != NULL; ) {
 422         CompileTask* next_task = task->next();
 423         CompileTaskWrapper ctw(task); // Frees the task
 424         task->set_failure_reason("stale task");
 425         task = next_task;
 426       }
 427     }
 428   }
 429 }
 430 
 431 void CompileQueue::remove(CompileTask* task) {
 432    assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 433   if (task->prev() != NULL) {
 434     task->prev()->set_next(task->next());
 435   } else {
 436     // max is the first element
 437     assert(task == _first, "Sanity");
 438     _first = task->next();
 439   }
 440 
 441   if (task->next() != NULL) {
 442     task->next()->set_prev(task->prev());
 443   } else {
 444     // max is the last element
 445     assert(task == _last, "Sanity");
 446     _last = task->prev();
 447   }
 448   --_size;
 449 }
 450 
 451 void CompileQueue::remove_and_mark_stale(CompileTask* task) {
 452   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 453   remove(task);
 454 
 455   // Enqueue the task for reclamation (should be done outside MCQ lock)
 456   task->set_next(_first_stale);
 457   task->set_prev(NULL);
 458   _first_stale = task;
 459 }
 460 
 461 // methods in the compile queue need to be marked as used on the stack
 462 // so that they don't get reclaimed by Redefine Classes
 463 void CompileQueue::mark_on_stack() {
 464   CompileTask* task = _first;
 465   while (task != NULL) {
 466     task->mark_on_stack();
 467     task = task->next();
 468   }
 469 }
 470 
 471 
 472 CompileQueue* CompileBroker::compile_queue(int comp_level) {
 473   if (is_c2_compile(comp_level)) return _c2_compile_queue;
 474   if (is_c1_compile(comp_level)) return _c1_compile_queue;
 475   return NULL;
 476 }
 477 
 478 void CompileBroker::print_compile_queues(outputStream* st) {
 479   st->print_cr("Current compiles: ");
 480   MutexLocker locker(MethodCompileQueue_lock);
 481 
 482   char buf[2000];
 483   int buflen = sizeof(buf);
 484   Threads::print_threads_compiling(st, buf, buflen);
 485 
 486   st->cr();
 487   if (_c1_compile_queue != NULL) {
 488     _c1_compile_queue->print(st);
 489   }
 490   if (_c2_compile_queue != NULL) {
 491     _c2_compile_queue->print(st);
 492   }
 493 }
 494 
 495 void CompileQueue::print(outputStream* st) {
 496   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 497   st->print_cr("%s:", name());
 498   CompileTask* task = _first;
 499   if (task == NULL) {
 500     st->print_cr("Empty");
 501   } else {
 502     while (task != NULL) {
 503       task->print(st, NULL, true, true);
 504       task = task->next();
 505     }
 506   }
 507   st->cr();
 508 }
 509 
 510 void CompileQueue::print_tty() {
 511   ttyLocker ttyl;
 512   print(tty);
 513 }
 514 
 515 CompilerCounters::CompilerCounters() {
 516   _current_method[0] = '\0';
 517   _compile_type = CompileBroker::no_compile;
 518 }
 519 
 520 // ------------------------------------------------------------------
 521 // CompileBroker::compilation_init
 522 //
 523 // Initialize the Compilation object
 524 void CompileBroker::compilation_init(TRAPS) {
 525   _last_method_compiled[0] = '\0';
 526 
 527   // No need to initialize compilation system if we do not use it.
 528   if (!UseCompiler) {
 529     return;
 530   }
 531   // Set the interface to the current compiler(s).
 532   int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
 533   int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
 534 
 535 #if INCLUDE_JVMCI
 536   if (EnableJVMCI) {
 537     // This is creating a JVMCICompiler singleton.
 538     JVMCICompiler* jvmci = new JVMCICompiler();
 539 
 540     if (UseJVMCICompiler) {
 541       _compilers[1] = jvmci;
 542       if (FLAG_IS_DEFAULT(JVMCIThreads)) {
 543         if (BootstrapJVMCI) {
 544           // JVMCI will bootstrap so give it more threads
 545           c2_count = MIN2(32, os::active_processor_count());
 546         }
 547       } else {
 548         c2_count = JVMCIThreads;
 549       }
 550       if (FLAG_IS_DEFAULT(JVMCIHostThreads)) {
 551       } else {
 552         c1_count = JVMCIHostThreads;
 553       }
 554     }
 555   }
 556 #endif // INCLUDE_JVMCI
 557 
 558 #ifdef COMPILER1
 559   if (c1_count > 0) {
 560     _compilers[0] = new Compiler();
 561   }
 562 #endif // COMPILER1
 563 
 564 #ifdef COMPILER2
 565   if (true JVMCI_ONLY( && !UseJVMCICompiler)) {
 566     if (c2_count > 0) {
 567       _compilers[1] = new C2Compiler();
 568     }
 569   }
 570 #endif // COMPILER2
 571 
 572   // Start the compiler thread(s) and the sweeper thread
 573   init_compiler_sweeper_threads(c1_count, c2_count);
 574   // totalTime performance counter is always created as it is required
 575   // by the implementation of java.lang.management.CompilationMBean.
 576   {
 577     EXCEPTION_MARK;
 578     _perf_total_compilation =
 579                  PerfDataManager::create_counter(JAVA_CI, "totalTime",
 580                                                  PerfData::U_Ticks, CHECK);
 581   }
 582 
 583   if (UsePerfData) {
 584 
 585     EXCEPTION_MARK;
 586 
 587     // create the jvmstat performance counters
 588     _perf_osr_compilation =
 589                  PerfDataManager::create_counter(SUN_CI, "osrTime",
 590                                                  PerfData::U_Ticks, CHECK);
 591 
 592     _perf_standard_compilation =
 593                  PerfDataManager::create_counter(SUN_CI, "standardTime",
 594                                                  PerfData::U_Ticks, CHECK);
 595 
 596     _perf_total_bailout_count =
 597                  PerfDataManager::create_counter(SUN_CI, "totalBailouts",
 598                                                  PerfData::U_Events, CHECK);
 599 
 600     _perf_total_invalidated_count =
 601                  PerfDataManager::create_counter(SUN_CI, "totalInvalidates",
 602                                                  PerfData::U_Events, CHECK);
 603 
 604     _perf_total_compile_count =
 605                  PerfDataManager::create_counter(SUN_CI, "totalCompiles",
 606                                                  PerfData::U_Events, CHECK);
 607     _perf_total_osr_compile_count =
 608                  PerfDataManager::create_counter(SUN_CI, "osrCompiles",
 609                                                  PerfData::U_Events, CHECK);
 610 
 611     _perf_total_standard_compile_count =
 612                  PerfDataManager::create_counter(SUN_CI, "standardCompiles",
 613                                                  PerfData::U_Events, CHECK);
 614 
 615     _perf_sum_osr_bytes_compiled =
 616                  PerfDataManager::create_counter(SUN_CI, "osrBytes",
 617                                                  PerfData::U_Bytes, CHECK);
 618 
 619     _perf_sum_standard_bytes_compiled =
 620                  PerfDataManager::create_counter(SUN_CI, "standardBytes",
 621                                                  PerfData::U_Bytes, CHECK);
 622 
 623     _perf_sum_nmethod_size =
 624                  PerfDataManager::create_counter(SUN_CI, "nmethodSize",
 625                                                  PerfData::U_Bytes, CHECK);
 626 
 627     _perf_sum_nmethod_code_size =
 628                  PerfDataManager::create_counter(SUN_CI, "nmethodCodeSize",
 629                                                  PerfData::U_Bytes, CHECK);
 630 
 631     _perf_last_method =
 632                  PerfDataManager::create_string_variable(SUN_CI, "lastMethod",
 633                                        CompilerCounters::cmname_buffer_length,
 634                                        "", CHECK);
 635 
 636     _perf_last_failed_method =
 637             PerfDataManager::create_string_variable(SUN_CI, "lastFailedMethod",
 638                                        CompilerCounters::cmname_buffer_length,
 639                                        "", CHECK);
 640 
 641     _perf_last_invalidated_method =
 642         PerfDataManager::create_string_variable(SUN_CI, "lastInvalidatedMethod",
 643                                      CompilerCounters::cmname_buffer_length,
 644                                      "", CHECK);
 645 
 646     _perf_last_compile_type =
 647              PerfDataManager::create_variable(SUN_CI, "lastType",
 648                                               PerfData::U_None,
 649                                               (jlong)CompileBroker::no_compile,
 650                                               CHECK);
 651 
 652     _perf_last_compile_size =
 653              PerfDataManager::create_variable(SUN_CI, "lastSize",
 654                                               PerfData::U_Bytes,
 655                                               (jlong)CompileBroker::no_compile,
 656                                               CHECK);
 657 
 658 
 659     _perf_last_failed_type =
 660              PerfDataManager::create_variable(SUN_CI, "lastFailedType",
 661                                               PerfData::U_None,
 662                                               (jlong)CompileBroker::no_compile,
 663                                               CHECK);
 664 
 665     _perf_last_invalidated_type =
 666          PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
 667                                           PerfData::U_None,
 668                                           (jlong)CompileBroker::no_compile,
 669                                           CHECK);
 670   }
 671 
 672   _initialized = true;
 673 }
 674 
 675 
 676 JavaThread* CompileBroker::make_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
 677                                        AbstractCompiler* comp, bool compiler_thread, TRAPS) {
 678   JavaThread* thread = NULL;
 679   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_0);
 680   InstanceKlass* klass = InstanceKlass::cast(k);
 681   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
 682   Handle string = java_lang_String::create_from_str(name, CHECK_0);
 683 
 684   // Initialize thread_oop to put it into the system threadGroup
 685   Handle thread_group (THREAD,  Universe::system_thread_group());
 686   JavaValue result(T_VOID);
 687   JavaCalls::call_special(&result, thread_oop,
 688                        klass,
 689                        vmSymbols::object_initializer_name(),
 690                        vmSymbols::threadgroup_string_void_signature(),
 691                        thread_group,
 692                        string,
 693                        CHECK_0);
 694 
 695   {
 696     MutexLocker mu(Threads_lock, THREAD);
 697     if (compiler_thread) {
 698       thread = new CompilerThread(queue, counters);
 699     } else {
 700       thread = new CodeCacheSweeperThread();
 701     }
 702     // At this point the new CompilerThread data-races with this startup
 703     // thread (which I believe is the primoridal thread and NOT the VM
 704     // thread).  This means Java bytecodes being executed at startup can
 705     // queue compile jobs which will run at whatever default priority the
 706     // newly created CompilerThread runs at.
 707 
 708 
 709     // At this point it may be possible that no osthread was created for the
 710     // JavaThread due to lack of memory. We would have to throw an exception
 711     // in that case. However, since this must work and we do not allow
 712     // exceptions anyway, check and abort if this fails. But first release the
 713     // lock.
 714 
 715     if (thread != NULL && thread->osthread() != NULL) {
 716 
 717       java_lang_Thread::set_thread(thread_oop(), thread);
 718 
 719       // Note that this only sets the JavaThread _priority field, which by
 720       // definition is limited to Java priorities and not OS priorities.
 721       // The os-priority is set in the CompilerThread startup code itself
 722 
 723       java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
 724 
 725       // Note that we cannot call os::set_priority because it expects Java
 726       // priorities and we are *explicitly* using OS priorities so that it's
 727       // possible to set the compiler thread priority higher than any Java
 728       // thread.
 729 
 730       int native_prio = CompilerThreadPriority;
 731       if (native_prio == -1) {
 732         if (UseCriticalCompilerThreadPriority) {
 733           native_prio = os::java_to_os_priority[CriticalPriority];
 734         } else {
 735           native_prio = os::java_to_os_priority[NearMaxPriority];
 736         }
 737       }
 738       os::set_native_priority(thread, native_prio);
 739 
 740       java_lang_Thread::set_daemon(thread_oop());
 741 
 742       thread->set_threadObj(thread_oop());
 743       if (compiler_thread) {
 744         thread->as_CompilerThread()->set_compiler(comp);
 745       }
 746       Threads::add(thread);
 747       Thread::start(thread);
 748     }
 749   }
 750 
 751   // First release lock before aborting VM.
 752   if (thread == NULL || thread->osthread() == NULL) {
 753     vm_exit_during_initialization("java.lang.OutOfMemoryError",
 754                                   os::native_thread_creation_failed_msg());
 755   }
 756 
 757   // Let go of Threads_lock before yielding
 758   os::naked_yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
 759 
 760   return thread;
 761 }
 762 
 763 
 764 void CompileBroker::init_compiler_sweeper_threads(int c1_compiler_count, int c2_compiler_count) {
 765   EXCEPTION_MARK;
 766 #if !defined(ZERO)
 767   assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
 768 #endif // !ZERO
 769   // Initialize the compilation queue
 770   if (c2_compiler_count > 0) {
 771     const char* name = JVMCI_ONLY(UseJVMCICompiler ? "JVMCI compile queue" :) "C2 compile queue";
 772     _c2_compile_queue  = new CompileQueue(name);
 773     _compilers[1]->set_num_compiler_threads(c2_compiler_count);
 774   }
 775   if (c1_compiler_count > 0) {
 776     _c1_compile_queue  = new CompileQueue("C1 compile queue");
 777     _compilers[0]->set_num_compiler_threads(c1_compiler_count);
 778   }
 779 
 780   int compiler_count = c1_compiler_count + c2_compiler_count;
 781 
 782   char name_buffer[256];
 783   const bool compiler_thread = true;
 784   for (int i = 0; i < c2_compiler_count; i++) {
 785     // Create a name for our thread.
 786     sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
 787     CompilerCounters* counters = new CompilerCounters();
 788     make_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], compiler_thread, CHECK);
 789   }
 790 
 791   for (int i = c2_compiler_count; i < compiler_count; i++) {
 792     // Create a name for our thread.
 793     sprintf(name_buffer, "C1 CompilerThread%d", i);
 794     CompilerCounters* counters = new CompilerCounters();
 795     // C1
 796     make_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], compiler_thread, CHECK);
 797   }
 798 
 799   if (UsePerfData) {
 800     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
 801   }
 802 
 803   if (MethodFlushing) {
 804     // Initialize the sweeper thread
 805     make_thread("Sweeper thread", NULL, NULL, NULL, false, CHECK);
 806   }
 807 }
 808 
 809 
 810 /**
 811  * Set the methods on the stack as on_stack so that redefine classes doesn't
 812  * reclaim them. This method is executed at a safepoint.
 813  */
 814 void CompileBroker::mark_on_stack() {
 815   assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
 816   // Since we are at a safepoint, we do not need a lock to access
 817   // the compile queues.
 818   if (_c2_compile_queue != NULL) {
 819     _c2_compile_queue->mark_on_stack();
 820   }
 821   if (_c1_compile_queue != NULL) {
 822     _c1_compile_queue->mark_on_stack();
 823   }
 824 }
 825 
 826 // ------------------------------------------------------------------
 827 // CompileBroker::compile_method
 828 //
 829 // Request compilation of a method.
 830 void CompileBroker::compile_method_base(const methodHandle& method,
 831                                         int osr_bci,
 832                                         int comp_level,
 833                                         const methodHandle& hot_method,
 834                                         int hot_count,
 835                                         CompileTask::CompileReason compile_reason,
 836                                         bool blocking,
 837                                         Thread* thread) {
 838   guarantee(!method->is_abstract(), "cannot compile abstract methods");
 839   assert(method->method_holder()->is_instance_klass(),
 840          "sanity check");
 841   assert(!method->method_holder()->is_not_initialized(),
 842          "method holder must be initialized");
 843   assert(!method->is_method_handle_intrinsic(), "do not enqueue these guys");
 844 
 845   if (CIPrintRequests) {
 846     tty->print("request: ");
 847     method->print_short_name(tty);
 848     if (osr_bci != InvocationEntryBci) {
 849       tty->print(" osr_bci: %d", osr_bci);
 850     }
 851     tty->print(" level: %d comment: %s count: %d", comp_level, CompileTask::reason_name(compile_reason), hot_count);
 852     if (!hot_method.is_null()) {
 853       tty->print(" hot: ");
 854       if (hot_method() != method()) {
 855           hot_method->print_short_name(tty);
 856       } else {
 857         tty->print("yes");
 858       }
 859     }
 860     tty->cr();
 861   }
 862 
 863   // A request has been made for compilation.  Before we do any
 864   // real work, check to see if the method has been compiled
 865   // in the meantime with a definitive result.
 866   if (compilation_is_complete(method, osr_bci, comp_level)) {
 867     return;
 868   }
 869 
 870 #ifndef PRODUCT
 871   if (osr_bci != -1 && !FLAG_IS_DEFAULT(OSROnlyBCI)) {
 872     if ((OSROnlyBCI > 0) ? (OSROnlyBCI != osr_bci) : (-OSROnlyBCI == osr_bci)) {
 873       // Positive OSROnlyBCI means only compile that bci.  Negative means don't compile that BCI.
 874       return;
 875     }
 876   }
 877 #endif
 878 
 879   // If this method is already in the compile queue, then
 880   // we do not block the current thread.
 881   if (compilation_is_in_queue(method)) {
 882     // We may want to decay our counter a bit here to prevent
 883     // multiple denied requests for compilation.  This is an
 884     // open compilation policy issue. Note: The other possibility,
 885     // in the case that this is a blocking compile request, is to have
 886     // all subsequent blocking requesters wait for completion of
 887     // ongoing compiles. Note that in this case we'll need a protocol
 888     // for freeing the associated compile tasks. [Or we could have
 889     // a single static monitor on which all these waiters sleep.]
 890     return;
 891   }
 892 
 893   if (TieredCompilation) {
 894     // Tiered policy requires MethodCounters to exist before adding a method to
 895     // the queue. Create if we don't have them yet.
 896     method->get_method_counters(thread);
 897   }
 898 
 899   // Outputs from the following MutexLocker block:
 900   CompileTask* task     = NULL;
 901   CompileQueue* queue  = compile_queue(comp_level);
 902 
 903   // Acquire our lock.
 904   {
 905     MutexLocker locker(MethodCompileQueue_lock, thread);
 906 
 907     // Make sure the method has not slipped into the queues since
 908     // last we checked; note that those checks were "fast bail-outs".
 909     // Here we need to be more careful, see 14012000 below.
 910     if (compilation_is_in_queue(method)) {
 911       return;
 912     }
 913 
 914     // We need to check again to see if the compilation has
 915     // completed.  A previous compilation may have registered
 916     // some result.
 917     if (compilation_is_complete(method, osr_bci, comp_level)) {
 918       return;
 919     }
 920 
 921     // We now know that this compilation is not pending, complete,
 922     // or prohibited.  Assign a compile_id to this compilation
 923     // and check to see if it is in our [Start..Stop) range.
 924     int compile_id = assign_compile_id(method, osr_bci);
 925     if (compile_id == 0) {
 926       // The compilation falls outside the allowed range.
 927       return;
 928     }
 929 
 930 #if INCLUDE_JVMCI
 931     if (UseJVMCICompiler) {
 932       if (blocking) {
 933         // Don't allow blocking compiles for requests triggered by JVMCI.
 934         if (thread->is_Compiler_thread()) {
 935           blocking = false;
 936         }
 937 
 938         // Don't allow blocking compiles if inside a class initializer or while performing class loading
 939         vframeStream vfst((JavaThread*) thread);
 940         for (; !vfst.at_end(); vfst.next()) {
 941           if (vfst.method()->is_static_initializer() ||
 942               (vfst.method()->method_holder()->is_subclass_of(SystemDictionary::ClassLoader_klass()) &&
 943                   vfst.method()->name() == vmSymbols::loadClass_name())) {
 944             blocking = false;
 945             break;
 946           }
 947         }
 948 
 949         // Don't allow blocking compilation requests to JVMCI
 950         // if JVMCI itself is not yet initialized
 951         if (!JVMCIRuntime::is_HotSpotJVMCIRuntime_initialized() && compiler(comp_level)->is_jvmci()) {
 952           blocking = false;
 953         }
 954 
 955         // Don't allow blocking compilation requests if we are in JVMCIRuntime::shutdown
 956         // to avoid deadlock between compiler thread(s) and threads run at shutdown
 957         // such as the DestroyJavaVM thread.
 958         if (JVMCIRuntime::shutdown_called()) {
 959           blocking = false;
 960         }
 961       }
 962     }
 963 #endif // INCLUDE_JVMCI
 964 
 965     // We will enter the compilation in the queue.
 966     // 14012000: Note that this sets the queued_for_compile bits in
 967     // the target method. We can now reason that a method cannot be
 968     // queued for compilation more than once, as follows:
 969     // Before a thread queues a task for compilation, it first acquires
 970     // the compile queue lock, then checks if the method's queued bits
 971     // are set or it has already been compiled. Thus there can not be two
 972     // instances of a compilation task for the same method on the
 973     // compilation queue. Consider now the case where the compilation
 974     // thread has already removed a task for that method from the queue
 975     // and is in the midst of compiling it. In this case, the
 976     // queued_for_compile bits must be set in the method (and these
 977     // will be visible to the current thread, since the bits were set
 978     // under protection of the compile queue lock, which we hold now.
 979     // When the compilation completes, the compiler thread first sets
 980     // the compilation result and then clears the queued_for_compile
 981     // bits. Neither of these actions are protected by a barrier (or done
 982     // under the protection of a lock), so the only guarantee we have
 983     // (on machines with TSO (Total Store Order)) is that these values
 984     // will update in that order. As a result, the only combinations of
 985     // these bits that the current thread will see are, in temporal order:
 986     // <RESULT, QUEUE> :
 987     //     <0, 1> : in compile queue, but not yet compiled
 988     //     <1, 1> : compiled but queue bit not cleared
 989     //     <1, 0> : compiled and queue bit cleared
 990     // Because we first check the queue bits then check the result bits,
 991     // we are assured that we cannot introduce a duplicate task.
 992     // Note that if we did the tests in the reverse order (i.e. check
 993     // result then check queued bit), we could get the result bit before
 994     // the compilation completed, and the queue bit after the compilation
 995     // completed, and end up introducing a "duplicate" (redundant) task.
 996     // In that case, the compiler thread should first check if a method
 997     // has already been compiled before trying to compile it.
 998     // NOTE: in the event that there are multiple compiler threads and
 999     // there is de-optimization/recompilation, things will get hairy,
1000     // and in that case it's best to protect both the testing (here) of
1001     // these bits, and their updating (here and elsewhere) under a
1002     // common lock.
1003     task = create_compile_task(queue,
1004                                compile_id, method,
1005                                osr_bci, comp_level,
1006                                hot_method, hot_count, compile_reason,
1007                                blocking);
1008   }
1009 
1010   if (blocking) {
1011     wait_for_completion(task);
1012   }
1013 }
1014 
1015 nmethod* CompileBroker::compile_method(const methodHandle& method, int osr_bci,
1016                                        int comp_level,
1017                                        const methodHandle& hot_method, int hot_count,
1018                                        CompileTask::CompileReason compile_reason,
1019                                        Thread* THREAD) {
1020   // Do nothing if compilebroker is not initalized or compiles are submitted on level none
1021   if (!_initialized || comp_level == CompLevel_none) {
1022     return NULL;
1023   }
1024 
1025   AbstractCompiler *comp = CompileBroker::compiler(comp_level);
1026   assert(comp != NULL, "Ensure we have a compiler");
1027 
1028   DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, comp);
1029   nmethod* nm = CompileBroker::compile_method(method, osr_bci, comp_level, hot_method, hot_count, compile_reason, directive, THREAD);
1030   DirectivesStack::release(directive);
1031   return nm;
1032 }
1033 
1034 nmethod* CompileBroker::compile_method(const methodHandle& method, int osr_bci,
1035                                          int comp_level,
1036                                          const methodHandle& hot_method, int hot_count,
1037                                          CompileTask::CompileReason compile_reason,
1038                                          DirectiveSet* directive,
1039                                          Thread* THREAD) {
1040 
1041   // make sure arguments make sense
1042   assert(method->method_holder()->is_instance_klass(), "not an instance method");
1043   assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range");
1044   assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods");
1045   assert(!method->method_holder()->is_not_initialized(), "method holder must be initialized");
1046   assert(!TieredCompilation || comp_level <= TieredStopAtLevel, "Invalid compilation level");
1047   // allow any levels for WhiteBox
1048   assert(WhiteBoxAPI || TieredCompilation || comp_level == CompLevel_highest_tier, "only CompLevel_highest_tier must be used in non-tiered");
1049   // return quickly if possible
1050 
1051   // Lambda forms with an Object in their signature can be passed a
1052   // value type. If compiled as root of a compilation, C2 has no way
1053   // to know a value type is passed.
1054   if (ValueTypePassFieldsAsArgs && method->is_compiled_lambda_form()) {
1055     ResourceMark rm;
1056     for (SignatureStream ss(method->signature()); !ss.at_return_type(); ss.next()) {
1057       if (ss.type() == T_VALUETYPE) {
1058         return NULL;
1059       }
1060     }
1061   }
1062 
1063   // Don't compile methods in __Value if value types are disabled
1064   if (!EnableMVT && !EnableValhalla && method->method_holder()->name() == vmSymbols::java_lang____Value()) {
1065     // This may happen with CompileTheWorld, ReplayCompiles or compilations triggered through the WhiteBox API
1066     return NULL;
1067   }
1068 
1069   // lock, make sure that the compilation
1070   // isn't prohibited in a straightforward way.
1071   AbstractCompiler* comp = CompileBroker::compiler(comp_level);
1072   if (comp == NULL || !comp->can_compile_method(method) ||
1073       compilation_is_prohibited(method, osr_bci, comp_level, directive->ExcludeOption)) {
1074     return NULL;
1075   }
1076 
1077 #if INCLUDE_JVMCI
1078   if (comp->is_jvmci() && !JVMCIRuntime::can_initialize_JVMCI()) {
1079     return NULL;
1080   }
1081 #endif
1082 
1083   if (osr_bci == InvocationEntryBci) {
1084     // standard compilation
1085     CompiledMethod* method_code = method->code();
1086     if (method_code != NULL && method_code->is_nmethod()) {
1087       if (compilation_is_complete(method, osr_bci, comp_level)) {
1088         return (nmethod*) method_code;
1089       }
1090     }
1091     if (method->is_not_compilable(comp_level)) {
1092       return NULL;
1093     }
1094   } else {
1095     // osr compilation
1096 #ifndef TIERED
1097     // seems like an assert of dubious value
1098     assert(comp_level == CompLevel_highest_tier,
1099            "all OSR compiles are assumed to be at a single compilation level");
1100 #endif // TIERED
1101     // We accept a higher level osr method
1102     nmethod* nm = method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
1103     if (nm != NULL) return nm;
1104     if (method->is_not_osr_compilable(comp_level)) return NULL;
1105   }
1106 
1107   assert(!HAS_PENDING_EXCEPTION, "No exception should be present");
1108   // some prerequisites that are compiler specific
1109   if (comp->is_c2()) {
1110     method->constants()->resolve_string_constants(CHECK_AND_CLEAR_NULL);
1111     // Resolve all classes seen in the signature of the method
1112     // we are compiling.
1113     Method::load_signature_classes(method, CHECK_AND_CLEAR_NULL);
1114   }
1115 
1116   // If the method is native, do the lookup in the thread requesting
1117   // the compilation. Native lookups can load code, which is not
1118   // permitted during compilation.
1119   //
1120   // Note: A native method implies non-osr compilation which is
1121   //       checked with an assertion at the entry of this method.
1122   if (method->is_native() && !method->is_method_handle_intrinsic()) {
1123     bool in_base_library;
1124     address adr = NativeLookup::lookup(method, in_base_library, THREAD);
1125     if (HAS_PENDING_EXCEPTION) {
1126       // In case of an exception looking up the method, we just forget
1127       // about it. The interpreter will kick-in and throw the exception.
1128       method->set_not_compilable(); // implies is_not_osr_compilable()
1129       CLEAR_PENDING_EXCEPTION;
1130       return NULL;
1131     }
1132     assert(method->has_native_function(), "must have native code by now");
1133   }
1134 
1135   // RedefineClasses() has replaced this method; just return
1136   if (method->is_old()) {
1137     return NULL;
1138   }
1139 
1140   // JVMTI -- post_compile_event requires jmethod_id() that may require
1141   // a lock the compiling thread can not acquire. Prefetch it here.
1142   if (JvmtiExport::should_post_compiled_method_load()) {
1143     method->jmethod_id();
1144   }
1145 
1146   // do the compilation
1147   if (method->is_native()) {
1148     if (!PreferInterpreterNativeStubs || method->is_method_handle_intrinsic()) {
1149       // The following native methods:
1150       //
1151       // java.lang.Float.intBitsToFloat
1152       // java.lang.Float.floatToRawIntBits
1153       // java.lang.Double.longBitsToDouble
1154       // java.lang.Double.doubleToRawLongBits
1155       //
1156       // are called through the interpreter even if interpreter native stubs
1157       // are not preferred (i.e., calling through adapter handlers is preferred).
1158       // The reason is that on x86_32 signaling NaNs (sNaNs) are not preserved
1159       // if the version of the methods from the native libraries is called.
1160       // As the interpreter and the C2-intrinsified version of the methods preserves
1161       // sNaNs, that would result in an inconsistent way of handling of sNaNs.
1162       if ((UseSSE >= 1 &&
1163           (method->intrinsic_id() == vmIntrinsics::_intBitsToFloat ||
1164            method->intrinsic_id() == vmIntrinsics::_floatToRawIntBits)) ||
1165           (UseSSE >= 2 &&
1166            (method->intrinsic_id() == vmIntrinsics::_longBitsToDouble ||
1167             method->intrinsic_id() == vmIntrinsics::_doubleToRawLongBits))) {
1168         return NULL;
1169       }
1170 
1171       // To properly handle the appendix argument for out-of-line calls we are using a small trampoline that
1172       // pops off the appendix argument and jumps to the target (see gen_special_dispatch in SharedRuntime).
1173       //
1174       // Since normal compiled-to-compiled calls are not able to handle such a thing we MUST generate an adapter
1175       // in this case.  If we can't generate one and use it we can not execute the out-of-line method handle calls.
1176       AdapterHandlerLibrary::create_native_wrapper(method);
1177     } else {
1178       return NULL;
1179     }
1180   } else {
1181     // If the compiler is shut off due to code cache getting full
1182     // fail out now so blocking compiles dont hang the java thread
1183     if (!should_compile_new_jobs()) {
1184       CompilationPolicy::policy()->delay_compilation(method());
1185       return NULL;
1186     }
1187     bool is_blocking = !directive->BackgroundCompilationOption || CompileTheWorld || ReplayCompiles;
1188     compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, compile_reason, is_blocking, THREAD);
1189   }
1190 
1191   // return requested nmethod
1192   // We accept a higher level osr method
1193   if (osr_bci == InvocationEntryBci) {
1194     CompiledMethod* code = method->code();
1195     if (code == NULL) {
1196       return (nmethod*) code;
1197     } else {
1198       return code->as_nmethod_or_null();
1199     }
1200   }
1201   return method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
1202 }
1203 
1204 
1205 // ------------------------------------------------------------------
1206 // CompileBroker::compilation_is_complete
1207 //
1208 // See if compilation of this method is already complete.
1209 bool CompileBroker::compilation_is_complete(const methodHandle& method,
1210                                             int                 osr_bci,
1211                                             int                 comp_level) {
1212   bool is_osr = (osr_bci != standard_entry_bci);
1213   if (is_osr) {
1214     if (method->is_not_osr_compilable(comp_level)) {
1215       return true;
1216     } else {
1217       nmethod* result = method->lookup_osr_nmethod_for(osr_bci, comp_level, true);
1218       return (result != NULL);
1219     }
1220   } else {
1221     if (method->is_not_compilable(comp_level)) {
1222       return true;
1223     } else {
1224       CompiledMethod* result = method->code();
1225       if (result == NULL) return false;
1226       return comp_level == result->comp_level();
1227     }
1228   }
1229 }
1230 
1231 
1232 /**
1233  * See if this compilation is already requested.
1234  *
1235  * Implementation note: there is only a single "is in queue" bit
1236  * for each method.  This means that the check below is overly
1237  * conservative in the sense that an osr compilation in the queue
1238  * will block a normal compilation from entering the queue (and vice
1239  * versa).  This can be remedied by a full queue search to disambiguate
1240  * cases.  If it is deemed profitable, this may be done.
1241  */
1242 bool CompileBroker::compilation_is_in_queue(const methodHandle& method) {
1243   return method->queued_for_compilation();
1244 }
1245 
1246 // ------------------------------------------------------------------
1247 // CompileBroker::compilation_is_prohibited
1248 //
1249 // See if this compilation is not allowed.
1250 bool CompileBroker::compilation_is_prohibited(const methodHandle& method, int osr_bci, int comp_level, bool excluded) {
1251   bool is_native = method->is_native();
1252   // Some compilers may not support the compilation of natives.
1253   AbstractCompiler *comp = compiler(comp_level);
1254   if (is_native &&
1255       (!CICompileNatives || comp == NULL || !comp->supports_native())) {
1256     method->set_not_compilable_quietly(comp_level);
1257     return true;
1258   }
1259 
1260   bool is_osr = (osr_bci != standard_entry_bci);
1261   // Some compilers may not support on stack replacement.
1262   if (is_osr &&
1263       (!CICompileOSR || comp == NULL || !comp->supports_osr())) {
1264     method->set_not_osr_compilable(comp_level);
1265     return true;
1266   }
1267 
1268   // The method may be explicitly excluded by the user.
1269   double scale;
1270   if (excluded || (CompilerOracle::has_option_value(method, "CompileThresholdScaling", scale) && scale == 0)) {
1271     bool quietly = CompilerOracle::should_exclude_quietly();
1272     if (PrintCompilation && !quietly) {
1273       // This does not happen quietly...
1274       ResourceMark rm;
1275       tty->print("### Excluding %s:%s",
1276                  method->is_native() ? "generation of native wrapper" : "compile",
1277                  (method->is_static() ? " static" : ""));
1278       method->print_short_name(tty);
1279       tty->cr();
1280     }
1281     method->set_not_compilable(comp_level, !quietly, "excluded by CompileCommand");
1282   }
1283 
1284   return false;
1285 }
1286 
1287 /**
1288  * Generate serialized IDs for compilation requests. If certain debugging flags are used
1289  * and the ID is not within the specified range, the method is not compiled and 0 is returned.
1290  * The function also allows to generate separate compilation IDs for OSR compilations.
1291  */
1292 int CompileBroker::assign_compile_id(const methodHandle& method, int osr_bci) {
1293 #ifdef ASSERT
1294   bool is_osr = (osr_bci != standard_entry_bci);
1295   int id;
1296   if (method->is_native()) {
1297     assert(!is_osr, "can't be osr");
1298     // Adapters, native wrappers and method handle intrinsics
1299     // should be generated always.
1300     return Atomic::add(1, &_compilation_id);
1301   } else if (CICountOSR && is_osr) {
1302     id = Atomic::add(1, &_osr_compilation_id);
1303     if (CIStartOSR <= id && id < CIStopOSR) {
1304       return id;
1305     }
1306   } else {
1307     id = Atomic::add(1, &_compilation_id);
1308     if (CIStart <= id && id < CIStop) {
1309       return id;
1310     }
1311   }
1312 
1313   // Method was not in the appropriate compilation range.
1314   method->set_not_compilable_quietly();
1315   return 0;
1316 #else
1317   // CICountOSR is a develop flag and set to 'false' by default. In a product built,
1318   // only _compilation_id is incremented.
1319   return Atomic::add(1, &_compilation_id);
1320 #endif
1321 }
1322 
1323 // ------------------------------------------------------------------
1324 // CompileBroker::assign_compile_id_unlocked
1325 //
1326 // Public wrapper for assign_compile_id that acquires the needed locks
1327 uint CompileBroker::assign_compile_id_unlocked(Thread* thread, const methodHandle& method, int osr_bci) {
1328   MutexLocker locker(MethodCompileQueue_lock, thread);
1329   return assign_compile_id(method, osr_bci);
1330 }
1331 
1332 // ------------------------------------------------------------------
1333 // CompileBroker::preload_classes
1334 void CompileBroker::preload_classes(const methodHandle& method, TRAPS) {
1335   // Move this code over from c1_Compiler.cpp
1336   ShouldNotReachHere();
1337 }
1338 
1339 
1340 // ------------------------------------------------------------------
1341 // CompileBroker::create_compile_task
1342 //
1343 // Create a CompileTask object representing the current request for
1344 // compilation.  Add this task to the queue.
1345 CompileTask* CompileBroker::create_compile_task(CompileQueue*       queue,
1346                                                 int                 compile_id,
1347                                                 const methodHandle& method,
1348                                                 int                 osr_bci,
1349                                                 int                 comp_level,
1350                                                 const methodHandle& hot_method,
1351                                                 int                 hot_count,
1352                                                 CompileTask::CompileReason compile_reason,
1353                                                 bool                blocking) {
1354   CompileTask* new_task = CompileTask::allocate();
1355   new_task->initialize(compile_id, method, osr_bci, comp_level,
1356                        hot_method, hot_count, compile_reason,
1357                        blocking);
1358   queue->add(new_task);
1359   return new_task;
1360 }
1361 
1362 #if INCLUDE_JVMCI
1363 // The number of milliseconds to wait before checking if
1364 // JVMCI compilation has made progress.
1365 static const long JVMCI_COMPILATION_PROGRESS_WAIT_TIMESLICE = 500;
1366 
1367 // The number of JVMCI compilation progress checks that must fail
1368 // before unblocking a thread waiting for a blocking compilation.
1369 static const int JVMCI_COMPILATION_PROGRESS_WAIT_ATTEMPTS = 5;
1370 
1371 /**
1372  * Waits for a JVMCI compiler to complete a given task. This thread
1373  * waits until either the task completes or it sees no JVMCI compilation
1374  * progress for N consecutive milliseconds where N is
1375  * JVMCI_COMPILATION_PROGRESS_WAIT_TIMESLICE *
1376  * JVMCI_COMPILATION_PROGRESS_WAIT_ATTEMPTS.
1377  *
1378  * @return true if this thread needs to free/recycle the task
1379  */
1380 bool CompileBroker::wait_for_jvmci_completion(JVMCICompiler* jvmci, CompileTask* task, JavaThread* thread) {
1381   MutexLocker waiter(task->lock(), thread);
1382   int progress_wait_attempts = 0;
1383   int methods_compiled = jvmci->methods_compiled();
1384   while (!task->is_complete() && !is_compilation_disabled_forever() &&
1385          task->lock()->wait(!Mutex::_no_safepoint_check_flag, JVMCI_COMPILATION_PROGRESS_WAIT_TIMESLICE)) {
1386     CompilerThread* jvmci_compiler_thread = task->jvmci_compiler_thread();
1387 
1388     bool progress;
1389     if (jvmci_compiler_thread != NULL) {
1390       // If the JVMCI compiler thread is not blocked, we deem it to be making progress.
1391       progress = jvmci_compiler_thread->thread_state() != _thread_blocked;
1392     } else {
1393       // Still waiting on JVMCI compiler queue. This thread may be holding a lock
1394       // that all JVMCI compiler threads are blocked on. We use the counter for
1395       // successful JVMCI compilations to determine whether JVMCI compilation
1396       // is still making progress through the JVMCI compiler queue.
1397       progress = jvmci->methods_compiled() != methods_compiled;
1398     }
1399 
1400     if (!progress) {
1401       if (++progress_wait_attempts == JVMCI_COMPILATION_PROGRESS_WAIT_ATTEMPTS) {
1402         if (PrintCompilation) {
1403           task->print(tty, "wait for blocking compilation timed out");
1404         }
1405         break;
1406       }
1407     } else {
1408       progress_wait_attempts = 0;
1409       if (jvmci_compiler_thread == NULL) {
1410         methods_compiled = jvmci->methods_compiled();
1411       }
1412     }
1413   }
1414   task->clear_waiter();
1415   return task->is_complete();
1416 }
1417 #endif
1418 
1419 /**
1420  *  Wait for the compilation task to complete.
1421  */
1422 void CompileBroker::wait_for_completion(CompileTask* task) {
1423   if (CIPrintCompileQueue) {
1424     ttyLocker ttyl;
1425     tty->print_cr("BLOCKING FOR COMPILE");
1426   }
1427 
1428   assert(task->is_blocking(), "can only wait on blocking task");
1429 
1430   JavaThread* thread = JavaThread::current();
1431   thread->set_blocked_on_compilation(true);
1432 
1433   methodHandle method(thread, task->method());
1434   bool free_task;
1435 #if INCLUDE_JVMCI
1436   AbstractCompiler* comp = compiler(task->comp_level());
1437   if (comp->is_jvmci()) {
1438     free_task = wait_for_jvmci_completion((JVMCICompiler*) comp, task, thread);
1439   } else
1440 #endif
1441   {
1442     MutexLocker waiter(task->lock(), thread);
1443     free_task = true;
1444     while (!task->is_complete() && !is_compilation_disabled_forever()) {
1445       task->lock()->wait();
1446     }
1447   }
1448 
1449   thread->set_blocked_on_compilation(false);
1450   if (free_task) {
1451     if (is_compilation_disabled_forever()) {
1452       CompileTask::free(task);
1453       return;
1454     }
1455 
1456     // It is harmless to check this status without the lock, because
1457     // completion is a stable property (until the task object is recycled).
1458     assert(task->is_complete(), "Compilation should have completed");
1459     assert(task->code_handle() == NULL, "must be reset");
1460 
1461     // By convention, the waiter is responsible for recycling a
1462     // blocking CompileTask. Since there is only one waiter ever
1463     // waiting on a CompileTask, we know that no one else will
1464     // be using this CompileTask; we can free it.
1465     CompileTask::free(task);
1466   }
1467 }
1468 
1469 /**
1470  * Initialize compiler thread(s) + compiler object(s). The postcondition
1471  * of this function is that the compiler runtimes are initialized and that
1472  * compiler threads can start compiling.
1473  */
1474 bool CompileBroker::init_compiler_runtime() {
1475   CompilerThread* thread = CompilerThread::current();
1476   AbstractCompiler* comp = thread->compiler();
1477   // Final sanity check - the compiler object must exist
1478   guarantee(comp != NULL, "Compiler object must exist");
1479 
1480   int system_dictionary_modification_counter;
1481   {
1482     MutexLocker locker(Compile_lock, thread);
1483     system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1484   }
1485 
1486   {
1487     // Must switch to native to allocate ci_env
1488     ThreadToNativeFromVM ttn(thread);
1489     ciEnv ci_env(NULL, system_dictionary_modification_counter);
1490     // Cache Jvmti state
1491     ci_env.cache_jvmti_state();
1492     // Cache DTrace flags
1493     ci_env.cache_dtrace_flags();
1494 
1495     // Switch back to VM state to do compiler initialization
1496     ThreadInVMfromNative tv(thread);
1497     ResetNoHandleMark rnhm;
1498 
1499     // Perform per-thread and global initializations
1500     comp->initialize();
1501   }
1502 
1503   if (comp->is_failed()) {
1504     disable_compilation_forever();
1505     // If compiler initialization failed, no compiler thread that is specific to a
1506     // particular compiler runtime will ever start to compile methods.
1507     shutdown_compiler_runtime(comp, thread);
1508     return false;
1509   }
1510 
1511   // C1 specific check
1512   if (comp->is_c1() && (thread->get_buffer_blob() == NULL)) {
1513     warning("Initialization of %s thread failed (no space to run compilers)", thread->name());
1514     return false;
1515   }
1516 
1517   return true;
1518 }
1519 
1520 /**
1521  * If C1 and/or C2 initialization failed, we shut down all compilation.
1522  * We do this to keep things simple. This can be changed if it ever turns
1523  * out to be a problem.
1524  */
1525 void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) {
1526   // Free buffer blob, if allocated
1527   if (thread->get_buffer_blob() != NULL) {
1528     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1529     CodeCache::free(thread->get_buffer_blob());
1530   }
1531 
1532   if (comp->should_perform_shutdown()) {
1533     // There are two reasons for shutting down the compiler
1534     // 1) compiler runtime initialization failed
1535     // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing
1536     warning("%s initialization failed. Shutting down all compilers", comp->name());
1537 
1538     // Only one thread per compiler runtime object enters here
1539     // Set state to shut down
1540     comp->set_shut_down();
1541 
1542     // Delete all queued compilation tasks to make compiler threads exit faster.
1543     if (_c1_compile_queue != NULL) {
1544       _c1_compile_queue->free_all();
1545     }
1546 
1547     if (_c2_compile_queue != NULL) {
1548       _c2_compile_queue->free_all();
1549     }
1550 
1551     // Set flags so that we continue execution with using interpreter only.
1552     UseCompiler    = false;
1553     UseInterpreter = true;
1554 
1555     // We could delete compiler runtimes also. However, there are references to
1556     // the compiler runtime(s) (e.g.,  nmethod::is_compiled_by_c1()) which then
1557     // fail. This can be done later if necessary.
1558   }
1559 }
1560 
1561 // ------------------------------------------------------------------
1562 // CompileBroker::compiler_thread_loop
1563 //
1564 // The main loop run by a CompilerThread.
1565 void CompileBroker::compiler_thread_loop() {
1566   CompilerThread* thread = CompilerThread::current();
1567   CompileQueue* queue = thread->queue();
1568   // For the thread that initializes the ciObjectFactory
1569   // this resource mark holds all the shared objects
1570   ResourceMark rm;
1571 
1572   // First thread to get here will initialize the compiler interface
1573 
1574   {
1575     ASSERT_IN_VM;
1576     MutexLocker only_one (CompileThread_lock, thread);
1577     if (!ciObjectFactory::is_initialized()) {
1578       ciObjectFactory::initialize();
1579     }
1580   }
1581 
1582   // Open a log.
1583   if (LogCompilation) {
1584     init_compiler_thread_log();
1585   }
1586   CompileLog* log = thread->log();
1587   if (log != NULL) {
1588     log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
1589                     thread->name(),
1590                     os::current_thread_id(),
1591                     os::current_process_id());
1592     log->stamp();
1593     log->end_elem();
1594   }
1595 
1596   // If compiler thread/runtime initialization fails, exit the compiler thread
1597   if (!init_compiler_runtime()) {
1598     return;
1599   }
1600 
1601   // Poll for new compilation tasks as long as the JVM runs. Compilation
1602   // should only be disabled if something went wrong while initializing the
1603   // compiler runtimes. This, in turn, should not happen. The only known case
1604   // when compiler runtime initialization fails is if there is not enough free
1605   // space in the code cache to generate the necessary stubs, etc.
1606   while (!is_compilation_disabled_forever()) {
1607     // We need this HandleMark to avoid leaking VM handles.
1608     HandleMark hm(thread);
1609 
1610     CompileTask* task = queue->get();
1611     if (task == NULL) {
1612       continue;
1613     }
1614 
1615     // Give compiler threads an extra quanta.  They tend to be bursty and
1616     // this helps the compiler to finish up the job.
1617     if (CompilerThreadHintNoPreempt) {
1618       os::hint_no_preempt();
1619     }
1620 
1621     // Assign the task to the current thread.  Mark this compilation
1622     // thread as active for the profiler.
1623     CompileTaskWrapper ctw(task);
1624     nmethodLocker result_handle;  // (handle for the nmethod produced by this task)
1625     task->set_code_handle(&result_handle);
1626     methodHandle method(thread, task->method());
1627 
1628     // Never compile a method if breakpoints are present in it
1629     if (method()->number_of_breakpoints() == 0) {
1630       // Compile the method.
1631       if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
1632         invoke_compiler_on_method(task);
1633       } else {
1634         // After compilation is disabled, remove remaining methods from queue
1635         method->clear_queued_for_compilation();
1636         task->set_failure_reason("compilation is disabled");
1637       }
1638     }
1639   }
1640 
1641   // Shut down compiler runtime
1642   shutdown_compiler_runtime(thread->compiler(), thread);
1643 }
1644 
1645 // ------------------------------------------------------------------
1646 // CompileBroker::init_compiler_thread_log
1647 //
1648 // Set up state required by +LogCompilation.
1649 void CompileBroker::init_compiler_thread_log() {
1650     CompilerThread* thread = CompilerThread::current();
1651     char  file_name[4*K];
1652     FILE* fp = NULL;
1653     intx thread_id = os::current_thread_id();
1654     for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) {
1655       const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL);
1656       if (dir == NULL) {
1657         jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log",
1658                      thread_id, os::current_process_id());
1659       } else {
1660         jio_snprintf(file_name, sizeof(file_name),
1661                      "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir,
1662                      os::file_separator(), thread_id, os::current_process_id());
1663       }
1664 
1665       fp = fopen(file_name, "wt");
1666       if (fp != NULL) {
1667         if (LogCompilation && Verbose) {
1668           tty->print_cr("Opening compilation log %s", file_name);
1669         }
1670         CompileLog* log = new(ResourceObj::C_HEAP, mtCompiler) CompileLog(file_name, fp, thread_id);
1671         if (log == NULL) {
1672           fclose(fp);
1673           return;
1674         }
1675         thread->init_log(log);
1676 
1677         if (xtty != NULL) {
1678           ttyLocker ttyl;
1679           // Record any per thread log files
1680           xtty->elem("thread_logfile thread='" INTX_FORMAT "' filename='%s'", thread_id, file_name);
1681         }
1682         return;
1683       }
1684     }
1685     warning("Cannot open log file: %s", file_name);
1686 }
1687 
1688 void CompileBroker::log_metaspace_failure() {
1689   const char* message = "some methods may not be compiled because metaspace "
1690                         "is out of memory";
1691   if (_compilation_log != NULL) {
1692     _compilation_log->log_metaspace_failure(message);
1693   }
1694   if (PrintCompilation) {
1695     tty->print_cr("COMPILE PROFILING SKIPPED: %s", message);
1696   }
1697 }
1698 
1699 
1700 // ------------------------------------------------------------------
1701 // CompileBroker::set_should_block
1702 //
1703 // Set _should_block.
1704 // Call this from the VM, with Threads_lock held and a safepoint requested.
1705 void CompileBroker::set_should_block() {
1706   assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
1707   assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint already");
1708 #ifndef PRODUCT
1709   if (PrintCompilation && (Verbose || WizardMode))
1710     tty->print_cr("notifying compiler thread pool to block");
1711 #endif
1712   _should_block = true;
1713 }
1714 
1715 // ------------------------------------------------------------------
1716 // CompileBroker::maybe_block
1717 //
1718 // Call this from the compiler at convenient points, to poll for _should_block.
1719 void CompileBroker::maybe_block() {
1720   if (_should_block) {
1721 #ifndef PRODUCT
1722     if (PrintCompilation && (Verbose || WizardMode))
1723       tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current()));
1724 #endif
1725     ThreadInVMfromNative tivfn(JavaThread::current());
1726   }
1727 }
1728 
1729 // wrapper for CodeCache::print_summary()
1730 static void codecache_print(bool detailed)
1731 {
1732   ResourceMark rm;
1733   stringStream s;
1734   // Dump code cache  into a buffer before locking the tty,
1735   {
1736     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1737     CodeCache::print_summary(&s, detailed);
1738   }
1739   ttyLocker ttyl;
1740   tty->print("%s", s.as_string());
1741 }
1742 
1743 // wrapper for CodeCache::print_summary() using outputStream
1744 static void codecache_print(outputStream* out, bool detailed) {
1745   ResourceMark rm;
1746   stringStream s;
1747 
1748   // Dump code cache into a buffer
1749   {
1750     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1751     CodeCache::print_summary(&s, detailed);
1752   }
1753 
1754   char* remaining_log = s.as_string();
1755   while (*remaining_log != '\0') {
1756     char* eol = strchr(remaining_log, '\n');
1757     if (eol == NULL) {
1758       out->print_cr("%s", remaining_log);
1759       remaining_log = remaining_log + strlen(remaining_log);
1760     } else {
1761       *eol = '\0';
1762       out->print_cr("%s", remaining_log);
1763       remaining_log = eol + 1;
1764     }
1765   }
1766 }
1767 
1768 void CompileBroker::post_compile(CompilerThread* thread, CompileTask* task, EventCompilation& event, bool success, ciEnv* ci_env) {
1769 
1770   if (success) {
1771     task->mark_success();
1772     if (ci_env != NULL) {
1773       task->set_num_inlined_bytecodes(ci_env->num_inlined_bytecodes());
1774     }
1775     if (_compilation_log != NULL) {
1776       nmethod* code = task->code();
1777       if (code != NULL) {
1778         _compilation_log->log_nmethod(thread, code);
1779       }
1780     }
1781   }
1782 
1783   // simulate crash during compilation
1784   assert(task->compile_id() != CICrashAt, "just as planned");
1785   if (event.should_commit()) {
1786     event.set_method(task->method());
1787     event.set_compileId(task->compile_id());
1788     event.set_compileLevel(task->comp_level());
1789     event.set_succeded(task->is_success());
1790     event.set_isOsr(task->osr_bci() != CompileBroker::standard_entry_bci);
1791     event.set_codeSize((task->code() == NULL) ? 0 : task->code()->total_size());
1792     event.set_inlinedBytes(task->num_inlined_bytecodes());
1793     event.commit();
1794   }
1795 }
1796 
1797 int DirectivesStack::_depth = 0;
1798 CompilerDirectives* DirectivesStack::_top = NULL;
1799 CompilerDirectives* DirectivesStack::_bottom = NULL;
1800 
1801 // ------------------------------------------------------------------
1802 // CompileBroker::invoke_compiler_on_method
1803 //
1804 // Compile a method.
1805 //
1806 void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
1807   task->print_ul();
1808   if (PrintCompilation) {
1809     ResourceMark rm;
1810     task->print_tty();
1811   }
1812   elapsedTimer time;
1813 
1814   CompilerThread* thread = CompilerThread::current();
1815   ResourceMark rm(thread);
1816 
1817   if (LogEvents) {
1818     _compilation_log->log_compile(thread, task);
1819   }
1820 
1821   // Common flags.
1822   uint compile_id = task->compile_id();
1823   int osr_bci = task->osr_bci();
1824   bool is_osr = (osr_bci != standard_entry_bci);
1825   bool should_log = (thread->log() != NULL);
1826   bool should_break = false;
1827   const int task_level = task->comp_level();
1828   AbstractCompiler* comp = task->compiler();
1829 
1830   DirectiveSet* directive;
1831   {
1832     // create the handle inside it's own block so it can't
1833     // accidentally be referenced once the thread transitions to
1834     // native.  The NoHandleMark before the transition should catch
1835     // any cases where this occurs in the future.
1836     methodHandle method(thread, task->method());
1837     assert(!method->is_native(), "no longer compile natives");
1838 
1839     // Look up matching directives
1840     directive = DirectivesStack::getMatchingDirective(method, comp);
1841 
1842     // Save information about this method in case of failure.
1843     set_last_compile(thread, method, is_osr, task_level);
1844 
1845     DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level));
1846   }
1847 
1848   should_break = directive->BreakAtExecuteOption || task->check_break_at_flags();
1849   if (should_log && !directive->LogOption) {
1850     should_log = false;
1851   }
1852 
1853   // Allocate a new set of JNI handles.
1854   push_jni_handle_block();
1855   Method* target_handle = task->method();
1856   int compilable = ciEnv::MethodCompilable;
1857   const char* failure_reason = NULL;
1858   const char* retry_message = NULL;
1859 
1860   int system_dictionary_modification_counter;
1861   {
1862     MutexLocker locker(Compile_lock, thread);
1863     system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1864   }
1865 
1866 #if INCLUDE_JVMCI
1867   if (UseJVMCICompiler && comp != NULL && comp->is_jvmci()) {
1868     JVMCICompiler* jvmci = (JVMCICompiler*) comp;
1869 
1870     TraceTime t1("compilation", &time);
1871     EventCompilation event;
1872 
1873     // Skip redefined methods
1874     if (target_handle->is_old()) {
1875         failure_reason = "redefined method";
1876         retry_message = "not retryable";
1877         compilable = ciEnv::MethodCompilable_never;
1878     } else {
1879         JVMCIEnv env(task, system_dictionary_modification_counter);
1880         methodHandle method(thread, target_handle);
1881         jvmci->compile_method(method, osr_bci, &env);
1882 
1883         failure_reason = env.failure_reason();
1884         if (!env.retryable()) {
1885           retry_message = "not retryable";
1886           compilable = ciEnv::MethodCompilable_not_at_tier;
1887         }
1888     }
1889     post_compile(thread, task, event, task->code() != NULL, NULL);
1890 
1891   } else
1892 #endif // INCLUDE_JVMCI
1893   {
1894     NoHandleMark  nhm;
1895     ThreadToNativeFromVM ttn(thread);
1896 
1897     ciEnv ci_env(task, system_dictionary_modification_counter);
1898     if (should_break) {
1899       ci_env.set_break_at_compile(true);
1900     }
1901     if (should_log) {
1902       ci_env.set_log(thread->log());
1903     }
1904     assert(thread->env() == &ci_env, "set by ci_env");
1905     // The thread-env() field is cleared in ~CompileTaskWrapper.
1906 
1907     // Cache Jvmti state
1908     ci_env.cache_jvmti_state();
1909 
1910     // Cache DTrace flags
1911     ci_env.cache_dtrace_flags();
1912 
1913     ciMethod* target = ci_env.get_method_from_handle(target_handle);
1914 
1915     TraceTime t1("compilation", &time);
1916     EventCompilation event;
1917 
1918     if (comp == NULL) {
1919       ci_env.record_method_not_compilable("no compiler", !TieredCompilation);
1920     } else {
1921       if (WhiteBoxAPI && WhiteBox::compilation_locked) {
1922         MonitorLockerEx locker(Compilation_lock, Mutex::_no_safepoint_check_flag);
1923         while (WhiteBox::compilation_locked) {
1924           locker.wait(Mutex::_no_safepoint_check_flag);
1925         }
1926       }
1927       comp->compile_method(&ci_env, target, osr_bci, directive);
1928     }
1929 
1930     if (!ci_env.failing() && task->code() == NULL) {
1931       //assert(false, "compiler should always document failure");
1932       // The compiler elected, without comment, not to register a result.
1933       // Do not attempt further compilations of this method.
1934       ci_env.record_method_not_compilable("compile failed", !TieredCompilation);
1935     }
1936 
1937     // Copy this bit to the enclosing block:
1938     compilable = ci_env.compilable();
1939 
1940     if (ci_env.failing()) {
1941       failure_reason = ci_env.failure_reason();
1942       retry_message = ci_env.retry_message();
1943       ci_env.report_failure(failure_reason);
1944     }
1945 
1946     post_compile(thread, task, event, !ci_env.failing(), &ci_env);
1947   }
1948   // Remove the JNI handle block after the ciEnv destructor has run in
1949   // the previous block.
1950   pop_jni_handle_block();
1951 
1952   if (failure_reason != NULL) {
1953     task->set_failure_reason(failure_reason);
1954     if (_compilation_log != NULL) {
1955       _compilation_log->log_failure(thread, task, failure_reason, retry_message);
1956     }
1957     if (PrintCompilation) {
1958       FormatBufferResource msg = retry_message != NULL ?
1959         FormatBufferResource("COMPILE SKIPPED: %s (%s)", failure_reason, retry_message) :
1960         FormatBufferResource("COMPILE SKIPPED: %s",      failure_reason);
1961       task->print(tty, msg);
1962     }
1963   }
1964 
1965   methodHandle method(thread, task->method());
1966 
1967   DTRACE_METHOD_COMPILE_END_PROBE(method, compiler_name(task_level), task->is_success());
1968 
1969   collect_statistics(thread, time, task);
1970 
1971   nmethod* nm = task->code();
1972   if (nm != NULL) {
1973     nm->maybe_print_nmethod(directive);
1974   }
1975   DirectivesStack::release(directive);
1976 
1977   if (PrintCompilation && PrintCompilation2) {
1978     tty->print("%7d ", (int) tty->time_stamp().milliseconds());  // print timestamp
1979     tty->print("%4d ", compile_id);    // print compilation number
1980     tty->print("%s ", (is_osr ? "%" : " "));
1981     if (task->code() != NULL) {
1982       tty->print("size: %d(%d) ", task->code()->total_size(), task->code()->insts_size());
1983     }
1984     tty->print_cr("time: %d inlined: %d bytes", (int)time.milliseconds(), task->num_inlined_bytecodes());
1985   }
1986 
1987   Log(compilation, codecache) log;
1988   if (log.is_debug()) {
1989     LogStream ls(log.debug());
1990     codecache_print(&ls, /* detailed= */ false);
1991   }
1992   if (PrintCodeCacheOnCompilation) {
1993     codecache_print(/* detailed= */ false);
1994   }
1995   // Disable compilation, if required.
1996   switch (compilable) {
1997   case ciEnv::MethodCompilable_never:
1998     if (is_osr)
1999       method->set_not_osr_compilable_quietly();
2000     else
2001       method->set_not_compilable_quietly();
2002     break;
2003   case ciEnv::MethodCompilable_not_at_tier:
2004     if (is_osr)
2005       method->set_not_osr_compilable_quietly(task_level);
2006     else
2007       method->set_not_compilable_quietly(task_level);
2008     break;
2009   }
2010 
2011   // Note that the queued_for_compilation bits are cleared without
2012   // protection of a mutex. [They were set by the requester thread,
2013   // when adding the task to the compile queue -- at which time the
2014   // compile queue lock was held. Subsequently, we acquired the compile
2015   // queue lock to get this task off the compile queue; thus (to belabour
2016   // the point somewhat) our clearing of the bits must be occurring
2017   // only after the setting of the bits. See also 14012000 above.
2018   method->clear_queued_for_compilation();
2019 
2020 #ifdef ASSERT
2021   if (CollectedHeap::fired_fake_oom()) {
2022     // The current compile received a fake OOM during compilation so
2023     // go ahead and exit the VM since the test apparently succeeded
2024     tty->print_cr("*** Shutting down VM after successful fake OOM");
2025     vm_exit(0);
2026   }
2027 #endif
2028 }
2029 
2030 /**
2031  * The CodeCache is full. Print warning and disable compilation.
2032  * Schedule code cache cleaning so compilation can continue later.
2033  * This function needs to be called only from CodeCache::allocate(),
2034  * since we currently handle a full code cache uniformly.
2035  */
2036 void CompileBroker::handle_full_code_cache(int code_blob_type) {
2037   UseInterpreter = true;
2038   if (UseCompiler || AlwaysCompileLoopMethods ) {
2039     if (xtty != NULL) {
2040       ResourceMark rm;
2041       stringStream s;
2042       // Dump code cache state into a buffer before locking the tty,
2043       // because log_state() will use locks causing lock conflicts.
2044       CodeCache::log_state(&s);
2045       // Lock to prevent tearing
2046       ttyLocker ttyl;
2047       xtty->begin_elem("code_cache_full");
2048       xtty->print("%s", s.as_string());
2049       xtty->stamp();
2050       xtty->end_elem();
2051     }
2052 
2053 #ifndef PRODUCT
2054     if (CompileTheWorld || ExitOnFullCodeCache) {
2055       codecache_print(/* detailed= */ true);
2056       before_exit(JavaThread::current());
2057       exit_globals(); // will delete tty
2058       vm_direct_exit(CompileTheWorld ? 0 : 1);
2059     }
2060 #endif
2061     if (UseCodeCacheFlushing) {
2062       // Since code cache is full, immediately stop new compiles
2063       if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) {
2064         NMethodSweeper::log_sweep("disable_compiler");
2065       }
2066     } else {
2067       disable_compilation_forever();
2068     }
2069 
2070     CodeCache::report_codemem_full(code_blob_type, should_print_compiler_warning());
2071   }
2072 }
2073 
2074 // ------------------------------------------------------------------
2075 // CompileBroker::set_last_compile
2076 //
2077 // Record this compilation for debugging purposes.
2078 void CompileBroker::set_last_compile(CompilerThread* thread, const methodHandle& method, bool is_osr, int comp_level) {
2079   ResourceMark rm;
2080   char* method_name = method->name()->as_C_string();
2081   strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length);
2082   _last_method_compiled[CompileBroker::name_buffer_length - 1] = '\0'; // ensure null terminated
2083   char current_method[CompilerCounters::cmname_buffer_length];
2084   size_t maxLen = CompilerCounters::cmname_buffer_length;
2085 
2086   if (UsePerfData) {
2087     const char* class_name = method->method_holder()->name()->as_C_string();
2088 
2089     size_t s1len = strlen(class_name);
2090     size_t s2len = strlen(method_name);
2091 
2092     // check if we need to truncate the string
2093     if (s1len + s2len + 2 > maxLen) {
2094 
2095       // the strategy is to lop off the leading characters of the
2096       // class name and the trailing characters of the method name.
2097 
2098       if (s2len + 2 > maxLen) {
2099         // lop of the entire class name string, let snprintf handle
2100         // truncation of the method name.
2101         class_name += s1len; // null string
2102       }
2103       else {
2104         // lop off the extra characters from the front of the class name
2105         class_name += ((s1len + s2len + 2) - maxLen);
2106       }
2107     }
2108 
2109     jio_snprintf(current_method, maxLen, "%s %s", class_name, method_name);
2110   }
2111 
2112   if (CICountOSR && is_osr) {
2113     _last_compile_type = osr_compile;
2114   } else {
2115     _last_compile_type = normal_compile;
2116   }
2117   _last_compile_level = comp_level;
2118 
2119   if (UsePerfData) {
2120     CompilerCounters* counters = thread->counters();
2121     counters->set_current_method(current_method);
2122     counters->set_compile_type((jlong)_last_compile_type);
2123   }
2124 }
2125 
2126 
2127 // ------------------------------------------------------------------
2128 // CompileBroker::push_jni_handle_block
2129 //
2130 // Push on a new block of JNI handles.
2131 void CompileBroker::push_jni_handle_block() {
2132   JavaThread* thread = JavaThread::current();
2133 
2134   // Allocate a new block for JNI handles.
2135   // Inlined code from jni_PushLocalFrame()
2136   JNIHandleBlock* java_handles = thread->active_handles();
2137   JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);
2138   assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");
2139   compile_handles->set_pop_frame_link(java_handles);  // make sure java handles get gc'd.
2140   thread->set_active_handles(compile_handles);
2141 }
2142 
2143 
2144 // ------------------------------------------------------------------
2145 // CompileBroker::pop_jni_handle_block
2146 //
2147 // Pop off the current block of JNI handles.
2148 void CompileBroker::pop_jni_handle_block() {
2149   JavaThread* thread = JavaThread::current();
2150 
2151   // Release our JNI handle block
2152   JNIHandleBlock* compile_handles = thread->active_handles();
2153   JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
2154   thread->set_active_handles(java_handles);
2155   compile_handles->set_pop_frame_link(NULL);
2156   JNIHandleBlock::release_block(compile_handles, thread); // may block
2157 }
2158 
2159 // ------------------------------------------------------------------
2160 // CompileBroker::collect_statistics
2161 //
2162 // Collect statistics about the compilation.
2163 
2164 void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) {
2165   bool success = task->is_success();
2166   methodHandle method (thread, task->method());
2167   uint compile_id = task->compile_id();
2168   bool is_osr = (task->osr_bci() != standard_entry_bci);
2169   nmethod* code = task->code();
2170   CompilerCounters* counters = thread->counters();
2171 
2172   assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker");
2173   MutexLocker locker(CompileStatistics_lock);
2174 
2175   // _perf variables are production performance counters which are
2176   // updated regardless of the setting of the CITime and CITimeEach flags
2177   //
2178 
2179   // account all time, including bailouts and failures in this counter;
2180   // C1 and C2 counters are counting both successful and unsuccessful compiles
2181   _t_total_compilation.add(time);
2182 
2183   if (!success) {
2184     _total_bailout_count++;
2185     if (UsePerfData) {
2186       _perf_last_failed_method->set_value(counters->current_method());
2187       _perf_last_failed_type->set_value(counters->compile_type());
2188       _perf_total_bailout_count->inc();
2189     }
2190     _t_bailedout_compilation.add(time);
2191   } else if (code == NULL) {
2192     if (UsePerfData) {
2193       _perf_last_invalidated_method->set_value(counters->current_method());
2194       _perf_last_invalidated_type->set_value(counters->compile_type());
2195       _perf_total_invalidated_count->inc();
2196     }
2197     _total_invalidated_count++;
2198     _t_invalidated_compilation.add(time);
2199   } else {
2200     // Compilation succeeded
2201 
2202     // update compilation ticks - used by the implementation of
2203     // java.lang.management.CompilationMBean
2204     _perf_total_compilation->inc(time.ticks());
2205     _peak_compilation_time = time.milliseconds() > _peak_compilation_time ? time.milliseconds() : _peak_compilation_time;
2206 
2207     if (CITime) {
2208       int bytes_compiled = method->code_size() + task->num_inlined_bytecodes();
2209       if (is_osr) {
2210         _t_osr_compilation.add(time);
2211         _sum_osr_bytes_compiled += bytes_compiled;
2212       } else {
2213         _t_standard_compilation.add(time);
2214         _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();
2215       }
2216 
2217 #if INCLUDE_JVMCI
2218       AbstractCompiler* comp = compiler(task->comp_level());
2219       if (comp) {
2220         CompilerStatistics* stats = comp->stats();
2221         if (stats) {
2222           if (is_osr) {
2223             stats->_osr.update(time, bytes_compiled);
2224           } else {
2225             stats->_standard.update(time, bytes_compiled);
2226           }
2227           stats->_nmethods_size += code->total_size();
2228           stats->_nmethods_code_size += code->insts_size();
2229         } else { // if (!stats)
2230           assert(false, "Compiler statistics object must exist");
2231         }
2232       } else { // if (!comp)
2233         assert(false, "Compiler object must exist");
2234       }
2235 #endif // INCLUDE_JVMCI
2236     }
2237 
2238     if (UsePerfData) {
2239       // save the name of the last method compiled
2240       _perf_last_method->set_value(counters->current_method());
2241       _perf_last_compile_type->set_value(counters->compile_type());
2242       _perf_last_compile_size->set_value(method->code_size() +
2243                                          task->num_inlined_bytecodes());
2244       if (is_osr) {
2245         _perf_osr_compilation->inc(time.ticks());
2246         _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
2247       } else {
2248         _perf_standard_compilation->inc(time.ticks());
2249         _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
2250       }
2251     }
2252 
2253     if (CITimeEach) {
2254       float bytes_per_sec = 1.0 * (method->code_size() + task->num_inlined_bytecodes()) / time.seconds();
2255       tty->print_cr("%3d   seconds: %f bytes/sec : %f (bytes %d + %d inlined)",
2256                     compile_id, time.seconds(), bytes_per_sec, method->code_size(), task->num_inlined_bytecodes());
2257     }
2258 
2259     // Collect counts of successful compilations
2260     _sum_nmethod_size      += code->total_size();
2261     _sum_nmethod_code_size += code->insts_size();
2262     _total_compile_count++;
2263 
2264     if (UsePerfData) {
2265       _perf_sum_nmethod_size->inc(     code->total_size());
2266       _perf_sum_nmethod_code_size->inc(code->insts_size());
2267       _perf_total_compile_count->inc();
2268     }
2269 
2270     if (is_osr) {
2271       if (UsePerfData) _perf_total_osr_compile_count->inc();
2272       _total_osr_compile_count++;
2273     } else {
2274       if (UsePerfData) _perf_total_standard_compile_count->inc();
2275       _total_standard_compile_count++;
2276     }
2277   }
2278   // set the current method for the thread to null
2279   if (UsePerfData) counters->set_current_method("");
2280 }
2281 
2282 const char* CompileBroker::compiler_name(int comp_level) {
2283   AbstractCompiler *comp = CompileBroker::compiler(comp_level);
2284   if (comp == NULL) {
2285     return "no compiler";
2286   } else {
2287     return (comp->name());
2288   }
2289 }
2290 
2291 #if INCLUDE_JVMCI
2292 void CompileBroker::print_times(AbstractCompiler* comp) {
2293   CompilerStatistics* stats = comp->stats();
2294   if (stats) {
2295     tty->print_cr("  %s {speed: %d bytes/s; standard: %6.3f s, %d bytes, %d methods; osr: %6.3f s, %d bytes, %d methods; nmethods_size: %d bytes; nmethods_code_size: %d bytes}",
2296                 comp->name(), stats->bytes_per_second(),
2297                 stats->_standard._time.seconds(), stats->_standard._bytes, stats->_standard._count,
2298                 stats->_osr._time.seconds(), stats->_osr._bytes, stats->_osr._count,
2299                 stats->_nmethods_size, stats->_nmethods_code_size);
2300   } else { // if (!stats)
2301     assert(false, "Compiler statistics object must exist");
2302   }
2303   comp->print_timers();
2304 }
2305 #endif // INCLUDE_JVMCI
2306 
2307 void CompileBroker::print_times(bool per_compiler, bool aggregate) {
2308 #if INCLUDE_JVMCI
2309   elapsedTimer standard_compilation;
2310   elapsedTimer total_compilation;
2311   elapsedTimer osr_compilation;
2312 
2313   int standard_bytes_compiled = 0;
2314   int osr_bytes_compiled = 0;
2315 
2316   int standard_compile_count = 0;
2317   int osr_compile_count = 0;
2318   int total_compile_count = 0;
2319 
2320   int nmethods_size = 0;
2321   int nmethods_code_size = 0;
2322   bool printedHeader = false;
2323 
2324   for (unsigned int i = 0; i < sizeof(_compilers) / sizeof(AbstractCompiler*); i++) {
2325     AbstractCompiler* comp = _compilers[i];
2326     if (comp != NULL) {
2327       if (per_compiler && aggregate && !printedHeader) {
2328         printedHeader = true;
2329         tty->cr();
2330         tty->print_cr("Individual compiler times (for compiled methods only)");
2331         tty->print_cr("------------------------------------------------");
2332         tty->cr();
2333       }
2334       CompilerStatistics* stats = comp->stats();
2335 
2336       if (stats) {
2337         standard_compilation.add(stats->_standard._time);
2338         osr_compilation.add(stats->_osr._time);
2339 
2340         standard_bytes_compiled += stats->_standard._bytes;
2341         osr_bytes_compiled += stats->_osr._bytes;
2342 
2343         standard_compile_count += stats->_standard._count;
2344         osr_compile_count += stats->_osr._count;
2345 
2346         nmethods_size += stats->_nmethods_size;
2347         nmethods_code_size += stats->_nmethods_code_size;
2348       } else { // if (!stats)
2349         assert(false, "Compiler statistics object must exist");
2350       }
2351 
2352       if (per_compiler) {
2353         print_times(comp);
2354       }
2355     }
2356   }
2357   total_compile_count = osr_compile_count + standard_compile_count;
2358   total_compilation.add(osr_compilation);
2359   total_compilation.add(standard_compilation);
2360 
2361   // In hosted mode, print the JVMCI compiler specific counters manually.
2362   if (!UseJVMCICompiler) {
2363     JVMCICompiler::print_compilation_timers();
2364   }
2365 #else // INCLUDE_JVMCI
2366   elapsedTimer standard_compilation = CompileBroker::_t_standard_compilation;
2367   elapsedTimer osr_compilation = CompileBroker::_t_osr_compilation;
2368   elapsedTimer total_compilation = CompileBroker::_t_total_compilation;
2369 
2370   int standard_bytes_compiled = CompileBroker::_sum_standard_bytes_compiled;
2371   int osr_bytes_compiled = CompileBroker::_sum_osr_bytes_compiled;
2372 
2373   int standard_compile_count = CompileBroker::_total_standard_compile_count;
2374   int osr_compile_count = CompileBroker::_total_osr_compile_count;
2375   int total_compile_count = CompileBroker::_total_compile_count;
2376 
2377   int nmethods_size = CompileBroker::_sum_nmethod_code_size;
2378   int nmethods_code_size = CompileBroker::_sum_nmethod_size;
2379 #endif // INCLUDE_JVMCI
2380 
2381   if (!aggregate) {
2382     return;
2383   }
2384   tty->cr();
2385   tty->print_cr("Accumulated compiler times");
2386   tty->print_cr("----------------------------------------------------------");
2387                //0000000000111111111122222222223333333333444444444455555555556666666666
2388                //0123456789012345678901234567890123456789012345678901234567890123456789
2389   tty->print_cr("  Total compilation time   : %7.3f s", total_compilation.seconds());
2390   tty->print_cr("    Standard compilation   : %7.3f s, Average : %2.3f s",
2391                 standard_compilation.seconds(),
2392                 standard_compilation.seconds() / standard_compile_count);
2393   tty->print_cr("    Bailed out compilation : %7.3f s, Average : %2.3f s",
2394                 CompileBroker::_t_bailedout_compilation.seconds(),
2395                 CompileBroker::_t_bailedout_compilation.seconds() / CompileBroker::_total_bailout_count);
2396   tty->print_cr("    On stack replacement   : %7.3f s, Average : %2.3f s",
2397                 osr_compilation.seconds(),
2398                 osr_compilation.seconds() / osr_compile_count);
2399   tty->print_cr("    Invalidated            : %7.3f s, Average : %2.3f s",
2400                 CompileBroker::_t_invalidated_compilation.seconds(),
2401                 CompileBroker::_t_invalidated_compilation.seconds() / CompileBroker::_total_invalidated_count);
2402 
2403   AbstractCompiler *comp = compiler(CompLevel_simple);
2404   if (comp != NULL) {
2405     tty->cr();
2406     comp->print_timers();
2407   }
2408   comp = compiler(CompLevel_full_optimization);
2409   if (comp != NULL) {
2410     tty->cr();
2411     comp->print_timers();
2412   }
2413   tty->cr();
2414   tty->print_cr("  Total compiled methods    : %8d methods", total_compile_count);
2415   tty->print_cr("    Standard compilation    : %8d methods", standard_compile_count);
2416   tty->print_cr("    On stack replacement    : %8d methods", osr_compile_count);
2417   int tcb = osr_bytes_compiled + standard_bytes_compiled;
2418   tty->print_cr("  Total compiled bytecodes  : %8d bytes", tcb);
2419   tty->print_cr("    Standard compilation    : %8d bytes", standard_bytes_compiled);
2420   tty->print_cr("    On stack replacement    : %8d bytes", osr_bytes_compiled);
2421   double tcs = total_compilation.seconds();
2422   int bps = tcs == 0.0 ? 0 : (int)(tcb / tcs);
2423   tty->print_cr("  Average compilation speed : %8d bytes/s", bps);
2424   tty->cr();
2425   tty->print_cr("  nmethod code size         : %8d bytes", nmethods_code_size);
2426   tty->print_cr("  nmethod total size        : %8d bytes", nmethods_size);
2427 }
2428 
2429 // Debugging output for failure
2430 void CompileBroker::print_last_compile() {
2431   if (_last_compile_level != CompLevel_none &&
2432       compiler(_last_compile_level) != NULL &&
2433       _last_compile_type != no_compile) {
2434     if (_last_compile_type == osr_compile) {
2435       tty->print_cr("Last parse:  [osr]%d+++(%d) %s",
2436                     _osr_compilation_id, _last_compile_level, _last_method_compiled);
2437     } else {
2438       tty->print_cr("Last parse:  %d+++(%d) %s",
2439                     _compilation_id, _last_compile_level, _last_method_compiled);
2440     }
2441   }
2442 }