2013-01-13 22 views
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這inturn #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 

我得到什麼錯?有沒有其他更簡單的方法來做同樣的事情?

感謝, 夏朗

回答

0

看那lockdep實現其也做鎖分析。

1

正如其他用戶所述,lockdep可以爲您做這樣的分析。 Lockdep是Linux內核中已經存在的一個功能(不需要修補),用於檢測內核代碼中的deadlocks。此外,它還提供了與鎖定,等待時間,保持時間,鎖鏈等有關的其他重要統計信息,這些信息可能會有助於提高系統性能。

documentation

「是什麼鎖驗證呢?它「觀察」並映射動態發生的所有鎖定規則(由內核自然使用的自旋鎖,rwlocks,互斥鎖和rwsems觸發)。只要鎖定驗證程序子系統檢測到新的鎖定方案,就會根據現有的一組規則驗證此新規則。如果這個新規則與現有規則集合一致,那麼新規則將被透明地添加並且內核繼續正常運行。如果新規則可能會導致死鎖情況,則會打印出此條件。「

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