|
| 1 | +//! Serializable snapshot of stage profiling metrics, for remote introspection. |
| 2 | +
|
| 3 | +extern crate alloc; |
| 4 | +use alloc::{string::String, vec::Vec}; |
| 5 | + |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | +use crate::profiling::{RecordProfilingMetrics, StageEntry}; |
| 9 | + |
| 10 | +/// A point-in-time snapshot of one execution stage's timing metrics. |
| 11 | +/// |
| 12 | +/// Carried in `RecordMetadata::stage_profiling` and exposed by the |
| 13 | +/// `get_stage_profiling` MCP tool. All times are wall-clock nanoseconds. |
| 14 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 15 | +pub struct StageProfilingInfo { |
| 16 | + /// Stage kind: `"source"`, `"tap"`, `"link"`, or `"transform"`. |
| 17 | + pub stage_type: String, |
| 18 | + /// Registration index within the stage kind (0-based). |
| 19 | + pub index: usize, |
| 20 | + /// Name assigned via `.with_name("...")`, if any. |
| 21 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 22 | + pub name: Option<String>, |
| 23 | + /// Number of recorded invocations. |
| 24 | + pub call_count: u64, |
| 25 | + /// Cumulative wall-clock time, nanoseconds. |
| 26 | + pub total_time_ns: u64, |
| 27 | + /// Mean wall-clock time per invocation, nanoseconds. |
| 28 | + pub avg_time_ns: u64, |
| 29 | + /// Fastest recorded invocation, nanoseconds. |
| 30 | + pub min_time_ns: u64, |
| 31 | + /// Slowest recorded invocation, nanoseconds. |
| 32 | + pub max_time_ns: u64, |
| 33 | +} |
| 34 | + |
| 35 | +impl StageProfilingInfo { |
| 36 | + fn from_entry(stage_type: &str, index: usize, entry: &StageEntry) -> Self { |
| 37 | + let m = &entry.metrics; |
| 38 | + Self { |
| 39 | + stage_type: String::from(stage_type), |
| 40 | + index, |
| 41 | + name: entry.name.clone(), |
| 42 | + call_count: m.call_count(), |
| 43 | + total_time_ns: m.total_time_ns(), |
| 44 | + avg_time_ns: m.avg_time_ns(), |
| 45 | + min_time_ns: m.min_time_ns(), |
| 46 | + max_time_ns: m.max_time_ns(), |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl RecordProfilingMetrics { |
| 52 | + /// Returns a serializable snapshot of every registered stage's metrics, |
| 53 | + /// ordered sources → taps → links → transforms. |
| 54 | + pub fn snapshot(&self) -> Vec<StageProfilingInfo> { |
| 55 | + let mut out = Vec::new(); |
| 56 | + for (kind, stages) in [ |
| 57 | + ("source", self.sources()), |
| 58 | + ("tap", self.taps()), |
| 59 | + ("link", self.links()), |
| 60 | + ] { |
| 61 | + for (i, entry) in stages.iter().enumerate() { |
| 62 | + out.push(StageProfilingInfo::from_entry(kind, i, entry)); |
| 63 | + } |
| 64 | + } |
| 65 | + out |
| 66 | + } |
| 67 | +} |
0 commit comments