191 (int)_daemon_threads_count->get_value(), daemon_count);
192 }
193
194 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
195 // Do not count hidden threads
196 if (is_hidden_thread(jt)) {
197 return;
198 }
199
200 assert(jt == JavaThread::current(), "Called by current thread");
201 assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
202
203 decrement_thread_counts(jt, daemon);
204 }
205
206 // FIXME: JVMTI should call this function
207 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
208 assert(thread != NULL, "should be non-NULL");
209 debug_only(Thread::check_for_dangling_thread_pointer(thread);)
210
211 ObjectMonitor *wait_obj = thread->current_waiting_monitor();
212
213 oop obj = NULL;
214 if (wait_obj != NULL) {
215 // thread is doing an Object.wait() call
216 obj = (oop) wait_obj->object();
217 assert(obj != NULL, "Object.wait() should have an object");
218 } else {
219 ObjectMonitor *enter_obj = thread->current_pending_monitor();
220 if (enter_obj != NULL) {
221 // thread is trying to enter() an ObjectMonitor.
222 obj = (oop) enter_obj->object();
223 assert(obj != NULL, "ObjectMonitor should have an associated object!");
224 }
225 }
226
227 Handle h(Thread::current(), obj);
228 return h;
229 }
230
231 bool ThreadService::set_thread_monitoring_contention(bool flag) {
232 MutexLocker m(Management_lock);
233
234 bool prev = _thread_monitoring_contention_enabled;
235 _thread_monitoring_contention_enabled = flag;
236
237 return prev;
238 }
239
345 ThreadStatistics* stat = thread->get_thread_stat();
346 if (stat != NULL) {
347 stat->reset_count_stat();
348 }
349 }
350
351 void ThreadService::reset_contention_time_stat(JavaThread* thread) {
352 ThreadStatistics* stat = thread->get_thread_stat();
353 if (stat != NULL) {
354 stat->reset_time_stat();
355 }
356 }
357
358 // Find deadlocks involving raw monitors, object monitors and concurrent locks
359 // if concurrent_locks is true.
360 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
361 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
362
363 // This code was modified from the original Threads::find_deadlocks code.
364 int globalDfn = 0, thisDfn;
365 ObjectMonitor* waitingToLockMonitor = NULL;
366 JvmtiRawMonitor* waitingToLockRawMonitor = NULL;
367 oop waitingToLockBlocker = NULL;
368 bool blocked_on_monitor = false;
369 JavaThread *currentThread, *previousThread;
370 int num_deadlocks = 0;
371
372 // Initialize the depth-first-number for each JavaThread.
373 JavaThreadIterator jti(t_list);
374 for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
375 jt->set_depth_first_number(-1);
376 }
377
378 DeadlockCycle* deadlocks = NULL;
379 DeadlockCycle* last = NULL;
380 DeadlockCycle* cycle = new DeadlockCycle();
381 for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
382 if (jt->depth_first_number() >= 0) {
383 // this thread was already visited
384 continue;
385 }
386
387 thisDfn = globalDfn;
388 jt->set_depth_first_number(globalDfn++);
389 previousThread = jt;
390 currentThread = jt;
391
392 cycle->reset();
393
394 // When there is a deadlock, all the monitors involved in the dependency
395 // cycle must be contended and heavyweight. So we only care about the
396 // heavyweight monitor a thread is waiting to lock.
397 waitingToLockMonitor = jt->current_pending_monitor();
398 // JVM TI raw monitors can also be involved in deadlocks, and we can be
399 // waiting to lock both a raw monitor and ObjectMonitor at the same time.
400 // It isn't clear how to make deadlock detection work correctly if that
401 // happens.
402 waitingToLockRawMonitor = jt->current_pending_raw_monitor();
403
404 if (concurrent_locks) {
405 waitingToLockBlocker = jt->current_park_blocker();
406 }
407
408 while (waitingToLockMonitor != NULL ||
409 waitingToLockRawMonitor != NULL ||
410 waitingToLockBlocker != NULL) {
411 cycle->add_thread(currentThread);
412 // Give preference to the raw monitor
413 if (waitingToLockRawMonitor != NULL) {
414 Thread* owner = waitingToLockRawMonitor->owner();
415 if (owner != NULL && // the raw monitor could be released at any time
416 owner->is_Java_thread()) {
417 // only JavaThreads can be reported here
468 } else if (currentThread == previousThread) {
469 // Self-loop, ignore
470 break;
471 } else {
472 // We have a (new) cycle
473 num_deadlocks++;
474
475 cycle->set_deadlock(true);
476
477 // add this cycle to the deadlocks list
478 if (deadlocks == NULL) {
479 deadlocks = cycle;
480 } else {
481 last->set_next(cycle);
482 }
483 last = cycle;
484 cycle = new DeadlockCycle();
485 break;
486 }
487 previousThread = currentThread;
488 waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
489 if (concurrent_locks) {
490 waitingToLockBlocker = currentThread->current_park_blocker();
491 }
492 }
493
494 }
495 delete cycle;
496 return deadlocks;
497 }
498
499 ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
500
501 // Create a new ThreadDumpResult object and append to the list.
502 // If GC happens before this function returns, Method*
503 // in the stack trace will be visited.
504 ThreadService::add_thread_dump(this);
505 }
506
507 ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
508 // Create a new ThreadDumpResult object and append to the list.
950 }
951 }
952
953
954 DeadlockCycle::DeadlockCycle() {
955 _is_deadlock = false;
956 _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
957 _next = NULL;
958 }
959
960 DeadlockCycle::~DeadlockCycle() {
961 delete _threads;
962 }
963
964 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
965 st->cr();
966 st->print_cr("Found one Java-level deadlock:");
967 st->print("=============================");
968
969 JavaThread* currentThread;
970 ObjectMonitor* waitingToLockMonitor;
971 JvmtiRawMonitor* waitingToLockRawMonitor;
972 oop waitingToLockBlocker;
973 int len = _threads->length();
974 for (int i = 0; i < len; i++) {
975 currentThread = _threads->at(i);
976 waitingToLockMonitor = currentThread->current_pending_monitor();
977 waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
978 waitingToLockBlocker = currentThread->current_park_blocker();
979 st->cr();
980 st->print_cr("\"%s\":", currentThread->get_thread_name());
981 const char* owner_desc = ",\n which is held by";
982
983 // Note: As the JVM TI "monitor contended enter" event callback is executed after ObjectMonitor
984 // sets the current pending monitor, it is possible to then see a pending raw monitor as well.
985 if (waitingToLockRawMonitor != NULL) {
986 st->print(" waiting to lock JVM TI raw monitor " INTPTR_FORMAT, p2i(waitingToLockRawMonitor));
987 Thread* owner = waitingToLockRawMonitor->owner();
988 // Could be NULL as the raw monitor could be released at any time if held by non-JavaThread
989 if (owner != NULL) {
990 if (owner->is_Java_thread()) {
991 currentThread = (JavaThread*) owner;
992 st->print_cr("%s \"%s\"", owner_desc, currentThread->get_thread_name());
993 } else {
994 st->print_cr(",\n which has now been released");
995 }
996 } else {
|
191 (int)_daemon_threads_count->get_value(), daemon_count);
192 }
193
194 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
195 // Do not count hidden threads
196 if (is_hidden_thread(jt)) {
197 return;
198 }
199
200 assert(jt == JavaThread::current(), "Called by current thread");
201 assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
202
203 decrement_thread_counts(jt, daemon);
204 }
205
206 // FIXME: JVMTI should call this function
207 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
208 assert(thread != NULL, "should be non-NULL");
209 debug_only(Thread::check_for_dangling_thread_pointer(thread);)
210
211 ObjectMonitorHandle omh;
212 ObjectMonitor *wait_obj = thread->current_waiting_monitor(&omh);
213
214 oop obj = NULL;
215 if (wait_obj != NULL) {
216 // thread is doing an Object.wait() call
217 obj = (oop) wait_obj->object();
218 assert(obj != NULL, "Object.wait() should have an object");
219 } else {
220 ObjectMonitorHandle omh;
221 ObjectMonitor *enter_obj = thread->current_pending_monitor(&omh);
222 if (enter_obj != NULL) {
223 // thread is trying to enter() an ObjectMonitor.
224 obj = (oop) enter_obj->object();
225 assert(obj != NULL, "ObjectMonitor should have an associated object!");
226 }
227 }
228
229 Handle h(Thread::current(), obj);
230 return h;
231 }
232
233 bool ThreadService::set_thread_monitoring_contention(bool flag) {
234 MutexLocker m(Management_lock);
235
236 bool prev = _thread_monitoring_contention_enabled;
237 _thread_monitoring_contention_enabled = flag;
238
239 return prev;
240 }
241
347 ThreadStatistics* stat = thread->get_thread_stat();
348 if (stat != NULL) {
349 stat->reset_count_stat();
350 }
351 }
352
353 void ThreadService::reset_contention_time_stat(JavaThread* thread) {
354 ThreadStatistics* stat = thread->get_thread_stat();
355 if (stat != NULL) {
356 stat->reset_time_stat();
357 }
358 }
359
360 // Find deadlocks involving raw monitors, object monitors and concurrent locks
361 // if concurrent_locks is true.
362 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
363 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
364
365 // This code was modified from the original Threads::find_deadlocks code.
366 int globalDfn = 0, thisDfn;
367 ObjectMonitorHandle omh;
368 ObjectMonitor* waitingToLockMonitor = NULL;
369 JvmtiRawMonitor* waitingToLockRawMonitor = NULL;
370 oop waitingToLockBlocker = NULL;
371 bool blocked_on_monitor = false;
372 JavaThread *currentThread, *previousThread;
373 int num_deadlocks = 0;
374
375 // Initialize the depth-first-number for each JavaThread.
376 JavaThreadIterator jti(t_list);
377 for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
378 jt->set_depth_first_number(-1);
379 }
380
381 DeadlockCycle* deadlocks = NULL;
382 DeadlockCycle* last = NULL;
383 DeadlockCycle* cycle = new DeadlockCycle();
384 for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
385 if (jt->depth_first_number() >= 0) {
386 // this thread was already visited
387 continue;
388 }
389
390 thisDfn = globalDfn;
391 jt->set_depth_first_number(globalDfn++);
392 previousThread = jt;
393 currentThread = jt;
394
395 cycle->reset();
396
397 if (waitingToLockMonitor != NULL) {
398 // Done with the current waitingToLockMonitor value so release
399 // the ObjectMonitorHandle manually before we use it again:
400 omh.unset_om_ptr();
401 }
402 // When there is a deadlock, all the monitors involved in the dependency
403 // cycle must be contended and heavyweight. So we only care about the
404 // heavyweight monitor a thread is waiting to lock.
405 waitingToLockMonitor = jt->current_pending_monitor(&omh);
406 // JVM TI raw monitors can also be involved in deadlocks, and we can be
407 // waiting to lock both a raw monitor and ObjectMonitor at the same time.
408 // It isn't clear how to make deadlock detection work correctly if that
409 // happens.
410 waitingToLockRawMonitor = jt->current_pending_raw_monitor();
411
412 if (concurrent_locks) {
413 waitingToLockBlocker = jt->current_park_blocker();
414 }
415
416 while (waitingToLockMonitor != NULL ||
417 waitingToLockRawMonitor != NULL ||
418 waitingToLockBlocker != NULL) {
419 cycle->add_thread(currentThread);
420 // Give preference to the raw monitor
421 if (waitingToLockRawMonitor != NULL) {
422 Thread* owner = waitingToLockRawMonitor->owner();
423 if (owner != NULL && // the raw monitor could be released at any time
424 owner->is_Java_thread()) {
425 // only JavaThreads can be reported here
476 } else if (currentThread == previousThread) {
477 // Self-loop, ignore
478 break;
479 } else {
480 // We have a (new) cycle
481 num_deadlocks++;
482
483 cycle->set_deadlock(true);
484
485 // add this cycle to the deadlocks list
486 if (deadlocks == NULL) {
487 deadlocks = cycle;
488 } else {
489 last->set_next(cycle);
490 }
491 last = cycle;
492 cycle = new DeadlockCycle();
493 break;
494 }
495 previousThread = currentThread;
496 if (waitingToLockMonitor != NULL) {
497 // Done with the current waitingToLockMonitor value so release
498 // the ObjectMonitorHandle manually before we use it again:
499 omh.unset_om_ptr();
500 }
501 waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor(&omh);
502 if (concurrent_locks) {
503 waitingToLockBlocker = currentThread->current_park_blocker();
504 }
505 }
506
507 }
508 delete cycle;
509 return deadlocks;
510 }
511
512 ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
513
514 // Create a new ThreadDumpResult object and append to the list.
515 // If GC happens before this function returns, Method*
516 // in the stack trace will be visited.
517 ThreadService::add_thread_dump(this);
518 }
519
520 ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
521 // Create a new ThreadDumpResult object and append to the list.
963 }
964 }
965
966
967 DeadlockCycle::DeadlockCycle() {
968 _is_deadlock = false;
969 _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
970 _next = NULL;
971 }
972
973 DeadlockCycle::~DeadlockCycle() {
974 delete _threads;
975 }
976
977 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
978 st->cr();
979 st->print_cr("Found one Java-level deadlock:");
980 st->print("=============================");
981
982 JavaThread* currentThread;
983 JvmtiRawMonitor* waitingToLockRawMonitor;
984 oop waitingToLockBlocker;
985 int len = _threads->length();
986 for (int i = 0; i < len; i++) {
987 currentThread = _threads->at(i);
988 ObjectMonitorHandle omh;
989 ObjectMonitor* waitingToLockMonitor = currentThread->current_pending_monitor(&omh);
990 waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
991 waitingToLockBlocker = currentThread->current_park_blocker();
992 st->cr();
993 st->print_cr("\"%s\":", currentThread->get_thread_name());
994 const char* owner_desc = ",\n which is held by";
995
996 // Note: As the JVM TI "monitor contended enter" event callback is executed after ObjectMonitor
997 // sets the current pending monitor, it is possible to then see a pending raw monitor as well.
998 if (waitingToLockRawMonitor != NULL) {
999 st->print(" waiting to lock JVM TI raw monitor " INTPTR_FORMAT, p2i(waitingToLockRawMonitor));
1000 Thread* owner = waitingToLockRawMonitor->owner();
1001 // Could be NULL as the raw monitor could be released at any time if held by non-JavaThread
1002 if (owner != NULL) {
1003 if (owner->is_Java_thread()) {
1004 currentThread = (JavaThread*) owner;
1005 st->print_cr("%s \"%s\"", owner_desc, currentThread->get_thread_name());
1006 } else {
1007 st->print_cr(",\n which has now been released");
1008 }
1009 } else {
|