Skip to content

Split measurement of walltime and perf. counters #1465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions collector/benchlib/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ impl BenchmarkSuite {

/// Registers a single benchmark.
/// `func` should return a closure that will be benchmarked.
pub fn register<F: Fn() -> Bench + 'static, R, Bench: FnOnce() -> R + 'static>(
pub fn register<F: Fn() -> Bench + Clone + 'static, R, Bench: FnOnce() -> R + 'static>(
&mut self,
name: &'static str,
func: F,
constructor: F,
) {
// We want to monomorphize the target `func` and then wrap it in a Box, to avoid going
// through a vtable when we execute the benchmarked function.
let benchmark_func = Box::new(move || {
let bench_fn = func();
benchmark_function(name, bench_fn)
});
// We want to type-erase the target `func` by wrapping it in a Box.
let benchmark_func = Box::new(move || benchmark_function(name, constructor.clone()));
let benchmark_def = BenchmarkWrapper {
func: benchmark_func,
};
Expand Down
29 changes: 19 additions & 10 deletions collector/benchlib/src/measure/perf_counter/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,42 @@ struct Counters {
cache_references: Counter,
}

/// Benchmarks a single function.
pub fn benchmark_function<F: FnOnce() -> R, R>(
/// Benchmarks a single function generated by `benchmark_constructor`.
/// The function is executed twice, once to gather wall-time measurement and the second time to
/// gather perf. counters.
pub fn benchmark_function<F: Fn() -> Bench + 'static, R, Bench: FnOnce() -> R + 'static>(
name: &'static str,
func: F,
benchmark_constructor: F,
) -> anyhow::Result<BenchmarkResult> {
let mut group = create_group()?;
let counters = prepare_counters(&mut group)?;

// FIXME: don't run perf counters and time measurements together, run the benchmark twice
// instead.
let start = Instant::now();
// Measure perf. counters.
let func = benchmark_constructor();

// Do not act on the return value to avoid including the branch in the measurement
let enable_ret = group.enable();
let output = func();
group.disable()?;

let duration = start.elapsed();
// Try to avoid optimizing the result out.
black_box(output);

// Check if we have succeeded before
enable_ret?;

// Try to avoid optimizing the result out
black_box(output);

let measurement = group.read()?;

// Measure wall time.
let func = benchmark_constructor();

let start = Instant::now();
let output = func();
let duration = start.elapsed();

// Try to avoid optimizing the result out.
black_box(output);

let result = BenchmarkResult {
name: String::from(name),
cycles: measurement[&counters.cycles],
Expand Down