< prev index next >

src/share/vm/runtime/safepoint.cpp

Print this page
rev 13265 : 8180932: Parallelize safepoint cleanup
Summary: Provide infrastructure to do safepoint cleanup tasks using parallel worker threads
Reviewed-by: dholmes, rehn, dcubed, thartmann


  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/stringTable.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "code/icBuffer.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "code/pcDesc.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #include "gc/shared/collectedHeap.hpp"
  35 #include "gc/shared/gcLocker.inline.hpp"


  36 #include "interpreter/interpreter.hpp"
  37 #include "logging/log.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "memory/universe.inline.hpp"
  40 #include "oops/oop.inline.hpp"
  41 #include "oops/symbol.hpp"
  42 #include "runtime/atomic.hpp"
  43 #include "runtime/compilationPolicy.hpp"
  44 #include "runtime/deoptimization.hpp"
  45 #include "runtime/frame.inline.hpp"
  46 #include "runtime/interfaceSupport.hpp"
  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/orderAccess.inline.hpp"
  49 #include "runtime/osThread.hpp"
  50 #include "runtime/safepoint.hpp"
  51 #include "runtime/signature.hpp"
  52 #include "runtime/stubCodeGenerator.hpp"
  53 #include "runtime/stubRoutines.hpp"
  54 #include "runtime/sweeper.hpp"
  55 #include "runtime/synchronizer.hpp"


 523     event.commit();
 524   }
 525 }
 526 
 527 bool SafepointSynchronize::is_cleanup_needed() {
 528   // Need a safepoint if there are many monitors to deflate.
 529   if (ObjectSynchronizer::is_cleanup_needed()) return true;
 530   // Need a safepoint if some inline cache buffers is non-empty
 531   if (!InlineCacheBuffer::is_empty()) return true;
 532   return false;
 533 }
 534 
 535 static void event_safepoint_cleanup_task_commit(EventSafepointCleanupTask& event, const char* name) {
 536   if (event.should_commit()) {
 537     event.set_safepointId(SafepointSynchronize::safepoint_counter());
 538     event.set_name(name);
 539     event.commit();
 540   }
 541 }
 542 
 543 // Various cleaning tasks that should be done periodically at safepoints
 544 void SafepointSynchronize::do_cleanup_tasks() {
 545   {




































 546     const char* name = "deflating idle monitors";
 547     EventSafepointCleanupTask event;
 548     TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 549     ObjectSynchronizer::deflate_idle_monitors();
 550     event_safepoint_cleanup_task_commit(event, name);
 551   }
 552 
 553   {
 554     const char* name = "updating inline caches";
 555     EventSafepointCleanupTask event;
 556     TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 557     InlineCacheBuffer::update_inline_caches();
 558     event_safepoint_cleanup_task_commit(event, name);
 559   }
 560   {
 561     const char* name = "compilation policy safepoint handler";
 562     EventSafepointCleanupTask event;
 563     TraceTime timer("compilation policy safepoint handler", TRACETIME_LOG(Info, safepoint, cleanup));
 564     CompilationPolicy::policy()->do_safepoint_work();
 565     event_safepoint_cleanup_task_commit(event, name);
 566   }
 567 
 568   {
 569     const char* name = "mark nmethods";
 570     EventSafepointCleanupTask event;
 571     TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 572     NMethodSweeper::mark_active_nmethods();
 573     event_safepoint_cleanup_task_commit(event, name);
 574   }
 575 

 576   if (SymbolTable::needs_rehashing()) {
 577     const char* name = "rehashing symbol table";
 578     EventSafepointCleanupTask event;
 579     TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 580     SymbolTable::rehash_table();
 581     event_safepoint_cleanup_task_commit(event, name);
 582   }

 583 

 584   if (StringTable::needs_rehashing()) {
 585     const char* name = "rehashing string table";
 586     EventSafepointCleanupTask event;
 587     TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 588     StringTable::rehash_table();
 589     event_safepoint_cleanup_task_commit(event, name);
 590   }

 591 
 592   {
 593     // CMS delays purging the CLDG until the beginning of the next safepoint and to
 594     // make sure concurrent sweep is done
 595     const char* name = "purging class loader data graph";
 596     EventSafepointCleanupTask event;
 597     TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 598     ClassLoaderDataGraph::purge_if_needed();
 599     event_safepoint_cleanup_task_commit(event, name);
 600   }































 601 }
 602 
 603 
 604 bool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
 605   switch(state) {
 606   case _thread_in_native:
 607     // native threads are safe if they have no java stack or have walkable stack
 608     return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
 609 
 610    // blocked threads should have already have walkable stack
 611   case _thread_blocked:
 612     assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
 613     return true;
 614 
 615   default:
 616     return false;
 617   }
 618 }
 619 
 620 




  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/stringTable.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "code/icBuffer.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "code/pcDesc.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #include "gc/shared/collectedHeap.hpp"
  35 #include "gc/shared/gcLocker.inline.hpp"
  36 #include "gc/shared/strongRootsScope.hpp"
  37 #include "gc/shared/workgroup.hpp"
  38 #include "interpreter/interpreter.hpp"
  39 #include "logging/log.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "memory/universe.inline.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "oops/symbol.hpp"
  44 #include "runtime/atomic.hpp"
  45 #include "runtime/compilationPolicy.hpp"
  46 #include "runtime/deoptimization.hpp"
  47 #include "runtime/frame.inline.hpp"
  48 #include "runtime/interfaceSupport.hpp"
  49 #include "runtime/mutexLocker.hpp"
  50 #include "runtime/orderAccess.inline.hpp"
  51 #include "runtime/osThread.hpp"
  52 #include "runtime/safepoint.hpp"
  53 #include "runtime/signature.hpp"
  54 #include "runtime/stubCodeGenerator.hpp"
  55 #include "runtime/stubRoutines.hpp"
  56 #include "runtime/sweeper.hpp"
  57 #include "runtime/synchronizer.hpp"


 525     event.commit();
 526   }
 527 }
 528 
 529 bool SafepointSynchronize::is_cleanup_needed() {
 530   // Need a safepoint if there are many monitors to deflate.
 531   if (ObjectSynchronizer::is_cleanup_needed()) return true;
 532   // Need a safepoint if some inline cache buffers is non-empty
 533   if (!InlineCacheBuffer::is_empty()) return true;
 534   return false;
 535 }
 536 
 537 static void event_safepoint_cleanup_task_commit(EventSafepointCleanupTask& event, const char* name) {
 538   if (event.should_commit()) {
 539     event.set_safepointId(SafepointSynchronize::safepoint_counter());
 540     event.set_name(name);
 541     event.commit();
 542   }
 543 }
 544 
 545 class ParallelSPCleanupThreadClosure : public ThreadClosure {
 546 private:
 547   CodeBlobClosure* _nmethod_cl;
 548   DeflateMonitorCounters* _counters;
 549 
 550 public:
 551   ParallelSPCleanupThreadClosure(DeflateMonitorCounters* counters) :
 552     _counters(counters),
 553     _nmethod_cl(NMethodSweeper::prepare_mark_active_nmethods()) {}
 554 
 555   void do_thread(Thread* thread) {
 556     ObjectSynchronizer::deflate_thread_local_monitors(thread, _counters);
 557     if (_nmethod_cl != NULL && thread->is_Java_thread() &&
 558         ! thread->is_Code_cache_sweeper_thread()) {
 559       JavaThread* jt = (JavaThread*) thread;
 560       jt->nmethods_do(_nmethod_cl);
 561     }
 562   }
 563 };
 564 
 565 class ParallelSPCleanupTask : public AbstractGangTask {
 566 private:
 567   SubTasksDone _subtasks;
 568   ParallelSPCleanupThreadClosure _cleanup_threads_cl;
 569   uint _num_workers;
 570   DeflateMonitorCounters* _counters;
 571 public:
 572   ParallelSPCleanupTask(uint num_workers, DeflateMonitorCounters* counters) :
 573     AbstractGangTask("Parallel Safepoint Cleanup"),
 574     _cleanup_threads_cl(ParallelSPCleanupThreadClosure(counters)),
 575     _num_workers(num_workers),
 576     _subtasks(SubTasksDone(SafepointSynchronize::SAFEPOINT_CLEANUP_NUM_TASKS)),
 577     _counters(counters) {}
 578 
 579   void work(uint worker_id) {
 580     // All threads deflate monitors and mark nmethods (if necessary).
 581     Threads::parallel_java_threads_do(&_cleanup_threads_cl);
 582 
 583     if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_DEFLATE_MONITORS)) {
 584       const char* name = "deflating idle monitors";
 585       EventSafepointCleanupTask event;
 586       TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 587       ObjectSynchronizer::deflate_idle_monitors(_counters);
 588       event_safepoint_cleanup_task_commit(event, name);
 589     }
 590 
 591     if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_UPDATE_INLINE_CACHES)) {
 592       const char* name = "updating inline caches";
 593       EventSafepointCleanupTask event;
 594       TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 595       InlineCacheBuffer::update_inline_caches();
 596       event_safepoint_cleanup_task_commit(event, name);
 597     }







 598 
 599     if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_COMPILATION_POLICY)) {
 600       const char* name = "compilation policy safepoint handler";
 601       EventSafepointCleanupTask event;
 602       TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 603       CompilationPolicy::policy()->do_safepoint_work();
 604       event_safepoint_cleanup_task_commit(event, name);
 605     }
 606 
 607     if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH)) {
 608       if (SymbolTable::needs_rehashing()) {
 609         const char* name = "rehashing symbol table";
 610         EventSafepointCleanupTask event;
 611         TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 612         SymbolTable::rehash_table();
 613         event_safepoint_cleanup_task_commit(event, name);
 614       }
 615     }
 616 
 617     if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_STRING_TABLE_REHASH)) {
 618       if (StringTable::needs_rehashing()) {
 619         const char* name = "rehashing string table";
 620         EventSafepointCleanupTask event;
 621         TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 622         StringTable::rehash_table();
 623         event_safepoint_cleanup_task_commit(event, name);
 624       }
 625     }
 626 
 627     if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_CLD_PURGE)) {
 628       // CMS delays purging the CLDG until the beginning of the next safepoint and to
 629       // make sure concurrent sweep is done
 630       const char* name = "purging class loader data graph";
 631       EventSafepointCleanupTask event;
 632       TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
 633       ClassLoaderDataGraph::purge_if_needed();
 634       event_safepoint_cleanup_task_commit(event, name);
 635     }
 636     _subtasks.all_tasks_completed(_num_workers);
 637   }
 638 };
 639 
 640 // Various cleaning tasks that should be done periodically at safepoints.
 641 void SafepointSynchronize::do_cleanup_tasks() {
 642 
 643   TraceTime timer("safepoint cleanup tasks", TRACETIME_LOG(Info, safepoint, cleanup));
 644 
 645   // Prepare for monitor deflation.
 646   DeflateMonitorCounters deflate_counters;
 647   ObjectSynchronizer::prepare_deflate_idle_monitors(&deflate_counters);
 648 
 649   CollectedHeap* heap = Universe::heap();
 650   assert(heap != NULL, "heap not initialized yet?");
 651   WorkGang* cleanup_workers = heap->get_safepoint_workers();
 652   if (cleanup_workers != NULL) {
 653     // Parallel cleanup using GC provided thread pool.
 654     uint num_cleanup_workers = cleanup_workers->active_workers();
 655     ParallelSPCleanupTask cleanup(num_cleanup_workers, &deflate_counters);
 656     StrongRootsScope srs(num_cleanup_workers);
 657     cleanup_workers->run_task(&cleanup);
 658   } else {
 659     // Serial cleanup using VMThread.
 660     ParallelSPCleanupTask cleanup(1, &deflate_counters);
 661     StrongRootsScope srs(1);
 662     cleanup.work(0);
 663   }
 664 
 665   // Finish monitor deflation.
 666   ObjectSynchronizer::finish_deflate_idle_monitors(&deflate_counters);
 667 }
 668 
 669 
 670 bool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
 671   switch(state) {
 672   case _thread_in_native:
 673     // native threads are safe if they have no java stack or have walkable stack
 674     return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
 675 
 676    // blocked threads should have already have walkable stack
 677   case _thread_blocked:
 678     assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
 679     return true;
 680 
 681   default:
 682     return false;
 683   }
 684 }
 685 
 686 


< prev index next >