Skip to content

Commit

Permalink
fix: must use clk not pc for sampling rate
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Nov 14, 2024
1 parent e14a380 commit 4837b83
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions crates/core/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<'a> Executor<'a> {
.ok()
.and_then(|rate| {
println!("Profiling sample rate: {rate}");
rate.parse::<u64>().ok()
rate.parse::<u32>().ok()
})
.unwrap_or(1);

Expand Down Expand Up @@ -1662,7 +1662,7 @@ impl<'a> Executor<'a> {
fn log(&mut self, _: &Instruction) {
if let Some((ref mut profiler, _)) = self.profiler {
if !self.unconstrained {
profiler.record(self.state.pc.into());
profiler.record(self.state.clk, self.state.pc as u64);
}
}

Expand Down
14 changes: 7 additions & 7 deletions crates/core/executor/src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum ProfilerError {
/// During exeuction, the profiler always keeps track of the callstack
/// and will occasionally save the stack according to the sample rate.
pub struct Profiler {
sample_rate: u64,
sample_rate: u32,
/// `start_address`-> index in `function_ranges`
start_lookup: HashMap<u64, usize>,
/// the start and end of the function
Expand All @@ -44,7 +44,7 @@ struct Sample {
}

impl Profiler {
pub(super) fn new(elf_bytes: &[u8], sample_rate: u64) -> Result<Self, ProfilerError> {
pub(super) fn new(elf_bytes: &[u8], sample_rate: u32) -> Result<Self, ProfilerError> {
let elf = Elf::parse(elf_bytes)?;

let mut start_lookup = HashMap::new();
Expand Down Expand Up @@ -91,10 +91,10 @@ impl Profiler {
})
}

pub(super) fn record(&mut self, pc: u64) {
pub(super) fn record(&mut self, clk: u32, pc: u64) {
// We are still in the current function.
if pc > self.current_function_range.0 && pc <= self.current_function_range.1 {
if pc % self.sample_rate == 0 {
if clk % self.sample_rate == 0 {
self.samples.push(Sample { stack: self.function_stack.clone() });
}

Expand Down Expand Up @@ -141,7 +141,7 @@ impl Profiler {
// so we'll just increment the counts for everything in the stack.
}

if pc % self.sample_rate == 0 {
if clk % self.sample_rate == 0 {
self.samples.push(Sample { stack: self.function_stack.clone() });
}
}
Expand Down Expand Up @@ -181,10 +181,10 @@ impl Profiler {
sample.stack.into_iter(),
// We don't have a way to know the duration of each sample, so we just use 1us for
// all instructions
std::time::Duration::from_micros(self.sample_rate),
std::time::Duration::from_micros(self.sample_rate as u64),
);

last_known_time += std::time::Duration::from_micros(self.sample_rate);
last_known_time += std::time::Duration::from_micros(self.sample_rate as u64);
}

profile_builder.add_thread(self.builder);
Expand Down

0 comments on commit 4837b83

Please sign in to comment.