src/share/vm/compiler/compileBroker.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/compiler

src/share/vm/compiler/compileBroker.cpp

Print this page
rev 7349 : 8061256: com/sun/management/DiagnosticCommandMBean/DcmdMBeanPermissionsTest.java timed out
Summary: Must not be at safepoint when taking CompileQueue_lock
Reviewed-by:


 577   // Note:  "_is_complete" is about to be set, but is not.
 578   if (_num_inlined_bytecodes != 0) {
 579     log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);
 580   }
 581   log->stamp();
 582   log->end_elem();
 583   log->tail("task");
 584   log->clear_identities();   // next task will have different CI
 585   if (log->unflushed_count() > 2000) {
 586     log->flush();
 587   }
 588   log->mark_file_end();
 589 }
 590 
 591 
 592 
 593 /**
 594  * Add a CompileTask to a CompileQueue.
 595  */
 596 void CompileQueue::add(CompileTask* task) {
 597   assert(lock()->owned_by_self(), "must own lock");
 598 
 599   task->set_next(NULL);
 600   task->set_prev(NULL);
 601 
 602   if (_last == NULL) {
 603     // The compile queue is empty.
 604     assert(_first == NULL, "queue is empty");
 605     _first = task;
 606     _last = task;
 607   } else {
 608     // Append the task to the queue.
 609     assert(_last->next() == NULL, "not last");
 610     _last->set_next(task);
 611     task->set_prev(_last);
 612     _last = task;
 613   }
 614   ++_size;
 615 
 616   // Mark the method as being in the compile queue.
 617   task->method()->set_queued_for_compilation();
 618 
 619   if (CIPrintCompileQueue) {
 620     print_tty();
 621   }
 622 
 623   if (LogCompilation && xtty != NULL) {
 624     task->log_task_queued();
 625   }
 626 
 627   // Notify CompilerThreads that a task is available.
 628   lock()->notify_all();
 629 }
 630 
 631 /**
 632  * Empties compilation queue by putting all compilation tasks onto
 633  * a freelist. Furthermore, the method wakes up all threads that are
 634  * waiting on a compilation task to finish. This can happen if background
 635  * compilation is disabled.
 636  */
 637 void CompileQueue::free_all() {
 638   MutexLocker mu(lock());
 639   CompileTask* next = _first;
 640 
 641   // Iterate over all tasks in the compile queue
 642   while (next != NULL) {
 643     CompileTask* current = next;
 644     next = current->next();
 645     {
 646       // Wake up thread that blocks on the compile task.
 647       MutexLocker ct_lock(current->lock());
 648       current->lock()->notify();
 649     }
 650     // Put the task back on the freelist.
 651     CompileTask::free(current);
 652   }
 653   _first = NULL;
 654 
 655   // Wake up all threads that block on the queue.
 656   lock()->notify_all();
 657 }
 658 
 659 /**
 660  * Get the next CompileTask from a CompileQueue
 661  */
 662 CompileTask* CompileQueue::get() {
 663   MutexLocker locker(lock());
 664   // If _first is NULL we have no more compile jobs. There are two reasons for
 665   // having no compile jobs: First, we compiled everything we wanted. Second,
 666   // we ran out of code cache so compilation has been disabled. In the latter
 667   // case we perform code cache sweeps to free memory such that we can re-enable
 668   // compilation.
 669   while (_first == NULL) {
 670     // Exit loop if compilation is disabled forever
 671     if (CompileBroker::is_compilation_disabled_forever()) {
 672       return NULL;
 673     }
 674 
 675     // If there are no compilation tasks and we can compile new jobs
 676     // (i.e., there is enough free space in the code cache) there is
 677     // no need to invoke the sweeper. As a result, the hotness of methods
 678     // remains unchanged. This behavior is desired, since we want to keep
 679     // the stable state, i.e., we do not want to evict methods from the
 680     // code cache if it is unnecessary.
 681     // We need a timed wait here, since compiler threads can exit if compilation
 682     // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
 683     // is not critical and we do not want idle compiler threads to wake up too often.
 684     lock()->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
 685   }
 686 
 687   if (CompileBroker::is_compilation_disabled_forever()) {
 688     return NULL;
 689   }
 690 
 691   CompileTask* task;
 692   {
 693     No_Safepoint_Verifier nsv;
 694     task = CompilationPolicy::policy()->select_task(this);
 695   }
 696   remove(task);
 697   purge_stale_tasks(); // may temporarily release MCQ lock
 698   return task;
 699 }
 700 
 701 // Clean & deallocate stale compile tasks.
 702 // Temporarily releases MethodCompileQueue lock.
 703 void CompileQueue::purge_stale_tasks() {
 704   assert(lock()->owned_by_self(), "must own lock");
 705   if (_first_stale != NULL) {
 706     // Stale tasks are purged when MCQ lock is released,
 707     // but _first_stale updates are protected by MCQ lock.
 708     // Once task processing starts and MCQ lock is released,
 709     // other compiler threads can reuse _first_stale.
 710     CompileTask* head = _first_stale;
 711     _first_stale = NULL;
 712     {
 713       MutexUnlocker ul(lock());
 714       for (CompileTask* task = head; task != NULL; ) {
 715         CompileTask* next_task = task->next();
 716         CompileTaskWrapper ctw(task); // Frees the task
 717         task->set_failure_reason("stale task");
 718         task = next_task;
 719       }
 720     }
 721   }
 722 }
 723 
 724 void CompileQueue::remove(CompileTask* task) {
 725    assert(lock()->owned_by_self(), "must own lock");
 726   if (task->prev() != NULL) {
 727     task->prev()->set_next(task->next());
 728   } else {
 729     // max is the first element
 730     assert(task == _first, "Sanity");
 731     _first = task->next();
 732   }
 733 
 734   if (task->next() != NULL) {
 735     task->next()->set_prev(task->prev());
 736   } else {
 737     // max is the last element
 738     assert(task == _last, "Sanity");
 739     _last = task->prev();
 740   }
 741   --_size;
 742 }
 743 
 744 void CompileQueue::remove_and_mark_stale(CompileTask* task) {
 745   assert(lock()->owned_by_self(), "must own lock");
 746   remove(task);
 747 
 748   // Enqueue the task for reclamation (should be done outside MCQ lock)
 749   task->set_next(_first_stale);
 750   task->set_prev(NULL);
 751   _first_stale = task;
 752 }
 753 
 754 // methods in the compile queue need to be marked as used on the stack
 755 // so that they don't get reclaimed by Redefine Classes
 756 void CompileQueue::mark_on_stack() {
 757   CompileTask* task = _first;
 758   while (task != NULL) {
 759     task->mark_on_stack();
 760     task = task->next();
 761   }
 762 }
 763 
 764 
 765 CompileQueue* CompileBroker::compile_queue(int comp_level) {
 766   if (is_c2_compile(comp_level)) return _c2_compile_queue;
 767   if (is_c1_compile(comp_level)) return _c1_compile_queue;
 768   return NULL;
 769 }
 770 
 771 
 772 void CompileBroker::print_compile_queues(outputStream* st) {
 773   MutexLocker locker(MethodCompileQueue_lock);
 774   if (_c1_compile_queue != NULL) {
 775     _c1_compile_queue->print(st);
 776   }
 777   if (_c2_compile_queue != NULL) {
 778     _c2_compile_queue->print(st);
 779   }
 780 }
 781 
 782 void CompileQueue::print(outputStream* st) {
 783   assert(lock()->owned_by_self(), "must own lock");
 784   st->print_cr("Contents of %s", name());
 785   st->print_cr("----------------------------");
 786   CompileTask* task = _first;
 787   if (task == NULL) {
 788     st->print_cr("Empty");
 789   } else {
 790     while (task != NULL) {
 791       task->print_compilation(st, NULL, true, true);
 792       task = task->next();
 793     }
 794   }
 795   st->print_cr("----------------------------");
 796 }
 797 
 798 void CompileQueue::print_tty() {
 799   ttyLocker ttyl;
 800   print(tty);
 801 }
 802 
 803 CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) {


1197   // the pending list lock or a 3-way deadlock may occur
1198   // between the reference handler thread, a GC (instigated
1199   // by a compiler thread), and compiled method registration.
1200   if (InstanceRefKlass::owns_pending_list_lock(JavaThread::current())) {
1201     return;
1202   }
1203 
1204   if (TieredCompilation) {
1205     // Tiered policy requires MethodCounters to exist before adding a method to
1206     // the queue. Create if we don't have them yet.
1207     method->get_method_counters(thread);
1208   }
1209 
1210   // Outputs from the following MutexLocker block:
1211   CompileTask* task     = NULL;
1212   bool         blocking = false;
1213   CompileQueue* queue  = compile_queue(comp_level);
1214 
1215   // Acquire our lock.
1216   {
1217     MutexLocker locker(queue->lock(), thread);
1218 
1219     // Make sure the method has not slipped into the queues since
1220     // last we checked; note that those checks were "fast bail-outs".
1221     // Here we need to be more careful, see 14012000 below.
1222     if (compilation_is_in_queue(method)) {
1223       return;
1224     }
1225 
1226     // We need to check again to see if the compilation has
1227     // completed.  A previous compilation may have registered
1228     // some result.
1229     if (compilation_is_complete(method, osr_bci, comp_level)) {
1230       return;
1231     }
1232 
1233     // We now know that this compilation is not pending, complete,
1234     // or prohibited.  Assign a compile_id to this compilation
1235     // and check to see if it is in our [Start..Stop) range.
1236     int compile_id = assign_compile_id(method, osr_bci);
1237     if (compile_id == 0) {




 577   // Note:  "_is_complete" is about to be set, but is not.
 578   if (_num_inlined_bytecodes != 0) {
 579     log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);
 580   }
 581   log->stamp();
 582   log->end_elem();
 583   log->tail("task");
 584   log->clear_identities();   // next task will have different CI
 585   if (log->unflushed_count() > 2000) {
 586     log->flush();
 587   }
 588   log->mark_file_end();
 589 }
 590 
 591 
 592 
 593 /**
 594  * Add a CompileTask to a CompileQueue.
 595  */
 596 void CompileQueue::add(CompileTask* task) {
 597   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 598 
 599   task->set_next(NULL);
 600   task->set_prev(NULL);
 601 
 602   if (_last == NULL) {
 603     // The compile queue is empty.
 604     assert(_first == NULL, "queue is empty");
 605     _first = task;
 606     _last = task;
 607   } else {
 608     // Append the task to the queue.
 609     assert(_last->next() == NULL, "not last");
 610     _last->set_next(task);
 611     task->set_prev(_last);
 612     _last = task;
 613   }
 614   ++_size;
 615 
 616   // Mark the method as being in the compile queue.
 617   task->method()->set_queued_for_compilation();
 618 
 619   if (CIPrintCompileQueue) {
 620     print_tty();
 621   }
 622 
 623   if (LogCompilation && xtty != NULL) {
 624     task->log_task_queued();
 625   }
 626 
 627   // Notify CompilerThreads that a task is available.
 628   MethodCompileQueue_lock->notify_all();
 629 }
 630 
 631 /**
 632  * Empties compilation queue by putting all compilation tasks onto
 633  * a freelist. Furthermore, the method wakes up all threads that are
 634  * waiting on a compilation task to finish. This can happen if background
 635  * compilation is disabled.
 636  */
 637 void CompileQueue::free_all() {
 638   MutexLocker mu(MethodCompileQueue_lock);
 639   CompileTask* next = _first;
 640 
 641   // Iterate over all tasks in the compile queue
 642   while (next != NULL) {
 643     CompileTask* current = next;
 644     next = current->next();
 645     {
 646       // Wake up thread that blocks on the compile task.
 647       MutexLocker ct_lock(current->lock());
 648       current->lock()->notify();
 649     }
 650     // Put the task back on the freelist.
 651     CompileTask::free(current);
 652   }
 653   _first = NULL;
 654 
 655   // Wake up all threads that block on the queue.
 656   MethodCompileQueue_lock->notify_all();
 657 }
 658 
 659 /**
 660  * Get the next CompileTask from a CompileQueue
 661  */
 662 CompileTask* CompileQueue::get() {
 663   MutexLocker locker(MethodCompileQueue_lock);
 664   // If _first is NULL we have no more compile jobs. There are two reasons for
 665   // having no compile jobs: First, we compiled everything we wanted. Second,
 666   // we ran out of code cache so compilation has been disabled. In the latter
 667   // case we perform code cache sweeps to free memory such that we can re-enable
 668   // compilation.
 669   while (_first == NULL) {
 670     // Exit loop if compilation is disabled forever
 671     if (CompileBroker::is_compilation_disabled_forever()) {
 672       return NULL;
 673     }
 674 
 675     // If there are no compilation tasks and we can compile new jobs
 676     // (i.e., there is enough free space in the code cache) there is
 677     // no need to invoke the sweeper. As a result, the hotness of methods
 678     // remains unchanged. This behavior is desired, since we want to keep
 679     // the stable state, i.e., we do not want to evict methods from the
 680     // code cache if it is unnecessary.
 681     // We need a timed wait here, since compiler threads can exit if compilation
 682     // is disabled forever. We use 5 seconds wait time; the exiting of compiler threads
 683     // is not critical and we do not want idle compiler threads to wake up too often.
 684     MethodCompileQueue_lock->wait(!Mutex::_no_safepoint_check_flag, 5*1000);
 685   }
 686 
 687   if (CompileBroker::is_compilation_disabled_forever()) {
 688     return NULL;
 689   }
 690 
 691   CompileTask* task;
 692   {
 693     No_Safepoint_Verifier nsv;
 694     task = CompilationPolicy::policy()->select_task(this);
 695   }
 696   remove(task);
 697   purge_stale_tasks(); // may temporarily release MCQ lock
 698   return task;
 699 }
 700 
 701 // Clean & deallocate stale compile tasks.
 702 // Temporarily releases MethodCompileQueue lock.
 703 void CompileQueue::purge_stale_tasks() {
 704   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 705   if (_first_stale != NULL) {
 706     // Stale tasks are purged when MCQ lock is released,
 707     // but _first_stale updates are protected by MCQ lock.
 708     // Once task processing starts and MCQ lock is released,
 709     // other compiler threads can reuse _first_stale.
 710     CompileTask* head = _first_stale;
 711     _first_stale = NULL;
 712     {
 713       MutexUnlocker ul(MethodCompileQueue_lock);
 714       for (CompileTask* task = head; task != NULL; ) {
 715         CompileTask* next_task = task->next();
 716         CompileTaskWrapper ctw(task); // Frees the task
 717         task->set_failure_reason("stale task");
 718         task = next_task;
 719       }
 720     }
 721   }
 722 }
 723 
 724 void CompileQueue::remove(CompileTask* task) {
 725    assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 726   if (task->prev() != NULL) {
 727     task->prev()->set_next(task->next());
 728   } else {
 729     // max is the first element
 730     assert(task == _first, "Sanity");
 731     _first = task->next();
 732   }
 733 
 734   if (task->next() != NULL) {
 735     task->next()->set_prev(task->prev());
 736   } else {
 737     // max is the last element
 738     assert(task == _last, "Sanity");
 739     _last = task->prev();
 740   }
 741   --_size;
 742 }
 743 
 744 void CompileQueue::remove_and_mark_stale(CompileTask* task) {
 745   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 746   remove(task);
 747 
 748   // Enqueue the task for reclamation (should be done outside MCQ lock)
 749   task->set_next(_first_stale);
 750   task->set_prev(NULL);
 751   _first_stale = task;
 752 }
 753 
 754 // methods in the compile queue need to be marked as used on the stack
 755 // so that they don't get reclaimed by Redefine Classes
 756 void CompileQueue::mark_on_stack() {
 757   CompileTask* task = _first;
 758   while (task != NULL) {
 759     task->mark_on_stack();
 760     task = task->next();
 761   }
 762 }
 763 
 764 
 765 CompileQueue* CompileBroker::compile_queue(int comp_level) {
 766   if (is_c2_compile(comp_level)) return _c2_compile_queue;
 767   if (is_c1_compile(comp_level)) return _c1_compile_queue;
 768   return NULL;
 769 }
 770 
 771 
 772 void CompileBroker::print_compile_queues(outputStream* st) {
 773   MutexLocker locker(MethodCompileQueue_lock);
 774   if (_c1_compile_queue != NULL) {
 775     _c1_compile_queue->print(st);
 776   }
 777   if (_c2_compile_queue != NULL) {
 778     _c2_compile_queue->print(st);
 779   }
 780 }
 781 
 782 void CompileQueue::print(outputStream* st) {
 783   assert(MethodCompileQueue_lock->owned_by_self(), "must own lock");
 784   st->print_cr("Contents of %s", name());
 785   st->print_cr("----------------------------");
 786   CompileTask* task = _first;
 787   if (task == NULL) {
 788     st->print_cr("Empty");
 789   } else {
 790     while (task != NULL) {
 791       task->print_compilation(st, NULL, true, true);
 792       task = task->next();
 793     }
 794   }
 795   st->print_cr("----------------------------");
 796 }
 797 
 798 void CompileQueue::print_tty() {
 799   ttyLocker ttyl;
 800   print(tty);
 801 }
 802 
 803 CompilerCounters::CompilerCounters(const char* thread_name, int instance, TRAPS) {


1197   // the pending list lock or a 3-way deadlock may occur
1198   // between the reference handler thread, a GC (instigated
1199   // by a compiler thread), and compiled method registration.
1200   if (InstanceRefKlass::owns_pending_list_lock(JavaThread::current())) {
1201     return;
1202   }
1203 
1204   if (TieredCompilation) {
1205     // Tiered policy requires MethodCounters to exist before adding a method to
1206     // the queue. Create if we don't have them yet.
1207     method->get_method_counters(thread);
1208   }
1209 
1210   // Outputs from the following MutexLocker block:
1211   CompileTask* task     = NULL;
1212   bool         blocking = false;
1213   CompileQueue* queue  = compile_queue(comp_level);
1214 
1215   // Acquire our lock.
1216   {
1217     MutexLocker locker(MethodCompileQueue_lock, thread);
1218 
1219     // Make sure the method has not slipped into the queues since
1220     // last we checked; note that those checks were "fast bail-outs".
1221     // Here we need to be more careful, see 14012000 below.
1222     if (compilation_is_in_queue(method)) {
1223       return;
1224     }
1225 
1226     // We need to check again to see if the compilation has
1227     // completed.  A previous compilation may have registered
1228     // some result.
1229     if (compilation_is_complete(method, osr_bci, comp_level)) {
1230       return;
1231     }
1232 
1233     // We now know that this compilation is not pending, complete,
1234     // or prohibited.  Assign a compile_id to this compilation
1235     // and check to see if it is in our [Start..Stop) range.
1236     int compile_id = assign_compile_id(method, osr_bci);
1237     if (compile_id == 0) {


src/share/vm/compiler/compileBroker.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File