在多线程的debug中,如果线程相互deak lock是很头痛的事情。下面的patch 可以把lock的owner 信息在ftrace 中记录,并且在systrace 中显示出来,可以加快对问题的debug
添加下面的patch 后
index 851fc3d..d75fe2a --- a/libc/bionic/pthread_mutex.cpp +++ b/libc/bionic/pthread_mutex.cpp @@ -44,6 +44,9 @@ #include "private/bionic_time_conversions.h" #include "private/bionic_tls.h" +#include <stdio.h> + + /* a mutex attribute holds the following fields * * bits: name description @@ -277,6 +280,9 @@ static inline __always_inline int __pthread_normal_mutex_trylock(pthread_mutex_i uint16_t old_state = unlocked; if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, locked_uncontended, memory_order_acquire, memory_order_relaxed))) { + pid_t tid = __get_thread()->tid; + + atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed); return 0; } return EBUSY; @@ -301,8 +307,11 @@ static inline __always_inline int __pthread_normal_mutex_lock(pthread_mutex_inte if (__predict_true(__pthread_normal_mutex_trylock(mutex, shared) == 0)) { return 0; } + char tracename[256]; + pid_t tid = atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed); + sprintf(tracename, " Contending for pthread mutex normal, mutex owner: %d\n ",tid); - ScopedTrace trace("Contending for pthread mutex"); + ScopedTrace trace(tracename); const uint16_t unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; const uint16_t locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; @@ -451,8 +460,11 @@ static int __pthread_mutex_lock_with_timeout(pthread_mutex_internal_t* mutex, return 0; } } + char tracename[256]; + pid_t mutex_tid = atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed); + sprintf(tracename, " Contending for pthread mutex timeout, mutex owner: %d\n ",mutex_tid); - ScopedTrace trace("Contending for pthread mutex"); + ScopedTrace trace(tracename); while (true) { if (old_state == unlocked) {