From 387177972d15d6cf5bf20c88fdeb5331d183ba2c Mon Sep 17 00:00:00 2001 From: Hiten Pandya Date: Thu, 7 Jul 2005 20:28:26 +0000 Subject: [PATCH] Add counters for recording Token/MPlock contention, this would help in determining the number of times contention has occured in the system. The contention counters have been made 64-bit quantities because they are situated within a tight-loop. KTR tracepoints have been added for marking start and stop of a token's contention. New field tr_flags added to struct lwkt_tokref. By adding tracepoints in lwkt_chktokens(9), it gives us interesting data on MP machines when it indirectly sends a passive IPI to the remote CPU for gaining ownership of a token. It would be interesting to see KTR dumps for a 4-CPU or an 8-CPU system. Discussed-with: Matthew Dillon --- sys/kern/lwkt_thread.c | 25 +++++++++++++++++++++---- sys/kern/lwkt_token.c | 20 +++++++++++++++++++- sys/sys/thread.h | 8 +++++++- sys/sys/thread2.h | 3 ++- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/sys/kern/lwkt_thread.c b/sys/kern/lwkt_thread.c index e20f8c4837..c50aac47e0 100644 --- a/sys/kern/lwkt_thread.c +++ b/sys/kern/lwkt_thread.c @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sys/kern/lwkt_thread.c,v 1.75 2005/06/20 07:31:05 dillon Exp $ + * $DragonFly: src/sys/kern/lwkt_thread.c,v 1.76 2005/07/07 20:28:26 hmp Exp $ */ /* @@ -98,6 +98,8 @@ static __int64_t switch_count = 0; static __int64_t preempt_hit = 0; static __int64_t preempt_miss = 0; static __int64_t preempt_weird = 0; +static __int64_t token_contention_count = 0; +static __int64_t mplock_contention_count = 0; #ifdef _KERNEL @@ -109,7 +111,12 @@ SYSCTL_QUAD(_lwkt, OID_AUTO, switch_count, CTLFLAG_RW, &switch_count, 0, ""); SYSCTL_QUAD(_lwkt, OID_AUTO, preempt_hit, CTLFLAG_RW, &preempt_hit, 0, ""); SYSCTL_QUAD(_lwkt, OID_AUTO, preempt_miss, CTLFLAG_RW, &preempt_miss, 0, ""); SYSCTL_QUAD(_lwkt, OID_AUTO, preempt_weird, CTLFLAG_RW, &preempt_weird, 0, ""); - +#ifdef INVARIANTS +SYSCTL_QUAD(_lwkt, OID_AUTO, token_contention_count, CTLFLAG_RW, + &token_contention_count, 0, "spinning due to token contention"); +SYSCTL_QUAD(_lwkt, OID_AUTO, mplock_contention_count, CTLFLAG_RW, + &mplock_contention_count, 0, "spinning due to MPLOCK contention"); +#endif #endif /* @@ -576,11 +583,21 @@ again: u_int32_t rqmask = gd->gd_runqmask; while (rqmask) { TAILQ_FOREACH(ntd, &gd->gd_tdrunq[nq], td_threadq) { - if (ntd->td_mpcount && !mpheld && !cpu_try_mplock()) + if (ntd->td_mpcount && !mpheld && !cpu_try_mplock()) { + /* spinning due to MP lock being held */ +#ifdef INVARIANTS + ++mplock_contention_count; +#endif continue; + } mpheld = MP_LOCK_HELD(); - if (ntd->td_toks && !lwkt_chktokens(ntd)) + if (ntd->td_toks && !lwkt_chktokens(ntd)) { + /* spinning due to token contention */ +#ifdef INVARIANTS + ++token_contention_count; +#endif continue; + } break; } if (ntd) diff --git a/sys/kern/lwkt_token.c b/sys/kern/lwkt_token.c index 5f4d433e7e..04f84c0a5d 100644 --- a/sys/kern/lwkt_token.c +++ b/sys/kern/lwkt_token.c @@ -31,7 +31,7 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sys/kern/lwkt_token.c,v 1.21 2005/06/27 19:24:52 dillon Exp $ + * $DragonFly: src/sys/kern/lwkt_token.c,v 1.22 2005/07/07 20:28:26 hmp Exp $ */ #ifdef _KERNEL @@ -104,6 +104,8 @@ static void lwkt_reqtoken_remote(void *data); static lwkt_token pool_tokens[LWKT_NUM_POOL_TOKENS]; #define TOKEN_STRING "REF=%p TOK=%p TD=%p" +#define CONTENDED_STRING "REF=%p TOK=%p TD=%p (contention started)" +#define UNCONTENDED_STRING "REF=%p TOK=%p TD=%p (contention stopped)" #if !defined(KTR_TOKENS) #define KTR_TOKENS KTR_ALL #endif @@ -117,6 +119,8 @@ KTR_INFO(KTR_TOKENS, tokens, remote, 3, TOKEN_STRING, sizeof(void *) * 3); KTR_INFO(KTR_TOKENS, tokens, reqremote, 4, TOKEN_STRING, sizeof(void *) * 3); KTR_INFO(KTR_TOKENS, tokens, reqfail, 5, TOKEN_STRING, sizeof(void *) * 3); KTR_INFO(KTR_TOKENS, tokens, drain, 6, TOKEN_STRING, sizeof(void *) * 3); +KTR_INFO(KTR_TOKENS, tokens, contention_start, 7, CONTENDED_STRING, sizeof(void *) * 3); +KTR_INFO(KTR_TOKENS, tokens, contention_stop, 7, UNCONTENDED_STRING, sizeof(void *) * 3); #endif #define logtoken(name, ref) \ @@ -158,6 +162,13 @@ lwkt_chktokens(thread_t td) if ((dgd = tok->t_cpu) != gd) { cpu_ccfence(); /* don't let the compiler reload tok->t_cpu */ r = 0; +#ifdef INVARIANTS + if ((refs->tr_flags & LWKT_TOKREF_CONTENDED) == 0) { + refs->tr_flags |= LWKT_TOKREF_CONTENDED; + /* mark token contended */ + logtoken(contention_start, refs); + } +#endif /* * Queue a request to the target cpu, exit the loop early if @@ -187,6 +198,13 @@ lwkt_chktokens(thread_t td) refs, refs->tr_tok, magic); } } +#ifdef INVARIANTS + if (refs->tr_flags & LWKT_TOKREF_CONTENDED) { + /* mark token uncontended */ + refs->tr_flags &= ~LWKT_TOKREF_CONTENDED; + logtoken(contention_stop, refs); + } +#endif } return(r); } diff --git a/sys/sys/thread.h b/sys/sys/thread.h index 30b4cb292c..128625e3d6 100644 --- a/sys/sys/thread.h +++ b/sys/sys/thread.h @@ -7,7 +7,7 @@ * Types which must already be defined when this header is included by * userland: struct md_thread * - * $DragonFly: src/sys/sys/thread.h,v 1.63 2005/06/19 22:07:17 dillon Exp $ + * $DragonFly: src/sys/sys/thread.h,v 1.64 2005/07/07 20:28:26 hmp Exp $ */ #ifndef _SYS_THREAD_H_ @@ -104,8 +104,14 @@ typedef struct lwkt_tokref { lwkt_tokref_t tr_next; /* linked list */ lwkt_tokref_t tr_gdreqnext; /* based at gd_tokreqbase */ struct globaldata *tr_reqgd; /* requesting cpu */ + int tr_flags; /* token state and debug flags */ } lwkt_tokref; +/* + * Token state and debug flags. + */ +#define LWKT_TOKREF_CONTENDED 0x002 /* token ownership contention */ + /* * The magic number indicates the trans-cpu state of a token reference. * diff --git a/sys/sys/thread2.h b/sys/sys/thread2.h index 6e07260635..2333a79120 100644 --- a/sys/sys/thread2.h +++ b/sys/sys/thread2.h @@ -8,7 +8,7 @@ * on a different cpu will not be immediately scheduled by a yield() on * this cpu. * - * $DragonFly: src/sys/sys/thread2.h,v 1.20 2005/06/19 21:50:50 dillon Exp $ + * $DragonFly: src/sys/sys/thread2.h,v 1.21 2005/07/07 20:28:26 hmp Exp $ */ #ifndef _SYS_THREAD2_H_ @@ -125,6 +125,7 @@ lwkt_tokref_init(lwkt_tokref_t ref, lwkt_token_t tok) { ref->tr_magic = LWKT_TOKREF_MAGIC1; ref->tr_tok = tok; + ref->tr_flags = 0; } /* -- 2.41.0