Skip to main content

store_io_engine/stats/
mod.rs

1//! The oldest operation in flight (D-084, D-095), for a watchdog the caller
2//! owns: store-io keeps no timer.
3//!
4//! The I/O path pays nothing but a lane's busy mark (odd while an operation
5//! is in flight on the lane, naming the oldest; see `QueueSet`). The clock
6//! is read here, by the query: it remembers when it first saw each busy
7//! mark, so an operation still the lane's oldest at a later query has been
8//! outstanding at least that long. The age is a
9//! lower bound, within one query interval of the truth, and 0 the first time
10//! an operation is seen; a watchdog polls and acts when the age passes its
11//! deadline (for example by calling `Store::poison`).
12
13use std::sync::{Arc, Mutex, PoisonError};
14use std::time::Duration;
15
16use store_io_platform::Platform;
17
18use crate::exec::LaneMark;
19use crate::store::Store;
20
21/// The oldest operation in flight on the store's queues.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct Outstanding {
24    /// How long it has been outstanding, at least: measured from the first
25    /// query that saw it (0 on that query).
26    pub age: Duration,
27    /// It is a device flush led by a barrier (rather than a write or read).
28    pub flush: bool,
29}
30
31/// What earlier queries saw: per lane, the busy mark and the clock reading
32/// when that mark was first seen; per busy submitter, its id too.
33#[derive(Debug, Default)]
34pub(crate) struct Seen {
35    lanes: Vec<(u64, u64)>,
36    submitters: Vec<(u64, u64, u64)>,
37}
38
39/// The busy marks of the store's live `Submitter`s (D-098), by id. Locked
40/// only when one is created or dropped, and by queries (after `Seen`).
41#[derive(Default)]
42pub(crate) struct Submitters {
43    next: u64,
44    marks: Vec<(u64, Arc<LaneMark>)>,
45}
46
47fn lock_submitters(s: &Mutex<Submitters>) -> std::sync::MutexGuard<'_, Submitters> {
48    s.lock().unwrap_or_else(PoisonError::into_inner)
49}
50
51/// A lane mark not yet given a first-seen time.
52const NOT_SEEN: u64 = u64::MAX;
53
54/// Locks the table; it holds no invariant a panic could break.
55pub(crate) fn lock_seen(s: &Mutex<Seen>) -> std::sync::MutexGuard<'_, Seen> {
56    s.lock().unwrap_or_else(PoisonError::into_inner)
57}
58
59impl<P: Platform> Store<P>
60where
61    P::Queue: Send,
62{
63    /// The oldest operation still in flight on the store's queues (shared,
64    /// parallel and flush) and its submitters, or `None` when they are all
65    /// idle. O(queues + submitters), no I/O; reads the platform's coarse
66    /// clock once. A submitter's operation counts until its completion is
67    /// processed by `reap` or `wait`, a read stream's until `next()`
68    /// collects it.
69    ///
70    /// The age counts from the first call that saw the operation, so a
71    /// watchdog calling this every second learns of a hung device within a
72    /// second of its deadline.
73    #[must_use]
74    pub fn oldest_outstanding(&self) -> Option<Outstanding> {
75        let inner = &self.inner;
76        let mut seen = lock_seen(&inner.seen);
77        let lanes = inner.queues.marks().len();
78        if seen.lanes.len() != lanes {
79            seen.lanes = vec![(0, 0); lanes];
80        }
81        // Marks first, then the clock: an operation that starts after the
82        // marks were read is not dated before it began (the age stays a
83        // lower bound).
84        for (mark, entry) in inner.queues.marks().zip(seen.lanes.iter_mut()) {
85            // Idle, or a different operation than last time: not yet seen.
86            if mark % 2 == 0 || entry.0 != mark {
87                *entry = (mark, NOT_SEEN);
88            }
89        }
90        let busy: Vec<(u64, u64)> = lock_submitters(&inner.submitters)
91            .marks
92            .iter()
93            .map(|(id, m)| (*id, m.get()))
94            .filter(|(_, mark)| mark % 2 == 1)
95            .collect();
96        let now = inner.platform.monotonic_ms();
97        // The flush lanes follow the shared and parallel lanes (D-124).
98        let first_flush = 2 * inner.queues.shared_len();
99        let mut oldest: Option<(u64, bool)> = None;
100        for (i, entry) in seen.lanes.iter_mut().enumerate() {
101            if entry.0 % 2 == 0 {
102                continue;
103            }
104            if entry.1 == NOT_SEEN {
105                // A new oldest operation on this lane: first seen now.
106                entry.1 = now;
107            }
108            let flush = i >= first_flush;
109            // The oldest wins; on a tie the flush (a leader blocked in a
110            // device flush is the more telling report).
111            if oldest.is_none_or(|(first, was_flush)| {
112                entry.1 < first || (entry.1 == first && flush && !was_flush)
113            }) {
114                oldest = Some((entry.1, flush));
115            }
116        }
117        // A submitter's mark names its oldest operation in flight: the same
118        // mark as last time is the same operation, still outstanding.
119        let before = std::mem::take(&mut seen.submitters);
120        for (id, mark) in busy {
121            let first = before
122                .iter()
123                .find(|e| e.0 == id && e.1 == mark)
124                .map_or(now, |e| e.2);
125            seen.submitters.push((id, mark, first));
126            if oldest.is_none_or(|(at, _)| first < at) {
127                oldest = Some((first, false));
128            }
129        }
130        oldest.map(|(first, flush)| Outstanding {
131            age: Duration::from_millis(now.saturating_sub(first)),
132            flush,
133        })
134    }
135}
136
137impl<P: Platform> Store<P>
138where
139    P::Queue: Send,
140{
141    /// Registers a submitter's busy mark; returns its id.
142    pub(crate) fn register_submitter(&self, mark: &Arc<LaneMark>) -> u64 {
143        let mut s = lock_submitters(&self.inner.submitters);
144        let id = s.next;
145        s.next += 1;
146        s.marks.push((id, Arc::clone(mark)));
147        id
148    }
149
150    pub(crate) fn unregister_submitter(&self, id: u64) {
151        lock_submitters(&self.inner.submitters)
152            .marks
153            .retain(|(i, _)| *i != id);
154    }
155}
156
157#[cfg(test)]
158mod tests;