use crate::{Result, RustTorchError};
use serde::Serialize;
use std::{collections::HashMap, sync::Mutex, thread::ThreadId, time::Instant};
#[derive(Debug, Serialize)]
struct Event {
name: String,
cat: &'static str,
ph: &'static str,
pid: u32,
tid: u64,
ts: f64,
dur: f64,
args: EventArgs,
#[serde(skip)]
finished: bool,
}
#[derive(Debug, Serialize)]
struct EventArgs {
outcome: &'static str,
}
#[derive(Debug, Default)]
struct Events {
values: Vec<Event>,
threads: HashMap<ThreadId, u64>,
}
#[derive(Debug)]
pub struct Profiler {
origin: Instant,
capacity: usize,
events: Mutex<Events>,
}
impl Profiler {
pub fn new(capacity: usize) -> Result<Self> {
if !(1..=1_000_000).contains(&capacity) {
return Err(invalid("event capacity must be in 1..=1_000_000"));
}
Ok(Self {
origin: Instant::now(),
capacity,
events: Mutex::new(Events::default()),
})
}
pub fn span(&self, label: impl Into<String>) -> Result<Span<'_>> {
let name = label.into();
if name.is_empty() || name.len() > 1024 {
return Err(invalid("span label must contain 1..=1024 bytes"));
}
let mut events = self
.events
.lock()
.map_err(|_| invalid("profiler state poisoned"))?;
if events.values.len() == self.capacity {
return Err(invalid(
"profile event capacity reached; export and clear the completed window",
));
}
let next = events.threads.len() as u64 + 1;
let thread = std::thread::current().id();
let tid = *events.threads.entry(thread).or_insert(next);
events
.values
.try_reserve(1)
.map_err(|_| invalid("cannot reserve trace event"))?;
let index = events.values.len();
let start = Instant::now();
events.values.push(Event {
name,
cat: "rusttorch.application",
ph: "X",
pid: std::process::id(),
tid,
ts: start.duration_since(self.origin).as_secs_f64() * 1_000_000.,
dur: 0.,
args: EventArgs {
outcome: "complete",
},
finished: false,
});
Ok(Span {
profiler: self,
index,
start,
failed: false,
thread_bound: std::marker::PhantomData,
})
}
pub fn record<T>(
&self,
label: impl Into<String>,
work: impl FnOnce() -> Result<T>,
) -> Result<T> {
let mut span = self.span(label)?;
let result = work();
span.failed = result.is_err();
result
}
pub fn chrome_trace(&self) -> Result<String> {
let events = self
.events
.lock()
.map_err(|_| invalid("profiler state poisoned"))?;
if events.values.iter().any(|event| !event.finished) {
return Err(invalid("finish active spans before exporting"));
}
#[derive(Serialize)]
struct Trace<'a> {
#[serde(rename = "traceEvents")]
events: &'a [Event],
#[serde(rename = "displayTimeUnit")]
unit: &'static str,
}
serde_json::to_string(&Trace {
events: &events.values,
unit: "ms",
})
.map_err(|error| invalid(&format!("cannot serialize trace: {error}")))
}
pub fn clear(&self) -> Result<()> {
let mut events = self
.events
.lock()
.map_err(|_| invalid("profiler state poisoned"))?;
if events.values.iter().any(|event| !event.finished) {
return Err(invalid("finish active spans before clearing"));
}
events.values.clear();
events.threads.clear();
Ok(())
}
}
#[derive(Debug)]
pub struct Span<'a> {
profiler: &'a Profiler,
index: usize,
start: Instant,
failed: bool,
thread_bound: std::marker::PhantomData<std::rc::Rc<()>>,
}
impl Drop for Span<'_> {
fn drop(&mut self) {
if let Ok(mut events) = self.profiler.events.lock() {
let event = &mut events.values[self.index];
event.dur = self.start.elapsed().as_secs_f64() * 1_000_000.;
event.args.outcome = if std::thread::panicking() {
"panic"
} else if self.failed {
"error"
} else {
"complete"
};
event.finished = true;
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BenchmarkOptions {
pub warmup: usize,
pub samples: usize,
}
impl Default for BenchmarkOptions {
fn default() -> Self {
Self {
warmup: 5,
samples: 20,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Measurement {
pub seconds: Vec<f64>,
pub mean: f64,
pub median: f64,
pub standard_deviation: f64,
pub minimum: f64,
pub maximum: f64,
}
pub fn benchmark<T>(
options: BenchmarkOptions,
mut work: impl FnMut() -> Result<T>,
mut synchronize: impl FnMut() -> Result<()>,
) -> Result<Measurement> {
if options.samples == 0 || options.samples > 1_000_000 || options.warmup > 1_000_000 {
return Err(invalid(
"benchmark requires 1..=1_000_000 samples and at most 1_000_000 warmup calls",
));
}
for _ in 0..options.warmup {
synchronize()?;
let output = work()?;
synchronize()?;
std::hint::black_box(output);
}
let mut seconds = Vec::new();
seconds
.try_reserve_exact(options.samples)
.map_err(|_| invalid("cannot reserve benchmark samples"))?;
for _ in 0..options.samples {
synchronize()?;
let start = Instant::now();
let output = work()?;
synchronize()?;
seconds.push(start.elapsed().as_secs_f64());
std::hint::black_box(output);
}
let mean = seconds.iter().sum::<f64>() / seconds.len() as f64;
let standard_deviation =
(seconds.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / seconds.len() as f64).sqrt();
let mut sorted = seconds.clone();
sorted.sort_by(f64::total_cmp);
let middle = sorted.len() / 2;
let median = if sorted.len() % 2 == 0 {
(sorted[middle - 1] + sorted[middle]) / 2.
} else {
sorted[middle]
};
Ok(Measurement {
minimum: sorted[0],
maximum: sorted[sorted.len() - 1],
mean,
median,
standard_deviation,
seconds,
})
}
fn invalid(reason: &str) -> RustTorchError {
RustTorchError::InvalidConfiguration {
field: "profiling",
reason: reason.into(),
}
}