Skip to main content

miden_debug_engine/profiling/instrument/
op_histogram_global.rs

1use miden_core::operations::Operation;
2
3use super::{Instrument, InstrumentRegistration};
4use crate::{profiling::helpers::op_histogram::OpHistogram, register_instrument};
5
6/// An [`Instrument`] to create global operation histograms.
7///
8/// The global histogram aggregates across all procedures over the entire runtime.
9///
10/// At each cycle, it records the current operation and produces a histogram of executed operations
11/// weighted by cycles per operation. If `opX` takes 4 cycles and was executed twice, its count
12/// will be 8.
13#[derive(Default)]
14pub struct OpHistogramGlobal {
15    hist: OpHistogram,
16}
17
18impl InstrumentRegistration for OpHistogramGlobal {
19    const NAME: &'static str = "op-histogram-global";
20
21    fn build(_config: &crate::profiling::ProfilerConfig) -> Result<Self, super::InstrumentError> {
22        Ok(Self::default())
23    }
24}
25
26register_instrument!(OpHistogramGlobal);
27
28impl Instrument for OpHistogramGlobal {
29    fn name(&self) -> &'static str {
30        Self::NAME
31    }
32
33    fn on_operation_execution_cycle(&mut self, op: Operation, _proc: Option<&str>) {
34        // The global histogram aggregates over all procedures, ignoring `proc`.
35        self.hist.record(op);
36    }
37
38    fn write_report_to(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
39        writer.write_all(self.hist.report().as_bytes())
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use miden_core::operations::Operation;
46
47    use super::OpHistogramGlobal;
48    use crate::profiling::instrument::Instrument;
49
50    #[test]
51    fn op_histogram_reports_recorded_ops() {
52        let mut hist = OpHistogramGlobal::default();
53
54        // 3 cycles, procedure names are ignored by the global histogram.
55        hist.on_operation_execution_cycle(Operation::Add, Some("main"));
56        hist.on_operation_execution_cycle(Operation::Add, Some("sum"));
57        hist.on_operation_execution_cycle(Operation::Noop, None);
58
59        let mut buf = Vec::new();
60        hist.write_report_to(&mut buf).unwrap();
61        let report = String::from_utf8(buf).unwrap();
62
63        let lines: Vec<&str> = report.lines().collect();
64        // First line is the header reporting 100% of 3 cycles.
65        assert!(lines[0].contains("total_cycles") && lines[0].contains('3'));
66
67        // `Add` must preceed `Noop`, due to higher count.
68        assert!(lines[1].contains(Operation::Add.to_string().as_str()) && lines[1].contains("2"));
69        assert!(lines[2].contains(Operation::Noop.to_string().as_str()) && lines[2].contains("1"));
70
71        // no further lines
72        assert_eq!(lines.len(), 3);
73    }
74
75    /// The global histogram must not be affected by which procedure an op is recorded in.
76    #[test]
77    fn op_histogram_ignores_procedure() {
78        let mut across_procs = OpHistogramGlobal::default();
79        let mut single_proc = OpHistogramGlobal::default();
80
81        across_procs.on_operation_execution_cycle(Operation::Add, Some("sum"));
82        across_procs.on_operation_execution_cycle(Operation::Add, Some("main"));
83        across_procs.on_operation_execution_cycle(Operation::Noop, None);
84
85        single_proc.on_operation_execution_cycle(Operation::Add, Some("main"));
86        single_proc.on_operation_execution_cycle(Operation::Add, Some("main"));
87        single_proc.on_operation_execution_cycle(Operation::Noop, Some("main"));
88
89        assert_eq!(across_procs.hist.report(), single_proc.hist.report());
90    }
91}