From nobody Sun Oct 22 17:09:50 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4SD4Z63FcQz4xZRl; Sun, 22 Oct 2023 17:09:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SD4Z62NlYz4bfm; Sun, 22 Oct 2023 17:09:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1697994590; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bp11gjNZnR3CzsQ5CCcgTNfdJnHVitFPV8dla4zsmi4=; b=ogpSZUu6RBKjK2hwsb7Ny2jzZq3h2fWnFwYIeHvc0zLpJu663EF93EIGMXcuyFt7m7qcFt 8fvSE3KUof6ogUJZyJDIn0GH3ildYD9D7FHFLXd8ScOKrO3OWs+XVwwlWj19m4b9k8ZNBW wdF9I72VuaKv3wW9j6u3KrJiuRsC3kjhAa4+jMFMv4By7+ZJrqMA1rLGIozsRiRP6yFrKx wzkTOOP2RPI2+g0I7n6bIfNTZF2xNXU3WDwNk4LY4To5rrNCCmwCvDcpd+xneldyCexE5b 62e1m7MDgLt7afI+40K1UwAkc+jap2zckcSR8oR8BBRTJeF7F0Agv+SSncBwyA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1697994590; a=rsa-sha256; cv=none; b=ej3MvhQWOBGRIKQHLrCheGBi2XgDxVh2djvnQ96xHOWkXxASqTfQWVr0iq35faG2cYGLjZ yMGF8St4HIxtL65JDXQcOHfBLZiCFYZycb3RTcvstNxaI5OamaqYIEuAcFASraZNeMOz7K UYg+/WqlTapa/lt+o9zEh3wkz+7JWmLOoazvr4R/FlwatBrNo8m74VnZVkfCOF2jrkSEfh alyk7Xr7zbMkarIRlIYnfw08WZ0tV56Yj8MbcGtbY+N4HTlBz5adl+1w0W8x6/hT8uuDdv v8r0PpoyWmTpmRQdFCQ8pd7vaRdSKGvfWHGgpTD1fEMtQU2ol/7ZLpeFNaf66Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1697994590; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bp11gjNZnR3CzsQ5CCcgTNfdJnHVitFPV8dla4zsmi4=; b=QTQ1LDc001NqaGsG/s7o06j7JDc2PdDS6WgLFBfWimPRZU6OBknflqITeYuPl1I6P9vVyV V8kAE0q2L6YJ0jhUq4h5UVcjq2b1XKtrRtpv0ZgKXtlbTIr8ae2MNlGWMIU/OiRbFsWIdB qAFfpsP0t9cXd6Zkta7OT1ofS1oet/sGpCNRzrgQuHou5ELnBrabmV9S1d76mbAmfPlhvh pJ1cN7IAzyMyLDpaR7FkqzYg06JWvckLPzGRGBM3HdBKFxW7vbd7UN2aQOvEO1DjDeBy05 RLwq9yXpLOgrtZgTHWxTmDiH9qsvaQzfKaVoR0PgZgRJTxVb5czTTlZvxq+eUg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4SD4Z61GTSz15Gg; Sun, 22 Oct 2023 17:09:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 39MH9oVj035029; Sun, 22 Oct 2023 17:09:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 39MH9o58035026; Sun, 22 Oct 2023 17:09:50 GMT (envelope-from git) Date: Sun, 22 Oct 2023 17:09:50 GMT Message-Id: <202310221709.39MH9o58035026@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Guzik Subject: git: 7530de77f539 - main - thread: add td_wantedlock List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mjg X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7530de77f539678847edd29489b7635f8f58b574 Auto-Submitted: auto-generated The branch main has been updated by mjg: URL: https://cgit.FreeBSD.org/src/commit/?id=7530de77f539678847edd29489b7635f8f58b574 commit 7530de77f539678847edd29489b7635f8f58b574 Author: Mateusz Guzik AuthorDate: 2023-10-22 15:46:39 +0000 Commit: Mateusz Guzik CommitDate: 2023-10-22 17:09:45 +0000 thread: add td_wantedlock This enables obtaining lock information threads are actively waiting for while sampling. Without the change one would only see a bunch of calls to lock_delay(), where the stacktrace often does not reveal what the lock might be. Note this is not the same as lock profiling, which only produces data for cases which wait for locks. struct thread already has a td_lockname field, but I did not use it because it has different semantics -- denotes when the thread is off cpu. At the same time it could not be converted to hold a lock_object pointer because non-curthread access would no longer be guaranteed to be safe -- by the time it reads the pointer the lock might have been taken, released and the object containing it freed. Sample usage with dtrace: rm /tmp/out.kern_stacks ; dtrace -x stackframes=100 -n 'profile-997 { @[curthread->td_wantedlock != NULL ? stringof(curthread->td_wantedlock->lo_name) : stringof("\n"), stack()] = count(); }' -o /tmp/out.kern_stacks This also facilitates addition of lock information to traces produced by hwpmc. Note: spinlocks are not supported at the moment. Sponsored by: Rubicon Communications, LLC ("Netgate") --- sys/kern/kern_mutex.c | 3 +++ sys/kern/kern_rwlock.c | 6 ++++++ sys/kern/kern_sx.c | 6 ++++++ sys/kern/kern_thread.c | 6 +++--- sys/sys/proc.h | 11 +++++++++++ 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c index 7f348530ed31..6071ac7fd6f1 100644 --- a/sys/kern/kern_mutex.c +++ b/sys/kern/kern_mutex.c @@ -574,6 +574,8 @@ __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v) "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", m->lock_object.lo_name, (void *)m->mtx_lock, file, line); + THREAD_CONTENDS_ON_LOCK(&m->lock_object); + for (;;) { if (v == MTX_UNOWNED) { if (_mtx_obtain_lock_fetch(m, &v, tid)) @@ -670,6 +672,7 @@ retry_turnstile: #endif v = MTX_READ_VALUE(m); } + THREAD_CONTENTION_DONE(&m->lock_object); #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) if (__predict_true(!doing_lockprof)) return; diff --git a/sys/kern/kern_rwlock.c b/sys/kern/kern_rwlock.c index 5705de0f311f..83d5862a6667 100644 --- a/sys/kern/kern_rwlock.c +++ b/sys/kern/kern_rwlock.c @@ -482,6 +482,8 @@ __rw_rlock_hard(struct rwlock *rw, struct thread *td, uintptr_t v lock_profile_obtain_lock_failed(&rw->lock_object, false, &contested, &waittime); + THREAD_CONTENDS_ON_LOCK(&rw->lock_object); + for (;;) { if (__rw_rlock_try(rw, td, &v, false LOCK_FILE_LINE_ARG)) break; @@ -628,6 +630,7 @@ retry_ts: __func__, rw); v = RW_READ_VALUE(rw); } + THREAD_CONTENTION_DONE(&rw->lock_object); #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) if (__predict_true(!doing_lockprof)) return; @@ -976,6 +979,8 @@ __rw_wlock_hard(volatile uintptr_t *c, uintptr_t v LOCK_FILE_LINE_ARG_DEF) lock_profile_obtain_lock_failed(&rw->lock_object, false, &contested, &waittime); + THREAD_CONTENDS_ON_LOCK(&rw->lock_object); + for (;;) { if (v == RW_UNLOCKED) { if (_rw_write_lock_fetch(rw, &v, tid)) @@ -1161,6 +1166,7 @@ retry_ts: #endif v = RW_READ_VALUE(rw); } + THREAD_CONTENTION_DONE(&rw->lock_object); if (__predict_true(!extra_work)) return; #ifdef ADAPTIVE_RWLOCKS diff --git a/sys/kern/kern_sx.c b/sys/kern/kern_sx.c index 81e46fceed5e..bc8a1214689f 100644 --- a/sys/kern/kern_sx.c +++ b/sys/kern/kern_sx.c @@ -653,6 +653,8 @@ _sx_xlock_hard(struct sx *sx, uintptr_t x, int opts LOCK_FILE_LINE_ARG_DEF) GIANT_SAVE(extra_work); #endif + THREAD_CONTENDS_ON_LOCK(&sx->lock_object); + for (;;) { if (x == SX_LOCK_UNLOCKED) { if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) @@ -872,6 +874,7 @@ retry_sleepq: __func__, sx); x = SX_READ_VALUE(sx); } + THREAD_CONTENTION_DONE(&sx->lock_object); if (__predict_true(!extra_work)) return (error); #ifdef ADAPTIVE_SX @@ -1074,6 +1077,8 @@ _sx_slock_hard(struct sx *sx, int opts, uintptr_t x LOCK_FILE_LINE_ARG_DEF) GIANT_SAVE(extra_work); #endif + THREAD_CONTENDS_ON_LOCK(&sx->lock_object); + /* * As with rwlocks, we don't make any attempt to try to block * shared locks once there is an exclusive waiter. @@ -1224,6 +1229,7 @@ retry_sleepq: __func__, sx); x = SX_READ_VALUE(sx); } + THREAD_CONTENTION_DONE(&sx->lock_object); #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) if (__predict_true(!extra_work)) return (error); diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c index dcebb08956e6..3bc8546db594 100644 --- a/sys/kern/kern_thread.c +++ b/sys/kern/kern_thread.c @@ -87,7 +87,7 @@ _Static_assert(offsetof(struct thread, td_flags) == 0x108, "struct thread KBI td_flags"); _Static_assert(offsetof(struct thread, td_pflags) == 0x114, "struct thread KBI td_pflags"); -_Static_assert(offsetof(struct thread, td_frame) == 0x4b0, +_Static_assert(offsetof(struct thread, td_frame) == 0x4b8, "struct thread KBI td_frame"); _Static_assert(offsetof(struct thread, td_emuldata) == 0x6c0, "struct thread KBI td_emuldata"); @@ -107,9 +107,9 @@ _Static_assert(offsetof(struct thread, td_flags) == 0x9c, "struct thread KBI td_flags"); _Static_assert(offsetof(struct thread, td_pflags) == 0xa8, "struct thread KBI td_pflags"); -_Static_assert(offsetof(struct thread, td_frame) == 0x314, +_Static_assert(offsetof(struct thread, td_frame) == 0x318, "struct thread KBI td_frame"); -_Static_assert(offsetof(struct thread, td_emuldata) == 0x358, +_Static_assert(offsetof(struct thread, td_emuldata) == 0x35c, "struct thread KBI td_emuldata"); _Static_assert(offsetof(struct proc, p_flag) == 0x6c, "struct proc KBI p_flag"); diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 8c916173b4ef..dc94d12d16c4 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -277,6 +277,7 @@ struct thread { int td_rw_rlocks; /* (k) Count of rwlock read locks. */ int td_sx_slocks; /* (k) Count of sx shared locks. */ int td_lk_slocks; /* (k) Count of lockmgr shared locks. */ + struct lock_object *td_wantedlock; /* (k) Lock we are contending on */ struct turnstile *td_blocked; /* (t) Lock thread is blocked on. */ const char *td_lockname; /* (t) Name of lock blocked on. */ LIST_HEAD(, turnstile) td_contested; /* (q) Contested locks. */ @@ -1074,6 +1075,16 @@ extern pid_t pid_max; #define THREAD_CAN_SLEEP() ((curthread)->td_no_sleeping == 0) +#define THREAD_CONTENDS_ON_LOCK(lo) do { \ + MPASS(curthread->td_wantedlock == NULL); \ + curthread->td_wantedlock = lo; \ +} while (0) + +#define THREAD_CONTENTION_DONE(lo) do { \ + MPASS(curthread->td_wantedlock == lo); \ + curthread->td_wantedlock = NULL; \ +} while (0) + #define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash]) #define PIDHASHLOCK(pid) (&pidhashtbl_lock[((pid) & pidhashlock)]) extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;