TBOX在调试模式下,回去自动检测每个库模块内部的线程锁的抢占情况,便于对线程做针对性的一些优化。

    目前可以探测使用内置mutex、spinlock的锁抢占。在程序退出运行前,会在终端打印所有锁的抢占信息:

    1. [tbox]: [lock_profiler]: lock: 0x7fb273000328, name: aicp, occupied: 0
    2. [tbox]: [lock_profiler]: lock: 0x10a362858, name: thread_store, occupied: 0
    3. [tbox]: [lock_profiler]: lock: 0x7fff55ae07e0, name: spider, occupied: 4870
    4. [tbox]: [lock_profiler]: lock: 0x7fb2728412c8, name: thread_pool, occupied: 24
    5. [tbox]: [lock_profiler]: lock: 0x7fb272403ae8, name: pool, occupied: 310
    6. [tbox]: [lock_profiler]: lock: 0x7fb273000400, name: aiop, occupied: 0
    7. [tbox]: [lock_profiler]: lock: 0x7fb27401d0e8, name: ltimer, occupied: 1
    8. [tbox]: [lock_profiler]: lock: 0x7fb27281c278, name: cookies, occupied: 0
    9. [tbox]: [lock_profiler]: lock: 0x7fb272403930, name: native_large_pool, occupied: 0
    10. [tbox]: [lock_profiler]: lock: 0x7fb27281c010, name: aicp_aiop, occupied: 84
    11. [tbox]: [lock_profiler]: lock: 0x10a57a8a8, name: transfer_pool, occupied: 22
    12. [tbox]: [lock_profiler]: lock: 0x7fb2730004f4, name: timer, occupied: 0

    例如上面的信息,是demo中的spider程序给出的锁抢占统计,其中:

    1. spider实例被抢占的最为厉害,总共4870次,可以看出,如果要提高效率,这个还需要进一步做些优化才行。
    2. 线程池总共被抢占24次
    3. 全局内存分配被强占310次
    4. aiop被抢占84次
    5. 传输池被抢占22次
    6. 定时器被抢占1次

    如果上层自己的程序也想对使用的锁进行统计分析的,可以使用下面的接口来注册需要分析的锁,例如:

    1. // 注册需要分析的锁
    2. #ifdef TB_LOCK_PROFILER_ENABLE
    3. tb_lock_profiler_register(tb_lock_profiler(), (tb_pointer_t)&thread_pool->lock, "thread_pool");
    4. #endif

    但是需要注意的一点:只能注册TBOX支持的锁类型:tb_mutex_xxx、tb_spinlock_xxx这两种锁。