From bb4ae18c2a7cec1fd8c3877a324a361b7f4cba72 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 7 Dec 2011 18:42:01 -0800 Subject: [PATCH] kernel - add DEBUG_LOCKS_LATENCY option * This is for debugging only, default disabled. If optioned this adds a sysctl to add a forced latency loop (count to N) in front of any spinlock or gettoken. --- sys/conf/options | 1 + sys/kern/kern_spinlock.c | 16 ++++++++++++++++ sys/kern/lwkt_token.c | 26 ++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/sys/conf/options b/sys/conf/options index f161292bb2..ca1eb9884c 100644 --- a/sys/conf/options +++ b/sys/conf/options @@ -469,6 +469,7 @@ ENABLE_ALART opt_intpm.h BLKDEV_IOSIZE opt_global.h DEBUG opt_global.h DEBUG_LOCKS opt_global.h +DEBUG_LOCKS_LATENCY opt_global.h DEBUG_CRIT_SECTIONS opt_global.h DIAGNOSTIC opt_global.h INVARIANTS opt_global.h diff --git a/sys/kern/kern_spinlock.c b/sys/kern/kern_spinlock.c index d836833514..38b222092c 100644 --- a/sys/kern/kern_spinlock.c +++ b/sys/kern/kern_spinlock.c @@ -94,6 +94,16 @@ SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested2, CTLFLAG_RD, &spinlocks_contested2, 0, "Serious spinlock contention count"); +#ifdef DEBUG_LOCKS_LATENCY + +static long spinlocks_add_latency; +SYSCTL_LONG(_debug, OID_AUTO, spinlocks_add_latency, CTLFLAG_RW, + &spinlocks_add_latency, 0, + "Add spinlock latency"); + +#endif + + /* * We need a fairly large pool to avoid contention on large SMP systems, * particularly multi-chip systems. @@ -167,6 +177,12 @@ spin_lock_contested(struct spinlock *spin) struct indefinite_info info = { 0, 0 }; int i; +#ifdef DEBUG_LOCKS_LATENCY + long j; + for (j = spinlocks_add_latency; j > 0; --j) + cpu_ccfence(); +#endif + i = 0; ++spin->countb; diff --git a/sys/kern/lwkt_token.c b/sys/kern/lwkt_token.c index 90c6e8b572..31494505cc 100644 --- a/sys/kern/lwkt_token.c +++ b/sys/kern/lwkt_token.c @@ -163,6 +163,16 @@ SYSCTL_LONG(_lwkt, OID_AUTO, tty_collisions, CTLFLAG_RW, SYSCTL_LONG(_lwkt, OID_AUTO, vnode_collisions, CTLFLAG_RW, &vnode_token.t_collisions, 0, "Collision counter of vnode_token"); +#ifdef DEBUG_LOCKS_LATENCY + +static long tokens_add_latency; +SYSCTL_LONG(_debug, OID_AUTO, tokens_add_latency, CTLFLAG_RW, + &tokens_add_latency, 0, + "Add spinlock latency"); + +#endif + + static int _lwkt_getalltokens_sorted(thread_t td); #ifdef SMP @@ -344,15 +354,27 @@ _lwkt_trytokref_spin(lwkt_tokref_t ref, thread_t td, long mode) { int spin; - if (_lwkt_trytokref(ref, td, mode)) + if (_lwkt_trytokref(ref, td, mode)) { +#ifdef DEBUG_LOCKS_LATENCY + long j; + for (j = tokens_add_latency; j > 0; --j) + cpu_ccfence(); +#endif return TRUE; + } for (spin = lwkt_token_spin; spin > 0; --spin) { if (lwkt_token_delay) tsc_delay(lwkt_token_delay); else cpu_pause(); - if (_lwkt_trytokref(ref, td, mode)) + if (_lwkt_trytokref(ref, td, mode)) { +#ifdef DEBUG_LOCKS_LATENCY + long j; + for (j = tokens_add_latency; j > 0; --j) + cpu_ccfence(); +#endif return TRUE; + } } return FALSE; } -- 2.41.0