src/share/vm/compiler/compileBroker.cpp

changeset 0
f90c822e73f8
child 1
2d8a650513c2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/compiler/compileBroker.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,2347 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/systemDictionary.hpp"
    1.30 +#include "classfile/vmSymbols.hpp"
    1.31 +#include "code/codeCache.hpp"
    1.32 +#include "compiler/compileBroker.hpp"
    1.33 +#include "compiler/compileLog.hpp"
    1.34 +#include "compiler/compilerOracle.hpp"
    1.35 +#include "interpreter/linkResolver.hpp"
    1.36 +#include "memory/allocation.inline.hpp"
    1.37 +#include "oops/methodData.hpp"
    1.38 +#include "oops/method.hpp"
    1.39 +#include "oops/oop.inline.hpp"
    1.40 +#include "prims/nativeLookup.hpp"
    1.41 +#include "runtime/arguments.hpp"
    1.42 +#include "runtime/compilationPolicy.hpp"
    1.43 +#include "runtime/init.hpp"
    1.44 +#include "runtime/interfaceSupport.hpp"
    1.45 +#include "runtime/javaCalls.hpp"
    1.46 +#include "runtime/os.hpp"
    1.47 +#include "runtime/sharedRuntime.hpp"
    1.48 +#include "runtime/sweeper.hpp"
    1.49 +#include "trace/tracing.hpp"
    1.50 +#include "utilities/dtrace.hpp"
    1.51 +#include "utilities/events.hpp"
    1.52 +#ifdef COMPILER1
    1.53 +#include "c1/c1_Compiler.hpp"
    1.54 +#endif
    1.55 +#ifdef COMPILER2
    1.56 +#include "opto/c2compiler.hpp"
    1.57 +#endif
    1.58 +#ifdef SHARK
    1.59 +#include "shark/sharkCompiler.hpp"
    1.60 +#endif
    1.61 +
    1.62 +#ifdef DTRACE_ENABLED
    1.63 +
    1.64 +// Only bother with this argument setup if dtrace is available
    1.65 +
    1.66 +#ifndef USDT2
    1.67 +HS_DTRACE_PROBE_DECL8(hotspot, method__compile__begin,
    1.68 +  char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t);
    1.69 +HS_DTRACE_PROBE_DECL9(hotspot, method__compile__end,
    1.70 +  char*, intptr_t, char*, intptr_t, char*, intptr_t, char*, intptr_t, bool);
    1.71 +
    1.72 +#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)             \
    1.73 +  {                                                                      \
    1.74 +    Symbol* klass_name = (method)->klass_name();                         \
    1.75 +    Symbol* name = (method)->name();                                     \
    1.76 +    Symbol* signature = (method)->signature();                           \
    1.77 +    HS_DTRACE_PROBE8(hotspot, method__compile__begin,                    \
    1.78 +      comp_name, strlen(comp_name),                                      \
    1.79 +      klass_name->bytes(), klass_name->utf8_length(),                    \
    1.80 +      name->bytes(), name->utf8_length(),                                \
    1.81 +      signature->bytes(), signature->utf8_length());                     \
    1.82 +  }
    1.83 +
    1.84 +#define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)      \
    1.85 +  {                                                                      \
    1.86 +    Symbol* klass_name = (method)->klass_name();                         \
    1.87 +    Symbol* name = (method)->name();                                     \
    1.88 +    Symbol* signature = (method)->signature();                           \
    1.89 +    HS_DTRACE_PROBE9(hotspot, method__compile__end,                      \
    1.90 +      comp_name, strlen(comp_name),                                      \
    1.91 +      klass_name->bytes(), klass_name->utf8_length(),                    \
    1.92 +      name->bytes(), name->utf8_length(),                                \
    1.93 +      signature->bytes(), signature->utf8_length(), (success));          \
    1.94 +  }
    1.95 +
    1.96 +#else /* USDT2 */
    1.97 +
    1.98 +#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)             \
    1.99 +  {                                                                      \
   1.100 +    Symbol* klass_name = (method)->klass_name();                         \
   1.101 +    Symbol* name = (method)->name();                                     \
   1.102 +    Symbol* signature = (method)->signature();                           \
   1.103 +    HOTSPOT_METHOD_COMPILE_BEGIN(                                        \
   1.104 +      comp_name, strlen(comp_name),                                      \
   1.105 +      (char *) klass_name->bytes(), klass_name->utf8_length(),           \
   1.106 +      (char *) name->bytes(), name->utf8_length(),                       \
   1.107 +      (char *) signature->bytes(), signature->utf8_length());            \
   1.108 +  }
   1.109 +
   1.110 +#define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)      \
   1.111 +  {                                                                      \
   1.112 +    Symbol* klass_name = (method)->klass_name();                         \
   1.113 +    Symbol* name = (method)->name();                                     \
   1.114 +    Symbol* signature = (method)->signature();                           \
   1.115 +    HOTSPOT_METHOD_COMPILE_END(                                          \
   1.116 +      comp_name, strlen(comp_name),                                      \
   1.117 +      (char *) klass_name->bytes(), klass_name->utf8_length(),           \
   1.118 +      (char *) name->bytes(), name->utf8_length(),                       \
   1.119 +      (char *) signature->bytes(), signature->utf8_length(), (success)); \
   1.120 +  }
   1.121 +#endif /* USDT2 */
   1.122 +
   1.123 +#else //  ndef DTRACE_ENABLED
   1.124 +
   1.125 +#define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)
   1.126 +#define DTRACE_METHOD_COMPILE_END_PROBE(method, comp_name, success)
   1.127 +
   1.128 +#endif // ndef DTRACE_ENABLED
   1.129 +
   1.130 +bool CompileBroker::_initialized = false;
   1.131 +volatile bool CompileBroker::_should_block = false;
   1.132 +volatile jint CompileBroker::_print_compilation_warning = 0;
   1.133 +volatile jint CompileBroker::_should_compile_new_jobs = run_compilation;
   1.134 +
   1.135 +// The installed compiler(s)
   1.136 +AbstractCompiler* CompileBroker::_compilers[2];
   1.137 +
   1.138 +// These counters are used to assign an unique ID to each compilation.
   1.139 +volatile jint CompileBroker::_compilation_id     = 0;
   1.140 +volatile jint CompileBroker::_osr_compilation_id = 0;
   1.141 +
   1.142 +// Debugging information
   1.143 +int  CompileBroker::_last_compile_type     = no_compile;
   1.144 +int  CompileBroker::_last_compile_level    = CompLevel_none;
   1.145 +char CompileBroker::_last_method_compiled[CompileBroker::name_buffer_length];
   1.146 +
   1.147 +// Performance counters
   1.148 +PerfCounter* CompileBroker::_perf_total_compilation = NULL;
   1.149 +PerfCounter* CompileBroker::_perf_osr_compilation = NULL;
   1.150 +PerfCounter* CompileBroker::_perf_standard_compilation = NULL;
   1.151 +
   1.152 +PerfCounter* CompileBroker::_perf_total_bailout_count = NULL;
   1.153 +PerfCounter* CompileBroker::_perf_total_invalidated_count = NULL;
   1.154 +PerfCounter* CompileBroker::_perf_total_compile_count = NULL;
   1.155 +PerfCounter* CompileBroker::_perf_total_osr_compile_count = NULL;
   1.156 +PerfCounter* CompileBroker::_perf_total_standard_compile_count = NULL;
   1.157 +
   1.158 +PerfCounter* CompileBroker::_perf_sum_osr_bytes_compiled = NULL;
   1.159 +PerfCounter* CompileBroker::_perf_sum_standard_bytes_compiled = NULL;
   1.160 +PerfCounter* CompileBroker::_perf_sum_nmethod_size = NULL;
   1.161 +PerfCounter* CompileBroker::_perf_sum_nmethod_code_size = NULL;
   1.162 +
   1.163 +PerfStringVariable* CompileBroker::_perf_last_method = NULL;
   1.164 +PerfStringVariable* CompileBroker::_perf_last_failed_method = NULL;
   1.165 +PerfStringVariable* CompileBroker::_perf_last_invalidated_method = NULL;
   1.166 +PerfVariable*       CompileBroker::_perf_last_compile_type = NULL;
   1.167 +PerfVariable*       CompileBroker::_perf_last_compile_size = NULL;
   1.168 +PerfVariable*       CompileBroker::_perf_last_failed_type = NULL;
   1.169 +PerfVariable*       CompileBroker::_perf_last_invalidated_type = NULL;
   1.170 +
   1.171 +// Timers and counters for generating statistics
   1.172 +elapsedTimer CompileBroker::_t_total_compilation;
   1.173 +elapsedTimer CompileBroker::_t_osr_compilation;
   1.174 +elapsedTimer CompileBroker::_t_standard_compilation;
   1.175 +
   1.176 +int CompileBroker::_total_bailout_count          = 0;
   1.177 +int CompileBroker::_total_invalidated_count      = 0;
   1.178 +int CompileBroker::_total_compile_count          = 0;
   1.179 +int CompileBroker::_total_osr_compile_count      = 0;
   1.180 +int CompileBroker::_total_standard_compile_count = 0;
   1.181 +
   1.182 +int CompileBroker::_sum_osr_bytes_compiled       = 0;
   1.183 +int CompileBroker::_sum_standard_bytes_compiled  = 0;
   1.184 +int CompileBroker::_sum_nmethod_size             = 0;
   1.185 +int CompileBroker::_sum_nmethod_code_size        = 0;
   1.186 +
   1.187 +long CompileBroker::_peak_compilation_time       = 0;
   1.188 +
   1.189 +CompileQueue* CompileBroker::_c2_method_queue    = NULL;
   1.190 +CompileQueue* CompileBroker::_c1_method_queue    = NULL;
   1.191 +CompileTask*  CompileBroker::_task_free_list     = NULL;
   1.192 +
   1.193 +GrowableArray<CompilerThread*>* CompileBroker::_compiler_threads = NULL;
   1.194 +
   1.195 +
   1.196 +class CompilationLog : public StringEventLog {
   1.197 + public:
   1.198 +  CompilationLog() : StringEventLog("Compilation events") {
   1.199 +  }
   1.200 +
   1.201 +  void log_compile(JavaThread* thread, CompileTask* task) {
   1.202 +    StringLogMessage lm;
   1.203 +    stringStream sstr = lm.stream();
   1.204 +    // msg.time_stamp().update_to(tty->time_stamp().ticks());
   1.205 +    task->print_compilation(&sstr, NULL, true);
   1.206 +    log(thread, "%s", (const char*)lm);
   1.207 +  }
   1.208 +
   1.209 +  void log_nmethod(JavaThread* thread, nmethod* nm) {
   1.210 +    log(thread, "nmethod %d%s " INTPTR_FORMAT " code ["INTPTR_FORMAT ", " INTPTR_FORMAT "]",
   1.211 +        nm->compile_id(), nm->is_osr_method() ? "%" : "",
   1.212 +        p2i(nm), p2i(nm->code_begin()), p2i(nm->code_end()));
   1.213 +  }
   1.214 +
   1.215 +  void log_failure(JavaThread* thread, CompileTask* task, const char* reason, const char* retry_message) {
   1.216 +    StringLogMessage lm;
   1.217 +    lm.print("%4d   COMPILE SKIPPED: %s", task->compile_id(), reason);
   1.218 +    if (retry_message != NULL) {
   1.219 +      lm.append(" (%s)", retry_message);
   1.220 +    }
   1.221 +    lm.print("\n");
   1.222 +    log(thread, "%s", (const char*)lm);
   1.223 +  }
   1.224 +};
   1.225 +
   1.226 +static CompilationLog* _compilation_log = NULL;
   1.227 +
   1.228 +void compileBroker_init() {
   1.229 +  if (LogEvents) {
   1.230 +    _compilation_log = new CompilationLog();
   1.231 +  }
   1.232 +}
   1.233 +
   1.234 +CompileTaskWrapper::CompileTaskWrapper(CompileTask* task) {
   1.235 +  CompilerThread* thread = CompilerThread::current();
   1.236 +  thread->set_task(task);
   1.237 +  CompileLog*     log  = thread->log();
   1.238 +  if (log != NULL)  task->log_task_start(log);
   1.239 +}
   1.240 +
   1.241 +CompileTaskWrapper::~CompileTaskWrapper() {
   1.242 +  CompilerThread* thread = CompilerThread::current();
   1.243 +  CompileTask* task = thread->task();
   1.244 +  CompileLog*  log  = thread->log();
   1.245 +  if (log != NULL)  task->log_task_done(log);
   1.246 +  thread->set_task(NULL);
   1.247 +  task->set_code_handle(NULL);
   1.248 +  thread->set_env(NULL);
   1.249 +  if (task->is_blocking()) {
   1.250 +    MutexLocker notifier(task->lock(), thread);
   1.251 +    task->mark_complete();
   1.252 +    // Notify the waiting thread that the compilation has completed.
   1.253 +    task->lock()->notify_all();
   1.254 +  } else {
   1.255 +    task->mark_complete();
   1.256 +
   1.257 +    // By convention, the compiling thread is responsible for
   1.258 +    // recycling a non-blocking CompileTask.
   1.259 +    CompileBroker::free_task(task);
   1.260 +  }
   1.261 +}
   1.262 +
   1.263 +
   1.264 +// ------------------------------------------------------------------
   1.265 +// CompileTask::initialize
   1.266 +void CompileTask::initialize(int compile_id,
   1.267 +                             methodHandle method,
   1.268 +                             int osr_bci,
   1.269 +                             int comp_level,
   1.270 +                             methodHandle hot_method,
   1.271 +                             int hot_count,
   1.272 +                             const char* comment,
   1.273 +                             bool is_blocking) {
   1.274 +  assert(!_lock->is_locked(), "bad locking");
   1.275 +
   1.276 +  _compile_id = compile_id;
   1.277 +  _method = method();
   1.278 +  _method_holder = JNIHandles::make_global(method->method_holder()->klass_holder());
   1.279 +  _osr_bci = osr_bci;
   1.280 +  _is_blocking = is_blocking;
   1.281 +  _comp_level = comp_level;
   1.282 +  _num_inlined_bytecodes = 0;
   1.283 +
   1.284 +  _is_complete = false;
   1.285 +  _is_success = false;
   1.286 +  _code_handle = NULL;
   1.287 +
   1.288 +  _hot_method = NULL;
   1.289 +  _hot_method_holder = NULL;
   1.290 +  _hot_count = hot_count;
   1.291 +  _time_queued = 0;  // tidy
   1.292 +  _comment = comment;
   1.293 +
   1.294 +  if (LogCompilation) {
   1.295 +    _time_queued = os::elapsed_counter();
   1.296 +    if (hot_method.not_null()) {
   1.297 +      if (hot_method == method) {
   1.298 +        _hot_method = _method;
   1.299 +      } else {
   1.300 +        _hot_method = hot_method();
   1.301 +        // only add loader or mirror if different from _method_holder
   1.302 +        _hot_method_holder = JNIHandles::make_global(hot_method->method_holder()->klass_holder());
   1.303 +      }
   1.304 +    }
   1.305 +  }
   1.306 +
   1.307 +  _next = NULL;
   1.308 +}
   1.309 +
   1.310 +// ------------------------------------------------------------------
   1.311 +// CompileTask::code/set_code
   1.312 +nmethod* CompileTask::code() const {
   1.313 +  if (_code_handle == NULL)  return NULL;
   1.314 +  return _code_handle->code();
   1.315 +}
   1.316 +void CompileTask::set_code(nmethod* nm) {
   1.317 +  if (_code_handle == NULL && nm == NULL)  return;
   1.318 +  guarantee(_code_handle != NULL, "");
   1.319 +  _code_handle->set_code(nm);
   1.320 +  if (nm == NULL)  _code_handle = NULL;  // drop the handle also
   1.321 +}
   1.322 +
   1.323 +// ------------------------------------------------------------------
   1.324 +// CompileTask::free
   1.325 +void CompileTask::free() {
   1.326 +  set_code(NULL);
   1.327 +  assert(!_lock->is_locked(), "Should not be locked when freed");
   1.328 +  JNIHandles::destroy_global(_method_holder);
   1.329 +  JNIHandles::destroy_global(_hot_method_holder);
   1.330 +}
   1.331 +
   1.332 +
   1.333 +void CompileTask::mark_on_stack() {
   1.334 +  // Mark these methods as something redefine classes cannot remove.
   1.335 +  _method->set_on_stack(true);
   1.336 +  if (_hot_method != NULL) {
   1.337 +    _hot_method->set_on_stack(true);
   1.338 +  }
   1.339 +}
   1.340 +
   1.341 +// ------------------------------------------------------------------
   1.342 +// CompileTask::print
   1.343 +void CompileTask::print() {
   1.344 +  tty->print("<CompileTask compile_id=%d ", _compile_id);
   1.345 +  tty->print("method=");
   1.346 +  _method->print_name(tty);
   1.347 +  tty->print_cr(" osr_bci=%d is_blocking=%s is_complete=%s is_success=%s>",
   1.348 +             _osr_bci, bool_to_str(_is_blocking),
   1.349 +             bool_to_str(_is_complete), bool_to_str(_is_success));
   1.350 +}
   1.351 +
   1.352 +
   1.353 +// ------------------------------------------------------------------
   1.354 +// CompileTask::print_line_on_error
   1.355 +//
   1.356 +// This function is called by fatal error handler when the thread
   1.357 +// causing troubles is a compiler thread.
   1.358 +//
   1.359 +// Do not grab any lock, do not allocate memory.
   1.360 +//
   1.361 +// Otherwise it's the same as CompileTask::print_line()
   1.362 +//
   1.363 +void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) {
   1.364 +  // print compiler name
   1.365 +  st->print("%s:", CompileBroker::compiler_name(comp_level()));
   1.366 +  print_compilation(st);
   1.367 +}
   1.368 +
   1.369 +// ------------------------------------------------------------------
   1.370 +// CompileTask::print_line
   1.371 +void CompileTask::print_line() {
   1.372 +  ttyLocker ttyl;  // keep the following output all in one block
   1.373 +  // print compiler name if requested
   1.374 +  if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler_name(comp_level()));
   1.375 +  print_compilation();
   1.376 +}
   1.377 +
   1.378 +
   1.379 +// ------------------------------------------------------------------
   1.380 +// CompileTask::print_compilation_impl
   1.381 +void CompileTask::print_compilation_impl(outputStream* st, Method* method, int compile_id, int comp_level,
   1.382 +                                         bool is_osr_method, int osr_bci, bool is_blocking,
   1.383 +                                         const char* msg, bool short_form) {
   1.384 +  if (!short_form) {
   1.385 +    st->print("%7d ", (int) st->time_stamp().milliseconds());  // print timestamp
   1.386 +  }
   1.387 +  st->print("%4d ", compile_id);    // print compilation number
   1.388 +
   1.389 +  // For unloaded methods the transition to zombie occurs after the
   1.390 +  // method is cleared so it's impossible to report accurate
   1.391 +  // information for that case.
   1.392 +  bool is_synchronized = false;
   1.393 +  bool has_exception_handler = false;
   1.394 +  bool is_native = false;
   1.395 +  if (method != NULL) {
   1.396 +    is_synchronized       = method->is_synchronized();
   1.397 +    has_exception_handler = method->has_exception_handler();
   1.398 +    is_native             = method->is_native();
   1.399 +  }
   1.400 +  // method attributes
   1.401 +  const char compile_type   = is_osr_method                   ? '%' : ' ';
   1.402 +  const char sync_char      = is_synchronized                 ? 's' : ' ';
   1.403 +  const char exception_char = has_exception_handler           ? '!' : ' ';
   1.404 +  const char blocking_char  = is_blocking                     ? 'b' : ' ';
   1.405 +  const char native_char    = is_native                       ? 'n' : ' ';
   1.406 +
   1.407 +  // print method attributes
   1.408 +  st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char);
   1.409 +
   1.410 +  if (TieredCompilation) {
   1.411 +    if (comp_level != -1)  st->print("%d ", comp_level);
   1.412 +    else                   st->print("- ");
   1.413 +  }
   1.414 +  st->print("     ");  // more indent
   1.415 +
   1.416 +  if (method == NULL) {
   1.417 +    st->print("(method)");
   1.418 +  } else {
   1.419 +    method->print_short_name(st);
   1.420 +    if (is_osr_method) {
   1.421 +      st->print(" @ %d", osr_bci);
   1.422 +    }
   1.423 +    if (method->is_native())
   1.424 +      st->print(" (native)");
   1.425 +    else
   1.426 +      st->print(" (%d bytes)", method->code_size());
   1.427 +  }
   1.428 +
   1.429 +  if (msg != NULL) {
   1.430 +    st->print("   %s", msg);
   1.431 +  }
   1.432 +  if (!short_form) {
   1.433 +    st->cr();
   1.434 +  }
   1.435 +}
   1.436 +
   1.437 +// ------------------------------------------------------------------
   1.438 +// CompileTask::print_inlining
   1.439 +void CompileTask::print_inlining(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) {
   1.440 +  //         1234567
   1.441 +  st->print("        ");     // print timestamp
   1.442 +  //         1234
   1.443 +  st->print("     ");        // print compilation number
   1.444 +
   1.445 +  // method attributes
   1.446 +  if (method->is_loaded()) {
   1.447 +    const char sync_char      = method->is_synchronized()        ? 's' : ' ';
   1.448 +    const char exception_char = method->has_exception_handlers() ? '!' : ' ';
   1.449 +    const char monitors_char  = method->has_monitor_bytecodes()  ? 'm' : ' ';
   1.450 +
   1.451 +    // print method attributes
   1.452 +    st->print(" %c%c%c  ", sync_char, exception_char, monitors_char);
   1.453 +  } else {
   1.454 +    //         %s!bn
   1.455 +    st->print("      ");     // print method attributes
   1.456 +  }
   1.457 +
   1.458 +  if (TieredCompilation) {
   1.459 +    st->print("  ");
   1.460 +  }
   1.461 +  st->print("     ");        // more indent
   1.462 +  st->print("    ");         // initial inlining indent
   1.463 +
   1.464 +  for (int i = 0; i < inline_level; i++)  st->print("  ");
   1.465 +
   1.466 +  st->print("@ %d  ", bci);  // print bci
   1.467 +  method->print_short_name(st);
   1.468 +  if (method->is_loaded())
   1.469 +    st->print(" (%d bytes)", method->code_size());
   1.470 +  else
   1.471 +    st->print(" (not loaded)");
   1.472 +
   1.473 +  if (msg != NULL) {
   1.474 +    st->print("   %s", msg);
   1.475 +  }
   1.476 +  st->cr();
   1.477 +}
   1.478 +
   1.479 +// ------------------------------------------------------------------
   1.480 +// CompileTask::print_inline_indent
   1.481 +void CompileTask::print_inline_indent(int inline_level, outputStream* st) {
   1.482 +  //         1234567
   1.483 +  st->print("        ");     // print timestamp
   1.484 +  //         1234
   1.485 +  st->print("     ");        // print compilation number
   1.486 +  //         %s!bn
   1.487 +  st->print("      ");       // print method attributes
   1.488 +  if (TieredCompilation) {
   1.489 +    st->print("  ");
   1.490 +  }
   1.491 +  st->print("     ");        // more indent
   1.492 +  st->print("    ");         // initial inlining indent
   1.493 +  for (int i = 0; i < inline_level; i++)  st->print("  ");
   1.494 +}
   1.495 +
   1.496 +// ------------------------------------------------------------------
   1.497 +// CompileTask::print_compilation
   1.498 +void CompileTask::print_compilation(outputStream* st, const char* msg, bool short_form) {
   1.499 +  bool is_osr_method = osr_bci() != InvocationEntryBci;
   1.500 +  print_compilation_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form);
   1.501 +}
   1.502 +
   1.503 +// ------------------------------------------------------------------
   1.504 +// CompileTask::log_task
   1.505 +void CompileTask::log_task(xmlStream* log) {
   1.506 +  Thread* thread = Thread::current();
   1.507 +  methodHandle method(thread, this->method());
   1.508 +  ResourceMark rm(thread);
   1.509 +
   1.510 +  // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'>
   1.511 +  log->print(" compile_id='%d'", _compile_id);
   1.512 +  if (_osr_bci != CompileBroker::standard_entry_bci) {
   1.513 +    log->print(" compile_kind='osr'");  // same as nmethod::compile_kind
   1.514 +  } // else compile_kind='c2c'
   1.515 +  if (!method.is_null())  log->method(method);
   1.516 +  if (_osr_bci != CompileBroker::standard_entry_bci) {
   1.517 +    log->print(" osr_bci='%d'", _osr_bci);
   1.518 +  }
   1.519 +  if (_comp_level != CompLevel_highest_tier) {
   1.520 +    log->print(" level='%d'", _comp_level);
   1.521 +  }
   1.522 +  if (_is_blocking) {
   1.523 +    log->print(" blocking='1'");
   1.524 +  }
   1.525 +  log->stamp();
   1.526 +}
   1.527 +
   1.528 +
   1.529 +// ------------------------------------------------------------------
   1.530 +// CompileTask::log_task_queued
   1.531 +void CompileTask::log_task_queued() {
   1.532 +  Thread* thread = Thread::current();
   1.533 +  ttyLocker ttyl;
   1.534 +  ResourceMark rm(thread);
   1.535 +
   1.536 +  xtty->begin_elem("task_queued");
   1.537 +  log_task(xtty);
   1.538 +  if (_comment != NULL) {
   1.539 +    xtty->print(" comment='%s'", _comment);
   1.540 +  }
   1.541 +  if (_hot_method != NULL) {
   1.542 +    methodHandle hot(thread, _hot_method);
   1.543 +    methodHandle method(thread, _method);
   1.544 +    if (hot() != method()) {
   1.545 +      xtty->method(hot);
   1.546 +    }
   1.547 +  }
   1.548 +  if (_hot_count != 0) {
   1.549 +    xtty->print(" hot_count='%d'", _hot_count);
   1.550 +  }
   1.551 +  xtty->end_elem();
   1.552 +}
   1.553 +
   1.554 +
   1.555 +// ------------------------------------------------------------------
   1.556 +// CompileTask::log_task_start
   1.557 +void CompileTask::log_task_start(CompileLog* log)   {
   1.558 +  log->begin_head("task");
   1.559 +  log_task(log);
   1.560 +  log->end_head();
   1.561 +}
   1.562 +
   1.563 +
   1.564 +// ------------------------------------------------------------------
   1.565 +// CompileTask::log_task_done
   1.566 +void CompileTask::log_task_done(CompileLog* log) {
   1.567 +  Thread* thread = Thread::current();
   1.568 +  methodHandle method(thread, this->method());
   1.569 +  ResourceMark rm(thread);
   1.570 +
   1.571 +  // <task_done ... stamp='1.234'>  </task>
   1.572 +  nmethod* nm = code();
   1.573 +  log->begin_elem("task_done success='%d' nmsize='%d' count='%d'",
   1.574 +                  _is_success, nm == NULL ? 0 : nm->content_size(),
   1.575 +                  method->invocation_count());
   1.576 +  int bec = method->backedge_count();
   1.577 +  if (bec != 0)  log->print(" backedge_count='%d'", bec);
   1.578 +  // Note:  "_is_complete" is about to be set, but is not.
   1.579 +  if (_num_inlined_bytecodes != 0) {
   1.580 +    log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);
   1.581 +  }
   1.582 +  log->stamp();
   1.583 +  log->end_elem();
   1.584 +  log->tail("task");
   1.585 +  log->clear_identities();   // next task will have different CI
   1.586 +  if (log->unflushed_count() > 2000) {
   1.587 +    log->flush();
   1.588 +  }
   1.589 +  log->mark_file_end();
   1.590 +}
   1.591 +
   1.592 +
   1.593 +
   1.594 +// Add a CompileTask to a CompileQueue
   1.595 +void CompileQueue::add(CompileTask* task) {
   1.596 +  assert(lock()->owned_by_self(), "must own lock");
   1.597 +
   1.598 +  task->set_next(NULL);
   1.599 +  task->set_prev(NULL);
   1.600 +
   1.601 +  if (_last == NULL) {
   1.602 +    // The compile queue is empty.
   1.603 +    assert(_first == NULL, "queue is empty");
   1.604 +    _first = task;
   1.605 +    _last = task;
   1.606 +  } else {
   1.607 +    // Append the task to the queue.
   1.608 +    assert(_last->next() == NULL, "not last");
   1.609 +    _last->set_next(task);
   1.610 +    task->set_prev(_last);
   1.611 +    _last = task;
   1.612 +  }
   1.613 +  ++_size;
   1.614 +
   1.615 +  // Mark the method as being in the compile queue.
   1.616 +  task->method()->set_queued_for_compilation();
   1.617 +
   1.618 +  if (CIPrintCompileQueue) {
   1.619 +    print();
   1.620 +  }
   1.621 +
   1.622 +  if (LogCompilation && xtty != NULL) {
   1.623 +    task->log_task_queued();
   1.624 +  }
   1.625 +
   1.626 +  // Notify CompilerThreads that a task is available.
   1.627 +  lock()->notify_all();
   1.628 +}
   1.629 +
   1.630 +void CompileQueue::delete_all() {
   1.631 +  assert(lock()->owned_by_self(), "must own lock");
   1.632 +  if (_first != NULL) {
   1.633 +    for (CompileTask* task = _first; task != NULL; task = task->next()) {
   1.634 +      delete task;
   1.635 +    }
   1.636 +    _first = NULL;
   1.637 +  }
   1.638 +}
   1.639 +
   1.640 +// ------------------------------------------------------------------
   1.641 +// CompileQueue::get
   1.642 +//
   1.643 +// Get the next CompileTask from a CompileQueue
   1.644 +CompileTask* CompileQueue::get() {
   1.645 +  NMethodSweeper::possibly_sweep();
   1.646 +
   1.647 +  MutexLocker locker(lock());
   1.648 +  // If _first is NULL we have no more compile jobs. There are two reasons for
   1.649 +  // having no compile jobs: First, we compiled everything we wanted. Second,
   1.650 +  // we ran out of code cache so compilation has been disabled. In the latter
   1.651 +  // case we perform code cache sweeps to free memory such that we can re-enable
   1.652 +  // compilation.
   1.653 +  while (_first == NULL) {
   1.654 +    // Exit loop if compilation is disabled forever
   1.655 +    if (CompileBroker::is_compilation_disabled_forever()) {
   1.656 +      return NULL;
   1.657 +    }
   1.658 +
   1.659 +    if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs()) {
   1.660 +      // Wait a certain amount of time to possibly do another sweep.
   1.661 +      // We must wait until stack scanning has happened so that we can
   1.662 +      // transition a method's state from 'not_entrant' to 'zombie'.
   1.663 +      long wait_time = NmethodSweepCheckInterval * 1000;
   1.664 +      if (FLAG_IS_DEFAULT(NmethodSweepCheckInterval)) {
   1.665 +        // Only one thread at a time can do sweeping. Scale the
   1.666 +        // wait time according to the number of compiler threads.
   1.667 +        // As a result, the next sweep is likely to happen every 100ms
   1.668 +        // with an arbitrary number of threads that do sweeping.
   1.669 +        wait_time = 100 * CICompilerCount;
   1.670 +      }
   1.671 +      bool timeout = lock()->wait(!Mutex::_no_safepoint_check_flag, wait_time);
   1.672 +      if (timeout) {
   1.673 +        MutexUnlocker ul(lock());
   1.674 +        NMethodSweeper::possibly_sweep();
   1.675 +      }
   1.676 +    } else {
   1.677 +      // If there are no compilation tasks and we can compile new jobs
   1.678 +      // (i.e., there is enough free space in the code cache) there is
   1.679 +      // no need to invoke the sweeper. As a result, the hotness of methods
   1.680 +      // remains unchanged. This behavior is desired, since we want to keep
   1.681 +      // the stable state, i.e., we do not want to evict methods from the
   1.682 +      // code cache if it is unnecessary.
   1.683 +      // We need a timed wait here, since compiler threads can exit if compilation
   1.684 +      // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
   1.685 +      // is not critical and we do not want idle compiler threads to wake up too often.
   1.686 +      lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
   1.687 +    }
   1.688 +  }
   1.689 +
   1.690 +  if (CompileBroker::is_compilation_disabled_forever()) {
   1.691 +    return NULL;
   1.692 +  }
   1.693 +
   1.694 +  CompileTask* task = CompilationPolicy::policy()->select_task(this);
   1.695 +  remove(task);
   1.696 +  return task;
   1.697 +}
   1.698 +
   1.699 +void CompileQueue::remove(CompileTask* task)
   1.700 +{
   1.701 +   assert(lock()->owned_by_self(), "must own lock");
   1.702 +  if (task->prev() != NULL) {
   1.703 +    task->prev()->set_next(task->next());
   1.704 +  } else {
   1.705 +    // max is the first element
   1.706 +    assert(task == _first, "Sanity");
   1.707 +    _first = task->next();
   1.708 +  }
   1.709 +
   1.710 +  if (task->next() != NULL) {
   1.711 +    task->next()->set_prev(task->prev());
   1.712 +  } else {
   1.713 +    // max is the last element
   1.714 +    assert(task == _last, "Sanity");
   1.715 +    _last = task->prev();
   1.716 +  }
   1.717 +  --_size;
   1.718 +}
   1.719 +
   1.720 +// methods in the compile queue need to be marked as used on the stack
   1.721 +// so that they don't get reclaimed by Redefine Classes
   1.722 +void CompileQueue::mark_on_stack() {
   1.723 +  CompileTask* task = _first;
   1.724 +  while (task != NULL) {
   1.725 +    task->mark_on_stack();
   1.726 +    task = task->next();
   1.727 +  }
   1.728 +}
   1.729 +
   1.730 +// ------------------------------------------------------------------
   1.731 +// CompileQueue::print
   1.732 +void CompileQueue::print() {
   1.733 +  tty->print_cr("Contents of %s", name());
   1.734 +  tty->print_cr("----------------------");
   1.735 +  CompileTask* task = _first;
   1.736 +  while (task != NULL) {
   1.737 +    task->print_line();
   1.738 +    task = task->next();
   1.739 +  }
   1.740 +  tty->print_cr("----------------------");
   1.741 +}
   1.742 +
   1.743 +CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) {
   1.744 +
   1.745 +  _current_method[0] = '\0';
   1.746 +  _compile_type = CompileBroker::no_compile;
   1.747 +
   1.748 +  if (UsePerfData) {
   1.749 +    ResourceMark rm;
   1.750 +
   1.751 +    // create the thread instance name space string - don't create an
   1.752 +    // instance subspace if instance is -1 - keeps the adapterThread
   1.753 +    // counters  from having a ".0" namespace.
   1.754 +    const char* thread_i = (instance == -1) ? thread_name :
   1.755 +                      PerfDataManager::name_space(thread_name, instance);
   1.756 +
   1.757 +
   1.758 +    char* name = PerfDataManager::counter_name(thread_i, "method");
   1.759 +    _perf_current_method =
   1.760 +               PerfDataManager::create_string_variable(SUN_CI, name,
   1.761 +                                                       cmname_buffer_length,
   1.762 +                                                       _current_method, CHECK);
   1.763 +
   1.764 +    name = PerfDataManager::counter_name(thread_i, "type");
   1.765 +    _perf_compile_type = PerfDataManager::create_variable(SUN_CI, name,
   1.766 +                                                          PerfData::U_None,
   1.767 +                                                         (jlong)_compile_type,
   1.768 +                                                          CHECK);
   1.769 +
   1.770 +    name = PerfDataManager::counter_name(thread_i, "time");
   1.771 +    _perf_time = PerfDataManager::create_counter(SUN_CI, name,
   1.772 +                                                 PerfData::U_Ticks, CHECK);
   1.773 +
   1.774 +    name = PerfDataManager::counter_name(thread_i, "compiles");
   1.775 +    _perf_compiles = PerfDataManager::create_counter(SUN_CI, name,
   1.776 +                                                     PerfData::U_Events, CHECK);
   1.777 +  }
   1.778 +}
   1.779 +
   1.780 +// ------------------------------------------------------------------
   1.781 +// CompileBroker::compilation_init
   1.782 +//
   1.783 +// Initialize the Compilation object
   1.784 +void CompileBroker::compilation_init() {
   1.785 +  _last_method_compiled[0] = '\0';
   1.786 +
   1.787 +  // No need to initialize compilation system if we do not use it.
   1.788 +  if (!UseCompiler) {
   1.789 +    return;
   1.790 +  }
   1.791 +#ifndef SHARK
   1.792 +  // Set the interface to the current compiler(s).
   1.793 +  int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
   1.794 +  int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
   1.795 +#ifdef COMPILER1
   1.796 +  if (c1_count > 0) {
   1.797 +    _compilers[0] = new Compiler();
   1.798 +  }
   1.799 +#endif // COMPILER1
   1.800 +
   1.801 +#ifdef COMPILER2
   1.802 +  if (c2_count > 0) {
   1.803 +    _compilers[1] = new C2Compiler();
   1.804 +  }
   1.805 +#endif // COMPILER2
   1.806 +
   1.807 +#else // SHARK
   1.808 +  int c1_count = 0;
   1.809 +  int c2_count = 1;
   1.810 +
   1.811 +  _compilers[1] = new SharkCompiler();
   1.812 +#endif // SHARK
   1.813 +
   1.814 +  // Initialize the CompileTask free list
   1.815 +  _task_free_list = NULL;
   1.816 +
   1.817 +  // Start the CompilerThreads
   1.818 +  init_compiler_threads(c1_count, c2_count);
   1.819 +  // totalTime performance counter is always created as it is required
   1.820 +  // by the implementation of java.lang.management.CompilationMBean.
   1.821 +  {
   1.822 +    EXCEPTION_MARK;
   1.823 +    _perf_total_compilation =
   1.824 +                 PerfDataManager::create_counter(JAVA_CI, "totalTime",
   1.825 +                                                 PerfData::U_Ticks, CHECK);
   1.826 +  }
   1.827 +
   1.828 +
   1.829 +  if (UsePerfData) {
   1.830 +
   1.831 +    EXCEPTION_MARK;
   1.832 +
   1.833 +    // create the jvmstat performance counters
   1.834 +    _perf_osr_compilation =
   1.835 +                 PerfDataManager::create_counter(SUN_CI, "osrTime",
   1.836 +                                                 PerfData::U_Ticks, CHECK);
   1.837 +
   1.838 +    _perf_standard_compilation =
   1.839 +                 PerfDataManager::create_counter(SUN_CI, "standardTime",
   1.840 +                                                 PerfData::U_Ticks, CHECK);
   1.841 +
   1.842 +    _perf_total_bailout_count =
   1.843 +                 PerfDataManager::create_counter(SUN_CI, "totalBailouts",
   1.844 +                                                 PerfData::U_Events, CHECK);
   1.845 +
   1.846 +    _perf_total_invalidated_count =
   1.847 +                 PerfDataManager::create_counter(SUN_CI, "totalInvalidates",
   1.848 +                                                 PerfData::U_Events, CHECK);
   1.849 +
   1.850 +    _perf_total_compile_count =
   1.851 +                 PerfDataManager::create_counter(SUN_CI, "totalCompiles",
   1.852 +                                                 PerfData::U_Events, CHECK);
   1.853 +    _perf_total_osr_compile_count =
   1.854 +                 PerfDataManager::create_counter(SUN_CI, "osrCompiles",
   1.855 +                                                 PerfData::U_Events, CHECK);
   1.856 +
   1.857 +    _perf_total_standard_compile_count =
   1.858 +                 PerfDataManager::create_counter(SUN_CI, "standardCompiles",
   1.859 +                                                 PerfData::U_Events, CHECK);
   1.860 +
   1.861 +    _perf_sum_osr_bytes_compiled =
   1.862 +                 PerfDataManager::create_counter(SUN_CI, "osrBytes",
   1.863 +                                                 PerfData::U_Bytes, CHECK);
   1.864 +
   1.865 +    _perf_sum_standard_bytes_compiled =
   1.866 +                 PerfDataManager::create_counter(SUN_CI, "standardBytes",
   1.867 +                                                 PerfData::U_Bytes, CHECK);
   1.868 +
   1.869 +    _perf_sum_nmethod_size =
   1.870 +                 PerfDataManager::create_counter(SUN_CI, "nmethodSize",
   1.871 +                                                 PerfData::U_Bytes, CHECK);
   1.872 +
   1.873 +    _perf_sum_nmethod_code_size =
   1.874 +                 PerfDataManager::create_counter(SUN_CI, "nmethodCodeSize",
   1.875 +                                                 PerfData::U_Bytes, CHECK);
   1.876 +
   1.877 +    _perf_last_method =
   1.878 +                 PerfDataManager::create_string_variable(SUN_CI, "lastMethod",
   1.879 +                                       CompilerCounters::cmname_buffer_length,
   1.880 +                                       "", CHECK);
   1.881 +
   1.882 +    _perf_last_failed_method =
   1.883 +            PerfDataManager::create_string_variable(SUN_CI, "lastFailedMethod",
   1.884 +                                       CompilerCounters::cmname_buffer_length,
   1.885 +                                       "", CHECK);
   1.886 +
   1.887 +    _perf_last_invalidated_method =
   1.888 +        PerfDataManager::create_string_variable(SUN_CI, "lastInvalidatedMethod",
   1.889 +                                     CompilerCounters::cmname_buffer_length,
   1.890 +                                     "", CHECK);
   1.891 +
   1.892 +    _perf_last_compile_type =
   1.893 +             PerfDataManager::create_variable(SUN_CI, "lastType",
   1.894 +                                              PerfData::U_None,
   1.895 +                                              (jlong)CompileBroker::no_compile,
   1.896 +                                              CHECK);
   1.897 +
   1.898 +    _perf_last_compile_size =
   1.899 +             PerfDataManager::create_variable(SUN_CI, "lastSize",
   1.900 +                                              PerfData::U_Bytes,
   1.901 +                                              (jlong)CompileBroker::no_compile,
   1.902 +                                              CHECK);
   1.903 +
   1.904 +
   1.905 +    _perf_last_failed_type =
   1.906 +             PerfDataManager::create_variable(SUN_CI, "lastFailedType",
   1.907 +                                              PerfData::U_None,
   1.908 +                                              (jlong)CompileBroker::no_compile,
   1.909 +                                              CHECK);
   1.910 +
   1.911 +    _perf_last_invalidated_type =
   1.912 +         PerfDataManager::create_variable(SUN_CI, "lastInvalidatedType",
   1.913 +                                          PerfData::U_None,
   1.914 +                                          (jlong)CompileBroker::no_compile,
   1.915 +                                          CHECK);
   1.916 +  }
   1.917 +
   1.918 +  _initialized = true;
   1.919 +}
   1.920 +
   1.921 +
   1.922 +CompilerThread* CompileBroker::make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters,
   1.923 +                                                    AbstractCompiler* comp, TRAPS) {
   1.924 +  CompilerThread* compiler_thread = NULL;
   1.925 +
   1.926 +  Klass* k =
   1.927 +    SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
   1.928 +                                      true, CHECK_0);
   1.929 +  instanceKlassHandle klass (THREAD, k);
   1.930 +  instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_0);
   1.931 +  Handle string = java_lang_String::create_from_str(name, CHECK_0);
   1.932 +
   1.933 +  // Initialize thread_oop to put it into the system threadGroup
   1.934 +  Handle thread_group (THREAD,  Universe::system_thread_group());
   1.935 +  JavaValue result(T_VOID);
   1.936 +  JavaCalls::call_special(&result, thread_oop,
   1.937 +                       klass,
   1.938 +                       vmSymbols::object_initializer_name(),
   1.939 +                       vmSymbols::threadgroup_string_void_signature(),
   1.940 +                       thread_group,
   1.941 +                       string,
   1.942 +                       CHECK_0);
   1.943 +
   1.944 +  {
   1.945 +    MutexLocker mu(Threads_lock, THREAD);
   1.946 +    compiler_thread = new CompilerThread(queue, counters);
   1.947 +    // At this point the new CompilerThread data-races with this startup
   1.948 +    // thread (which I believe is the primoridal thread and NOT the VM
   1.949 +    // thread).  This means Java bytecodes being executed at startup can
   1.950 +    // queue compile jobs which will run at whatever default priority the
   1.951 +    // newly created CompilerThread runs at.
   1.952 +
   1.953 +
   1.954 +    // At this point it may be possible that no osthread was created for the
   1.955 +    // JavaThread due to lack of memory. We would have to throw an exception
   1.956 +    // in that case. However, since this must work and we do not allow
   1.957 +    // exceptions anyway, check and abort if this fails.
   1.958 +
   1.959 +    if (compiler_thread == NULL || compiler_thread->osthread() == NULL){
   1.960 +      vm_exit_during_initialization("java.lang.OutOfMemoryError",
   1.961 +                                    "unable to create new native thread");
   1.962 +    }
   1.963 +
   1.964 +    java_lang_Thread::set_thread(thread_oop(), compiler_thread);
   1.965 +
   1.966 +    // Note that this only sets the JavaThread _priority field, which by
   1.967 +    // definition is limited to Java priorities and not OS priorities.
   1.968 +    // The os-priority is set in the CompilerThread startup code itself
   1.969 +
   1.970 +    java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
   1.971 +
   1.972 +    // Note that we cannot call os::set_priority because it expects Java
   1.973 +    // priorities and we are *explicitly* using OS priorities so that it's
   1.974 +    // possible to set the compiler thread priority higher than any Java
   1.975 +    // thread.
   1.976 +
   1.977 +    int native_prio = CompilerThreadPriority;
   1.978 +    if (native_prio == -1) {
   1.979 +      if (UseCriticalCompilerThreadPriority) {
   1.980 +        native_prio = os::java_to_os_priority[CriticalPriority];
   1.981 +      } else {
   1.982 +        native_prio = os::java_to_os_priority[NearMaxPriority];
   1.983 +      }
   1.984 +    }
   1.985 +    os::set_native_priority(compiler_thread, native_prio);
   1.986 +
   1.987 +    java_lang_Thread::set_daemon(thread_oop());
   1.988 +
   1.989 +    compiler_thread->set_threadObj(thread_oop());
   1.990 +    compiler_thread->set_compiler(comp);
   1.991 +    Threads::add(compiler_thread);
   1.992 +    Thread::start(compiler_thread);
   1.993 +  }
   1.994 +
   1.995 +  // Let go of Threads_lock before yielding
   1.996 +  os::yield(); // make sure that the compiler thread is started early (especially helpful on SOLARIS)
   1.997 +
   1.998 +  return compiler_thread;
   1.999 +}
  1.1000 +
  1.1001 +
  1.1002 +void CompileBroker::init_compiler_threads(int c1_compiler_count, int c2_compiler_count) {
  1.1003 +  EXCEPTION_MARK;
  1.1004 +#if !defined(ZERO) && !defined(SHARK)
  1.1005 +  assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
  1.1006 +#endif // !ZERO && !SHARK
  1.1007 +  // Initialize the compilation queue
  1.1008 +  if (c2_compiler_count > 0) {
  1.1009 +    _c2_method_queue  = new CompileQueue("C2MethodQueue",  MethodCompileQueue_lock);
  1.1010 +    _compilers[1]->set_num_compiler_threads(c2_compiler_count);
  1.1011 +  }
  1.1012 +  if (c1_compiler_count > 0) {
  1.1013 +    _c1_method_queue  = new CompileQueue("C1MethodQueue",  MethodCompileQueue_lock);
  1.1014 +    _compilers[0]->set_num_compiler_threads(c1_compiler_count);
  1.1015 +  }
  1.1016 +
  1.1017 +  int compiler_count = c1_compiler_count + c2_compiler_count;
  1.1018 +
  1.1019 +  _compiler_threads =
  1.1020 +    new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<CompilerThread*>(compiler_count, true);
  1.1021 +
  1.1022 +  char name_buffer[256];
  1.1023 +  for (int i = 0; i < c2_compiler_count; i++) {
  1.1024 +    // Create a name for our thread.
  1.1025 +    sprintf(name_buffer, "C2 CompilerThread%d", i);
  1.1026 +    CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
  1.1027 +    // Shark and C2
  1.1028 +    CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, _compilers[1], CHECK);
  1.1029 +    _compiler_threads->append(new_thread);
  1.1030 +  }
  1.1031 +
  1.1032 +  for (int i = c2_compiler_count; i < compiler_count; i++) {
  1.1033 +    // Create a name for our thread.
  1.1034 +    sprintf(name_buffer, "C1 CompilerThread%d", i);
  1.1035 +    CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
  1.1036 +    // C1
  1.1037 +    CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, _compilers[0], CHECK);
  1.1038 +    _compiler_threads->append(new_thread);
  1.1039 +  }
  1.1040 +
  1.1041 +  if (UsePerfData) {
  1.1042 +    PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
  1.1043 +  }
  1.1044 +}
  1.1045 +
  1.1046 +
  1.1047 +// Set the methods on the stack as on_stack so that redefine classes doesn't
  1.1048 +// reclaim them
  1.1049 +void CompileBroker::mark_on_stack() {
  1.1050 +  if (_c2_method_queue != NULL) {
  1.1051 +    _c2_method_queue->mark_on_stack();
  1.1052 +  }
  1.1053 +  if (_c1_method_queue != NULL) {
  1.1054 +    _c1_method_queue->mark_on_stack();
  1.1055 +  }
  1.1056 +}
  1.1057 +
  1.1058 +// ------------------------------------------------------------------
  1.1059 +// CompileBroker::compile_method
  1.1060 +//
  1.1061 +// Request compilation of a method.
  1.1062 +void CompileBroker::compile_method_base(methodHandle method,
  1.1063 +                                        int osr_bci,
  1.1064 +                                        int comp_level,
  1.1065 +                                        methodHandle hot_method,
  1.1066 +                                        int hot_count,
  1.1067 +                                        const char* comment,
  1.1068 +                                        Thread* thread) {
  1.1069 +  // do nothing if compiler thread(s) is not available
  1.1070 +  if (!_initialized ) {
  1.1071 +    return;
  1.1072 +  }
  1.1073 +
  1.1074 +  guarantee(!method->is_abstract(), "cannot compile abstract methods");
  1.1075 +  assert(method->method_holder()->oop_is_instance(),
  1.1076 +         "sanity check");
  1.1077 +  assert(!method->method_holder()->is_not_initialized(),
  1.1078 +         "method holder must be initialized");
  1.1079 +  assert(!method->is_method_handle_intrinsic(), "do not enqueue these guys");
  1.1080 +
  1.1081 +  if (CIPrintRequests) {
  1.1082 +    tty->print("request: ");
  1.1083 +    method->print_short_name(tty);
  1.1084 +    if (osr_bci != InvocationEntryBci) {
  1.1085 +      tty->print(" osr_bci: %d", osr_bci);
  1.1086 +    }
  1.1087 +    tty->print(" comment: %s count: %d", comment, hot_count);
  1.1088 +    if (!hot_method.is_null()) {
  1.1089 +      tty->print(" hot: ");
  1.1090 +      if (hot_method() != method()) {
  1.1091 +          hot_method->print_short_name(tty);
  1.1092 +      } else {
  1.1093 +        tty->print("yes");
  1.1094 +      }
  1.1095 +    }
  1.1096 +    tty->cr();
  1.1097 +  }
  1.1098 +
  1.1099 +  // A request has been made for compilation.  Before we do any
  1.1100 +  // real work, check to see if the method has been compiled
  1.1101 +  // in the meantime with a definitive result.
  1.1102 +  if (compilation_is_complete(method, osr_bci, comp_level)) {
  1.1103 +    return;
  1.1104 +  }
  1.1105 +
  1.1106 +#ifndef PRODUCT
  1.1107 +  if (osr_bci != -1 && !FLAG_IS_DEFAULT(OSROnlyBCI)) {
  1.1108 +    if ((OSROnlyBCI > 0) ? (OSROnlyBCI != osr_bci) : (-OSROnlyBCI == osr_bci)) {
  1.1109 +      // Positive OSROnlyBCI means only compile that bci.  Negative means don't compile that BCI.
  1.1110 +      return;
  1.1111 +    }
  1.1112 +  }
  1.1113 +#endif
  1.1114 +
  1.1115 +  // If this method is already in the compile queue, then
  1.1116 +  // we do not block the current thread.
  1.1117 +  if (compilation_is_in_queue(method, osr_bci)) {
  1.1118 +    // We may want to decay our counter a bit here to prevent
  1.1119 +    // multiple denied requests for compilation.  This is an
  1.1120 +    // open compilation policy issue. Note: The other possibility,
  1.1121 +    // in the case that this is a blocking compile request, is to have
  1.1122 +    // all subsequent blocking requesters wait for completion of
  1.1123 +    // ongoing compiles. Note that in this case we'll need a protocol
  1.1124 +    // for freeing the associated compile tasks. [Or we could have
  1.1125 +    // a single static monitor on which all these waiters sleep.]
  1.1126 +    return;
  1.1127 +  }
  1.1128 +
  1.1129 +  // If the requesting thread is holding the pending list lock
  1.1130 +  // then we just return. We can't risk blocking while holding
  1.1131 +  // the pending list lock or a 3-way deadlock may occur
  1.1132 +  // between the reference handler thread, a GC (instigated
  1.1133 +  // by a compiler thread), and compiled method registration.
  1.1134 +  if (InstanceRefKlass::owns_pending_list_lock(JavaThread::current())) {
  1.1135 +    return;
  1.1136 +  }
  1.1137 +
  1.1138 +  // Outputs from the following MutexLocker block:
  1.1139 +  CompileTask* task     = NULL;
  1.1140 +  bool         blocking = false;
  1.1141 +  CompileQueue* queue  = compile_queue(comp_level);
  1.1142 +
  1.1143 +  // Acquire our lock.
  1.1144 +  {
  1.1145 +    MutexLocker locker(queue->lock(), thread);
  1.1146 +
  1.1147 +    // Make sure the method has not slipped into the queues since
  1.1148 +    // last we checked; note that those checks were "fast bail-outs".
  1.1149 +    // Here we need to be more careful, see 14012000 below.
  1.1150 +    if (compilation_is_in_queue(method, osr_bci)) {
  1.1151 +      return;
  1.1152 +    }
  1.1153 +
  1.1154 +    // We need to check again to see if the compilation has
  1.1155 +    // completed.  A previous compilation may have registered
  1.1156 +    // some result.
  1.1157 +    if (compilation_is_complete(method, osr_bci, comp_level)) {
  1.1158 +      return;
  1.1159 +    }
  1.1160 +
  1.1161 +    // We now know that this compilation is not pending, complete,
  1.1162 +    // or prohibited.  Assign a compile_id to this compilation
  1.1163 +    // and check to see if it is in our [Start..Stop) range.
  1.1164 +    int compile_id = assign_compile_id(method, osr_bci);
  1.1165 +    if (compile_id == 0) {
  1.1166 +      // The compilation falls outside the allowed range.
  1.1167 +      return;
  1.1168 +    }
  1.1169 +
  1.1170 +    // Should this thread wait for completion of the compile?
  1.1171 +    blocking = is_compile_blocking(method, osr_bci);
  1.1172 +
  1.1173 +    // We will enter the compilation in the queue.
  1.1174 +    // 14012000: Note that this sets the queued_for_compile bits in
  1.1175 +    // the target method. We can now reason that a method cannot be
  1.1176 +    // queued for compilation more than once, as follows:
  1.1177 +    // Before a thread queues a task for compilation, it first acquires
  1.1178 +    // the compile queue lock, then checks if the method's queued bits
  1.1179 +    // are set or it has already been compiled. Thus there can not be two
  1.1180 +    // instances of a compilation task for the same method on the
  1.1181 +    // compilation queue. Consider now the case where the compilation
  1.1182 +    // thread has already removed a task for that method from the queue
  1.1183 +    // and is in the midst of compiling it. In this case, the
  1.1184 +    // queued_for_compile bits must be set in the method (and these
  1.1185 +    // will be visible to the current thread, since the bits were set
  1.1186 +    // under protection of the compile queue lock, which we hold now.
  1.1187 +    // When the compilation completes, the compiler thread first sets
  1.1188 +    // the compilation result and then clears the queued_for_compile
  1.1189 +    // bits. Neither of these actions are protected by a barrier (or done
  1.1190 +    // under the protection of a lock), so the only guarantee we have
  1.1191 +    // (on machines with TSO (Total Store Order)) is that these values
  1.1192 +    // will update in that order. As a result, the only combinations of
  1.1193 +    // these bits that the current thread will see are, in temporal order:
  1.1194 +    // <RESULT, QUEUE> :
  1.1195 +    //     <0, 1> : in compile queue, but not yet compiled
  1.1196 +    //     <1, 1> : compiled but queue bit not cleared
  1.1197 +    //     <1, 0> : compiled and queue bit cleared
  1.1198 +    // Because we first check the queue bits then check the result bits,
  1.1199 +    // we are assured that we cannot introduce a duplicate task.
  1.1200 +    // Note that if we did the tests in the reverse order (i.e. check
  1.1201 +    // result then check queued bit), we could get the result bit before
  1.1202 +    // the compilation completed, and the queue bit after the compilation
  1.1203 +    // completed, and end up introducing a "duplicate" (redundant) task.
  1.1204 +    // In that case, the compiler thread should first check if a method
  1.1205 +    // has already been compiled before trying to compile it.
  1.1206 +    // NOTE: in the event that there are multiple compiler threads and
  1.1207 +    // there is de-optimization/recompilation, things will get hairy,
  1.1208 +    // and in that case it's best to protect both the testing (here) of
  1.1209 +    // these bits, and their updating (here and elsewhere) under a
  1.1210 +    // common lock.
  1.1211 +    task = create_compile_task(queue,
  1.1212 +                               compile_id, method,
  1.1213 +                               osr_bci, comp_level,
  1.1214 +                               hot_method, hot_count, comment,
  1.1215 +                               blocking);
  1.1216 +  }
  1.1217 +
  1.1218 +  if (blocking) {
  1.1219 +    wait_for_completion(task);
  1.1220 +  }
  1.1221 +}
  1.1222 +
  1.1223 +
  1.1224 +nmethod* CompileBroker::compile_method(methodHandle method, int osr_bci,
  1.1225 +                                       int comp_level,
  1.1226 +                                       methodHandle hot_method, int hot_count,
  1.1227 +                                       const char* comment, Thread* THREAD) {
  1.1228 +  // make sure arguments make sense
  1.1229 +  assert(method->method_holder()->oop_is_instance(), "not an instance method");
  1.1230 +  assert(osr_bci == InvocationEntryBci || (0 <= osr_bci && osr_bci < method->code_size()), "bci out of range");
  1.1231 +  assert(!method->is_abstract() && (osr_bci == InvocationEntryBci || !method->is_native()), "cannot compile abstract/native methods");
  1.1232 +  assert(!method->method_holder()->is_not_initialized(), "method holder must be initialized");
  1.1233 +  // allow any levels for WhiteBox
  1.1234 +  assert(WhiteBoxAPI || TieredCompilation || comp_level == CompLevel_highest_tier, "only CompLevel_highest_tier must be used in non-tiered");
  1.1235 +  // return quickly if possible
  1.1236 +
  1.1237 +  // lock, make sure that the compilation
  1.1238 +  // isn't prohibited in a straightforward way.
  1.1239 +  AbstractCompiler *comp = CompileBroker::compiler(comp_level);
  1.1240 +  if (comp == NULL || !comp->can_compile_method(method) ||
  1.1241 +      compilation_is_prohibited(method, osr_bci, comp_level)) {
  1.1242 +    return NULL;
  1.1243 +  }
  1.1244 +
  1.1245 +  if (osr_bci == InvocationEntryBci) {
  1.1246 +    // standard compilation
  1.1247 +    nmethod* method_code = method->code();
  1.1248 +    if (method_code != NULL) {
  1.1249 +      if (compilation_is_complete(method, osr_bci, comp_level)) {
  1.1250 +        return method_code;
  1.1251 +      }
  1.1252 +    }
  1.1253 +    if (method->is_not_compilable(comp_level)) {
  1.1254 +      return NULL;
  1.1255 +    }
  1.1256 +  } else {
  1.1257 +    // osr compilation
  1.1258 +#ifndef TIERED
  1.1259 +    // seems like an assert of dubious value
  1.1260 +    assert(comp_level == CompLevel_highest_tier,
  1.1261 +           "all OSR compiles are assumed to be at a single compilation lavel");
  1.1262 +#endif // TIERED
  1.1263 +    // We accept a higher level osr method
  1.1264 +    nmethod* nm = method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
  1.1265 +    if (nm != NULL) return nm;
  1.1266 +    if (method->is_not_osr_compilable(comp_level)) return NULL;
  1.1267 +  }
  1.1268 +
  1.1269 +  assert(!HAS_PENDING_EXCEPTION, "No exception should be present");
  1.1270 +  // some prerequisites that are compiler specific
  1.1271 +  if (comp->is_c2() || comp->is_shark()) {
  1.1272 +    method->constants()->resolve_string_constants(CHECK_AND_CLEAR_NULL);
  1.1273 +    // Resolve all classes seen in the signature of the method
  1.1274 +    // we are compiling.
  1.1275 +    Method::load_signature_classes(method, CHECK_AND_CLEAR_NULL);
  1.1276 +  }
  1.1277 +
  1.1278 +  // If the method is native, do the lookup in the thread requesting
  1.1279 +  // the compilation. Native lookups can load code, which is not
  1.1280 +  // permitted during compilation.
  1.1281 +  //
  1.1282 +  // Note: A native method implies non-osr compilation which is
  1.1283 +  //       checked with an assertion at the entry of this method.
  1.1284 +  if (method->is_native() && !method->is_method_handle_intrinsic()) {
  1.1285 +    bool in_base_library;
  1.1286 +    address adr = NativeLookup::lookup(method, in_base_library, THREAD);
  1.1287 +    if (HAS_PENDING_EXCEPTION) {
  1.1288 +      // In case of an exception looking up the method, we just forget
  1.1289 +      // about it. The interpreter will kick-in and throw the exception.
  1.1290 +      method->set_not_compilable(); // implies is_not_osr_compilable()
  1.1291 +      CLEAR_PENDING_EXCEPTION;
  1.1292 +      return NULL;
  1.1293 +    }
  1.1294 +    assert(method->has_native_function(), "must have native code by now");
  1.1295 +  }
  1.1296 +
  1.1297 +  // RedefineClasses() has replaced this method; just return
  1.1298 +  if (method->is_old()) {
  1.1299 +    return NULL;
  1.1300 +  }
  1.1301 +
  1.1302 +  // JVMTI -- post_compile_event requires jmethod_id() that may require
  1.1303 +  // a lock the compiling thread can not acquire. Prefetch it here.
  1.1304 +  if (JvmtiExport::should_post_compiled_method_load()) {
  1.1305 +    method->jmethod_id();
  1.1306 +  }
  1.1307 +
  1.1308 +  // do the compilation
  1.1309 +  if (method->is_native()) {
  1.1310 +    if (!PreferInterpreterNativeStubs || method->is_method_handle_intrinsic()) {
  1.1311 +      // To properly handle the appendix argument for out-of-line calls we are using a small trampoline that
  1.1312 +      // pops off the appendix argument and jumps to the target (see gen_special_dispatch in SharedRuntime).
  1.1313 +      //
  1.1314 +      // Since normal compiled-to-compiled calls are not able to handle such a thing we MUST generate an adapter
  1.1315 +      // in this case.  If we can't generate one and use it we can not execute the out-of-line method handle calls.
  1.1316 +      AdapterHandlerLibrary::create_native_wrapper(method);
  1.1317 +    } else {
  1.1318 +      return NULL;
  1.1319 +    }
  1.1320 +  } else {
  1.1321 +    // If the compiler is shut off due to code cache getting full
  1.1322 +    // fail out now so blocking compiles dont hang the java thread
  1.1323 +    if (!should_compile_new_jobs()) {
  1.1324 +      CompilationPolicy::policy()->delay_compilation(method());
  1.1325 +      return NULL;
  1.1326 +    }
  1.1327 +    compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, THREAD);
  1.1328 +  }
  1.1329 +
  1.1330 +  // return requested nmethod
  1.1331 +  // We accept a higher level osr method
  1.1332 +  return osr_bci  == InvocationEntryBci ? method->code() : method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
  1.1333 +}
  1.1334 +
  1.1335 +
  1.1336 +// ------------------------------------------------------------------
  1.1337 +// CompileBroker::compilation_is_complete
  1.1338 +//
  1.1339 +// See if compilation of this method is already complete.
  1.1340 +bool CompileBroker::compilation_is_complete(methodHandle method,
  1.1341 +                                            int          osr_bci,
  1.1342 +                                            int          comp_level) {
  1.1343 +  bool is_osr = (osr_bci != standard_entry_bci);
  1.1344 +  if (is_osr) {
  1.1345 +    if (method->is_not_osr_compilable(comp_level)) {
  1.1346 +      return true;
  1.1347 +    } else {
  1.1348 +      nmethod* result = method->lookup_osr_nmethod_for(osr_bci, comp_level, true);
  1.1349 +      return (result != NULL);
  1.1350 +    }
  1.1351 +  } else {
  1.1352 +    if (method->is_not_compilable(comp_level)) {
  1.1353 +      return true;
  1.1354 +    } else {
  1.1355 +      nmethod* result = method->code();
  1.1356 +      if (result == NULL) return false;
  1.1357 +      return comp_level == result->comp_level();
  1.1358 +    }
  1.1359 +  }
  1.1360 +}
  1.1361 +
  1.1362 +
  1.1363 +// ------------------------------------------------------------------
  1.1364 +// CompileBroker::compilation_is_in_queue
  1.1365 +//
  1.1366 +// See if this compilation is already requested.
  1.1367 +//
  1.1368 +// Implementation note: there is only a single "is in queue" bit
  1.1369 +// for each method.  This means that the check below is overly
  1.1370 +// conservative in the sense that an osr compilation in the queue
  1.1371 +// will block a normal compilation from entering the queue (and vice
  1.1372 +// versa).  This can be remedied by a full queue search to disambiguate
  1.1373 +// cases.  If it is deemed profitible, this may be done.
  1.1374 +bool CompileBroker::compilation_is_in_queue(methodHandle method,
  1.1375 +                                            int          osr_bci) {
  1.1376 +  return method->queued_for_compilation();
  1.1377 +}
  1.1378 +
  1.1379 +// ------------------------------------------------------------------
  1.1380 +// CompileBroker::compilation_is_prohibited
  1.1381 +//
  1.1382 +// See if this compilation is not allowed.
  1.1383 +bool CompileBroker::compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level) {
  1.1384 +  bool is_native = method->is_native();
  1.1385 +  // Some compilers may not support the compilation of natives.
  1.1386 +  AbstractCompiler *comp = compiler(comp_level);
  1.1387 +  if (is_native &&
  1.1388 +      (!CICompileNatives || comp == NULL || !comp->supports_native())) {
  1.1389 +    method->set_not_compilable_quietly(comp_level);
  1.1390 +    return true;
  1.1391 +  }
  1.1392 +
  1.1393 +  bool is_osr = (osr_bci != standard_entry_bci);
  1.1394 +  // Some compilers may not support on stack replacement.
  1.1395 +  if (is_osr &&
  1.1396 +      (!CICompileOSR || comp == NULL || !comp->supports_osr())) {
  1.1397 +    method->set_not_osr_compilable(comp_level);
  1.1398 +    return true;
  1.1399 +  }
  1.1400 +
  1.1401 +  // The method may be explicitly excluded by the user.
  1.1402 +  bool quietly;
  1.1403 +  if (CompilerOracle::should_exclude(method, quietly)) {
  1.1404 +    if (!quietly) {
  1.1405 +      // This does not happen quietly...
  1.1406 +      ResourceMark rm;
  1.1407 +      tty->print("### Excluding %s:%s",
  1.1408 +                 method->is_native() ? "generation of native wrapper" : "compile",
  1.1409 +                 (method->is_static() ? " static" : ""));
  1.1410 +      method->print_short_name(tty);
  1.1411 +      tty->cr();
  1.1412 +    }
  1.1413 +    method->set_not_compilable(CompLevel_all, !quietly, "excluded by CompilerOracle");
  1.1414 +  }
  1.1415 +
  1.1416 +  return false;
  1.1417 +}
  1.1418 +
  1.1419 +/**
  1.1420 + * Generate serialized IDs for compilation requests. If certain debugging flags are used
  1.1421 + * and the ID is not within the specified range, the method is not compiled and 0 is returned.
  1.1422 + * The function also allows to generate separate compilation IDs for OSR compilations.
  1.1423 + */
  1.1424 +int CompileBroker::assign_compile_id(methodHandle method, int osr_bci) {
  1.1425 +#ifdef ASSERT
  1.1426 +  bool is_osr = (osr_bci != standard_entry_bci);
  1.1427 +  int id;
  1.1428 +  if (method->is_native()) {
  1.1429 +    assert(!is_osr, "can't be osr");
  1.1430 +    // Adapters, native wrappers and method handle intrinsics
  1.1431 +    // should be generated always.
  1.1432 +    return Atomic::add(1, &_compilation_id);
  1.1433 +  } else if (CICountOSR && is_osr) {
  1.1434 +    id = Atomic::add(1, &_osr_compilation_id);
  1.1435 +    if (CIStartOSR <= id && id < CIStopOSR) {
  1.1436 +      return id;
  1.1437 +    }
  1.1438 +  } else {
  1.1439 +    id = Atomic::add(1, &_compilation_id);
  1.1440 +    if (CIStart <= id && id < CIStop) {
  1.1441 +      return id;
  1.1442 +    }
  1.1443 +  }
  1.1444 +
  1.1445 +  // Method was not in the appropriate compilation range.
  1.1446 +  method->set_not_compilable_quietly();
  1.1447 +  return 0;
  1.1448 +#else
  1.1449 +  // CICountOSR is a develop flag and set to 'false' by default. In a product built,
  1.1450 +  // only _compilation_id is incremented.
  1.1451 +  return Atomic::add(1, &_compilation_id);
  1.1452 +#endif
  1.1453 +}
  1.1454 +
  1.1455 +
  1.1456 +// ------------------------------------------------------------------
  1.1457 +// CompileBroker::is_compile_blocking
  1.1458 +//
  1.1459 +// Should the current thread be blocked until this compilation request
  1.1460 +// has been fulfilled?
  1.1461 +bool CompileBroker::is_compile_blocking(methodHandle method, int osr_bci) {
  1.1462 +  assert(!InstanceRefKlass::owns_pending_list_lock(JavaThread::current()), "possible deadlock");
  1.1463 +  return !BackgroundCompilation;
  1.1464 +}
  1.1465 +
  1.1466 +
  1.1467 +// ------------------------------------------------------------------
  1.1468 +// CompileBroker::preload_classes
  1.1469 +void CompileBroker::preload_classes(methodHandle method, TRAPS) {
  1.1470 +  // Move this code over from c1_Compiler.cpp
  1.1471 +  ShouldNotReachHere();
  1.1472 +}
  1.1473 +
  1.1474 +
  1.1475 +// ------------------------------------------------------------------
  1.1476 +// CompileBroker::create_compile_task
  1.1477 +//
  1.1478 +// Create a CompileTask object representing the current request for
  1.1479 +// compilation.  Add this task to the queue.
  1.1480 +CompileTask* CompileBroker::create_compile_task(CompileQueue* queue,
  1.1481 +                                              int           compile_id,
  1.1482 +                                              methodHandle  method,
  1.1483 +                                              int           osr_bci,
  1.1484 +                                              int           comp_level,
  1.1485 +                                              methodHandle  hot_method,
  1.1486 +                                              int           hot_count,
  1.1487 +                                              const char*   comment,
  1.1488 +                                              bool          blocking) {
  1.1489 +  CompileTask* new_task = allocate_task();
  1.1490 +  new_task->initialize(compile_id, method, osr_bci, comp_level,
  1.1491 +                       hot_method, hot_count, comment,
  1.1492 +                       blocking);
  1.1493 +  queue->add(new_task);
  1.1494 +  return new_task;
  1.1495 +}
  1.1496 +
  1.1497 +
  1.1498 +// ------------------------------------------------------------------
  1.1499 +// CompileBroker::allocate_task
  1.1500 +//
  1.1501 +// Allocate a CompileTask, from the free list if possible.
  1.1502 +CompileTask* CompileBroker::allocate_task() {
  1.1503 +  MutexLocker locker(CompileTaskAlloc_lock);
  1.1504 +  CompileTask* task = NULL;
  1.1505 +  if (_task_free_list != NULL) {
  1.1506 +    task = _task_free_list;
  1.1507 +    _task_free_list = task->next();
  1.1508 +    task->set_next(NULL);
  1.1509 +  } else {
  1.1510 +    task = new CompileTask();
  1.1511 +    task->set_next(NULL);
  1.1512 +  }
  1.1513 +  return task;
  1.1514 +}
  1.1515 +
  1.1516 +
  1.1517 +// ------------------------------------------------------------------
  1.1518 +// CompileBroker::free_task
  1.1519 +//
  1.1520 +// Add a task to the free list.
  1.1521 +void CompileBroker::free_task(CompileTask* task) {
  1.1522 +  MutexLocker locker(CompileTaskAlloc_lock);
  1.1523 +  task->free();
  1.1524 +  task->set_next(_task_free_list);
  1.1525 +  _task_free_list = task;
  1.1526 +}
  1.1527 +
  1.1528 +
  1.1529 +// ------------------------------------------------------------------
  1.1530 +// CompileBroker::wait_for_completion
  1.1531 +//
  1.1532 +// Wait for the given method CompileTask to complete.
  1.1533 +void CompileBroker::wait_for_completion(CompileTask* task) {
  1.1534 +  if (CIPrintCompileQueue) {
  1.1535 +    tty->print_cr("BLOCKING FOR COMPILE");
  1.1536 +  }
  1.1537 +
  1.1538 +  assert(task->is_blocking(), "can only wait on blocking task");
  1.1539 +
  1.1540 +  JavaThread *thread = JavaThread::current();
  1.1541 +  thread->set_blocked_on_compilation(true);
  1.1542 +
  1.1543 +  methodHandle method(thread, task->method());
  1.1544 +  {
  1.1545 +    MutexLocker waiter(task->lock(), thread);
  1.1546 +
  1.1547 +    while (!task->is_complete())
  1.1548 +      task->lock()->wait();
  1.1549 +  }
  1.1550 +  // It is harmless to check this status without the lock, because
  1.1551 +  // completion is a stable property (until the task object is recycled).
  1.1552 +  assert(task->is_complete(), "Compilation should have completed");
  1.1553 +  assert(task->code_handle() == NULL, "must be reset");
  1.1554 +
  1.1555 +  thread->set_blocked_on_compilation(false);
  1.1556 +
  1.1557 +  // By convention, the waiter is responsible for recycling a
  1.1558 +  // blocking CompileTask. Since there is only one waiter ever
  1.1559 +  // waiting on a CompileTask, we know that no one else will
  1.1560 +  // be using this CompileTask; we can free it.
  1.1561 +  free_task(task);
  1.1562 +}
  1.1563 +
  1.1564 +// Initialize compiler thread(s) + compiler object(s). The postcondition
  1.1565 +// of this function is that the compiler runtimes are initialized and that
  1.1566 +//compiler threads can start compiling.
  1.1567 +bool CompileBroker::init_compiler_runtime() {
  1.1568 +  CompilerThread* thread = CompilerThread::current();
  1.1569 +  AbstractCompiler* comp = thread->compiler();
  1.1570 +  // Final sanity check - the compiler object must exist
  1.1571 +  guarantee(comp != NULL, "Compiler object must exist");
  1.1572 +
  1.1573 +  int system_dictionary_modification_counter;
  1.1574 +  {
  1.1575 +    MutexLocker locker(Compile_lock, thread);
  1.1576 +    system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
  1.1577 +  }
  1.1578 +
  1.1579 +  {
  1.1580 +    // Must switch to native to allocate ci_env
  1.1581 +    ThreadToNativeFromVM ttn(thread);
  1.1582 +    ciEnv ci_env(NULL, system_dictionary_modification_counter);
  1.1583 +    // Cache Jvmti state
  1.1584 +    ci_env.cache_jvmti_state();
  1.1585 +    // Cache DTrace flags
  1.1586 +    ci_env.cache_dtrace_flags();
  1.1587 +
  1.1588 +    // Switch back to VM state to do compiler initialization
  1.1589 +    ThreadInVMfromNative tv(thread);
  1.1590 +    ResetNoHandleMark rnhm;
  1.1591 +
  1.1592 +
  1.1593 +    if (!comp->is_shark()) {
  1.1594 +      // Perform per-thread and global initializations
  1.1595 +      comp->initialize();
  1.1596 +    }
  1.1597 +  }
  1.1598 +
  1.1599 +  if (comp->is_failed()) {
  1.1600 +    disable_compilation_forever();
  1.1601 +    // If compiler initialization failed, no compiler thread that is specific to a
  1.1602 +    // particular compiler runtime will ever start to compile methods.
  1.1603 +
  1.1604 +    shutdown_compiler_runtime(comp, thread);
  1.1605 +    return false;
  1.1606 +  }
  1.1607 +
  1.1608 +  // C1 specific check
  1.1609 +  if (comp->is_c1() && (thread->get_buffer_blob() == NULL)) {
  1.1610 +    warning("Initialization of %s thread failed (no space to run compilers)", thread->name());
  1.1611 +    return false;
  1.1612 +  }
  1.1613 +
  1.1614 +  return true;
  1.1615 +}
  1.1616 +
  1.1617 +// If C1 and/or C2 initialization failed, we shut down all compilation.
  1.1618 +// We do this to keep things simple. This can be changed if it ever turns out to be
  1.1619 +// a problem.
  1.1620 +void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) {
  1.1621 +  // Free buffer blob, if allocated
  1.1622 +  if (thread->get_buffer_blob() != NULL) {
  1.1623 +    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
  1.1624 +    CodeCache::free(thread->get_buffer_blob());
  1.1625 +  }
  1.1626 +
  1.1627 +  if (comp->should_perform_shutdown()) {
  1.1628 +    // There are two reasons for shutting down the compiler
  1.1629 +    // 1) compiler runtime initialization failed
  1.1630 +    // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing
  1.1631 +    warning("Shutting down compiler %s (no space to run compilers)", comp->name());
  1.1632 +
  1.1633 +    // Only one thread per compiler runtime object enters here
  1.1634 +    // Set state to shut down
  1.1635 +    comp->set_shut_down();
  1.1636 +
  1.1637 +    MutexLocker mu(MethodCompileQueue_lock, thread);
  1.1638 +    CompileQueue* queue;
  1.1639 +    if (_c1_method_queue != NULL) {
  1.1640 +      _c1_method_queue->delete_all();
  1.1641 +      queue = _c1_method_queue;
  1.1642 +      _c1_method_queue = NULL;
  1.1643 +      delete _c1_method_queue;
  1.1644 +    }
  1.1645 +
  1.1646 +    if (_c2_method_queue != NULL) {
  1.1647 +      _c2_method_queue->delete_all();
  1.1648 +      queue = _c2_method_queue;
  1.1649 +      _c2_method_queue = NULL;
  1.1650 +      delete _c2_method_queue;
  1.1651 +    }
  1.1652 +
  1.1653 +    // We could delete compiler runtimes also. However, there are references to
  1.1654 +    // the compiler runtime(s) (e.g.,  nmethod::is_compiled_by_c1()) which then
  1.1655 +    // fail. This can be done later if necessary.
  1.1656 +  }
  1.1657 +}
  1.1658 +
  1.1659 +// ------------------------------------------------------------------
  1.1660 +// CompileBroker::compiler_thread_loop
  1.1661 +//
  1.1662 +// The main loop run by a CompilerThread.
  1.1663 +void CompileBroker::compiler_thread_loop() {
  1.1664 +  CompilerThread* thread = CompilerThread::current();
  1.1665 +  CompileQueue* queue = thread->queue();
  1.1666 +  // For the thread that initializes the ciObjectFactory
  1.1667 +  // this resource mark holds all the shared objects
  1.1668 +  ResourceMark rm;
  1.1669 +
  1.1670 +  // First thread to get here will initialize the compiler interface
  1.1671 +
  1.1672 +  if (!ciObjectFactory::is_initialized()) {
  1.1673 +    ASSERT_IN_VM;
  1.1674 +    MutexLocker only_one (CompileThread_lock, thread);
  1.1675 +    if (!ciObjectFactory::is_initialized()) {
  1.1676 +      ciObjectFactory::initialize();
  1.1677 +    }
  1.1678 +  }
  1.1679 +
  1.1680 +  // Open a log.
  1.1681 +  if (LogCompilation) {
  1.1682 +    init_compiler_thread_log();
  1.1683 +  }
  1.1684 +  CompileLog* log = thread->log();
  1.1685 +  if (log != NULL) {
  1.1686 +    log->begin_elem("start_compile_thread name='%s' thread='" UINTX_FORMAT "' process='%d'",
  1.1687 +                    thread->name(),
  1.1688 +                    os::current_thread_id(),
  1.1689 +                    os::current_process_id());
  1.1690 +    log->stamp();
  1.1691 +    log->end_elem();
  1.1692 +  }
  1.1693 +
  1.1694 +  // If compiler thread/runtime initialization fails, exit the compiler thread
  1.1695 +  if (!init_compiler_runtime()) {
  1.1696 +    return;
  1.1697 +  }
  1.1698 +
  1.1699 +  // Poll for new compilation tasks as long as the JVM runs. Compilation
  1.1700 +  // should only be disabled if something went wrong while initializing the
  1.1701 +  // compiler runtimes. This, in turn, should not happen. The only known case
  1.1702 +  // when compiler runtime initialization fails is if there is not enough free
  1.1703 +  // space in the code cache to generate the necessary stubs, etc.
  1.1704 +  while (!is_compilation_disabled_forever()) {
  1.1705 +    // We need this HandleMark to avoid leaking VM handles.
  1.1706 +    HandleMark hm(thread);
  1.1707 +
  1.1708 +    if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) {
  1.1709 +      // the code cache is really full
  1.1710 +      handle_full_code_cache();
  1.1711 +    }
  1.1712 +
  1.1713 +    CompileTask* task = queue->get();
  1.1714 +    if (task == NULL) {
  1.1715 +      continue;
  1.1716 +    }
  1.1717 +
  1.1718 +    // Give compiler threads an extra quanta.  They tend to be bursty and
  1.1719 +    // this helps the compiler to finish up the job.
  1.1720 +    if( CompilerThreadHintNoPreempt )
  1.1721 +      os::hint_no_preempt();
  1.1722 +
  1.1723 +    // trace per thread time and compile statistics
  1.1724 +    CompilerCounters* counters = ((CompilerThread*)thread)->counters();
  1.1725 +    PerfTraceTimedEvent(counters->time_counter(), counters->compile_counter());
  1.1726 +
  1.1727 +    // Assign the task to the current thread.  Mark this compilation
  1.1728 +    // thread as active for the profiler.
  1.1729 +    CompileTaskWrapper ctw(task);
  1.1730 +    nmethodLocker result_handle;  // (handle for the nmethod produced by this task)
  1.1731 +    task->set_code_handle(&result_handle);
  1.1732 +    methodHandle method(thread, task->method());
  1.1733 +
  1.1734 +    // Never compile a method if breakpoints are present in it
  1.1735 +    if (method()->number_of_breakpoints() == 0) {
  1.1736 +      // Compile the method.
  1.1737 +      if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
  1.1738 +#ifdef COMPILER1
  1.1739 +        // Allow repeating compilations for the purpose of benchmarking
  1.1740 +        // compile speed. This is not useful for customers.
  1.1741 +        if (CompilationRepeat != 0) {
  1.1742 +          int compile_count = CompilationRepeat;
  1.1743 +          while (compile_count > 0) {
  1.1744 +            invoke_compiler_on_method(task);
  1.1745 +            nmethod* nm = method->code();
  1.1746 +            if (nm != NULL) {
  1.1747 +              nm->make_zombie();
  1.1748 +              method->clear_code();
  1.1749 +            }
  1.1750 +            compile_count--;
  1.1751 +          }
  1.1752 +        }
  1.1753 +#endif /* COMPILER1 */
  1.1754 +        invoke_compiler_on_method(task);
  1.1755 +      } else {
  1.1756 +        // After compilation is disabled, remove remaining methods from queue
  1.1757 +        method->clear_queued_for_compilation();
  1.1758 +      }
  1.1759 +    }
  1.1760 +  }
  1.1761 +
  1.1762 +  // Shut down compiler runtime
  1.1763 +  shutdown_compiler_runtime(thread->compiler(), thread);
  1.1764 +}
  1.1765 +
  1.1766 +// ------------------------------------------------------------------
  1.1767 +// CompileBroker::init_compiler_thread_log
  1.1768 +//
  1.1769 +// Set up state required by +LogCompilation.
  1.1770 +void CompileBroker::init_compiler_thread_log() {
  1.1771 +    CompilerThread* thread = CompilerThread::current();
  1.1772 +    char  file_name[4*K];
  1.1773 +    FILE* fp = NULL;
  1.1774 +    intx thread_id = os::current_thread_id();
  1.1775 +    for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) {
  1.1776 +      const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL);
  1.1777 +      if (dir == NULL) {
  1.1778 +        jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log",
  1.1779 +                     thread_id, os::current_process_id());
  1.1780 +      } else {
  1.1781 +        jio_snprintf(file_name, sizeof(file_name),
  1.1782 +                     "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir,
  1.1783 +                     os::file_separator(), thread_id, os::current_process_id());
  1.1784 +      }
  1.1785 +
  1.1786 +      fp = fopen(file_name, "at");
  1.1787 +      if (fp != NULL) {
  1.1788 +        if (LogCompilation && Verbose) {
  1.1789 +          tty->print_cr("Opening compilation log %s", file_name);
  1.1790 +        }
  1.1791 +        CompileLog* log = new(ResourceObj::C_HEAP, mtCompiler) CompileLog(file_name, fp, thread_id);
  1.1792 +        thread->init_log(log);
  1.1793 +
  1.1794 +        if (xtty != NULL) {
  1.1795 +          ttyLocker ttyl;
  1.1796 +          // Record any per thread log files
  1.1797 +          xtty->elem("thread_logfile thread='" INTX_FORMAT "' filename='%s'", thread_id, file_name);
  1.1798 +        }
  1.1799 +        return;
  1.1800 +      }
  1.1801 +    }
  1.1802 +    warning("Cannot open log file: %s", file_name);
  1.1803 +}
  1.1804 +
  1.1805 +// ------------------------------------------------------------------
  1.1806 +// CompileBroker::set_should_block
  1.1807 +//
  1.1808 +// Set _should_block.
  1.1809 +// Call this from the VM, with Threads_lock held and a safepoint requested.
  1.1810 +void CompileBroker::set_should_block() {
  1.1811 +  assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
  1.1812 +  assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint already");
  1.1813 +#ifndef PRODUCT
  1.1814 +  if (PrintCompilation && (Verbose || WizardMode))
  1.1815 +    tty->print_cr("notifying compiler thread pool to block");
  1.1816 +#endif
  1.1817 +  _should_block = true;
  1.1818 +}
  1.1819 +
  1.1820 +// ------------------------------------------------------------------
  1.1821 +// CompileBroker::maybe_block
  1.1822 +//
  1.1823 +// Call this from the compiler at convenient points, to poll for _should_block.
  1.1824 +void CompileBroker::maybe_block() {
  1.1825 +  if (_should_block) {
  1.1826 +#ifndef PRODUCT
  1.1827 +    if (PrintCompilation && (Verbose || WizardMode))
  1.1828 +      tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current()));
  1.1829 +#endif
  1.1830 +    ThreadInVMfromNative tivfn(JavaThread::current());
  1.1831 +  }
  1.1832 +}
  1.1833 +
  1.1834 +// wrapper for CodeCache::print_summary()
  1.1835 +static void codecache_print(bool detailed)
  1.1836 +{
  1.1837 +  ResourceMark rm;
  1.1838 +  stringStream s;
  1.1839 +  // Dump code cache  into a buffer before locking the tty,
  1.1840 +  {
  1.1841 +    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
  1.1842 +    CodeCache::print_summary(&s, detailed);
  1.1843 +  }
  1.1844 +  ttyLocker ttyl;
  1.1845 +  tty->print("%s", s.as_string());
  1.1846 +}
  1.1847 +
  1.1848 +// ------------------------------------------------------------------
  1.1849 +// CompileBroker::invoke_compiler_on_method
  1.1850 +//
  1.1851 +// Compile a method.
  1.1852 +//
  1.1853 +void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
  1.1854 +  if (PrintCompilation) {
  1.1855 +    ResourceMark rm;
  1.1856 +    task->print_line();
  1.1857 +  }
  1.1858 +  elapsedTimer time;
  1.1859 +
  1.1860 +  CompilerThread* thread = CompilerThread::current();
  1.1861 +  ResourceMark rm(thread);
  1.1862 +
  1.1863 +  if (LogEvents) {
  1.1864 +    _compilation_log->log_compile(thread, task);
  1.1865 +  }
  1.1866 +
  1.1867 +  // Common flags.
  1.1868 +  uint compile_id = task->compile_id();
  1.1869 +  int osr_bci = task->osr_bci();
  1.1870 +  bool is_osr = (osr_bci != standard_entry_bci);
  1.1871 +  bool should_log = (thread->log() != NULL);
  1.1872 +  bool should_break = false;
  1.1873 +  int task_level = task->comp_level();
  1.1874 +  {
  1.1875 +    // create the handle inside it's own block so it can't
  1.1876 +    // accidentally be referenced once the thread transitions to
  1.1877 +    // native.  The NoHandleMark before the transition should catch
  1.1878 +    // any cases where this occurs in the future.
  1.1879 +    methodHandle method(thread, task->method());
  1.1880 +    should_break = check_break_at(method, compile_id, is_osr);
  1.1881 +    if (should_log && !CompilerOracle::should_log(method)) {
  1.1882 +      should_log = false;
  1.1883 +    }
  1.1884 +    assert(!method->is_native(), "no longer compile natives");
  1.1885 +
  1.1886 +    // Save information about this method in case of failure.
  1.1887 +    set_last_compile(thread, method, is_osr, task_level);
  1.1888 +
  1.1889 +    DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level));
  1.1890 +  }
  1.1891 +
  1.1892 +  // Allocate a new set of JNI handles.
  1.1893 +  push_jni_handle_block();
  1.1894 +  Method* target_handle = task->method();
  1.1895 +  int compilable = ciEnv::MethodCompilable;
  1.1896 +  {
  1.1897 +    int system_dictionary_modification_counter;
  1.1898 +    {
  1.1899 +      MutexLocker locker(Compile_lock, thread);
  1.1900 +      system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
  1.1901 +    }
  1.1902 +
  1.1903 +    NoHandleMark  nhm;
  1.1904 +    ThreadToNativeFromVM ttn(thread);
  1.1905 +
  1.1906 +    ciEnv ci_env(task, system_dictionary_modification_counter);
  1.1907 +    if (should_break) {
  1.1908 +      ci_env.set_break_at_compile(true);
  1.1909 +    }
  1.1910 +    if (should_log) {
  1.1911 +      ci_env.set_log(thread->log());
  1.1912 +    }
  1.1913 +    assert(thread->env() == &ci_env, "set by ci_env");
  1.1914 +    // The thread-env() field is cleared in ~CompileTaskWrapper.
  1.1915 +
  1.1916 +    // Cache Jvmti state
  1.1917 +    ci_env.cache_jvmti_state();
  1.1918 +
  1.1919 +    // Cache DTrace flags
  1.1920 +    ci_env.cache_dtrace_flags();
  1.1921 +
  1.1922 +    ciMethod* target = ci_env.get_method_from_handle(target_handle);
  1.1923 +
  1.1924 +    TraceTime t1("compilation", &time);
  1.1925 +    EventCompilation event;
  1.1926 +
  1.1927 +    AbstractCompiler *comp = compiler(task_level);
  1.1928 +    if (comp == NULL) {
  1.1929 +      ci_env.record_method_not_compilable("no compiler", !TieredCompilation);
  1.1930 +    } else {
  1.1931 +      comp->compile_method(&ci_env, target, osr_bci);
  1.1932 +    }
  1.1933 +
  1.1934 +    if (!ci_env.failing() && task->code() == NULL) {
  1.1935 +      //assert(false, "compiler should always document failure");
  1.1936 +      // The compiler elected, without comment, not to register a result.
  1.1937 +      // Do not attempt further compilations of this method.
  1.1938 +      ci_env.record_method_not_compilable("compile failed", !TieredCompilation);
  1.1939 +    }
  1.1940 +
  1.1941 +    // Copy this bit to the enclosing block:
  1.1942 +    compilable = ci_env.compilable();
  1.1943 +
  1.1944 +    if (ci_env.failing()) {
  1.1945 +      const char* retry_message = ci_env.retry_message();
  1.1946 +      if (_compilation_log != NULL) {
  1.1947 +        _compilation_log->log_failure(thread, task, ci_env.failure_reason(), retry_message);
  1.1948 +      }
  1.1949 +      if (PrintCompilation) {
  1.1950 +        FormatBufferResource msg = retry_message != NULL ?
  1.1951 +            err_msg_res("COMPILE SKIPPED: %s (%s)", ci_env.failure_reason(), retry_message) :
  1.1952 +            err_msg_res("COMPILE SKIPPED: %s",      ci_env.failure_reason());
  1.1953 +        task->print_compilation(tty, msg);
  1.1954 +      }
  1.1955 +    } else {
  1.1956 +      task->mark_success();
  1.1957 +      task->set_num_inlined_bytecodes(ci_env.num_inlined_bytecodes());
  1.1958 +      if (_compilation_log != NULL) {
  1.1959 +        nmethod* code = task->code();
  1.1960 +        if (code != NULL) {
  1.1961 +          _compilation_log->log_nmethod(thread, code);
  1.1962 +        }
  1.1963 +      }
  1.1964 +    }
  1.1965 +    // simulate crash during compilation
  1.1966 +    assert(task->compile_id() != CICrashAt, "just as planned");
  1.1967 +    if (event.should_commit()) {
  1.1968 +      event.set_method(target->get_Method());
  1.1969 +      event.set_compileID(compile_id);
  1.1970 +      event.set_compileLevel(task->comp_level());
  1.1971 +      event.set_succeded(task->is_success());
  1.1972 +      event.set_isOsr(is_osr);
  1.1973 +      event.set_codeSize((task->code() == NULL) ? 0 : task->code()->total_size());
  1.1974 +      event.set_inlinedBytes(task->num_inlined_bytecodes());
  1.1975 +      event.commit();
  1.1976 +    }
  1.1977 +  }
  1.1978 +  pop_jni_handle_block();
  1.1979 +
  1.1980 +  methodHandle method(thread, task->method());
  1.1981 +
  1.1982 +  DTRACE_METHOD_COMPILE_END_PROBE(method, compiler_name(task_level), task->is_success());
  1.1983 +
  1.1984 +  collect_statistics(thread, time, task);
  1.1985 +
  1.1986 +  if (PrintCompilation && PrintCompilation2) {
  1.1987 +    tty->print("%7d ", (int) tty->time_stamp().milliseconds());  // print timestamp
  1.1988 +    tty->print("%4d ", compile_id);    // print compilation number
  1.1989 +    tty->print("%s ", (is_osr ? "%" : " "));
  1.1990 +    if (task->code() != NULL) {
  1.1991 +      tty->print("size: %d(%d) ", task->code()->total_size(), task->code()->insts_size());
  1.1992 +    }
  1.1993 +    tty->print_cr("time: %d inlined: %d bytes", (int)time.milliseconds(), task->num_inlined_bytecodes());
  1.1994 +  }
  1.1995 +
  1.1996 +  if (PrintCodeCacheOnCompilation)
  1.1997 +    codecache_print(/* detailed= */ false);
  1.1998 +
  1.1999 +  // Disable compilation, if required.
  1.2000 +  switch (compilable) {
  1.2001 +  case ciEnv::MethodCompilable_never:
  1.2002 +    if (is_osr)
  1.2003 +      method->set_not_osr_compilable_quietly();
  1.2004 +    else
  1.2005 +      method->set_not_compilable_quietly();
  1.2006 +    break;
  1.2007 +  case ciEnv::MethodCompilable_not_at_tier:
  1.2008 +    if (is_osr)
  1.2009 +      method->set_not_osr_compilable_quietly(task_level);
  1.2010 +    else
  1.2011 +      method->set_not_compilable_quietly(task_level);
  1.2012 +    break;
  1.2013 +  }
  1.2014 +
  1.2015 +  // Note that the queued_for_compilation bits are cleared without
  1.2016 +  // protection of a mutex. [They were set by the requester thread,
  1.2017 +  // when adding the task to the complie queue -- at which time the
  1.2018 +  // compile queue lock was held. Subsequently, we acquired the compile
  1.2019 +  // queue lock to get this task off the compile queue; thus (to belabour
  1.2020 +  // the point somewhat) our clearing of the bits must be occurring
  1.2021 +  // only after the setting of the bits. See also 14012000 above.
  1.2022 +  method->clear_queued_for_compilation();
  1.2023 +
  1.2024 +#ifdef ASSERT
  1.2025 +  if (CollectedHeap::fired_fake_oom()) {
  1.2026 +    // The current compile received a fake OOM during compilation so
  1.2027 +    // go ahead and exit the VM since the test apparently succeeded
  1.2028 +    tty->print_cr("*** Shutting down VM after successful fake OOM");
  1.2029 +    vm_exit(0);
  1.2030 +  }
  1.2031 +#endif
  1.2032 +}
  1.2033 +
  1.2034 +/**
  1.2035 + * The CodeCache is full.  Print out warning and disable compilation
  1.2036 + * or try code cache cleaning so compilation can continue later.
  1.2037 + */
  1.2038 +void CompileBroker::handle_full_code_cache() {
  1.2039 +  UseInterpreter = true;
  1.2040 +  if (UseCompiler || AlwaysCompileLoopMethods ) {
  1.2041 +    if (xtty != NULL) {
  1.2042 +      ResourceMark rm;
  1.2043 +      stringStream s;
  1.2044 +      // Dump code cache state into a buffer before locking the tty,
  1.2045 +      // because log_state() will use locks causing lock conflicts.
  1.2046 +      CodeCache::log_state(&s);
  1.2047 +      // Lock to prevent tearing
  1.2048 +      ttyLocker ttyl;
  1.2049 +      xtty->begin_elem("code_cache_full");
  1.2050 +      xtty->print("%s", s.as_string());
  1.2051 +      xtty->stamp();
  1.2052 +      xtty->end_elem();
  1.2053 +    }
  1.2054 +
  1.2055 +    CodeCache::report_codemem_full();
  1.2056 +
  1.2057 +#ifndef PRODUCT
  1.2058 +    if (CompileTheWorld || ExitOnFullCodeCache) {
  1.2059 +      codecache_print(/* detailed= */ true);
  1.2060 +      before_exit(JavaThread::current());
  1.2061 +      exit_globals(); // will delete tty
  1.2062 +      vm_direct_exit(CompileTheWorld ? 0 : 1);
  1.2063 +    }
  1.2064 +#endif
  1.2065 +    if (UseCodeCacheFlushing) {
  1.2066 +      // Since code cache is full, immediately stop new compiles
  1.2067 +      if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) {
  1.2068 +        NMethodSweeper::log_sweep("disable_compiler");
  1.2069 +      }
  1.2070 +      // Switch to 'vm_state'. This ensures that possibly_sweep() can be called
  1.2071 +      // without having to consider the state in which the current thread is.
  1.2072 +      ThreadInVMfromUnknown in_vm;
  1.2073 +      NMethodSweeper::possibly_sweep();
  1.2074 +    } else {
  1.2075 +      disable_compilation_forever();
  1.2076 +    }
  1.2077 +
  1.2078 +    // Print warning only once
  1.2079 +    if (should_print_compiler_warning()) {
  1.2080 +      warning("CodeCache is full. Compiler has been disabled.");
  1.2081 +      warning("Try increasing the code cache size using -XX:ReservedCodeCacheSize=");
  1.2082 +      codecache_print(/* detailed= */ true);
  1.2083 +    }
  1.2084 +  }
  1.2085 +}
  1.2086 +
  1.2087 +// ------------------------------------------------------------------
  1.2088 +// CompileBroker::set_last_compile
  1.2089 +//
  1.2090 +// Record this compilation for debugging purposes.
  1.2091 +void CompileBroker::set_last_compile(CompilerThread* thread, methodHandle method, bool is_osr, int comp_level) {
  1.2092 +  ResourceMark rm;
  1.2093 +  char* method_name = method->name()->as_C_string();
  1.2094 +  strncpy(_last_method_compiled, method_name, CompileBroker::name_buffer_length);
  1.2095 +  _last_method_compiled[CompileBroker::name_buffer_length - 1] = '\0'; // ensure null terminated
  1.2096 +  char current_method[CompilerCounters::cmname_buffer_length];
  1.2097 +  size_t maxLen = CompilerCounters::cmname_buffer_length;
  1.2098 +
  1.2099 +  if (UsePerfData) {
  1.2100 +    const char* class_name = method->method_holder()->name()->as_C_string();
  1.2101 +
  1.2102 +    size_t s1len = strlen(class_name);
  1.2103 +    size_t s2len = strlen(method_name);
  1.2104 +
  1.2105 +    // check if we need to truncate the string
  1.2106 +    if (s1len + s2len + 2 > maxLen) {
  1.2107 +
  1.2108 +      // the strategy is to lop off the leading characters of the
  1.2109 +      // class name and the trailing characters of the method name.
  1.2110 +
  1.2111 +      if (s2len + 2 > maxLen) {
  1.2112 +        // lop of the entire class name string, let snprintf handle
  1.2113 +        // truncation of the method name.
  1.2114 +        class_name += s1len; // null string
  1.2115 +      }
  1.2116 +      else {
  1.2117 +        // lop off the extra characters from the front of the class name
  1.2118 +        class_name += ((s1len + s2len + 2) - maxLen);
  1.2119 +      }
  1.2120 +    }
  1.2121 +
  1.2122 +    jio_snprintf(current_method, maxLen, "%s %s", class_name, method_name);
  1.2123 +  }
  1.2124 +
  1.2125 +  if (CICountOSR && is_osr) {
  1.2126 +    _last_compile_type = osr_compile;
  1.2127 +  } else {
  1.2128 +    _last_compile_type = normal_compile;
  1.2129 +  }
  1.2130 +  _last_compile_level = comp_level;
  1.2131 +
  1.2132 +  if (UsePerfData) {
  1.2133 +    CompilerCounters* counters = thread->counters();
  1.2134 +    counters->set_current_method(current_method);
  1.2135 +    counters->set_compile_type((jlong)_last_compile_type);
  1.2136 +  }
  1.2137 +}
  1.2138 +
  1.2139 +
  1.2140 +// ------------------------------------------------------------------
  1.2141 +// CompileBroker::push_jni_handle_block
  1.2142 +//
  1.2143 +// Push on a new block of JNI handles.
  1.2144 +void CompileBroker::push_jni_handle_block() {
  1.2145 +  JavaThread* thread = JavaThread::current();
  1.2146 +
  1.2147 +  // Allocate a new block for JNI handles.
  1.2148 +  // Inlined code from jni_PushLocalFrame()
  1.2149 +  JNIHandleBlock* java_handles = thread->active_handles();
  1.2150 +  JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);
  1.2151 +  assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");
  1.2152 +  compile_handles->set_pop_frame_link(java_handles);  // make sure java handles get gc'd.
  1.2153 +  thread->set_active_handles(compile_handles);
  1.2154 +}
  1.2155 +
  1.2156 +
  1.2157 +// ------------------------------------------------------------------
  1.2158 +// CompileBroker::pop_jni_handle_block
  1.2159 +//
  1.2160 +// Pop off the current block of JNI handles.
  1.2161 +void CompileBroker::pop_jni_handle_block() {
  1.2162 +  JavaThread* thread = JavaThread::current();
  1.2163 +
  1.2164 +  // Release our JNI handle block
  1.2165 +  JNIHandleBlock* compile_handles = thread->active_handles();
  1.2166 +  JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
  1.2167 +  thread->set_active_handles(java_handles);
  1.2168 +  compile_handles->set_pop_frame_link(NULL);
  1.2169 +  JNIHandleBlock::release_block(compile_handles, thread); // may block
  1.2170 +}
  1.2171 +
  1.2172 +
  1.2173 +// ------------------------------------------------------------------
  1.2174 +// CompileBroker::check_break_at
  1.2175 +//
  1.2176 +// Should the compilation break at the current compilation.
  1.2177 +bool CompileBroker::check_break_at(methodHandle method, int compile_id, bool is_osr) {
  1.2178 +  if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) {
  1.2179 +    return true;
  1.2180 +  } else if( CompilerOracle::should_break_at(method) ) { // break when compiling
  1.2181 +    return true;
  1.2182 +  } else {
  1.2183 +    return (compile_id == CIBreakAt);
  1.2184 +  }
  1.2185 +}
  1.2186 +
  1.2187 +// ------------------------------------------------------------------
  1.2188 +// CompileBroker::collect_statistics
  1.2189 +//
  1.2190 +// Collect statistics about the compilation.
  1.2191 +
  1.2192 +void CompileBroker::collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task) {
  1.2193 +  bool success = task->is_success();
  1.2194 +  methodHandle method (thread, task->method());
  1.2195 +  uint compile_id = task->compile_id();
  1.2196 +  bool is_osr = (task->osr_bci() != standard_entry_bci);
  1.2197 +  nmethod* code = task->code();
  1.2198 +  CompilerCounters* counters = thread->counters();
  1.2199 +
  1.2200 +  assert(code == NULL || code->is_locked_by_vm(), "will survive the MutexLocker");
  1.2201 +  MutexLocker locker(CompileStatistics_lock);
  1.2202 +
  1.2203 +  // _perf variables are production performance counters which are
  1.2204 +  // updated regardless of the setting of the CITime and CITimeEach flags
  1.2205 +  //
  1.2206 +  if (!success) {
  1.2207 +    _total_bailout_count++;
  1.2208 +    if (UsePerfData) {
  1.2209 +      _perf_last_failed_method->set_value(counters->current_method());
  1.2210 +      _perf_last_failed_type->set_value(counters->compile_type());
  1.2211 +      _perf_total_bailout_count->inc();
  1.2212 +    }
  1.2213 +  } else if (code == NULL) {
  1.2214 +    if (UsePerfData) {
  1.2215 +      _perf_last_invalidated_method->set_value(counters->current_method());
  1.2216 +      _perf_last_invalidated_type->set_value(counters->compile_type());
  1.2217 +      _perf_total_invalidated_count->inc();
  1.2218 +    }
  1.2219 +    _total_invalidated_count++;
  1.2220 +  } else {
  1.2221 +    // Compilation succeeded
  1.2222 +
  1.2223 +    // update compilation ticks - used by the implementation of
  1.2224 +    // java.lang.management.CompilationMBean
  1.2225 +    _perf_total_compilation->inc(time.ticks());
  1.2226 +
  1.2227 +    _t_total_compilation.add(time);
  1.2228 +    _peak_compilation_time = time.milliseconds() > _peak_compilation_time ? time.milliseconds() : _peak_compilation_time;
  1.2229 +
  1.2230 +    if (CITime) {
  1.2231 +      if (is_osr) {
  1.2232 +        _t_osr_compilation.add(time);
  1.2233 +        _sum_osr_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();
  1.2234 +      } else {
  1.2235 +        _t_standard_compilation.add(time);
  1.2236 +        _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();
  1.2237 +      }
  1.2238 +    }
  1.2239 +
  1.2240 +    if (UsePerfData) {
  1.2241 +      // save the name of the last method compiled
  1.2242 +      _perf_last_method->set_value(counters->current_method());
  1.2243 +      _perf_last_compile_type->set_value(counters->compile_type());
  1.2244 +      _perf_last_compile_size->set_value(method->code_size() +
  1.2245 +                                         task->num_inlined_bytecodes());
  1.2246 +      if (is_osr) {
  1.2247 +        _perf_osr_compilation->inc(time.ticks());
  1.2248 +        _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
  1.2249 +      } else {
  1.2250 +        _perf_standard_compilation->inc(time.ticks());
  1.2251 +        _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
  1.2252 +      }
  1.2253 +    }
  1.2254 +
  1.2255 +    if (CITimeEach) {
  1.2256 +      float bytes_per_sec = 1.0 * (method->code_size() + task->num_inlined_bytecodes()) / time.seconds();
  1.2257 +      tty->print_cr("%3d   seconds: %f bytes/sec : %f (bytes %d + %d inlined)",
  1.2258 +                    compile_id, time.seconds(), bytes_per_sec, method->code_size(), task->num_inlined_bytecodes());
  1.2259 +    }
  1.2260 +
  1.2261 +    // Collect counts of successful compilations
  1.2262 +    _sum_nmethod_size      += code->total_size();
  1.2263 +    _sum_nmethod_code_size += code->insts_size();
  1.2264 +    _total_compile_count++;
  1.2265 +
  1.2266 +    if (UsePerfData) {
  1.2267 +      _perf_sum_nmethod_size->inc(     code->total_size());
  1.2268 +      _perf_sum_nmethod_code_size->inc(code->insts_size());
  1.2269 +      _perf_total_compile_count->inc();
  1.2270 +    }
  1.2271 +
  1.2272 +    if (is_osr) {
  1.2273 +      if (UsePerfData) _perf_total_osr_compile_count->inc();
  1.2274 +      _total_osr_compile_count++;
  1.2275 +    } else {
  1.2276 +      if (UsePerfData) _perf_total_standard_compile_count->inc();
  1.2277 +      _total_standard_compile_count++;
  1.2278 +    }
  1.2279 +  }
  1.2280 +  // set the current method for the thread to null
  1.2281 +  if (UsePerfData) counters->set_current_method("");
  1.2282 +}
  1.2283 +
  1.2284 +const char* CompileBroker::compiler_name(int comp_level) {
  1.2285 +  AbstractCompiler *comp = CompileBroker::compiler(comp_level);
  1.2286 +  if (comp == NULL) {
  1.2287 +    return "no compiler";
  1.2288 +  } else {
  1.2289 +    return (comp->name());
  1.2290 +  }
  1.2291 +}
  1.2292 +
  1.2293 +void CompileBroker::print_times() {
  1.2294 +  tty->cr();
  1.2295 +  tty->print_cr("Accumulated compiler times (for compiled methods only)");
  1.2296 +  tty->print_cr("------------------------------------------------");
  1.2297 +               //0000000000111111111122222222223333333333444444444455555555556666666666
  1.2298 +               //0123456789012345678901234567890123456789012345678901234567890123456789
  1.2299 +  tty->print_cr("  Total compilation time   : %6.3f s", CompileBroker::_t_total_compilation.seconds());
  1.2300 +  tty->print_cr("    Standard compilation   : %6.3f s, Average : %2.3f",
  1.2301 +                CompileBroker::_t_standard_compilation.seconds(),
  1.2302 +                CompileBroker::_t_standard_compilation.seconds() / CompileBroker::_total_standard_compile_count);
  1.2303 +  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.2304 +
  1.2305 +  AbstractCompiler *comp = compiler(CompLevel_simple);
  1.2306 +  if (comp != NULL) {
  1.2307 +    comp->print_timers();
  1.2308 +  }
  1.2309 +  comp = compiler(CompLevel_full_optimization);
  1.2310 +  if (comp != NULL) {
  1.2311 +    comp->print_timers();
  1.2312 +  }
  1.2313 +  tty->cr();
  1.2314 +  tty->print_cr("  Total compiled methods   : %6d methods", CompileBroker::_total_compile_count);
  1.2315 +  tty->print_cr("    Standard compilation   : %6d methods", CompileBroker::_total_standard_compile_count);
  1.2316 +  tty->print_cr("    On stack replacement   : %6d methods", CompileBroker::_total_osr_compile_count);
  1.2317 +  int tcb = CompileBroker::_sum_osr_bytes_compiled + CompileBroker::_sum_standard_bytes_compiled;
  1.2318 +  tty->print_cr("  Total compiled bytecodes : %6d bytes", tcb);
  1.2319 +  tty->print_cr("    Standard compilation   : %6d bytes", CompileBroker::_sum_standard_bytes_compiled);
  1.2320 +  tty->print_cr("    On stack replacement   : %6d bytes", CompileBroker::_sum_osr_bytes_compiled);
  1.2321 +  int bps = (int)(tcb / CompileBroker::_t_total_compilation.seconds());
  1.2322 +  tty->print_cr("  Average compilation speed: %6d bytes/s", bps);
  1.2323 +  tty->cr();
  1.2324 +  tty->print_cr("  nmethod code size        : %6d bytes", CompileBroker::_sum_nmethod_code_size);
  1.2325 +  tty->print_cr("  nmethod total size       : %6d bytes", CompileBroker::_sum_nmethod_size);
  1.2326 +}
  1.2327 +
  1.2328 +// Debugging output for failure
  1.2329 +void CompileBroker::print_last_compile() {
  1.2330 +  if ( _last_compile_level != CompLevel_none &&
  1.2331 +       compiler(_last_compile_level) != NULL &&
  1.2332 +       _last_method_compiled != NULL &&
  1.2333 +       _last_compile_type != no_compile) {
  1.2334 +    if (_last_compile_type == osr_compile) {
  1.2335 +      tty->print_cr("Last parse:  [osr]%d+++(%d) %s",
  1.2336 +                    _osr_compilation_id, _last_compile_level, _last_method_compiled);
  1.2337 +    } else {
  1.2338 +      tty->print_cr("Last parse:  %d+++(%d) %s",
  1.2339 +                    _compilation_id, _last_compile_level, _last_method_compiled);
  1.2340 +    }
  1.2341 +  }
  1.2342 +}
  1.2343 +
  1.2344 +
  1.2345 +void CompileBroker::print_compiler_threads_on(outputStream* st) {
  1.2346 +#ifndef PRODUCT
  1.2347 +  st->print_cr("Compiler thread printing unimplemented.");
  1.2348 +  st->cr();
  1.2349 +#endif
  1.2350 +}

mercurial