use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TraceFormat {
None,
Human,
Chrome,
Flamegraph,
}
impl FromStr for TraceFormat {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"none" => Ok(Self::None),
"human" => Ok(Self::Human),
"chrome" => Ok(Self::Chrome),
"flamegraph" => Ok(Self::Flamegraph),
other => Err(format!(
"Fix: unsupported trace format `{other}`. Use one of none,human,chrome,flamegraph."
)),
}
}
}
impl TraceFormat {
#[inline]
pub fn values() -> &'static str {
"none,human,chrome,flamegraph"
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraceRecord {
pub name: String,
pub layer_id: Option<String>,
pub op_id: Option<String>,
pub verdict: Option<String>,
pub start_us: u128,
pub duration_us: u128,
pub stack: Vec<String>,
pub thread_id: u64,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TraceSnapshot {
pub records: Vec<TraceRecord>,
}
impl TraceSnapshot {
#[inline]
pub fn layer_records(&self) -> impl Iterator<Item = &TraceRecord> {
self.records
.iter()
.filter(|record| record.layer_id.as_deref().is_some_and(is_numbered_layer))
}
}
fn is_numbered_layer(layer: &str) -> bool {
matches!(
layer,
"L1" | "L2" | "L3" | "L4" | "L5" | "L6" | "L7" | "L8" | "L9"
)
}