前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在Systrace中添加 mutex lock owner

在Systrace中添加 mutex lock owner

作者头像
用户9732312
发布2022-05-13 17:50:03
2.4K0
发布2022-05-13 17:50:03
举报
文章被收录于专栏:ADAS性能优化

在多线程的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) {

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2016-07-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Android性能优化 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档