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