lock_stat: Add "con-hungry" to show that how many person-time fight for the ticket spinlock
Subsystems (3)
PATCH 1/1 lock_stat: Add "con-hungry" to show that how many person-time fight for the ticket spinlock
Because the implementation of X86 spinlock adopts ticket lock, we can know how many threads are waiting for the lock when there is a contention on the spinlock. We add the number of these threads to the bounces[bounce_hungry] and name "bounces[bounce_hungry]" as "con-hungry". Finally, we can use this number to show that "the number of threads waiting for a spinlock/ a contention". From it, we can know that whether it is valuable to replace the spinlock with more scalable spinlock since the design principle of scalability spinlock is to decrease the cache invalid cost, if the "con-hungry/contentions" is not high, we cannot benefit from more scalable spinlocks.
include/linux/lockdep.h +2 -0
@@ -136,6 +136,7 @@ enum bounce_type { bounce_acquired_read, bounce_contended_write, bounce_contended_read, + bounce_hungry, nr_bounce_types, bounce_acquired = bounce_acquired_write,
@@ -165,6 +166,7 @@ struct lockdep_map {const char *name;#ifdef CONFIG_LOCK_STAT int cpu; +int isspinlock;#endif };
kernel/lockdep.c +0 -0
@@ -3000,6 +3000,20 @@ __lock_contended(struct lockdep_map *lock,
unsigned long ip)
struct lock_class_stats *stats;
unsigned int depth;
int i, point;+ spinlock_t * lock_ptr;
+ unsigned long hungry;
+
+ if (lock->isspinlock){
+ lock_ptr = container_of(lock,spinlock_t,dep_map);
+
+#if (NR_CPUS < 256)
+ hungry=(unsigned
char)((lock_ptr->raw_lock.slock>>8&0xff)-(lock_ptr->raw_lock.slock&0xff)); +
+#else
+ hungry=(unsigned
short)((lock_ptr->raw_lock.slock>>16&0xffff)-(lock_ptr->raw_lock.slock&0xffff)); +
+#endif
+ }
depth = curr->lockdep_depth;
if (DEBUG_LOCKS_WARN_ON(!depth))
@@ -3030,9 +3044,13 @@ found_it:
stats->contention_point[point]++;
if (lock->cpu != smp_processor_id())
stats->bounces[bounce_contended + !!hlock->read]++;
+ if (lock->isspinlock){
+ stats->bounces[bounce_hungry]+=hungry+1;
+ }
put_lock_stats(stats);}
+
static void
__lock_acquired(struct lockdep_map *lock)
{
kernel/lockdep_proc.c +0 -0
@@ -534,7 +534,8 @@ static void seq_stats(struct seq_file *m, struct
lock_stat_data *data)
else
seq_printf(m, "%40s:", name);- seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
+ seq_printf(m, "%14lu ",
stats->bounces[bounce_contended_write]);
+ seq_printf(m, "%14lu ", stats->bounces[bounce_hungry]);
seq_lock_time(m, &stats->write_waittime);
seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
seq_lock_time(m, &stats->write_holdtime);
@@ -584,10 +585,11 @@ static void seq_header(struct seq_file *m)
{
seq_printf(m, "lock_stat version 0.2\n");
seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
- seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
+ seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
"%14s %14s\n",
"class name",
"con-bounces",
+ "con-hungry",
"contentions",
"waittime-min",
"waittime-max",
@@ -597,7 +599,7 @@ static void seq_header(struct seq_file *m)
"holdtime-min",
"holdtime-max",
"holdtime-total");
- seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
+ seq_line(m, '-', 0, 40 + 1 + 11 * (14 + 1));
seq_printf(m, "\n");}
lib/spinlock_debug.c +2 -0
@@ -27,6 +27,10 @@ void __spin_lock_init(spinlock_t *lock, const char *name, lock->magic =SPINLOCK_MAGIC ; lock->owner =SPINLOCK_OWNER_INIT ; lock->owner_cpu =-1 ; + lock->dep_map.isspinlock =0 ; +#if ((defined(CONFIG_X86) || defined(CONFIG_X86_64))&&
defined(CONFIG_LOCK_STAT))
+ lock->dep_map.isspinlock = 1;
+#endif
}
EXPORT_SYMBOL(__spin_lock_init);
coder@coder-desktop:~/work/linux/linux-2.6$ git diff -u -p
include/linux/lockdep.h +2 -0
@@ -136,6 +136,7 @@ enum bounce_type { bounce_acquired_read, bounce_contended_write, bounce_contended_read, + bounce_hungry, nr_bounce_types, bounce_acquired = bounce_acquired_write,
@@ -165,6 +166,7 @@ struct lockdep_map {const char *name;#ifdef CONFIG_LOCK_STAT int cpu; +int isspinlock;#endif };
kernel/lockdep.c +0 -0
@@ -3000,6 +3000,20 @@ __lock_contended(struct lockdep_map *lock,
unsigned long ip)
struct lock_class_stats *stats;
unsigned int depth;
int i, point;+ spinlock_t * lock_ptr;
+ unsigned long hungry;
+
+ if (lock->isspinlock){
+ lock_ptr = container_of(lock,spinlock_t,dep_map);
+
+#if (NR_CPUS < 256)
+ hungry=(unsigned
char)((lock_ptr->raw_lock.slock>>8&0xff)-(lock_ptr->raw_lock.slock&0xff)); +
+#else
+ hungry=(unsigned
short)((lock_ptr->raw_lock.slock>>16&0xffff)-(lock_ptr->raw_lock.slock&0xffff)); +
+#endif
+ }
depth = curr->lockdep_depth;
if (DEBUG_LOCKS_WARN_ON(!depth))
@@ -3030,9 +3044,13 @@ found_it:
stats->contention_point[point]++;
if (lock->cpu != smp_processor_id())
stats->bounces[bounce_contended + !!hlock->read]++;
+ if (lock->isspinlock){
+ stats->bounces[bounce_hungry]+=hungry+1;
+ }
put_lock_stats(stats);}
+
static void
__lock_acquired(struct lockdep_map *lock)
{
kernel/lockdep_proc.c +0 -0
@@ -534,7 +534,8 @@ static void seq_stats(struct seq_file *m, struct
lock_stat_data *data)
else
seq_printf(m, "%40s:", name);- seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
+ seq_printf(m, "%14lu ",
stats->bounces[bounce_contended_write]);
+ seq_printf(m, "%14lu ", stats->bounces[bounce_hungry]);
seq_lock_time(m, &stats->write_waittime);
seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
seq_lock_time(m, &stats->write_holdtime);
@@ -584,10 +585,11 @@ static void seq_header(struct seq_file *m)
{
seq_printf(m, "lock_stat version 0.2\n");
seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
- seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
+ seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
"%14s %14s\n",
"class name",
"con-bounces",
+ "con-hungry",
"contentions",
"waittime-min",
"waittime-max",
@@ -597,7 +599,7 @@ static void seq_header(struct seq_file *m)
"holdtime-min",
"holdtime-max",
"holdtime-total");
- seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
+ seq_line(m, '-', 0, 40 + 1 + 11 * (14 + 1));
seq_printf(m, "\n");}
lib/spinlock_debug.c +2 -0
@@ -27,6 +27,10 @@ void __spin_lock_init(spinlock_t *lock, const char *name, lock->magic =SPINLOCK_MAGIC ; lock->owner =SPINLOCK_OWNER_INIT ; lock->owner_cpu =-1 ; + lock->dep_map.isspinlock =0 ; +#if ((defined(CONFIG_X86) || defined(CONFIG_X86_64))&&
defined(CONFIG_LOCK_STAT))
+ lock->dep_map.isspinlock = 1;
+#endif
}
EXPORT_SYMBOL(__spin_lock_init);
Quoted 12 lines · Yang Xi
Because the implementation of X86 spinlock adopts ticket lock, we can know how many threads are waiting for the lock when there is a contention on the spinlock. We add the number of these threads to the bounces[bounce_hungry] and name "bounces[bounce_hungry]" as "con-hungry". Finally, we can use this number to show that "the number of threads waiting for a spinlock/ a contention". From it, we can know that whether it is valuable to replace the spinlock with more scalable spinlock since the design principle of scalability spinlock is to decrease the cache invalid cost, if the "con-hungry/contentions" is not high, we cannot benefit from more scalable spinlocks.
Quoted 1 line
Signed-off-by Yang Xi <[email protected]>
While I like the idea, the code is horrid, please clean this up and try again.
The lockstat code should not ever need to know about lock implementation details like which you #ifdeffed in.
Quoted 13 lines
include/linux/lockdep.h +1
-0@@ -136,6 +136,7 @@ enum bounce_type { bounce_acquired_read, bounce_contended_write, bounce_contended_read, + bounce_hungry, nr_bounce_types, bounce_acquired = bounce_acquired_write,
include/linux/lockdep.h struct lockdep_map { · line 170
Quoted 5 lines
include/linux/lockdep.h +1
-0@@ -165,6 +166,7 @@ struct lockdep_map {const char *name;#ifdef CONFIG_LOCK_STAT int cpu; +int isspinlock;#endif
perhaps a bitfield?
Quoted 16 lines
include/linux/lockdep.h +0
-0};kernel/lockdep.c +5
-0@@ -3000,6 +3000,20 @@ __lock_contended(struct lockdep_map *lock, unsigned long ip)struct lock_class_stats *stats;unsigned int depth;int i, point; +spinlock_t * lock_ptr; +unsigned long hungry; + +if (lock->isspinlock){ + lock_ptr =container_of (lock,spinlock_t,dep_map);
kernel/lockdep.c __lock_contended(struct lockdep_map *lock, · line 3018
Quoted 10 lines
kernel/lockdep.c +8
-0+ +#if (NR_CPUS <256 ) + hungry=(unsigned char )((lock_ptr->raw_lock.slock>>8 &0xff )-(lock_ptr->raw_lock.slock&0xff )); + +#else + hungry=(unsigned short )((lock_ptr->raw_lock.slock>>16 &0xffff )-(lock_ptr->raw_lock.slock&0xffff )); + +#endif
Prime example of uglyness that is unacceptable.
kernel/lockdep.c found_it: · line 3050
Quoted 12 lines
kernel/lockdep.c +1
-0+ }depth = curr->lockdep_depth; if (DEBUG_LOCKS_WARN_ON(!depth))kernel/lockdep.c +3
-0@@ -3030,9 +3044,13 @@ found_it: stats->contention_point[point]++;if (lock->cpu !=smp_processor_id ()) stats->bounces[bounce_contended + !!hlock->read]++; +if (lock->isspinlock){ + stats->bounces[bounce_hungry]+=hungry+1 ; + } put
superfluous braces.
lib/spinlock_debug.c void __spin_lock_init(spinlock_t *lock, const char *name, · line 34
Quoted 13 lines
lib/spinlock_debug.c +4
-0@@ -27,6 +27,10 @@ void __spin_lock_init(spinlock_t *lock, const char *name, lock->magic =SPINLOCK_MAGIC ; lock->owner =SPINLOCK_OWNER_INIT ; lock->owner_cpu =-1 ; + lock->dep_map.isspinlock =0 ; +#if ((defined(CONFIG_X86 ) || defined(CONFIG_X86_64 ))&& defined(CONFIG_LOCK_STAT )) + lock->dep_map.isspinlock =1 ; +#endif
sorry, no, imagine the utter mess this will become when some other arch adds ticket locks too...
Thanks. This should be better. I add __ticket_spin_nm_contended in x86/include/asm/spinlock.h to return the number of threads waiting/holding for the ticket spinlock(Note: The number contains the holder). If the spinlock is ticket lock, "spin_nm_contended" will be __ticket_spin_nm_contended, otherwise, it will be 0.
arch/x86/include/asm/spinlock.h +0 -0
@@ -172,6 +172,13 @@ static inline int
__ticket_spin_is_contended(raw_spinlock_t *lock)
return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) > 1;
}
+static inline int __ticket_spin_nm_contended(raw_spinlock_t *lock) +{
+ int tmp = ACCESS_ONCE(lock->slock);
+
+ return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) + 1;
+}
+
#ifdef CONFIG_PARAVIRT
/*
* Define virtualization-friendly old-style lock byte lock, for use in
include/linux/lockdep.h +2 -0
@@ -136,6 +136,7 @@ enum bounce_type { bounce_acquired_read, bounce_contended_write, bounce_contended_read, + bounce_hungry, nr_bounce_types, bounce_acquired = bounce_acquired_write,
@@ -165,6 +166,7 @@ struct lockdep_map {const char *name;#ifdef CONFIG_LOCK_STAT int cpu; + unsigned int isspinlock:1;#endif };
include/linux/spinlock.h +0 -0
@@ -127,6 +127,12 @@ do { \#define spin_is_contended (lock)
__raw_spin_is_contended(&(lock)->raw_lock)
#endif
+#ifdef TICKET_SHIFT
+#define spin_nm_contended(lock) __ticket_spin_nm_contended(&(lock)->raw_lock) +#else
+#define spin_nm_contended(lock) (0)
+#endif
+
/**
* spin_unlock_wait - wait until the spinlock gets unlocked
* @lock: the spinlock in question.
kernel/lockdep.c +0 -0
@@ -3000,7 +3000,14 @@ __lock_contended(struct lockdep_map *lock,
unsigned long ip)
struct lock_class_stats *stats;
unsigned int depth;
int i, point;-
+ spinlock_t * lock_ptr;
+ unsigned long hungry = 0;
+
+ if (lock->isspinlock) {
+ lock_ptr = container_of(lock,spinlock_t,dep_map);
+ hungry = spin_nm_contended(lock_ptr);
+ }
+
depth = curr->lockdep_depth;
if (DEBUG_LOCKS_WARN_ON(!depth))
return;
@@ -3029,10 +3036,13 @@ found_it:
if (point < ARRAY_SIZE(stats->contention_point))
stats->contention_point[point]++;
if (lock->cpu != smp_processor_id())- stats->bounces[bounce_contended + !!hlock->read]++;
+ stats->bounces[bounce_contended + !!hlock->read]++;
+ stats->bounces[bounce_hungry]+=hungry;
+
put_lock_stats(stats);
}
+
static void
__lock_acquired(struct lockdep_map *lock)
{
kernel/lockdep_proc.c +0 -0
@@ -534,7 +534,8 @@ static void seq_stats(struct seq_file *m, struct
lock_stat_data *data)
else
seq_printf(m, "%40s:", name);- seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
+ seq_printf(m, "%14lu ",
stats->bounces[bounce_contended_write]);
+ seq_printf(m, "%14lu ", stats->bounces[bounce_hungry]);
seq_lock_time(m, &stats->write_waittime);
seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
seq_lock_time(m, &stats->write_holdtime);
@@ -584,10 +585,11 @@ static void seq_header(struct seq_file *m)
{
seq_printf(m, "lock_stat version 0.2\n");
seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
- seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
+ seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
"%14s %14s\n",
"class name",
"con-bounces",
+ "con-hungry",
"contentions",
"waittime-min",
"waittime-max",
@@ -597,7 +599,7 @@ static void seq_header(struct seq_file *m)
"holdtime-min",
"holdtime-max",
"holdtime-total");
- seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
+ seq_line(m, '-', 0, 40 + 1 + 11 * (14 + 1));
seq_printf(m, "\n");}
lib/spinlock_debug.c +1 -0
@@ -27,6 +27,7 @@ void __spin_lock_init(spinlock_t *lock, const char *name, lock->magic =SPINLOCK_MAGIC ; lock->owner =SPINLOCK_OWNER_INIT ; lock->owner_cpu =-1 ; + lock->dep_map.isspinlock =1 ; }EXPORT_SYMBOL (__spin_lock_init);
Quoted 5 lines · Yang Xi
Thanks. This should be better. I add
__ticket_spin_nm_contendedinx86/include/asm/spinlock.hto return the number of threads waiting/holding for the ticket spinlock(Note: The number contains the holder). If the spinlock is ticket lock, "spin_nm_contended" will be__ticket_spin_nm_contended, otherwise, it will be 0.
Much better indeed, still some comments though :-)
Quoted 34 lines
Signed-off-by Yang Xi <[email protected]>arch/x86/include/asm/spinlock.h +7
-0@@ -172,6 +172,13 @@ static inline int__ticket_spin_is_contended (raw_spinlock_t *lock) return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) > 1; } +static inline int __ticket_spin_nm_contended (raw_spinlock_t *lock) +{ +int tmp =ACCESS_ONCE (lock->slock); + +return (((tmp >>TICKET_SHIFT ) - tmp) & ((1 <<TICKET_SHIFT ) -1 )) +1 ; +} +#ifdef CONFIG_PARAVIRT /* * Define virtualization-friendly old-style lock byte lock, for use in
include/linux/lockdep.h +1
-0@@ -136,6 +136,7 @@ enum bounce_type { bounce_acquired_read, bounce_contended_write, bounce_contended_read, + bounce_hungry, nr_bounce_types, bounce_acquired = bounce_acquired_write,
include/linux/lockdep.h struct lockdep_map { · line 169
Quoted 4 lines
include/linux/lockdep.h +1
-0@@ -165,6 +166,7 @@ struct lockdep_map {const char *name;#ifdef CONFIG_LOCK_STAT int cpu; + unsigned int isspinlock:1;
I of course meant folding cpu and isspinlock into a combined bitfield (sorry for not being more clear), thereby saving space, this still takes 2*sizeof(int).
We can safely take some bits from the cpu number as there currently are no plans for a 2g cpu machine, right SGI? :-)
Quoted 8 lines
include/linux/lockdep.h +0
-0#endif };include/linux/spinlock.h +0
-0
include/linux/spinlock.h do { · line 136
Quoted 10 lines
include/linux/spinlock.h +5
-0@@ -127,6 +127,12 @@ do { \#define spin_is_contended (lock) __raw_spin_is_contended(&(lock)->raw_lock)#endif +#ifdef TICKET_SHIFT +#define spin_nm_contended (lock) __ticket_spin_nm_contended(&(lock)->raw_lock) +#else +#define spin_nm_contended (lock) (0) +#endif
This is a bit icky still, I don't think TICKET_SHIFT is nessecarily the best macro to check on (other ticket lock implementations might not define it).
A possible solution is to introduce a Kconfig variable HAVE_TICKET_LOCK and select that from x86.
Also, may I suggest another name, spin_nr_contended() perhaps?
Quoted 8 lines
include/linux/spinlock.h +0
-0/** * spin_unlock_wait - wait until the spinlock gets unlocked * @lock: the spinlock in question. kernel/lockdep.c +0
-0
kernel/lockdep.c __lock_contended(struct lockdep_map *lock, · line 3005
Quoted 7 lines
kernel/lockdep.c +2
-1@@ -3000,7 +3000,14 @@ __lock_contended(struct lockdep_map *lock, unsigned long ip)struct lock_class_stats *stats;unsigned int depth;int i, point; - +spinlock_t * lock_ptr; +unsigned long hungry =0 ;
This violates coding style, please run checkpatch.
Quoted 9 lines
kernel/lockdep.c +6
-0+ +if (lock->isspinlock) { + lock_ptr =container_of (lock,spinlock_t,dep_map); + hungry =spin_nm_contended (lock_ptr); + } + depth = curr->lockdep_depth;if (DEBUG_LOCKS_WARN_ON (!depth))return;
Hth,
Peter
Quoted 6 lines
I of course meant folding cpu and isspinlock into a combined bitfield (sorry for not being more clear), thereby saving space, this still takes 2*sizeof(int).
We can safely take some bits from the cpu number as there currently are no plans for a 2g cpu machine, right SGI? :-)
Thanks, ok, 31bits enough for cpu at now stage. 1bit for isticketspinlock. Here is the new one. Welcome more comments :).
arch/x86/Kconfig +1 -0
@@ -34,6 +34,7 @@ config X86 select HAVE_ARCH_TRACEHOOK select HAVE_GENERIC_DMA_COHERENT if X86_32 select HAVE_EFFICIENT_UNALIGNED_ACCESS + select HAVE_TICKET_SPINLOCK config ARCH_DEFCONFIG string
arch/x86/include/asm/spinlock.h +0 -0
@@ -172,6 +172,13 @@ static inline int
__ticket_spin_is_contended(raw_spinlock_t *lock)
return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) > 1;
}
+static inline int __ticket_spin_nr_contended(raw_spinlock_t *lock) +{
+ int tmp = ACCESS_ONCE(lock->slock);
+
+ return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) + 1;
+}
+
#ifdef CONFIG_PARAVIRT
/*
* Define virtualization-friendly old-style lock byte lock, for use in
include/linux/lockdep.h +3 -1
@@ -136,6 +136,7 @@ enum bounce_type { bounce_acquired_read, bounce_contended_write, bounce_contended_read, + bounce_hungry, nr_bounce_types, bounce_acquired = bounce_acquired_write,
@@ -164,7 +165,8 @@ struct lockdep_map {struct lock_class *class_cache;const char *name;#ifdef CONFIG_LOCK_STAT -int cpu; + unsigned int cpu:31; + unsigned int isticketspinlock:1;#endif };
include/linux/spinlock.h +0 -0
@@ -127,6 +127,12 @@ do { \#define spin_is_contended (lock)
__raw_spin_is_contended(&(lock)->raw_lock)
#endif
+#ifdef CONFIG_HAVE_TICKET_SPINLOCK
+#define spin_nr_contended(lock) __ticket_spin_nr_contended(&(lock)->raw_lock) +#else
+#define spin_nr_contended(lock) (0)
+#endif
+
/**
* spin_unlock_wait - wait until the spinlock gets unlocked
* @lock: the spinlock in question.
kernel/lockdep.c +0 -0
@@ -3000,6 +3000,13 @@ __lock_contended(struct lockdep_map *lock,
unsigned long ip)
struct lock_class_stats *stats;
unsigned int depth;
int i, point;+ spinlock_t *lock_ptr;
+ unsigned long hungry = 0;
+
+ if (lock->isticketspinlock) {
+ lock_ptr = container_of(lock, spinlock_t, dep_map);
+ hungry = spin_nr_contended(lock_ptr);
+ }
depth = curr->lockdep_depth;
if (DEBUG_LOCKS_WARN_ON(!depth))
@@ -3030,9 +3037,12 @@ found_it:
stats->contention_point[point]++;
if (lock->cpu != smp_processor_id())
stats->bounces[bounce_contended + !!hlock->read]++;
+ stats->bounces[bounce_hungry] += hungry;
+
put_lock_stats(stats);}
+
static void
__lock_acquired(struct lockdep_map *lock)
{
kernel/lockdep_proc.c +0 -0
@@ -535,6 +535,7 @@ static void seq_stats(struct seq_file *m, struct
lock_stat_data *data)
seq_printf(m, "%40s:", name);
seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
+ seq_printf(m, "%14lu ", stats->bounces[bounce_hungry]);
seq_lock_time(m, &stats->write_waittime);
seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
seq_lock_time(m, &stats->write_holdtime);
@@ -584,11 +585,12 @@ static void seq_header(struct seq_file *m)
{
seq_printf(m, "lock_stat version 0.2\n");
seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
- seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
+ seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
"%14s %14s\n",
"class name",
"con-bounces",
- "contentions",
+ "con-hungry",
+ "contentions",
"waittime-min",
"waittime-max",
"waittime-total",
@@ -597,7 +599,7 @@ static void seq_header(struct seq_file *m)
"holdtime-min",
"holdtime-max",
"holdtime-total");
- seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
+ seq_line(m, '-', 0, 40 + 1 + 11 * (14 + 1));
seq_printf(m, "\n");}
lib/Kconfig.debug +3 -0
@@ -318,6 +318,9 @@ config RT_MUTEX_TESTER help This option enables a rt-mutex tester. +config HAVE_TICKET_SPINLOCK + bool + config DEBUG_SPINLOCK bool "Spinlock and rw-lock debugging: basic checks" depends on DEBUG_KERNEL
lib/spinlock_debug.c +1 -0
@@ -27,6 +27,7 @@ void __spin_lock_init(spinlock_t *lock, const char *name, lock->magic =SPINLOCK_MAGIC ; lock->owner =SPINLOCK_OWNER_INIT ; lock->owner_cpu =-1 ; + lock->dep_map.isticketspinlock =1 ; }EXPORT_SYMBOL (__spin_lock_init);
Quoted 8 lines · Yang Xi
Quoted 6 lines
I of course meant folding cpu and isspinlock into a combined bitfield (sorry for not being more clear), thereby saving space, this still takes 2*sizeof(int).
We can safely take some bits from the cpu number as there currently are no plans for a 2g cpu machine, right SGI? :-)
Thanks, ok, 31bits enough for cpu at now stage. 1bit for isticketspinlock. Here is the new one. Welcome more comments :).
Looks good, one more question :-)
Quoted 4 lines
include/linux/spinlock.h +0
-0@@ -127,6 +127,12 @@ do { \
include/linux/spinlock.h do { · line 137
Quoted 10 lines
include/linux/spinlock.h +6
-0#define spin_is_contended (lock) __raw_spin_is_contended(&(lock)->raw_lock)#endif +#ifdef CONFIG_HAVE_TICKET_SPINLOCK +#define spin_nr_contended (lock) __ticket_spin_nr_contended(&(lock)->raw_lock) +#else +#define spin_nr_contended (lock) (0) +#endif +
Does it make sense to make the alternative case return spin_is_contended()?
Quoted 20 lines · Peter Zijlstra
Quoted 8 lines · Yang Xi
Quoted 6 lines
I of course meant folding cpu and isspinlock into a combined bitfield (sorry for not being more clear), thereby saving space, this still takes 2*sizeof(int).
We can safely take some bits from the cpu number as there currently are no plans for a 2g cpu machine, right SGI? :-)
Thanks, ok, 31bits enough for cpu at now stage. 1bit for isticketspinlock. Here is the new one. Welcome more comments :).
Looks good, one more question :-)
Quoted 8 lines
include/linux/spinlock.h +0
-0@@ -127,6 +127,12 @@ do { \#define spin_is_contended (lock) __raw_spin_is_contended(&(lock)->raw_lock)#endif
Quoted 9 lines
include/linux/spinlock.h do { · line 137
Quoted 6 lines
include/linux/spinlock.h +6
-0+#ifdef CONFIG_HAVE_TICKET_SPINLOCK +#define spin_nr_contended (lock) __ticket_spin_nr_contended(&(lock)->raw_lock) +#else +#define spin_nr_contended (lock) (0) +#endif +Does it make sense to make the alternative case return
spin_is_contended()?
You mean do it like this?
#ifdef CONFIG_HAVE_TICKET_SPINLOCK
#define spin_nr_contended(lock) __ticket_spin_nr_contended(&(lock)->raw_lock) #else
#define spin_nr_contended(lock) (spin_is_contended(lock) ? 1: 0)
If we do it like this, for some architectures which donot have ticket spinlock, we can know that the percentage of contentions which involve more than two threads (con-hungry/ contention times). But i donot know whether there are some guys are interested in this number.
Best regards
Quoted 2 lines
*Add max-hungry to capture the max value of the number of threads who fight on a ticket lock
*For non-ticketlock implementation, spin_nr_contended(lock) is (spin_is_contended(lock) ? 1 : 0), which means "con-hungry" represent how many times there are more than two threads waiting for the spinlock to be free.
Because the lock-stat and lock-dep is so heavy, this statistic result is not very accurate :(
Here is the patch.
---
arch/x86/Kconfig | 1 + arch/x86/include/asm/spinlock.h | 7 +++++++ include/linux/lockdep.h | 5 ++++- include/linux/spinlock.h | 6 ++++++ kernel/lockdep.c | 24 +++++++++++++++++++++--- kernel/lockdep_proc.c | 10 +++++++--- lib/Kconfig.debug | 3 +++ lib/spinlock_debug.c | 1 + 8 files changed, 50 insertions(+), 7 deletions(-)
arch/x86/Kconfig +1 -0
@@ -34,6 +34,7 @@ config X86 select HAVE_ARCH_TRACEHOOK select HAVE_GENERIC_DMA_COHERENT if X86_32 select HAVE_EFFICIENT_UNALIGNED_ACCESS + select HAVE_TICKET_SPINLOCK config ARCH_DEFCONFIG string
arch/x86/include/asm/spinlock.h +0 -0
@@ -172,6 +172,13 @@ static inline int
__ticket_spin_is_contended(raw_spinlock_t *lock)
return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) > 1;
}
+static inline int __ticket_spin_nr_contended(raw_spinlock_t *lock) +{
+ int tmp = ACCESS_ONCE(lock->slock);
+
+ return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) + 1;
+}
+
#ifdef CONFIG_PARAVIRT
/*
* Define virtualization-friendly old-style lock byte lock, for use in
include/linux/lockdep.h +4 -1
@@ -136,6 +136,8 @@ enum bounce_type { bounce_acquired_read, bounce_contended_write, bounce_contended_read, + bounce_hungry, + bounce_max_hungry, nr_bounce_types, bounce_acquired = bounce_acquired_write,
@@ -164,7 +166,8 @@ struct lockdep_map {struct lock_class *class_cache;const char *name;#ifdef CONFIG_LOCK_STAT -int cpu; + unsigned int cpu:31; + unsigned int isticketspinlock:1;#endif };
include/linux/spinlock.h +6 -0
@@ -127,6 +127,12 @@ do { \#define spin_is_contended (lock) __raw_spin_is_contended(&(lock)->raw_lock)#endif +#ifdef CONFIG_HAVE_TICKET_SPINLOCK +#define spin_nr_contended (lock) __ticket_spin_nr_contended(&(lock)->raw_lock) +#else +#define spin_nr_contended (lock) (spin_is_contended(lock) ? 1 : 0) +#endif +/** * spin_unlock_wait - wait until the spinlock gets unlocked * @lock: the spinlock in question.
kernel/lockdep.c +0 -0
@@ -191,8 +191,14 @@ struct lock_class_stats lock_stats(struct
lock_class *class)
lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
- for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
+ for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) {
+ if (i == bounce_max_hungry) {
+ if (stats.bounces[i] < pcs->bounces[i])
+ stats.bounces[i] = pcs->bounces[i];
+ continue;
+ }
stats.bounces[i] += pcs->bounces[i];
+ }
}
return stats;
@@ -2588,7 +2594,6 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
if (check == 2 && !mark_irqflags(curr, hlock))
return 0;
-
/* mark it as used: */
if (!mark_lock(curr, hlock, LOCK_USED))
return 0;
@@ -2623,7 +2628,6 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
return 0;
-
curr->curr_chain_key = chain_key; curr->lockdep_depth++;
check_chain_key(curr);
@@ -3000,6 +3004,13 @@ __lock_contended(struct lockdep_map *lock, unsigned long ip)
struct lock_class_stats *stats;
unsigned int depth;
int i, point;
+ spinlock_t *lock_ptr;
+ unsigned long hungry = 0;
+
+ if (lock->isticketspinlock) {
+ lock_ptr = container_of(lock, spinlock_t, dep_map);
+ hungry = spin_nr_contended(lock_ptr);
+ }
depth = curr->lockdep_depth;
if (DEBUG_LOCKS_WARN_ON(!depth))
@@ -3030,9 +3041,16 @@ found_it:
stats->contention_point[point]++;
if (lock->cpu != smp_processor_id())
stats->bounces[bounce_contended + !!hlock->read]++;
+ stats->bounces[bounce_hungry] += hungry;
+ if (lock->isticketspinlock) {
+ if (stats->bounces[bounce_max_hungry] < hungry)
+ stats->bounces[bounce_max_hungry] = hungry;
+ }
+
put_lock_stats(stats);
}
+
static void
__lock_acquired(struct lockdep_map *lock)
{
kernel/lockdep_proc.c +0 -0
@@ -535,6 +535,8 @@ static void seq_stats(struct seq_file *m, struct
lock_stat_data *data)
seq_printf(m, "%40s:", name);
seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
+ seq_printf(m, "%14lu ", stats->bounces[bounce_hungry]);
+ seq_printf(m, "%14lu ", stats->bounces[bounce_max_hungry]);
seq_lock_time(m, &stats->write_waittime); seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]); seq_lock_time(m, &stats->write_holdtime);
@@ -583,11 +585,13 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
static void seq_header(struct seq_file *m)
{
seq_printf(m, "lock_stat version 0.2\n");
- seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
- seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
+ seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
+ seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
"%14s %14s\n",
"class name",
"con-bounces",
+ "con-hungry",
+ "max-hungry",
"contentions",
"waittime-min",
"waittime-max",
@@ -597,7 +601,7 @@ static void seq_header(struct seq_file *m)
"holdtime-min",
"holdtime-max",
"holdtime-total");
- seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
+ seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
seq_printf(m, "\n");
}
lib/Kconfig.debug +3 -0
@@ -318,6 +318,9 @@ config RT_MUTEX_TESTER help This option enables a rt-mutex tester. +config HAVE_TICKET_SPINLOCK + bool + config DEBUG_SPINLOCK bool "Spinlock and rw-lock debugging: basic checks" depends on DEBUG_KERNEL
lib/spinlock_debug.c +1 -0
@@ -27,6 +27,7 @@ void __spin_lock_init(spinlock_t *lock, const char *name, lock->magic =SPINLOCK_MAGIC ; lock->owner =SPINLOCK_OWNER_INIT ; lock->owner_cpu =-1 ; + lock->dep_map.isticketspinlock =1 ; }EXPORT_SYMBOL (__spin_lock_init);
Quoted 2 lines · Yang Xi
Because the lock-stat and lock-dep is so heavy, this statistic result is not very accurate :(
hm, lockdep indeed has to do some non-trivial work - but is that really true of pure lockstat too? If you have a workload where you can see that it's heavy, you could do an NMI profile on x86 by running kerneltop on tip/master:
http://redhat.com/~mingo/perfcounters/kerneltop.c
You should get a top-alike list of the highest-cost functions. If lockstat is heavy, its activities should show up there.
arch/x86/Kconfig config X86 · line 37
Quoted 5 lines
arch/x86/Kconfig +1
-0@@ -34,6 +34,7 @@ config X86 select HAVE_ARCH_TRACEHOOK select HAVE_GENERIC_DMA_COHERENT if X86_32 select HAVE_EFFICIENT_UNALIGNED_ACCESS + select HAVE_TICKET_SPINLOCK
no fundamental objections against your patch, but i think it needs a couple of cleanups first.
For example, this HAVE_TICKET_SPINLOCK distinction is unnecessarily exposed to the core kernel, why not just allow architectures to define spin_nr_contended():
include/linux/spinlock.h do { \ · line 134
Quoted 5 lines
include/linux/spinlock.h +5
-0+#ifdef CONFIG_HAVE_TICKET_SPINLOCK +#define spin_nr_contended (lock) __ticket_spin_nr_contended(&(lock)->raw_lock) +#else +#define spin_nr_contended (lock) (spin_is_contended(lock) ? 1 : 0) +#endif
and do something like this in spinlock.h to give a default definition:
#ifndef spin_nr_contended
# define spin_nr_contended(lock) (spin_is_contended(lock) ? 1 : 0) #endif
Review __lock_contended(struct lockdep_map *lock,
Quoted 19 lines
(quoted diff) +0
-1@@ -2588,7 +2594,6 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, if (check == 2 && !mark_irqflags(curr, hlock)) return 0; - /* mark it as used: */ if (!mark_lock(curr, hlock, LOCK_USED))(quoted diff) +0
-1return 0;@@ -2623,7 +2628,6 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, if (!validate_chain(curr, lock, hlock, chain_head, chain_key)) return 0; - curr->curr_chain_key = chain_key; curr->lockdep_depth++;(quoted diff) +0
-0check_chain_key(curr);@@ -3000,6 +3004,13 @@ __lock_contended(struct lockdep_map *lock,
those newlines you removed were there for a reason - they delimit blocks of code from each other and make return statements more visible.
Review __lock_contended(struct lockdep_map *lock, · line 3009
Quoted 6 lines
(quoted diff) +2
-0unsigned long ip) struct lock_class_stats *stats; unsigned int depth; int i, point; + spinlock_t *lock_ptr; + unsigned long hungry = 0;
please keep the local variable definitions in their original style, i.e. a reverse christmas tree:
Review __lock_contended(struct lockdep_map *lock,
Quoted 11 lines
(quoted diff) +7
-0struct lock_class_stats *stats; + unsigned long hungry = 0; + spinlock_t *lock_ptr; unsigned int depth; int i, point; + + if (lock->isticketspinlock) { + lock_ptr = container_of(lock, spinlock_t, dep_map); + hungry = spin_nr_contended(lock_ptr); + }
do we need the ->isticketspinlock distinction? Cannot we call spin_nr_contended() for all spinlocks? (it's just that for ticket spinlocks we get a real value out of it - for normal spinlocks we only get 0/1 out of it. But that is not a problem really.)
also, please rename 'hungry' to something more descriptive: for example 'nr_contended' fits pretty well?
Quoted 5 lines
(quoted diff) +1
-0@@ -3030,9 +3041,16 @@ found_it: stats->contention_point[point]++; if (lock->cpu != smp_processor_id()) stats->bounces[bounce_contended + !!hlock->read]++; + stats->bounces[bounce_hungry] += hungry;
Review found_it: · line 3054
Quoted 10 lines
(quoted diff) +6
-0+ if (lock->isticketspinlock) { + if (stats->bounces[bounce_max_hungry] < hungry) + stats->bounces[bounce_max_hungry] = hungry; + } + put_lock_stats(stats); } + static void
spurious newline.
Ingo
Quoted 3 lines
#ifndef
spin_nr_contended# define spin_nr_contended(lock) (spin_is_contended(lock) ? 1 : 0) #endif
And I change the spin_nr_contended to spin_nr_contender
Quoted 4 lines
do we need the ->isticketspinlock distinction? Cannot we call
spin_nr_contended()for all spinlocks? (it's just that for ticket spinlocks we get a real value out of it - for normal spinlocks we only get 0/1 out of it. But that is not a problem really.)
Not only the spin lock invokes lock_contended(). So we have to know the invoker is spin lock. I have changed the isticketspinlock to isspinlock to avoid the confusion.
Quoted 3 lines
also, please rename 'hungry' to something more descriptive: for example '
nr_contended' fits pretty well?
I changed hungry to contender. con-hungry to nr-contender, max hungry to max-contender.
Here is the patch
---arch/x86/include/asm/spinlock.h | 9 +++++++++
include/linux/lockdep.h | 5 ++++- include/linux/spinlock.h | 4 ++++ kernel/lockdep.c | 21 ++++++++++++++++++++- kernel/lockdep_proc.c | 10 +++++++--- lib/spinlock_debug.c | 1 + 6 files changed, 45 insertions(+), 5 deletions(-)
arch/x86/include/asm/spinlock.h +0 -0
@@ -172,6 +172,15 @@ static inline int
__ticket_spin_is_contended(raw_spinlock_t *lock)
return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) > 1;
}
+static inline int __ticket_spin_nr_contender(raw_spinlock_t *lock) +{
+ int tmp = ACCESS_ONCE(lock->slock);
+
+ return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) + 1;
+}
+
+#define spin_nr_contender(lock) __ticket_spin_nr_contender(&(lock)->raw_lock) +
#ifdef CONFIG_PARAVIRT
/*
* Define virtualization-friendly old-style lock byte lock, for use in
include/linux/lockdep.h +4 -1
@@ -136,6 +136,8 @@ enum bounce_type { bounce_acquired_read, bounce_contended_write, bounce_contended_read, + nr_contender, + max_contender, nr_bounce_types, bounce_acquired = bounce_acquired_write,
@@ -164,7 +166,8 @@ struct lockdep_map {struct lock_class *class_cache;const char *name;#ifdef CONFIG_LOCK_STAT -int cpu; + unsigned int cpu:32; + unsigned int isspinlock:1;#endif };
include/linux/spinlock.h +4 -0
@@ -127,6 +127,10 @@ do { \#define spin_is_contended (lock) __raw_spin_is_contended(&(lock)->raw_lock)#endif +#ifndef spin_nr_contender +#define spin_nr_contender (lock) (spin_is_contended(lock) ? 1 : 0) +#endif +/** * spin_unlock_wait - wait until the spinlock gets unlocked * @lock: the spinlock in question.
kernel/lockdep.c +0 -0
@@ -191,8 +191,14 @@ struct lock_class_stats lock_stats(struct
lock_class *class)
lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
- for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
+ for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) {
+ if (i == max_contender) {
+ if (stats.bounces[i] < pcs->bounces[i])
+ stats.bounces[i] = pcs->bounces[i];
+ continue;
+ }
stats.bounces[i] += pcs->bounces[i];
+ }
}
return stats;
@@ -2998,9 +3004,16 @@ __lock_contended(struct lockdep_map *lock, unsigned long ip)
struct task_struct *curr = current;
struct held_lock *hlock, *prev_hlock;
struct lock_class_stats *stats;
+ unsigned long contender;
+ spinlock_t *lock_ptr;
unsigned int depth;
int i, point;
+ if (lock->isspinlock) {
+ lock_ptr = container_of(lock, spinlock_t, dep_map);
+ contender = spin_nr_contender(lock_ptr);
+ }
+
depth = curr->lockdep_depth;
if (DEBUG_LOCKS_WARN_ON(!depth))
return;
@@ -3030,6 +3043,12 @@ found_it:
stats->contention_point[point]++;
if (lock->cpu != smp_processor_id())
stats->bounces[bounce_contended + !!hlock->read]++;
+ stats->bounces[nr_contender] += contender;
+ if (lock->isspinlock) {
+ if (stats->bounces[max_contender] < contender)
+ stats->bounces[max_contender] = contender;
+ }
+
put_lock_stats(stats);
}
kernel/lockdep_proc.c +0 -0
@@ -535,6 +535,8 @@ static void seq_stats(struct seq_file *m, struct
lock_stat_data *data)
seq_printf(m, "%40s:", name);
seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
+ seq_printf(m, "%14lu ", stats->bounces[nr_contender]);
+ seq_printf(m, "%14lu ", stats->bounces[max_contender]);
seq_lock_time(m, &stats->write_waittime); seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]); seq_lock_time(m, &stats->write_holdtime);
@@ -583,11 +585,13 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
static void seq_header(struct seq_file *m)
{
seq_printf(m, "lock_stat version 0.2\n");
- seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
- seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
+ seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
+ seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
"%14s %14s\n",
"class name",
"con-bounces",
+ "nr-contender",
+ "max-contender",
"contentions",
"waittime-min",
"waittime-max",
@@ -597,7 +601,7 @@ static void seq_header(struct seq_file *m)
"holdtime-min",
"holdtime-max",
"holdtime-total");
- seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
+ seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
seq_printf(m, "\n");
}
lib/spinlock_debug.c +1 -0
@@ -27,6 +27,7 @@ void __spin_lock_init(spinlock_t *lock, const char *name, lock->magic =SPINLOCK_MAGIC ; lock->owner =SPINLOCK_OWNER_INIT ; lock->owner_cpu =-1 ; + lock->dep_map.isspinlock =1 ; }EXPORT_SYMBOL (__spin_lock_init);