I want to modify spin_lock & spin_unlock API in spinlock.h of 2.6.36.4. I want to add a counter for each core so that every time a lock is taken on a core, its counter in incremented and decremented when spin_lock is called. At any point of time I can get lock_depth of each core.

I tried doing this by adding a per CPU variable. using DECLARE_PER_CPU(int, crnt_lck_depth) but to do this I had to #include percpu.hwhich inturn #includes spinlock.h

So I did a work around by creating an array and writing into respective index, but to do this I needed the executing thread's cpu using cpu_id(), again I got the same issues of dependencies.

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

Am getting anything wrong? Is there any other simpler way to do the same .

2 Answers
2

As mentioned by the other user, lockdep can do such profiling for you. Lockdep is a feature already present (no patching required) in the linux kernel to detect deadlocks inside the kernel code. Also, it provides other important statistics related to locking, waitime, holdtime, lock-chains etc which might be useful to improve the system performance.

“What does the lock validator do? It "observes" and maps all locking rules as they occur dynamically (as triggered by the kernel's natural use of spinlocks, rwlocks, mutexes and rwsems). Whenever the lock validator subsystem detects a new locking scenario, it validates this new rule against the existing set of rules. If this new rule is consistent with the existing set of rules then the new rule is added transparently and the kernel continues as normal. If the new rule could create a deadlock scenario then this condition is printed out.”