30 #include "code/nmethod.hpp" |
30 #include "code/nmethod.hpp" |
31 #include "code/pcDesc.hpp" |
31 #include "code/pcDesc.hpp" |
32 #include "code/scopeDesc.hpp" |
32 #include "code/scopeDesc.hpp" |
33 #include "gc_interface/collectedHeap.hpp" |
33 #include "gc_interface/collectedHeap.hpp" |
34 #include "interpreter/interpreter.hpp" |
34 #include "interpreter/interpreter.hpp" |
|
35 #include "jfr/jfrEvents.hpp" |
35 #include "memory/resourceArea.hpp" |
36 #include "memory/resourceArea.hpp" |
36 #include "memory/universe.inline.hpp" |
37 #include "memory/universe.inline.hpp" |
37 #include "oops/oop.inline.hpp" |
38 #include "oops/oop.inline.hpp" |
38 #include "oops/symbol.hpp" |
39 #include "oops/symbol.hpp" |
39 #include "runtime/compilationPolicy.hpp" |
40 #include "runtime/compilationPolicy.hpp" |
81 #include "c1/c1_globals.hpp" |
82 #include "c1/c1_globals.hpp" |
82 #endif |
83 #endif |
83 |
84 |
84 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
85 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
85 |
86 |
|
87 template <typename E> |
|
88 static void set_current_safepoint_id(E* event, int adjustment = 0) { |
|
89 assert(event != NULL, "invariant"); |
|
90 event->set_safepointId(SafepointSynchronize::safepoint_counter() + adjustment); |
|
91 } |
|
92 |
|
93 static void post_safepoint_begin_event(EventSafepointBegin* event, |
|
94 int thread_count, |
|
95 int critical_thread_count) { |
|
96 assert(event != NULL, "invariant"); |
|
97 assert(event->should_commit(), "invariant"); |
|
98 set_current_safepoint_id(event); |
|
99 event->set_totalThreadCount(thread_count); |
|
100 event->set_jniCriticalThreadCount(critical_thread_count); |
|
101 event->commit(); |
|
102 } |
|
103 |
|
104 static void post_safepoint_cleanup_event(EventSafepointCleanup* event) { |
|
105 assert(event != NULL, "invariant"); |
|
106 assert(event->should_commit(), "invariant"); |
|
107 set_current_safepoint_id(event); |
|
108 event->commit(); |
|
109 } |
|
110 |
|
111 static void post_safepoint_synchronize_event(EventSafepointStateSynchronization* event, |
|
112 int initial_number_of_threads, |
|
113 int threads_waiting_to_block, |
|
114 unsigned int iterations) { |
|
115 assert(event != NULL, "invariant"); |
|
116 if (event->should_commit()) { |
|
117 // Group this event together with the ones committed after the counter is increased |
|
118 set_current_safepoint_id(event, 1); |
|
119 event->set_initialThreadCount(initial_number_of_threads); |
|
120 event->set_runningThreadCount(threads_waiting_to_block); |
|
121 event->set_iterations(iterations); |
|
122 event->commit(); |
|
123 } |
|
124 } |
|
125 |
|
126 static void post_safepoint_wait_blocked_event(EventSafepointWaitBlocked* event, |
|
127 int initial_threads_waiting_to_block) { |
|
128 assert(event != NULL, "invariant"); |
|
129 assert(event->should_commit(), "invariant"); |
|
130 set_current_safepoint_id(event); |
|
131 event->set_runningThreadCount(initial_threads_waiting_to_block); |
|
132 event->commit(); |
|
133 } |
|
134 |
|
135 static void post_safepoint_cleanup_task_event(EventSafepointCleanupTask* event, |
|
136 const char* name) { |
|
137 assert(event != NULL, "invariant"); |
|
138 if (event->should_commit()) { |
|
139 set_current_safepoint_id(event); |
|
140 event->set_name(name); |
|
141 event->commit(); |
|
142 } |
|
143 } |
|
144 |
|
145 static void post_safepoint_end_event(EventSafepointEnd* event) { |
|
146 assert(event != NULL, "invariant"); |
|
147 if (event->should_commit()) { |
|
148 // Group this event together with the ones committed before the counter increased |
|
149 set_current_safepoint_id(event, -1); |
|
150 event->commit(); |
|
151 } |
|
152 } |
|
153 |
86 // -------------------------------------------------------------------------------------------------- |
154 // -------------------------------------------------------------------------------------------------- |
87 // Implementation of Safepoint begin/end |
155 // Implementation of Safepoint begin/end |
88 |
156 |
89 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized; |
157 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized; |
90 volatile int SafepointSynchronize::_waiting_to_block = 0; |
158 volatile int SafepointSynchronize::_waiting_to_block = 0; |
95 static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only |
163 static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only |
96 static bool timeout_error_printed = false; |
164 static bool timeout_error_printed = false; |
97 |
165 |
98 // Roll all threads forward to a safepoint and suspend them all |
166 // Roll all threads forward to a safepoint and suspend them all |
99 void SafepointSynchronize::begin() { |
167 void SafepointSynchronize::begin() { |
100 |
168 EventSafepointBegin begin_event; |
101 Thread* myThread = Thread::current(); |
169 Thread* myThread = Thread::current(); |
102 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint"); |
170 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint"); |
103 |
171 |
104 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) { |
172 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) { |
105 _safepoint_begin_time = os::javaTimeNanos(); |
173 _safepoint_begin_time = os::javaTimeNanos(); |
187 // 5. In VM or Transitioning between states |
255 // 5. In VM or Transitioning between states |
188 // If a Java thread is currently running in the VM or transitioning |
256 // If a Java thread is currently running in the VM or transitioning |
189 // between states, the safepointing code will wait for the thread to |
257 // between states, the safepointing code will wait for the thread to |
190 // block itself when it attempts transitions to a new state. |
258 // block itself when it attempts transitions to a new state. |
191 // |
259 // |
|
260 EventSafepointStateSynchronization sync_event; |
|
261 int initial_running = 0; |
|
262 |
192 _state = _synchronizing; |
263 _state = _synchronizing; |
193 OrderAccess::fence(); |
264 OrderAccess::fence(); |
194 |
265 |
195 // Flush all thread states to memory |
266 // Flush all thread states to memory |
196 if (!UseMembar) { |
267 if (!UseMembar) { |
334 |
408 |
335 if (PrintSafepointStatistics) { |
409 if (PrintSafepointStatistics) { |
336 update_statistics_on_spin_end(); |
410 update_statistics_on_spin_end(); |
337 } |
411 } |
338 |
412 |
|
413 if (sync_event.should_commit()) { |
|
414 post_safepoint_synchronize_event(&sync_event, initial_running, _waiting_to_block, iterations); |
|
415 } |
|
416 |
339 // wait until all threads are stopped |
417 // wait until all threads are stopped |
340 while (_waiting_to_block > 0) { |
418 { |
341 if (TraceSafepoint) tty->print_cr("Waiting for %d thread(s) to block", _waiting_to_block); |
419 EventSafepointWaitBlocked wait_blocked_event; |
342 if (!SafepointTimeout || timeout_error_printed) { |
420 int initial_waiting_to_block = _waiting_to_block; |
343 Safepoint_lock->wait(true); // true, means with no safepoint checks |
421 |
344 } else { |
422 while (_waiting_to_block > 0) { |
345 // Compute remaining time |
423 if (TraceSafepoint) tty->print_cr("Waiting for %d thread(s) to block", _waiting_to_block); |
346 jlong remaining_time = safepoint_limit_time - os::javaTimeNanos(); |
424 if (!SafepointTimeout || timeout_error_printed) { |
347 |
425 Safepoint_lock->wait(true); // true, means with no safepoint checks |
348 // If there is no remaining time, then there is an error |
426 } else { |
349 if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) { |
427 // Compute remaining time |
350 print_safepoint_timeout(_blocking_timeout); |
428 jlong remaining_time = safepoint_limit_time - os::javaTimeNanos(); |
|
429 |
|
430 // If there is no remaining time, then there is an error |
|
431 if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) { |
|
432 print_safepoint_timeout(_blocking_timeout); |
|
433 } |
351 } |
434 } |
352 } |
435 } |
353 } |
436 assert(_waiting_to_block == 0, "sanity check"); |
354 assert(_waiting_to_block == 0, "sanity check"); |
|
355 |
437 |
356 #ifndef PRODUCT |
438 #ifndef PRODUCT |
357 if (SafepointTimeout) { |
439 if (SafepointTimeout) { |
358 jlong current_time = os::javaTimeNanos(); |
440 jlong current_time = os::javaTimeNanos(); |
359 if (safepoint_limit_time < current_time) { |
441 if (safepoint_limit_time < current_time) { |
360 tty->print_cr("# SafepointSynchronize: Finished after " |
442 tty->print_cr("# SafepointSynchronize: Finished after " |
361 INT64_FORMAT_W(6) " ms", |
443 INT64_FORMAT_W(6) " ms", |
362 ((current_time - safepoint_limit_time) / MICROUNITS + |
444 ((current_time - safepoint_limit_time) / MICROUNITS + |
363 SafepointTimeoutDelay)); |
445 SafepointTimeoutDelay)); |
364 } |
446 } |
365 } |
447 } |
366 #endif |
448 #endif |
367 |
449 |
368 assert((_safepoint_counter & 0x1) == 0, "must be even"); |
450 assert((_safepoint_counter & 0x1) == 0, "must be even"); |
369 assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); |
451 assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); |
370 _safepoint_counter ++; |
452 _safepoint_counter ++; |
371 |
453 |
372 // Record state |
454 // Record state |
373 _state = _synchronized; |
455 _state = _synchronized; |
374 |
456 |
375 OrderAccess::fence(); |
457 OrderAccess::fence(); |
|
458 |
|
459 if (wait_blocked_event.should_commit()) { |
|
460 post_safepoint_wait_blocked_event(&wait_blocked_event, initial_waiting_to_block); |
|
461 } |
|
462 } |
376 |
463 |
377 #ifdef ASSERT |
464 #ifdef ASSERT |
378 for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) { |
465 for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) { |
379 // make sure all the threads were visited |
466 // make sure all the threads were visited |
380 assert(cur->was_visited_for_critical_count(), "missed a thread"); |
467 assert(cur->was_visited_for_critical_count(), "missed a thread"); |
393 if (PrintSafepointStatistics) { |
480 if (PrintSafepointStatistics) { |
394 update_statistics_on_sync_end(os::javaTimeNanos()); |
481 update_statistics_on_sync_end(os::javaTimeNanos()); |
395 } |
482 } |
396 |
483 |
397 // Call stuff that needs to be run when a safepoint is just about to be completed |
484 // Call stuff that needs to be run when a safepoint is just about to be completed |
398 do_cleanup_tasks(); |
485 { |
|
486 EventSafepointCleanup cleanup_event; |
|
487 do_cleanup_tasks(); |
|
488 if (cleanup_event.should_commit()) { |
|
489 post_safepoint_cleanup_event(&cleanup_event); |
|
490 } |
|
491 } |
399 |
492 |
400 if (PrintSafepointStatistics) { |
493 if (PrintSafepointStatistics) { |
401 // Record how much time spend on the above cleanup tasks |
494 // Record how much time spend on the above cleanup tasks |
402 update_statistics_on_cleanup_end(os::javaTimeNanos()); |
495 update_statistics_on_cleanup_end(os::javaTimeNanos()); |
403 } |
496 } |
|
497 |
|
498 if (begin_event.should_commit()) { |
|
499 post_safepoint_begin_event(&begin_event, nof_threads, _current_jni_active_count); |
|
500 } |
404 } |
501 } |
405 |
502 |
406 // Wake up all threads, so they are ready to resume execution after the safepoint |
503 // Wake up all threads, so they are ready to resume execution after the safepoint |
407 // operation has been carried out |
504 // operation has been carried out |
408 void SafepointSynchronize::end() { |
505 void SafepointSynchronize::end() { |
409 |
506 |
410 assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); |
507 assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); |
411 assert((_safepoint_counter & 0x1) == 1, "must be odd"); |
508 assert((_safepoint_counter & 0x1) == 1, "must be odd"); |
|
509 EventSafepointEnd event; |
412 _safepoint_counter ++; |
510 _safepoint_counter ++; |
413 // memory fence isn't required here since an odd _safepoint_counter |
511 // memory fence isn't required here since an odd _safepoint_counter |
414 // value can do no harm and a fence is issued below anyway. |
512 // value can do no harm and a fence is issued below anyway. |
415 |
513 |
416 DEBUG_ONLY(Thread* myThread = Thread::current();) |
514 DEBUG_ONLY(Thread* myThread = Thread::current();) |
492 } |
590 } |
493 #endif // INCLUDE_ALL_GCS |
591 #endif // INCLUDE_ALL_GCS |
494 // record this time so VMThread can keep track how much time has elasped |
592 // record this time so VMThread can keep track how much time has elasped |
495 // since last safepoint. |
593 // since last safepoint. |
496 _end_of_last_safepoint = os::javaTimeMillis(); |
594 _end_of_last_safepoint = os::javaTimeMillis(); |
|
595 if (event.should_commit()) { |
|
596 post_safepoint_end_event(&event); |
|
597 } |
497 } |
598 } |
498 |
599 |
499 bool SafepointSynchronize::is_cleanup_needed() { |
600 bool SafepointSynchronize::is_cleanup_needed() { |
500 // Need a safepoint if some inline cache buffers is non-empty |
601 // Need a safepoint if some inline cache buffers is non-empty |
501 if (!InlineCacheBuffer::is_empty()) return true; |
602 if (!InlineCacheBuffer::is_empty()) return true; |
505 |
606 |
506 |
607 |
507 // Various cleaning tasks that should be done periodically at safepoints |
608 // Various cleaning tasks that should be done periodically at safepoints |
508 void SafepointSynchronize::do_cleanup_tasks() { |
609 void SafepointSynchronize::do_cleanup_tasks() { |
509 { |
610 { |
510 TraceTime t1("deflating idle monitors", TraceSafepointCleanupTime); |
611 const char* name = "deflating idle monitors"; |
|
612 EventSafepointCleanupTask event; |
|
613 TraceTime t1(name, TraceSafepointCleanupTime); |
511 ObjectSynchronizer::deflate_idle_monitors(); |
614 ObjectSynchronizer::deflate_idle_monitors(); |
|
615 if (event.should_commit()) { |
|
616 post_safepoint_cleanup_task_event(&event, name); |
|
617 } |
512 } |
618 } |
513 |
619 |
514 { |
620 { |
515 TraceTime t2("updating inline caches", TraceSafepointCleanupTime); |
621 const char* name = "updating inline caches"; |
|
622 EventSafepointCleanupTask event; |
|
623 TraceTime t2(name, TraceSafepointCleanupTime); |
516 InlineCacheBuffer::update_inline_caches(); |
624 InlineCacheBuffer::update_inline_caches(); |
|
625 if (event.should_commit()) { |
|
626 post_safepoint_cleanup_task_event(&event, name); |
|
627 } |
517 } |
628 } |
518 { |
629 { |
519 TraceTime t3("compilation policy safepoint handler", TraceSafepointCleanupTime); |
630 const char* name = "compilation policy safepoint handler"; |
|
631 EventSafepointCleanupTask event; |
|
632 TraceTime t3(name, TraceSafepointCleanupTime); |
520 CompilationPolicy::policy()->do_safepoint_work(); |
633 CompilationPolicy::policy()->do_safepoint_work(); |
|
634 if (event.should_commit()) { |
|
635 post_safepoint_cleanup_task_event(&event, name); |
|
636 } |
521 } |
637 } |
522 |
638 |
523 { |
639 { |
524 TraceTime t4("mark nmethods", TraceSafepointCleanupTime); |
640 const char* name = "mark nmethods"; |
|
641 EventSafepointCleanupTask event; |
|
642 TraceTime t4(name, TraceSafepointCleanupTime); |
525 NMethodSweeper::mark_active_nmethods(); |
643 NMethodSweeper::mark_active_nmethods(); |
|
644 if (event.should_commit()) { |
|
645 post_safepoint_cleanup_task_event(&event, name); |
|
646 } |
526 } |
647 } |
527 |
648 |
528 if (SymbolTable::needs_rehashing()) { |
649 if (SymbolTable::needs_rehashing()) { |
529 TraceTime t5("rehashing symbol table", TraceSafepointCleanupTime); |
650 const char* name = "rehashing symbol table"; |
|
651 EventSafepointCleanupTask event; |
|
652 TraceTime t5(name, TraceSafepointCleanupTime); |
530 SymbolTable::rehash_table(); |
653 SymbolTable::rehash_table(); |
|
654 if (event.should_commit()) { |
|
655 post_safepoint_cleanup_task_event(&event, name); |
|
656 } |
531 } |
657 } |
532 |
658 |
533 if (StringTable::needs_rehashing()) { |
659 if (StringTable::needs_rehashing()) { |
534 TraceTime t6("rehashing string table", TraceSafepointCleanupTime); |
660 const char* name = "rehashing string table"; |
|
661 EventSafepointCleanupTask event; |
|
662 TraceTime t6(name, TraceSafepointCleanupTime); |
535 StringTable::rehash_table(); |
663 StringTable::rehash_table(); |
|
664 if (event.should_commit()) { |
|
665 post_safepoint_cleanup_task_event(&event, name); |
|
666 } |
536 } |
667 } |
537 |
668 |
538 // rotate log files? |
669 // rotate log files? |
539 if (UseGCLogFileRotation) { |
670 if (UseGCLogFileRotation) { |
540 TraceTime t8("rotating gc logs", TraceSafepointCleanupTime); |
671 TraceTime t8("rotating gc logs", TraceSafepointCleanupTime); |