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::{OutputResult, OutputWriter, helpers::op_histogram::OpHistogram};
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
26#[cfg(feature = "std")]
27crate::register_instrument!(OpHistogramGlobal);
28
29impl Instrument for OpHistogramGlobal {
30    fn name(&self) -> &'static str {
31        Self::NAME
32    }
33
34    fn on_operation_execution_cycle(&mut self, op: Operation, _proc: Option<&str>) {
35        // The global histogram aggregates over all procedures, ignoring `proc`.
36        self.hist.record(op);
37    }
38
39    fn write_report_to(&self, writer: &mut dyn OutputWriter) -> OutputResult<()> {
40        writer.write_all(self.hist.report().as_bytes())
41    }
42}
43
44#[cfg(test)]
45mod tests;