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"
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
94 #if INCLUDE_ALL_GCS
95 if (UseConcMarkSweepGC) {
96 // In the future we should investigate whether CMS can use the
97 // more-general mechanism below. DLD (01/05).
98 ConcurrentMarkSweepThread::synchronize(false);
99 } else if (UseG1GC) {
100 SuspendibleThreadSet::synchronize();
101 }
102 #endif // INCLUDE_ALL_GCS
103
104 // By getting the Threads_lock, we assure that no threads are about to start or
105 // exit. It is released again in SafepointSynchronize::end().
156 // a memory barrier for each Java thread making native calls, each Java
157 // thread performs a write to a single memory page after changing
158 // the thread state. The VM thread performs a sequence of
159 // mprotect OS calls which forces all previous writes from all
160 // Java threads to be serialized. This is done in the
161 // os::serialize_thread_states() call. This has proven to be
162 // much more efficient than executing a membar instruction
163 // on every call to native code.
164 // 3. Running compiled Code
165 // Compiled code reads a global (Safepoint Polling) page that
166 // is set to fault if we are trying to get to a safepoint.
167 // 4. Blocked
168 // A thread which is blocked will not be allowed to return from the
169 // block condition until the safepoint operation is complete.
170 // 5. In VM or Transitioning between states
171 // If a Java thread is currently running in the VM or transitioning
172 // between states, the safepointing code will wait for the thread to
173 // block itself when it attempts transitions to a new state.
174 //
175 {
176 EventSafepointStateSynchronization sync_event;
177 int initial_running = 0;
178
179 _state = _synchronizing;
180 OrderAccess::fence();
181
182 // Flush all thread states to memory
183 if (!UseMembar) {
184 os::serialize_thread_states();
185 }
186
187 // Make interpreter safepoint aware
188 Interpreter::notice_safepoints();
189
190 if (DeferPollingPageLoopCount < 0) {
191 // Make polling safepoint aware
192 guarantee (PageArmed == 0, "invariant") ;
193 PageArmed = 1 ;
194 os::make_polling_page_unreadable();
195 }
196
310 ++steps ;
311 if (ncpus > 1 && steps < SafepointSpinBeforeYield) {
312 SpinPause() ; // MP-Polite spin
313 } else
314 if (steps < DeferThrSuspendLoopCount) {
315 os::naked_yield() ;
316 } else {
317 os::naked_short_sleep(1);
318 }
319
320 iterations ++ ;
321 }
322 assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
323 }
324 assert(still_running == 0, "sanity check");
325
326 if (PrintSafepointStatistics) {
327 update_statistics_on_spin_end();
328 }
329
330 if (sync_event.should_commit()) {
331 sync_event.set_safepointId(safepoint_counter());
332 sync_event.set_initialThreadCount(initial_running);
333 sync_event.set_runningThreadCount(_waiting_to_block);
334 sync_event.set_iterations(iterations);
335 sync_event.commit();
336 }
337 } //EventSafepointStateSync
338
339 // wait until all threads are stopped
340 {
341 EventSafepointWaitBlocked wait_blocked_event;
342 int initial_waiting_to_block = _waiting_to_block;
343
344 while (_waiting_to_block > 0) {
345 log_debug(safepoint)("Waiting for %d thread(s) to block", _waiting_to_block);
346 if (!SafepointTimeout || timeout_error_printed) {
347 Safepoint_lock->wait(true); // true, means with no safepoint checks
348 } else {
349 // Compute remaining time
350 jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
351
352 // If there is no remaining time, then there is an error
353 if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
354 print_safepoint_timeout(_blocking_timeout);
355 }
356 }
357 }
358 assert(_waiting_to_block == 0, "sanity check");
359
360 #ifndef PRODUCT
361 if (SafepointTimeout) {
362 jlong current_time = os::javaTimeNanos();
363 if (safepoint_limit_time < current_time) {
364 tty->print_cr("# SafepointSynchronize: Finished after "
365 INT64_FORMAT_W(6) " ms",
366 ((current_time - safepoint_limit_time) / MICROUNITS +
367 (jlong)SafepointTimeoutDelay));
368 }
369 }
370 #endif
371
372 assert((_safepoint_counter & 0x1) == 0, "must be even");
373 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
374 _safepoint_counter ++;
375
376 // Record state
377 _state = _synchronized;
378
379 OrderAccess::fence();
380
381 if (wait_blocked_event.should_commit()) {
382 wait_blocked_event.set_safepointId(safepoint_counter());
383 wait_blocked_event.set_runningThreadCount(initial_waiting_to_block);
384 wait_blocked_event.commit();
385 }
386 } // EventSafepointWaitBlocked
387
388 #ifdef ASSERT
389 for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
390 // make sure all the threads were visited
391 assert(cur->was_visited_for_critical_count(), "missed a thread");
392 }
393 #endif // ASSERT
394
395 // Update the count of active JNI critical regions
396 GCLocker::set_jni_lock_count(_current_jni_active_count);
397
398 if (log_is_enabled(Debug, safepoint)) {
399 log_debug(safepoint)("Entering safepoint region: %s", VMThread::vm_safepoint_description());
400 }
401
402 RuntimeService::record_safepoint_synchronized();
403 if (PrintSafepointStatistics) {
404 update_statistics_on_sync_end(os::javaTimeNanos());
405 }
406
407 // Call stuff that needs to be run when a safepoint is just about to be completed
408 {
409 EventSafepointCleanup cleanup_event;
410 do_cleanup_tasks();
411 if (cleanup_event.should_commit()) {
412 cleanup_event.set_safepointId(safepoint_counter());
413 cleanup_event.commit();
414 }
415 }
416
417 if (PrintSafepointStatistics) {
418 // Record how much time spend on the above cleanup tasks
419 update_statistics_on_cleanup_end(os::javaTimeNanos());
420 }
421 if (begin_event.should_commit()) {
422 begin_event.set_safepointId(safepoint_counter());
423 begin_event.set_totalThreadCount(nof_threads);
424 begin_event.set_jniCriticalThreadCount(_current_jni_active_count);
425 begin_event.commit();
426 }
427 }
428
429 // Wake up all threads, so they are ready to resume execution after the safepoint
430 // operation has been carried out
431 void SafepointSynchronize::end() {
432 EventSafepointEnd event;
433 int safepoint_id = safepoint_counter(); // Keep the odd counter as "id"
434
435 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
436 assert((_safepoint_counter & 0x1) == 1, "must be odd");
437 _safepoint_counter ++;
438 // memory fence isn't required here since an odd _safepoint_counter
439 // value can do no harm and a fence is issued below anyway.
440
441 DEBUG_ONLY(Thread* myThread = Thread::current();)
442 assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
443
444 if (PrintSafepointStatistics) {
445 end_statistics(os::javaTimeNanos());
446 }
447
448 #ifdef ASSERT
449 // A pending_exception cannot be installed during a safepoint. The threads
450 // may install an async exception after they come back from a safepoint into
451 // pending_exception after they unblock. But that should happen later.
452 for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
501
502 RuntimeService::record_safepoint_end();
503
504 // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
505 // blocked in signal_thread_blocked
506 Threads_lock->unlock();
507
508 }
509 #if INCLUDE_ALL_GCS
510 // If there are any concurrent GC threads resume them.
511 if (UseConcMarkSweepGC) {
512 ConcurrentMarkSweepThread::desynchronize(false);
513 } else if (UseG1GC) {
514 SuspendibleThreadSet::desynchronize();
515 }
516 #endif // INCLUDE_ALL_GCS
517 // record this time so VMThread can keep track how much time has elapsed
518 // since last safepoint.
519 _end_of_last_safepoint = os::javaTimeMillis();
520
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 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
|
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"
56 #include "runtime/thread.inline.hpp"
57 #include "runtime/timerTrace.hpp"
58 #include "services/runtimeService.hpp"
59 #include "utilities/events.hpp"
60 #include "utilities/macros.hpp"
61 #if INCLUDE_ALL_GCS
62 #include "gc/cms/concurrentMarkSweepThread.hpp"
63 #include "gc/g1/suspendibleThreadSet.hpp"
64 #endif // INCLUDE_ALL_GCS
65 #ifdef COMPILER1
66 #include "c1/c1_globals.hpp"
67 #endif
68 #if INCLUDE_TRACE
69 #include "trace/tracing.hpp"
70
71 static void post_safepoint_begin_event(EventSafepointBegin* event,
72 int safepoint_id,
73 int thread_count,
74 int critical_thread_count) {
75 assert(event != NULL, "invariant");
76 if (event->should_commit()) {
77 event->set_safepointId(safepoint_id);
78 event->set_totalThreadCount(thread_count);
79 event->set_jniCriticalThreadCount(critical_thread_count);
80 event->commit();
81 }
82 }
83
84 static void post_safepoint_cleanup_event(EventSafepointCleanup* event,
85 int safepoint_id) {
86 assert(event != NULL, "invariant");
87 if (event->should_commit()) {
88 event->set_safepointId(safepoint_id);
89 event->commit();
90 }
91 }
92
93 static void post_safepoint_synchronize_event(EventSafepointStateSynchronization* event,
94 int safepoint_id,
95 int initial_number_of_threads,
96 int threads_waiting_to_block,
97 unsigned int iterations) {
98 assert(event != NULL, "invariant");
99 if (event->should_commit()) {
100 event->set_safepointId(safepoint_id);
101 event->set_initialThreadCount(initial_number_of_threads);
102 event->set_runningThreadCount(threads_waiting_to_block);
103 event->set_iterations(iterations);
104 event->commit();
105 }
106 }
107
108 static void post_safepoint_wait_blocked_event(EventSafepointWaitBlocked* event,
109 int safepoint_id,
110 int initial_threads_waiting_to_block) {
111 assert(event != NULL, "invariant");
112 if (event->should_commit()) {
113 event->set_safepointId(safepoint_id);
114 event->set_runningThreadCount(initial_threads_waiting_to_block);
115 event->commit();
116 }
117 }
118
119 static void post_safepoint_end_event(EventSafepointEnd* event, int safepoint_id) {
120 assert(event != NULL, "invariant");
121 if (event->should_commit()) {
122 event->set_safepointId(safepoint_id);
123 event->commit();
124 }
125 }
126
127 static void post_safepoint_cleanup_task_event(EventSafepointCleanupTask* event,
128 int safepoint_id,
129 const char* name) {
130 assert(event != NULL, "invariant");
131 if (event->should_commit()) {
132 event->set_safepointId(safepoint_id);
133 event->set_name(name);
134 event->commit();
135 }
136 }
137 #endif // INCLUDE_TRACE
138
139 // --------------------------------------------------------------------------------------------------
140 // Implementation of Safepoint begin/end
141
142 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
143 volatile int SafepointSynchronize::_waiting_to_block = 0;
144 volatile int SafepointSynchronize::_safepoint_counter = 0;
145 int SafepointSynchronize::_current_jni_active_count = 0;
146 long SafepointSynchronize::_end_of_last_safepoint = 0;
147 static volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE
148 static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only
149 static bool timeout_error_printed = false;
150
151 // Roll all threads forward to a safepoint and suspend them all
152 void SafepointSynchronize::begin() {
153 TRACE_ONLY(EventSafepointBegin begin_event;)
154 Thread* myThread = Thread::current();
155 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
156
157 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
158 _safepoint_begin_time = os::javaTimeNanos();
159 _ts_of_current_safepoint = tty->time_stamp().seconds();
160 }
161
162 #if INCLUDE_ALL_GCS
163 if (UseConcMarkSweepGC) {
164 // In the future we should investigate whether CMS can use the
165 // more-general mechanism below. DLD (01/05).
166 ConcurrentMarkSweepThread::synchronize(false);
167 } else if (UseG1GC) {
168 SuspendibleThreadSet::synchronize();
169 }
170 #endif // INCLUDE_ALL_GCS
171
172 // By getting the Threads_lock, we assure that no threads are about to start or
173 // exit. It is released again in SafepointSynchronize::end().
224 // a memory barrier for each Java thread making native calls, each Java
225 // thread performs a write to a single memory page after changing
226 // the thread state. The VM thread performs a sequence of
227 // mprotect OS calls which forces all previous writes from all
228 // Java threads to be serialized. This is done in the
229 // os::serialize_thread_states() call. This has proven to be
230 // much more efficient than executing a membar instruction
231 // on every call to native code.
232 // 3. Running compiled Code
233 // Compiled code reads a global (Safepoint Polling) page that
234 // is set to fault if we are trying to get to a safepoint.
235 // 4. Blocked
236 // A thread which is blocked will not be allowed to return from the
237 // block condition until the safepoint operation is complete.
238 // 5. In VM or Transitioning between states
239 // If a Java thread is currently running in the VM or transitioning
240 // between states, the safepointing code will wait for the thread to
241 // block itself when it attempts transitions to a new state.
242 //
243 {
244 TRACE_ONLY(EventSafepointStateSynchronization sync_event;)
245 int initial_running = 0;
246
247 _state = _synchronizing;
248 OrderAccess::fence();
249
250 // Flush all thread states to memory
251 if (!UseMembar) {
252 os::serialize_thread_states();
253 }
254
255 // Make interpreter safepoint aware
256 Interpreter::notice_safepoints();
257
258 if (DeferPollingPageLoopCount < 0) {
259 // Make polling safepoint aware
260 guarantee (PageArmed == 0, "invariant") ;
261 PageArmed = 1 ;
262 os::make_polling_page_unreadable();
263 }
264
378 ++steps ;
379 if (ncpus > 1 && steps < SafepointSpinBeforeYield) {
380 SpinPause() ; // MP-Polite spin
381 } else
382 if (steps < DeferThrSuspendLoopCount) {
383 os::naked_yield() ;
384 } else {
385 os::naked_short_sleep(1);
386 }
387
388 iterations ++ ;
389 }
390 assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
391 }
392 assert(still_running == 0, "sanity check");
393
394 if (PrintSafepointStatistics) {
395 update_statistics_on_spin_end();
396 }
397
398 TRACE_ONLY(post_safepoint_synchronize_event(&sync_event,
399 safepoint_counter(),
400 initial_running,
401 _waiting_to_block,
402 iterations);)
403 }
404
405 // wait until all threads are stopped
406 {
407 TRACE_ONLY(EventSafepointWaitBlocked wait_blocked_event;)
408 int initial_waiting_to_block = _waiting_to_block;
409
410 while (_waiting_to_block > 0) {
411 log_debug(safepoint)("Waiting for %d thread(s) to block", _waiting_to_block);
412 if (!SafepointTimeout || timeout_error_printed) {
413 Safepoint_lock->wait(true); // true, means with no safepoint checks
414 } else {
415 // Compute remaining time
416 jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
417
418 // If there is no remaining time, then there is an error
419 if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
420 print_safepoint_timeout(_blocking_timeout);
421 }
422 }
423 }
424 assert(_waiting_to_block == 0, "sanity check");
425
426 #ifndef PRODUCT
427 if (SafepointTimeout) {
428 jlong current_time = os::javaTimeNanos();
429 if (safepoint_limit_time < current_time) {
430 tty->print_cr("# SafepointSynchronize: Finished after "
431 INT64_FORMAT_W(6) " ms",
432 ((current_time - safepoint_limit_time) / MICROUNITS +
433 (jlong)SafepointTimeoutDelay));
434 }
435 }
436 #endif
437
438 assert((_safepoint_counter & 0x1) == 0, "must be even");
439 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
440 _safepoint_counter ++;
441
442 // Record state
443 _state = _synchronized;
444
445 OrderAccess::fence();
446
447 TRACE_ONLY(post_safepoint_wait_blocked_event(&wait_blocked_event,
448 safepoint_counter(),
449 initial_waiting_to_block);)
450 }
451
452 #ifdef ASSERT
453 for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
454 // make sure all the threads were visited
455 assert(cur->was_visited_for_critical_count(), "missed a thread");
456 }
457 #endif // ASSERT
458
459 // Update the count of active JNI critical regions
460 GCLocker::set_jni_lock_count(_current_jni_active_count);
461
462 if (log_is_enabled(Debug, safepoint)) {
463 log_debug(safepoint)("Entering safepoint region: %s", VMThread::vm_safepoint_description());
464 }
465
466 RuntimeService::record_safepoint_synchronized();
467 if (PrintSafepointStatistics) {
468 update_statistics_on_sync_end(os::javaTimeNanos());
469 }
470
471 // Call stuff that needs to be run when a safepoint is just about to be completed
472 {
473 TRACE_ONLY(EventSafepointCleanup cleanup_event;)
474 do_cleanup_tasks();
475 TRACE_ONLY(post_safepoint_cleanup_event(&cleanup_event, safepoint_counter());)
476 }
477
478 if (PrintSafepointStatistics) {
479 // Record how much time spend on the above cleanup tasks
480 update_statistics_on_cleanup_end(os::javaTimeNanos());
481 }
482 TRACE_ONLY(post_safepoint_begin_event(&begin_event, safepoint_counter(),
483 nof_threads, _current_jni_active_count);)
484 }
485
486 // Wake up all threads, so they are ready to resume execution after the safepoint
487 // operation has been carried out
488 void SafepointSynchronize::end() {
489 TRACE_ONLY(EventSafepointEnd event;)
490 int safepoint_id = safepoint_counter(); // Keep the odd counter as "id"
491
492 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
493 assert((_safepoint_counter & 0x1) == 1, "must be odd");
494 _safepoint_counter ++;
495 // memory fence isn't required here since an odd _safepoint_counter
496 // value can do no harm and a fence is issued below anyway.
497
498 DEBUG_ONLY(Thread* myThread = Thread::current();)
499 assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
500
501 if (PrintSafepointStatistics) {
502 end_statistics(os::javaTimeNanos());
503 }
504
505 #ifdef ASSERT
506 // A pending_exception cannot be installed during a safepoint. The threads
507 // may install an async exception after they come back from a safepoint into
508 // pending_exception after they unblock. But that should happen later.
509 for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
558
559 RuntimeService::record_safepoint_end();
560
561 // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
562 // blocked in signal_thread_blocked
563 Threads_lock->unlock();
564
565 }
566 #if INCLUDE_ALL_GCS
567 // If there are any concurrent GC threads resume them.
568 if (UseConcMarkSweepGC) {
569 ConcurrentMarkSweepThread::desynchronize(false);
570 } else if (UseG1GC) {
571 SuspendibleThreadSet::desynchronize();
572 }
573 #endif // INCLUDE_ALL_GCS
574 // record this time so VMThread can keep track how much time has elapsed
575 // since last safepoint.
576 _end_of_last_safepoint = os::javaTimeMillis();
577
578 TRACE_ONLY(post_safepoint_end_event(&event, safepoint_id);)
579 }
580
581 bool SafepointSynchronize::is_cleanup_needed() {
582 // Need a safepoint if there are many monitors to deflate.
583 if (ObjectSynchronizer::is_cleanup_needed()) return true;
584 // Need a safepoint if some inline cache buffers is non-empty
585 if (!InlineCacheBuffer::is_empty()) return true;
586 return false;
587 }
588
589 // Various cleaning tasks that should be done periodically at safepoints
590 void SafepointSynchronize::do_cleanup_tasks() {
591 {
592 const char* name = "deflating idle monitors";
593 TRACE_ONLY(EventSafepointCleanupTask event;)
594 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
595 ObjectSynchronizer::deflate_idle_monitors();
596 TRACE_ONLY(post_safepoint_cleanup_task_event(&event, safepoint_counter(), name);)
597 }
598
599 {
600 const char* name = "updating inline caches";
601 TRACE_ONLY(EventSafepointCleanupTask event;)
602 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
603 InlineCacheBuffer::update_inline_caches();
604 TRACE_ONLY(post_safepoint_cleanup_task_event(&event, safepoint_counter(), name);)
605 }
606 {
607 const char* name = "compilation policy safepoint handler";
608 TRACE_ONLY(EventSafepointCleanupTask event;)
609 TraceTime timer("compilation policy safepoint handler", TRACETIME_LOG(Info, safepoint, cleanup));
610 CompilationPolicy::policy()->do_safepoint_work();
611 TRACE_ONLY(post_safepoint_cleanup_task_event(&event, safepoint_counter(), name);)
612 }
613
614 {
615 const char* name = "mark nmethods";
616 TRACE_ONLY(EventSafepointCleanupTask event;)
617 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
618 NMethodSweeper::mark_active_nmethods();
619 TRACE_ONLY(post_safepoint_cleanup_task_event(&event, safepoint_counter(), name);)
620 }
621
622 if (SymbolTable::needs_rehashing()) {
623 const char* name = "rehashing symbol table";
624 TRACE_ONLY(EventSafepointCleanupTask event;)
625 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
626 SymbolTable::rehash_table();
627 TRACE_ONLY(post_safepoint_cleanup_task_event(&event, safepoint_counter(), name);)
628 }
629
630 if (StringTable::needs_rehashing()) {
631 const char* name = "rehashing string table";
632 TRACE_ONLY(EventSafepointCleanupTask event;)
633 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
634 StringTable::rehash_table();
635 TRACE_ONLY(post_safepoint_cleanup_task_event(&event, safepoint_counter(), name);)
636 }
637
638 {
639 // CMS delays purging the CLDG until the beginning of the next safepoint and to
640 // make sure concurrent sweep is done
641 const char* name = "purging class loader data graph";
642 TRACE_ONLY(EventSafepointCleanupTask event;)
643 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
644 ClassLoaderDataGraph::purge_if_needed();
645 TRACE_ONLY(post_safepoint_cleanup_task_event(&event, safepoint_counter(), name);)
646 }
647 }
648
649
650 bool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
651 switch(state) {
652 case _thread_in_native:
653 // native threads are safe if they have no java stack or have walkable stack
654 return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
655
656 // blocked threads should have already have walkable stack
657 case _thread_blocked:
658 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
659 return true;
660
661 default:
662 return false;
663 }
664 }
665
|