locktick 0.6.0

Automated lock accounting & profiling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::{
    collections::HashMap,
    fmt,
    ops::{Deref, DerefMut},
    path::Path,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc, LazyLock, Mutex, RwLock,
    },
    time::{Duration, Instant},
};

use simple_moving_average::{SingleSumSMA, SMA};
#[cfg(feature = "tracing")]
use tracing::trace;

// Contains data on all created locks and their guards.
static LOCK_INFOS: LazyLock<RwLock<HashMap<Location, Mutex<LockInfo>>>> =
    LazyLock::new(|| RwLock::new(HashMap::new()));

// Provides a common source of indices for all the guards.
static GUARD_COUNTER: AtomicUsize = AtomicUsize::new(0);

/// Points to the filesystem location where a lock or guard was created.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Location {
    pub path: Arc<Path>,
    pub line: u32,
    pub col: u32,
}

impl fmt::Display for Location {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}@{}:{}", self.path.display(), self.line, self.col)
    }
}

// Provides the means to procure the location of a lock or its guard.
#[track_caller]
pub(crate) fn call_location() -> Location {
    let loc = std::panic::Location::caller();
    Location {
        path: Arc::from(Path::new(loc.file())),
        line: loc.line(),
        col: loc.column(),
    }
}

/// Returns a vector containing snapshots of the data related to all the locks.
pub fn lock_snapshots() -> Vec<LockInfo> {
    LOCK_INFOS
        .read()
        .unwrap()
        .values()
        .map(|info| info.lock().unwrap().clone())
        .collect()
}

#[cfg(feature = "test")]
pub fn clear_lock_infos() {
    LOCK_INFOS.write().unwrap().clear();
}

/// Contains all the details related to a given lock, and it can only
/// be obtained through a call to `lock_snapshots`.
#[derive(Debug, Clone)]
pub struct LockInfo {
    pub kind: LockKind,
    pub location: Location,
    pub known_guards: HashMap<Location, GuardInfo>,
}

impl LockInfo {
    /// Registers the creation of a lock; this is meant to be called
    /// when creating wrapper objects for different kinds of locks.
    #[track_caller]
    pub(crate) fn register(kind: LockKind) -> Location {
        let location = call_location();

        LOCK_INFOS
            .write()
            .unwrap()
            .entry(location.clone())
            .or_insert_with(|| {
                Mutex::new(Self {
                    kind,
                    location: location.clone(),
                    known_guards: Default::default(),
                })
            });

        location
    }
}

impl fmt::Display for LockInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({:?}):", self.location, self.kind)?;

        for guard in self.known_guards.values() {
            write!(f, "\n- {guard}")?;
        }

        Ok(())
    }
}

/// The type of the lock; either a `Mutex` or an `RwLock`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LockKind {
    Mutex,
    RwLock,
}

/// A wrapper for the lock guard produced when working with a lock. It
/// only contains the guard itself and metadata that allows it to be
/// distinguished from other guards belonging to a single lock.
pub struct LockGuard<T> {
    guard: T,
    pub lock_location: Location,
    pub guard_location: Location,
    pub guard_index: usize,
}

impl<T> LockGuard<T> {
    /// Registers the creation of a guard and returns it wrapped in an object
    /// used to perform relevant accounting when the guard is dropped.
    pub(crate) fn new(
        guard: T,
        guard_kind: GuardKind,
        lock_location: &Location,
        guard_location: Location,
        wait_time: Duration,
    ) -> Self {
        #[cfg(feature = "tracing")]
        trace!("Acquired a {:?} guard at {}", guard_kind, guard_location);

        let guard_index = if let Some(lock_info) = LOCK_INFOS.read().unwrap().get(lock_location) {
            let guard_idx = GUARD_COUNTER.fetch_add(1, Ordering::Relaxed);
            let mut lock_info = lock_info.lock().unwrap();

            let guard_info = lock_info
                .known_guards
                .entry(guard_location.clone())
                .or_insert_with(|| GuardInfo::new(guard_kind, guard_location.clone()));
            guard_info.num_uses += 1;
            guard_info.avg_wait_time.add_sample(wait_time);
            if wait_time > guard_info.max_wait_time {
                guard_info.max_wait_time = wait_time;
            }
            guard_info.active_uses.insert(guard_idx, Instant::now());

            guard_idx
        } else {
            unreachable!();
        };

        LockGuard {
            guard,
            lock_location: lock_location.clone(),
            guard_location,
            guard_index,
        }
    }

    /// Registers the creation of a guard from a WaitGuard, reusing the wait index.
    /// This is called when a waiting task successfully acquires the lock.
    pub(crate) fn from_wait_guard(guard: T, wait_guard: WaitGuard, wait_time: Duration) -> Self {
        let guard_kind = wait_guard.guard_kind;
        let lock_location = wait_guard.lock_location.clone();
        let guard_location = wait_guard.guard_location.clone();
        let guard_index = wait_guard.wait_index;

        // Consume the wait guard, neutralizing its Drop logic; the transition
        // from waiting to active is recorded below under a single lock
        wait_guard.finish();

        #[cfg(feature = "tracing")]
        trace!("Acquired a {:?} guard at {}", guard_kind, guard_location);

        if let Some(lock_info) = LOCK_INFOS.read().unwrap().get(&lock_location) {
            let mut lock_info = lock_info.lock().unwrap();

            let guard_info = lock_info
                .known_guards
                .entry(guard_location.clone())
                .or_insert_with(|| GuardInfo::new(guard_kind, guard_location.clone()));

            // Remove from waiting, add to active
            guard_info.waiting_tasks.remove(&guard_index);
            guard_info.num_uses += 1;
            guard_info.avg_wait_time.add_sample(wait_time);
            if wait_time > guard_info.max_wait_time {
                guard_info.max_wait_time = wait_time;
            }
            guard_info.active_uses.insert(guard_index, Instant::now());
        } else {
            unreachable!();
        }

        LockGuard {
            guard,
            lock_location,
            guard_location,
            guard_index,
        }
    }
}

/// A RAII guard that tracks when a task is waiting for a lock.
/// When dropped, it automatically unregisters the waiting task.
pub struct WaitGuard {
    pub(crate) lock_location: Location,
    pub(crate) guard_location: Location,
    pub(crate) guard_kind: GuardKind,
    pub(crate) wait_index: usize,
    finished: bool,
}

impl WaitGuard {
    /// Creates a new WaitGuard and registers the waiting task.
    pub(crate) fn new(
        guard_kind: GuardKind,
        lock_location: &Location,
        guard_location: Location,
    ) -> Self {
        #[cfg(feature = "tracing")]
        trace!(
            "Task waiting for {:?} guard at {}",
            guard_kind,
            guard_location
        );

        let wait_index = GUARD_COUNTER.fetch_add(1, Ordering::Relaxed);

        if let Some(lock_info) = LOCK_INFOS.read().unwrap().get(lock_location) {
            let mut lock_info = lock_info.lock().unwrap();

            let guard_info = lock_info
                .known_guards
                .entry(guard_location.clone())
                .or_insert_with(|| GuardInfo::new(guard_kind, guard_location.clone()));
            guard_info.waiting_tasks.insert(wait_index, Instant::now());
        } else {
            unreachable!();
        }

        WaitGuard {
            lock_location: lock_location.clone(),
            guard_location,
            guard_kind,
            wait_index,
            finished: false,
        }
    }

    /// Consumes this WaitGuard, making its Drop impl a no-op so that the waiting
    /// task is not unregistered twice. This should be called when the lock has
    /// been successfully acquired.
    pub(crate) fn finish(mut self) {
        self.finished = true;
    }
}

impl Drop for WaitGuard {
    fn drop(&mut self) {
        if self.finished {
            return;
        }

        #[cfg(feature = "tracing")]
        trace!(
            "Task stopped waiting for {:?} guard at {} (cancelled or failed)",
            self.guard_kind,
            self.guard_location
        );

        if let Some(lock_info) = LOCK_INFOS.read().unwrap().get(&self.lock_location) {
            let mut lock_info = lock_info.lock().unwrap();
            if let Some(guard_info) = lock_info.known_guards.get_mut(&self.guard_location) {
                guard_info.waiting_tasks.remove(&self.wait_index);
            }
        }
    }
}

/// Contains data and statistics related to a single guard.
#[derive(Debug, Clone)]
pub struct GuardInfo {
    pub kind: GuardKind,
    pub location: Location,
    pub num_uses: usize,
    active_uses: HashMap<usize, Instant>,
    waiting_tasks: HashMap<usize, Instant>,
    avg_wait_time: SingleSumSMA<Duration, u32, 50>,
    pub max_wait_time: Duration,
    avg_duration: SingleSumSMA<Duration, u32, 50>,
    pub max_duration: Duration,
}

impl GuardInfo {
    fn new(kind: GuardKind, location: Location) -> Self {
        Self {
            kind,
            location,
            num_uses: 0,
            active_uses: Default::default(),
            waiting_tasks: Default::default(),
            avg_wait_time: SingleSumSMA::from_zero(Duration::ZERO),
            max_wait_time: Duration::ZERO,
            avg_duration: SingleSumSMA::from_zero(Duration::ZERO),
            max_duration: Duration::ZERO,
        }
    }

    /// Returns `true` if threads are currently holding or waiting for this guard.
    pub fn is_in_use(&self) -> bool {
        !self.active_uses.is_empty() || !self.waiting_tasks.is_empty()
    }

    /// Returns the number of current uses of the guard. It can
    /// be greater than `1` only in case of a read guard, and `0`
    /// indicates that the guard is currently inactive.
    pub fn num_active_uses(&self) -> usize {
        self.active_uses.len()
    }

    /// Returns numbers corresponding to the order in which the currently
    /// active guards were called, which can be useful for debugging purposes.
    pub fn active_call_indices(&self) -> Vec<usize> {
        let mut indices = self.active_uses.keys().copied().collect::<Vec<_>>();
        indices.sort_unstable();
        indices
    }

    /// Returns the number of tasks currently waiting to acquire this guard.
    pub fn num_waiting(&self) -> usize {
        self.waiting_tasks.len()
    }

    /// Returns numbers corresponding to the order in which the currently
    /// waiting tasks started waiting, which can be useful for debugging purposes.
    pub fn waiting_call_indices(&self) -> Vec<usize> {
        let mut indices = self.waiting_tasks.keys().copied().collect::<Vec<_>>();
        indices.sort_unstable();
        indices
    }

    /// Returns the average wait time for the guard. It is a moving
    /// average that gets updated with each use.
    pub fn avg_wait_time(&self) -> Duration {
        self.avg_wait_time.get_average()
    }

    /// Returns the average duration of the guard. It is a moving
    /// average that gets updated with each use.
    pub fn avg_duration(&self) -> Duration {
        self.avg_duration.get_average()
    }
}

impl fmt::Display for GuardInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} ({:?}): curr users: {}; waiting: {}; calls: {}; duration: {:?} avg, {:?} max; wait: {:?} avg, {:?} max",
            self.location,
            self.kind,
            self.active_uses.len(),
            self.waiting_tasks.len(),
            self.num_uses,
            self.avg_duration.get_average(),
            self.max_duration,
            self.avg_wait_time.get_average(),
            self.max_wait_time,
        )
    }
}

impl<T: Deref> Deref for LockGuard<T> {
    type Target = T::Target;

    fn deref(&self) -> &Self::Target {
        self.guard.deref()
    }
}

impl<T: DerefMut> DerefMut for LockGuard<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.guard.deref_mut()
    }
}

impl<T> Drop for LockGuard<T> {
    fn drop(&mut self) {
        let timestamp = Instant::now();

        if let Some(lock_info) = LOCK_INFOS.read().unwrap().get(&self.lock_location) {
            let mut lock_info = lock_info.lock().unwrap();
            let known_guard = lock_info
                .known_guards
                .get_mut(&self.guard_location)
                .unwrap();
            let guard_timestamp = known_guard.active_uses.remove(&self.guard_index).unwrap();
            let duration = timestamp - guard_timestamp;
            known_guard.avg_duration.add_sample(duration);
            if duration > known_guard.max_duration {
                known_guard.max_duration = duration;
            }

            #[cfg(feature = "tracing")]
            trace!(
                "The {:?} guard for lock {} acquired at {} was dropped after {:?}",
                known_guard.kind,
                self.lock_location,
                known_guard.location,
                duration,
            );
        }
    }
}

/// The type of the guard that was created when working with a lock.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GuardKind {
    Lock,
    Read,
    Write,
}