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