From c8f7a45a0aa73418ebbb3cf8aca40b67a7572fdc Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sat, 14 Jan 2017 17:39:14 +0200 Subject: [PATCH] Improve mutex debugging prints When lock contention is detected, print also the name of the thread holding the lock and also where the lock was tried to acquire. After the lock is finally acquired after the contention, add a new print there as well to indicate that the lock is now acquired and the thread will continue. This adds overhead to the mutex operations, but that shouldn't be an issue yet. If it becomes too slow, an option can be added later to remove the prints and the overhead altogether. Now we are more happy about the improved verbosity of the locking operations. Signed-off-by: Timo Kokkonen --- process.c | 14 +++++++++++--- process.h | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/process.c b/process.c index 13fe7e5..a54f3dd 100644 --- a/process.c +++ b/process.c @@ -608,24 +608,32 @@ void _mutex_lock_acquired(struct mutex *lock, char *file, int line) { lock->line = line; lock->file = file; + pthread_getname_np(pthread_self(), + lock->owner_name, sizeof(lock->owner_name)); } int _mutex_lock(struct mutex *lock, char *file, int line) { int ret = 0; + int contended = 0; if (!pthread_mutex_trylock(&lock->lock)) goto out_lock; - pr_info("Lock contention on lock %s on %s:%d\n", - lock->name, lock->file, lock->line); + contended = 1; + pr_info("Lock contention at %s:%d on lock %s acquired by %s at %s:%d\n", + file, line, lock->name, + lock->owner_name, lock->file, lock->line); ret = pthread_mutex_lock(&lock->lock); if (ret) - pr_err("Acquirin lock %s failed: %m, acquired %s:%d\n", + pr_err("Acquirin lock %s failed: %m, acquired on %s:%d\n", lock->name, lock->file, lock->line); out_lock: + if (contended) + pr_info("Lock %s acquired at %s:%d after contention\n", + lock->name, file, line); _mutex_lock_acquired(lock, file, line); return ret; } diff --git a/process.h b/process.h index 4134d30..2d58565 100644 --- a/process.h +++ b/process.h @@ -27,6 +27,7 @@ struct mutex { pthread_mutex_t lock; int line; char *file; + char owner_name[16]; time_t lock_time; char *name; }; -- 2.45.0