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`.
68 ///
69 /// The age counts from the first call that saw the operation, so a
70 /// watchdog calling this every second learns of a hung device within a
71 /// second of its deadline.
72 #[must_use]
73 pub fn oldest_outstanding(&self) -> Option<Outstanding> {
74 let inner = &self.inner;
75 let mut seen = lock_seen(&inner.seen);
76 let lanes = inner.queues.marks().len();
77 if seen.lanes.len() != lanes {
78 seen.lanes = vec![(0, 0); lanes];
79 }
80 // Marks first, then the clock: an operation that starts after the
81 // marks were read is not dated before it began (the age stays a
82 // lower bound).
83 for (mark, entry) in inner.queues.marks().zip(seen.lanes.iter_mut()) {
84 // Idle, or a different operation than last time: not yet seen.
85 if mark % 2 == 0 || entry.0 != mark {
86 *entry = (mark, NOT_SEEN);
87 }
88 }
89 let busy: Vec<(u64, u64)> = lock_submitters(&inner.submitters)
90 .marks
91 .iter()
92 .map(|(id, m)| (*id, m.get()))
93 .filter(|(_, mark)| mark % 2 == 1)
94 .collect();
95 let now = inner.platform.monotonic_ms();
96 // The flush lanes follow the shared and parallel lanes (D-124).
97 let first_flush = 2 * inner.queues.shared_len();
98 let mut oldest: Option<(u64, bool)> = None;
99 for (i, entry) in seen.lanes.iter_mut().enumerate() {
100 if entry.0 % 2 == 0 {
101 continue;
102 }
103 if entry.1 == NOT_SEEN {
104 // A new oldest operation on this lane: first seen now.
105 entry.1 = now;
106 }
107 let flush = i >= first_flush;
108 // The oldest wins; on a tie the flush (a leader blocked in a
109 // device flush is the more telling report).
110 if oldest.is_none_or(|(first, was_flush)| {
111 entry.1 < first || (entry.1 == first && flush && !was_flush)
112 }) {
113 oldest = Some((entry.1, flush));
114 }
115 }
116 // A submitter's mark names its oldest operation in flight: the same
117 // mark as last time is the same operation, still outstanding.
118 let before = std::mem::take(&mut seen.submitters);
119 for (id, mark) in busy {
120 let first = before
121 .iter()
122 .find(|e| e.0 == id && e.1 == mark)
123 .map_or(now, |e| e.2);
124 seen.submitters.push((id, mark, first));
125 if oldest.is_none_or(|(at, _)| first < at) {
126 oldest = Some((first, false));
127 }
128 }
129 oldest.map(|(first, flush)| Outstanding {
130 age: Duration::from_millis(now.saturating_sub(first)),
131 flush,
132 })
133 }
134}
135
136impl<P: Platform> Store<P>
137where
138 P::Queue: Send,
139{
140 /// Registers a submitter's busy mark; returns its id.
141 pub(crate) fn register_submitter(&self, mark: &Arc<LaneMark>) -> u64 {
142 let mut s = lock_submitters(&self.inner.submitters);
143 let id = s.next;
144 s.next += 1;
145 s.marks.push((id, Arc::clone(mark)));
146 id
147 }
148
149 pub(crate) fn unregister_submitter(&self, id: u64) {
150 lock_submitters(&self.inner.submitters)
151 .marks
152 .retain(|(i, _)| *i != id);
153 }
154}
155
156#[cfg(test)]
157mod tests;