Skip to content

Commit

Permalink
Add API to get raw counter values
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuzhao committed Mar 28, 2024
1 parent c8ac6d7 commit 44515e6
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ license = "MIT"

[workspace.dependencies]
harness = { path = "./harness", version = "0.0" }
harness-macros = { path = "./harness/macros", version = "0.0.1" }
harness-probe-perf = { path = "./probes/perf", version = "0.0.4" }
harness-macros = { path = "./harness/macros", version = "0.0.2" }
harness-probe-perf = { path = "./probes/perf", version = "0.0.5" }
2 changes: 1 addition & 1 deletion harness/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "harness"
version = "0.0.5"
version = "0.0.6"
description = "Precise and reproducible benchmarking"
repository = "https://github.com/wenyuzhao/harness"
homepage = "https://github.com/wenyuzhao/harness"
Expand Down
2 changes: 1 addition & 1 deletion harness/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "harness-macros"
version = "0.0.1"
version = "0.0.2"
description = "Procedural macros for the harness crate"
repository = "https://github.com/wenyuzhao/harness"
homepage = "https://github.com/wenyuzhao/harness"
Expand Down
2 changes: 1 addition & 1 deletion harness/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn probe(_attr: TokenStream, item: TokenStream) -> TokenStream {
#input

#[no_mangle]
pub extern "C" fn harness_register_probe(probes: &mut ProbeManager) {
pub extern "C" fn harness_register_probe(probes: &mut ::harness::probe::ProbeManager) {
probes.register(Box::new(#name::default()));
}
};
Expand Down
16 changes: 14 additions & 2 deletions harness/src/bencher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a> Drop for BenchTimer<'a> {
*state = BencherState::AfterTiming;
}
let elapsed = self.start_time.elapsed();
self.bencher.timing_end();
self.bencher.timing_end(elapsed.clone());
let mut lock = self.bencher.elapsed.lock().unwrap();
assert!(lock.is_none(), "More than one benchmark timer detected");
*lock = Some(elapsed);
Expand Down Expand Up @@ -142,12 +142,13 @@ impl Bencher {
)
}

fn timing_end(&self) {
fn timing_end(&self, walltime: Duration) {
let mut probes = self.probes.borrow_mut();
probes.end(
&self.bench,
self.current_iteration,
!self.is_timing_iteration(),
walltime,
)
}

Expand Down Expand Up @@ -235,6 +236,17 @@ impl Bencher {
.unwrap()
.push((name.as_ref().to_owned(), Box::new(value)));
}

/// Returns the wall-clock time of the last timing phase.
/// Returns `None` if the timing phase has not finished yet.
pub fn get_walltime(&self) -> Option<Duration> {
self.elapsed.lock().unwrap().clone()
}

/// Returns the value of a counter as a floating point number.
pub fn get_raw_counter_value(&self, name: impl AsRef<str>) -> Option<f32> {
self.probes.borrow().get_value(name.as_ref())
}
}

pub struct SingleBenchmarkRunner {
Expand Down
37 changes: 26 additions & 11 deletions harness/src/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::bencher::{StatPrintFormat, Value};

#[derive(Default)]
struct Counters {
counters: Vec<(String, f32)>,
}

impl Counters {
pub(crate) fn new(walltime: Duration) -> Self {
Self {
counters: vec![("time".to_owned(), walltime.as_micros() as f32 / 1000.0)],
}
}

fn merge(&mut self, values: HashMap<String, f32>) {
let mut values = values.iter().collect::<Vec<_>>();
values.sort_by_key(|x| x.0.as_str());
for (k, v) in values {
self.counters.push((k.clone(), *v));
}
}

fn get_value(&self, name: &str) -> Option<f32> {
self.counters
.iter()
.find(|(k, _)| k == name)
.map(|(_, v)| *v)
}
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -76,7 +88,6 @@ impl Probe for BaseProbe {
}

pub struct ProbeManager {
base_probe: BaseProbe,
probes: Vec<Box<dyn Probe>>,
counters: Counters,
libraries: Vec<Library>,
Expand All @@ -85,9 +96,8 @@ pub struct ProbeManager {
impl ProbeManager {
pub(crate) fn new() -> Self {
Self {
base_probe: BaseProbe::default(),
probes: vec![],
counters: Counters::default(),
counters: Counters::new(Duration::ZERO),
libraries: vec![],
}
}
Expand Down Expand Up @@ -122,42 +132,47 @@ impl ProbeManager {
}
probe_args.push(Some(args));
}
self.base_probe.init(ProbeArgs::default());
for (i, probe) in self.probes.iter_mut().enumerate() {
let args = probe_args[i].take().unwrap();
probe.init(args);
}
}

pub(crate) fn deinit(&mut self) {
self.base_probe.deinit();
for probe in self.probes.iter_mut() {
probe.deinit();
}
}

pub(crate) fn begin(&mut self, benchmark: &str, iteration: usize, warmup: bool) {
self.base_probe.begin(benchmark, iteration, warmup);
for probe in self.probes.iter_mut() {
probe.begin(benchmark, iteration, warmup)
}
}

pub(crate) fn end(&mut self, benchmark: &str, iteration: usize, warmup: bool) {
pub(crate) fn end(
&mut self,
benchmark: &str,
iteration: usize,
warmup: bool,
walltime: Duration,
) {
// harness_end
self.base_probe.end(benchmark, iteration, warmup);
for probe in self.probes.iter_mut() {
probe.end(benchmark, iteration, warmup)
}
// report values
let mut counters = Counters::default();
counters.merge(self.base_probe.report());
let mut counters = Counters::new(walltime);
for probe in self.probes.iter_mut() {
counters.merge(probe.report());
}
self.counters = counters;
}

pub(crate) fn get_value(&self, name: &str) -> Option<f32> {
self.counters.get_value(name)
}

fn dump_counters_stderr_table(&self, stats: &[(String, Box<dyn Value>)]) {
for (name, _) in stats {
eprint!("{}\t", name);
Expand Down
2 changes: 1 addition & 1 deletion probes/perf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "harness-probe-perf"
version = "0.0.4"
version = "0.0.5"
description = "harness probe for reporting linux perf-event counter values"
repository = "https://github.com/wenyuzhao/harness"
homepage = "https://github.com/wenyuzhao/harness"
Expand Down
2 changes: 1 addition & 1 deletion probes/perf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(target_os = "linux")]
use std::collections::HashMap;

use harness::probe::Probe;
#[cfg(target_os = "linux")]
use harness::probe::ProbeArgs;
use harness::probe::{Probe, ProbeManager};

#[harness::probe]
#[derive(Default)]
Expand Down

0 comments on commit 44515e6

Please sign in to comment.