locks: add process-wide lock hold time tracking

TrackedMutex and TrackedRwLock wrappers that record hold durations
by source location using #[track_caller]. Stats written to
~/.consciousness/lock-stats.json every second, sorted by max hold time.

Re-exported as crate::Mutex so all locks are instrumented. To disable,
swap the re-export back to tokio::sync::Mutex.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
Kent Overstreet 2026-04-12 20:27:42 -04:00
parent b94e056372
commit f56fc3a7c7
9 changed files with 286 additions and 17 deletions

View file

@ -21,16 +21,16 @@ use std::path::Path;
use std::sync::Arc;
/// Process-global cached store. Reloads only when log files change.
static CACHED_STORE: tokio::sync::OnceCell<Arc<tokio::sync::Mutex<Store>>> =
static CACHED_STORE: tokio::sync::OnceCell<Arc<crate::Mutex<Store>>> =
tokio::sync::OnceCell::const_new();
impl Store {
/// Get or create the process-global cached store.
/// Reloads from disk if log files have changed since last load.
pub async fn cached() -> Result<Arc<tokio::sync::Mutex<Store>>, String> {
pub async fn cached() -> Result<Arc<crate::Mutex<Store>>, String> {
let store = CACHED_STORE.get_or_try_init(|| async {
let s = Store::load()?;
Ok::<_, String>(Arc::new(tokio::sync::Mutex::new(s)))
Ok::<_, String>(Arc::new(crate::Mutex::new(s)))
}).await?;
{
let mut guard = store.lock().await;