Skip to content

Commit d7ddfc7

Browse files
committed
feat(openvm_circuit): store span info in cycle tracker for convenient debug info
1 parent 3582ebf commit d7ddfc7

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

crates/vm/src/arch/segment.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,18 @@ impl<F: PrimeField32, VC: VmConfig<F>> ExecutionSegment<F, VC> {
281281
Some(SysPhantom::CtStart) =>
282282
{
283283
#[cfg(feature = "bench-metrics")]
284-
metrics
285-
.cycle_tracker
286-
.start(dsl_instr.cloned().unwrap_or("Default".to_string()))
284+
metrics.cycle_tracker.start(
285+
dsl_instr.cloned().unwrap_or("Default".to_string()),
286+
metrics.cycle_count,
287+
)
287288
}
288289
Some(SysPhantom::CtEnd) =>
289290
{
290291
#[cfg(feature = "bench-metrics")]
291-
metrics
292-
.cycle_tracker
293-
.end(dsl_instr.cloned().unwrap_or("Default".to_string()))
292+
metrics.cycle_tracker.end(
293+
dsl_instr.cloned().unwrap_or("Default".to_string()),
294+
metrics.cycle_count,
295+
)
294296
}
295297
_ => {}
296298
}

crates/vm/src/metrics/cycle_tracker/mod.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
/// Stats for a nested span in the execution segment that is tracked by the [`CycleTracker`].
2+
#[derive(Clone, Debug, Default)]
3+
pub struct SpanInfo {
4+
/// The name of the span.
5+
tag: String,
6+
/// The cycle count at which the span starts.
7+
start: usize,
8+
}
9+
110
#[derive(Clone, Debug, Default)]
211
pub struct CycleTracker {
312
/// Stack of span names, with most recent at the end
4-
stack: Vec<String>,
13+
stack: Vec<SpanInfo>,
14+
/// Depth of the stack.
15+
depth: usize,
516
}
617

718
impl CycleTracker {
@@ -11,23 +22,33 @@ impl CycleTracker {
1122

1223
/// Starts a new cycle tracker span for the given name.
1324
/// If a span already exists for the given name, it ends the existing span and pushes a new one to the vec.
14-
pub fn start(&mut self, mut name: String) {
25+
pub fn start(&mut self, mut name: String, cycles_count: usize) {
1526
// hack to remove "CT-" prefix
1627
if name.starts_with("CT-") {
1728
name = name.split_off(3);
1829
}
19-
self.stack.push(name);
30+
self.stack.push(SpanInfo {
31+
tag: name.clone(),
32+
start: cycles_count,
33+
});
34+
let padding = "│ ".repeat(self.depth);
35+
tracing::info!("{}┌╴{}", padding, name);
36+
self.depth += 1;
2037
}
2138

2239
/// Ends the cycle tracker span for the given name.
2340
/// If no span exists for the given name, it panics.
24-
pub fn end(&mut self, mut name: String) {
41+
pub fn end(&mut self, mut name: String, cycles_count: usize) {
2542
// hack to remove "CT-" prefix
2643
if name.starts_with("CT-") {
2744
name = name.split_off(3);
2845
}
29-
let stack_top = self.stack.pop();
30-
assert_eq!(stack_top.unwrap(), name, "Stack top does not match name");
46+
let SpanInfo { tag, start } = self.stack.pop().unwrap();
47+
assert_eq!(tag, name, "Stack top does not match name");
48+
self.depth -= 1;
49+
let padding = "│ ".repeat(self.depth);
50+
let span_cycles = cycles_count - start;
51+
tracing::info!("{}└╴{} cycles", padding, span_cycles);
3152
}
3253

3354
/// Ends the current cycle tracker span.
@@ -37,7 +58,11 @@ impl CycleTracker {
3758

3859
/// Get full name of span with all parent names separated by ";" in flamegraph format
3960
pub fn get_full_name(&self) -> String {
40-
self.stack.join(";")
61+
self.stack
62+
.iter()
63+
.map(|span_info| span_info.tag.clone())
64+
.collect::<Vec<String>>()
65+
.join(";")
4166
}
4267
}
4368

extensions/native/compiler/src/constraints/halo2/compiler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,11 @@ impl<C: Config + Debug> Halo2ConstraintCompiler<C> {
492492
}
493493
DslIr::CycleTrackerStart(_name) => {
494494
#[cfg(feature = "bench-metrics")]
495-
cell_tracker.start(_name);
495+
cell_tracker.start(_name, 0);
496496
}
497497
DslIr::CycleTrackerEnd(_name) => {
498498
#[cfg(feature = "bench-metrics")]
499-
cell_tracker.end(_name);
499+
cell_tracker.end(_name, 0);
500500
}
501501
DslIr::CircuitPublish(val, index) => {
502502
public_values[index] = vars[&val.0];

0 commit comments

Comments
 (0)