54 #include "runtime/sweeper.hpp"
55 #include "runtime/synchronizer.hpp"
56 #include "runtime/thread.inline.hpp"
57 #include "runtime/timerTrace.hpp"
58 #include "services/runtimeService.hpp"
59 #include "trace/tracing.hpp"
60 #include "trace/traceMacros.hpp"
61 #include "utilities/events.hpp"
62 #include "utilities/macros.hpp"
63 #if INCLUDE_ALL_GCS
64 #include "gc/cms/concurrentMarkSweepThread.hpp"
65 #include "gc/g1/suspendibleThreadSet.hpp"
66 #endif // INCLUDE_ALL_GCS
67 #ifdef COMPILER1
68 #include "c1/c1_globals.hpp"
69 #endif
70
71 // --------------------------------------------------------------------------------------------------
72 // Implementation of Safepoint begin/end
73
74 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
75 volatile int SafepointSynchronize::_waiting_to_block = 0;
76 volatile int SafepointSynchronize::_safepoint_counter = 0;
77 int SafepointSynchronize::_current_jni_active_count = 0;
78 long SafepointSynchronize::_end_of_last_safepoint = 0;
79 static volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE
80 static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only
81 static bool timeout_error_printed = false;
82
83 // Roll all threads forward to a safepoint and suspend them all
84 void SafepointSynchronize::begin() {
85 EventSafepointBegin begin_event;
86 Thread* myThread = Thread::current();
87 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
88
89 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
90 _safepoint_begin_time = os::javaTimeNanos();
91 _ts_of_current_safepoint = tty->time_stamp().seconds();
92 }
93
521 if (event.should_commit()) {
522 event.set_safepointId(safepoint_id);
523 event.commit();
524 }
525 }
526
527 bool SafepointSynchronize::is_cleanup_needed() {
528 // Need a safepoint if some inline cache buffers is non-empty
529 if (!InlineCacheBuffer::is_empty()) return true;
530 return false;
531 }
532
533 static void event_safepoint_cleanup_task_commit(EventSafepointCleanupTask& event, const char* name) {
534 if (event.should_commit()) {
535 event.set_safepointId(SafepointSynchronize::safepoint_counter());
536 event.set_name(name);
537 event.commit();
538 }
539 }
540
541 // Various cleaning tasks that should be done periodically at safepoints
542 void SafepointSynchronize::do_cleanup_tasks() {
543 {
544 const char* name = "deflating idle monitors";
545 EventSafepointCleanupTask event;
546 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
547 ObjectSynchronizer::deflate_idle_monitors();
548 event_safepoint_cleanup_task_commit(event, name);
549 }
550
551 {
552 const char* name = "updating inline caches";
553 EventSafepointCleanupTask event;
554 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
555 InlineCacheBuffer::update_inline_caches();
556 event_safepoint_cleanup_task_commit(event, name);
557 }
558 {
559 const char* name = "compilation policy safepoint handler";
560 EventSafepointCleanupTask event;
561 TraceTime timer("compilation policy safepoint handler", TRACETIME_LOG(Info, safepoint, cleanup));
562 CompilationPolicy::policy()->do_safepoint_work();
563 event_safepoint_cleanup_task_commit(event, name);
564 }
565
566 {
567 const char* name = "mark nmethods";
568 EventSafepointCleanupTask event;
569 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
570 NMethodSweeper::mark_active_nmethods();
571 event_safepoint_cleanup_task_commit(event, name);
572 }
573
574 if (SymbolTable::needs_rehashing()) {
575 const char* name = "rehashing symbol table";
576 EventSafepointCleanupTask event;
577 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
578 SymbolTable::rehash_table();
579 event_safepoint_cleanup_task_commit(event, name);
580 }
581
582 if (StringTable::needs_rehashing()) {
583 const char* name = "rehashing string table";
584 EventSafepointCleanupTask event;
585 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
586 StringTable::rehash_table();
587 event_safepoint_cleanup_task_commit(event, name);
588 }
589
590 {
591 // CMS delays purging the CLDG until the beginning of the next safepoint and to
592 // make sure concurrent sweep is done
593 const char* name = "purging class loader data graph";
594 EventSafepointCleanupTask event;
595 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
596 ClassLoaderDataGraph::purge_if_needed();
597 event_safepoint_cleanup_task_commit(event, name);
598 }
599 }
600
601
602 bool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
603 switch(state) {
604 case _thread_in_native:
605 // native threads are safe if they have no java stack or have walkable stack
606 return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
607
608 // blocked threads should have already have walkable stack
609 case _thread_blocked:
610 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
611 return true;
612
613 default:
614 return false;
615 }
616 }
617
618
619 // See if the thread is running inside a lazy critical native and
620 // update the thread critical count if so. Also set a suspend flag to
|
54 #include "runtime/sweeper.hpp"
55 #include "runtime/synchronizer.hpp"
56 #include "runtime/thread.inline.hpp"
57 #include "runtime/timerTrace.hpp"
58 #include "services/runtimeService.hpp"
59 #include "trace/tracing.hpp"
60 #include "trace/traceMacros.hpp"
61 #include "utilities/events.hpp"
62 #include "utilities/macros.hpp"
63 #if INCLUDE_ALL_GCS
64 #include "gc/cms/concurrentMarkSweepThread.hpp"
65 #include "gc/g1/suspendibleThreadSet.hpp"
66 #endif // INCLUDE_ALL_GCS
67 #ifdef COMPILER1
68 #include "c1/c1_globals.hpp"
69 #endif
70
71 // --------------------------------------------------------------------------------------------------
72 // Implementation of Safepoint begin/end
73
74 WorkGang* SafepointSynchronize::_cleanup_workers = NULL;
75 SubTasksDone* SafepointSynchronize::_cleanup_subtasks = NULL;
76 uint SafepointSynchronize::_num_cleanup_workers = 0;
77
78 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
79 volatile int SafepointSynchronize::_waiting_to_block = 0;
80 volatile int SafepointSynchronize::_safepoint_counter = 0;
81 int SafepointSynchronize::_current_jni_active_count = 0;
82 long SafepointSynchronize::_end_of_last_safepoint = 0;
83 static volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE
84 static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only
85 static bool timeout_error_printed = false;
86
87 // Roll all threads forward to a safepoint and suspend them all
88 void SafepointSynchronize::begin() {
89 EventSafepointBegin begin_event;
90 Thread* myThread = Thread::current();
91 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
92
93 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
94 _safepoint_begin_time = os::javaTimeNanos();
95 _ts_of_current_safepoint = tty->time_stamp().seconds();
96 }
97
525 if (event.should_commit()) {
526 event.set_safepointId(safepoint_id);
527 event.commit();
528 }
529 }
530
531 bool SafepointSynchronize::is_cleanup_needed() {
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
549 public:
550 ParallelSPCleanupThreadClosure() {
551 _nmethod_cl = NMethodSweeper::prepare_mark_active_nmethods();
552 }
553
554 void do_thread(Thread* thread) {
555 ObjectSynchronizer::deflate_thread_local_monitors(thread);
556 if (_nmethod_cl != NULL && thread->is_Java_thread() &&
557 ! thread->is_Code_cache_sweeper_thread()) {
558 JavaThread* jt = (JavaThread*) thread;
559 jt->nmethods_do(_nmethod_cl);
560 }
561 }
562 };
563
564 class ParallelSPCleanupTask : public AbstractGangTask {
565 private:
566 SubTasksDone* _subtasks;
567 ParallelSPCleanupThreadClosure _cleanup_threads_cl;
568 public:
569 ParallelSPCleanupTask(SubTasksDone* subtasks) :
570 AbstractGangTask("Parallel Safepoint Cleanup"),
571 _cleanup_threads_cl(ParallelSPCleanupThreadClosure()),
572 _subtasks(subtasks) {}
573
574 void work(uint worker_id) {
575 // All threads deflate monitors and mark nmethods (if necessary).
576 Threads::parallel_java_threads_do(&_cleanup_threads_cl);
577
578 if (! _subtasks->is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_DEFLATE_MONITORS)) {
579 const char* name = "deflating idle monitors";
580 EventSafepointCleanupTask event;
581 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
582 ObjectSynchronizer::deflate_idle_monitors();
583 event_safepoint_cleanup_task_commit(event, name);
584 }
585
586 if (! _subtasks->is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_UPDATE_INLINE_CACHES)) {
587 const char* name = "updating inline caches";
588 EventSafepointCleanupTask event;
589 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
590 InlineCacheBuffer::update_inline_caches();
591 event_safepoint_cleanup_task_commit(event, name);
592 }
593
594 if (! _subtasks->is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_COMPILATION_POLICY)) {
595 const char* name = "compilation policy safepoint handler";
596 EventSafepointCleanupTask event;
597 TraceTime timer("compilation policy safepoint handler", TRACETIME_LOG(Info, safepoint, cleanup));
598 CompilationPolicy::policy()->do_safepoint_work();
599 event_safepoint_cleanup_task_commit(event, name);
600 }
601
602 if (! _subtasks->is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH)) {
603 if (SymbolTable::needs_rehashing()) {
604 const char* name = "rehashing symbol table";
605 EventSafepointCleanupTask event;
606 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
607 SymbolTable::rehash_table();
608 event_safepoint_cleanup_task_commit(event, name);
609 }
610 }
611
612 if (! _subtasks->is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_STRING_TABLE_REHASH)) {
613 if (StringTable::needs_rehashing()) {
614 const char* name = "rehashing string table";
615 EventSafepointCleanupTask event;
616 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
617 StringTable::rehash_table();
618 event_safepoint_cleanup_task_commit(event, name);
619 }
620 }
621
622 if (! _subtasks->is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_CLD_PURGE)) {
623 // CMS delays purging the CLDG until the beginning of the next safepoint and to
624 // make sure concurrent sweep is done
625 const char* name = "purging class loader data graph";
626 EventSafepointCleanupTask event;
627 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
628 ClassLoaderDataGraph::purge_if_needed();
629 event_safepoint_cleanup_task_commit(event, name);
630 }
631 _subtasks->all_tasks_completed(SafepointSynchronize::_num_cleanup_workers);
632 }
633 };
634
635 // Various cleaning tasks that should be done periodically at safepoints
636 void SafepointSynchronize::do_cleanup_tasks() {
637 if (_num_cleanup_workers == 0) {
638 // Deferred initialization.
639 if (ParallelSafepointCleanup) {
640 assert(_cleanup_workers == NULL, "already initialized?");
641 // Ask the GC to share its workers.
642 CollectedHeap* heap = Universe::heap();
643 assert(heap != NULL, "heap not initialized yet?");
644 _cleanup_workers = heap->get_safepoint_workers();
645 if (_cleanup_workers == NULL) {
646 // If GC doesn't want to share or doesn't even use worker threads, we create our own thread pool.
647 _cleanup_workers = new WorkGang("Parallel Safepoint Cleanup", ParallelSafepointCleanupThreads, false, false);
648 _cleanup_workers->initialize_workers();
649 }
650 _num_cleanup_workers = MIN2(ParallelSafepointCleanupThreads, _cleanup_workers->total_workers());
651 } else {
652 _num_cleanup_workers = 1;
653 }
654 assert(_num_cleanup_workers != 0, "not initialized?");
655 _cleanup_subtasks = new SubTasksDone(SAFEPOINT_CLEANUP_NUM_TASKS);
656 }
657
658 // Run cleanup serially or in parallel.
659 ParallelSPCleanupTask cleanup(_cleanup_subtasks);
660 StrongRootsScope srs(_num_cleanup_workers);
661 if (ParallelSafepointCleanup) {
662 assert(_cleanup_workers != NULL, "no cleanup workers?");
663 _cleanup_workers->run_task(&cleanup, _num_cleanup_workers);
664 } else {
665 assert(_num_cleanup_workers == 1, "only 1 thread");
666 cleanup.work(0);
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
687 // See if the thread is running inside a lazy critical native and
688 // update the thread critical count if so. Also set a suspend flag to
|