-
-
Notifications
You must be signed in to change notification settings - Fork 725
利用锁分析器进行线程竞争检测
ruki edited this page Aug 18, 2014
·
1 revision
TBOX在调试模式下,回去自动检测每个库模块内部的线程锁的抢占情况,便于对线程做针对性的一些优化。
目前可以探测使用内置mutex、spinlock的锁抢占。在程序退出运行前,会在终端打印所有锁的抢占信息:
[tbox]: [lock_profiler]: lock: 0x7fb273000328, name: aicp, occupied: 0
[tbox]: [lock_profiler]: lock: 0x10a362858, name: thread_store, occupied: 0
[tbox]: [lock_profiler]: lock: 0x7fff55ae07e0, name: spider, occupied: 4870
[tbox]: [lock_profiler]: lock: 0x7fb2728412c8, name: thread_pool, occupied: 24
[tbox]: [lock_profiler]: lock: 0x7fb272403ae8, name: pool, occupied: 310
[tbox]: [lock_profiler]: lock: 0x7fb273000400, name: aiop, occupied: 0
[tbox]: [lock_profiler]: lock: 0x7fb27401d0e8, name: ltimer, occupied: 1
[tbox]: [lock_profiler]: lock: 0x7fb27281c278, name: cookies, occupied: 0
[tbox]: [lock_profiler]: lock: 0x7fb272403930, name: native_large_pool, occupied: 0
[tbox]: [lock_profiler]: lock: 0x7fb27281c010, name: aicp_aiop, occupied: 84
[tbox]: [lock_profiler]: lock: 0x10a57a8a8, name: transfer_pool, occupied: 22
[tbox]: [lock_profiler]: lock: 0x7fb2730004f4, name: timer, occupied: 0
例如上面的信息,是demo中的spider程序给出的锁抢占统计,其中:
- spider实例被抢占的最为厉害,总共4870次,可以看出,如果要提高效率,这个还需要进一步做些优化才行。
- 线程池总共被抢占24次
- 全局内存分配被强占310次
- aiop被抢占84次
- 传输池被抢占22次
- 定时器被抢占1次
如果上层自己的程序也想对使用的锁进行统计分析的,可以使用下面的接口来注册需要分析的锁,例如:
// 注册需要分析的锁
#ifdef TB_LOCK_PROFILER_ENABLE
tb_lock_profiler_register(tb_lock_profiler(), (tb_pointer_t)&thread_pool->lock, "thread_pool");
#endif
但是需要注意的一点:只能注册TBOX支持的锁类型:tb_mutex_xxx、tb_spinlock_xxx这两种锁。