compileBroker.cpp 86.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33
#include "precompiled.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "code/codeCache.hpp"
#include "compiler/compileBroker.hpp"
#include "compiler/compileLog.hpp"
#include "compiler/compilerOracle.hpp"
#include "interpreter/linkResolver.hpp"
#include "memory/allocation.inline.hpp"
34 35
#include "oops/methodData.hpp"
#include "oops/method.hpp"
36 37 38 39 40 41 42 43 44 45
#include "oops/oop.inline.hpp"
#include "prims/nativeLookup.hpp"
#include "runtime/arguments.hpp"
#include "runtime/compilationPolicy.hpp"
#include "runtime/init.hpp"
#include "runtime/interfaceSupport.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/os.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/sweeper.hpp"
S
sla 已提交
46
#include "trace/tracing.hpp"
47
#include "utilities/dtrace.hpp"
48
#include "utilities/events.hpp"
49 50 51 52 53 54 55 56 57
#ifdef COMPILER1
#include "c1/c1_Compiler.hpp"
#endif
#ifdef COMPILER2
#include "opto/c2compiler.hpp"
#endif
#ifdef SHARK
#include "shark/sharkCompiler.hpp"
#endif
D
duke 已提交
58 59 60 61 62

#ifdef DTRACE_ENABLED

// Only bother with this argument setup if dtrace is available

D
dcubed 已提交
63
#ifndef USDT2
D
duke 已提交
64 65 66 67 68
HS_DTRACE_PROBE_DECL8(hotspot, method__compile__begin,
  char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t);
HS_DTRACE_PROBE_DECL9(hotspot, method__compile__end,
  char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t, bool);

69
#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)             \
D
duke 已提交
70
  {                                                                      \
71 72 73
    Symbol* klass_name = (method)->klass_name();                         \
    Symbol* name = (method)->name();                                     \
    Symbol* signature = (method)->signature();                           \
D
duke 已提交
74 75 76 77 78 79 80
    HS_DTRACE_PROBE8(hotspot, method__compile__begin,                    \
      comp_name, strlen(comp_name),                                      \
      klass_name->bytes(), klass_name->utf8_length(),                    \
      name->bytes(), name->utf8_length(),                                \
      signature->bytes(), signature->utf8_length());                     \
  }

81
#define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)      \
D
duke 已提交
82
  {                                                                      \
83 84 85
    Symbol* klass_name = (method)->klass_name();                         \
    Symbol* name = (method)->name();                                     \
    Symbol* signature = (method)->signature();                           \
D
duke 已提交
86 87 88 89 90 91 92
    HS_DTRACE_PROBE9(hotspot, method__compile__end,                      \
      comp_name, strlen(comp_name),                                      \
      klass_name->bytes(), klass_name->utf8_length(),                    \
      name->bytes(), name->utf8_length(),                                \
      signature->bytes(), signature->utf8_length(), (success));          \
  }

D
dcubed 已提交
93 94
#else /* USDT2 */

95
#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)             \
D
dcubed 已提交
96 97 98 99
  {                                                                      \
    Symbol* klass_name = (method)->klass_name();                         \
    Symbol* name = (method)->name();                                     \
    Symbol* signature = (method)->signature();                           \
100
    HOTSPOT_METHOD_COMPILE_BEGIN(                                        \
D
dcubed 已提交
101
      comp_name, strlen(comp_name),                                      \
102
      (char *) klass_name->bytes(), klass_name->utf8_length(),           \
D
dcubed 已提交
103 104 105 106
      (char *) name->bytes(), name->utf8_length(),                       \
      (char *) signature->bytes(), signature->utf8_length());            \
  }

107
#define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)      \
D
dcubed 已提交
108 109 110 111 112 113 114 115 116 117 118 119
  {                                                                      \
    Symbol* klass_name = (method)->klass_name();                         \
    Symbol* name = (method)->name();                                     \
    Symbol* signature = (method)->signature();                           \
    HOTSPOT_METHOD_COMPILE_END(                                          \
      comp_name, strlen(comp_name),                                      \
      (char *) klass_name->bytes(), klass_name->utf8_length(),           \
      (char *) name->bytes(), name->utf8_length(),                       \
      (char *) signature->bytes(), signature->utf8_length(), (success)); \
  }
#endif /* USDT2 */

D
duke 已提交
120 121
#else //  ndef DTRACE_ENABLED

122 123
#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
#define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
D
duke 已提交
124 125 126 127 128

#endif // ndef DTRACE_ENABLED

bool CompileBroker::_initialized = false;
volatile bool CompileBroker::_should_block = false;
129
volatile jint CompileBroker::_print_compilation_warning = 0;
130
volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
D
duke 已提交
131 132 133 134

// The installed compiler(s)
AbstractCompiler* CompileBroker::_compilers[2];

A
anoll 已提交
135 136 137
// These counters are used to assign an unique ID to each compilation.
volatile jint CompileBroker::_compilation_id     = 0;
volatile jint CompileBroker::_osr_compilation_id = 0;
D
duke 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

// Debugging information
int  CompileBroker::_last_compile_type     = no_compile;
int  CompileBroker::_last_compile_level    = CompLevel_none;
char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];

// Performance counters
PerfCounter* CompileBroker::_perf_total_compilation = NULL;
PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
PerfCounter* CompileBroker::_perf_standard_compilation = NULL;

PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;

PerfCounter* CompileBroker::_perf_sum_osr_bytes_compiled = NULL;
PerfCounter* CompileBroker::_perf_sum_standard_bytes_compiled = NULL;
PerfCounter* CompileBroker::_perf_sum_nmethod_size = NULL;
PerfCounter* CompileBroker::_perf_sum_nmethod_code_size = NULL;

PerfStringVariable* CompileBroker::_perf_last_method = NULL;
PerfStringVariable* CompileBroker::_perf_last_failed_method = NULL;
PerfStringVariable* CompileBroker::_perf_last_invalidated_method = NULL;
PerfVariable*       CompileBroker::_perf_last_compile_type = NULL;
PerfVariable*       CompileBroker::_perf_last_compile_size = NULL;
PerfVariable*       CompileBroker::_perf_last_failed_type = NULL;
PerfVariable*       CompileBroker::_perf_last_invalidated_type = NULL;

// Timers and counters for generating statistics
elapsedTimer CompileBroker::_t_total_compilation;
elapsedTimer CompileBroker::_t_osr_compilation;
elapsedTimer CompileBroker::_t_standard_compilation;

int CompileBroker::_total_bailout_count          = 0;
int CompileBroker::_total_invalidated_count      = 0;
int CompileBroker::_total_compile_count          = 0;
int CompileBroker::_total_osr_compile_count      = 0;
int CompileBroker::_total_standard_compile_count = 0;

int CompileBroker::_sum_osr_bytes_compiled       = 0;
int CompileBroker::_sum_standard_bytes_compiled  = 0;
int CompileBroker::_sum_nmethod_size             = 0;
int CompileBroker::_sum_nmethod_code_size        = 0;

S
sla 已提交
184 185
long CompileBroker::_peak_compilation_time       = 0;

186 187
CompileQueue* CompileBroker::_c2_compile_queue   = NULL;
CompileQueue* CompileBroker::_c1_compile_queue   = NULL;
D
duke 已提交
188

189
GrowableArray<CompilerThread*>* CompileBroker::_compiler_threads = NULL;
D
duke 已提交
190 191


192 193 194 195 196 197 198
class CompilationLog : public StringEventLog {
 public:
  CompilationLog() : StringEventLog("Compilation events") {
  }

  void log_compile(JavaThread* thread, CompileTask* task) {
    StringLogMessage lm;
199
    stringStream sstr = lm.stream();
200
    // msg.time_stamp().update_to(tty->time_stamp().ticks());
201
    task->print_compilation(&sstr, NULL, true);
202 203 204 205
    log(thread, "%s", (const char*)lm);
  }

  void log_nmethod(JavaThread* thread, nmethod* nm) {
206
    log(thread, "nmethod %d%s " INTPTR_FORMAT " code [" INTPTR_FORMAT ", " INTPTR_FORMAT "]",
N
never 已提交
207
        nm->compile_id(), nm->is_osr_method() ? "%" : "",
208
        p2i(nm), p2i(nm->code_begin()), p2i(nm->code_end()));
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  }

  void log_failure(JavaThread* thread, CompileTask* task, const char* reason, const char* retry_message) {
    StringLogMessage lm;
    lm.print("%4d   COMPILE SKIPPED: %s", task->compile_id(), reason);
    if (retry_message != NULL) {
      lm.append(" (%s)", retry_message);
    }
    lm.print("\n");
    log(thread, "%s", (const char*)lm);
  }
};

static CompilationLog* _compilation_log = NULL;

void compileBroker_init() {
  if (LogEvents) {
    _compilation_log = new CompilationLog();
  }
}

D
duke 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243
CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) {
  CompilerThread* thread = CompilerThread::current();
  thread->set_task(task);
  CompileLog*     log  = thread->log();
  if (log != NULL)  task->log_task_start(log);
}

CompileTaskWrapper::~CompileTaskWrapper() {
  CompilerThread* thread = CompilerThread::current();
  CompileTask* task = thread->task();
  CompileLog*  log  = thread->log();
  if (log != NULL)  task->log_task_done(log);
  thread->set_task(NULL);
  task->set_code_handle(NULL);
244
  thread->set_env(NULL);
D
duke 已提交
245 246 247 248 249 250 251 252 253 254
  if (task->is_blocking()) {
    MutexLocker notifier(task->lock(), thread);
    task->mark_complete();
    // Notify the waiting thread that the compilation has completed.
    task->lock()->notify_all();
  } else {
    task->mark_complete();

    // By convention, the compiling thread is responsible for
    // recycling a non-blocking CompileTask.
255
    CompileTask::free(task);
D
duke 已提交
256 257 258 259
  }
}


260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
CompileTask*  CompileTask::_task_free_list = NULL;
#ifdef ASSERT
int CompileTask::_num_allocated_tasks = 0;
#endif
/**
 * Allocate a CompileTask, from the free list if possible.
 */
CompileTask* CompileTask::allocate() {
  MutexLocker locker(CompileTaskAlloc_lock);
  CompileTask* task = NULL;

  if (_task_free_list != NULL) {
    task = _task_free_list;
    _task_free_list = task->next();
    task->set_next(NULL);
  } else {
    task = new CompileTask();
    DEBUG_ONLY(_num_allocated_tasks++;)
    assert (_num_allocated_tasks < 10000, "Leaking compilation tasks?");
    task->set_next(NULL);
    task->set_is_free(true);
  }
  assert(task->is_free(), "Task must be free.");
  task->set_is_free(false);
  return task;
}


/**
 * Add a task to the free list.
 */
void CompileTask::free(CompileTask* task) {
  MutexLocker locker(CompileTaskAlloc_lock);
  if (!task->is_free()) {
    task->set_code(NULL);
    assert(!task->lock()->is_locked(), "Should not be locked when freed");
    JNIHandles::destroy_global(task->_method_holder);
    JNIHandles::destroy_global(task->_hot_method_holder);

    task->set_is_free(true);
    task->set_next(_task_free_list);
    _task_free_list = task;
  }
}

D
duke 已提交
305 306 307 308 309 310 311 312 313 314 315
void CompileTask::initialize(int compile_id,
                             methodHandle method,
                             int osr_bci,
                             int comp_level,
                             methodHandle hot_method,
                             int hot_count,
                             const char* comment,
                             bool is_blocking) {
  assert(!_lock->is_locked(), "bad locking");

  _compile_id = compile_id;
316
  _method = method();
317
  _method_holder = JNIHandles::make_global(method->method_holder()->klass_holder());
D
duke 已提交
318 319 320 321 322 323 324 325 326 327
  _osr_bci = osr_bci;
  _is_blocking = is_blocking;
  _comp_level = comp_level;
  _num_inlined_bytecodes = 0;

  _is_complete = false;
  _is_success = false;
  _code_handle = NULL;

  _hot_method = NULL;
328
  _hot_method_holder = NULL;
D
duke 已提交
329 330 331
  _hot_count = hot_count;
  _time_queued = 0;  // tidy
  _comment = comment;
332
  _failure_reason = NULL;
D
duke 已提交
333 334 335 336 337 338 339

  if (LogCompilation) {
    _time_queued = os::elapsed_counter();
    if (hot_method.not_null()) {
      if (hot_method == method) {
        _hot_method = _method;
      } else {
340
        _hot_method = hot_method();
341
        // only add loader or mirror if different from _method_holder
342
        _hot_method_holder = JNIHandles::make_global(hot_method->method_holder()->klass_holder());
D
duke 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
      }
    }
  }

  _next = NULL;
}

// ------------------------------------------------------------------
// CompileTask::code/set_code
nmethod* CompileTask::code() const {
  if (_code_handle == NULL)  return NULL;
  return _code_handle->code();
}
void CompileTask::set_code(nmethod* nm) {
  if (_code_handle == NULL && nm == NULL)  return;
  guarantee(_code_handle != NULL, "");
  _code_handle->set_code(nm);
  if (nm == NULL)  _code_handle = NULL;  // drop the handle also
}


364 365 366 367 368 369 370 371
void CompileTask::mark_on_stack() {
  // Mark these methods as something redefine classes cannot remove.
  _method->set_on_stack(true);
  if (_hot_method != NULL) {
    _hot_method->set_on_stack(true);
  }
}

D
duke 已提交
372 373 374 375 376
// ------------------------------------------------------------------
// CompileTask::print
void CompileTask::print() {
  tty->print("<CompileTask compile_id=%d ", _compile_id);
  tty->print("method=");
377
  _method->print_name(tty);
D
duke 已提交
378 379 380 381 382
  tty->print_cr(" osr_bci=%d is_blocking=%s is_complete=%s is_success=%s>",
             _osr_bci, bool_to_str(_is_blocking),
             bool_to_str(_is_complete), bool_to_str(_is_success));
}

I
iveresov 已提交
383

D
duke 已提交
384 385 386 387 388 389 390 391 392 393 394 395
// ------------------------------------------------------------------
// CompileTask::print_line_on_error
//
// This function is called by fatal error handler when the thread
// causing troubles is a compiler thread.
//
// Do not grab any lock, do not allocate memory.
//
// Otherwise it's the same as CompileTask::print_line()
//
void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) {
  // print compiler name
396
  st->print("%s:", CompileBroker::compiler_name(comp_level()));
397
  print_compilation(st);
D
duke 已提交
398 399 400 401 402 403 404
}

// ------------------------------------------------------------------
// CompileTask::print_line
void CompileTask::print_line() {
  ttyLocker ttyl;  // keep the following output all in one block
  // print compiler name if requested
405
  if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler_name(comp_level()));
406 407 408 409 410 411
  print_compilation();
}


// ------------------------------------------------------------------
// CompileTask::print_compilation_impl
412
void CompileTask::print_compilation_impl(outputStream* st, Method* method, int compile_id, int comp_level,
413 414 415 416 417
                                         bool is_osr_method, int osr_bci, bool is_blocking,
                                         const char* msg, bool short_form) {
  if (!short_form) {
    st->print("%7d ", (int) st->time_stamp().milliseconds());  // print timestamp
  }
418 419
  st->print("%4d ", compile_id);    // print compilation number

420 421 422 423 424 425 426 427 428 429 430
  // For unloaded methods the transition to zombie occurs after the
  // method is cleared so it's impossible to report accurate
  // information for that case.
  bool is_synchronized = false;
  bool has_exception_handler = false;
  bool is_native = false;
  if (method != NULL) {
    is_synchronized       = method->is_synchronized();
    has_exception_handler = method->has_exception_handler();
    is_native             = method->is_native();
  }
431 432
  // method attributes
  const char compile_type   = is_osr_method                   ? '%' : ' ';
433 434
  const char sync_char      = is_synchronized                 ? 's' : ' ';
  const char exception_char = has_exception_handler           ? '!' : ' ';
435
  const char blocking_char  = is_blocking                     ? 'b' : ' ';
436
  const char native_char    = is_native                       ? 'n' : ' ';
437 438 439 440 441 442 443 444 445 446

  // print method attributes
  st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char);

  if (TieredCompilation) {
    if (comp_level != -1)  st->print("%d ", comp_level);
    else                   st->print("- ");
  }
  st->print("     ");  // more indent

447 448 449 450 451 452 453
  if (method == NULL) {
    st->print("(method)");
  } else {
    method->print_short_name(st);
    if (is_osr_method) {
      st->print(" @ %d", osr_bci);
    }
454 455 456 457
    if (method->is_native())
      st->print(" (native)");
    else
      st->print(" (%d bytes)", method->code_size());
458 459 460 461 462
  }

  if (msg != NULL) {
    st->print("   %s", msg);
  }
463 464 465
  if (!short_form) {
    st->cr();
  }
466 467 468 469 470 471 472 473 474 475 476
}

// ------------------------------------------------------------------
// CompileTask::print_inlining
void CompileTask::print_inlining(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) {
  //         1234567
  st->print("        ");     // print timestamp
  //         1234
  st->print("     ");        // print compilation number

  // method attributes
477 478 479 480
  if (method->is_loaded()) {
    const char sync_char      = method->is_synchronized()        ? 's' : ' ';
    const char exception_char = method->has_exception_handlers() ? '!' : ' ';
    const char monitors_char  = method->has_monitor_bytecodes()  ? 'm' : ' ';
481

482 483 484 485 486 487
    // print method attributes
    st->print(" %c%c%c  ", sync_char, exception_char, monitors_char);
  } else {
    //         %s!bn
    st->print("      ");     // print method attributes
  }
488 489 490 491 492 493 494 495 496 497 498

  if (TieredCompilation) {
    st->print("  ");
  }
  st->print("     ");        // more indent
  st->print("    ");         // initial inlining indent

  for (int i = 0; i < inline_level; i++)  st->print("  ");

  st->print("@ %d  ", bci);  // print bci
  method->print_short_name(st);
499 500 501 502
  if (method->is_loaded())
    st->print(" (%d bytes)", method->code_size());
  else
    st->print(" (not loaded)");
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

  if (msg != NULL) {
    st->print("   %s", msg);
  }
  st->cr();
}

// ------------------------------------------------------------------
// CompileTask::print_inline_indent
void CompileTask::print_inline_indent(int inline_level, outputStream* st) {
  //         1234567
  st->print("        ");     // print timestamp
  //         1234
  st->print("     ");        // print compilation number
  //         %s!bn
  st->print("      ");       // print method attributes
  if (TieredCompilation) {
    st->print("  ");
  }
  st->print("     ");        // more indent
  st->print("    ");         // initial inlining indent
  for (int i = 0; i < inline_level; i++)  st->print("  ");
D
duke 已提交
525 526
}

527 528
// ------------------------------------------------------------------
// CompileTask::print_compilation
529
void CompileTask::print_compilation(outputStream* st, const char* msg, bool short_form) {
530
  bool is_osr_method = osr_bci() != InvocationEntryBci;
531
  print_compilation_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form);
532
}
D
duke 已提交
533 534 535 536 537

// ------------------------------------------------------------------
// CompileTask::log_task
void CompileTask::log_task(xmlStream* log) {
  Thread* thread = Thread::current();
538
  methodHandle method(thread, this->method());
D
duke 已提交
539 540 541
  ResourceMark rm(thread);

  // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'>
542
  log->print(" compile_id='%d'", _compile_id);
D
duke 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  if (_osr_bci != CompileBroker::standard_entry_bci) {
    log->print(" compile_kind='osr'");  // same as nmethod::compile_kind
  } // else compile_kind='c2c'
  if (!method.is_null())  log->method(method);
  if (_osr_bci != CompileBroker::standard_entry_bci) {
    log->print(" osr_bci='%d'", _osr_bci);
  }
  if (_comp_level != CompLevel_highest_tier) {
    log->print(" level='%d'", _comp_level);
  }
  if (_is_blocking) {
    log->print(" blocking='1'");
  }
  log->stamp();
}


// ------------------------------------------------------------------
// CompileTask::log_task_queued
void CompileTask::log_task_queued() {
  Thread* thread = Thread::current();
  ttyLocker ttyl;
  ResourceMark rm(thread);

  xtty->begin_elem("task_queued");
  log_task(xtty);
  if (_comment != NULL) {
    xtty->print(" comment='%s'", _comment);
  }
  if (_hot_method != NULL) {
573 574
    methodHandle hot(thread, _hot_method);
    methodHandle method(thread, _method);
D
duke 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
    if (hot() != method()) {
      xtty->method(hot);
    }
  }
  if (_hot_count != 0) {
    xtty->print(" hot_count='%d'", _hot_count);
  }
  xtty->end_elem();
}


// ------------------------------------------------------------------
// CompileTask::log_task_start
void CompileTask::log_task_start(CompileLog* log)   {
  log->begin_head("task");
  log_task(log);
  log->end_head();
}


// ------------------------------------------------------------------
// CompileTask::log_task_done
void CompileTask::log_task_done(CompileLog* log) {
  Thread* thread = Thread::current();
599
  methodHandle method(thread, this->method());
D
duke 已提交
600 601
  ResourceMark rm(thread);

602 603 604 605 606
  if (!_is_success) {
    const char* reason = _failure_reason != NULL ? _failure_reason : "unknown";
    log->elem("failure reason='%s'", reason);
  }

D
duke 已提交
607 608 609
  // <task_done ... stamp='1.234'>  </task>
  nmethod* nm = code();
  log->begin_elem("task_done success='%d' nmsize='%d' count='%d'",
T
twisti 已提交
610
                  _is_success, nm == NULL ? 0 : nm->content_size(),
D
duke 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
                  method->invocation_count());
  int bec = method->backedge_count();
  if (bec != 0)  log->print(" backedge_count='%d'", bec);
  // Note:  "_is_complete" is about to be set, but is not.
  if (_num_inlined_bytecodes != 0) {
    log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);
  }
  log->stamp();
  log->end_elem();
  log->tail("task");
  log->clear_identities();   // next task will have different CI
  if (log->unflushed_count() > 2000) {
    log->flush();
  }
  log->mark_file_end();
}



630 631 632
/**
 * Add a CompileTask to a CompileQueue
 */
D
duke 已提交
633 634
void CompileQueue::add(CompileTask* task) {
  assert(lock()->owned_by_self(), "must own lock");
635
  assert(!CompileBroker::is_compilation_disabled_forever(), "Do not add task if compilation is turned off forever");
D
duke 已提交
636 637

  task->set_next(NULL);
I
iveresov 已提交
638
  task->set_prev(NULL);
D
duke 已提交
639 640 641 642 643 644 645 646 647 648

  if (_last == NULL) {
    // The compile queue is empty.
    assert(_first == NULL, "queue is empty");
    _first = task;
    _last = task;
  } else {
    // Append the task to the queue.
    assert(_last->next() == NULL, "not last");
    _last->set_next(task);
I
iveresov 已提交
649
    task->set_prev(_last);
D
duke 已提交
650 651
    _last = task;
  }
I
iveresov 已提交
652
  ++_size;
D
duke 已提交
653 654

  // Mark the method as being in the compile queue.
655
  task->method()->set_queued_for_compilation();
D
duke 已提交
656

657
  NOT_PRODUCT(print();)
D
duke 已提交
658 659 660 661 662 663

  if (LogCompilation && xtty != NULL) {
    task->log_task_queued();
  }

  // Notify CompilerThreads that a task is available.
I
iveresov 已提交
664
  lock()->notify_all();
D
duke 已提交
665 666
}

667 668 669 670 671 672
/**
 * Empties compilation queue by putting all compilation tasks onto
 * a freelist. Furthermore, the method wakes up all threads that are
 * waiting on a compilation task to finish. This can happen if background
 * compilation is disabled.
 */
673 674
void CompileQueue::free_all() {
  MutexLocker mu(lock());
675 676 677 678 679 680
  CompileTask* next = _first;

  // Iterate over all tasks in the compile queue
  while (next != NULL) {
    CompileTask* current = next;
    next = current->next();
681 682 683 684 685
    {
      // Wake up thread that blocks on the compile task.
      MutexLocker ct_lock(current->lock());
      current->lock()->notify();
    }
686 687
    // Put the task back on the freelist.
    CompileTask::free(current);
688
  }
689 690
  _first = NULL;

691 692
  // Wake up all threads that block on the queue.
  lock()->notify_all();
693 694
}

D
duke 已提交
695 696 697 698 699
// ------------------------------------------------------------------
// CompileQueue::get
//
// Get the next CompileTask from a CompileQueue
CompileTask* CompileQueue::get() {
700 701
  NMethodSweeper::possibly_sweep();

D
duke 已提交
702
  MutexLocker locker(lock());
703 704 705 706 707
  // If _first is NULL we have no more compile jobs. There are two reasons for
  // having no compile jobs: First, we compiled everything we wanted. Second,
  // we ran out of code cache so compilation has been disabled. In the latter
  // case we perform code cache sweeps to free memory such that we can re-enable
  // compilation.
D
duke 已提交
708
  while (_first == NULL) {
709 710 711 712 713
    // Exit loop if compilation is disabled forever
    if (CompileBroker::is_compilation_disabled_forever()) {
      return NULL;
    }

714 715 716 717 718 719 720 721 722 723 724 725 726 727
    if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) {
      // Wait a certain amount of time to possibly do another sweep.
      // We must wait until stack scanning has happened so that we can
      // transition a method's state from 'not_entrant' to 'zombie'.
      long wait_time = NmethodSweepCheckInterval * 1000;
      if (FLAG_IS_DEFAULT(NmethodSweepCheckInterval)) {
        // Only one thread at a time can do sweeping. Scale the
        // wait time according to the number of compiler threads.
        // As a result, the next sweep is likely to happen every 100ms
        // with an arbitrary number of threads that do sweeping.
        wait_time = 100 * CICompilerCount;
      }
      bool timeout = lock()->wait(!Mutex::_no_safepoint_check_flag, wait_time);
      if (timeout) {
728 729 730 731
        MutexUnlocker ul(lock());
        NMethodSweeper::possibly_sweep();
      }
    } else {
732 733 734 735 736 737
      // If there are no compilation tasks and we can compile new jobs
      // (i.e., there is enough free space in the code cache) there is
      // no need to invoke the sweeper. As a result, the hotness of methods
      // remains unchanged. This behavior is desired, since we want to keep
      // the stable state, i.e., we do not want to evict methods from the
      // code cache if it is unnecessary.
738 739 740 741
      // We need a timed wait here, since compiler threads can exit if compilation
      // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
      // is not critical and we do not want idle compiler threads to wake up too often.
      lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
742
    }
D
duke 已提交
743
  }
744 745 746 747 748

  if (CompileBroker::is_compilation_disabled_forever()) {
    return NULL;
  }

749 750 751 752 753
  CompileTask* task;
  {
    No_Safepoint_Verifier nsv;
    task = CompilationPolicy::policy()->select_task(this);
  }
754 755 756 757
  if (task != NULL) {
    remove(task);
    purge_stale_tasks(); // may temporarily release MCQ lock
  }
I
iveresov 已提交
758 759
  return task;
}
D
duke 已提交
760

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
// Clean & deallocate stale compile tasks.
// Temporarily releases MethodCompileQueue lock.
void CompileQueue::purge_stale_tasks() {
  assert(lock()->owned_by_self(), "must own lock");
  if (_first_stale != NULL) {
    // Stale tasks are purged when MCQ lock is released,
    // but _first_stale updates are protected by MCQ lock.
    // Once task processing starts and MCQ lock is released,
    // other compiler threads can reuse _first_stale.
    CompileTask* head = _first_stale;
    _first_stale = NULL;
    {
      MutexUnlocker ul(lock());
      for (CompileTask* task = head; task != NULL; ) {
        CompileTask* next_task = task->next();
        CompileTaskWrapper ctw(task); // Frees the task
777
        task->set_failure_reason("stale task");
778 779 780 781 782 783 784
        task = next_task;
      }
    }
  }
}

void CompileQueue::remove(CompileTask* task) {
I
iveresov 已提交
785 786 787 788 789 790 791
   assert(lock()->owned_by_self(), "must own lock");
  if (task->prev() != NULL) {
    task->prev()->set_next(task->next());
  } else {
    // max is the first element
    assert(task == _first, "Sanity");
    _first = task->next();
D
duke 已提交
792 793
  }

I
iveresov 已提交
794 795 796 797 798 799 800 801
  if (task->next() != NULL) {
    task->next()->set_prev(task->prev());
  } else {
    // max is the last element
    assert(task == _last, "Sanity");
    _last = task->prev();
  }
  --_size;
D
duke 已提交
802 803
}

804 805 806 807 808 809 810 811 812 813
void CompileQueue::remove_and_mark_stale(CompileTask* task) {
  assert(lock()->owned_by_self(), "must own lock");
  remove(task);

  // Enqueue the task for reclamation (should be done outside MCQ lock)
  task->set_next(_first_stale);
  task->set_prev(NULL);
  _first_stale = task;
}

814 815 816 817 818 819 820 821 822 823
// methods in the compile queue need to be marked as used on the stack
// so that they don't get reclaimed by Redefine Classes
void CompileQueue::mark_on_stack() {
  CompileTask* task = _first;
  while (task != NULL) {
    task->mark_on_stack();
    task = task->next();
  }
}

824 825 826 827
#ifndef PRODUCT
/**
 * Print entire compilation queue.
 */
D
duke 已提交
828
void CompileQueue::print() {
829 830 831 832 833 834 835 836 837 838
  if (CIPrintCompileQueue) {
    ttyLocker ttyl;
    tty->print_cr("Contents of %s", name());
    tty->print_cr("----------------------");
    CompileTask* task = _first;
    while (task != NULL) {
      task->print_line();
      task = task->next();
    }
    tty->print_cr("----------------------");
D
duke 已提交
839 840
  }
}
841
#endif // PRODUCT
D
duke 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886

CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) {

  _current_method[0] = '\0';
  _compile_type = CompileBroker::no_compile;

  if (UsePerfData) {
    ResourceMark rm;

    // create the thread instance name space string - don't create an
    // instance subspace if instance is -1 - keeps the adapterThread
    // counters  from having a ".0" namespace.
    const char* thread_i = (instance == -1) ? thread_name :
                      PerfDataManager::name_space(thread_name, instance);


    char* name = PerfDataManager::counter_name(thread_i, "method");
    _perf_current_method =
               PerfDataManager::create_string_variable(SUN_CI, name,
                                                       cmname_buffer_length,
                                                       _current_method, CHECK);

    name = PerfDataManager::counter_name(thread_i, "type");
    _perf_compile_type = PerfDataManager::create_variable(SUN_CI, name,
                                                          PerfData::U_None,
                                                         (jlong)_compile_type,
                                                          CHECK);

    name = PerfDataManager::counter_name(thread_i, "time");
    _perf_time = PerfDataManager::create_counter(SUN_CI, name,
                                                 PerfData::U_Ticks, CHECK);

    name = PerfDataManager::counter_name(thread_i, "compiles");
    _perf_compiles = PerfDataManager::create_counter(SUN_CI, name,
                                                     PerfData::U_Events, CHECK);
  }
}

// ------------------------------------------------------------------
// CompileBroker::compilation_init
//
// Initialize the Compilation object
void CompileBroker::compilation_init() {
  _last_method_compiled[0] = '\0';

887 888 889 890
  // No need to initialize compilation system if we do not use it.
  if (!UseCompiler) {
    return;
  }
T
twisti 已提交
891
#ifndef SHARK
D
duke 已提交
892
  // Set the interface to the current compiler(s).
I
iveresov 已提交
893 894
  int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
  int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
D
duke 已提交
895
#ifdef COMPILER1
I
iveresov 已提交
896 897 898
  if (c1_count > 0) {
    _compilers[0] = new Compiler();
  }
D
duke 已提交
899 900 901
#endif // COMPILER1

#ifdef COMPILER2
I
iveresov 已提交
902 903 904
  if (c2_count > 0) {
    _compilers[1] = new C2Compiler();
  }
D
duke 已提交
905 906
#endif // COMPILER2

T
twisti 已提交
907 908 909 910 911 912
#else // SHARK
  int c1_count = 0;
  int c2_count = 1;

  _compilers[1] = new SharkCompiler();
#endif // SHARK
913

D
duke 已提交
914
  // Start the CompilerThreads
I
iveresov 已提交
915
  init_compiler_threads(c1_count, c2_count);
D
duke 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
  // totalTime performance counter is always created as it is required
  // by the implementation of java.lang.management.CompilationMBean.
  {
    EXCEPTION_MARK;
    _perf_total_compilation =
                 PerfDataManager::create_counter(JAVA_CI, "totalTime",
                                                 PerfData::U_Ticks, CHECK);
  }


  if (UsePerfData) {

    EXCEPTION_MARK;

    // create the jvmstat performance counters
    _perf_osr_compilation =
                 PerfDataManager::create_counter(SUN_CI, "osrTime",
                                                 PerfData::U_Ticks, CHECK);

    _perf_standard_compilation =
                 PerfDataManager::create_counter(SUN_CI, "standardTime",
                                                 PerfData::U_Ticks, CHECK);

    _perf_total_bailout_count =
                 PerfDataManager::create_counter(SUN_CI, "totalBailouts",
                                                 PerfData::U_Events, CHECK);

    _perf_total_invalidated_count =
                 PerfDataManager::create_counter(SUN_CI, "totalInvalidates",
                                                 PerfData::U_Events, CHECK);

    _perf_total_compile_count =
                 PerfDataManager::create_counter(SUN_CI, "totalCompiles",
                                                 PerfData::U_Events, CHECK);
    _perf_total_osr_compile_count =
                 PerfDataManager::create_counter(SUN_CI, "osrCompiles",
                                                 PerfData::U_Events, CHECK);

    _perf_total_standard_compile_count =
                 PerfDataManager::create_counter(SUN_CI, "standardCompiles",
                                                 PerfData::U_Events, CHECK);

    _perf_sum_osr_bytes_compiled =
                 PerfDataManager::create_counter(SUN_CI, "osrBytes",
                                                 PerfData::U_Bytes, CHECK);

    _perf_sum_standard_bytes_compiled =
                 PerfDataManager::create_counter(SUN_CI, "standardBytes",
                                                 PerfData::U_Bytes, CHECK);

    _perf_sum_nmethod_size =
                 PerfDataManager::create_counter(SUN_CI, "nmethodSize",
                                                 PerfData::U_Bytes, CHECK);

    _perf_sum_nmethod_code_size =
                 PerfDataManager::create_counter(SUN_CI, "nmethodCodeSize",
                                                 PerfData::U_Bytes, CHECK);

    _perf_last_method =
                 PerfDataManager::create_string_variable(SUN_CI, "lastMethod",
                                       CompilerCounters::cmname_buffer_length,
                                       "", CHECK);

    _perf_last_failed_method =
            PerfDataManager::create_string_variable(SUN_CI, "lastFailedMethod",
                                       CompilerCounters::cmname_buffer_length,
                                       "", CHECK);

    _perf_last_invalidated_method =
        PerfDataManager::create_string_variable(SUN_CI, "lastInvalidatedMethod",
                                     CompilerCounters::cmname_buffer_length,
                                     "", CHECK);

    _perf_last_compile_type =
             PerfDataManager::create_variable(SUN_CI, "lastType",
                                              PerfData::U_None,
                                              (jlong)CompileBroker::no_compile,
                                              CHECK);

    _perf_last_compile_size =
             PerfDataManager::create_variable(SUN_CI, "lastSize",
                                              PerfData::U_Bytes,
                                              (jlong)CompileBroker::no_compile,
                                              CHECK);


    _perf_last_failed_type =
             PerfDataManager::create_variable(SUN_CI, "lastFailedType",
                                              PerfData::U_None,
                                              (jlong)CompileBroker::no_compile,
                                              CHECK);

    _perf_last_invalidated_type =
         PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
                                          PerfData::U_None,
                                          (jlong)CompileBroker::no_compile,
                                          CHECK);
  }

  _initialized = true;
}


1019 1020
CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
                                                    AbstractCompiler* comp, TRAPS) {
D
duke 已提交
1021 1022
  CompilerThread* compiler_thread = NULL;

1023
  Klass* k =
1024
    SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
D
duke 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
                                      true, CHECK_0);
  instanceKlassHandle klass (THREAD, k);
  instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
  Handle string = java_lang_String::create_from_str(name, CHECK_0);

  // Initialize thread_oop to put it into the system threadGroup
  Handle thread_group (THREAD,  Universe::system_thread_group());
  JavaValue result(T_VOID);
  JavaCalls::call_special(&result, thread_oop,
                       klass,
1035 1036
                       vmSymbols::object_initializer_name(),
                       vmSymbols::threadgroup_string_void_signature(),
D
duke 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
                       thread_group,
                       string,
                       CHECK_0);

  {
    MutexLocker mu(Threads_lock, THREAD);
    compiler_thread = new CompilerThread(queue, counters);
    // At this point the new CompilerThread data-races with this startup
    // thread (which I believe is the primoridal thread and NOT the VM
    // thread).  This means Java bytecodes being executed at startup can
    // queue compile jobs which will run at whatever default priority the
    // newly created CompilerThread runs at.


    // At this point it may be possible that no osthread was created for the
    // JavaThread due to lack of memory. We would have to throw an exception
    // in that case. However, since this must work and we do not allow
    // exceptions anyway, check and abort if this fails.

    if (compiler_thread == NULL || compiler_thread->osthread() == NULL){
      vm_exit_during_initialization("java.lang.OutOfMemoryError",
                                    "unable to create new native thread");
    }

    java_lang_Thread::set_thread(thread_oop(), compiler_thread);

    // Note that this only sets the JavaThread _priority field, which by
    // definition is limited to Java priorities and not OS priorities.
    // The os-priority is set in the CompilerThread startup code itself
1066

D
duke 已提交
1067
    java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

    // Note that we cannot call os::set_priority because it expects Java
    // priorities and we are *explicitly* using OS priorities so that it's
    // possible to set the compiler thread priority higher than any Java
    // thread.

    int native_prio = CompilerThreadPriority;
    if (native_prio == -1) {
      if (UseCriticalCompilerThreadPriority) {
        native_prio = os::java_to_os_priority[CriticalPriority];
      } else {
        native_prio = os::java_to_os_priority[NearMaxPriority];
      }
    }
    os::set_native_priority(compiler_thread, native_prio);
D
duke 已提交
1083 1084 1085 1086

    java_lang_Thread::set_daemon(thread_oop());

    compiler_thread->set_threadObj(thread_oop());
1087
    compiler_thread->set_compiler(comp);
D
duke 已提交
1088 1089 1090
    Threads::add(compiler_thread);
    Thread::start(compiler_thread);
  }
1091

D
duke 已提交
1092 1093 1094 1095 1096 1097 1098
  // Let go of Threads_lock before yielding
  os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)

  return compiler_thread;
}


I
iveresov 已提交
1099
void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) {
D
duke 已提交
1100
  EXCEPTION_MARK;
T
twisti 已提交
1101
#if !defined(ZERO) && !defined(SHARK)
I
iveresov 已提交
1102
  assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
T
twisti 已提交
1103
#endif // !ZERO && !SHARK
1104
  // Initialize the compilation queue
I
iveresov 已提交
1105
  if (c2_compiler_count > 0) {
1106
    _c2_compile_queue  = new CompileQueue("C2 CompileQueue",  MethodCompileQueue_lock);
1107
    _compilers[1]->set_num_compiler_threads(c2_compiler_count);
I
iveresov 已提交
1108 1109
  }
  if (c1_compiler_count > 0) {
1110
    _c1_compile_queue  = new CompileQueue("C1 CompileQueue",  MethodCompileQueue_lock);
1111
    _compilers[0]->set_num_compiler_threads(c1_compiler_count);
I
iveresov 已提交
1112 1113 1114
  }

  int compiler_count = c1_compiler_count + c2_compiler_count;
D
duke 已提交
1115

1116
  _compiler_threads =
Z
zgu 已提交
1117
    new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true);
D
duke 已提交
1118 1119

  char name_buffer[256];
I
iveresov 已提交
1120
  for (int i = 0; i < c2_compiler_count; i++) {
D
duke 已提交
1121
    // Create a name for our thread.
I
iveresov 已提交
1122
    sprintf(name_buffer, "C2 CompilerThread%d", i);
D
duke 已提交
1123
    CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1124
    // Shark and C2
1125
    CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], CHECK);
1126
    _compiler_threads->append(new_thread);
I
iveresov 已提交
1127
  }
D
duke 已提交
1128

I
iveresov 已提交
1129 1130 1131 1132
  for (int i = c2_compiler_count; i < compiler_count; i++) {
    // Create a name for our thread.
    sprintf(name_buffer, "C1 CompilerThread%d", i);
    CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1133
    // C1
1134
    CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], CHECK);
1135
    _compiler_threads->append(new_thread);
D
duke 已提交
1136
  }
I
iveresov 已提交
1137

D
duke 已提交
1138
  if (UsePerfData) {
1139
    PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
D
duke 已提交
1140 1141 1142
  }
}

1143

1144 1145
/**
 * Set the methods on the stack as on_stack so that redefine classes doesn't
1146
 * reclaim them. This method is executed at a safepoint.
1147
 */
1148
void CompileBroker::mark_on_stack() {
1149 1150 1151
  assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
  // Since we are at a safepoint, we do not need a lock to access
  // the compile queues.
1152 1153
  if (_c2_compile_queue != NULL) {
    _c2_compile_queue->mark_on_stack();
1154
  }
1155 1156
  if (_c1_compile_queue != NULL) {
    _c1_compile_queue->mark_on_stack();
1157 1158 1159
  }
}

D
duke 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
// ------------------------------------------------------------------
// CompileBroker::compile_method
//
// Request compilation of a method.
void CompileBroker::compile_method_base(methodHandle method,
                                        int osr_bci,
                                        int comp_level,
                                        methodHandle hot_method,
                                        int hot_count,
                                        const char* comment,
1170
                                        Thread* thread) {
D
duke 已提交
1171
  // do nothing if compiler thread(s) is not available
1172
  if (!_initialized) {
D
duke 已提交
1173 1174 1175 1176
    return;
  }

  guarantee(!method->is_abstract(), "cannot compile abstract methods");
1177
  assert(method->method_holder()->oop_is_instance(),
D
duke 已提交
1178
         "sanity check");
1179
  assert(!method->method_holder()->is_not_initialized(),
D
duke 已提交
1180
         "method holder must be initialized");
1181
  assert(!method->is_method_handle_intrinsic(), "do not enqueue these guys");
D
duke 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207

  if (CIPrintRequests) {
    tty->print("request: ");
    method->print_short_name(tty);
    if (osr_bci != InvocationEntryBci) {
      tty->print(" osr_bci: %d", osr_bci);
    }
    tty->print(" comment: %s count: %d", comment, hot_count);
    if (!hot_method.is_null()) {
      tty->print(" hot: ");
      if (hot_method() != method()) {
          hot_method->print_short_name(tty);
      } else {
        tty->print("yes");
      }
    }
    tty->cr();
  }

  // A request has been made for compilation.  Before we do any
  // real work, check to see if the method has been compiled
  // in the meantime with a definitive result.
  if (compilation_is_complete(method, osr_bci, comp_level)) {
    return;
  }

1208 1209 1210 1211 1212 1213 1214 1215
#ifndef PRODUCT
  if (osr_bci != -1 && !FLAG_IS_DEFAULT(OSROnlyBCI)) {
    if ((OSROnlyBCI > 0) ? (OSROnlyBCI != osr_bci) : (-OSROnlyBCI == osr_bci)) {
      // Positive OSROnlyBCI means only compile that bci.  Negative means don't compile that BCI.
      return;
    }
  }
#endif
I
iveresov 已提交
1216

D
duke 已提交
1217 1218
  // If this method is already in the compile queue, then
  // we do not block the current thread.
1219
  if (compilation_is_in_queue(method)) {
D
duke 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
    // We may want to decay our counter a bit here to prevent
    // multiple denied requests for compilation.  This is an
    // open compilation policy issue. Note: The other possibility,
    // in the case that this is a blocking compile request, is to have
    // all subsequent blocking requesters wait for completion of
    // ongoing compiles. Note that in this case we'll need a protocol
    // for freeing the associated compile tasks. [Or we could have
    // a single static monitor on which all these waiters sleep.]
    return;
  }

1231 1232 1233 1234 1235
  // If the requesting thread is holding the pending list lock
  // then we just return. We can't risk blocking while holding
  // the pending list lock or a 3-way deadlock may occur
  // between the reference handler thread, a GC (instigated
  // by a compiler thread), and compiled method registration.
1236
  if (InstanceRefKlass::owns_pending_list_lock(JavaThread::current())) {
1237 1238 1239
    return;
  }

1240 1241 1242 1243 1244 1245
  if (TieredCompilation) {
    // Tiered policy requires MethodCounters to exist before adding a method to
    // the queue. Create if we don't have them yet.
    method->get_method_counters(thread);
  }

D
duke 已提交
1246 1247 1248
  // Outputs from the following MutexLocker block:
  CompileTask* task     = NULL;
  bool         blocking = false;
I
iveresov 已提交
1249
  CompileQueue* queue  = compile_queue(comp_level);
D
duke 已提交
1250 1251 1252

  // Acquire our lock.
  {
1253
    MutexLocker locker(queue->lock(), thread);
D
duke 已提交
1254 1255 1256 1257

    // Make sure the method has not slipped into the queues since
    // last we checked; note that those checks were "fast bail-outs".
    // Here we need to be more careful, see 14012000 below.
1258
    if (compilation_is_in_queue(method)) {
D
duke 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
      return;
    }

    // We need to check again to see if the compilation has
    // completed.  A previous compilation may have registered
    // some result.
    if (compilation_is_complete(method, osr_bci, comp_level)) {
      return;
    }

    // We now know that this compilation is not pending, complete,
    // or prohibited.  Assign a compile_id to this compilation
    // and check to see if it is in our [Start..Stop) range.
A
anoll 已提交
1272
    int compile_id = assign_compile_id(method, osr_bci);
D
duke 已提交
1273 1274 1275 1276 1277 1278
    if (compile_id == 0) {
      // The compilation falls outside the allowed range.
      return;
    }

    // Should this thread wait for completion of the compile?
1279
    blocking = is_compile_blocking();
D
duke 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318

    // We will enter the compilation in the queue.
    // 14012000: Note that this sets the queued_for_compile bits in
    // the target method. We can now reason that a method cannot be
    // queued for compilation more than once, as follows:
    // Before a thread queues a task for compilation, it first acquires
    // the compile queue lock, then checks if the method's queued bits
    // are set or it has already been compiled. Thus there can not be two
    // instances of a compilation task for the same method on the
    // compilation queue. Consider now the case where the compilation
    // thread has already removed a task for that method from the queue
    // and is in the midst of compiling it. In this case, the
    // queued_for_compile bits must be set in the method (and these
    // will be visible to the current thread, since the bits were set
    // under protection of the compile queue lock, which we hold now.
    // When the compilation completes, the compiler thread first sets
    // the compilation result and then clears the queued_for_compile
    // bits. Neither of these actions are protected by a barrier (or done
    // under the protection of a lock), so the only guarantee we have
    // (on machines with TSO (Total Store Order)) is that these values
    // will update in that order. As a result, the only combinations of
    // these bits that the current thread will see are, in temporal order:
    // <RESULT, QUEUE> :
    //     <0, 1> : in compile queue, but not yet compiled
    //     <1, 1> : compiled but queue bit not cleared
    //     <1, 0> : compiled and queue bit cleared
    // Because we first check the queue bits then check the result bits,
    // we are assured that we cannot introduce a duplicate task.
    // Note that if we did the tests in the reverse order (i.e. check
    // result then check queued bit), we could get the result bit before
    // the compilation completed, and the queue bit after the compilation
    // completed, and end up introducing a "duplicate" (redundant) task.
    // In that case, the compiler thread should first check if a method
    // has already been compiled before trying to compile it.
    // NOTE: in the event that there are multiple compiler threads and
    // there is de-optimization/recompilation, things will get hairy,
    // and in that case it's best to protect both the testing (here) of
    // these bits, and their updating (here and elsewhere) under a
    // common lock.
I
iveresov 已提交
1319
    task = create_compile_task(queue,
D
duke 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
                               compile_id, method,
                               osr_bci, comp_level,
                               hot_method, hot_count, comment,
                               blocking);
  }

  if (blocking) {
    wait_for_completion(task);
  }
}


nmethod* CompileBroker::compile_method(methodHandle method, int osr_bci,
I
iveresov 已提交
1333
                                       int comp_level,
D
duke 已提交
1334
                                       methodHandle hot_method, int hot_count,
1335
                                       const char* comment, Thread* THREAD) {
D
duke 已提交
1336
  // make sure arguments make sense
1337
  assert(method->method_holder()->oop_is_instance(), "not an instance method");
D
duke 已提交
1338 1339
  assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range");
  assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods");
1340
  assert(!method->method_holder()->is_not_initialized(), "method holder must be initialized");
1341 1342
  // allow any levels for WhiteBox
  assert(WhiteBoxAPI || TieredCompilation || comp_level == CompLevel_highest_tier, "only CompLevel_highest_tier must be used in non-tiered");
D
duke 已提交
1343 1344 1345 1346
  // return quickly if possible

  // lock, make sure that the compilation
  // isn't prohibited in a straightforward way.
1347 1348 1349
  AbstractCompiler *comp = CompileBroker::compiler(comp_level);
  if (comp == NULL || !comp->can_compile_method(method) ||
      compilation_is_prohibited(method, osr_bci, comp_level)) {
D
duke 已提交
1350 1351 1352 1353 1354 1355
    return NULL;
  }

  if (osr_bci == InvocationEntryBci) {
    // standard compilation
    nmethod* method_code = method->code();
I
iveresov 已提交
1356 1357 1358 1359
    if (method_code != NULL) {
      if (compilation_is_complete(method, osr_bci, comp_level)) {
        return method_code;
      }
D
duke 已提交
1360
    }
1361 1362
    if (method->is_not_compilable(comp_level)) {
      return NULL;
1363
    }
D
duke 已提交
1364 1365 1366 1367
  } else {
    // osr compilation
#ifndef TIERED
    // seems like an assert of dubious value
I
iveresov 已提交
1368
    assert(comp_level == CompLevel_highest_tier,
D
duke 已提交
1369 1370
           "all OSR compiles are assumed to be at a single compilation lavel");
#endif // TIERED
I
iveresov 已提交
1371 1372
    // We accept a higher level osr method
    nmethod* nm = method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
D
duke 已提交
1373
    if (nm != NULL) return nm;
1374
    if (method->is_not_osr_compilable(comp_level)) return NULL;
D
duke 已提交
1375 1376 1377 1378
  }

  assert(!HAS_PENDING_EXCEPTION, "No exception should be present");
  // some prerequisites that are compiler specific
1379
  if (comp->is_c2() || comp->is_shark()) {
1380
    method->constants()->resolve_string_constants(CHECK_AND_CLEAR_NULL);
D
duke 已提交
1381 1382
    // Resolve all classes seen in the signature of the method
    // we are compiling.
1383
    Method::load_signature_classes(method, CHECK_AND_CLEAR_NULL);
D
duke 已提交
1384 1385 1386 1387 1388 1389 1390 1391
  }

  // If the method is native, do the lookup in the thread requesting
  // the compilation. Native lookups can load code, which is not
  // permitted during compilation.
  //
  // Note: A native method implies non-osr compilation which is
  //       checked with an assertion at the entry of this method.
1392
  if (method->is_native() && !method->is_method_handle_intrinsic()) {
D
duke 已提交
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
    bool in_base_library;
    address adr = NativeLookup::lookup(method, in_base_library, THREAD);
    if (HAS_PENDING_EXCEPTION) {
      // In case of an exception looking up the method, we just forget
      // about it. The interpreter will kick-in and throw the exception.
      method->set_not_compilable(); // implies is_not_osr_compilable()
      CLEAR_PENDING_EXCEPTION;
      return NULL;
    }
    assert(method->has_native_function(), "must have native code by now");
  }

  // RedefineClasses() has replaced this method; just return
  if (method->is_old()) {
    return NULL;
  }

  // JVMTI -- post_compile_event requires jmethod_id() that may require
  // a lock the compiling thread can not acquire. Prefetch it here.
  if (JvmtiExport::should_post_compiled_method_load()) {
    method->jmethod_id();
  }

  // do the compilation
  if (method->is_native()) {
1418
    if (!PreferInterpreterNativeStubs || method->is_method_handle_intrinsic()) {
1419 1420 1421 1422 1423
      // To properly handle the appendix argument for out-of-line calls we are using a small trampoline that
      // pops off the appendix argument and jumps to the target (see gen_special_dispatch in SharedRuntime).
      //
      // Since normal compiled-to-compiled calls are not able to handle such a thing we MUST generate an adapter
      // in this case.  If we can't generate one and use it we can not execute the out-of-line method handle calls.
A
anoll 已提交
1424
      AdapterHandlerLibrary::create_native_wrapper(method);
D
duke 已提交
1425 1426 1427 1428
    } else {
      return NULL;
    }
  } else {
1429 1430 1431 1432 1433 1434
    // If the compiler is shut off due to code cache getting full
    // fail out now so blocking compiles dont hang the java thread
    if (!should_compile_new_jobs()) {
      CompilationPolicy::policy()->delay_compilation(method());
      return NULL;
    }
1435
    compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, THREAD);
D
duke 已提交
1436 1437 1438
  }

  // return requested nmethod
I
iveresov 已提交
1439 1440
  // We accept a higher level osr method
  return osr_bci  == InvocationEntryBci ? method->code() : method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
D
duke 已提交
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
}


// ------------------------------------------------------------------
// CompileBroker::compilation_is_complete
//
// See if compilation of this method is already complete.
bool CompileBroker::compilation_is_complete(methodHandle method,
                                            int          osr_bci,
                                            int          comp_level) {
  bool is_osr = (osr_bci != standard_entry_bci);
  if (is_osr) {
1453
    if (method->is_not_osr_compilable(comp_level)) {
D
duke 已提交
1454 1455
      return true;
    } else {
I
iveresov 已提交
1456
      nmethod* result = method->lookup_osr_nmethod_for(osr_bci, comp_level, true);
D
duke 已提交
1457 1458 1459 1460 1461 1462 1463 1464
      return (result != NULL);
    }
  } else {
    if (method->is_not_compilable(comp_level)) {
      return true;
    } else {
      nmethod* result = method->code();
      if (result == NULL) return false;
I
iveresov 已提交
1465
      return comp_level == result->comp_level();
D
duke 已提交
1466 1467 1468 1469 1470
    }
  }
}


1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
/**
 * See if this compilation is already requested.
 *
 * Implementation note: there is only a single "is in queue" bit
 * for each method.  This means that the check below is overly
 * conservative in the sense that an osr compilation in the queue
 * will block a normal compilation from entering the queue (and vice
 * versa).  This can be remedied by a full queue search to disambiguate
 * cases.  If it is deemed profitable, this may be done.
 */
bool CompileBroker::compilation_is_in_queue(methodHandle method) {
D
duke 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
  return method->queued_for_compilation();
}

// ------------------------------------------------------------------
// CompileBroker::compilation_is_prohibited
//
// See if this compilation is not allowed.
bool CompileBroker::compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level) {
  bool is_native = method->is_native();
  // Some compilers may not support the compilation of natives.
1492
  AbstractCompiler *comp = compiler(comp_level);
D
duke 已提交
1493
  if (is_native &&
1494
      (!CICompileNatives || comp == NULL || !comp->supports_native())) {
I
iveresov 已提交
1495
    method->set_not_compilable_quietly(comp_level);
D
duke 已提交
1496 1497 1498 1499 1500 1501
    return true;
  }

  bool is_osr = (osr_bci != standard_entry_bci);
  // Some compilers may not support on stack replacement.
  if (is_osr &&
1502
      (!CICompileOSR || comp == NULL || !comp->supports_osr())) {
1503
    method->set_not_osr_compilable(comp_level);
D
duke 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
    return true;
  }

  // The method may be explicitly excluded by the user.
  bool quietly;
  if (CompilerOracle::should_exclude(method, quietly)) {
    if (!quietly) {
      // This does not happen quietly...
      ResourceMark rm;
      tty->print("### Excluding %s:%s",
                 method->is_native() ? "generation of native wrapper" : "compile",
                 (method->is_static() ? " static" : ""));
      method->print_short_name(tty);
      tty->cr();
    }
1519
    method->set_not_compilable(CompLevel_all, !quietly, "excluded by CompilerOracle");
D
duke 已提交
1520 1521 1522 1523 1524
  }

  return false;
}

A
anoll 已提交
1525 1526 1527 1528 1529 1530 1531
/**
 * Generate serialized IDs for compilation requests. If certain debugging flags are used
 * and the ID is not within the specified range, the method is not compiled and 0 is returned.
 * The function also allows to generate separate compilation IDs for OSR compilations.
 */
int CompileBroker::assign_compile_id(methodHandle method, int osr_bci) {
#ifdef ASSERT
D
duke 已提交
1532
  bool is_osr = (osr_bci != standard_entry_bci);
A
anoll 已提交
1533 1534 1535 1536 1537 1538 1539 1540 1541
  int id;
  if (method->is_native()) {
    assert(!is_osr, "can't be osr");
    // Adapters, native wrappers and method handle intrinsics
    // should be generated always.
    return Atomic::add(1, &_compilation_id);
  } else if (CICountOSR && is_osr) {
    id = Atomic::add(1, &_osr_compilation_id);
    if (CIStartOSR <= id && id < CIStopOSR) {
D
duke 已提交
1542 1543 1544
      return id;
    }
  } else {
A
anoll 已提交
1545 1546
    id = Atomic::add(1, &_compilation_id);
    if (CIStart <= id && id < CIStop) {
D
duke 已提交
1547 1548 1549 1550 1551
      return id;
    }
  }

  // Method was not in the appropriate compilation range.
1552
  method->set_not_compilable_quietly();
D
duke 已提交
1553
  return 0;
A
anoll 已提交
1554 1555 1556 1557 1558
#else
  // CICountOSR is a develop flag and set to 'false' by default. In a product built,
  // only _compilation_id is incremented.
  return Atomic::add(1, &_compilation_id);
#endif
D
duke 已提交
1559 1560
}

1561 1562 1563 1564 1565
/**
 * Should the current thread block until this compilation request
 * has been fulfilled?
 */
bool CompileBroker::is_compile_blocking() {
1566
  assert(!InstanceRefKlass::owns_pending_list_lock(JavaThread::current()), "possible deadlock");
1567
  return !BackgroundCompilation;
D
duke 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
}


// ------------------------------------------------------------------
// CompileBroker::preload_classes
void CompileBroker::preload_classes(methodHandle method, TRAPS) {
  // Move this code over from c1_Compiler.cpp
  ShouldNotReachHere();
}


// ------------------------------------------------------------------
// CompileBroker::create_compile_task
//
// Create a CompileTask object representing the current request for
// compilation.  Add this task to the queue.
CompileTask* CompileBroker::create_compile_task(CompileQueue* queue,
                                              int           compile_id,
                                              methodHandle  method,
                                              int           osr_bci,
                                              int           comp_level,
                                              methodHandle  hot_method,
                                              int           hot_count,
                                              const char*   comment,
                                              bool          blocking) {
1593
  CompileTask* new_task = CompileTask::allocate();
D
duke 已提交
1594 1595 1596 1597 1598 1599 1600 1601
  new_task->initialize(compile_id, method, osr_bci, comp_level,
                       hot_method, hot_count, comment,
                       blocking);
  queue->add(new_task);
  return new_task;
}


1602 1603 1604
/**
 *  Wait for the compilation task to complete.
 */
D
duke 已提交
1605 1606
void CompileBroker::wait_for_completion(CompileTask* task) {
  if (CIPrintCompileQueue) {
1607
    ttyLocker ttyl;
D
duke 已提交
1608 1609 1610 1611 1612
    tty->print_cr("BLOCKING FOR COMPILE");
  }

  assert(task->is_blocking(), "can only wait on blocking task");

1613
  JavaThread* thread = JavaThread::current();
D
duke 已提交
1614 1615
  thread->set_blocked_on_compilation(true);

1616
  methodHandle method(thread, task->method());
D
duke 已提交
1617 1618 1619
  {
    MutexLocker waiter(task->lock(), thread);

1620
    while (!task->is_complete() && !is_compilation_disabled_forever()) {
D
duke 已提交
1621
      task->lock()->wait();
1622 1623 1624 1625 1626 1627 1628
    }
  }

  thread->set_blocked_on_compilation(false);
  if (is_compilation_disabled_forever()) {
    CompileTask::free(task);
    return;
D
duke 已提交
1629
  }
1630

D
duke 已提交
1631 1632 1633 1634 1635 1636 1637 1638 1639
  // It is harmless to check this status without the lock, because
  // completion is a stable property (until the task object is recycled).
  assert(task->is_complete(), "Compilation should have completed");
  assert(task->code_handle() == NULL, "must be reset");

  // By convention, the waiter is responsible for recycling a
  // blocking CompileTask. Since there is only one waiter ever
  // waiting on a CompileTask, we know that no one else will
  // be using this CompileTask; we can free it.
1640
  CompileTask::free(task);
D
duke 已提交
1641 1642
}

1643 1644 1645 1646 1647
/**
 * Initialize compiler thread(s) + compiler object(s). The postcondition
 * of this function is that the compiler runtimes are initialized and that
 * compiler threads can start compiling.
 */
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
bool CompileBroker::init_compiler_runtime() {
  CompilerThread* thread = CompilerThread::current();
  AbstractCompiler* comp = thread->compiler();
  // Final sanity check - the compiler object must exist
  guarantee(comp != NULL, "Compiler object must exist");

  int system_dictionary_modification_counter;
  {
    MutexLocker locker(Compile_lock, thread);
    system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
  }

  {
    // Must switch to native to allocate ci_env
    ThreadToNativeFromVM ttn(thread);
    ciEnv ci_env(NULL, system_dictionary_modification_counter);
    // Cache Jvmti state
    ci_env.cache_jvmti_state();
    // Cache DTrace flags
    ci_env.cache_dtrace_flags();

    // Switch back to VM state to do compiler initialization
    ThreadInVMfromNative tv(thread);
    ResetNoHandleMark rnhm;


    if (!comp->is_shark()) {
      // Perform per-thread and global initializations
      comp->initialize();
    }
  }

  if (comp->is_failed()) {
    disable_compilation_forever();
    // If compiler initialization failed, no compiler thread that is specific to a
    // particular compiler runtime will ever start to compile methods.
    shutdown_compiler_runtime(comp, thread);
    return false;
  }

  // C1 specific check
  if (comp->is_c1() && (thread->get_buffer_blob() == NULL)) {
    warning("Initialization of %s thread failed (no space to run compilers)", thread->name());
    return false;
  }

  return true;
}

1697 1698 1699 1700 1701
/**
 * If C1 and/or C2 initialization failed, we shut down all compilation.
 * We do this to keep things simple. This can be changed if it ever turns
 * out to be a problem.
 */
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) {
  // Free buffer blob, if allocated
  if (thread->get_buffer_blob() != NULL) {
    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
    CodeCache::free(thread->get_buffer_blob());
  }

  if (comp->should_perform_shutdown()) {
    // There are two reasons for shutting down the compiler
    // 1) compiler runtime initialization failed
    // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing
1713
    warning("%s initialization failed. Shutting down all compilers", comp->name());
1714 1715 1716 1717 1718

    // Only one thread per compiler runtime object enters here
    // Set state to shut down
    comp->set_shut_down();

1719 1720 1721
    // Delete all queued compilation tasks to make compiler threads exit faster.
    if (_c1_compile_queue != NULL) {
      _c1_compile_queue->free_all();
1722 1723
    }

1724 1725
    if (_c2_compile_queue != NULL) {
      _c2_compile_queue->free_all();
1726 1727
    }

1728 1729 1730 1731
    // Set flags so that we continue execution with using interpreter only.
    UseCompiler    = false;
    UseInterpreter = true;

1732 1733 1734 1735 1736 1737
    // We could delete compiler runtimes also. However, there are references to
    // the compiler runtime(s) (e.g.,  nmethod::is_compiled_by_c1()) which then
    // fail. This can be done later if necessary.
  }
}

D
duke 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
// ------------------------------------------------------------------
// CompileBroker::compiler_thread_loop
//
// The main loop run by a CompilerThread.
void CompileBroker::compiler_thread_loop() {
  CompilerThread* thread = CompilerThread::current();
  CompileQueue* queue = thread->queue();
  // For the thread that initializes the ciObjectFactory
  // this resource mark holds all the shared objects
  ResourceMark rm;

  // First thread to get here will initialize the compiler interface

  if (!ciObjectFactory::is_initialized()) {
    ASSERT_IN_VM;
    MutexLocker only_one (CompileThread_lock, thread);
    if (!ciObjectFactory::is_initialized()) {
      ciObjectFactory::initialize();
    }
  }

  // Open a log.
  if (LogCompilation) {
    init_compiler_thread_log();
  }
  CompileLog* log = thread->log();
  if (log != NULL) {
V
vlivanov 已提交
1765 1766
    log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
                    thread->name(),
D
duke 已提交
1767 1768 1769 1770 1771 1772
                    os::current_thread_id(),
                    os::current_process_id());
    log->stamp();
    log->end_elem();
  }

1773 1774 1775 1776
  // If compiler thread/runtime initialization fails, exit the compiler thread
  if (!init_compiler_runtime()) {
    return;
  }
1777

1778 1779 1780 1781 1782 1783 1784 1785
  // Poll for new compilation tasks as long as the JVM runs. Compilation
  // should only be disabled if something went wrong while initializing the
  // compiler runtimes. This, in turn, should not happen. The only known case
  // when compiler runtime initialization fails is if there is not enough free
  // space in the code cache to generate the necessary stubs, etc.
  while (!is_compilation_disabled_forever()) {
    // We need this HandleMark to avoid leaking VM handles.
    HandleMark hm(thread);
D
duke 已提交
1786

1787 1788 1789 1790
    if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) {
      // the code cache is really full
      handle_full_code_cache();
    }
D
duke 已提交
1791

1792 1793 1794 1795
    CompileTask* task = queue->get();
    if (task == NULL) {
      continue;
    }
D
duke 已提交
1796

1797 1798 1799 1800
    // Give compiler threads an extra quanta.  They tend to be bursty and
    // this helps the compiler to finish up the job.
    if( CompilerThreadHintNoPreempt )
      os::hint_no_preempt();
D
duke 已提交
1801

1802 1803 1804
    // trace per thread time and compile statistics
    CompilerCounters* counters = ((CompilerThread*)thread)->counters();
    PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter());
D
duke 已提交
1805

1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
    // Assign the task to the current thread.  Mark this compilation
    // thread as active for the profiler.
    CompileTaskWrapper ctw(task);
    nmethodLocker result_handle;  // (handle for the nmethod produced by this task)
    task->set_code_handle(&result_handle);
    methodHandle method(thread, task->method());

    // Never compile a method if breakpoints are present in it
    if (method()->number_of_breakpoints() == 0) {
      // Compile the method.
      if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
        invoke_compiler_on_method(task);
      } else {
        // After compilation is disabled, remove remaining methods from queue
        method->clear_queued_for_compilation();
1821
        task->set_failure_reason("compilation is disabled");
D
duke 已提交
1822 1823 1824 1825
      }
    }
  }

1826 1827 1828
  // Shut down compiler runtime
  shutdown_compiler_runtime(thread->compiler(), thread);
}
D
duke 已提交
1829 1830 1831 1832 1833 1834 1835

// ------------------------------------------------------------------
// CompileBroker::init_compiler_thread_log
//
// Set up state required by +LogCompilation.
void CompileBroker::init_compiler_thread_log() {
    CompilerThread* thread = CompilerThread::current();
1836
    char  file_name[4*K];
D
duke 已提交
1837 1838 1839 1840
    FILE* fp = NULL;
    intx thread_id = os::current_thread_id();
    for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) {
      const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL);
1841
      if (dir == NULL) {
1842
        jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log",
1843 1844
                     thread_id, os::current_process_id());
      } else {
1845
        jio_snprintf(file_name, sizeof(file_name),
1846 1847 1848
                     "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir,
                     os::file_separator(), thread_id, os::current_process_id());
      }
D
duke 已提交
1849

1850
      fp = fopen(file_name, "wt");
1851 1852 1853 1854 1855
      if (fp != NULL) {
        if (LogCompilation && Verbose) {
          tty->print_cr("Opening compilation log %s", file_name);
        }
        CompileLog* log = new(ResourceObj::C_HEAP, mtCompiler) CompileLog(file_name, fp, thread_id);
1856 1857 1858 1859
        if (log == NULL) {
          fclose(fp);
          return;
        }
1860
        thread->init_log(log);
D
duke 已提交
1861

1862 1863 1864
        if (xtty != NULL) {
          ttyLocker ttyl;
          // Record any per thread log files
1865
          xtty->elem("thread_logfile thread='" INTX_FORMAT "' filename='%s'", thread_id, file_name);
1866 1867
        }
        return;
D
duke 已提交
1868 1869
      }
    }
1870
    warning("Cannot open log file: %s", file_name);
D
duke 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
}

// ------------------------------------------------------------------
// CompileBroker::set_should_block
//
// Set _should_block.
// Call this from the VM, with Threads_lock held and a safepoint requested.
void CompileBroker::set_should_block() {
  assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
  assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint already");
#ifndef PRODUCT
  if (PrintCompilation && (Verbose || WizardMode))
    tty->print_cr("notifying compiler thread pool to block");
#endif
  _should_block = true;
}

// ------------------------------------------------------------------
// CompileBroker::maybe_block
//
// Call this from the compiler at convenient points, to poll for _should_block.
void CompileBroker::maybe_block() {
  if (_should_block) {
#ifndef PRODUCT
    if (PrintCompilation && (Verbose || WizardMode))
1896
      tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current()));
D
duke 已提交
1897 1898 1899 1900 1901
#endif
    ThreadInVMfromNative tivfn(JavaThread::current());
  }
}

1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
// wrapper for CodeCache::print_summary()
static void codecache_print(bool detailed)
{
  ResourceMark rm;
  stringStream s;
  // Dump code cache  into a buffer before locking the tty,
  {
    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
    CodeCache::print_summary(&s, detailed);
  }
  ttyLocker ttyl;
1913
  tty->print("%s", s.as_string());
1914 1915
}

D
duke 已提交
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
// ------------------------------------------------------------------
// CompileBroker::invoke_compiler_on_method
//
// Compile a method.
//
void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
  if (PrintCompilation) {
    ResourceMark rm;
    task->print_line();
  }
  elapsedTimer time;

  CompilerThread* thread = CompilerThread::current();
  ResourceMark rm(thread);

1931 1932 1933 1934
  if (LogEvents) {
    _compilation_log->log_compile(thread, task);
  }

D
duke 已提交
1935 1936 1937 1938 1939 1940
  // Common flags.
  uint compile_id = task->compile_id();
  int osr_bci = task->osr_bci();
  bool is_osr = (osr_bci != standard_entry_bci);
  bool should_log = (thread->log() != NULL);
  bool should_break = false;
1941
  int task_level = task->comp_level();
D
duke 已提交
1942 1943 1944 1945 1946
  {
    // create the handle inside it's own block so it can't
    // accidentally be referenced once the thread transitions to
    // native.  The NoHandleMark before the transition should catch
    // any cases where this occurs in the future.
1947
    methodHandle method(thread, task->method());
D
duke 已提交
1948 1949 1950 1951 1952 1953 1954
    should_break = check_break_at(method, compile_id, is_osr);
    if (should_log && !CompilerOracle::should_log(method)) {
      should_log = false;
    }
    assert(!method->is_native(), "no longer compile natives");

    // Save information about this method in case of failure.
1955
    set_last_compile(thread, method, is_osr, task_level);
D
duke 已提交
1956

1957
    DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level));
D
duke 已提交
1958 1959 1960 1961
  }

  // Allocate a new set of JNI handles.
  push_jni_handle_block();
1962
  Method* target_handle = task->method();
D
duke 已提交
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
  int compilable = ciEnv::MethodCompilable;
  {
    int system_dictionary_modification_counter;
    {
      MutexLocker locker(Compile_lock, thread);
      system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
    }

    NoHandleMark  nhm;
    ThreadToNativeFromVM ttn(thread);

    ciEnv ci_env(task, system_dictionary_modification_counter);
    if (should_break) {
      ci_env.set_break_at_compile(true);
    }
    if (should_log) {
      ci_env.set_log(thread->log());
    }
    assert(thread->env() == &ci_env, "set by ci_env");
    // The thread-env() field is cleared in ~CompileTaskWrapper.

1984 1985 1986 1987 1988 1989
    // Cache Jvmti state
    ci_env.cache_jvmti_state();

    // Cache DTrace flags
    ci_env.cache_dtrace_flags();

D
duke 已提交
1990 1991 1992
    ciMethod* target = ci_env.get_method_from_handle(target_handle);

    TraceTime t1("compilation", &time);
S
sla 已提交
1993
    EventCompilation event;
D
duke 已提交
1994

1995 1996 1997 1998 1999 2000
    AbstractCompiler *comp = compiler(task_level);
    if (comp == NULL) {
      ci_env.record_method_not_compilable("no compiler", !TieredCompilation);
    } else {
      comp->compile_method(&ci_env, target, osr_bci);
    }
D
duke 已提交
2001 2002 2003 2004 2005

    if (!ci_env.failing() && task->code() == NULL) {
      //assert(false, "compiler should always document failure");
      // The compiler elected, without comment, not to register a result.
      // Do not attempt further compilations of this method.
2006
      ci_env.record_method_not_compilable("compile failed", !TieredCompilation);
D
duke 已提交
2007 2008
    }

2009 2010 2011
    // Copy this bit to the enclosing block:
    compilable = ci_env.compilable();

D
duke 已提交
2012
    if (ci_env.failing()) {
2013
      task->set_failure_reason(ci_env.failure_reason());
2014 2015 2016 2017
      const char* retry_message = ci_env.retry_message();
      if (_compilation_log != NULL) {
        _compilation_log->log_failure(thread, task, ci_env.failure_reason(), retry_message);
      }
D
duke 已提交
2018
      if (PrintCompilation) {
2019 2020 2021 2022
        FormatBufferResource msg = retry_message != NULL ?
            err_msg_res("COMPILE SKIPPED: %s (%s)", ci_env.failure_reason(), retry_message) :
            err_msg_res("COMPILE SKIPPED: %s",      ci_env.failure_reason());
        task->print_compilation(tty, msg);
D
duke 已提交
2023 2024 2025 2026
      }
    } else {
      task->mark_success();
      task->set_num_inlined_bytecodes(ci_env.num_inlined_bytecodes());
2027 2028 2029 2030 2031 2032
      if (_compilation_log != NULL) {
        nmethod* code = task->code();
        if (code != NULL) {
          _compilation_log->log_nmethod(thread, code);
        }
      }
D
duke 已提交
2033
    }
2034 2035
    // simulate crash during compilation
    assert(task->compile_id() != CICrashAt, "just as planned");
S
sla 已提交
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
    if (event.should_commit()) {
      event.set_method(target->get_Method());
      event.set_compileID(compile_id);
      event.set_compileLevel(task->comp_level());
      event.set_succeded(task->is_success());
      event.set_isOsr(is_osr);
      event.set_codeSize((task->code() == NULL) ? 0 : task->code()->total_size());
      event.set_inlinedBytes(task->num_inlined_bytecodes());
      event.commit();
    }
D
duke 已提交
2046 2047 2048
  }
  pop_jni_handle_block();

2049
  methodHandle method(thread, task->method());
D
duke 已提交
2050

2051
  DTRACE_METHOD_COMPILE_END_PROBE(method, compiler_name(task_level), task->is_success());
D
duke 已提交
2052 2053 2054

  collect_statistics(thread, time, task);

2055
  if (PrintCompilation && PrintCompilation2) {
K
kvn 已提交
2056 2057 2058
    tty->print("%7d ", (int) tty->time_stamp().milliseconds());  // print timestamp
    tty->print("%4d ", compile_id);    // print compilation number
    tty->print("%s ", (is_osr ? "%" : " "));
2059 2060 2061 2062
    if (task->code() != NULL) {
      tty->print("size: %d(%d) ", task->code()->total_size(), task->code()->insts_size());
    }
    tty->print_cr("time: %d inlined: %d bytes", (int)time.milliseconds(), task->num_inlined_bytecodes());
K
kvn 已提交
2063 2064
  }

2065 2066 2067
  if (PrintCodeCacheOnCompilation)
    codecache_print(/* detailed= */ false);

2068 2069 2070 2071 2072 2073
  // Disable compilation, if required.
  switch (compilable) {
  case ciEnv::MethodCompilable_never:
    if (is_osr)
      method->set_not_osr_compilable_quietly();
    else
2074
      method->set_not_compilable_quietly();
2075 2076 2077
    break;
  case ciEnv::MethodCompilable_not_at_tier:
    if (is_osr)
2078
      method->set_not_osr_compilable_quietly(task_level);
2079
    else
2080
      method->set_not_compilable_quietly(task_level);
2081
    break;
D
duke 已提交
2082 2083 2084 2085
  }

  // Note that the queued_for_compilation bits are cleared without
  // protection of a mutex. [They were set by the requester thread,
2086
  // when adding the task to the compile queue -- at which time the
D
duke 已提交
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
  // compile queue lock was held. Subsequently, we acquired the compile
  // queue lock to get this task off the compile queue; thus (to belabour
  // the point somewhat) our clearing of the bits must be occurring
  // only after the setting of the bits. See also 14012000 above.
  method->clear_queued_for_compilation();

#ifdef ASSERT
  if (CollectedHeap::fired_fake_oom()) {
    // The current compile received a fake OOM during compilation so
    // go ahead and exit the VM since the test apparently succeeded
    tty->print_cr("*** Shutting down VM after successful fake OOM");
    vm_exit(0);
  }
#endif
}

2103 2104 2105 2106
/**
 * The CodeCache is full.  Print out warning and disable compilation
 * or try code cache cleaning so compilation can continue later.
 */
2107 2108 2109
void CompileBroker::handle_full_code_cache() {
  UseInterpreter = true;
  if (UseCompiler || AlwaysCompileLoopMethods ) {
2110
    if (xtty != NULL) {
2111
      ResourceMark rm;
2112 2113 2114 2115 2116 2117
      stringStream s;
      // Dump code cache state into a buffer before locking the tty,
      // because log_state() will use locks causing lock conflicts.
      CodeCache::log_state(&s);
      // Lock to prevent tearing
      ttyLocker ttyl;
2118
      xtty->begin_elem("code_cache_full");
2119
      xtty->print("%s", s.as_string());
2120 2121
      xtty->stamp();
      xtty->end_elem();
2122
    }
S
sla 已提交
2123 2124 2125

    CodeCache::report_codemem_full();

2126
#ifndef PRODUCT
2127
    if (CompileTheWorld || ExitOnFullCodeCache) {
2128
      codecache_print(/* detailed= */ true);
2129 2130 2131 2132
      before_exit(JavaThread::current());
      exit_globals(); // will delete tty
      vm_direct_exit(CompileTheWorld ? 0 : 1);
    }
2133
#endif
2134
    if (UseCodeCacheFlushing) {
2135 2136 2137 2138
      // Since code cache is full, immediately stop new compiles
      if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) {
        NMethodSweeper::log_sweep("disable_compiler");
      }
2139 2140 2141 2142
      // Switch to 'vm_state'. This ensures that possibly_sweep() can be called
      // without having to consider the state in which the current thread is.
      ThreadInVMfromUnknown in_vm;
      NMethodSweeper::possibly_sweep();
2143
    } else {
2144
      disable_compilation_forever();
2145
    }
2146 2147 2148 2149 2150 2151 2152

    // Print warning only once
    if (should_print_compiler_warning()) {
      warning("CodeCache is full. Compiler has been disabled.");
      warning("Try increasing the code cache size using -XX:ReservedCodeCacheSize=");
      codecache_print(/* detailed= */ true);
    }
2153 2154 2155
  }
}

D
duke 已提交
2156 2157 2158 2159 2160 2161 2162 2163
// ------------------------------------------------------------------
// CompileBroker::set_last_compile
//
// Record this compilation for debugging purposes.
void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) {
  ResourceMark rm;
  char* method_name = method->name()->as_C_string();
  strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length);
2164
  _last_method_compiled[CompileBroker::name_buffer_length - 1] = '\0'; // ensure null terminated
D
duke 已提交
2165 2166 2167 2168
  char current_method[CompilerCounters::cmname_buffer_length];
  size_t maxLen = CompilerCounters::cmname_buffer_length;

  if (UsePerfData) {
2169
    const char* class_name = method->method_holder()->name()->as_C_string();
D
duke 已提交
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262

    size_t s1len = strlen(class_name);
    size_t s2len = strlen(method_name);

    // check if we need to truncate the string
    if (s1len + s2len + 2 > maxLen) {

      // the strategy is to lop off the leading characters of the
      // class name and the trailing characters of the method name.

      if (s2len + 2 > maxLen) {
        // lop of the entire class name string, let snprintf handle
        // truncation of the method name.
        class_name += s1len; // null string
      }
      else {
        // lop off the extra characters from the front of the class name
        class_name += ((s1len + s2len + 2) - maxLen);
      }
    }

    jio_snprintf(current_method, maxLen, "%s %s", class_name, method_name);
  }

  if (CICountOSR && is_osr) {
    _last_compile_type = osr_compile;
  } else {
    _last_compile_type = normal_compile;
  }
  _last_compile_level = comp_level;

  if (UsePerfData) {
    CompilerCounters* counters = thread->counters();
    counters->set_current_method(current_method);
    counters->set_compile_type((jlong)_last_compile_type);
  }
}


// ------------------------------------------------------------------
// CompileBroker::push_jni_handle_block
//
// Push on a new block of JNI handles.
void CompileBroker::push_jni_handle_block() {
  JavaThread* thread = JavaThread::current();

  // Allocate a new block for JNI handles.
  // Inlined code from jni_PushLocalFrame()
  JNIHandleBlock* java_handles = thread->active_handles();
  JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);
  assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");
  compile_handles->set_pop_frame_link(java_handles);  // make sure java handles get gc'd.
  thread->set_active_handles(compile_handles);
}


// ------------------------------------------------------------------
// CompileBroker::pop_jni_handle_block
//
// Pop off the current block of JNI handles.
void CompileBroker::pop_jni_handle_block() {
  JavaThread* thread = JavaThread::current();

  // Release our JNI handle block
  JNIHandleBlock* compile_handles = thread->active_handles();
  JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
  thread->set_active_handles(java_handles);
  compile_handles->set_pop_frame_link(NULL);
  JNIHandleBlock::release_block(compile_handles, thread); // may block
}


// ------------------------------------------------------------------
// CompileBroker::check_break_at
//
// Should the compilation break at the current compilation.
bool CompileBroker::check_break_at(methodHandle method, int compile_id, bool is_osr) {
  if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) {
    return true;
  } else if( CompilerOracle::should_break_at(method) ) { // break when compiling
    return true;
  } else {
    return (compile_id == CIBreakAt);
  }
}

// ------------------------------------------------------------------
// CompileBroker::collect_statistics
//
// Collect statistics about the compilation.

void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) {
  bool success = task->is_success();
2263
  methodHandle method (thread, task->method());
D
duke 已提交
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
  uint compile_id = task->compile_id();
  bool is_osr = (task->osr_bci() != standard_entry_bci);
  nmethod* code = task->code();
  CompilerCounters* counters = thread->counters();

  assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker");
  MutexLocker locker(CompileStatistics_lock);

  // _perf variables are production performance counters which are
  // updated regardless of the setting of the CITime and CITimeEach flags
  //
  if (!success) {
    _total_bailout_count++;
    if (UsePerfData) {
      _perf_last_failed_method->set_value(counters->current_method());
      _perf_last_failed_type->set_value(counters->compile_type());
      _perf_total_bailout_count->inc();
    }
  } else if (code == NULL) {
    if (UsePerfData) {
      _perf_last_invalidated_method->set_value(counters->current_method());
      _perf_last_invalidated_type->set_value(counters->compile_type());
      _perf_total_invalidated_count->inc();
    }
    _total_invalidated_count++;
  } else {
    // Compilation succeeded

    // update compilation ticks - used by the implementation of
    // java.lang.management.CompilationMBean
    _perf_total_compilation->inc(time.ticks());

S
sla 已提交
2296 2297 2298
    _t_total_compilation.add(time);
    _peak_compilation_time = time.milliseconds() > _peak_compilation_time ? time.milliseconds() : _peak_compilation_time;

D
duke 已提交
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
    if (CITime) {
      if (is_osr) {
        _t_osr_compilation.add(time);
        _sum_osr_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();
      } else {
        _t_standard_compilation.add(time);
        _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();
      }
    }

    if (UsePerfData) {
      // save the name of the last method compiled
      _perf_last_method->set_value(counters->current_method());
      _perf_last_compile_type->set_value(counters->compile_type());
      _perf_last_compile_size->set_value(method->code_size() +
                                         task->num_inlined_bytecodes());
      if (is_osr) {
        _perf_osr_compilation->inc(time.ticks());
        _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
      } else {
        _perf_standard_compilation->inc(time.ticks());
        _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
      }
    }

    if (CITimeEach) {
      float bytes_per_sec = 1.0 * (method->code_size() + task->num_inlined_bytecodes()) / time.seconds();
      tty->print_cr("%3d   seconds: %f bytes/sec : %f (bytes %d + %d inlined)",
                    compile_id, time.seconds(), bytes_per_sec, method->code_size(), task->num_inlined_bytecodes());
    }

    // Collect counts of successful compilations
T
twisti 已提交
2331 2332
    _sum_nmethod_size      += code->total_size();
    _sum_nmethod_code_size += code->insts_size();
D
duke 已提交
2333 2334 2335
    _total_compile_count++;

    if (UsePerfData) {
T
twisti 已提交
2336 2337
      _perf_sum_nmethod_size->inc(     code->total_size());
      _perf_sum_nmethod_code_size->inc(code->insts_size());
D
duke 已提交
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
      _perf_total_compile_count->inc();
    }

    if (is_osr) {
      if (UsePerfData) _perf_total_osr_compile_count->inc();
      _total_osr_compile_count++;
    } else {
      if (UsePerfData) _perf_total_standard_compile_count->inc();
      _total_standard_compile_count++;
    }
  }
  // set the current method for the thread to null
  if (UsePerfData) counters->set_current_method("");
}

2353 2354 2355 2356 2357 2358 2359 2360
const char* CompileBroker::compiler_name(int comp_level) {
  AbstractCompiler *comp = CompileBroker::compiler(comp_level);
  if (comp == NULL) {
    return "no compiler";
  } else {
    return (comp->name());
  }
}
D
duke 已提交
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372

void CompileBroker::print_times() {
  tty->cr();
  tty->print_cr("Accumulated compiler times (for compiled methods only)");
  tty->print_cr("------------------------------------------------");
               //0000000000111111111122222222223333333333444444444455555555556666666666
               //0123456789012345678901234567890123456789012345678901234567890123456789
  tty->print_cr("  Total compilation time   : %6.3f s", CompileBroker::_t_total_compilation.seconds());
  tty->print_cr("    Standard compilation   : %6.3f s, Average : %2.3f",
                CompileBroker::_t_standard_compilation.seconds(),
                CompileBroker::_t_standard_compilation.seconds() / CompileBroker::_total_standard_compile_count);
  tty->print_cr("    On stack replacement   : %6.3f s, Average : %2.3f", CompileBroker::_t_osr_compilation.seconds(), CompileBroker::_t_osr_compilation.seconds() / CompileBroker::_total_osr_compile_count);
T
twisti 已提交
2373

2374 2375 2376
  AbstractCompiler *comp = compiler(CompLevel_simple);
  if (comp != NULL) {
    comp->print_timers();
I
iveresov 已提交
2377
  }
2378 2379 2380
  comp = compiler(CompLevel_full_optimization);
  if (comp != NULL) {
    comp->print_timers();
D
duke 已提交
2381 2382
  }
  tty->cr();
2383 2384 2385
  tty->print_cr("  Total compiled methods   : %6d methods", CompileBroker::_total_compile_count);
  tty->print_cr("    Standard compilation   : %6d methods", CompileBroker::_total_standard_compile_count);
  tty->print_cr("    On stack replacement   : %6d methods", CompileBroker::_total_osr_compile_count);
D
duke 已提交
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
  int tcb = CompileBroker::_sum_osr_bytes_compiled + CompileBroker::_sum_standard_bytes_compiled;
  tty->print_cr("  Total compiled bytecodes : %6d bytes", tcb);
  tty->print_cr("    Standard compilation   : %6d bytes", CompileBroker::_sum_standard_bytes_compiled);
  tty->print_cr("    On stack replacement   : %6d bytes", CompileBroker::_sum_osr_bytes_compiled);
  int bps = (int)(tcb / CompileBroker::_t_total_compilation.seconds());
  tty->print_cr("  Average compilation speed: %6d bytes/s", bps);
  tty->cr();
  tty->print_cr("  nmethod code size        : %6d bytes", CompileBroker::_sum_nmethod_code_size);
  tty->print_cr("  nmethod total size       : %6d bytes", CompileBroker::_sum_nmethod_size);
}

// Debugging output for failure
void CompileBroker::print_last_compile() {
  if ( _last_compile_level != CompLevel_none &&
       compiler(_last_compile_level) != NULL &&
       _last_method_compiled != NULL &&
       _last_compile_type != no_compile) {
    if (_last_compile_type == osr_compile) {
      tty->print_cr("Last parse:  [osr]%d+++(%d) %s",
                    _osr_compilation_id, _last_compile_level, _last_method_compiled);
    } else {
      tty->print_cr("Last parse:  %d+++(%d) %s",
                    _compilation_id, _last_compile_level, _last_method_compiled);
    }
  }
}


void CompileBroker::print_compiler_threads_on(outputStream* st) {
#ifndef PRODUCT
  st->print_cr("Compiler thread printing unimplemented.");
  st->cr();
#endif
}