src/share/vm/compiler/compileBroker.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6782
f73af4455d7d
parent 26
ed5b982c0b0e
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial