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