task/manager/metrics.rs
1// Metrics module - read-only activity snapshots for monitoring applications
2
3use super::*;
4use crate::metrics::MetricsSnapshot;
5
6impl Manager {
7 /// Sample the cumulative poll counters of every registered task and every
8 /// executor with a spawner registration. Returns `None` without the
9 /// `metrics` feature.
10 ///
11 /// Counters only grow (task counters restart with each registration, see
12 /// [`crate::metrics::TaskMetrics::generation`]), so callers compute rates
13 /// from the differences between two snapshots, with `saturating_sub`; see
14 /// [`crate::metrics`] for how to compute executor and task loads. Each
15 /// executor is read with its own timestamp, at the same instant as its
16 /// counters, so `Δbusy + Δidle` stays within `Δtimestamp` up to rounding
17 /// and cross-core extrapolation, described in [`crate::metrics`]: clamp
18 /// loads to `[0, 1]`. The snapshot timestamp is read last, after every
19 /// counter.
20 ///
21 /// Readers spin while an executor writes its counters, outside any
22 /// critical section. Never call this from a task of an interrupt executor
23 /// that can preempt an instrumented executor on the same core.
24 pub async fn get_metrics(&self) -> Option<MetricsSnapshot> {
25 #[cfg(feature = "metrics")]
26 {
27 use crate::metrics::{ExecutorCounters, TaskMetrics, ticks_to_nanoseconds};
28 use alloc::vec::Vec;
29
30 let inner = self.0.read().await;
31 let tasks = inner
32 .tasks
33 .iter()
34 .map(|(identifier, metadata)| {
35 let sample = metadata
36 .instrumentation
37 .map(|instrumentation| instrumentation.task.sample())
38 .unwrap_or_default();
39 TaskMetrics {
40 identifier: *identifier,
41 generation: metadata.generation,
42 name: metadata.name.clone(),
43 spawner: metadata.spawner_identifier,
44 executor: metadata
45 .instrumentation
46 .map(|instrumentation| instrumentation.executor.executor()),
47 polls: sample.polls,
48 busy_nanoseconds: ticks_to_nanoseconds(sample.busy),
49 longest_poll_nanoseconds: ticks_to_nanoseconds(sample.longest_poll),
50 }
51 })
52 .collect();
53 drop(inner);
54 // Copy the counters out of the critical section and sample them
55 // after releasing it: readers spin while a writer is active, and
56 // that writer may need the critical section on another core.
57 let counters = self.1.lock(|spawners| {
58 let spawners = spawners.borrow();
59 let mut counters: Vec<(SpawnerIdentifier, &'static ExecutorCounters)> =
60 Vec::with_capacity(spawners.len());
61 // Registrations iterate in increasing identifier order, so
62 // each executor is reported under its lowest registration.
63 for (identifier, state) in spawners.iter() {
64 if !counters
65 .iter()
66 .any(|(_, known)| core::ptr::eq(*known, state.counters))
67 {
68 counters.push((*identifier, state.counters));
69 }
70 }
71 counters
72 });
73 let executors = counters
74 .into_iter()
75 .map(|(identifier, counters)| counters.sample(identifier))
76 .collect();
77 Some(MetricsSnapshot {
78 timestamp_nanoseconds: ticks_to_nanoseconds(crate::metrics::now()),
79 tasks,
80 executors,
81 })
82 }
83 #[cfg(not(feature = "metrics"))]
84 None
85 }
86}