修改自旋锁时面临依赖关系问题

时间:2013-01-13 20:00:29

标签: linux kernel c dependencies

我想修改spin_lock& 2.6.36.4的spinlock.h中的spin_unlock API。我想为每个核心添加一个计数器,这样每次对核心进行锁定时,其计数器会在调用spin_lock时递增和递减。在任何时候我都可以获得每个核心的lock_depth。

我尝试通过添加每个CPU变量来做到这一点。使用DECLARE_PER_CPU(int, crnt_lck_depth)但要执行此操作,我必须#include percpu.h进入#includes spinlock.h

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

以下是我在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

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

谢谢, 夏朗

2 个答案:

答案 0 :(得分:1)

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

来自documentation

“锁定验证器有什么作用?它"观察"并映射动态发生的所有锁定规则(由内核自然使用自旋锁,rwlocks,互斥锁和rwsems触发)。只要锁定验证器子系统检测到新的锁定方案,它就会根据现有规则集验证此新规则。如果此新规则与现有规则集一致,则新规则将以透明方式添加,内核将继续正常运行。如果新规则可能会创建死锁方案,则会打印出此条件。“

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

答案 1 :(得分:0)

查看lockdep实现,它也在进行锁定分析。