comparison src/share/vm/compiler/compileBroker.cpp @ 20804:7848fc12602b

Merge with jdk8u40-b25
author Gilles Duboscq <gilles.m.duboscq@oracle.com>
date Tue, 07 Apr 2015 14:58:49 +0200
parents 6a0692faf9fd ecccc23346fe
children 07b088d61d5d
comparison
equal deleted inserted replaced
20184:84105dcdb05b 20804:7848fc12602b
188 int CompileBroker::_sum_nmethod_size = 0; 188 int CompileBroker::_sum_nmethod_size = 0;
189 int CompileBroker::_sum_nmethod_code_size = 0; 189 int CompileBroker::_sum_nmethod_code_size = 0;
190 190
191 long CompileBroker::_peak_compilation_time = 0; 191 long CompileBroker::_peak_compilation_time = 0;
192 192
193 CompileQueue* CompileBroker::_c2_method_queue = NULL; 193 CompileQueue* CompileBroker::_c2_compile_queue = NULL;
194 CompileQueue* CompileBroker::_c1_method_queue = NULL; 194 CompileQueue* CompileBroker::_c1_compile_queue = NULL;
195 CompileTask* CompileBroker::_task_free_list = NULL;
196 195
197 GrowableArray<CompilerThread*>* CompileBroker::_compiler_threads = NULL; 196 GrowableArray<CompilerThread*>* CompileBroker::_compiler_threads = NULL;
198 197
199 198
200 class CompilationLog : public StringEventLog { 199 class CompilationLog : public StringEventLog {
258 } else { 257 } else {
259 task->mark_complete(); 258 task->mark_complete();
260 259
261 // By convention, the compiling thread is responsible for 260 // By convention, the compiling thread is responsible for
262 // recycling a non-blocking CompileTask. 261 // recycling a non-blocking CompileTask.
263 CompileBroker::free_task(task); 262 CompileTask::free(task);
264 } 263 }
265 } 264 }
266 265
267 266
268 // ------------------------------------------------------------------ 267 CompileTask* CompileTask::_task_free_list = NULL;
269 // CompileTask::initialize 268 #ifdef ASSERT
269 int CompileTask::_num_allocated_tasks = 0;
270 #endif
271 /**
272 * Allocate a CompileTask, from the free list if possible.
273 */
274 CompileTask* CompileTask::allocate() {
275 MutexLocker locker(CompileTaskAlloc_lock);
276 CompileTask* task = NULL;
277
278 if (_task_free_list != NULL) {
279 task = _task_free_list;
280 _task_free_list = task->next();
281 task->set_next(NULL);
282 } else {
283 task = new CompileTask();
284 DEBUG_ONLY(_num_allocated_tasks++;)
285 assert (_num_allocated_tasks < 10000, "Leaking compilation tasks?");
286 task->set_next(NULL);
287 task->set_is_free(true);
288 }
289 assert(task->is_free(), "Task must be free.");
290 task->set_is_free(false);
291 return task;
292 }
293
294
295 /**
296 * Add a task to the free list.
297 */
298 void CompileTask::free(CompileTask* task) {
299 MutexLocker locker(CompileTaskAlloc_lock);
300 if (!task->is_free()) {
301 task->set_code(NULL);
302 assert(!task->lock()->is_locked(), "Should not be locked when freed");
303 JNIHandles::destroy_global(task->_method_holder);
304 JNIHandles::destroy_global(task->_hot_method_holder);
305
306 task->set_is_free(true);
307 task->set_next(_task_free_list);
308 _task_free_list = task;
309 }
310 }
311
270 void CompileTask::initialize(int compile_id, 312 void CompileTask::initialize(int compile_id,
271 methodHandle method, 313 methodHandle method,
272 int osr_bci, 314 int osr_bci,
273 int comp_level, 315 int comp_level,
274 methodHandle hot_method, 316 methodHandle hot_method,
292 _hot_method = NULL; 334 _hot_method = NULL;
293 _hot_method_holder = NULL; 335 _hot_method_holder = NULL;
294 _hot_count = hot_count; 336 _hot_count = hot_count;
295 _time_queued = 0; // tidy 337 _time_queued = 0; // tidy
296 _comment = comment; 338 _comment = comment;
339 _failure_reason = NULL;
297 340
298 if (LogCompilation) { 341 if (LogCompilation) {
299 _time_queued = os::elapsed_counter(); 342 _time_queued = os::elapsed_counter();
300 if (hot_method.not_null()) { 343 if (hot_method.not_null()) {
301 if (hot_method == method) { 344 if (hot_method == method) {
320 void CompileTask::set_code(nmethod* nm) { 363 void CompileTask::set_code(nmethod* nm) {
321 if (_code_handle == NULL && nm == NULL) return; 364 if (_code_handle == NULL && nm == NULL) return;
322 guarantee(_code_handle != NULL, ""); 365 guarantee(_code_handle != NULL, "");
323 _code_handle->set_code(nm); 366 _code_handle->set_code(nm);
324 if (nm == NULL) _code_handle = NULL; // drop the handle also 367 if (nm == NULL) _code_handle = NULL; // drop the handle also
325 }
326
327 // ------------------------------------------------------------------
328 // CompileTask::free
329 void CompileTask::free() {
330 set_code(NULL);
331 assert(!_lock->is_locked(), "Should not be locked when freed");
332 JNIHandles::destroy_global(_method_holder);
333 JNIHandles::destroy_global(_hot_method_holder);
334 } 368 }
335 369
336 370
337 void CompileTask::mark_on_stack() { 371 void CompileTask::mark_on_stack() {
338 // Mark these methods as something redefine classes cannot remove. 372 // Mark these methods as something redefine classes cannot remove.
589 void CompileTask::log_task_done(CompileLog* log) { 623 void CompileTask::log_task_done(CompileLog* log) {
590 Thread* thread = Thread::current(); 624 Thread* thread = Thread::current();
591 methodHandle method(thread, this->method()); 625 methodHandle method(thread, this->method());
592 ResourceMark rm(thread); 626 ResourceMark rm(thread);
593 627
628 if (!_is_success) {
629 const char* reason = _failure_reason != NULL ? _failure_reason : "unknown";
630 log->elem("failure reason='%s'", reason);
631 }
632
594 // <task_done ... stamp='1.234'> </task> 633 // <task_done ... stamp='1.234'> </task>
595 nmethod* nm = code(); 634 nmethod* nm = code();
596 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'", 635 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'",
597 _is_success, nm == NULL ? 0 : nm->content_size(), 636 _is_success, nm == NULL ? 0 : nm->content_size(),
598 method->invocation_count()); 637 method->invocation_count());
612 log->mark_file_end(); 651 log->mark_file_end();
613 } 652 }
614 653
615 654
616 655
617 // Add a CompileTask to a CompileQueue 656 /**
657 * Add a CompileTask to a CompileQueue
658 */
618 void CompileQueue::add(CompileTask* task) { 659 void CompileQueue::add(CompileTask* task) {
619 assert(lock()->owned_by_self(), "must own lock"); 660 assert(lock()->owned_by_self(), "must own lock");
661 assert(!CompileBroker::is_compilation_disabled_forever(), "Do not add task if compilation is turned off forever");
620 662
621 task->set_next(NULL); 663 task->set_next(NULL);
622 task->set_prev(NULL); 664 task->set_prev(NULL);
623 665
624 if (_last == NULL) { 666 if (_last == NULL) {
636 ++_size; 678 ++_size;
637 679
638 // Mark the method as being in the compile queue. 680 // Mark the method as being in the compile queue.
639 task->method()->set_queued_for_compilation(); 681 task->method()->set_queued_for_compilation();
640 682
641 if (CIPrintCompileQueue) { 683 NOT_PRODUCT(print();)
642 print();
643 }
644 684
645 if (LogCompilation && xtty != NULL) { 685 if (LogCompilation && xtty != NULL) {
646 task->log_task_queued(); 686 task->log_task_queued();
647 } 687 }
648 688
649 // Notify CompilerThreads that a task is available. 689 // Notify CompilerThreads that a task is available.
650 lock()->notify_all(); 690 lock()->notify_all();
651 } 691 }
652 692
653 void CompileQueue::delete_all() { 693 /**
654 assert(lock()->owned_by_self(), "must own lock"); 694 * Empties compilation queue by putting all compilation tasks onto
655 if (_first != NULL) { 695 * a freelist. Furthermore, the method wakes up all threads that are
656 for (CompileTask* task = _first; task != NULL; task = task->next()) { 696 * waiting on a compilation task to finish. This can happen if background
657 delete task; 697 * compilation is disabled.
658 } 698 */
659 _first = NULL; 699 void CompileQueue::free_all() {
660 } 700 MutexLocker mu(lock());
701 CompileTask* next = _first;
702
703 // Iterate over all tasks in the compile queue
704 while (next != NULL) {
705 CompileTask* current = next;
706 next = current->next();
707 {
708 // Wake up thread that blocks on the compile task.
709 MutexLocker ct_lock(current->lock());
710 current->lock()->notify();
711 }
712 // Put the task back on the freelist.
713 CompileTask::free(current);
714 }
715 _first = NULL;
716
717 // Wake up all threads that block on the queue.
718 lock()->notify_all();
661 } 719 }
662 720
663 // ------------------------------------------------------------------ 721 // ------------------------------------------------------------------
664 // CompileQueue::get 722 // CompileQueue::get
665 // 723 //
712 770
713 if (CompileBroker::is_compilation_disabled_forever()) { 771 if (CompileBroker::is_compilation_disabled_forever()) {
714 return NULL; 772 return NULL;
715 } 773 }
716 774
717 CompileTask* task = CompilationPolicy::policy()->select_task(this); 775 CompileTask* task;
776 {
777 No_Safepoint_Verifier nsv;
778 task = CompilationPolicy::policy()->select_task(this);
779 }
718 remove(task); 780 remove(task);
781 purge_stale_tasks(); // may temporarily release MCQ lock
719 return task; 782 return task;
720 } 783 }
721 784
722 void CompileQueue::remove(CompileTask* task) 785 // Clean & deallocate stale compile tasks.
723 { 786 // Temporarily releases MethodCompileQueue lock.
787 void CompileQueue::purge_stale_tasks() {
788 assert(lock()->owned_by_self(), "must own lock");
789 if (_first_stale != NULL) {
790 // Stale tasks are purged when MCQ lock is released,
791 // but _first_stale updates are protected by MCQ lock.
792 // Once task processing starts and MCQ lock is released,
793 // other compiler threads can reuse _first_stale.
794 CompileTask* head = _first_stale;
795 _first_stale = NULL;
796 {
797 MutexUnlocker ul(lock());
798 for (CompileTask* task = head; task != NULL; ) {
799 CompileTask* next_task = task->next();
800 CompileTaskWrapper ctw(task); // Frees the task
801 task->set_failure_reason("stale task");
802 task = next_task;
803 }
804 }
805 }
806 }
807
808 void CompileQueue::remove(CompileTask* task) {
724 assert(lock()->owned_by_self(), "must own lock"); 809 assert(lock()->owned_by_self(), "must own lock");
725 if (task->prev() != NULL) { 810 if (task->prev() != NULL) {
726 task->prev()->set_next(task->next()); 811 task->prev()->set_next(task->next());
727 } else { 812 } else {
728 // max is the first element 813 // max is the first element
738 _last = task->prev(); 823 _last = task->prev();
739 } 824 }
740 --_size; 825 --_size;
741 } 826 }
742 827
828 void CompileQueue::remove_and_mark_stale(CompileTask* task) {
829 assert(lock()->owned_by_self(), "must own lock");
830 remove(task);
831
832 // Enqueue the task for reclamation (should be done outside MCQ lock)
833 task->set_next(_first_stale);
834 task->set_prev(NULL);
835 _first_stale = task;
836 }
837
743 // methods in the compile queue need to be marked as used on the stack 838 // methods in the compile queue need to be marked as used on the stack
744 // so that they don't get reclaimed by Redefine Classes 839 // so that they don't get reclaimed by Redefine Classes
745 void CompileQueue::mark_on_stack() { 840 void CompileQueue::mark_on_stack() {
746 CompileTask* task = _first; 841 CompileTask* task = _first;
747 while (task != NULL) { 842 while (task != NULL) {
748 task->mark_on_stack(); 843 task->mark_on_stack();
749 task = task->next(); 844 task = task->next();
750 } 845 }
751 } 846 }
752 847
753 // ------------------------------------------------------------------ 848 #ifndef PRODUCT
754 // CompileQueue::print 849 /**
850 * Print entire compilation queue.
851 */
755 void CompileQueue::print() { 852 void CompileQueue::print() {
756 tty->print_cr("Contents of %s", name()); 853 if (CIPrintCompileQueue) {
757 tty->print_cr("----------------------"); 854 ttyLocker ttyl;
758 CompileTask* task = _first; 855 tty->print_cr("Contents of %s", name());
759 while (task != NULL) { 856 tty->print_cr("----------------------");
760 task->print_line(); 857 CompileTask* task = _first;
761 task = task->next(); 858 while (task != NULL) {
762 } 859 task->print_line();
763 tty->print_cr("----------------------"); 860 task = task->next();
764 } 861 }
862 tty->print_cr("----------------------");
863 }
864 }
865 #endif // PRODUCT
765 866
766 CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) { 867 CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) {
767 868
768 _current_method[0] = '\0'; 869 _current_method[0] = '\0';
769 _compile_type = CompileBroker::no_compile; 870 _compile_type = CompileBroker::no_compile;
851 int c1_count = 0; 952 int c1_count = 0;
852 int c2_count = 1; 953 int c2_count = 1;
853 954
854 _compilers[1] = new SharkCompiler(); 955 _compilers[1] = new SharkCompiler();
855 #endif // SHARK 956 #endif // SHARK
856
857 // Initialize the CompileTask free list
858 _task_free_list = NULL;
859 957
860 // Start the CompilerThreads 958 // Start the CompilerThreads
861 init_compiler_threads(c1_count, c2_count); 959 init_compiler_threads(c1_count, c2_count);
862 // totalTime performance counter is always created as it is required 960 // totalTime performance counter is always created as it is required
863 // by the implementation of java.lang.management.CompilationMBean. 961 // by the implementation of java.lang.management.CompilationMBean.
1047 #if !defined(ZERO) && !defined(SHARK) && !defined(COMPILERGRAAL) 1145 #if !defined(ZERO) && !defined(SHARK) && !defined(COMPILERGRAAL)
1048 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?"); 1146 assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
1049 #endif // !ZERO && !SHARK && !COMPILERGRAAL 1147 #endif // !ZERO && !SHARK && !COMPILERGRAAL
1050 // Initialize the compilation queue 1148 // Initialize the compilation queue
1051 if (c2_compiler_count > 0) { 1149 if (c2_compiler_count > 0) {
1052 _c2_method_queue = new CompileQueue("C2MethodQueue", MethodCompileQueue_lock); 1150 _c2_compile_queue = new CompileQueue("C2 CompileQueue", MethodCompileQueue_lock);
1053 _compilers[1]->set_num_compiler_threads(c2_compiler_count); 1151 _compilers[1]->set_num_compiler_threads(c2_compiler_count);
1054 } 1152 }
1055 if (c1_compiler_count > 0) { 1153 if (c1_compiler_count > 0) {
1056 _c1_method_queue = new CompileQueue("C1MethodQueue", MethodCompileQueue_lock); 1154 _c1_compile_queue = new CompileQueue("C1 CompileQueue", MethodCompileQueue_lock);
1057 _compilers[0]->set_num_compiler_threads(c1_compiler_count); 1155 _compilers[0]->set_num_compiler_threads(c1_compiler_count);
1058 } 1156 }
1059 1157
1060 int compiler_count = c1_compiler_count + c2_compiler_count; 1158 int compiler_count = c1_compiler_count + c2_compiler_count;
1061 1159
1066 for (int i = 0; i < c2_compiler_count; i++) { 1164 for (int i = 0; i < c2_compiler_count; i++) {
1067 // Create a name for our thread. 1165 // Create a name for our thread.
1068 sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i); 1166 sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
1069 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); 1167 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1070 // Shark and C2 1168 // Shark and C2
1071 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_method_queue, counters, _compilers[1], CHECK); 1169 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], CHECK);
1072 _compiler_threads->append(new_thread); 1170 _compiler_threads->append(new_thread);
1073 } 1171 }
1074 1172
1075 for (int i = c2_compiler_count; i < compiler_count; i++) { 1173 for (int i = c2_compiler_count; i < compiler_count; i++) {
1076 // Create a name for our thread. 1174 // Create a name for our thread.
1077 sprintf(name_buffer, "C1 CompilerThread%d", i); 1175 sprintf(name_buffer, "C1 CompilerThread%d", i);
1078 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK); 1176 CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1079 // C1 1177 // C1
1080 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_method_queue, counters, _compilers[0], CHECK); 1178 CompilerThread* new_thread = make_compiler_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], CHECK);
1081 _compiler_threads->append(new_thread); 1179 _compiler_threads->append(new_thread);
1082 } 1180 }
1083 1181
1084 if (UsePerfData) { 1182 if (UsePerfData) {
1085 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK); 1183 PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
1086 } 1184 }
1087 } 1185 }
1088 1186
1089 1187
1090 // Set the methods on the stack as on_stack so that redefine classes doesn't 1188 /**
1091 // reclaim them 1189 * Set the methods on the stack as on_stack so that redefine classes doesn't
1190 * reclaim them. This method is executed at a safepoint.
1191 */
1092 void CompileBroker::mark_on_stack() { 1192 void CompileBroker::mark_on_stack() {
1093 if (_c2_method_queue != NULL) { 1193 assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
1094 _c2_method_queue->mark_on_stack(); 1194 // Since we are at a safepoint, we do not need a lock to access
1095 } 1195 // the compile queues.
1096 if (_c1_method_queue != NULL) { 1196 if (_c2_compile_queue != NULL) {
1097 _c1_method_queue->mark_on_stack(); 1197 _c2_compile_queue->mark_on_stack();
1198 }
1199 if (_c1_compile_queue != NULL) {
1200 _c1_compile_queue->mark_on_stack();
1098 } 1201 }
1099 } 1202 }
1100 1203
1101 // ------------------------------------------------------------------ 1204 // ------------------------------------------------------------------
1102 // CompileBroker::compile_method 1205 // CompileBroker::compile_method
1108 methodHandle hot_method, 1211 methodHandle hot_method,
1109 int hot_count, 1212 int hot_count,
1110 const char* comment, 1213 const char* comment,
1111 Thread* thread) { 1214 Thread* thread) {
1112 // do nothing if compiler thread(s) is not available 1215 // do nothing if compiler thread(s) is not available
1113 if (!_initialized ) { 1216 if (!_initialized) {
1114 return; 1217 return;
1115 } 1218 }
1116 1219
1117 guarantee(!method->is_abstract(), "cannot compile abstract methods"); 1220 guarantee(!method->is_abstract(), "cannot compile abstract methods");
1118 assert(method->method_holder()->oop_is_instance(), 1221 assert(method->method_holder()->oop_is_instance(),
1155 } 1258 }
1156 #endif 1259 #endif
1157 1260
1158 // If this method is already in the compile queue, then 1261 // If this method is already in the compile queue, then
1159 // we do not block the current thread. 1262 // we do not block the current thread.
1160 if (compilation_is_in_queue(method, osr_bci)) { 1263 if (compilation_is_in_queue(method)) {
1161 // We may want to decay our counter a bit here to prevent 1264 // We may want to decay our counter a bit here to prevent
1162 // multiple denied requests for compilation. This is an 1265 // multiple denied requests for compilation. This is an
1163 // open compilation policy issue. Note: The other possibility, 1266 // open compilation policy issue. Note: The other possibility,
1164 // in the case that this is a blocking compile request, is to have 1267 // in the case that this is a blocking compile request, is to have
1165 // all subsequent blocking requesters wait for completion of 1268 // all subsequent blocking requesters wait for completion of
1176 // by a compiler thread), and compiled method registration. 1279 // by a compiler thread), and compiled method registration.
1177 if (InstanceRefKlass::owns_pending_list_lock(JavaThread::current())) { 1280 if (InstanceRefKlass::owns_pending_list_lock(JavaThread::current())) {
1178 return; 1281 return;
1179 } 1282 }
1180 1283
1284 if (TieredCompilation) {
1285 // Tiered policy requires MethodCounters to exist before adding a method to
1286 // the queue. Create if we don't have them yet.
1287 method->get_method_counters(thread);
1288 }
1289
1181 // Outputs from the following MutexLocker block: 1290 // Outputs from the following MutexLocker block:
1182 CompileTask* task = NULL; 1291 CompileTask* task = NULL;
1183 bool blocking = false; 1292 bool blocking = false;
1184 CompileQueue* queue = compile_queue(comp_level); 1293 CompileQueue* queue = compile_queue(comp_level);
1185 1294
1188 MutexLocker locker(queue->lock(), thread); 1297 MutexLocker locker(queue->lock(), thread);
1189 1298
1190 // Make sure the method has not slipped into the queues since 1299 // Make sure the method has not slipped into the queues since
1191 // last we checked; note that those checks were "fast bail-outs". 1300 // last we checked; note that those checks were "fast bail-outs".
1192 // Here we need to be more careful, see 14012000 below. 1301 // Here we need to be more careful, see 14012000 below.
1193 if (compilation_is_in_queue(method, osr_bci)) { 1302 if (compilation_is_in_queue(method)) {
1194 return; 1303 return;
1195 } 1304 }
1196 1305
1197 // We need to check again to see if the compilation has 1306 // We need to check again to see if the compilation has
1198 // completed. A previous compilation may have registered 1307 // completed. A previous compilation may have registered
1209 // The compilation falls outside the allowed range. 1318 // The compilation falls outside the allowed range.
1210 return; 1319 return;
1211 } 1320 }
1212 1321
1213 // Should this thread wait for completion of the compile? 1322 // Should this thread wait for completion of the compile?
1214 blocking = is_compile_blocking(method, osr_bci); 1323 blocking = is_compile_blocking();
1215 1324
1216 #ifdef COMPILERGRAAL 1325 #ifdef COMPILERGRAAL
1217 if (blocking) { 1326 if (blocking) {
1218 // Don't allow blocking compiles for requests triggered by Graal. 1327 // Don't allow blocking compiles for requests triggered by Graal.
1219 if (thread->is_Compiler_thread()) { 1328 if (thread->is_Compiler_thread()) {
1435 } 1544 }
1436 } 1545 }
1437 } 1546 }
1438 1547
1439 1548
1440 // ------------------------------------------------------------------ 1549 /**
1441 // CompileBroker::compilation_is_in_queue 1550 * See if this compilation is already requested.
1442 // 1551 *
1443 // See if this compilation is already requested. 1552 * Implementation note: there is only a single "is in queue" bit
1444 // 1553 * for each method. This means that the check below is overly
1445 // Implementation note: there is only a single "is in queue" bit 1554 * conservative in the sense that an osr compilation in the queue
1446 // for each method. This means that the check below is overly 1555 * will block a normal compilation from entering the queue (and vice
1447 // conservative in the sense that an osr compilation in the queue 1556 * versa). This can be remedied by a full queue search to disambiguate
1448 // will block a normal compilation from entering the queue (and vice 1557 * cases. If it is deemed profitable, this may be done.
1449 // versa). This can be remedied by a full queue search to disambiguate 1558 */
1450 // cases. If it is deemed profitible, this may be done. 1559 bool CompileBroker::compilation_is_in_queue(methodHandle method) {
1451 bool CompileBroker::compilation_is_in_queue(methodHandle method,
1452 int osr_bci) {
1453 return method->queued_for_compilation(); 1560 return method->queued_for_compilation();
1454 } 1561 }
1455 1562
1456 // ------------------------------------------------------------------ 1563 // ------------------------------------------------------------------
1457 // CompileBroker::compilation_is_prohibited 1564 // CompileBroker::compilation_is_prohibited
1537 uint CompileBroker::assign_compile_id_unlocked(Thread* thread, methodHandle method, int osr_bci) { 1644 uint CompileBroker::assign_compile_id_unlocked(Thread* thread, methodHandle method, int osr_bci) {
1538 MutexLocker locker(MethodCompileQueue_lock, thread); 1645 MutexLocker locker(MethodCompileQueue_lock, thread);
1539 return assign_compile_id(method, osr_bci); 1646 return assign_compile_id(method, osr_bci);
1540 } 1647 }
1541 1648
1542 1649 /**
1543 // ------------------------------------------------------------------ 1650 * Should the current thread block until this compilation request
1544 // CompileBroker::is_compile_blocking 1651 * has been fulfilled?
1545 // 1652 */
1546 // Should the current thread be blocked until this compilation request 1653 bool CompileBroker::is_compile_blocking() {
1547 // has been fulfilled?
1548 bool CompileBroker::is_compile_blocking(methodHandle method, int osr_bci) {
1549 assert(!InstanceRefKlass::owns_pending_list_lock(JavaThread::current()), "possible deadlock"); 1654 assert(!InstanceRefKlass::owns_pending_list_lock(JavaThread::current()), "possible deadlock");
1550 return !BackgroundCompilation; 1655 return !BackgroundCompilation;
1551 } 1656 }
1552 1657
1553 1658
1571 int comp_level, 1676 int comp_level,
1572 methodHandle hot_method, 1677 methodHandle hot_method,
1573 int hot_count, 1678 int hot_count,
1574 const char* comment, 1679 const char* comment,
1575 bool blocking) { 1680 bool blocking) {
1576 CompileTask* new_task = allocate_task(); 1681 CompileTask* new_task = CompileTask::allocate();
1577 new_task->initialize(compile_id, method, osr_bci, comp_level, 1682 new_task->initialize(compile_id, method, osr_bci, comp_level,
1578 hot_method, hot_count, comment, 1683 hot_method, hot_count, comment,
1579 blocking); 1684 blocking);
1580 queue->add(new_task); 1685 queue->add(new_task);
1581 return new_task; 1686 return new_task;
1582 } 1687 }
1583 1688
1584 1689
1585 // ------------------------------------------------------------------ 1690 /**
1586 // CompileBroker::allocate_task 1691 * Wait for the compilation task to complete.
1587 // 1692 */
1588 // Allocate a CompileTask, from the free list if possible.
1589 CompileTask* CompileBroker::allocate_task() {
1590 MutexLocker locker(CompileTaskAlloc_lock);
1591 CompileTask* task = NULL;
1592 if (_task_free_list != NULL) {
1593 task = _task_free_list;
1594 _task_free_list = task->next();
1595 task->set_next(NULL);
1596 } else {
1597 task = new CompileTask();
1598 task->set_next(NULL);
1599 }
1600 return task;
1601 }
1602
1603
1604 // ------------------------------------------------------------------
1605 // CompileBroker::free_task
1606 //
1607 // Add a task to the free list.
1608 void CompileBroker::free_task(CompileTask* task) {
1609 MutexLocker locker(CompileTaskAlloc_lock);
1610 task->free();
1611 task->set_next(_task_free_list);
1612 _task_free_list = task;
1613 }
1614
1615
1616 // ------------------------------------------------------------------
1617 // CompileBroker::wait_for_completion
1618 //
1619 // Wait for the given method CompileTask to complete.
1620 void CompileBroker::wait_for_completion(CompileTask* task) { 1693 void CompileBroker::wait_for_completion(CompileTask* task) {
1621 if (CIPrintCompileQueue) { 1694 if (CIPrintCompileQueue) {
1695 ttyLocker ttyl;
1622 tty->print_cr("BLOCKING FOR COMPILE"); 1696 tty->print_cr("BLOCKING FOR COMPILE");
1623 } 1697 }
1624 1698
1625 assert(task->is_blocking(), "can only wait on blocking task"); 1699 assert(task->is_blocking(), "can only wait on blocking task");
1626 1700
1627 JavaThread *thread = JavaThread::current(); 1701 JavaThread* thread = JavaThread::current();
1628 thread->set_blocked_on_compilation(true); 1702 thread->set_blocked_on_compilation(true);
1629 1703
1630 methodHandle method(thread, task->method()); 1704 methodHandle method(thread, task->method());
1631 { 1705 {
1632 MutexLocker waiter(task->lock(), thread); 1706 MutexLocker waiter(task->lock(), thread);
1633 1707
1634 while (!task->is_complete()) 1708 while (!task->is_complete() && !is_compilation_disabled_forever()) {
1635 task->lock()->wait(); 1709 task->lock()->wait();
1636 } 1710 }
1711 }
1712
1713 thread->set_blocked_on_compilation(false);
1714 if (is_compilation_disabled_forever()) {
1715 CompileTask::free(task);
1716 return;
1717 }
1718
1637 // It is harmless to check this status without the lock, because 1719 // It is harmless to check this status without the lock, because
1638 // completion is a stable property (until the task object is recycled). 1720 // completion is a stable property (until the task object is recycled).
1639 assert(task->is_complete(), "Compilation should have completed"); 1721 assert(task->is_complete(), "Compilation should have completed");
1640 assert(task->code_handle() == NULL, "must be reset"); 1722 assert(task->code_handle() == NULL, "must be reset");
1641 1723
1642 thread->set_blocked_on_compilation(false);
1643
1644 // By convention, the waiter is responsible for recycling a 1724 // By convention, the waiter is responsible for recycling a
1645 // blocking CompileTask. Since there is only one waiter ever 1725 // blocking CompileTask. Since there is only one waiter ever
1646 // waiting on a CompileTask, we know that no one else will 1726 // waiting on a CompileTask, we know that no one else will
1647 // be using this CompileTask; we can free it. 1727 // be using this CompileTask; we can free it.
1648 free_task(task); 1728 CompileTask::free(task);
1649 } 1729 }
1650 1730
1651 // Initialize compiler thread(s) + compiler object(s). The postcondition 1731 /**
1652 // of this function is that the compiler runtimes are initialized and that 1732 * Initialize compiler thread(s) + compiler object(s). The postcondition
1653 //compiler threads can start compiling. 1733 * of this function is that the compiler runtimes are initialized and that
1734 * compiler threads can start compiling.
1735 */
1654 bool CompileBroker::init_compiler_runtime() { 1736 bool CompileBroker::init_compiler_runtime() {
1655 CompilerThread* thread = CompilerThread::current(); 1737 CompilerThread* thread = CompilerThread::current();
1656 AbstractCompiler* comp = thread->compiler(); 1738 AbstractCompiler* comp = thread->compiler();
1657 // Final sanity check - the compiler object must exist 1739 // Final sanity check - the compiler object must exist
1658 guarantee(comp != NULL, "Compiler object must exist"); 1740 guarantee(comp != NULL, "Compiler object must exist");
1685 1767
1686 if (comp->is_failed()) { 1768 if (comp->is_failed()) {
1687 disable_compilation_forever(); 1769 disable_compilation_forever();
1688 // If compiler initialization failed, no compiler thread that is specific to a 1770 // If compiler initialization failed, no compiler thread that is specific to a
1689 // particular compiler runtime will ever start to compile methods. 1771 // particular compiler runtime will ever start to compile methods.
1690
1691 shutdown_compiler_runtime(comp, thread); 1772 shutdown_compiler_runtime(comp, thread);
1692 return false; 1773 return false;
1693 } 1774 }
1694 1775
1695 // C1 specific check 1776 // C1 specific check
1699 } 1780 }
1700 1781
1701 return true; 1782 return true;
1702 } 1783 }
1703 1784
1704 // If C1 and/or C2 initialization failed, we shut down all compilation. 1785 /**
1705 // We do this to keep things simple. This can be changed if it ever turns out to be 1786 * If C1 and/or C2 initialization failed, we shut down all compilation.
1706 // a problem. 1787 * We do this to keep things simple. This can be changed if it ever turns
1788 * out to be a problem.
1789 */
1707 void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) { 1790 void CompileBroker::shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread) {
1708 // Free buffer blob, if allocated 1791 // Free buffer blob, if allocated
1709 if (thread->get_buffer_blob() != NULL) { 1792 if (thread->get_buffer_blob() != NULL) {
1710 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1793 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1711 CodeCache::free(thread->get_buffer_blob()); 1794 CodeCache::free(thread->get_buffer_blob());
1713 1796
1714 if (comp->should_perform_shutdown()) { 1797 if (comp->should_perform_shutdown()) {
1715 // There are two reasons for shutting down the compiler 1798 // There are two reasons for shutting down the compiler
1716 // 1) compiler runtime initialization failed 1799 // 1) compiler runtime initialization failed
1717 // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing 1800 // 2) The code cache is full and the following flag is set: -XX:-UseCodeCacheFlushing
1718 warning("Shutting down compiler %s (no space to run compilers)", comp->name()); 1801 warning("%s initialization failed. Shutting down all compilers", comp->name());
1719 1802
1720 // Only one thread per compiler runtime object enters here 1803 // Only one thread per compiler runtime object enters here
1721 // Set state to shut down 1804 // Set state to shut down
1722 comp->set_shut_down(); 1805 comp->set_shut_down();
1723 1806
1724 MutexLocker mu(MethodCompileQueue_lock, thread); 1807 // Delete all queued compilation tasks to make compiler threads exit faster.
1725 CompileQueue* queue; 1808 if (_c1_compile_queue != NULL) {
1726 if (_c1_method_queue != NULL) { 1809 _c1_compile_queue->free_all();
1727 _c1_method_queue->delete_all(); 1810 }
1728 queue = _c1_method_queue; 1811
1729 _c1_method_queue = NULL; 1812 if (_c2_compile_queue != NULL) {
1730 delete _c1_method_queue; 1813 _c2_compile_queue->free_all();
1731 } 1814 }
1732 1815
1733 if (_c2_method_queue != NULL) { 1816 // Set flags so that we continue execution with using interpreter only.
1734 _c2_method_queue->delete_all(); 1817 UseCompiler = false;
1735 queue = _c2_method_queue; 1818 UseInterpreter = true;
1736 _c2_method_queue = NULL;
1737 delete _c2_method_queue;
1738 }
1739 1819
1740 // We could delete compiler runtimes also. However, there are references to 1820 // We could delete compiler runtimes also. However, there are references to
1741 // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then 1821 // the compiler runtime(s) (e.g., nmethod::is_compiled_by_c1()) which then
1742 // fail. This can be done later if necessary. 1822 // fail. This can be done later if necessary.
1743 } 1823 }
1820 1900
1821 // Never compile a method if breakpoints are present in it 1901 // Never compile a method if breakpoints are present in it
1822 if (method()->number_of_breakpoints() == 0) { 1902 if (method()->number_of_breakpoints() == 0) {
1823 // Compile the method. 1903 // Compile the method.
1824 if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) { 1904 if ((UseCompiler || AlwaysCompileLoopMethods) && CompileBroker::should_compile_new_jobs()) {
1825 #ifdef COMPILER1
1826 // Allow repeating compilations for the purpose of benchmarking
1827 // compile speed. This is not useful for customers.
1828 if (CompilationRepeat != 0) {
1829 int compile_count = CompilationRepeat;
1830 while (compile_count > 0) {
1831 invoke_compiler_on_method(task);
1832 nmethod* nm = method->code();
1833 if (nm != NULL) {
1834 nm->make_zombie();
1835 method->clear_code();
1836 }
1837 compile_count--;
1838 }
1839 }
1840 #endif /* COMPILER1 */
1841 invoke_compiler_on_method(task); 1905 invoke_compiler_on_method(task);
1842 } else { 1906 } else {
1843 // After compilation is disabled, remove remaining methods from queue 1907 // After compilation is disabled, remove remaining methods from queue
1844 method->clear_queued_for_compilation(); 1908 method->clear_queued_for_compilation();
1909 task->set_failure_reason("compilation is disabled");
1845 } 1910 }
1846 } 1911 }
1847 } 1912 }
1848 1913
1849 // Shut down compiler runtime 1914 // Shut down compiler runtime
1868 jio_snprintf(file_name, sizeof(file_name), 1933 jio_snprintf(file_name, sizeof(file_name),
1869 "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir, 1934 "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir,
1870 os::file_separator(), thread_id, os::current_process_id()); 1935 os::file_separator(), thread_id, os::current_process_id());
1871 } 1936 }
1872 1937
1873 fp = fopen(file_name, "at"); 1938 fp = fopen(file_name, "wt");
1874 if (fp != NULL) { 1939 if (fp != NULL) {
1875 if (LogCompilation && Verbose) { 1940 if (LogCompilation && Verbose) {
1876 tty->print_cr("Opening compilation log %s", file_name); 1941 tty->print_cr("Opening compilation log %s", file_name);
1877 } 1942 }
1878 CompileLog* log = new(ResourceObj::C_HEAP, mtCompiler) CompileLog(file_name, fp, thread_id); 1943 CompileLog* log = new(ResourceObj::C_HEAP, mtCompiler) CompileLog(file_name, fp, thread_id);
2070 2135
2071 // Copy this bit to the enclosing block: 2136 // Copy this bit to the enclosing block:
2072 compilable = ci_env.compilable(); 2137 compilable = ci_env.compilable();
2073 2138
2074 if (ci_env.failing()) { 2139 if (ci_env.failing()) {
2140 task->set_failure_reason(ci_env.failure_reason());
2075 const char* retry_message = ci_env.retry_message(); 2141 const char* retry_message = ci_env.retry_message();
2076 if (_compilation_log != NULL) { 2142 if (_compilation_log != NULL) {
2077 _compilation_log->log_failure(thread, task, ci_env.failure_reason(), retry_message); 2143 _compilation_log->log_failure(thread, task, ci_env.failure_reason(), retry_message);
2078 } 2144 }
2079 if (PrintCompilation) { 2145 if (PrintCompilation) {
2123 break; 2189 break;
2124 } 2190 }
2125 2191
2126 // Note that the queued_for_compilation bits are cleared without 2192 // Note that the queued_for_compilation bits are cleared without
2127 // protection of a mutex. [They were set by the requester thread, 2193 // protection of a mutex. [They were set by the requester thread,
2128 // when adding the task to the complie queue -- at which time the 2194 // when adding the task to the compile queue -- at which time the
2129 // compile queue lock was held. Subsequently, we acquired the compile 2195 // compile queue lock was held. Subsequently, we acquired the compile
2130 // queue lock to get this task off the compile queue; thus (to belabour 2196 // queue lock to get this task off the compile queue; thus (to belabour
2131 // the point somewhat) our clearing of the bits must be occurring 2197 // the point somewhat) our clearing of the bits must be occurring
2132 // only after the setting of the bits. See also 14012000 above. 2198 // only after the setting of the bits. See also 14012000 above.
2133 method->clear_queued_for_compilation(); 2199 method->clear_queued_for_compilation();