355 }
356 ThreadSnapshot* ts = snapshot_thread(jt, tcl);
357 _result->add_thread_snapshot(ts);
358 }
359 }
360 }
361
362 ThreadSnapshot* VM_ThreadDump::snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl) {
363 ThreadSnapshot* snapshot = new ThreadSnapshot(java_thread);
364 snapshot->dump_stack_at_safepoint(_max_depth, _with_locked_monitors);
365 snapshot->set_concurrent_locks(tcl);
366 return snapshot;
367 }
368
369 volatile bool VM_Exit::_vm_exited = false;
370 Thread * VM_Exit::_shutdown_thread = NULL;
371
372 int VM_Exit::set_vm_exited() {
373 CodeCacheExtensions::complete_step(CodeCacheExtensionsSteps::LastStep);
374
375 Thread * thr_cur = ThreadLocalStorage::get_thread_slow();
376
377 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already");
378
379 int num_active = 0;
380
381 _shutdown_thread = thr_cur;
382 _vm_exited = true; // global flag
383 for(JavaThread *thr = Threads::first(); thr != NULL; thr = thr->next())
384 if (thr!=thr_cur && thr->thread_state() == _thread_in_native) {
385 ++num_active;
386 thr->set_terminated(JavaThread::_vm_exited); // per-thread flag
387 }
388
389 return num_active;
390 }
391
392 int VM_Exit::wait_for_threads_in_native_to_block() {
393 // VM exits at safepoint. This function must be called at the final safepoint
394 // to wait for threads in _thread_in_native state to be quiescent.
395 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already");
396
397 Thread * thr_cur = ThreadLocalStorage::get_thread_slow();
398 Monitor timer(Mutex::leaf, "VM_Exit timer", true,
399 Monitor::_safepoint_check_never);
400
401 // Compiler threads need longer wait because they can access VM data directly
402 // while in native. If they are active and some structures being used are
403 // deleted by the shutdown sequence, they will crash. On the other hand, user
404 // threads must go through native=>Java/VM transitions first to access VM
405 // data, and they will be stopped during state transition. In theory, we
406 // don't have to wait for user threads to be quiescent, but it's always
407 // better to terminate VM when current thread is the only active thread, so
408 // wait for user threads too. Numbers are in 10 milliseconds.
409 int max_wait_user_thread = 30; // at least 300 milliseconds
410 int max_wait_compiler_thread = 1000; // at least 10 seconds
411
412 int max_wait = max_wait_compiler_thread;
413
414 int attempts = 0;
415 while (true) {
416 int num_active = 0;
417 int num_active_compiler_thread = 0;
454
455 // cleanup globals resources before exiting. exit_globals() currently
456 // cleans up outputStream resources and PerfMemory resources.
457 exit_globals();
458
459 // Check for exit hook
460 exit_hook_t exit_hook = Arguments::exit_hook();
461 if (exit_hook != NULL) {
462 // exit hook should exit.
463 exit_hook(_exit_code);
464 // ... but if it didn't, we must do it here
465 vm_direct_exit(_exit_code);
466 } else {
467 vm_direct_exit(_exit_code);
468 }
469 }
470
471
472 void VM_Exit::wait_if_vm_exited() {
473 if (_vm_exited &&
474 ThreadLocalStorage::get_thread_slow() != _shutdown_thread) {
475 // _vm_exited is set at safepoint, and the Threads_lock is never released
476 // we will block here until the process dies
477 Threads_lock->lock_without_safepoint_check();
478 ShouldNotReachHere();
479 }
480 }
481
482 void VM_PrintCompileQueue::doit() {
483 CompileBroker::print_compile_queues(_out);
484 }
485
486 void VM_PrintCodeList::doit() {
487 CodeCache::print_codelist(_out);
488 }
489
490 void VM_PrintCodeCache::doit() {
491 CodeCache::print_layout(_out);
492 }
493
494 #if INCLUDE_SERVICES
|
355 }
356 ThreadSnapshot* ts = snapshot_thread(jt, tcl);
357 _result->add_thread_snapshot(ts);
358 }
359 }
360 }
361
362 ThreadSnapshot* VM_ThreadDump::snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl) {
363 ThreadSnapshot* snapshot = new ThreadSnapshot(java_thread);
364 snapshot->dump_stack_at_safepoint(_max_depth, _with_locked_monitors);
365 snapshot->set_concurrent_locks(tcl);
366 return snapshot;
367 }
368
369 volatile bool VM_Exit::_vm_exited = false;
370 Thread * VM_Exit::_shutdown_thread = NULL;
371
372 int VM_Exit::set_vm_exited() {
373 CodeCacheExtensions::complete_step(CodeCacheExtensionsSteps::LastStep);
374
375 Thread * thr_cur = Thread::current_or_null();
376
377 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already");
378
379 int num_active = 0;
380
381 _shutdown_thread = thr_cur;
382 _vm_exited = true; // global flag
383 for(JavaThread *thr = Threads::first(); thr != NULL; thr = thr->next())
384 if (thr!=thr_cur && thr->thread_state() == _thread_in_native) {
385 ++num_active;
386 thr->set_terminated(JavaThread::_vm_exited); // per-thread flag
387 }
388
389 return num_active;
390 }
391
392 int VM_Exit::wait_for_threads_in_native_to_block() {
393 // VM exits at safepoint. This function must be called at the final safepoint
394 // to wait for threads in _thread_in_native state to be quiescent.
395 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already");
396
397 Thread * thr_cur = Thread::current();
398 Monitor timer(Mutex::leaf, "VM_Exit timer", true,
399 Monitor::_safepoint_check_never);
400
401 // Compiler threads need longer wait because they can access VM data directly
402 // while in native. If they are active and some structures being used are
403 // deleted by the shutdown sequence, they will crash. On the other hand, user
404 // threads must go through native=>Java/VM transitions first to access VM
405 // data, and they will be stopped during state transition. In theory, we
406 // don't have to wait for user threads to be quiescent, but it's always
407 // better to terminate VM when current thread is the only active thread, so
408 // wait for user threads too. Numbers are in 10 milliseconds.
409 int max_wait_user_thread = 30; // at least 300 milliseconds
410 int max_wait_compiler_thread = 1000; // at least 10 seconds
411
412 int max_wait = max_wait_compiler_thread;
413
414 int attempts = 0;
415 while (true) {
416 int num_active = 0;
417 int num_active_compiler_thread = 0;
454
455 // cleanup globals resources before exiting. exit_globals() currently
456 // cleans up outputStream resources and PerfMemory resources.
457 exit_globals();
458
459 // Check for exit hook
460 exit_hook_t exit_hook = Arguments::exit_hook();
461 if (exit_hook != NULL) {
462 // exit hook should exit.
463 exit_hook(_exit_code);
464 // ... but if it didn't, we must do it here
465 vm_direct_exit(_exit_code);
466 } else {
467 vm_direct_exit(_exit_code);
468 }
469 }
470
471
472 void VM_Exit::wait_if_vm_exited() {
473 if (_vm_exited &&
474 Thread::current_or_null() != _shutdown_thread) {
475 // _vm_exited is set at safepoint, and the Threads_lock is never released
476 // we will block here until the process dies
477 Threads_lock->lock_without_safepoint_check();
478 ShouldNotReachHere();
479 }
480 }
481
482 void VM_PrintCompileQueue::doit() {
483 CompileBroker::print_compile_queues(_out);
484 }
485
486 void VM_PrintCodeList::doit() {
487 CodeCache::print_codelist(_out);
488 }
489
490 void VM_PrintCodeCache::doit() {
491 CodeCache::print_layout(_out);
492 }
493
494 #if INCLUDE_SERVICES
|