3456 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
3457 Prefetch::read((void*)addr, prefetch_interval);
3458 return *addr;
3459 }
3460
3461 // Possibly the ugliest for loop the world has seen. C++ does not allow
3462 // multiple types in the declaration section of the for loop. In this case
3463 // we are only dealing with pointers and hence can cast them. It looks ugly
3464 // but macros are ugly and therefore it's fine to make things absurdly ugly.
3465 #define DO_JAVA_THREADS(LIST, X) \
3466 for (JavaThread *MACRO_scan_interval = (JavaThread*)(uintptr_t)PrefetchScanIntervalInBytes, \
3467 *MACRO_list = (JavaThread*)(LIST), \
3468 **MACRO_end = ((JavaThread**)((ThreadsList*)MACRO_list)->threads()) + ((ThreadsList*)MACRO_list)->length(), \
3469 **MACRO_current_p = (JavaThread**)((ThreadsList*)MACRO_list)->threads(), \
3470 *X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval); \
3471 MACRO_current_p != MACRO_end; \
3472 MACRO_current_p++, \
3473 X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval))
3474
3475 // All JavaThreads
3476 #define ALL_JAVA_THREADS(X) DO_JAVA_THREADS(ThreadsSMRSupport::get_smr_java_thread_list(), X)
3477
3478 // All JavaThreads + all non-JavaThreads (i.e., every thread in the system)
3479 void Threads::threads_do(ThreadClosure* tc) {
3480 assert_locked_or_safepoint(Threads_lock);
3481 // ALL_JAVA_THREADS iterates through all JavaThreads
3482 ALL_JAVA_THREADS(p) {
3483 tc->do_thread(p);
3484 }
3485 // Someday we could have a table or list of all non-JavaThreads.
3486 // For now, just manually iterate through them.
3487 tc->do_thread(VMThread::vm_thread());
3488 Universe::heap()->gc_threads_do(tc);
3489 WatcherThread *wt = WatcherThread::watcher_thread();
3490 // Strictly speaking, the following NULL check isn't sufficient to make sure
3491 // the data for WatcherThread is still valid upon being examined. However,
3492 // considering that WatchThread terminates when the VM is on the way to
3493 // exit at safepoint, the chance of the above is extremely small. The right
3494 // way to prevent termination of WatcherThread would be to acquire
3495 // Terminator_lock, but we can't do that without violating the lock rank
3496 // checking in some cases.
4365 }
4366
4367 ThreadService::add_thread(p, daemon);
4368
4369 // Maintain fast thread list
4370 ThreadsSMRSupport::add_thread(p);
4371
4372 // Possible GC point.
4373 Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4374 }
4375
4376 void Threads::remove(JavaThread* p) {
4377
4378 // Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
4379 ObjectSynchronizer::omFlush(p);
4380
4381 // Extra scope needed for Thread_lock, so we can check
4382 // that we do not remove thread without safepoint code notice
4383 { MutexLocker ml(Threads_lock);
4384
4385 assert(ThreadsSMRSupport::get_smr_java_thread_list()->includes(p), "p must be present");
4386
4387 // Maintain fast thread list
4388 ThreadsSMRSupport::remove_thread(p);
4389
4390 JavaThread* current = _thread_list;
4391 JavaThread* prev = NULL;
4392
4393 while (current != p) {
4394 prev = current;
4395 current = current->next();
4396 }
4397
4398 if (prev) {
4399 prev->set_next(current->next());
4400 } else {
4401 _thread_list = p->next();
4402 }
4403
4404 _number_of_threads--;
4405 oop threadObj = p->threadObj();
4593 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
4594 void Threads::print_on(outputStream* st, bool print_stacks,
4595 bool internal_format, bool print_concurrent_locks) {
4596 char buf[32];
4597 st->print_raw_cr(os::local_time_string(buf, sizeof(buf)));
4598
4599 st->print_cr("Full thread dump %s (%s %s):",
4600 Abstract_VM_Version::vm_name(),
4601 Abstract_VM_Version::vm_release(),
4602 Abstract_VM_Version::vm_info_string());
4603 st->cr();
4604
4605 #if INCLUDE_SERVICES
4606 // Dump concurrent locks
4607 ConcurrentLocksDump concurrent_locks;
4608 if (print_concurrent_locks) {
4609 concurrent_locks.dump_at_safepoint();
4610 }
4611 #endif // INCLUDE_SERVICES
4612
4613 ThreadsSMRSupport::print_smr_info_on(st);
4614 st->cr();
4615
4616 ALL_JAVA_THREADS(p) {
4617 ResourceMark rm;
4618 p->print_on(st);
4619 if (print_stacks) {
4620 if (internal_format) {
4621 p->trace_stack();
4622 } else {
4623 p->print_stack_on(st);
4624 }
4625 }
4626 st->cr();
4627 #if INCLUDE_SERVICES
4628 if (print_concurrent_locks) {
4629 concurrent_locks.print_locks_on(p, st);
4630 }
4631 #endif // INCLUDE_SERVICES
4632 }
4633
4662 Thread* _current;
4663 char* _buf;
4664 int _buflen;
4665 bool* _found_current;
4666 public:
4667 PrintOnErrorClosure(outputStream* st, Thread* current, char* buf,
4668 int buflen, bool* found_current) :
4669 _st(st), _current(current), _buf(buf), _buflen(buflen), _found_current(found_current) {}
4670
4671 virtual void do_thread(Thread* thread) {
4672 Threads::print_on_error(thread, _st, _current, _buf, _buflen, _found_current);
4673 }
4674 };
4675
4676 // Threads::print_on_error() is called by fatal error handler. It's possible
4677 // that VM is not at safepoint and/or current thread is inside signal handler.
4678 // Don't print stack trace, as the stack may not be walkable. Don't allocate
4679 // memory (even in resource area), it might deadlock the error handler.
4680 void Threads::print_on_error(outputStream* st, Thread* current, char* buf,
4681 int buflen) {
4682 ThreadsSMRSupport::print_smr_info_on(st);
4683 st->cr();
4684
4685 bool found_current = false;
4686 st->print_cr("Java Threads: ( => current thread )");
4687 ALL_JAVA_THREADS(thread) {
4688 print_on_error(thread, st, current, buf, buflen, &found_current);
4689 }
4690 st->cr();
4691
4692 st->print_cr("Other Threads:");
4693 print_on_error(VMThread::vm_thread(), st, current, buf, buflen, &found_current);
4694 print_on_error(WatcherThread::watcher_thread(), st, current, buf, buflen, &found_current);
4695
4696 PrintOnErrorClosure print_closure(st, current, buf, buflen, &found_current);
4697 Universe::heap()->gc_threads_do(&print_closure);
4698
4699 if (!found_current) {
4700 st->cr();
4701 st->print("=>" PTR_FORMAT " (exited) ", p2i(current));
4702 current->print_on_error(st, buf, buflen);
|
3456 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
3457 Prefetch::read((void*)addr, prefetch_interval);
3458 return *addr;
3459 }
3460
3461 // Possibly the ugliest for loop the world has seen. C++ does not allow
3462 // multiple types in the declaration section of the for loop. In this case
3463 // we are only dealing with pointers and hence can cast them. It looks ugly
3464 // but macros are ugly and therefore it's fine to make things absurdly ugly.
3465 #define DO_JAVA_THREADS(LIST, X) \
3466 for (JavaThread *MACRO_scan_interval = (JavaThread*)(uintptr_t)PrefetchScanIntervalInBytes, \
3467 *MACRO_list = (JavaThread*)(LIST), \
3468 **MACRO_end = ((JavaThread**)((ThreadsList*)MACRO_list)->threads()) + ((ThreadsList*)MACRO_list)->length(), \
3469 **MACRO_current_p = (JavaThread**)((ThreadsList*)MACRO_list)->threads(), \
3470 *X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval); \
3471 MACRO_current_p != MACRO_end; \
3472 MACRO_current_p++, \
3473 X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval))
3474
3475 // All JavaThreads
3476 #define ALL_JAVA_THREADS(X) DO_JAVA_THREADS(ThreadsSMRSupport::get_java_thread_list(), X)
3477
3478 // All JavaThreads + all non-JavaThreads (i.e., every thread in the system)
3479 void Threads::threads_do(ThreadClosure* tc) {
3480 assert_locked_or_safepoint(Threads_lock);
3481 // ALL_JAVA_THREADS iterates through all JavaThreads
3482 ALL_JAVA_THREADS(p) {
3483 tc->do_thread(p);
3484 }
3485 // Someday we could have a table or list of all non-JavaThreads.
3486 // For now, just manually iterate through them.
3487 tc->do_thread(VMThread::vm_thread());
3488 Universe::heap()->gc_threads_do(tc);
3489 WatcherThread *wt = WatcherThread::watcher_thread();
3490 // Strictly speaking, the following NULL check isn't sufficient to make sure
3491 // the data for WatcherThread is still valid upon being examined. However,
3492 // considering that WatchThread terminates when the VM is on the way to
3493 // exit at safepoint, the chance of the above is extremely small. The right
3494 // way to prevent termination of WatcherThread would be to acquire
3495 // Terminator_lock, but we can't do that without violating the lock rank
3496 // checking in some cases.
4365 }
4366
4367 ThreadService::add_thread(p, daemon);
4368
4369 // Maintain fast thread list
4370 ThreadsSMRSupport::add_thread(p);
4371
4372 // Possible GC point.
4373 Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4374 }
4375
4376 void Threads::remove(JavaThread* p) {
4377
4378 // Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
4379 ObjectSynchronizer::omFlush(p);
4380
4381 // Extra scope needed for Thread_lock, so we can check
4382 // that we do not remove thread without safepoint code notice
4383 { MutexLocker ml(Threads_lock);
4384
4385 assert(ThreadsSMRSupport::get_java_thread_list()->includes(p), "p must be present");
4386
4387 // Maintain fast thread list
4388 ThreadsSMRSupport::remove_thread(p);
4389
4390 JavaThread* current = _thread_list;
4391 JavaThread* prev = NULL;
4392
4393 while (current != p) {
4394 prev = current;
4395 current = current->next();
4396 }
4397
4398 if (prev) {
4399 prev->set_next(current->next());
4400 } else {
4401 _thread_list = p->next();
4402 }
4403
4404 _number_of_threads--;
4405 oop threadObj = p->threadObj();
4593 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
4594 void Threads::print_on(outputStream* st, bool print_stacks,
4595 bool internal_format, bool print_concurrent_locks) {
4596 char buf[32];
4597 st->print_raw_cr(os::local_time_string(buf, sizeof(buf)));
4598
4599 st->print_cr("Full thread dump %s (%s %s):",
4600 Abstract_VM_Version::vm_name(),
4601 Abstract_VM_Version::vm_release(),
4602 Abstract_VM_Version::vm_info_string());
4603 st->cr();
4604
4605 #if INCLUDE_SERVICES
4606 // Dump concurrent locks
4607 ConcurrentLocksDump concurrent_locks;
4608 if (print_concurrent_locks) {
4609 concurrent_locks.dump_at_safepoint();
4610 }
4611 #endif // INCLUDE_SERVICES
4612
4613 ThreadsSMRSupport::print_info_on(st);
4614 st->cr();
4615
4616 ALL_JAVA_THREADS(p) {
4617 ResourceMark rm;
4618 p->print_on(st);
4619 if (print_stacks) {
4620 if (internal_format) {
4621 p->trace_stack();
4622 } else {
4623 p->print_stack_on(st);
4624 }
4625 }
4626 st->cr();
4627 #if INCLUDE_SERVICES
4628 if (print_concurrent_locks) {
4629 concurrent_locks.print_locks_on(p, st);
4630 }
4631 #endif // INCLUDE_SERVICES
4632 }
4633
4662 Thread* _current;
4663 char* _buf;
4664 int _buflen;
4665 bool* _found_current;
4666 public:
4667 PrintOnErrorClosure(outputStream* st, Thread* current, char* buf,
4668 int buflen, bool* found_current) :
4669 _st(st), _current(current), _buf(buf), _buflen(buflen), _found_current(found_current) {}
4670
4671 virtual void do_thread(Thread* thread) {
4672 Threads::print_on_error(thread, _st, _current, _buf, _buflen, _found_current);
4673 }
4674 };
4675
4676 // Threads::print_on_error() is called by fatal error handler. It's possible
4677 // that VM is not at safepoint and/or current thread is inside signal handler.
4678 // Don't print stack trace, as the stack may not be walkable. Don't allocate
4679 // memory (even in resource area), it might deadlock the error handler.
4680 void Threads::print_on_error(outputStream* st, Thread* current, char* buf,
4681 int buflen) {
4682 ThreadsSMRSupport::print_info_on(st);
4683 st->cr();
4684
4685 bool found_current = false;
4686 st->print_cr("Java Threads: ( => current thread )");
4687 ALL_JAVA_THREADS(thread) {
4688 print_on_error(thread, st, current, buf, buflen, &found_current);
4689 }
4690 st->cr();
4691
4692 st->print_cr("Other Threads:");
4693 print_on_error(VMThread::vm_thread(), st, current, buf, buflen, &found_current);
4694 print_on_error(WatcherThread::watcher_thread(), st, current, buf, buflen, &found_current);
4695
4696 PrintOnErrorClosure print_closure(st, current, buf, buflen, &found_current);
4697 Universe::heap()->gc_threads_do(&print_closure);
4698
4699 if (!found_current) {
4700 st->cr();
4701 st->print("=>" PTR_FORMAT " (exited) ", p2i(current));
4702 current->print_on_error(st, buf, buflen);
|