src/share/vm/compiler/compileBroker.cpp

changeset 435
a61af66fc99e
child 1215
c96bf21b756f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/compiler/compileBroker.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,1857 @@
     1.4 +/*
     1.5 + * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "incls/_precompiled.incl"
    1.29 +#include "incls/_compileBroker.cpp.incl"
    1.30 +
    1.31 +#ifdef DTRACE_ENABLED
    1.32 +
    1.33 +// Only bother with this argument setup if dtrace is available
    1.34 +
    1.35 +HS_DTRACE_PROBE_DECL8(hotspot, method__compile__begin,
    1.36 +  char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t);
    1.37 +HS_DTRACE_PROBE_DECL9(hotspot, method__compile__end,
    1.38 +  char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t, bool);
    1.39 +
    1.40 +#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(compiler, method)              \
    1.41 +  {                                                                      \
    1.42 +    char* comp_name = (char*)(compiler)->name();                         \
    1.43 +    symbolOop klass_name = (method)->klass_name();                       \
    1.44 +    symbolOop name = (method)->name();                                   \
    1.45 +    symbolOop signature = (method)->signature();                         \
    1.46 +    HS_DTRACE_PROBE8(hotspot, method__compile__begin,                    \
    1.47 +      comp_name, strlen(comp_name),                                      \
    1.48 +      klass_name->bytes(), klass_name->utf8_length(),                    \
    1.49 +      name->bytes(), name->utf8_length(),                                \
    1.50 +      signature->bytes(), signature->utf8_length());                     \
    1.51 +  }
    1.52 +
    1.53 +#define DTRACE_METHOD_COMPILE_END_PROBE(compiler, method, success)       \
    1.54 +  {                                                                      \
    1.55 +    char* comp_name = (char*)(compiler)->name();                         \
    1.56 +    symbolOop klass_name = (method)->klass_name();                       \
    1.57 +    symbolOop name = (method)->name();                                   \
    1.58 +    symbolOop signature = (method)->signature();                         \
    1.59 +    HS_DTRACE_PROBE9(hotspot, method__compile__end,                      \
    1.60 +      comp_name, strlen(comp_name),                                      \
    1.61 +      klass_name->bytes(), klass_name->utf8_length(),                    \
    1.62 +      name->bytes(), name->utf8_length(),                                \
    1.63 +      signature->bytes(), signature->utf8_length(), (success));          \
    1.64 +  }
    1.65 +
    1.66 +#else //  ndef DTRACE_ENABLED
    1.67 +
    1.68 +#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(compiler, method)
    1.69 +#define DTRACE_METHOD_COMPILE_END_PROBE(compiler, method, success)
    1.70 +
    1.71 +#endif // ndef DTRACE_ENABLED
    1.72 +
    1.73 +bool CompileBroker::_initialized = false;
    1.74 +volatile bool CompileBroker::_should_block = false;
    1.75 +
    1.76 +// The installed compiler(s)
    1.77 +AbstractCompiler* CompileBroker::_compilers[2];
    1.78 +
    1.79 +// These counters are used for assigning id's to each compilation
    1.80 +uint CompileBroker::_compilation_id        = 0;
    1.81 +uint CompileBroker::_osr_compilation_id    = 0;
    1.82 +
    1.83 +// Debugging information
    1.84 +int  CompileBroker::_last_compile_type     = no_compile;
    1.85 +int  CompileBroker::_last_compile_level    = CompLevel_none;
    1.86 +char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];
    1.87 +
    1.88 +// Performance counters
    1.89 +PerfCounter* CompileBroker::_perf_total_compilation = NULL;
    1.90 +PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
    1.91 +PerfCounter* CompileBroker::_perf_standard_compilation = NULL;
    1.92 +
    1.93 +PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
    1.94 +PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
    1.95 +PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
    1.96 +PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
    1.97 +PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;
    1.98 +
    1.99 +PerfCounter* CompileBroker::_perf_sum_osr_bytes_compiled = NULL;
   1.100 +PerfCounter* CompileBroker::_perf_sum_standard_bytes_compiled = NULL;
   1.101 +PerfCounter* CompileBroker::_perf_sum_nmethod_size = NULL;
   1.102 +PerfCounter* CompileBroker::_perf_sum_nmethod_code_size = NULL;
   1.103 +
   1.104 +PerfStringVariable* CompileBroker::_perf_last_method = NULL;
   1.105 +PerfStringVariable* CompileBroker::_perf_last_failed_method = NULL;
   1.106 +PerfStringVariable* CompileBroker::_perf_last_invalidated_method = NULL;
   1.107 +PerfVariable*       CompileBroker::_perf_last_compile_type = NULL;
   1.108 +PerfVariable*       CompileBroker::_perf_last_compile_size = NULL;
   1.109 +PerfVariable*       CompileBroker::_perf_last_failed_type = NULL;
   1.110 +PerfVariable*       CompileBroker::_perf_last_invalidated_type = NULL;
   1.111 +
   1.112 +// Timers and counters for generating statistics
   1.113 +elapsedTimer CompileBroker::_t_total_compilation;
   1.114 +elapsedTimer CompileBroker::_t_osr_compilation;
   1.115 +elapsedTimer CompileBroker::_t_standard_compilation;
   1.116 +
   1.117 +int CompileBroker::_total_bailout_count          = 0;
   1.118 +int CompileBroker::_total_invalidated_count      = 0;
   1.119 +int CompileBroker::_total_compile_count          = 0;
   1.120 +int CompileBroker::_total_osr_compile_count      = 0;
   1.121 +int CompileBroker::_total_standard_compile_count = 0;
   1.122 +
   1.123 +int CompileBroker::_sum_osr_bytes_compiled       = 0;
   1.124 +int CompileBroker::_sum_standard_bytes_compiled  = 0;
   1.125 +int CompileBroker::_sum_nmethod_size             = 0;
   1.126 +int CompileBroker::_sum_nmethod_code_size        = 0;
   1.127 +
   1.128 +CompileQueue* CompileBroker::_method_queue   = NULL;
   1.129 +CompileTask*  CompileBroker::_task_free_list = NULL;
   1.130 +
   1.131 +GrowableArray<CompilerThread*>* CompileBroker::_method_threads = NULL;
   1.132 +
   1.133 +// CompileTaskWrapper
   1.134 +//
   1.135 +// Assign this task to the current thread.  Deallocate the task
   1.136 +// when the compilation is complete.
   1.137 +class CompileTaskWrapper : StackObj {
   1.138 +public:
   1.139 +  CompileTaskWrapper(CompileTask* task);
   1.140 +  ~CompileTaskWrapper();
   1.141 +};
   1.142 +
   1.143 +CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) {
   1.144 +  CompilerThread* thread = CompilerThread::current();
   1.145 +  thread->set_task(task);
   1.146 +  CompileLog*     log  = thread->log();
   1.147 +  if (log != NULL)  task->log_task_start(log);
   1.148 +}
   1.149 +
   1.150 +CompileTaskWrapper::~CompileTaskWrapper() {
   1.151 +  CompilerThread* thread = CompilerThread::current();
   1.152 +  CompileTask* task = thread->task();
   1.153 +  CompileLog*  log  = thread->log();
   1.154 +  if (log != NULL)  task->log_task_done(log);
   1.155 +  thread->set_task(NULL);
   1.156 +  task->set_code_handle(NULL);
   1.157 +  DEBUG_ONLY(thread->set_env((ciEnv*)badAddress));
   1.158 +  if (task->is_blocking()) {
   1.159 +    MutexLocker notifier(task->lock(), thread);
   1.160 +    task->mark_complete();
   1.161 +    // Notify the waiting thread that the compilation has completed.
   1.162 +    task->lock()->notify_all();
   1.163 +  } else {
   1.164 +    task->mark_complete();
   1.165 +
   1.166 +    // By convention, the compiling thread is responsible for
   1.167 +    // recycling a non-blocking CompileTask.
   1.168 +    CompileBroker::free_task(task);
   1.169 +  }
   1.170 +}
   1.171 +
   1.172 +
   1.173 +// ------------------------------------------------------------------
   1.174 +// CompileTask::initialize
   1.175 +void CompileTask::initialize(int compile_id,
   1.176 +                             methodHandle method,
   1.177 +                             int osr_bci,
   1.178 +                             int comp_level,
   1.179 +                             methodHandle hot_method,
   1.180 +                             int hot_count,
   1.181 +                             const char* comment,
   1.182 +                             bool is_blocking) {
   1.183 +  assert(!_lock->is_locked(), "bad locking");
   1.184 +
   1.185 +  _compile_id = compile_id;
   1.186 +  _method = JNIHandles::make_global(method);
   1.187 +  _osr_bci = osr_bci;
   1.188 +  _is_blocking = is_blocking;
   1.189 +  _comp_level = comp_level;
   1.190 +  _num_inlined_bytecodes = 0;
   1.191 +
   1.192 +  _is_complete = false;
   1.193 +  _is_success = false;
   1.194 +  _code_handle = NULL;
   1.195 +
   1.196 +  _hot_method = NULL;
   1.197 +  _hot_count = hot_count;
   1.198 +  _time_queued = 0;  // tidy
   1.199 +  _comment = comment;
   1.200 +
   1.201 +  if (LogCompilation) {
   1.202 +    _time_queued = os::elapsed_counter();
   1.203 +    if (hot_method.not_null()) {
   1.204 +      if (hot_method == method) {
   1.205 +        _hot_method = _method;
   1.206 +      } else {
   1.207 +        _hot_method = JNIHandles::make_global(hot_method);
   1.208 +      }
   1.209 +    }
   1.210 +  }
   1.211 +
   1.212 +  _next = NULL;
   1.213 +}
   1.214 +
   1.215 +// ------------------------------------------------------------------
   1.216 +// CompileTask::code/set_code
   1.217 +nmethod* CompileTask::code() const {
   1.218 +  if (_code_handle == NULL)  return NULL;
   1.219 +  return _code_handle->code();
   1.220 +}
   1.221 +void CompileTask::set_code(nmethod* nm) {
   1.222 +  if (_code_handle == NULL && nm == NULL)  return;
   1.223 +  guarantee(_code_handle != NULL, "");
   1.224 +  _code_handle->set_code(nm);
   1.225 +  if (nm == NULL)  _code_handle = NULL;  // drop the handle also
   1.226 +}
   1.227 +
   1.228 +// ------------------------------------------------------------------
   1.229 +// CompileTask::free
   1.230 +void CompileTask::free() {
   1.231 +  set_code(NULL);
   1.232 +  assert(!_lock->is_locked(), "Should not be locked when freed");
   1.233 +  if (_hot_method != NULL && _hot_method != _method) {
   1.234 +    JNIHandles::destroy_global(_hot_method);
   1.235 +  }
   1.236 +  JNIHandles::destroy_global(_method);
   1.237 +}
   1.238 +
   1.239 +
   1.240 +// ------------------------------------------------------------------
   1.241 +// CompileTask::print
   1.242 +void CompileTask::print() {
   1.243 +  tty->print("<CompileTask compile_id=%d ", _compile_id);
   1.244 +  tty->print("method=");
   1.245 +  ((methodOop)JNIHandles::resolve(_method))->print_name(tty);
   1.246 +  tty->print_cr(" osr_bci=%d is_blocking=%s is_complete=%s is_success=%s>",
   1.247 +             _osr_bci, bool_to_str(_is_blocking),
   1.248 +             bool_to_str(_is_complete), bool_to_str(_is_success));
   1.249 +}
   1.250 +
   1.251 +// ------------------------------------------------------------------
   1.252 +// CompileTask::print_line_on_error
   1.253 +//
   1.254 +// This function is called by fatal error handler when the thread
   1.255 +// causing troubles is a compiler thread.
   1.256 +//
   1.257 +// Do not grab any lock, do not allocate memory.
   1.258 +//
   1.259 +// Otherwise it's the same as CompileTask::print_line()
   1.260 +//
   1.261 +void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) {
   1.262 +  methodOop method = (methodOop)JNIHandles::resolve(_method);
   1.263 +
   1.264 +  // print compiler name
   1.265 +  st->print("%s:", CompileBroker::compiler(comp_level())->name());
   1.266 +
   1.267 +  // print compilation number
   1.268 +  st->print("%3d", compile_id());
   1.269 +
   1.270 +  // print method attributes
   1.271 +  const bool is_osr = osr_bci() != CompileBroker::standard_entry_bci;
   1.272 +  { const char blocking_char  = is_blocking()                      ? 'b' : ' ';
   1.273 +    const char compile_type   = is_osr                             ? '%' : ' ';
   1.274 +    const char sync_char      = method->is_synchronized()          ? 's' : ' ';
   1.275 +    const char exception_char = method->has_exception_handler()    ? '!' : ' ';
   1.276 +    const char tier_char      =
   1.277 +      is_highest_tier_compile(comp_level())                        ? ' ' : ('0' + comp_level());
   1.278 +    st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, tier_char);
   1.279 +  }
   1.280 +
   1.281 +  // Use buf to get method name and signature
   1.282 +  if (method != NULL) st->print("%s", method->name_and_sig_as_C_string(buf, buflen));
   1.283 +
   1.284 +  // print osr_bci if any
   1.285 +  if (is_osr) st->print(" @ %d", osr_bci());
   1.286 +
   1.287 +  // print method size
   1.288 +  st->print_cr(" (%d bytes)", method->code_size());
   1.289 +}
   1.290 +
   1.291 +// ------------------------------------------------------------------
   1.292 +// CompileTask::print_line
   1.293 +void CompileTask::print_line() {
   1.294 +  Thread *thread = Thread::current();
   1.295 +  methodHandle method(thread,
   1.296 +                      (methodOop)JNIHandles::resolve(method_handle()));
   1.297 +  ResourceMark rm(thread);
   1.298 +
   1.299 +  ttyLocker ttyl;  // keep the following output all in one block
   1.300 +
   1.301 +  // print compiler name if requested
   1.302 +  if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler(comp_level())->name());
   1.303 +
   1.304 +  // print compilation number
   1.305 +  tty->print("%3d", compile_id());
   1.306 +
   1.307 +  // print method attributes
   1.308 +  const bool is_osr = osr_bci() != CompileBroker::standard_entry_bci;
   1.309 +  { const char blocking_char  = is_blocking()                      ? 'b' : ' ';
   1.310 +    const char compile_type   = is_osr                             ? '%' : ' ';
   1.311 +    const char sync_char      = method->is_synchronized()          ? 's' : ' ';
   1.312 +    const char exception_char = method->has_exception_handler()    ? '!' : ' ';
   1.313 +    const char tier_char      =
   1.314 +      is_highest_tier_compile(comp_level())                        ? ' ' : ('0' + comp_level());
   1.315 +    tty->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, tier_char);
   1.316 +  }
   1.317 +
   1.318 +  // print method name
   1.319 +  method->print_short_name(tty);
   1.320 +
   1.321 +  // print osr_bci if any
   1.322 +  if (is_osr) tty->print(" @ %d", osr_bci());
   1.323 +
   1.324 +  // print method size
   1.325 +  tty->print_cr(" (%d bytes)", method->code_size());
   1.326 +}
   1.327 +
   1.328 +
   1.329 +// ------------------------------------------------------------------
   1.330 +// CompileTask::log_task
   1.331 +void CompileTask::log_task(xmlStream* log) {
   1.332 +  Thread* thread = Thread::current();
   1.333 +  methodHandle method(thread,
   1.334 +                      (methodOop)JNIHandles::resolve(method_handle()));
   1.335 +  ResourceMark rm(thread);
   1.336 +
   1.337 +  // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'>
   1.338 +  if (_compile_id != 0)   log->print(" compile_id='%d'", _compile_id);
   1.339 +  if (_osr_bci != CompileBroker::standard_entry_bci) {
   1.340 +    log->print(" compile_kind='osr'");  // same as nmethod::compile_kind
   1.341 +  } // else compile_kind='c2c'
   1.342 +  if (!method.is_null())  log->method(method);
   1.343 +  if (_osr_bci != CompileBroker::standard_entry_bci) {
   1.344 +    log->print(" osr_bci='%d'", _osr_bci);
   1.345 +  }
   1.346 +  if (_comp_level != CompLevel_highest_tier) {
   1.347 +    log->print(" level='%d'", _comp_level);
   1.348 +  }
   1.349 +  if (_is_blocking) {
   1.350 +    log->print(" blocking='1'");
   1.351 +  }
   1.352 +  log->stamp();
   1.353 +}
   1.354 +
   1.355 +
   1.356 +// ------------------------------------------------------------------
   1.357 +// CompileTask::log_task_queued
   1.358 +void CompileTask::log_task_queued() {
   1.359 +  Thread* thread = Thread::current();
   1.360 +  ttyLocker ttyl;
   1.361 +  ResourceMark rm(thread);
   1.362 +
   1.363 +  xtty->begin_elem("task_queued");
   1.364 +  log_task(xtty);
   1.365 +  if (_comment != NULL) {
   1.366 +    xtty->print(" comment='%s'", _comment);
   1.367 +  }
   1.368 +  if (_hot_method != NULL) {
   1.369 +    methodHandle hot(thread,
   1.370 +                     (methodOop)JNIHandles::resolve(_hot_method));
   1.371 +    methodHandle method(thread,
   1.372 +                        (methodOop)JNIHandles::resolve(_method));
   1.373 +    if (hot() != method()) {
   1.374 +      xtty->method(hot);
   1.375 +    }
   1.376 +  }
   1.377 +  if (_hot_count != 0) {
   1.378 +    xtty->print(" hot_count='%d'", _hot_count);
   1.379 +  }
   1.380 +  xtty->end_elem();
   1.381 +}
   1.382 +
   1.383 +
   1.384 +// ------------------------------------------------------------------
   1.385 +// CompileTask::log_task_start
   1.386 +void CompileTask::log_task_start(CompileLog* log)   {
   1.387 +  log->begin_head("task");
   1.388 +  log_task(log);
   1.389 +  log->end_head();
   1.390 +}
   1.391 +
   1.392 +
   1.393 +// ------------------------------------------------------------------
   1.394 +// CompileTask::log_task_done
   1.395 +void CompileTask::log_task_done(CompileLog* log) {
   1.396 +  Thread* thread = Thread::current();
   1.397 +  methodHandle method(thread,
   1.398 +                      (methodOop)JNIHandles::resolve(method_handle()));
   1.399 +  ResourceMark rm(thread);
   1.400 +
   1.401 +  // <task_done ... stamp='1.234'>  </task>
   1.402 +  nmethod* nm = code();
   1.403 +  log->begin_elem("task_done success='%d' nmsize='%d' count='%d'",
   1.404 +                  _is_success, nm == NULL ? 0 : nm->instructions_size(),
   1.405 +                  method->invocation_count());
   1.406 +  int bec = method->backedge_count();
   1.407 +  if (bec != 0)  log->print(" backedge_count='%d'", bec);
   1.408 +  // Note:  "_is_complete" is about to be set, but is not.
   1.409 +  if (_num_inlined_bytecodes != 0) {
   1.410 +    log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);
   1.411 +  }
   1.412 +  log->stamp();
   1.413 +  log->end_elem();
   1.414 +  log->tail("task");
   1.415 +  log->clear_identities();   // next task will have different CI
   1.416 +  if (log->unflushed_count() > 2000) {
   1.417 +    log->flush();
   1.418 +  }
   1.419 +  log->mark_file_end();
   1.420 +}
   1.421 +
   1.422 +
   1.423 +
   1.424 +// ------------------------------------------------------------------
   1.425 +// CompileQueue::add
   1.426 +//
   1.427 +// Add a CompileTask to a CompileQueue
   1.428 +void CompileQueue::add(CompileTask* task) {
   1.429 +  assert(lock()->owned_by_self(), "must own lock");
   1.430 +
   1.431 +  task->set_next(NULL);
   1.432 +
   1.433 +  if (_last == NULL) {
   1.434 +    // The compile queue is empty.
   1.435 +    assert(_first == NULL, "queue is empty");
   1.436 +    _first = task;
   1.437 +    _last = task;
   1.438 +  } else {
   1.439 +    // Append the task to the queue.
   1.440 +    assert(_last->next() == NULL, "not last");
   1.441 +    _last->set_next(task);
   1.442 +    _last = task;
   1.443 +  }
   1.444 +
   1.445 +  // Mark the method as being in the compile queue.
   1.446 +  ((methodOop)JNIHandles::resolve(task->method_handle()))->set_queued_for_compilation();
   1.447 +
   1.448 +  if (CIPrintCompileQueue) {
   1.449 +    print();
   1.450 +  }
   1.451 +
   1.452 +  if (LogCompilation && xtty != NULL) {
   1.453 +    task->log_task_queued();
   1.454 +  }
   1.455 +
   1.456 +  // Notify CompilerThreads that a task is available.
   1.457 +  lock()->notify();
   1.458 +}
   1.459 +
   1.460 +
   1.461 +// ------------------------------------------------------------------
   1.462 +// CompileQueue::get
   1.463 +//
   1.464 +// Get the next CompileTask from a CompileQueue
   1.465 +CompileTask* CompileQueue::get() {
   1.466 +  MutexLocker locker(lock());
   1.467 +
   1.468 +  // Wait for an available CompileTask.
   1.469 +  while (_first == NULL) {
   1.470 +    // There is no work to be done right now.  Wait.
   1.471 +    lock()->wait();
   1.472 +  }
   1.473 +
   1.474 +  CompileTask* task = _first;
   1.475 +
   1.476 +  // Update queue first and last
   1.477 +  _first =_first->next();
   1.478 +  if (_first == NULL) {
   1.479 +    _last = NULL;
   1.480 +  }
   1.481 +
   1.482 +  return task;
   1.483 +
   1.484 +}
   1.485 +
   1.486 +
   1.487 +// ------------------------------------------------------------------
   1.488 +// CompileQueue::print
   1.489 +void CompileQueue::print() {
   1.490 +  tty->print_cr("Contents of %s", name());
   1.491 +  tty->print_cr("----------------------");
   1.492 +  CompileTask* task = _first;
   1.493 +  while (task != NULL) {
   1.494 +    task->print_line();
   1.495 +    task = task->next();
   1.496 +  }
   1.497 +  tty->print_cr("----------------------");
   1.498 +}
   1.499 +
   1.500 +CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) {
   1.501 +
   1.502 +  _current_method[0] = '\0';
   1.503 +  _compile_type = CompileBroker::no_compile;
   1.504 +
   1.505 +  if (UsePerfData) {
   1.506 +    ResourceMark rm;
   1.507 +
   1.508 +    // create the thread instance name space string - don't create an
   1.509 +    // instance subspace if instance is -1 - keeps the adapterThread
   1.510 +    // counters  from having a ".0" namespace.
   1.511 +    const char* thread_i = (instance == -1) ? thread_name :
   1.512 +                      PerfDataManager::name_space(thread_name, instance);
   1.513 +
   1.514 +
   1.515 +    char* name = PerfDataManager::counter_name(thread_i, "method");
   1.516 +    _perf_current_method =
   1.517 +               PerfDataManager::create_string_variable(SUN_CI, name,
   1.518 +                                                       cmname_buffer_length,
   1.519 +                                                       _current_method, CHECK);
   1.520 +
   1.521 +    name = PerfDataManager::counter_name(thread_i, "type");
   1.522 +    _perf_compile_type = PerfDataManager::create_variable(SUN_CI, name,
   1.523 +                                                          PerfData::U_None,
   1.524 +                                                         (jlong)_compile_type,
   1.525 +                                                          CHECK);
   1.526 +
   1.527 +    name = PerfDataManager::counter_name(thread_i, "time");
   1.528 +    _perf_time = PerfDataManager::create_counter(SUN_CI, name,
   1.529 +                                                 PerfData::U_Ticks, CHECK);
   1.530 +
   1.531 +    name = PerfDataManager::counter_name(thread_i, "compiles");
   1.532 +    _perf_compiles = PerfDataManager::create_counter(SUN_CI, name,
   1.533 +                                                     PerfData::U_Events, CHECK);
   1.534 +  }
   1.535 +}
   1.536 +
   1.537 +
   1.538 +// ------------------------------------------------------------------
   1.539 +// CompileBroker::compilation_init
   1.540 +//
   1.541 +// Initialize the Compilation object
   1.542 +void CompileBroker::compilation_init() {
   1.543 +  _last_method_compiled[0] = '\0';
   1.544 +
   1.545 +  // Set the interface to the current compiler(s).
   1.546 +#ifdef COMPILER1
   1.547 +  _compilers[0] = new Compiler();
   1.548 +#ifndef COMPILER2
   1.549 +  _compilers[1] = _compilers[0];
   1.550 +#endif
   1.551 +#endif // COMPILER1
   1.552 +
   1.553 +#ifdef COMPILER2
   1.554 +  _compilers[1] = new C2Compiler();
   1.555 +#ifndef COMPILER1
   1.556 +  _compilers[0] = _compilers[1];
   1.557 +#endif
   1.558 +#endif // COMPILER2
   1.559 +
   1.560 +  // Initialize the CompileTask free list
   1.561 +  _task_free_list = NULL;
   1.562 +
   1.563 +  // Start the CompilerThreads
   1.564 +  init_compiler_threads(compiler_count());
   1.565 +
   1.566 +
   1.567 +  // totalTime performance counter is always created as it is required
   1.568 +  // by the implementation of java.lang.management.CompilationMBean.
   1.569 +  {
   1.570 +    EXCEPTION_MARK;
   1.571 +    _perf_total_compilation =
   1.572 +                 PerfDataManager::create_counter(JAVA_CI, "totalTime",
   1.573 +                                                 PerfData::U_Ticks, CHECK);
   1.574 +  }
   1.575 +
   1.576 +
   1.577 +  if (UsePerfData) {
   1.578 +
   1.579 +    EXCEPTION_MARK;
   1.580 +
   1.581 +    // create the jvmstat performance counters
   1.582 +    _perf_osr_compilation =
   1.583 +                 PerfDataManager::create_counter(SUN_CI, "osrTime",
   1.584 +                                                 PerfData::U_Ticks, CHECK);
   1.585 +
   1.586 +    _perf_standard_compilation =
   1.587 +                 PerfDataManager::create_counter(SUN_CI, "standardTime",
   1.588 +                                                 PerfData::U_Ticks, CHECK);
   1.589 +
   1.590 +    _perf_total_bailout_count =
   1.591 +                 PerfDataManager::create_counter(SUN_CI, "totalBailouts",
   1.592 +                                                 PerfData::U_Events, CHECK);
   1.593 +
   1.594 +    _perf_total_invalidated_count =
   1.595 +                 PerfDataManager::create_counter(SUN_CI, "totalInvalidates",
   1.596 +                                                 PerfData::U_Events, CHECK);
   1.597 +
   1.598 +    _perf_total_compile_count =
   1.599 +                 PerfDataManager::create_counter(SUN_CI, "totalCompiles",
   1.600 +                                                 PerfData::U_Events, CHECK);
   1.601 +    _perf_total_osr_compile_count =
   1.602 +                 PerfDataManager::create_counter(SUN_CI, "osrCompiles",
   1.603 +                                                 PerfData::U_Events, CHECK);
   1.604 +
   1.605 +    _perf_total_standard_compile_count =
   1.606 +                 PerfDataManager::create_counter(SUN_CI, "standardCompiles",
   1.607 +                                                 PerfData::U_Events, CHECK);
   1.608 +
   1.609 +    _perf_sum_osr_bytes_compiled =
   1.610 +                 PerfDataManager::create_counter(SUN_CI, "osrBytes",
   1.611 +                                                 PerfData::U_Bytes, CHECK);
   1.612 +
   1.613 +    _perf_sum_standard_bytes_compiled =
   1.614 +                 PerfDataManager::create_counter(SUN_CI, "standardBytes",
   1.615 +                                                 PerfData::U_Bytes, CHECK);
   1.616 +
   1.617 +    _perf_sum_nmethod_size =
   1.618 +                 PerfDataManager::create_counter(SUN_CI, "nmethodSize",
   1.619 +                                                 PerfData::U_Bytes, CHECK);
   1.620 +
   1.621 +    _perf_sum_nmethod_code_size =
   1.622 +                 PerfDataManager::create_counter(SUN_CI, "nmethodCodeSize",
   1.623 +                                                 PerfData::U_Bytes, CHECK);
   1.624 +
   1.625 +    _perf_last_method =
   1.626 +                 PerfDataManager::create_string_variable(SUN_CI, "lastMethod",
   1.627 +                                       CompilerCounters::cmname_buffer_length,
   1.628 +                                       "", CHECK);
   1.629 +
   1.630 +    _perf_last_failed_method =
   1.631 +            PerfDataManager::create_string_variable(SUN_CI, "lastFailedMethod",
   1.632 +                                       CompilerCounters::cmname_buffer_length,
   1.633 +                                       "", CHECK);
   1.634 +
   1.635 +    _perf_last_invalidated_method =
   1.636 +        PerfDataManager::create_string_variable(SUN_CI, "lastInvalidatedMethod",
   1.637 +                                     CompilerCounters::cmname_buffer_length,
   1.638 +                                     "", CHECK);
   1.639 +
   1.640 +    _perf_last_compile_type =
   1.641 +             PerfDataManager::create_variable(SUN_CI, "lastType",
   1.642 +                                              PerfData::U_None,
   1.643 +                                              (jlong)CompileBroker::no_compile,
   1.644 +                                              CHECK);
   1.645 +
   1.646 +    _perf_last_compile_size =
   1.647 +             PerfDataManager::create_variable(SUN_CI, "lastSize",
   1.648 +                                              PerfData::U_Bytes,
   1.649 +                                              (jlong)CompileBroker::no_compile,
   1.650 +                                              CHECK);
   1.651 +
   1.652 +
   1.653 +    _perf_last_failed_type =
   1.654 +             PerfDataManager::create_variable(SUN_CI, "lastFailedType",
   1.655 +                                              PerfData::U_None,
   1.656 +                                              (jlong)CompileBroker::no_compile,
   1.657 +                                              CHECK);
   1.658 +
   1.659 +    _perf_last_invalidated_type =
   1.660 +         PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
   1.661 +                                          PerfData::U_None,
   1.662 +                                          (jlong)CompileBroker::no_compile,
   1.663 +                                          CHECK);
   1.664 +  }
   1.665 +
   1.666 +  _initialized = true;
   1.667 +}
   1.668 +
   1.669 +
   1.670 +
   1.671 +// ------------------------------------------------------------------
   1.672 +// CompileBroker::make_compiler_thread
   1.673 +CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, TRAPS) {
   1.674 +  CompilerThread* compiler_thread = NULL;
   1.675 +
   1.676 +  klassOop k =
   1.677 +    SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_Thread(),
   1.678 +                                      true, CHECK_0);
   1.679 +  instanceKlassHandle klass (THREAD, k);
   1.680 +  instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
   1.681 +  Handle string = java_lang_String::create_from_str(name, CHECK_0);
   1.682 +
   1.683 +  // Initialize thread_oop to put it into the system threadGroup
   1.684 +  Handle thread_group (THREAD,  Universe::system_thread_group());
   1.685 +  JavaValue result(T_VOID);
   1.686 +  JavaCalls::call_special(&result, thread_oop,
   1.687 +                       klass,
   1.688 +                       vmSymbolHandles::object_initializer_name(),
   1.689 +                       vmSymbolHandles::threadgroup_string_void_signature(),
   1.690 +                       thread_group,
   1.691 +                       string,
   1.692 +                       CHECK_0);
   1.693 +
   1.694 +  {
   1.695 +    MutexLocker mu(Threads_lock, THREAD);
   1.696 +    compiler_thread = new CompilerThread(queue, counters);
   1.697 +    // At this point the new CompilerThread data-races with this startup
   1.698 +    // thread (which I believe is the primoridal thread and NOT the VM
   1.699 +    // thread).  This means Java bytecodes being executed at startup can
   1.700 +    // queue compile jobs which will run at whatever default priority the
   1.701 +    // newly created CompilerThread runs at.
   1.702 +
   1.703 +
   1.704 +    // At this point it may be possible that no osthread was created for the
   1.705 +    // JavaThread due to lack of memory. We would have to throw an exception
   1.706 +    // in that case. However, since this must work and we do not allow
   1.707 +    // exceptions anyway, check and abort if this fails.
   1.708 +
   1.709 +    if (compiler_thread == NULL || compiler_thread->osthread() == NULL){
   1.710 +      vm_exit_during_initialization("java.lang.OutOfMemoryError",
   1.711 +                                    "unable to create new native thread");
   1.712 +    }
   1.713 +
   1.714 +    java_lang_Thread::set_thread(thread_oop(), compiler_thread);
   1.715 +
   1.716 +    // Note that this only sets the JavaThread _priority field, which by
   1.717 +    // definition is limited to Java priorities and not OS priorities.
   1.718 +    // The os-priority is set in the CompilerThread startup code itself
   1.719 +    java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
   1.720 +    // CLEANUP PRIORITIES: This -if- statement hids a bug whereby the compiler
   1.721 +    // threads never have their OS priority set.  The assumption here is to
   1.722 +    // enable the Performance group to do flag tuning, figure out a suitable
   1.723 +    // CompilerThreadPriority, and then remove this 'if' statement (and
   1.724 +    // comment) and unconditionally set the priority.
   1.725 +
   1.726 +    // Compiler Threads should be at the highest Priority
   1.727 +    if ( CompilerThreadPriority != -1 )
   1.728 +      os::set_native_priority( compiler_thread, CompilerThreadPriority );
   1.729 +    else
   1.730 +      os::set_native_priority( compiler_thread, os::java_to_os_priority[NearMaxPriority]);
   1.731 +
   1.732 +      // Note that I cannot call os::set_priority because it expects Java
   1.733 +      // priorities and I am *explicitly* using OS priorities so that it's
   1.734 +      // possible to set the compiler thread priority higher than any Java
   1.735 +      // thread.
   1.736 +
   1.737 +    java_lang_Thread::set_daemon(thread_oop());
   1.738 +
   1.739 +    compiler_thread->set_threadObj(thread_oop());
   1.740 +    Threads::add(compiler_thread);
   1.741 +    Thread::start(compiler_thread);
   1.742 +  }
   1.743 +  // Let go of Threads_lock before yielding
   1.744 +  os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
   1.745 +
   1.746 +  return compiler_thread;
   1.747 +}
   1.748 +
   1.749 +
   1.750 +// ------------------------------------------------------------------
   1.751 +// CompileBroker::init_compiler_threads
   1.752 +//
   1.753 +// Initialize the compilation queue
   1.754 +void CompileBroker::init_compiler_threads(int compiler_count) {
   1.755 +  EXCEPTION_MARK;
   1.756 +
   1.757 +  _method_queue  = new CompileQueue("MethodQueue",  MethodCompileQueue_lock);
   1.758 +  _method_threads =
   1.759 +    new (ResourceObj::C_HEAP) GrowableArray<CompilerThread*>(compiler_count, true);
   1.760 +
   1.761 +  char name_buffer[256];
   1.762 +  int i;
   1.763 +  for (i = 0; i < compiler_count; i++) {
   1.764 +    // Create a name for our thread.
   1.765 +    sprintf(name_buffer, "CompilerThread%d", i);
   1.766 +    CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
   1.767 +
   1.768 +    CompilerThread* new_thread = make_compiler_thread(name_buffer, _method_queue, counters, CHECK);
   1.769 +    _method_threads->append(new_thread);
   1.770 +  }
   1.771 +  if (UsePerfData) {
   1.772 +    PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes,
   1.773 +                                     compiler_count, CHECK);
   1.774 +  }
   1.775 +}
   1.776 +
   1.777 +// ------------------------------------------------------------------
   1.778 +// CompileBroker::is_idle
   1.779 +bool CompileBroker::is_idle() {
   1.780 +  if (!_method_queue->is_empty()) {
   1.781 +    return false;
   1.782 +  } else {
   1.783 +    int num_threads = _method_threads->length();
   1.784 +    for (int i=0; i<num_threads; i++) {
   1.785 +      if (_method_threads->at(i)->task() != NULL) {
   1.786 +        return false;
   1.787 +      }
   1.788 +    }
   1.789 +
   1.790 +    // No pending or active compilations.
   1.791 +    return true;
   1.792 +  }
   1.793 +}
   1.794 +
   1.795 +
   1.796 +// ------------------------------------------------------------------
   1.797 +// CompileBroker::compile_method
   1.798 +//
   1.799 +// Request compilation of a method.
   1.800 +void CompileBroker::compile_method_base(methodHandle method,
   1.801 +                                        int osr_bci,
   1.802 +                                        int comp_level,
   1.803 +                                        methodHandle hot_method,
   1.804 +                                        int hot_count,
   1.805 +                                        const char* comment,
   1.806 +                                        TRAPS) {
   1.807 +  // do nothing if compiler thread(s) is not available
   1.808 +  if (!_initialized ) {
   1.809 +    return;
   1.810 +  }
   1.811 +
   1.812 +  guarantee(!method->is_abstract(), "cannot compile abstract methods");
   1.813 +  assert(method->method_holder()->klass_part()->oop_is_instance(),
   1.814 +         "sanity check");
   1.815 +  assert(!instanceKlass::cast(method->method_holder())->is_not_initialized(),
   1.816 +         "method holder must be initialized");
   1.817 +
   1.818 +  if (CIPrintRequests) {
   1.819 +    tty->print("request: ");
   1.820 +    method->print_short_name(tty);
   1.821 +    if (osr_bci != InvocationEntryBci) {
   1.822 +      tty->print(" osr_bci: %d", osr_bci);
   1.823 +    }
   1.824 +    tty->print(" comment: %s count: %d", comment, hot_count);
   1.825 +    if (!hot_method.is_null()) {
   1.826 +      tty->print(" hot: ");
   1.827 +      if (hot_method() != method()) {
   1.828 +          hot_method->print_short_name(tty);
   1.829 +      } else {
   1.830 +        tty->print("yes");
   1.831 +      }
   1.832 +    }
   1.833 +    tty->cr();
   1.834 +  }
   1.835 +
   1.836 +  // A request has been made for compilation.  Before we do any
   1.837 +  // real work, check to see if the method has been compiled
   1.838 +  // in the meantime with a definitive result.
   1.839 +  if (compilation_is_complete(method, osr_bci, comp_level)) {
   1.840 +    return;
   1.841 +  }
   1.842 +
   1.843 +  // If this method is already in the compile queue, then
   1.844 +  // we do not block the current thread.
   1.845 +  if (compilation_is_in_queue(method, osr_bci)) {
   1.846 +    // We may want to decay our counter a bit here to prevent
   1.847 +    // multiple denied requests for compilation.  This is an
   1.848 +    // open compilation policy issue. Note: The other possibility,
   1.849 +    // in the case that this is a blocking compile request, is to have
   1.850 +    // all subsequent blocking requesters wait for completion of
   1.851 +    // ongoing compiles. Note that in this case we'll need a protocol
   1.852 +    // for freeing the associated compile tasks. [Or we could have
   1.853 +    // a single static monitor on which all these waiters sleep.]
   1.854 +    return;
   1.855 +  }
   1.856 +
   1.857 +  // Outputs from the following MutexLocker block:
   1.858 +  CompileTask* task     = NULL;
   1.859 +  bool         blocking = false;
   1.860 +
   1.861 +  // Acquire our lock.
   1.862 +  {
   1.863 +    MutexLocker locker(_method_queue->lock(), THREAD);
   1.864 +
   1.865 +    // Make sure the method has not slipped into the queues since
   1.866 +    // last we checked; note that those checks were "fast bail-outs".
   1.867 +    // Here we need to be more careful, see 14012000 below.
   1.868 +    if (compilation_is_in_queue(method, osr_bci)) {
   1.869 +      return;
   1.870 +    }
   1.871 +
   1.872 +    // We need to check again to see if the compilation has
   1.873 +    // completed.  A previous compilation may have registered
   1.874 +    // some result.
   1.875 +    if (compilation_is_complete(method, osr_bci, comp_level)) {
   1.876 +      return;
   1.877 +    }
   1.878 +
   1.879 +    // We now know that this compilation is not pending, complete,
   1.880 +    // or prohibited.  Assign a compile_id to this compilation
   1.881 +    // and check to see if it is in our [Start..Stop) range.
   1.882 +    uint compile_id = assign_compile_id(method, osr_bci);
   1.883 +    if (compile_id == 0) {
   1.884 +      // The compilation falls outside the allowed range.
   1.885 +      return;
   1.886 +    }
   1.887 +
   1.888 +    // Should this thread wait for completion of the compile?
   1.889 +    blocking = is_compile_blocking(method, osr_bci);
   1.890 +
   1.891 +    // We will enter the compilation in the queue.
   1.892 +    // 14012000: Note that this sets the queued_for_compile bits in
   1.893 +    // the target method. We can now reason that a method cannot be
   1.894 +    // queued for compilation more than once, as follows:
   1.895 +    // Before a thread queues a task for compilation, it first acquires
   1.896 +    // the compile queue lock, then checks if the method's queued bits
   1.897 +    // are set or it has already been compiled. Thus there can not be two
   1.898 +    // instances of a compilation task for the same method on the
   1.899 +    // compilation queue. Consider now the case where the compilation
   1.900 +    // thread has already removed a task for that method from the queue
   1.901 +    // and is in the midst of compiling it. In this case, the
   1.902 +    // queued_for_compile bits must be set in the method (and these
   1.903 +    // will be visible to the current thread, since the bits were set
   1.904 +    // under protection of the compile queue lock, which we hold now.
   1.905 +    // When the compilation completes, the compiler thread first sets
   1.906 +    // the compilation result and then clears the queued_for_compile
   1.907 +    // bits. Neither of these actions are protected by a barrier (or done
   1.908 +    // under the protection of a lock), so the only guarantee we have
   1.909 +    // (on machines with TSO (Total Store Order)) is that these values
   1.910 +    // will update in that order. As a result, the only combinations of
   1.911 +    // these bits that the current thread will see are, in temporal order:
   1.912 +    // <RESULT, QUEUE> :
   1.913 +    //     <0, 1> : in compile queue, but not yet compiled
   1.914 +    //     <1, 1> : compiled but queue bit not cleared
   1.915 +    //     <1, 0> : compiled and queue bit cleared
   1.916 +    // Because we first check the queue bits then check the result bits,
   1.917 +    // we are assured that we cannot introduce a duplicate task.
   1.918 +    // Note that if we did the tests in the reverse order (i.e. check
   1.919 +    // result then check queued bit), we could get the result bit before
   1.920 +    // the compilation completed, and the queue bit after the compilation
   1.921 +    // completed, and end up introducing a "duplicate" (redundant) task.
   1.922 +    // In that case, the compiler thread should first check if a method
   1.923 +    // has already been compiled before trying to compile it.
   1.924 +    // NOTE: in the event that there are multiple compiler threads and
   1.925 +    // there is de-optimization/recompilation, things will get hairy,
   1.926 +    // and in that case it's best to protect both the testing (here) of
   1.927 +    // these bits, and their updating (here and elsewhere) under a
   1.928 +    // common lock.
   1.929 +    task = create_compile_task(_method_queue,
   1.930 +                               compile_id, method,
   1.931 +                               osr_bci, comp_level,
   1.932 +                               hot_method, hot_count, comment,
   1.933 +                               blocking);
   1.934 +  }
   1.935 +
   1.936 +  if (blocking) {
   1.937 +    wait_for_completion(task);
   1.938 +  }
   1.939 +}
   1.940 +
   1.941 +
   1.942 +nmethod* CompileBroker::compile_method(methodHandle method, int osr_bci,
   1.943 +                                       methodHandle hot_method, int hot_count,
   1.944 +                                       const char* comment, TRAPS) {
   1.945 +  // make sure arguments make sense
   1.946 +  assert(method->method_holder()->klass_part()->oop_is_instance(), "not an instance method");
   1.947 +  assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range");
   1.948 +  assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods");
   1.949 +  assert(!instanceKlass::cast(method->method_holder())->is_not_initialized(), "method holder must be initialized");
   1.950 +
   1.951 +  int comp_level = CompilationPolicy::policy()->compilation_level(method, osr_bci);
   1.952 +
   1.953 +#ifdef TIERED
   1.954 +  if (TieredCompilation && StressTieredRuntime) {
   1.955 +    static int flipper = 0;
   1.956 +    if (is_even(flipper++)) {
   1.957 +      comp_level = CompLevel_fast_compile;
   1.958 +    } else {
   1.959 +      comp_level = CompLevel_full_optimization;
   1.960 +    }
   1.961 +  }
   1.962 +#ifdef SPARC
   1.963 +  // QQQ FIX ME
   1.964 +  // C2 only returns long results in G1 and c1 doesn't understand so disallow c2
   1.965 +  // compiles of long results
   1.966 +  if (TieredCompilation && method()->result_type() == T_LONG) {
   1.967 +    comp_level = CompLevel_fast_compile;
   1.968 +  }
   1.969 +#endif // SPARC
   1.970 +#endif // TIERED
   1.971 +
   1.972 +  // return quickly if possible
   1.973 +
   1.974 +  // lock, make sure that the compilation
   1.975 +  // isn't prohibited in a straightforward way.
   1.976 +
   1.977 +  if (compiler(comp_level) == NULL || compilation_is_prohibited(method, osr_bci, comp_level)) {
   1.978 +    return NULL;
   1.979 +  }
   1.980 +
   1.981 +  if (osr_bci == InvocationEntryBci) {
   1.982 +    // standard compilation
   1.983 +    nmethod* method_code = method->code();
   1.984 +    if (method_code != NULL
   1.985 +#ifdef TIERED
   1.986 +       && ( method_code->is_compiled_by_c2() || comp_level == CompLevel_fast_compile )
   1.987 +#endif // TIERED
   1.988 +      ) {
   1.989 +      return method_code;
   1.990 +    }
   1.991 +    if (method->is_not_compilable(comp_level)) return NULL;
   1.992 +  } else {
   1.993 +    // osr compilation
   1.994 +#ifndef TIERED
   1.995 +    // seems like an assert of dubious value
   1.996 +    assert(comp_level == CompLevel_full_optimization,
   1.997 +           "all OSR compiles are assumed to be at a single compilation lavel");
   1.998 +#endif // TIERED
   1.999 +    nmethod* nm = method->lookup_osr_nmethod_for(osr_bci);
  1.1000 +    if (nm != NULL) return nm;
  1.1001 +    if (method->is_not_osr_compilable()) return NULL;
  1.1002 +  }
  1.1003 +
  1.1004 +  assert(!HAS_PENDING_EXCEPTION, "No exception should be present");
  1.1005 +  // some prerequisites that are compiler specific
  1.1006 +  if (compiler(comp_level)->is_c2()) {
  1.1007 +    method->constants()->resolve_string_constants(CHECK_0);
  1.1008 +    // Resolve all classes seen in the signature of the method
  1.1009 +    // we are compiling.
  1.1010 +    methodOopDesc::load_signature_classes(method, CHECK_0);
  1.1011 +  }
  1.1012 +
  1.1013 +  // If the method is native, do the lookup in the thread requesting
  1.1014 +  // the compilation. Native lookups can load code, which is not
  1.1015 +  // permitted during compilation.
  1.1016 +  //
  1.1017 +  // Note: A native method implies non-osr compilation which is
  1.1018 +  //       checked with an assertion at the entry of this method.
  1.1019 +  if (method->is_native()) {
  1.1020 +    bool in_base_library;
  1.1021 +    address adr = NativeLookup::lookup(method, in_base_library, THREAD);
  1.1022 +    if (HAS_PENDING_EXCEPTION) {
  1.1023 +      // In case of an exception looking up the method, we just forget
  1.1024 +      // about it. The interpreter will kick-in and throw the exception.
  1.1025 +      method->set_not_compilable(); // implies is_not_osr_compilable()
  1.1026 +      CLEAR_PENDING_EXCEPTION;
  1.1027 +      return NULL;
  1.1028 +    }
  1.1029 +    assert(method->has_native_function(), "must have native code by now");
  1.1030 +  }
  1.1031 +
  1.1032 +  // RedefineClasses() has replaced this method; just return
  1.1033 +  if (method->is_old()) {
  1.1034 +    return NULL;
  1.1035 +  }
  1.1036 +
  1.1037 +  // JVMTI -- post_compile_event requires jmethod_id() that may require
  1.1038 +  // a lock the compiling thread can not acquire. Prefetch it here.
  1.1039 +  if (JvmtiExport::should_post_compiled_method_load()) {
  1.1040 +    method->jmethod_id();
  1.1041 +  }
  1.1042 +
  1.1043 +  // do the compilation
  1.1044 +  if (method->is_native()) {
  1.1045 +    if (!PreferInterpreterNativeStubs) {
  1.1046 +      (void) AdapterHandlerLibrary::create_native_wrapper(method);
  1.1047 +    } else {
  1.1048 +      return NULL;
  1.1049 +    }
  1.1050 +  } else {
  1.1051 +    compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, CHECK_0);
  1.1052 +  }
  1.1053 +
  1.1054 +  // return requested nmethod
  1.1055 +  return osr_bci  == InvocationEntryBci ? method->code() : method->lookup_osr_nmethod_for(osr_bci);
  1.1056 +}
  1.1057 +
  1.1058 +
  1.1059 +// ------------------------------------------------------------------
  1.1060 +// CompileBroker::compilation_is_complete
  1.1061 +//
  1.1062 +// See if compilation of this method is already complete.
  1.1063 +bool CompileBroker::compilation_is_complete(methodHandle method,
  1.1064 +                                            int          osr_bci,
  1.1065 +                                            int          comp_level) {
  1.1066 +  bool is_osr = (osr_bci != standard_entry_bci);
  1.1067 +  if (is_osr) {
  1.1068 +    if (method->is_not_osr_compilable()) {
  1.1069 +      return true;
  1.1070 +    } else {
  1.1071 +      nmethod* result = method->lookup_osr_nmethod_for(osr_bci);
  1.1072 +      return (result != NULL);
  1.1073 +    }
  1.1074 +  } else {
  1.1075 +    if (method->is_not_compilable(comp_level)) {
  1.1076 +      return true;
  1.1077 +    } else {
  1.1078 +      nmethod* result = method->code();
  1.1079 +      if (result == NULL) return false;
  1.1080 +#ifdef TIERED
  1.1081 +      if (comp_level == CompLevel_fast_compile) {
  1.1082 +        // At worst the code is from c1
  1.1083 +        return true;
  1.1084 +      }
  1.1085 +      // comp level must be full opt
  1.1086 +      return result->is_compiled_by_c2();
  1.1087 +#endif // TIERED
  1.1088 +      return true;
  1.1089 +    }
  1.1090 +  }
  1.1091 +}
  1.1092 +
  1.1093 +
  1.1094 +// ------------------------------------------------------------------
  1.1095 +// CompileBroker::compilation_is_in_queue
  1.1096 +//
  1.1097 +// See if this compilation is already requested.
  1.1098 +//
  1.1099 +// Implementation note: there is only a single "is in queue" bit
  1.1100 +// for each method.  This means that the check below is overly
  1.1101 +// conservative in the sense that an osr compilation in the queue
  1.1102 +// will block a normal compilation from entering the queue (and vice
  1.1103 +// versa).  This can be remedied by a full queue search to disambiguate
  1.1104 +// cases.  If it is deemed profitible, this may be done.
  1.1105 +bool CompileBroker::compilation_is_in_queue(methodHandle method,
  1.1106 +                                          int          osr_bci) {
  1.1107 +  return method->queued_for_compilation();
  1.1108 +}
  1.1109 +
  1.1110 +
  1.1111 +// ------------------------------------------------------------------
  1.1112 +// CompileBroker::compilation_is_prohibited
  1.1113 +//
  1.1114 +// See if this compilation is not allowed.
  1.1115 +bool CompileBroker::compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level) {
  1.1116 +  bool is_native = method->is_native();
  1.1117 +  // Some compilers may not support the compilation of natives.
  1.1118 +  // QQQ this needs some work ought to only record not compilable at
  1.1119 +  // the specified level
  1.1120 +  if (is_native &&
  1.1121 +      (!CICompileNatives || !compiler(comp_level)->supports_native())) {
  1.1122 +    method->set_not_compilable();
  1.1123 +    return true;
  1.1124 +  }
  1.1125 +
  1.1126 +  bool is_osr = (osr_bci != standard_entry_bci);
  1.1127 +  // Some compilers may not support on stack replacement.
  1.1128 +  if (is_osr &&
  1.1129 +      (!CICompileOSR || !compiler(comp_level)->supports_osr())) {
  1.1130 +    method->set_not_osr_compilable();
  1.1131 +    return true;
  1.1132 +  }
  1.1133 +
  1.1134 +  // The method may be explicitly excluded by the user.
  1.1135 +  bool quietly;
  1.1136 +  if (CompilerOracle::should_exclude(method, quietly)) {
  1.1137 +    if (!quietly) {
  1.1138 +      // This does not happen quietly...
  1.1139 +      ResourceMark rm;
  1.1140 +      tty->print("### Excluding %s:%s",
  1.1141 +                 method->is_native() ? "generation of native wrapper" : "compile",
  1.1142 +                 (method->is_static() ? " static" : ""));
  1.1143 +      method->print_short_name(tty);
  1.1144 +      tty->cr();
  1.1145 +    }
  1.1146 +    method->set_not_compilable();
  1.1147 +  }
  1.1148 +
  1.1149 +  return false;
  1.1150 +}
  1.1151 +
  1.1152 +
  1.1153 +// ------------------------------------------------------------------
  1.1154 +// CompileBroker::assign_compile_id
  1.1155 +//
  1.1156 +// Assign a serialized id number to this compilation request.  If the
  1.1157 +// number falls out of the allowed range, return a 0.  OSR
  1.1158 +// compilations may be numbered separately from regular compilations
  1.1159 +// if certain debugging flags are used.
  1.1160 +uint CompileBroker::assign_compile_id(methodHandle method, int osr_bci) {
  1.1161 +  assert(_method_queue->lock()->owner() == JavaThread::current(),
  1.1162 +         "must hold the compilation queue lock");
  1.1163 +  bool is_osr = (osr_bci != standard_entry_bci);
  1.1164 +  assert(!method->is_native(), "no longer compile natives");
  1.1165 +  uint id;
  1.1166 +  if (CICountOSR && is_osr) {
  1.1167 +    id = ++_osr_compilation_id;
  1.1168 +    if ((uint)CIStartOSR <= id && id < (uint)CIStopOSR) {
  1.1169 +      return id;
  1.1170 +    }
  1.1171 +  } else {
  1.1172 +    id = ++_compilation_id;
  1.1173 +    if ((uint)CIStart <= id && id < (uint)CIStop) {
  1.1174 +      return id;
  1.1175 +    }
  1.1176 +  }
  1.1177 +
  1.1178 +  // Method was not in the appropriate compilation range.
  1.1179 +  method->set_not_compilable();
  1.1180 +  return 0;
  1.1181 +}
  1.1182 +
  1.1183 +
  1.1184 +// ------------------------------------------------------------------
  1.1185 +// CompileBroker::is_compile_blocking
  1.1186 +//
  1.1187 +// Should the current thread be blocked until this compilation request
  1.1188 +// has been fulfilled?
  1.1189 +bool CompileBroker::is_compile_blocking(methodHandle method, int osr_bci) {
  1.1190 +  return !BackgroundCompilation;
  1.1191 +}
  1.1192 +
  1.1193 +
  1.1194 +// ------------------------------------------------------------------
  1.1195 +// CompileBroker::preload_classes
  1.1196 +void CompileBroker::preload_classes(methodHandle method, TRAPS) {
  1.1197 +  // Move this code over from c1_Compiler.cpp
  1.1198 +  ShouldNotReachHere();
  1.1199 +}
  1.1200 +
  1.1201 +
  1.1202 +// ------------------------------------------------------------------
  1.1203 +// CompileBroker::create_compile_task
  1.1204 +//
  1.1205 +// Create a CompileTask object representing the current request for
  1.1206 +// compilation.  Add this task to the queue.
  1.1207 +CompileTask* CompileBroker::create_compile_task(CompileQueue* queue,
  1.1208 +                                              int           compile_id,
  1.1209 +                                              methodHandle  method,
  1.1210 +                                              int           osr_bci,
  1.1211 +                                              int           comp_level,
  1.1212 +                                              methodHandle  hot_method,
  1.1213 +                                              int           hot_count,
  1.1214 +                                              const char*   comment,
  1.1215 +                                              bool          blocking) {
  1.1216 +  CompileTask* new_task = allocate_task();
  1.1217 +  new_task->initialize(compile_id, method, osr_bci, comp_level,
  1.1218 +                       hot_method, hot_count, comment,
  1.1219 +                       blocking);
  1.1220 +  queue->add(new_task);
  1.1221 +  return new_task;
  1.1222 +}
  1.1223 +
  1.1224 +
  1.1225 +// ------------------------------------------------------------------
  1.1226 +// CompileBroker::allocate_task
  1.1227 +//
  1.1228 +// Allocate a CompileTask, from the free list if possible.
  1.1229 +CompileTask* CompileBroker::allocate_task() {
  1.1230 +  MutexLocker locker(CompileTaskAlloc_lock);
  1.1231 +  CompileTask* task = NULL;
  1.1232 +  if (_task_free_list != NULL) {
  1.1233 +    task = _task_free_list;
  1.1234 +    _task_free_list = task->next();
  1.1235 +    task->set_next(NULL);
  1.1236 +  } else {
  1.1237 +    task = new CompileTask();
  1.1238 +    task->set_next(NULL);
  1.1239 +  }
  1.1240 +  return task;
  1.1241 +}
  1.1242 +
  1.1243 +
  1.1244 +// ------------------------------------------------------------------
  1.1245 +// CompileBroker::free_task
  1.1246 +//
  1.1247 +// Add a task to the free list.
  1.1248 +void CompileBroker::free_task(CompileTask* task) {
  1.1249 +  MutexLocker locker(CompileTaskAlloc_lock);
  1.1250 +  task->free();
  1.1251 +  task->set_next(_task_free_list);
  1.1252 +  _task_free_list = task;
  1.1253 +}
  1.1254 +
  1.1255 +
  1.1256 +// ------------------------------------------------------------------
  1.1257 +// CompileBroker::wait_for_completion
  1.1258 +//
  1.1259 +// Wait for the given method CompileTask to complete.
  1.1260 +void CompileBroker::wait_for_completion(CompileTask* task) {
  1.1261 +  if (CIPrintCompileQueue) {
  1.1262 +    tty->print_cr("BLOCKING FOR COMPILE");
  1.1263 +  }
  1.1264 +
  1.1265 +  assert(task->is_blocking(), "can only wait on blocking task");
  1.1266 +
  1.1267 +  JavaThread *thread = JavaThread::current();
  1.1268 +  thread->set_blocked_on_compilation(true);
  1.1269 +
  1.1270 +  methodHandle method(thread,
  1.1271 +                      (methodOop)JNIHandles::resolve(task->method_handle()));
  1.1272 +  {
  1.1273 +    MutexLocker waiter(task->lock(), thread);
  1.1274 +
  1.1275 +    while (!task->is_complete())
  1.1276 +      task->lock()->wait();
  1.1277 +  }
  1.1278 +  // It is harmless to check this status without the lock, because
  1.1279 +  // completion is a stable property (until the task object is recycled).
  1.1280 +  assert(task->is_complete(), "Compilation should have completed");
  1.1281 +  assert(task->code_handle() == NULL, "must be reset");
  1.1282 +
  1.1283 +  thread->set_blocked_on_compilation(false);
  1.1284 +
  1.1285 +  // By convention, the waiter is responsible for recycling a
  1.1286 +  // blocking CompileTask. Since there is only one waiter ever
  1.1287 +  // waiting on a CompileTask, we know that no one else will
  1.1288 +  // be using this CompileTask; we can free it.
  1.1289 +  free_task(task);
  1.1290 +}
  1.1291 +
  1.1292 +// ------------------------------------------------------------------
  1.1293 +// CompileBroker::compiler_thread_loop
  1.1294 +//
  1.1295 +// The main loop run by a CompilerThread.
  1.1296 +void CompileBroker::compiler_thread_loop() {
  1.1297 +  CompilerThread* thread = CompilerThread::current();
  1.1298 +  CompileQueue* queue = thread->queue();
  1.1299 +
  1.1300 +  // For the thread that initializes the ciObjectFactory
  1.1301 +  // this resource mark holds all the shared objects
  1.1302 +  ResourceMark rm;
  1.1303 +
  1.1304 +  // First thread to get here will initialize the compiler interface
  1.1305 +
  1.1306 +  if (!ciObjectFactory::is_initialized()) {
  1.1307 +    ASSERT_IN_VM;
  1.1308 +    MutexLocker only_one (CompileThread_lock, thread);
  1.1309 +    if (!ciObjectFactory::is_initialized()) {
  1.1310 +      ciObjectFactory::initialize();
  1.1311 +    }
  1.1312 +  }
  1.1313 +
  1.1314 +  // Open a log.
  1.1315 +  if (LogCompilation) {
  1.1316 +    init_compiler_thread_log();
  1.1317 +  }
  1.1318 +  CompileLog* log = thread->log();
  1.1319 +  if (log != NULL) {
  1.1320 +    log->begin_elem("start_compile_thread thread='" UINTX_FORMAT "' process='%d'",
  1.1321 +                    os::current_thread_id(),
  1.1322 +                    os::current_process_id());
  1.1323 +    log->stamp();
  1.1324 +    log->end_elem();
  1.1325 +  }
  1.1326 +
  1.1327 +  while (true) {
  1.1328 +    {
  1.1329 +      // We need this HandleMark to avoid leaking VM handles.
  1.1330 +      HandleMark hm(thread);
  1.1331 +      if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) {
  1.1332 +        // The CodeCache is full.  Print out warning and disable compilation.
  1.1333 +        UseInterpreter = true;
  1.1334 +        if (UseCompiler || AlwaysCompileLoopMethods ) {
  1.1335 +          if (log != NULL) {
  1.1336 +            log->begin_elem("code_cache_full");
  1.1337 +            log->stamp();
  1.1338 +            log->end_elem();
  1.1339 +          }
  1.1340 +#ifndef PRODUCT
  1.1341 +          warning("CodeCache is full. Compiler has been disabled");
  1.1342 +          if (CompileTheWorld || ExitOnFullCodeCache) {
  1.1343 +            before_exit(thread);
  1.1344 +            exit_globals(); // will delete tty
  1.1345 +            vm_direct_exit(CompileTheWorld ? 0 : 1);
  1.1346 +          }
  1.1347 +#endif
  1.1348 +          UseCompiler               = false;
  1.1349 +          AlwaysCompileLoopMethods  = false;
  1.1350 +        }
  1.1351 +      }
  1.1352 +
  1.1353 +      CompileTask* task = queue->get();
  1.1354 +
  1.1355 +      // Give compiler threads an extra quanta.  They tend to be bursty and
  1.1356 +      // this helps the compiler to finish up the job.
  1.1357 +      if( CompilerThreadHintNoPreempt )
  1.1358 +        os::hint_no_preempt();
  1.1359 +
  1.1360 +      // trace per thread time and compile statistics
  1.1361 +      CompilerCounters* counters = ((CompilerThread*)thread)->counters();
  1.1362 +      PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter());
  1.1363 +
  1.1364 +      // Assign the task to the current thread.  Mark this compilation
  1.1365 +      // thread as active for the profiler.
  1.1366 +      CompileTaskWrapper ctw(task);
  1.1367 +      nmethodLocker result_handle;  // (handle for the nmethod produced by this task)
  1.1368 +      task->set_code_handle(&result_handle);
  1.1369 +      methodHandle method(thread,
  1.1370 +                     (methodOop)JNIHandles::resolve(task->method_handle()));
  1.1371 +
  1.1372 +      // Never compile a method if breakpoints are present in it
  1.1373 +      if (method()->number_of_breakpoints() == 0) {
  1.1374 +        // Compile the method.
  1.1375 +        if (UseCompiler || AlwaysCompileLoopMethods) {
  1.1376 +#ifdef COMPILER1
  1.1377 +          // Allow repeating compilations for the purpose of benchmarking
  1.1378 +          // compile speed. This is not useful for customers.
  1.1379 +          if (CompilationRepeat != 0) {
  1.1380 +            int compile_count = CompilationRepeat;
  1.1381 +            while (compile_count > 0) {
  1.1382 +              invoke_compiler_on_method(task);
  1.1383 +              nmethod* nm = method->code();
  1.1384 +              if (nm != NULL) {
  1.1385 +                nm->make_zombie();
  1.1386 +                method->clear_code();
  1.1387 +              }
  1.1388 +              compile_count--;
  1.1389 +            }
  1.1390 +          }
  1.1391 +#endif /* COMPILER1 */
  1.1392 +          invoke_compiler_on_method(task);
  1.1393 +        } else {
  1.1394 +          // After compilation is disabled, remove remaining methods from queue
  1.1395 +          method->clear_queued_for_compilation();
  1.1396 +        }
  1.1397 +      }
  1.1398 +    }
  1.1399 +  }
  1.1400 +}
  1.1401 +
  1.1402 +
  1.1403 +// ------------------------------------------------------------------
  1.1404 +// CompileBroker::init_compiler_thread_log
  1.1405 +//
  1.1406 +// Set up state required by +LogCompilation.
  1.1407 +void CompileBroker::init_compiler_thread_log() {
  1.1408 +    CompilerThread* thread = CompilerThread::current();
  1.1409 +    char  fileBuf[4*K];
  1.1410 +    FILE* fp = NULL;
  1.1411 +    char* file = NULL;
  1.1412 +    intx thread_id = os::current_thread_id();
  1.1413 +    for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) {
  1.1414 +      const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL);
  1.1415 +      if (dir == NULL)  dir = "";
  1.1416 +      sprintf(fileBuf, "%shs_c" UINTX_FORMAT "_pid%u.log",
  1.1417 +              dir, thread_id, os::current_process_id());
  1.1418 +      fp = fopen(fileBuf, "at");
  1.1419 +      if (fp != NULL) {
  1.1420 +        file = NEW_C_HEAP_ARRAY(char, strlen(fileBuf)+1);
  1.1421 +        strcpy(file, fileBuf);
  1.1422 +        break;
  1.1423 +      }
  1.1424 +    }
  1.1425 +    if (fp == NULL) {
  1.1426 +      warning("Cannot open log file: %s", fileBuf);
  1.1427 +    } else {
  1.1428 +      if (LogCompilation && Verbose)
  1.1429 +        tty->print_cr("Opening compilation log %s", file);
  1.1430 +      CompileLog* log = new(ResourceObj::C_HEAP) CompileLog(file, fp, thread_id);
  1.1431 +      thread->init_log(log);
  1.1432 +
  1.1433 +      if (xtty != NULL) {
  1.1434 +        ttyLocker ttyl;
  1.1435 +
  1.1436 +        // Record any per thread log files
  1.1437 +        xtty->elem("thread_logfile thread='%d' filename='%s'", thread_id, file);
  1.1438 +      }
  1.1439 +    }
  1.1440 +}
  1.1441 +
  1.1442 +// ------------------------------------------------------------------
  1.1443 +// CompileBroker::set_should_block
  1.1444 +//
  1.1445 +// Set _should_block.
  1.1446 +// Call this from the VM, with Threads_lock held and a safepoint requested.
  1.1447 +void CompileBroker::set_should_block() {
  1.1448 +  assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
  1.1449 +  assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint already");
  1.1450 +#ifndef PRODUCT
  1.1451 +  if (PrintCompilation && (Verbose || WizardMode))
  1.1452 +    tty->print_cr("notifying compiler thread pool to block");
  1.1453 +#endif
  1.1454 +  _should_block = true;
  1.1455 +}
  1.1456 +
  1.1457 +// ------------------------------------------------------------------
  1.1458 +// CompileBroker::maybe_block
  1.1459 +//
  1.1460 +// Call this from the compiler at convenient points, to poll for _should_block.
  1.1461 +void CompileBroker::maybe_block() {
  1.1462 +  if (_should_block) {
  1.1463 +#ifndef PRODUCT
  1.1464 +    if (PrintCompilation && (Verbose || WizardMode))
  1.1465 +      tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", Thread::current());
  1.1466 +#endif
  1.1467 +    ThreadInVMfromNative tivfn(JavaThread::current());
  1.1468 +  }
  1.1469 +}
  1.1470 +
  1.1471 +
  1.1472 +// ------------------------------------------------------------------
  1.1473 +// CompileBroker::invoke_compiler_on_method
  1.1474 +//
  1.1475 +// Compile a method.
  1.1476 +//
  1.1477 +void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
  1.1478 +  if (PrintCompilation) {
  1.1479 +    ResourceMark rm;
  1.1480 +    task->print_line();
  1.1481 +  }
  1.1482 +  elapsedTimer time;
  1.1483 +
  1.1484 +  CompilerThread* thread = CompilerThread::current();
  1.1485 +  ResourceMark rm(thread);
  1.1486 +
  1.1487 +  // Common flags.
  1.1488 +  uint compile_id = task->compile_id();
  1.1489 +  int osr_bci = task->osr_bci();
  1.1490 +  bool is_osr = (osr_bci != standard_entry_bci);
  1.1491 +  bool should_log = (thread->log() != NULL);
  1.1492 +  bool should_break = false;
  1.1493 +  {
  1.1494 +    // create the handle inside it's own block so it can't
  1.1495 +    // accidentally be referenced once the thread transitions to
  1.1496 +    // native.  The NoHandleMark before the transition should catch
  1.1497 +    // any cases where this occurs in the future.
  1.1498 +    methodHandle method(thread,
  1.1499 +                        (methodOop)JNIHandles::resolve(task->method_handle()));
  1.1500 +    should_break = check_break_at(method, compile_id, is_osr);
  1.1501 +    if (should_log && !CompilerOracle::should_log(method)) {
  1.1502 +      should_log = false;
  1.1503 +    }
  1.1504 +    assert(!method->is_native(), "no longer compile natives");
  1.1505 +
  1.1506 +    // Save information about this method in case of failure.
  1.1507 +    set_last_compile(thread, method, is_osr, task->comp_level());
  1.1508 +
  1.1509 +    DTRACE_METHOD_COMPILE_BEGIN_PROBE(compiler(task->comp_level()), method);
  1.1510 +  }
  1.1511 +
  1.1512 +  // Allocate a new set of JNI handles.
  1.1513 +  push_jni_handle_block();
  1.1514 +  jobject target_handle = JNIHandles::make_local(thread, JNIHandles::resolve(task->method_handle()));
  1.1515 +  int compilable = ciEnv::MethodCompilable;
  1.1516 +  {
  1.1517 +    int system_dictionary_modification_counter;
  1.1518 +    {
  1.1519 +      MutexLocker locker(Compile_lock, thread);
  1.1520 +      system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
  1.1521 +    }
  1.1522 +
  1.1523 +    NoHandleMark  nhm;
  1.1524 +    ThreadToNativeFromVM ttn(thread);
  1.1525 +
  1.1526 +    ciEnv ci_env(task, system_dictionary_modification_counter);
  1.1527 +    if (should_break) {
  1.1528 +      ci_env.set_break_at_compile(true);
  1.1529 +    }
  1.1530 +    if (should_log) {
  1.1531 +      ci_env.set_log(thread->log());
  1.1532 +    }
  1.1533 +    assert(thread->env() == &ci_env, "set by ci_env");
  1.1534 +    // The thread-env() field is cleared in ~CompileTaskWrapper.
  1.1535 +
  1.1536 +    ciMethod* target = ci_env.get_method_from_handle(target_handle);
  1.1537 +
  1.1538 +    TraceTime t1("compilation", &time);
  1.1539 +
  1.1540 +    compiler(task->comp_level())->compile_method(&ci_env, target, osr_bci);
  1.1541 +
  1.1542 +    if (!ci_env.failing() && task->code() == NULL) {
  1.1543 +      //assert(false, "compiler should always document failure");
  1.1544 +      // The compiler elected, without comment, not to register a result.
  1.1545 +      // Do not attempt further compilations of this method.
  1.1546 +      ci_env.record_method_not_compilable("compile failed");
  1.1547 +    }
  1.1548 +
  1.1549 +    if (ci_env.failing()) {
  1.1550 +      // Copy this bit to the enclosing block:
  1.1551 +      compilable = ci_env.compilable();
  1.1552 +      if (PrintCompilation) {
  1.1553 +        const char* reason = ci_env.failure_reason();
  1.1554 +        if (compilable == ciEnv::MethodCompilable_not_at_tier) {
  1.1555 +          if (is_highest_tier_compile(ci_env.comp_level())) {
  1.1556 +            // Already at highest tier, promote to not compilable.
  1.1557 +            compilable = ciEnv::MethodCompilable_never;
  1.1558 +          } else {
  1.1559 +            tty->print_cr("%3d   COMPILE SKIPPED: %s (retry at different tier)", compile_id, reason);
  1.1560 +          }
  1.1561 +        }
  1.1562 +
  1.1563 +        if (compilable == ciEnv::MethodCompilable_never) {
  1.1564 +          tty->print_cr("%3d   COMPILE SKIPPED: %s (not retryable)", compile_id, reason);
  1.1565 +        } else if (compilable == ciEnv::MethodCompilable) {
  1.1566 +          tty->print_cr("%3d   COMPILE SKIPPED: %s", compile_id, reason);
  1.1567 +        }
  1.1568 +      }
  1.1569 +    } else {
  1.1570 +      task->mark_success();
  1.1571 +      task->set_num_inlined_bytecodes(ci_env.num_inlined_bytecodes());
  1.1572 +    }
  1.1573 +  }
  1.1574 +  pop_jni_handle_block();
  1.1575 +
  1.1576 +  methodHandle method(thread,
  1.1577 +                      (methodOop)JNIHandles::resolve(task->method_handle()));
  1.1578 +
  1.1579 +  DTRACE_METHOD_COMPILE_END_PROBE(compiler(task->comp_level()), method, task->is_success());
  1.1580 +
  1.1581 +  collect_statistics(thread, time, task);
  1.1582 +
  1.1583 +  if (compilable == ciEnv::MethodCompilable_never) {
  1.1584 +    if (is_osr) {
  1.1585 +      method->set_not_osr_compilable();
  1.1586 +    } else {
  1.1587 +      method->set_not_compilable();
  1.1588 +    }
  1.1589 +  } else if (compilable == ciEnv::MethodCompilable_not_at_tier) {
  1.1590 +    method->set_not_compilable(task->comp_level());
  1.1591 +  }
  1.1592 +
  1.1593 +  // Note that the queued_for_compilation bits are cleared without
  1.1594 +  // protection of a mutex. [They were set by the requester thread,
  1.1595 +  // when adding the task to the complie queue -- at which time the
  1.1596 +  // compile queue lock was held. Subsequently, we acquired the compile
  1.1597 +  // queue lock to get this task off the compile queue; thus (to belabour
  1.1598 +  // the point somewhat) our clearing of the bits must be occurring
  1.1599 +  // only after the setting of the bits. See also 14012000 above.
  1.1600 +  method->clear_queued_for_compilation();
  1.1601 +
  1.1602 +#ifdef ASSERT
  1.1603 +  if (CollectedHeap::fired_fake_oom()) {
  1.1604 +    // The current compile received a fake OOM during compilation so
  1.1605 +    // go ahead and exit the VM since the test apparently succeeded
  1.1606 +    tty->print_cr("*** Shutting down VM after successful fake OOM");
  1.1607 +    vm_exit(0);
  1.1608 +  }
  1.1609 +#endif
  1.1610 +}
  1.1611 +
  1.1612 +
  1.1613 +// ------------------------------------------------------------------
  1.1614 +// CompileBroker::set_last_compile
  1.1615 +//
  1.1616 +// Record this compilation for debugging purposes.
  1.1617 +void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) {
  1.1618 +  ResourceMark rm;
  1.1619 +  char* method_name = method->name()->as_C_string();
  1.1620 +  strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length);
  1.1621 +  char current_method[CompilerCounters::cmname_buffer_length];
  1.1622 +  size_t maxLen = CompilerCounters::cmname_buffer_length;
  1.1623 +
  1.1624 +  if (UsePerfData) {
  1.1625 +    const char* class_name = method->method_holder()->klass_part()->name()->as_C_string();
  1.1626 +
  1.1627 +    size_t s1len = strlen(class_name);
  1.1628 +    size_t s2len = strlen(method_name);
  1.1629 +
  1.1630 +    // check if we need to truncate the string
  1.1631 +    if (s1len + s2len + 2 > maxLen) {
  1.1632 +
  1.1633 +      // the strategy is to lop off the leading characters of the
  1.1634 +      // class name and the trailing characters of the method name.
  1.1635 +
  1.1636 +      if (s2len + 2 > maxLen) {
  1.1637 +        // lop of the entire class name string, let snprintf handle
  1.1638 +        // truncation of the method name.
  1.1639 +        class_name += s1len; // null string
  1.1640 +      }
  1.1641 +      else {
  1.1642 +        // lop off the extra characters from the front of the class name
  1.1643 +        class_name += ((s1len + s2len + 2) - maxLen);
  1.1644 +      }
  1.1645 +    }
  1.1646 +
  1.1647 +    jio_snprintf(current_method, maxLen, "%s %s", class_name, method_name);
  1.1648 +  }
  1.1649 +
  1.1650 +  if (CICountOSR && is_osr) {
  1.1651 +    _last_compile_type = osr_compile;
  1.1652 +  } else {
  1.1653 +    _last_compile_type = normal_compile;
  1.1654 +  }
  1.1655 +  _last_compile_level = comp_level;
  1.1656 +
  1.1657 +  if (UsePerfData) {
  1.1658 +    CompilerCounters* counters = thread->counters();
  1.1659 +    counters->set_current_method(current_method);
  1.1660 +    counters->set_compile_type((jlong)_last_compile_type);
  1.1661 +  }
  1.1662 +}
  1.1663 +
  1.1664 +
  1.1665 +// ------------------------------------------------------------------
  1.1666 +// CompileBroker::push_jni_handle_block
  1.1667 +//
  1.1668 +// Push on a new block of JNI handles.
  1.1669 +void CompileBroker::push_jni_handle_block() {
  1.1670 +  JavaThread* thread = JavaThread::current();
  1.1671 +
  1.1672 +  // Allocate a new block for JNI handles.
  1.1673 +  // Inlined code from jni_PushLocalFrame()
  1.1674 +  JNIHandleBlock* java_handles = thread->active_handles();
  1.1675 +  JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);
  1.1676 +  assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");
  1.1677 +  compile_handles->set_pop_frame_link(java_handles);  // make sure java handles get gc'd.
  1.1678 +  thread->set_active_handles(compile_handles);
  1.1679 +}
  1.1680 +
  1.1681 +
  1.1682 +// ------------------------------------------------------------------
  1.1683 +// CompileBroker::pop_jni_handle_block
  1.1684 +//
  1.1685 +// Pop off the current block of JNI handles.
  1.1686 +void CompileBroker::pop_jni_handle_block() {
  1.1687 +  JavaThread* thread = JavaThread::current();
  1.1688 +
  1.1689 +  // Release our JNI handle block
  1.1690 +  JNIHandleBlock* compile_handles = thread->active_handles();
  1.1691 +  JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
  1.1692 +  thread->set_active_handles(java_handles);
  1.1693 +  compile_handles->set_pop_frame_link(NULL);
  1.1694 +  JNIHandleBlock::release_block(compile_handles, thread); // may block
  1.1695 +}
  1.1696 +
  1.1697 +
  1.1698 +// ------------------------------------------------------------------
  1.1699 +// CompileBroker::check_break_at
  1.1700 +//
  1.1701 +// Should the compilation break at the current compilation.
  1.1702 +bool CompileBroker::check_break_at(methodHandle method, int compile_id, bool is_osr) {
  1.1703 +  if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) {
  1.1704 +    return true;
  1.1705 +  } else if( CompilerOracle::should_break_at(method) ) { // break when compiling
  1.1706 +    return true;
  1.1707 +  } else {
  1.1708 +    return (compile_id == CIBreakAt);
  1.1709 +  }
  1.1710 +}
  1.1711 +
  1.1712 +// ------------------------------------------------------------------
  1.1713 +// CompileBroker::collect_statistics
  1.1714 +//
  1.1715 +// Collect statistics about the compilation.
  1.1716 +
  1.1717 +void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) {
  1.1718 +  bool success = task->is_success();
  1.1719 +  methodHandle method (thread, (methodOop)JNIHandles::resolve(task->method_handle()));
  1.1720 +  uint compile_id = task->compile_id();
  1.1721 +  bool is_osr = (task->osr_bci() != standard_entry_bci);
  1.1722 +  nmethod* code = task->code();
  1.1723 +  CompilerCounters* counters = thread->counters();
  1.1724 +
  1.1725 +  assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker");
  1.1726 +  MutexLocker locker(CompileStatistics_lock);
  1.1727 +
  1.1728 +  // _perf variables are production performance counters which are
  1.1729 +  // updated regardless of the setting of the CITime and CITimeEach flags
  1.1730 +  //
  1.1731 +  if (!success) {
  1.1732 +    _total_bailout_count++;
  1.1733 +    if (UsePerfData) {
  1.1734 +      _perf_last_failed_method->set_value(counters->current_method());
  1.1735 +      _perf_last_failed_type->set_value(counters->compile_type());
  1.1736 +      _perf_total_bailout_count->inc();
  1.1737 +    }
  1.1738 +  } else if (code == NULL) {
  1.1739 +    if (UsePerfData) {
  1.1740 +      _perf_last_invalidated_method->set_value(counters->current_method());
  1.1741 +      _perf_last_invalidated_type->set_value(counters->compile_type());
  1.1742 +      _perf_total_invalidated_count->inc();
  1.1743 +    }
  1.1744 +    _total_invalidated_count++;
  1.1745 +  } else {
  1.1746 +    // Compilation succeeded
  1.1747 +
  1.1748 +    // update compilation ticks - used by the implementation of
  1.1749 +    // java.lang.management.CompilationMBean
  1.1750 +    _perf_total_compilation->inc(time.ticks());
  1.1751 +
  1.1752 +    if (CITime) {
  1.1753 +      _t_total_compilation.add(time);
  1.1754 +      if (is_osr) {
  1.1755 +        _t_osr_compilation.add(time);
  1.1756 +        _sum_osr_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();
  1.1757 +      } else {
  1.1758 +        _t_standard_compilation.add(time);
  1.1759 +        _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();
  1.1760 +      }
  1.1761 +    }
  1.1762 +
  1.1763 +    if (UsePerfData) {
  1.1764 +      // save the name of the last method compiled
  1.1765 +      _perf_last_method->set_value(counters->current_method());
  1.1766 +      _perf_last_compile_type->set_value(counters->compile_type());
  1.1767 +      _perf_last_compile_size->set_value(method->code_size() +
  1.1768 +                                         task->num_inlined_bytecodes());
  1.1769 +      if (is_osr) {
  1.1770 +        _perf_osr_compilation->inc(time.ticks());
  1.1771 +        _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
  1.1772 +      } else {
  1.1773 +        _perf_standard_compilation->inc(time.ticks());
  1.1774 +        _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
  1.1775 +      }
  1.1776 +    }
  1.1777 +
  1.1778 +    if (CITimeEach) {
  1.1779 +      float bytes_per_sec = 1.0 * (method->code_size() + task->num_inlined_bytecodes()) / time.seconds();
  1.1780 +      tty->print_cr("%3d   seconds: %f bytes/sec : %f (bytes %d + %d inlined)",
  1.1781 +                    compile_id, time.seconds(), bytes_per_sec, method->code_size(), task->num_inlined_bytecodes());
  1.1782 +    }
  1.1783 +
  1.1784 +    // Collect counts of successful compilations
  1.1785 +    _sum_nmethod_size += code->total_size();
  1.1786 +    _sum_nmethod_code_size += code->code_size();
  1.1787 +    _total_compile_count++;
  1.1788 +
  1.1789 +    if (UsePerfData) {
  1.1790 +      _perf_sum_nmethod_size->inc(code->total_size());
  1.1791 +      _perf_sum_nmethod_code_size->inc(code->code_size());
  1.1792 +      _perf_total_compile_count->inc();
  1.1793 +    }
  1.1794 +
  1.1795 +    if (is_osr) {
  1.1796 +      if (UsePerfData) _perf_total_osr_compile_count->inc();
  1.1797 +      _total_osr_compile_count++;
  1.1798 +    } else {
  1.1799 +      if (UsePerfData) _perf_total_standard_compile_count->inc();
  1.1800 +      _total_standard_compile_count++;
  1.1801 +    }
  1.1802 +  }
  1.1803 +  // set the current method for the thread to null
  1.1804 +  if (UsePerfData) counters->set_current_method("");
  1.1805 +}
  1.1806 +
  1.1807 +
  1.1808 +
  1.1809 +void CompileBroker::print_times() {
  1.1810 +  tty->cr();
  1.1811 +  tty->print_cr("Accumulated compiler times (for compiled methods only)");
  1.1812 +  tty->print_cr("------------------------------------------------");
  1.1813 +               //0000000000111111111122222222223333333333444444444455555555556666666666
  1.1814 +               //0123456789012345678901234567890123456789012345678901234567890123456789
  1.1815 +  tty->print_cr("  Total compilation time   : %6.3f s", CompileBroker::_t_total_compilation.seconds());
  1.1816 +  tty->print_cr("    Standard compilation   : %6.3f s, Average : %2.3f",
  1.1817 +                CompileBroker::_t_standard_compilation.seconds(),
  1.1818 +                CompileBroker::_t_standard_compilation.seconds() / CompileBroker::_total_standard_compile_count);
  1.1819 +  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);
  1.1820 +  compiler(CompLevel_fast_compile)->print_timers();
  1.1821 +  if (compiler(CompLevel_fast_compile) != compiler(CompLevel_highest_tier)) {
  1.1822 +    compiler(CompLevel_highest_tier)->print_timers();
  1.1823 +  }
  1.1824 +
  1.1825 +  tty->cr();
  1.1826 +  int tcb = CompileBroker::_sum_osr_bytes_compiled + CompileBroker::_sum_standard_bytes_compiled;
  1.1827 +  tty->print_cr("  Total compiled bytecodes : %6d bytes", tcb);
  1.1828 +  tty->print_cr("    Standard compilation   : %6d bytes", CompileBroker::_sum_standard_bytes_compiled);
  1.1829 +  tty->print_cr("    On stack replacement   : %6d bytes", CompileBroker::_sum_osr_bytes_compiled);
  1.1830 +  int bps = (int)(tcb / CompileBroker::_t_total_compilation.seconds());
  1.1831 +  tty->print_cr("  Average compilation speed: %6d bytes/s", bps);
  1.1832 +  tty->cr();
  1.1833 +  tty->print_cr("  nmethod code size        : %6d bytes", CompileBroker::_sum_nmethod_code_size);
  1.1834 +  tty->print_cr("  nmethod total size       : %6d bytes", CompileBroker::_sum_nmethod_size);
  1.1835 +}
  1.1836 +
  1.1837 +
  1.1838 +// Debugging output for failure
  1.1839 +void CompileBroker::print_last_compile() {
  1.1840 +  if ( _last_compile_level != CompLevel_none &&
  1.1841 +       compiler(_last_compile_level) != NULL &&
  1.1842 +       _last_method_compiled != NULL &&
  1.1843 +       _last_compile_type != no_compile) {
  1.1844 +    if (_last_compile_type == osr_compile) {
  1.1845 +      tty->print_cr("Last parse:  [osr]%d+++(%d) %s",
  1.1846 +                    _osr_compilation_id, _last_compile_level, _last_method_compiled);
  1.1847 +    } else {
  1.1848 +      tty->print_cr("Last parse:  %d+++(%d) %s",
  1.1849 +                    _compilation_id, _last_compile_level, _last_method_compiled);
  1.1850 +    }
  1.1851 +  }
  1.1852 +}
  1.1853 +
  1.1854 +
  1.1855 +void CompileBroker::print_compiler_threads_on(outputStream* st) {
  1.1856 +#ifndef PRODUCT
  1.1857 +  st->print_cr("Compiler thread printing unimplemented.");
  1.1858 +  st->cr();
  1.1859 +#endif
  1.1860 +}

mercurial