Skip to content
Open
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
9 changes: 9 additions & 0 deletions crates/divan_compat/benches/thread_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ fn fib_in_thread_bench_local(bencher: codspeed_divan_compat::Bencher, n: usize)
})
}

#[cfg_attr(
not(codspeed),
codspeed_divan_compat::bench(threads = 4, sample_count = 1)
)]
#[cfg_attr(codspeed, codspeed_divan_compat::bench(sample_count = 1))]
fn divan_threads() {
codspeed_divan_compat::black_box(fibo(20));
}

fn main() {
codspeed_divan_compat::main();
}
44 changes: 29 additions & 15 deletions crates/divan_compat/divan_fork/src/bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,18 @@ impl<'a> BenchContext<'a> {
};

// Sample loop:
let ([start, end], alloc_info) =
let ([start, end], alloc_info, [benchmark_start, benchmark_end]) =
record_sample(sample_size as usize, barrier.as_ref(), &mut count_input);

RawSample { start, end, timer, alloc_info, counter_totals }
RawSample {
start,
end,
benchmark_start,
benchmark_end,
timer,
alloc_info,
counter_totals,
}
};

// Sample loop:
Expand All @@ -723,6 +731,11 @@ impl<'a> BenchContext<'a> {
std::slice::from_raw_parts(raw_samples.as_ptr().cast(), raw_samples.len())
}
};
let benchmark_start =
raw_samples.iter().map(|sample| sample.benchmark_start).min().unwrap();
let benchmark_end =
raw_samples.iter().map(|sample| sample.benchmark_end).max().unwrap();
InstrumentHooks::instance().add_benchmark_timestamps(benchmark_start, benchmark_end);

// If testing, exit the benchmarking loop immediately after timing a
// single run.
Expand Down Expand Up @@ -825,8 +838,11 @@ impl<'a> BenchContext<'a> {
gen_input: impl Fn() -> I,
benched: impl Fn(&UnsafeCell<MaybeUninit<I>>) -> O,
drop_input: impl Fn(&UnsafeCell<MaybeUninit<I>>),
) -> impl Fn(usize, Option<&Barrier>, &mut dyn FnMut(&I)) -> ([Timestamp; 2], ThreadAllocInfo)
{
) -> impl Fn(
usize,
Option<&Barrier>,
&mut dyn FnMut(&I),
) -> ([Timestamp; 2], ThreadAllocInfo, [u64; 2]) {
// We defer:
// - Usage of `gen_input` values.
// - Drop destructor for `O`, preventing it from affecting sample
Expand Down Expand Up @@ -897,8 +913,9 @@ impl<'a> BenchContext<'a> {
// benchmarking.
let sample_start: UntaggedTimestamp;
let sample_end: UntaggedTimestamp;
let benchmark_start: u64;
let benchmark_end: u64;

let instrument_hooks = InstrumentHooks::instance();
if size_of::<I>() == 0 && (size_of::<O>() == 0 || !mem::needs_drop::<O>()) {
// Use a range instead of `defer_store` to make the benchmarking
// loop cheaper.
Expand All @@ -915,7 +932,7 @@ impl<'a> BenchContext<'a> {

sync_threads(true);

let start_time = InstrumentHooks::current_timestamp();
benchmark_start = InstrumentHooks::current_timestamp();
sample_start = UntaggedTimestamp::start(timer_kind);

// Sample loop:
Expand All @@ -928,8 +945,7 @@ impl<'a> BenchContext<'a> {
}

sample_end = UntaggedTimestamp::end(timer_kind);
let end_time = InstrumentHooks::current_timestamp();
instrument_hooks.add_benchmark_timestamps(start_time, end_time);
benchmark_end = InstrumentHooks::current_timestamp();

sync_threads(false);
save_alloc_info();
Expand Down Expand Up @@ -972,7 +988,7 @@ impl<'a> BenchContext<'a> {
let defer_slots_iter = defer_slots_slice.iter();

sync_threads(true);
let start_time = InstrumentHooks::current_timestamp();
benchmark_start = InstrumentHooks::current_timestamp();
sample_start = UntaggedTimestamp::start(timer_kind);

// Sample loop:
Expand All @@ -987,8 +1003,7 @@ impl<'a> BenchContext<'a> {
}

sample_end = UntaggedTimestamp::end(timer_kind);
let end_time = InstrumentHooks::current_timestamp();
instrument_hooks.add_benchmark_timestamps(start_time, end_time);
benchmark_end = InstrumentHooks::current_timestamp();
sync_threads(false);
save_alloc_info();

Expand Down Expand Up @@ -1028,7 +1043,7 @@ impl<'a> BenchContext<'a> {
let defer_inputs_iter = defer_inputs_slice.iter();

sync_threads(true);
let start_time = InstrumentHooks::current_timestamp();
benchmark_start = InstrumentHooks::current_timestamp();
sample_start = UntaggedTimestamp::start(timer_kind);

// Sample loop:
Expand All @@ -1039,8 +1054,7 @@ impl<'a> BenchContext<'a> {
}

sample_end = UntaggedTimestamp::end(timer_kind);
let end_time = InstrumentHooks::current_timestamp();
instrument_hooks.add_benchmark_timestamps(start_time, end_time);
benchmark_end = InstrumentHooks::current_timestamp();
sync_threads(false);
save_alloc_info();

Expand All @@ -1065,7 +1079,7 @@ impl<'a> BenchContext<'a> {
[sample_start.into_timestamp(timer_kind), sample_end.into_timestamp(timer_kind)]
};

(interval, saved_alloc_info)
(interval, saved_alloc_info, [benchmark_start, benchmark_end])
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/divan_compat/divan_fork/src/stats/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub(crate) struct TimeSample {
pub(crate) struct RawSample {
pub start: Timestamp,
pub end: Timestamp,
pub benchmark_start: u64,
pub benchmark_end: u64,
pub timer: Timer,
pub alloc_info: ThreadAllocInfo,
pub counter_totals: [u128; KnownCounterKind::COUNT],
Expand Down
Loading