enhance lock debugging
v4 cover enhance lock debugging
While hunting a locking problem in my core scheduling series I have added some debugging aids to spinlock handling making it easier to find the root cause for the problem.
Making use of the already existing lock profiling and enhancing it a little bit produces some really valuable diagnostic data e.g. when a NMI watchdog is triggering a crash.
Changes in V4:
- some comments by Jan Beulich addressed
- replaced patch 5 with another approach to make lock names unique
Changes in V3:
- rebase to current staging (after realizing that patch 4 still applied, but resulting in patching a wrong function)
Changes in V2:
- multiple comments addressed
- added patch 5
Juergen Gross (5):
xen/spinlocks: in debug builds store cpu holding the lock
xen: add new CONFIG_DEBUG_LOCKS option
xen: print lock profile info in panic()
xen: modify lock profiling interface
xen: add function name to lock profiling data
tools/libxc/xc_misc.c | 1 + tools/misc/xenlockprof.c | 17 +--- xen/Kconfig.debug | 10 ++- xen/arch/arm/xen.lds.S | 13 ++-- xen/arch/x86/domain.c | 2 +- xen/arch/x86/xen.lds.S | 13 ++-- xen/common/domain.c | 4 +- xen/common/keyhandler.c | 2 +- xen/common/spinlock.c | 185 ++++++++++++++++++++++++++++++++------------ xen/common/sysctl.c | 2 +- xen/drivers/char/console.c | 4 +- xen/include/public/sysctl.h | 11 +-- xen/include/xen/spinlock.h | 75 +++++++++++------- 13 files changed, 221 insertions(+), 118 deletions(-)
Signature · 7 lines
2.16.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 1/5 xen/spinlocks: in debug builds store cpu holding the lock
Add the cpu currently holding the lock to struct lock_debug. This makes analysis of locking errors easier and it can be tested whether the correct cpu is releasing a lock again.
---
V2:
- adjust types (Jan Beulich)
V4:
- add define for bitfield size to store cpu number (Jan Beulich)
- make padding field unnamed (Jan Beulich)
--- xen/common/spinlock.c | 33 ++++++++++++++++++++++++++------- xen/include/xen/spinlock.h | 28 +++++++++++++++++++--------- 2 files changed, 45 insertions(+), 16 deletions(-)
xen/common/spinlock.c +26 -7
@@ -13,9 +13,9 @@static atomic_t spin_debug__read_mostly =ATOMIC_INIT (0 ); -static void check_lock (struct lock_debug *debug) +static void check_lock (union lock_debug *debug) { -int irq_safe = !local_irq_is_enabled (); +bool irq_safe = !local_irq_is_enabled ();if (unlikely (atomic_read (&spin_debug) <=0 ) )return ;
@@ -43,18 +43,21 @@ static void check_lock(struct lock_debug *debug)*/ if (unlikely (debug->irq_safe != irq_safe) ) { -int seen =cmpxchg (&debug->irq_safe,-1 , irq_safe); +union lock_debug seen, new = {0 }; -if ( seen == !irq_safe ) + new.irq_safe = irq_safe; + seen.val =cmpxchg (&debug->val,LOCK_DEBUG_INITVAL , new.val); + +if ( !seen.unseen && seen.irq_safe == !irq_safe ) {printk ("CHECKLOCK FAILURE: prev irqsafe: %d, curr irqsafe %d\n" , - seen, irq_safe); + seen.irq_safe, irq_safe);BUG (); } } } -static void check_barrier (struct lock_debug *debug) +static void check_barrier (union lock_debug *debug) {if (unlikely (atomic_read (&spin_debug) <=0 ) )return ;
@@ -70,7 +73,18 @@ static void check_barrier(struct lock_debug *debug)* However, if we spin on an IRQ-unsafe lock with IRQs disabled then that * is clearly wrong, for the same reason outlined in check_lock() above. */ -BUG_ON (!local_irq_is_enabled () && (debug->irq_safe ==0 )); +BUG_ON (!local_irq_is_enabled () && !debug->irq_safe); +} + +static void got_lock (union lock_debug *debug) +{ + debug->cpu =smp_processor_id (); +} + +static void rel_lock (union lock_debug *debug) +{ +ASSERT (debug->cpu ==smp_processor_id ()); + debug->cpu =SPINLOCK_NO_CPU ; }void spin_debug_enable (void )
@@ -87,6 +101,8 @@ void spin_debug_disable(void)#define check_lock (l) ((void)0)#define check_barrier (l) ((void)0) +#define got_lock (l) ((void)0) +#define rel_lock (l) ((void)0)#endif
@@ -150,6 +166,7 @@ void inline _spin_lock_cb(spinlock_t *lock, void (*cb)(void *), void *data)cb (data);arch_lock_relax (); } +got_lock (&lock->debug);LOCK_PROFILE_GOT ;preempt_disable ();arch_lock_acquire_barrier ();
@@ -181,6 +198,7 @@ void _spin_unlock(spinlock_t *lock)arch_lock_release_barrier ();preempt_enable ();LOCK_PROFILE_REL ; +rel_lock (&lock->debug);add_sized (&lock->tickets.head,1 );arch_lock_signal (); }
@@ -224,6 +242,7 @@ int _spin_trylock(spinlock_t *lock)if (cmpxchg (&lock->tickets.head_tail, old.head_tail, new.head_tail) != old.head_tail )return 0 ; +got_lock (&lock->debug);#ifdef CONFIG_LOCK_PROFILE if (lock->profile) lock->profile->time_locked =NOW ();
xen/include/xen/spinlock.h +19 -9
@@ -5,15 +5,24 @@#include <asm/spinlock.h> #include <asm/types.h> +#define SPINLOCK_CPU_BITS 12 +#ifndef NDEBUG -struct lock_debug { -s16 irq_safe;/* +1: IRQ-safe; 0: not IRQ-safe; -1: don't know yet */ +union lock_debug { +uint16_t val; +#define LOCK_DEBUG_INITVAL 0xffff +struct { +uint16_t cpu:SPINLOCK_CPU_BITS ; + uint16_t :(14 - SPINLOCK_CPU_BITS); +bool irq_safe:1 ; +bool unseen:1 ; + }; }; -#define _LOCK_DEBUG { -1 } +#define _LOCK_DEBUG { LOCK_DEBUG_INITVAL }void spin_debug_enable (void );void spin_debug_disable (void );#else -struct lock_debug { }; +union lock_debug { };#define _LOCK_DEBUG { }#define spin_debug_enable () ((void)0)#define spin_debug_disable () ((void)0)
@@ -138,11 +147,12 @@ typedef union { typedef struct spinlock { spinlock_tickets_t tickets; - u16 recurse_cpu:12; -#define SPINLOCK_NO_CPU 0xfffu - u16 recurse_cnt:4; -#define SPINLOCK_MAX_RECURSE 0xfu - struct lock_debug debug; + u16 recurse_cpu:SPINLOCK_CPU_BITS; +#define SPINLOCK_NO_CPU ((1u << SPINLOCK_CPU_BITS) - 1) +#define SPINLOCK_RECURSE_BITS (16 - SPINLOCK_CPU_BITS) + u16 recurse_cnt:SPINLOCK_RECURSE_BITS; +#define SPINLOCK_MAX_RECURSE ((1u << SPINLOCK_RECURSE_BITS) - 1) + union lock_debug debug; #ifdef CONFIG_LOCK_PROFILE struct lock_profile *profile; #endif
Signature · 7 lines
2.16.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH v4 1/5] xen/spinlocks: in debug builds store cpu holding the lock
Quoted 7 lines · Juergen Gross
xen/include/xen/spinlock.h +1
-0@@ -5,15 +5,24 @@#include <asm/spinlock.h> #include <asm/types.h> +#define SPINLOCK_CPU_BITS 12
xen/include/xen/spinlock.h line 16
Quoted 10 lines
xen/include/xen/spinlock.h +7
-2+ #ifndef NDEBUG -struct lock_debug { - s16 irq_safe; /* +1: IRQ-safe; 0: not IRQ-safe; -1: don't know yet */ +union lock_debug { + uint16_t val; +#define LOCK_DEBUG_INITVAL 0xffff + struct { + uint16_t cpu:SPINLOCK_CPU_BITS; + uint16_t :(14 - SPINLOCK_CPU_BITS);
I'm sorry that I realize this only now that I see this and ...
Quoted 18 lines
xen/include/xen/spinlock.h +5
-2+ bool irq_safe:1; + bool unseen:1; + }; }; -#define _LOCK_DEBUG { -1 } +#define _LOCK_DEBUG { LOCK_DEBUG_INITVAL }void spin_debug_enable (void );void spin_debug_disable (void );#else -struct lock_debug { }; +union lock_debug { };#define _LOCK_DEBUG { }#define spin_debug_enable () ((void)0)#define spin_debug_disable () ((void)0)@@ -138,11 +147,12 @@ typedef union { typedef struct spinlock { spinlock_tickets_t tickets;
xen/include/xen/spinlock.h typedef union { · line 154
Quoted 10 lines
xen/include/xen/spinlock.h +5
-5- u16 recurse_cpu:12; -#define SPINLOCK_NO_CPU 0xfffu - u16 recurse_cnt:4; -#define SPINLOCK_MAX_RECURSE 0xfu -struct lock_debug debug; + u16 recurse_cpu:SPINLOCK_CPU_BITS; +#define SPINLOCK_NO_CPU ((1u << SPINLOCK_CPU_BITS) - 1) +#define SPINLOCK_RECURSE_BITS (16 - SPINLOCK_CPU_BITS) + u16 recurse_cnt:SPINLOCK_RECURSE_BITS; +#define SPINLOCK_MAX_RECURSE ((1u << SPINLOCK_RECURSE_BITS) - 1)
... this: These subtractions are prone to yield de-generate bitfields when the difference ends up zero (leading to presumably very strange breakage, albeit one would hope that it also would be very obvious that _something_ is broken). I think we need BUILD_BUG_ON()s checking that neither difference actually is zero.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH v4 1/5] xen/spinlocks: in debug builds store cpu holding the lock
Quoted 48 lines · Jan Beulich
Quoted 7 lines · Juergen Gross
xen/include/xen/spinlock.h +1
-0@@ -5,15 +5,24 @@#include <asm/spinlock.h> #include <asm/types.h> +#define SPINLOCK_CPU_BITS 12xen/include/xen/spinlock.h line 16
Quoted 10 lines
xen/include/xen/spinlock.h +7
-2+ #ifndef NDEBUG -struct lock_debug { - s16 irq_safe; /* +1: IRQ-safe; 0: not IRQ-safe; -1: don't know yet */ +union lock_debug { + uint16_t val; +#define LOCK_DEBUG_INITVAL 0xffff + struct { + uint16_t cpu:SPINLOCK_CPU_BITS; + uint16_t :(14 - SPINLOCK_CPU_BITS);I'm sorry that I realize this only now that I see this and ...
Quoted 27 lines
xen/include/xen/spinlock.h +9
-7+ bool irq_safe:1; + bool unseen:1; + }; }; -#define _LOCK_DEBUG { -1 } +#define _LOCK_DEBUG { LOCK_DEBUG_INITVAL }void spin_debug_enable (void );void spin_debug_disable (void );#else -struct lock_debug { }; +union lock_debug { };#define _LOCK_DEBUG { }#define spin_debug_enable () ((void)0)#define spin_debug_disable () ((void)0)@@ -138,11 +147,12 @@ typedef union { typedef struct spinlock { spinlock_tickets_t tickets; - u16 recurse_cpu:12; -#define SPINLOCK_NO_CPU 0xfffu - u16 recurse_cnt:4; -#define SPINLOCK_MAX_RECURSE 0xfu - struct lock_debug debug; + u16 recurse_cpu:SPINLOCK_CPU_BITS; +#define SPINLOCK_NO_CPU ((1u << SPINLOCK_CPU_BITS) - 1) +#define SPINLOCK_RECURSE_BITS (16 - SPINLOCK_CPU_BITS) + u16 recurse_cnt:SPINLOCK_RECURSE_BITS;
Quoted 8 lines
xen/include/xen/spinlock.h typedef union { · line 154
Quoted 1 line
xen/include/xen/spinlock.h +1
-0+#define SPINLOCK_MAX_RECURSE ((1u << SPINLOCK_RECURSE_BITS) - 1)... this: These subtractions are prone to yield de-generate bitfields when the difference ends up zero (leading to presumably very strange breakage, albeit one would hope that it also would be very obvious that _something_ is broken). I think we need BUILD_BUG_ON()s checking that neither difference actually is zero.
Okay, will add them.
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 2/5 xen: add new CONFIG_DEBUG_LOCKS option
In reply to Juergen GrossInstead of enabling debugging for debug builds only add a dedicated Kconfig option for that purpose which defaults to DEBUG.
---
V2:
- rename to CONFIG_DEBUG_LOCKS (Jan Beulich)
--- xen/Kconfig.debug | 7 +++++++ xen/common/spinlock.c | 4 ++-- xen/include/xen/spinlock.h | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-)
xen/Kconfig.debug +7 -0
@@ -51,6 +51,13 @@ config LOCK_PROFILE You can use serial console to print (and reset) using 'l' and 'L' respectively, or the 'xenlockprof' tool. +config DEBUG_LOCKS + bool "Lock debugging" + default DEBUG + ---help--- + Enable debugging features of lock handling. Some additional + checks will be performed when acquiring and releasing locks. + config PERF_COUNTERS bool "Performance Counters" ---help---
xen/common/spinlock.c +2 -2
@@ -9,7 +9,7 @@#include <asm/processor.h> #include <asm/atomic.h> -#ifndef NDEBUG +#ifdef CONFIG_DEBUG_LOCKS static atomic_t spin_debug__read_mostly =ATOMIC_INIT (0 );
@@ -97,7 +97,7 @@ void spin_debug_disable(void) atomic_dec(&spin_debug); } -#else /* defined(NDEBUG) */ +#else /* CONFIG_DEBUG_LOCKS */ #define check_lock (l) ((void)0)#define check_barrier (l) ((void)0)
xen/include/xen/spinlock.h +1 -1
@@ -7,7 +7,7 @@#define SPINLOCK_CPU_BITS 12 -#ifndef NDEBUG +#ifdef CONFIG_DEBUG_LOCKS union lock_debug { uint16_t val; #define LOCK_DEBUG_INITVAL 0xffff
Signature · 7 lines
2.16.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 3/5 xen: print lock profile info in panic()
In reply to Juergen GrossPrint the lock profile data when the system crashes and add some more information for each lock data (lock address, cpu holding the lock). While at it use the PRI_stime format specifier for printing time data.
This is especially beneficial for watchdog triggered crashes in case of deadlocks.
In order to have the cpu holding the lock available let the lock profile config option select DEBUG_LOCKS.
As printing the lock profile data will make use of locking, too, we need to disable spinlock debugging before calling spinlock_profile_printall() from panic().
While at it remove a superfluous #ifdef CONFIG_LOCK_PROFILE and rename CONFIG_LOCK_PROFILE to CONFIG_DEBUG_LOCK_PROFILE.
Also move the .lockprofile.data section to init area in linker scripts as the data is no longer needed after boot.
---
V2:
- rename CONFIG_LOCK_PROFILE to CONFIG_DEBUG_LOCK_PROFILE (Jan Beulich)
- move .lockprofile.data section to init area in linker scripts
- use PRI_stime (Andrew Cooper)
- don't print "cpu=4095", but "not locked" (Andrew Cooper)
--- xen/Kconfig.debug | 3 ++- xen/arch/arm/xen.lds.S | 13 +++++++------ xen/arch/x86/domain.c | 2 +- xen/arch/x86/xen.lds.S | 13 +++++++------ xen/common/keyhandler.c | 2 +- xen/common/spinlock.c | 33 ++++++++++++++++++--------------- xen/common/sysctl.c | 2 +- xen/drivers/char/console.c | 4 +++- xen/include/xen/spinlock.h | 12 +++++++----- 9 files changed, 47 insertions(+), 37 deletions(-)
xen/Kconfig.debug +2 -1
@@ -44,8 +44,9 @@ config COVERAGE If unsure, say N here. -config LOCK_PROFILE +config DEBUG_LOCK_PROFILE bool "Lock Profiling" + select DEBUG_LOCKS ---help--- Lock profiling allows you to see how often locks are taken and blocked. You can use serial console to print (and reset) using 'l' and 'L'
xen/arch/arm/xen.lds.S +7 -6
@@ -54,12 +54,6 @@ SECTIONS *(.data.rel.ro) *(.data.rel.ro.*) -#ifdef CONFIG_LOCK_PROFILE - . = ALIGN(POINTER_ALIGN); - __lock_profile_start = .; - *(.lockprofile.data) - __lock_profile_end = .; -#endif . = ALIGN(POINTER_ALIGN); __param_start = .; *(.data.param)
@@ -173,6 +167,13 @@ SECTIONS . = ALIGN(4); *(.altinstr_replacement) +#ifdef CONFIG_DEBUG_LOCK_PROFILE + . = ALIGN(POINTER_ALIGN); + __lock_profile_start = .; + *(.lockprofile.data) + __lock_profile_end = .; +#endif + *(.init.data) *(.init.data.rel) *(.init.data.rel.*)
xen/arch/x86/domain.c +1 -1
@@ -305,7 +305,7 @@ struct domain *alloc_domain_struct(void)#endif -#ifndef CONFIG_LOCK_PROFILE +#ifndef CONFIG_DEBUG_LOCK_PROFILE BUILD_BUG_ON (sizeof (*d) >PAGE_SIZE );#endif d =alloc_xenheap_pages (order,MEMF_bits (bits));
xen/arch/x86/xen.lds.S +7 -6
@@ -128,12 +128,6 @@ SECTIONS *(.ex_table.pre) __stop___pre_ex_table = .; -#ifdef CONFIG_LOCK_PROFILE - . = ALIGN(POINTER_ALIGN); - __lock_profile_start = .; - *(.lockprofile.data) - __lock_profile_end = .; -#endif . = ALIGN(POINTER_ALIGN); __param_start = .; *(.data.param)
@@ -251,6 +245,13 @@ SECTIONS *(.altinstructions) __alt_instructions_end = .; +#ifdef CONFIG_DEBUG_LOCK_PROFILE + . = ALIGN(POINTER_ALIGN); + __lock_profile_start = .; + *(.lockprofile.data) + __lock_profile_end = .; +#endif + . = ALIGN(8); __ctors_start = .; *(.ctors)
xen/common/keyhandler.c +1 -1
@@ -62,7 +62,7 @@ static struct keyhandler { KEYHANDLER('P', perfc_reset, "reset performance counters", 0),#endif -#ifdef CONFIG_LOCK_PROFILE +#ifdef CONFIG_DEBUG_LOCK_PROFILE KEYHANDLER('l', spinlock_profile_printall, "print lock profile info", 1), KEYHANDLER('L', spinlock_profile_reset, "reset lock profile info", 0),#endif
xen/common/spinlock.c +18 -15
@@ -106,7 +106,7 @@ void spin_debug_disable(void)#endif -#ifdef CONFIG_LOCK_PROFILE +#ifdef CONFIG_DEBUG_LOCK_PROFILE #define LOCK_PROFILE_REL \ if (lock->profile) \
@@ -243,7 +243,7 @@ int _spin_trylock(spinlock_t *lock) old.head_tail, new.head_tail) != old.head_tail )return 0 ;got_lock (&lock->debug); -#ifdef CONFIG_LOCK_PROFILE +#ifdef CONFIG_DEBUG_LOCK_PROFILE if (lock->profile) lock->profile->time_locked =NOW ();#endif
@@ -258,7 +258,7 @@ int _spin_trylock(spinlock_t *lock)void _spin_barrier (spinlock_t *lock) {spinlock_tickets_t sample; -#ifdef CONFIG_LOCK_PROFILE +#ifdef CONFIG_DEBUG_LOCK_PROFILE s_time_t block =NOW ();#endif
@@ -269,7 +269,7 @@ void _spin_barrier(spinlock_t *lock) {while (observe_head (&lock->tickets) == sample.head )arch_lock_relax (); -#ifdef CONFIG_LOCK_PROFILE +#ifdef CONFIG_DEBUG_LOCK_PROFILE if ( lock->profile ) { lock->profile->time_block +=NOW () - block;
@@ -327,7 +327,7 @@ void _spin_unlock_recursive(spinlock_t *lock) } } -#ifdef CONFIG_LOCK_PROFILE +#ifdef CONFIG_DEBUG_LOCK_PROFILE struct lock_profile_anc { struct lock_profile_qhead *head_q; /* first head of this type */
@@ -362,14 +362,19 @@ static void spinlock_profile_iterate(lock_profile_subfunc *sub, void *par)static void spinlock_profile_print_elem (struct lock_profile *data,int32_t type,int32_t idx,void *par) { -if ( type ==LOCKPROF_TYPE_GLOBAL ) -printk ("%s %s:\n" , lock_profile_ancs[type].name, data->name); +struct spinlock *lock = data->lock; + +printk ("%s " , lock_profile_ancs[type].name); +if ( type !=LOCKPROF_TYPE_GLOBAL ) +printk ("%d " , idx); +printk ("%s: addr=%p, lockval=%08x, " , data->name, lock, + lock->tickets.head_tail); +if ( lock->debug.cpu ==SPINLOCK_NO_CPU ) +printk ("not locked\n" );else -printk ("%s %d %s:\n" , lock_profile_ancs[type].name, idx, data->name); -printk (" lock:%12" PRId64"(%08X:%08X), block:%12" PRId64"(%08X:%08X)\n" , - data->lock_cnt, (u32)(data->time_hold >>32 ), (u32 )data->time_hold, - data->block_cnt, (u32)(data->time_block >>32 ), - (u32 )data->time_block); +printk ("cpu=%d\n" , lock->debug.cpu); +printk (" lock:%" PRId64"(%" PRI_stime"), block:%" PRId64"(%" PRI_stime")\n" , + data->lock_cnt, data->time_hold, data->block_cnt, data->time_block); }void spinlock_profile_printall (unsigned char key)
@@ -488,7 +493,6 @@ void _lock_profile_deregister_struct(spin_unlock (&lock_profile_lock); } -#ifdef CONFIG_LOCK_PROFILE static int __init lock_prof_init (void ) {struct lock_profile **q;
@@ -507,6 +511,5 @@ static int __init lock_prof_init(void) return 0; }__initcall (lock_prof_init); -#endif -#endif /* LOCK_PROFILE */ +#endif /* CONFIG_DEBUG_LOCK_PROFILE */
xen/common/sysctl.c +1 -1
@@ -119,7 +119,7 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)break ;#endif -#ifdef CONFIG_LOCK_PROFILE +#ifdef CONFIG_DEBUG_LOCK_PROFILE case XEN_SYSCTL_lockprof_op: ret =spinlock_profile_control (&op->u.lockprof_op);break ;
xen/drivers/char/console.c +3 -1
@@ -1170,7 +1170,9 @@ void panic(const char *fmt, ...)unsigned long flags;static DEFINE_SPINLOCK (lock);static char buf[128 ]; - + +spin_debug_disable (); +spinlock_profile_printall ('\0' );debugtrace_dump ();/* Protects buf[] and ensure multi-line message prints atomically. */
xen/include/xen/spinlock.h +7 -5
@@ -1,6 +1,7 @@#ifndef __SPINLOCK_H__#define __SPINLOCK_H__ +#include <xen/time.h> #include <asm/system.h> #include <asm/spinlock.h> #include <asm/types.h>
@@ -28,7 +29,7 @@ union lock_debug { };#define spin_debug_disable () ((void)0)#endif -#ifdef CONFIG_LOCK_PROFILE +#ifdef CONFIG_DEBUG_LOCK_PROFILE #include <public/sysctl.h>
@@ -74,9 +75,9 @@ struct lock_profile {struct spinlock *lock;/* the lock itself */ u64 lock_cnt;/* # of complete locking ops */ u64 block_cnt;/* # of complete wait for lock */ -s64 time_hold;/* cumulated lock time */ -s64 time_block;/* cumulated wait time */ -s64 time_locked;/* system time of last locking */ +s_time_t time_hold;/* cumulated lock time */ +s_time_t time_block;/* cumulated wait time */ +s_time_t time_locked;/* system time of last locking */ };struct lock_profile_qhead {
@@ -132,6 +133,7 @@ struct lock_profile_qhead { };#define spin_lock_init_prof (s, l) spin_lock_init(&((s)->l))#define lock_profile_register_struct (type, ptr, idx, print)#define lock_profile_deregister_struct (type, ptr) +#define spinlock_profile_printall (key)#endif
@@ -153,7 +155,7 @@ typedef struct spinlock { u16 recurse_cnt:SPINLOCK_RECURSE_BITS;#define SPINLOCK_MAX_RECURSE ((1u << SPINLOCK_RECURSE_BITS) - 1)union lock_debug debug; -#ifdef CONFIG_LOCK_PROFILE +#ifdef CONFIG_DEBUG_LOCK_PROFILE struct lock_profile *profile;#endif } spinlock_t;
Signature · 7 lines
2.16.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 4/5 xen: modify lock profiling interface
In reply to Juergen GrossToday adding locks located in a struct to lock profiling requires a unique type index for each structure. This makes it hard to add any new structure as the related sysctl interface needs to be changed, too.
Instead of using an index the already existing struct name specified in lock_profile_register_struct() can be used as an identifier instead.
Modify the sysctl interface to use the struct name instead of the type index and adapt the related coding accordingly. Instead of an array of struct anchors for lock profiling we now use a linked list for that purpose. Use the special idx value -1 for cases where the idx isn't relevant (global locks) and shouldn't be printed.
Increment the sysctl.h interface version as an interface is being modified.
Add the missing setting of profiling time to xc_lockprof_query().
Add freeing element data when deregistering a structure.
---
V2:
- add comment about memory allocation needs (Jan Beulich)
- check success of memory allocation (Jan Beulich) V4:
- style correction (Jan Beulich)
- avoid memory allocation while holding lock (Jan Beulich)
--- tools/libxc/xc_misc.c | 1 + tools/misc/xenlockprof.c | 17 ++----- xen/common/domain.c | 4 +- xen/common/spinlock.c | 107 ++++++++++++++++++++++++++++++++------------ xen/include/public/sysctl.h | 11 ++--- xen/include/xen/spinlock.h | 29 +++++++----- 6 files changed, 107 insertions(+), 62 deletions(-)
tools/libxc/xc_misc.c +1 -0
@@ -533,6 +533,7 @@ int xc_lockprof_query(xc_interface *xch, rc =do_sysctl (xch, &sysctl); *n_elems = sysctl.u.lockprof_op.nr_elem; + *time = sysctl.u.lockprof_op.time;return rc; }
tools/misc/xenlockprof.c +4 -13
@@ -88,19 +88,10 @@ int main(int argc, char *argv[]) sb =0 ;for ( j =0 ; j < i; j++ ) { -switch ( data[j].type ) - { -case LOCKPROF_TYPE_GLOBAL : -sprintf (name,"global lock %s" , data[j].name); -break ; -case LOCKPROF_TYPE_PERDOM : -sprintf (name,"domain %d lock %s" , data[j].idx, data[j].name); -break ; -default : -sprintf (name,"unknown type(%d) %d lock %s" , data[j].type, - data[j].idx, data[j].name); -break ; - } +if ( data[j].idx ==LOCKPROF_IDX_NONE ) +sprintf (name,"%s %s" , data[j].type, data[j].name); +else +sprintf (name,"%s %d %s" , data[j].type, data[j].idx, data[j].name); l = (double )(data[j].lock_time) /1E+09 ; b = (double )(data[j].block_time) /1E+09 ; sl += l;
xen/common/domain.c +2 -2
@@ -290,7 +290,7 @@ static void _domain_destroy(struct domain *d)xsm_free_security_domain (d); -lock_profile_deregister_struct (LOCKPROF_TYPE_PERDOM , d); +lock_profile_deregister_struct (d,"Domain" );free_domain_struct (d); }
@@ -376,7 +376,7 @@ struct domain *domain_create(domid_t domid, d->max_vcpus = config->max_vcpus; } -lock_profile_register_struct (LOCKPROF_TYPE_PERDOM , d, domid,"Domain" ); +lock_profile_register_struct (d, domid,"Domain" );if ( (err =xsm_alloc_security_domain (d)) !=0 ) goto fail;
xen/common/spinlock.c +79 -28
182 lines not on this page · raw message
xen/include/public/sysctl.h +4 -7
@@ -35,7 +35,7 @@#include "domctl.h" #include "physdev.h" -#define XEN_SYSCTL_INTERFACE_VERSION 0x00000012 +#define XEN_SYSCTL_INTERFACE_VERSION 0x00000013/* * Read console content from Xen buffer ring.
@@ -433,14 +433,11 @@ struct xen_sysctl_page_offline_op {/* Sub-operations: */ #define XEN_SYSCTL_LOCKPROF_reset 1/* Reset all profile data to zero. */ #define XEN_SYSCTL_LOCKPROF_query 2/* Get lock profile information. */ -/* Record-type: */ -#define LOCKPROF_TYPE_GLOBAL 0/* global lock, idx meaningless */ -#define LOCKPROF_TYPE_PERDOM 1/* per-domain lock, idx is domid */ -#define LOCKPROF_TYPE_N 2/* number of types */ struct xen_sysctl_lockprof_data { -char name[40 ];/* lock name (may include up to 2 %d specifiers) */ -int32_t type;/* LOCKPROF_TYPE_??? */ +char name[40 ];/* lock name */ +char type[20 ];/* e.g. "domain" */ int32_t idx;/* index (e.g. domain id) */ +#define LOCKPROF_IDX_NONE -1uint64_aligned_t lock_cnt;/* # of locking succeeded */ uint64_aligned_t block_cnt;/* # of wait for lock */ uint64_aligned_t lock_time;/* nsecs lock held */
xen/include/xen/spinlock.h +17 -12
@@ -52,19 +52,24 @@ union lock_debug { };with ptr being the main structure pointer and lock the spinlock field +It should be noted that this will need to allocate memory, so interrupts +must be enabled. +- each structure has to be added to profiling with -lock_profile_register_struct(type, ptr, idx, print); +lock_profile_register_struct(ptr, idx, print); with: -type: something like LOCKPROF_TYPE_PERDOM ptr: pointer to the structure idx: index of that structure, e.g. domid print: descriptive string like "domain" +It should be noted that this will might need to allocate memory, so +interrupts must be enabled. +- removing of a structure is done via -lock_profile_deregister_struct(type, ptr); +lock_profile_deregister_struct(ptr, print); */ struct spinlock ;
@@ -110,14 +115,14 @@ struct lock_profile_qhead { (s)->profile_head.elem_q = prof; \ }while (0 ) -void _lock_profile_register_struct( - int32_t, struct lock_profile_qhead *, int32_t, char *); -void _lock_profile_deregister_struct (int32_t ,struct lock_profile_qhead *); +void _lock_profile_register_struct(struct lock_profile_qhead *, int32_t, +const char *); +void _lock_profile_deregister_struct (struct lock_profile_qhead *,const char *); -#define lock_profile_register_struct (type, ptr, idx, print) \ - _lock_profile_register_struct(type, &((ptr)->profile_head), idx, print) -#define lock_profile_deregister_struct (type, ptr) \ - _lock_profile_deregister_struct(type, &((ptr)->profile_head)) +#define lock_profile_register_struct (ptr, idx, print) \ + _lock_profile_register_struct(&((ptr)->profile_head), idx, print) +#define lock_profile_deregister_struct (ptr, print) \ + _lock_profile_deregister_struct(&((ptr)->profile_head), print)extern int spinlock_profile_control (struct xen_sysctl_lockprof_op *pc);extern void spinlock_profile_printall (unsigned char key);
@@ -131,8 +136,8 @@ struct lock_profile_qhead { };#define DEFINE_SPINLOCK (l) spinlock_t l = SPIN_LOCK_UNLOCKED#define spin_lock_init_prof (s, l) spin_lock_init(&((s)->l)) -#define lock_profile_register_struct (type, ptr, idx, print) -#define lock_profile_deregister_struct (type, ptr) +#define lock_profile_register_struct (ptr, idx, print) +#define lock_profile_deregister_struct (ptr, print)#define spinlock_profile_printall (key)#endif
Signature · 7 lines
2.16.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 5/5 xen: add function name to lock profiling data
In reply to Juergen GrossA spinlock defined via DEFINE_SPINLOCK() as a static variable local to a function shows up in lock profiling just with its local variable name. This results in multiple locks just named "lock".
In order to be able to distinguish those locks in the lock profiling output add the function name to struct lock_profile and initialize it with __PRETTY_FUNCTION__ (__func__ or __FUNCTION__ are not usable outside of functions with some compilers).
--- xen/common/spinlock.c | 16 +++++++++++++--- xen/include/xen/spinlock.h | 4 +++- 2 files changed, 16 insertions(+), 4 deletions(-)
xen/common/spinlock.c +13 -3
@@ -345,6 +345,7 @@ static s_time_t lock_profile_start;static struct lock_profile_anc *lock_profile_ancs;static struct lock_profile_qhead lock_profile_glb_q;static spinlock_t lock_profile_lock =SPIN_LOCK_UNLOCKED ; +static const char *lock_profile_nofunc = __PRETTY_FUNCTION__;static void spinlock_profile_iterate (lock_profile_subfunc *sub,void *par) {
@@ -368,8 +369,10 @@ static void spinlock_profile_print_elem(struct lock_profile *data,printk ("%s " , type);if ( idx !=LOCKPROF_IDX_NONE )printk ("%d " , idx); -printk ("%s: addr=%p, lockval=%08x, " , data->name, lock, - lock->tickets.head_tail); +printk ("%s" , data->name); +if ( data->func &&strcmp (data->func, lock_profile_nofunc) ) +printk ("@%s" , data->func); +printk (": addr=%p, lockval=%08x, " , lock, lock->tickets.head_tail);if ( lock->debug.cpu ==SPINLOCK_NO_CPU )printk ("not locked\n" );else
@@ -424,7 +427,14 @@ static void spinlock_profile_ucopy_elem(struct lock_profile *data,if ( p->pc->nr_elem < p->pc->max_elem ) { -safe_strcpy (elem.name, data->name); +if ( data->func &&strcmp (data->func, lock_profile_nofunc) ) + { +snprintf (elem.name,sizeof (elem.name),"%s@%s" , data->name, + data->func); + elem.name[sizeof (elem.name) -1 ] =0 ; + } +else +safe_strcpy (elem.name, data->name);safe_strcpy (elem.type, type); elem.idx = idx; elem.lock_cnt = data->lock_cnt;
xen/include/xen/spinlock.h +3 -1
@@ -77,6 +77,7 @@ struct spinlock;struct lock_profile {struct lock_profile *next;/* forward link */ char *name;/* lock name */ +const char *func;/* function name */ struct spinlock *lock;/* the lock itself */ u64 lock_cnt;/* # of complete locking ops */ u64 block_cnt;/* # of complete wait for lock */
@@ -91,7 +92,8 @@ struct lock_profile_qhead {int32_t idx;/* index for printout */ }; -#define _LOCK_PROFILE (name) { 0, #name, &name, 0, 0, 0, 0, 0 } +#define _LOCK_PROFILE (name) { 0, #name, __PRETTY_FUNCTION__, &name, \ + 0, 0, 0, 0, 0 }#define _LOCK_PROFILE_PTR (name) \ static struct lock_profile * const __lock_profile_##name \ __used_section(".lockprofile.data") = \
Signature · 7 lines
2.16.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel