openvm_circuit/metrics/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

pub mod cycle_tracker;

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct VmMetrics {
    pub chip_heights: Vec<(String, usize)>,
    /// Maps (dsl_ir, opcode) to number of times opcode was executed
    pub counts: BTreeMap<(Option<String>, String), usize>,
    /// Maps (dsl_ir, opcode, air_name) to number of trace cells generated by opcode
    pub trace_cells: BTreeMap<(Option<String>, String, String), usize>,
}

#[cfg(feature = "bench-metrics")]
mod emit {
    use metrics::counter;

    use super::VmMetrics;

    impl VmMetrics {
        pub fn emit(&self) {
            for (name, value) in self.chip_heights.iter() {
                let labels = [("chip_name", name.clone())];
                counter!("rows_used", &labels).absolute(*value as u64);
            }

            for ((dsl_ir, opcode), value) in self.counts.iter() {
                let labels = [
                    ("dsl_ir", dsl_ir.clone().unwrap_or_else(String::new)),
                    ("opcode", opcode.clone()),
                ];
                counter!("frequency", &labels).absolute(*value as u64);
            }

            for ((dsl_ir, opcode, air_name), value) in self.trace_cells.iter() {
                let labels = [
                    ("dsl_ir", dsl_ir.clone().unwrap_or_else(String::new)),
                    ("opcode", opcode.clone()),
                    ("air_name", air_name.clone()),
                ];
                counter!("cells_used", &labels).absolute(*value as u64);
            }
        }
    }
}