1

我想在 2.6.36.4 的 spinlock.h 中修改 spin_lock & spin_unlock API。我想为每个核心添加一个计数器,以便每次在核心上获取锁时,它的计数器在调用 spin_lock 时递增和递减。在任何时候,我都可以获得每个核心的 lock_depth。

我尝试通过添加每个 CPU 变量来做到这一点。使用DECLARE_PER_CPU(int, crnt_lck_depth)但要做到这一点,我不得不#include percpu.h反过来#includes spinlock.h

所以我通过创建一个数组并写入相应的索引来解决这个问题,但要做到这一点,我需要执行线程的 cpu using cpu_id(),我再次遇到了相同的依赖问题。

这是我到目前为止在 spinlock.h 中所做的

static int ctr_lock_depth[24];                                                       
EXPORT_SYMBOL(ctr_lock_depth);//ctr_depth is used by my module

/* from smp.h */                                                                                     
extern int raw_smp_processor_id(void);                                               
static inline void spin_lock(spinlock_t *lock)                                       
{                                                                                    
        int cpu;                                                                     
        raw_spin_lock(&lock->rlock);                                                 
        cpu = raw_smp_processor_id();                                                
        ctr_lock_depth[cpu]++;                                                       
}     
static inline void spin_unlock(spinlock_t *lock)                                     
{                                                                                    
        int cpu ;                                                                    
        raw_spin_unlock(&lock->rlock);                                               
        cpu = raw_smp_processor_id();                                                
        ctr_lock_depth[cpu]--;                                                       
}

这些是我得到的警告/错误

include/linux/spinlock.h:292:1: warning: data definition has no type or storage class
include/linux/spinlock.h:292:1: warning: type defaults to ‘int’ in declaration of ‘EXPORT_SYMBOL’
include/linux/spinlock.h:292:1: warning: parameter names (without types) in function declaration
include/linux/timex.h:76:17: error: field ‘time’ has incomplete type
In file included from include/linux/ktime.h:25:0,
                 from include/linux/timer.h:5,
                 from include/linux/workqueue.h:8,
                 from include/linux/pm.h:25,
                 from /usr/src/linux-2.6.36.4.kvm-rr/arch/x86/include/asm/apic.h:6,
                 from /usr/src/linux-2.6.36.4.kvm-rr/arch/x86/include/asm/smp.h:13,
                 from include/linux/spinlock.h:62,
                 from include/linux/seqlock.h:29,
                 from include/linux/time.h:8,
                 from include/linux/stat.h:60,
                 from include/linux/module.h:10,
                 from include/linux/crypto.h:21,
                 from arch/x86/kernel/asm-offsets_64.c:8,
                 from arch/x86/kernel/asm-offsets.c:4:
include/linux/jiffies.h:257:10: warning: "NSEC_PER_SEC" is not defined
include/linux/ktime.h:84:6: error: ‘NSEC_PER_SEC’ undeclared (first use in this function)
include/linux/time.h:240:23: error: conflicting types for ‘ns_to_timeval’
include/linux/ktime.h:294:22: note: previous implicit declaration of ‘ns_to_timeval’ was here

有什么不对吗?有没有其他更简单的方法来做同样的事情。

谢谢, 莎朗

4

2 回答 2

1

正如其他用户所提到的,lockdep可以为您进行此类分析。Lockdep是 linux 内核中已经存在的功能(无需修补),用于检测deadlocks内核代码内部。此外,它还提供与锁定、等待时间、保持时间、锁链等相关的其他重要统计信息,这些统计信息可能有助于提高系统性能。

文档中:

“锁验证器是做什么的?它“观察”并映射所有动态发生的锁定规则(由内核对自旋锁、rwlock、互斥锁和 rwsem 的自然使用触发)。每当锁定验证器子系统检测到新的锁定场景时,它都会根据现有规则集验证此新规则。如果此新规则与现有规则集一致,则透明添加新规则,内核继续正常运行。如果新规则可能造成死锁场景,则打印出此条件。”</p>

另外,这篇文章指出了如何使用它:How to use lockdep feature in linux kernel for deadlock detection

于 2014-03-14T05:51:35.860 回答
0

查看同样在进行锁分析的 lockdep 实现。

于 2013-01-14T12:37:00.700 回答