src/share/vm/compiler/compileBroker.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 26
ed5b982c0b0e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial