41 #include "logging/logStream.hpp"
42 #include "memory/resourceArea.hpp"
43 #include "memory/universe.inline.hpp"
44 #include "oops/oop.inline.hpp"
45 #include "oops/symbol.hpp"
46 #include "runtime/atomic.hpp"
47 #include "runtime/compilationPolicy.hpp"
48 #include "runtime/deoptimization.hpp"
49 #include "runtime/frame.inline.hpp"
50 #include "runtime/interfaceSupport.hpp"
51 #include "runtime/mutexLocker.hpp"
52 #include "runtime/orderAccess.inline.hpp"
53 #include "runtime/osThread.hpp"
54 #include "runtime/safepoint.hpp"
55 #include "runtime/signature.hpp"
56 #include "runtime/stubCodeGenerator.hpp"
57 #include "runtime/stubRoutines.hpp"
58 #include "runtime/sweeper.hpp"
59 #include "runtime/synchronizer.hpp"
60 #include "runtime/thread.inline.hpp"
61 #include "runtime/timerTrace.hpp"
62 #include "services/runtimeService.hpp"
63 #include "trace/tracing.hpp"
64 #include "trace/traceMacros.hpp"
65 #include "utilities/events.hpp"
66 #include "utilities/macros.hpp"
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
171 _state = _synchronizing;
172 OrderAccess::fence();
173
174 // Flush all thread states to memory
175 if (!UseMembar) {
176 os::serialize_thread_states();
177 }
178
179 // Make interpreter safepoint aware
180 Interpreter::notice_safepoints();
181
182 if (DeferPollingPageLoopCount < 0) {
183 // Make polling safepoint aware
184 guarantee (PageArmed == 0, "invariant") ;
185 PageArmed = 1 ;
186 os::make_polling_page_unreadable();
187 }
188
189 // Consider using active_processor_count() ... but that call is expensive.
190 int ncpus = os::processor_count() ;
191
192 #ifdef ASSERT
193 for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
194 assert(cur->safepoint_state()->is_running(), "Illegal initial state");
195 // Clear the visited flag to ensure that the critical counts are collected properly.
196 cur->set_visited_for_critical_count(false);
197 }
198 #endif // ASSERT
199
200 if (SafepointTimeout)
201 safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
202
203 // Iterate through all threads until it have been determined how to stop them all at a safepoint
204 unsigned int iterations = 0;
205 int steps = 0 ;
206 while(still_running > 0) {
207 for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
208 assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended");
209 ThreadSafepointState *cur_state = cur->safepoint_state();
210 if (cur_state->is_running()) {
211 cur_state->examine_state_of_thread();
212 if (!cur_state->is_running()) {
213 still_running--;
214 // consider adjusting steps downward:
215 // steps = 0
216 // steps -= NNN
217 // steps >>= 1
218 // steps = MIN(steps, 2000-100)
219 // if (iterations != 0) steps -= NNN
220 }
221 LogTarget(Trace, safepoint) lt;
222 if (lt.is_enabled()) {
223 ResourceMark rm;
224 LogStream ls(lt);
225 cur_state->print_on(&ls);
226 }
227 }
298 PageArmed = 1 ;
299 os::make_polling_page_unreadable();
300 }
301
302 // Instead of (ncpus > 1) consider either (still_running < (ncpus + EPSILON)) or
303 // ((still_running + _waiting_to_block - TryingToBlock)) < ncpus)
304 ++steps ;
305 if (ncpus > 1 && steps < SafepointSpinBeforeYield) {
306 SpinPause() ; // MP-Polite spin
307 } else
308 if (steps < DeferThrSuspendLoopCount) {
309 os::naked_yield() ;
310 } else {
311 os::naked_short_sleep(1);
312 }
313
314 iterations ++ ;
315 }
316 assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
317 }
318 assert(still_running == 0, "sanity check");
319
320 if (PrintSafepointStatistics) {
321 update_statistics_on_spin_end();
322 }
323
324 if (sync_event.should_commit()) {
325 // Group this event together with the ones committed after the counter is increased
326 sync_event.set_safepointId(safepoint_counter() + 1);
327 sync_event.set_initialThreadCount(initial_running);
328 sync_event.set_runningThreadCount(_waiting_to_block);
329 sync_event.set_iterations(iterations);
330 sync_event.commit();
331 }
332 } //EventSafepointStateSync
333
334 // wait until all threads are stopped
335 {
336 EventSafepointWaitBlocked wait_blocked_event;
337 int initial_waiting_to_block = _waiting_to_block;
338
339 while (_waiting_to_block > 0) {
340 log_debug(safepoint)("Waiting for %d thread(s) to block", _waiting_to_block);
341 if (!SafepointTimeout || timeout_error_printed) {
342 Safepoint_lock->wait(true); // true, means with no safepoint checks
343 } else {
344 // Compute remaining time
345 jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
346
347 // If there is no remaining time, then there is an error
348 if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
349 print_safepoint_timeout(_blocking_timeout);
350 }
351 }
352 }
364 }
365 #endif
366
367 assert((_safepoint_counter & 0x1) == 0, "must be even");
368 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
369 _safepoint_counter ++;
370
371 // Record state
372 _state = _synchronized;
373
374 OrderAccess::fence();
375
376 if (wait_blocked_event.should_commit()) {
377 wait_blocked_event.set_safepointId(safepoint_counter());
378 wait_blocked_event.set_runningThreadCount(initial_waiting_to_block);
379 wait_blocked_event.commit();
380 }
381 } // EventSafepointWaitBlocked
382
383 #ifdef ASSERT
384 for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
385 // make sure all the threads were visited
386 assert(cur->was_visited_for_critical_count(), "missed a thread");
387 }
388 #endif // ASSERT
389
390 // Update the count of active JNI critical regions
391 GCLocker::set_jni_lock_count(_current_jni_active_count);
392
393 if (log_is_enabled(Debug, safepoint)) {
394 log_debug(safepoint)("Entering safepoint region: %s", VMThread::vm_safepoint_description());
395 }
396
397 RuntimeService::record_safepoint_synchronized();
398 if (PrintSafepointStatistics) {
399 update_statistics_on_sync_end(os::javaTimeNanos());
400 }
401
402 // Call stuff that needs to be run when a safepoint is just about to be completed
403 {
404 EventSafepointCleanup cleanup_event;
405 do_cleanup_tasks();
423
424 // Wake up all threads, so they are ready to resume execution after the safepoint
425 // operation has been carried out
426 void SafepointSynchronize::end() {
427 EventSafepointEnd event;
428 int safepoint_id = safepoint_counter(); // Keep the odd counter as "id"
429
430 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
431 assert((_safepoint_counter & 0x1) == 1, "must be odd");
432 _safepoint_counter ++;
433 // memory fence isn't required here since an odd _safepoint_counter
434 // value can do no harm and a fence is issued below anyway.
435
436 DEBUG_ONLY(Thread* myThread = Thread::current();)
437 assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
438
439 if (PrintSafepointStatistics) {
440 end_statistics(os::javaTimeNanos());
441 }
442
443 #ifdef ASSERT
444 // A pending_exception cannot be installed during a safepoint. The threads
445 // may install an async exception after they come back from a safepoint into
446 // pending_exception after they unblock. But that should happen later.
447 for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
448 assert (!(cur->has_pending_exception() &&
449 cur->safepoint_state()->is_at_poll_safepoint()),
450 "safepoint installed a pending exception");
451 }
452 #endif // ASSERT
453
454 if (PageArmed) {
455 // Make polling safepoint aware
456 os::make_polling_page_readable();
457 PageArmed = 0 ;
458 }
459
460 // Remove safepoint check from interpreter
461 Interpreter::ignore_safepoints();
462
463 {
464 MutexLocker mu(Safepoint_lock);
465
466 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
467
468 // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
469 // when they get restarted.
470 _state = _not_synchronized;
471 OrderAccess::fence();
472
473 log_debug(safepoint)("Leaving safepoint region");
474
475 // Start suspended threads
476 for(JavaThread *current = Threads::first(); current; current = current->next()) {
477 // A problem occurring on Solaris is when attempting to restart threads
478 // the first #cpus - 1 go well, but then the VMThread is preempted when we get
479 // to the next one (since it has been running the longest). We then have
480 // to wait for a cpu to become available before we can continue restarting
481 // threads.
482 // FIXME: This causes the performance of the VM to degrade when active and with
483 // large numbers of threads. Apparently this is due to the synchronous nature
484 // of suspending threads.
485 //
486 // TODO-FIXME: the comments above are vestigial and no longer apply.
487 // Furthermore, using solaris' schedctl in this particular context confers no benefit
488 if (VMThreadHintNoPreempt) {
489 os::hint_no_preempt();
490 }
491 ThreadSafepointState* cur_state = current->safepoint_state();
492 assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
493 cur_state->restart();
494 assert(cur_state->is_running(), "safepoint state has not been reset");
495 }
496
497 RuntimeService::record_safepoint_end();
498
499 // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
500 // blocked in signal_thread_blocked
501 Threads_lock->unlock();
502
503 }
504 Universe::heap()->safepoint_synchronize_end();
505 // record this time so VMThread can keep track how much time has elapsed
506 // since last safepoint.
507 _end_of_last_safepoint = os::javaTimeMillis();
508
509 if (event.should_commit()) {
510 event.set_safepointId(safepoint_id);
511 event.commit();
512 }
513 }
514
515 bool SafepointSynchronize::is_cleanup_needed() {
516 // Need a safepoint if there are many monitors to deflate.
517 if (ObjectSynchronizer::is_cleanup_needed()) return true;
518 // Need a safepoint if some inline cache buffers is non-empty
519 if (!InlineCacheBuffer::is_empty()) return true;
520 return false;
521 }
522
523 static void event_safepoint_cleanup_task_commit(EventSafepointCleanupTask& event, const char* name) {
870 state->handle_polling_page_exception();
871 }
872
873
874 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
875 if (!timeout_error_printed) {
876 timeout_error_printed = true;
877 // Print out the thread info which didn't reach the safepoint for debugging
878 // purposes (useful when there are lots of threads in the debugger).
879 tty->cr();
880 tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
881 if (reason == _spinning_timeout) {
882 tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
883 } else if (reason == _blocking_timeout) {
884 tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
885 }
886
887 tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
888 ThreadSafepointState *cur_state;
889 ResourceMark rm;
890 for(JavaThread *cur_thread = Threads::first(); cur_thread;
891 cur_thread = cur_thread->next()) {
892 cur_state = cur_thread->safepoint_state();
893
894 if (cur_thread->thread_state() != _thread_blocked &&
895 ((reason == _spinning_timeout && cur_state->is_running()) ||
896 (reason == _blocking_timeout && !cur_state->has_called_back()))) {
897 tty->print("# ");
898 cur_thread->print();
899 tty->cr();
900 }
901 }
902 tty->print_cr("# SafepointSynchronize::begin: (End of list)");
903 }
904
905 // To debug the long safepoint, specify both DieOnSafepointTimeout &
906 // ShowMessageBoxOnError.
907 if (DieOnSafepointTimeout) {
908 fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
909 SafepointTimeoutDelay, VMThread::vm_safepoint_description());
910 }
911 }
1381 _coalesced_vmop_count);
1382 tty->print_cr("Maximum sync time " INT64_FORMAT_W(5) " ms",
1383 (int64_t)(_max_sync_time / MICROUNITS));
1384 tty->print_cr("Maximum vm operation time (except for Exit VM operation) "
1385 INT64_FORMAT_W(5) " ms",
1386 (int64_t)(_max_vmop_time / MICROUNITS));
1387 }
1388
1389 // ------------------------------------------------------------------------------------------------
1390 // Non-product code
1391
1392 #ifndef PRODUCT
1393
1394 void SafepointSynchronize::print_state() {
1395 if (_state == _not_synchronized) {
1396 tty->print_cr("not synchronized");
1397 } else if (_state == _synchronizing || _state == _synchronized) {
1398 tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
1399 "synchronized");
1400
1401 for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
1402 cur->safepoint_state()->print();
1403 }
1404 }
1405 }
1406
1407 void SafepointSynchronize::safepoint_msg(const char* format, ...) {
1408 if (ShowSafepointMsgs) {
1409 va_list ap;
1410 va_start(ap, format);
1411 tty->vprint_cr(format, ap);
1412 va_end(ap);
1413 }
1414 }
1415
1416 #endif // !PRODUCT
|
41 #include "logging/logStream.hpp"
42 #include "memory/resourceArea.hpp"
43 #include "memory/universe.inline.hpp"
44 #include "oops/oop.inline.hpp"
45 #include "oops/symbol.hpp"
46 #include "runtime/atomic.hpp"
47 #include "runtime/compilationPolicy.hpp"
48 #include "runtime/deoptimization.hpp"
49 #include "runtime/frame.inline.hpp"
50 #include "runtime/interfaceSupport.hpp"
51 #include "runtime/mutexLocker.hpp"
52 #include "runtime/orderAccess.inline.hpp"
53 #include "runtime/osThread.hpp"
54 #include "runtime/safepoint.hpp"
55 #include "runtime/signature.hpp"
56 #include "runtime/stubCodeGenerator.hpp"
57 #include "runtime/stubRoutines.hpp"
58 #include "runtime/sweeper.hpp"
59 #include "runtime/synchronizer.hpp"
60 #include "runtime/thread.inline.hpp"
61 #include "runtime/threadSMR.hpp"
62 #include "runtime/timerTrace.hpp"
63 #include "services/runtimeService.hpp"
64 #include "trace/tracing.hpp"
65 #include "trace/traceMacros.hpp"
66 #include "utilities/events.hpp"
67 #include "utilities/macros.hpp"
68 #ifdef COMPILER1
69 #include "c1/c1_globals.hpp"
70 #endif
71
72 // --------------------------------------------------------------------------------------------------
73 // Implementation of Safepoint begin/end
74
75 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
76 volatile int SafepointSynchronize::_waiting_to_block = 0;
77 volatile int SafepointSynchronize::_safepoint_counter = 0;
78 int SafepointSynchronize::_current_jni_active_count = 0;
79 long SafepointSynchronize::_end_of_last_safepoint = 0;
80 static volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE
81 static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only
172 _state = _synchronizing;
173 OrderAccess::fence();
174
175 // Flush all thread states to memory
176 if (!UseMembar) {
177 os::serialize_thread_states();
178 }
179
180 // Make interpreter safepoint aware
181 Interpreter::notice_safepoints();
182
183 if (DeferPollingPageLoopCount < 0) {
184 // Make polling safepoint aware
185 guarantee (PageArmed == 0, "invariant") ;
186 PageArmed = 1 ;
187 os::make_polling_page_unreadable();
188 }
189
190 // Consider using active_processor_count() ... but that call is expensive.
191 int ncpus = os::processor_count() ;
192 unsigned int iterations = 0;
193
194 {
195 JavaThreadIteratorWithHandle jtiwh;
196 #ifdef ASSERT
197 for (; JavaThread *cur = jtiwh.next(); ) {
198 assert(cur->safepoint_state()->is_running(), "Illegal initial state");
199 // Clear the visited flag to ensure that the critical counts are collected properly.
200 cur->set_visited_for_critical_count(false);
201 }
202 #endif // ASSERT
203
204 if (SafepointTimeout)
205 safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
206
207 // Iterate through all threads until it have been determined how to stop them all at a safepoint
208 int steps = 0 ;
209 while(still_running > 0) {
210 jtiwh.rewind();
211 for (; JavaThread *cur = jtiwh.next(); ) {
212 assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended");
213 ThreadSafepointState *cur_state = cur->safepoint_state();
214 if (cur_state->is_running()) {
215 cur_state->examine_state_of_thread();
216 if (!cur_state->is_running()) {
217 still_running--;
218 // consider adjusting steps downward:
219 // steps = 0
220 // steps -= NNN
221 // steps >>= 1
222 // steps = MIN(steps, 2000-100)
223 // if (iterations != 0) steps -= NNN
224 }
225 LogTarget(Trace, safepoint) lt;
226 if (lt.is_enabled()) {
227 ResourceMark rm;
228 LogStream ls(lt);
229 cur_state->print_on(&ls);
230 }
231 }
302 PageArmed = 1 ;
303 os::make_polling_page_unreadable();
304 }
305
306 // Instead of (ncpus > 1) consider either (still_running < (ncpus + EPSILON)) or
307 // ((still_running + _waiting_to_block - TryingToBlock)) < ncpus)
308 ++steps ;
309 if (ncpus > 1 && steps < SafepointSpinBeforeYield) {
310 SpinPause() ; // MP-Polite spin
311 } else
312 if (steps < DeferThrSuspendLoopCount) {
313 os::naked_yield() ;
314 } else {
315 os::naked_short_sleep(1);
316 }
317
318 iterations ++ ;
319 }
320 assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
321 }
322 } // ThreadsListHandle destroyed here.
323 assert(still_running == 0, "sanity check");
324
325 if (PrintSafepointStatistics) {
326 update_statistics_on_spin_end();
327 }
328
329 if (sync_event.should_commit()) {
330 // Group this event together with the ones committed after the counter is increased
331 sync_event.set_safepointId(safepoint_counter() + 1);
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 } // EventSafepointStateSynchronization destroyed here.
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 }
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 // Make sure all the threads were visited.
390 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
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();
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 {
449 JavaThreadIteratorWithHandle jtiwh;
450 #ifdef ASSERT
451 // A pending_exception cannot be installed during a safepoint. The threads
452 // may install an async exception after they come back from a safepoint into
453 // pending_exception after they unblock. But that should happen later.
454 for (; JavaThread *cur = jtiwh.next(); ) {
455 assert (!(cur->has_pending_exception() &&
456 cur->safepoint_state()->is_at_poll_safepoint()),
457 "safepoint installed a pending exception");
458 }
459 #endif // ASSERT
460
461 if (PageArmed) {
462 // Make polling safepoint aware
463 os::make_polling_page_readable();
464 PageArmed = 0 ;
465 }
466
467 // Remove safepoint check from interpreter
468 Interpreter::ignore_safepoints();
469
470 {
471 MutexLocker mu(Safepoint_lock);
472
473 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
474
475 // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
476 // when they get restarted.
477 _state = _not_synchronized;
478 OrderAccess::fence();
479
480 log_debug(safepoint)("Leaving safepoint region");
481
482 // Start suspended threads
483 jtiwh.rewind();
484 for (; JavaThread *current = jtiwh.next(); ) {
485 // A problem occurring on Solaris is when attempting to restart threads
486 // the first #cpus - 1 go well, but then the VMThread is preempted when we get
487 // to the next one (since it has been running the longest). We then have
488 // to wait for a cpu to become available before we can continue restarting
489 // threads.
490 // FIXME: This causes the performance of the VM to degrade when active and with
491 // large numbers of threads. Apparently this is due to the synchronous nature
492 // of suspending threads.
493 //
494 // TODO-FIXME: the comments above are vestigial and no longer apply.
495 // Furthermore, using solaris' schedctl in this particular context confers no benefit
496 if (VMThreadHintNoPreempt) {
497 os::hint_no_preempt();
498 }
499 ThreadSafepointState* cur_state = current->safepoint_state();
500 assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
501 cur_state->restart();
502 assert(cur_state->is_running(), "safepoint state has not been reset");
503 }
504
505 RuntimeService::record_safepoint_end();
506
507 // Release threads lock, so threads can be created/destroyed again.
508 // It will also release all threads blocked in signal_thread_blocked.
509 Threads_lock->unlock();
510 }
511 } // ThreadsListHandle destroyed here.
512
513 Universe::heap()->safepoint_synchronize_end();
514 // record this time so VMThread can keep track how much time has elapsed
515 // since last safepoint.
516 _end_of_last_safepoint = os::javaTimeMillis();
517
518 if (event.should_commit()) {
519 event.set_safepointId(safepoint_id);
520 event.commit();
521 }
522 }
523
524 bool SafepointSynchronize::is_cleanup_needed() {
525 // Need a safepoint if there are many monitors to deflate.
526 if (ObjectSynchronizer::is_cleanup_needed()) return true;
527 // Need a safepoint if some inline cache buffers is non-empty
528 if (!InlineCacheBuffer::is_empty()) return true;
529 return false;
530 }
531
532 static void event_safepoint_cleanup_task_commit(EventSafepointCleanupTask& event, const char* name) {
879 state->handle_polling_page_exception();
880 }
881
882
883 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
884 if (!timeout_error_printed) {
885 timeout_error_printed = true;
886 // Print out the thread info which didn't reach the safepoint for debugging
887 // purposes (useful when there are lots of threads in the debugger).
888 tty->cr();
889 tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
890 if (reason == _spinning_timeout) {
891 tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
892 } else if (reason == _blocking_timeout) {
893 tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
894 }
895
896 tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
897 ThreadSafepointState *cur_state;
898 ResourceMark rm;
899 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) {
900 cur_state = cur_thread->safepoint_state();
901
902 if (cur_thread->thread_state() != _thread_blocked &&
903 ((reason == _spinning_timeout && cur_state->is_running()) ||
904 (reason == _blocking_timeout && !cur_state->has_called_back()))) {
905 tty->print("# ");
906 cur_thread->print();
907 tty->cr();
908 }
909 }
910 tty->print_cr("# SafepointSynchronize::begin: (End of list)");
911 }
912
913 // To debug the long safepoint, specify both DieOnSafepointTimeout &
914 // ShowMessageBoxOnError.
915 if (DieOnSafepointTimeout) {
916 fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
917 SafepointTimeoutDelay, VMThread::vm_safepoint_description());
918 }
919 }
1389 _coalesced_vmop_count);
1390 tty->print_cr("Maximum sync time " INT64_FORMAT_W(5) " ms",
1391 (int64_t)(_max_sync_time / MICROUNITS));
1392 tty->print_cr("Maximum vm operation time (except for Exit VM operation) "
1393 INT64_FORMAT_W(5) " ms",
1394 (int64_t)(_max_vmop_time / MICROUNITS));
1395 }
1396
1397 // ------------------------------------------------------------------------------------------------
1398 // Non-product code
1399
1400 #ifndef PRODUCT
1401
1402 void SafepointSynchronize::print_state() {
1403 if (_state == _not_synchronized) {
1404 tty->print_cr("not synchronized");
1405 } else if (_state == _synchronizing || _state == _synchronized) {
1406 tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
1407 "synchronized");
1408
1409 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
1410 cur->safepoint_state()->print();
1411 }
1412 }
1413 }
1414
1415 void SafepointSynchronize::safepoint_msg(const char* format, ...) {
1416 if (ShowSafepointMsgs) {
1417 va_list ap;
1418 va_start(ap, format);
1419 tty->vprint_cr(format, ap);
1420 va_end(ap);
1421 }
1422 }
1423
1424 #endif // !PRODUCT
|