Skip to content

Commit 0ab5bd5

Browse files
antoniolocascio0xVolosnikov
authored andcommitted
fix: Fixes for cycle marker and opcode stats (#11)
## What ❔ <!-- What are the changes this PR brings about? --> <!-- Example: This PR adds a PR template to the repo. --> <!-- (For bigger PRs adding more context is appreciated) --> ## Why ❔ <!-- Why are these changes done? What goal do they contribute to? What are the principles behind them? --> <!-- The `Why` has to be clear to non-Matter Labs entities running their own ZK Chain --> <!-- Example: PR templates ensure PR reviewers, observers, and future iterators are in context about the evolution of repos. --> ## Is this a breaking change? - [ ] Yes - [ ] No ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted.
1 parent f0b4524 commit 0ab5bd5

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

risc_v_simulator/src/cycle/state.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub const MARKER_CSR: u32 = 0x7ff;
2828
#[cfg(feature = "opcode_stats")]
2929
thread_local! {
3030

31-
static OPCODES_COUNTER: std::cell::RefCell<std::collections::HashMap<&'static str, usize>> = std::cell::RefCell::new(std::collections::HashMap::new());
31+
pub(crate) static OPCODES_COUNTER: std::cell::RefCell<std::collections::HashMap<&'static str, usize>> = std::cell::RefCell::new(std::collections::HashMap::new());
3232
}
3333

3434
#[inline(always)]
@@ -89,36 +89,42 @@ impl Mark {
8989
#[cfg(feature = "cycle_marker")]
9090
#[derive(Debug, Default)]
9191
pub struct CycleMarker {
92+
cycle_counter: u64,
9293
pub markers: Vec<Mark>,
9394
pub delegation_counter: HashMap<u32, u64>,
9495
}
9596

9697
#[cfg(feature = "cycle_marker")]
9798
impl CycleMarker {
98-
fn new() -> Self {
99+
pub(crate) fn new() -> Self {
99100
Self::default()
100101
}
101102

102103
#[inline(always)]
103-
fn add_marker(&mut self, count: u64) {
104+
pub(crate) fn add_marker(&mut self) {
104105
self.markers.push(Mark {
105-
cycles: count,
106+
cycles: self.cycle_counter,
106107
delegations: self.delegation_counter.clone(),
107108
})
108109
}
109110

110111
#[inline(always)]
111-
fn add_delegation(&mut self, id: u32) {
112+
pub(crate) fn add_delegation(&mut self, id: u32) {
112113
self.delegation_counter
113114
.entry(id)
114115
.and_modify(|n| *n += 1)
115116
.or_insert(1);
116117
}
118+
119+
#[inline(always)]
120+
pub(crate) fn incr_cycle_counter(&mut self) {
121+
self.cycle_counter += 1
122+
}
117123
}
118124

119125
#[cfg(feature = "cycle_marker")]
120126
thread_local! {
121-
static CYCLE_MARKER: std::cell::RefCell<CycleMarker> = std::cell::RefCell::new(CycleMarker::new());
127+
pub(crate) static CYCLE_MARKER: std::cell::RefCell<CycleMarker> = std::cell::RefCell::new(CycleMarker::new());
122128
}
123129

124130
#[cfg(feature = "cycle_marker")]
@@ -264,7 +270,6 @@ pub struct RiscV32State<Config: MachineConfig = IMStandardIsaConfig> {
264270
pub pc: u32,
265271
pub extra_flags: ExtraFlags, // everything that doesn't need full register
266272

267-
pub cycle_counter: usize,
268273
pub timer: u64,
269274
pub timer_match: u64,
270275

@@ -311,7 +316,6 @@ impl<Config: MachineConfig> RiscV32State<Config> {
311316
registers,
312317
pc,
313318
extra_flags,
314-
cycle_counter,
315319
timer,
316320
timer_match,
317321
machine_mode_trap_data,
@@ -360,7 +364,6 @@ impl<Config: MachineConfig> RiscV32State<Config> {
360364
pc: initial_pc,
361365
extra_flags,
362366

363-
cycle_counter,
364367
timer,
365368
timer_match,
366369

@@ -446,7 +449,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
446449
#[inline(always)]
447450
fn add_marker(&self) {
448451
#[cfg(feature = "cycle_marker")]
449-
CYCLE_MARKER.with_borrow_mut(|cm| cm.add_marker(self.cycle_counter as u64))
452+
CYCLE_MARKER.with_borrow_mut(|cm| cm.add_marker())
450453
}
451454

452455
#[inline(always)]
@@ -455,6 +458,12 @@ impl<Config: MachineConfig> RiscV32State<Config> {
455458
CYCLE_MARKER.with_borrow_mut(|cm| cm.add_delegation(id))
456459
}
457460

461+
#[inline(always)]
462+
fn count_new_cycle_for_markers(&self) {
463+
#[cfg(feature = "cycle_marker")]
464+
CYCLE_MARKER.with_borrow_mut(|cm| cm.incr_cycle_counter())
465+
}
466+
458467
pub fn cycle_ext<
459468
'a,
460469
M: MemorySource,
@@ -1394,10 +1403,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
13941403

13951404
// Handle traps and interrupts.
13961405
if trap.is_a_trap() {
1397-
println!(
1398-
"trap: {:?}, pc: {:08x}, proc_cycle: {:?}, instr: {:08x}",
1399-
trap, pc, self.cycle_counter, instr
1400-
);
1406+
println!("trap: {:?}, pc: {:08x}, instr: {:08x}", trap, pc, instr);
14011407

14021408
if Config::HANDLE_EXCEPTIONS == false {
14031409
panic!("Simulator encountered an exception");
@@ -1440,7 +1446,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
14401446

14411447
// for debugging
14421448
self.sapt = mmu.read_sapt(current_privilege_mode, &mut trap);
1443-
self.cycle_counter += 1;
1449+
self.count_new_cycle_for_markers();
14441450
tracer.at_cycle_end(&*self);
14451451

14461452
//let trap = trap.as_register_value();

risc_v_simulator/src/cycle/state_new.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ use crate::cycle::state::report_opcode;
1515
use crate::cycle::state::MARKER_CSR;
1616
use crate::cycle::state::NON_DETERMINISM_CSR;
1717
use crate::cycle::state::NUM_REGISTERS;
18+
#[cfg(feature = "opcode_stats")]
19+
use crate::cycle::state::OPCODES_COUNTER;
20+
#[cfg(feature = "cycle_marker")]
21+
use crate::cycle::state::{CycleMarker, Mark, CYCLE_MARKER};
1822
use crate::cycle::status_registers::TrapReason;
1923
use crate::cycle::IMStandardIsaConfig;
2024
use crate::cycle::MachineConfig;
@@ -148,7 +152,7 @@ impl<Config: MachineConfig> RiscV32StateForUnrolledProver<Config> {
148152
#[inline(always)]
149153
fn add_marker(&self) {
150154
#[cfg(feature = "cycle_marker")]
151-
CYCLE_MARKER.with_borrow_mut(|cm| cm.add_marker(self.cycle_counter as u64))
155+
CYCLE_MARKER.with_borrow_mut(|cm| cm.add_marker())
152156
}
153157

154158
#[inline(always)]
@@ -157,6 +161,12 @@ impl<Config: MachineConfig> RiscV32StateForUnrolledProver<Config> {
157161
CYCLE_MARKER.with_borrow_mut(|cm| cm.add_delegation(id))
158162
}
159163

164+
#[inline(always)]
165+
fn count_new_cycle_for_markers(&self) {
166+
#[cfg(feature = "cycle_marker")]
167+
CYCLE_MARKER.with_borrow_mut(|cm| cm.incr_cycle_counter())
168+
}
169+
160170
#[inline(always)]
161171
fn decoder_step<M: MemorySource, TR: Tracer<Config>>(
162172
&mut self,
@@ -854,6 +864,8 @@ impl<Config: MachineConfig> RiscV32StateForUnrolledProver<Config> {
854864
}
855865
}
856866

867+
self.count_new_cycle_for_markers();
868+
857869
tracer.at_cycle_end_ext(&*self);
858870

859871
if self.pc == pc {

risc_v_simulator/src/runner/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ pub fn run_simple_for_num_cycles<S: NonDeterminismCSRSource<VectorMemoryImpl>, C
8787
memory.load_image(entry_point, binary.iter().copied());
8888

8989
let mut previous_pc = entry_point;
90+
let mut cycle_counter = 0u64;
9091

9192
for _cycle in 0..cycles as usize {
93+
cycle_counter += 1;
9294
RiscV32State::<C>::cycle(
9395
&mut state,
9496
&mut memory,
@@ -98,7 +100,7 @@ pub fn run_simple_for_num_cycles<S: NonDeterminismCSRSource<VectorMemoryImpl>, C
98100
);
99101

100102
if state.pc == previous_pc {
101-
println!("Took {} cycles to finish", state.cycle_counter);
103+
println!("Took {} cycles to finish", cycle_counter);
102104
break;
103105
}
104106
previous_pc = state.pc;

0 commit comments

Comments
 (0)