Skip to content

Commit 96e1aca

Browse files
committed
fix(divan): support threaded walltime benchmarks
Aggregate worker timestamps into one benchmark marker envelope per Divan sampling round so benchmark bodies stay parallel without interleaving FIFO requests.
1 parent 6666998 commit 96e1aca

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

‎crates/divan_compat/divan_fork/src/bench/mod.rs‎

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,18 @@ impl<'a> BenchContext<'a> {
698698
};
699699

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

704-
RawSample { start, end, timer, alloc_info, counter_totals }
704+
RawSample {
705+
start,
706+
end,
707+
benchmark_start,
708+
benchmark_end,
709+
timer,
710+
alloc_info,
711+
counter_totals,
712+
}
705713
};
706714

707715
// Sample loop:
@@ -723,6 +731,11 @@ impl<'a> BenchContext<'a> {
723731
std::slice::from_raw_parts(raw_samples.as_ptr().cast(), raw_samples.len())
724732
}
725733
};
734+
let benchmark_start =
735+
raw_samples.iter().map(|sample| sample.benchmark_start).min().unwrap();
736+
let benchmark_end =
737+
raw_samples.iter().map(|sample| sample.benchmark_end).max().unwrap();
738+
InstrumentHooks::instance().add_benchmark_timestamps(benchmark_start, benchmark_end);
726739

727740
// If testing, exit the benchmarking loop immediately after timing a
728741
// single run.
@@ -825,8 +838,11 @@ impl<'a> BenchContext<'a> {
825838
gen_input: impl Fn() -> I,
826839
benched: impl Fn(&UnsafeCell<MaybeUninit<I>>) -> O,
827840
drop_input: impl Fn(&UnsafeCell<MaybeUninit<I>>),
828-
) -> impl Fn(usize, Option<&Barrier>, &mut dyn FnMut(&I)) -> ([Timestamp; 2], ThreadAllocInfo)
829-
{
841+
) -> impl Fn(
842+
usize,
843+
Option<&Barrier>,
844+
&mut dyn FnMut(&I),
845+
) -> ([Timestamp; 2], ThreadAllocInfo, [u64; 2]) {
830846
// We defer:
831847
// - Usage of `gen_input` values.
832848
// - Drop destructor for `O`, preventing it from affecting sample
@@ -897,8 +913,9 @@ impl<'a> BenchContext<'a> {
897913
// benchmarking.
898914
let sample_start: UntaggedTimestamp;
899915
let sample_end: UntaggedTimestamp;
916+
let benchmark_start: u64;
917+
let benchmark_end: u64;
900918

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

916933
sync_threads(true);
917934

918-
let start_time = InstrumentHooks::current_timestamp();
935+
benchmark_start = InstrumentHooks::current_timestamp();
919936
sample_start = UntaggedTimestamp::start(timer_kind);
920937

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

930947
sample_end = UntaggedTimestamp::end(timer_kind);
931-
let end_time = InstrumentHooks::current_timestamp();
932-
instrument_hooks.add_benchmark_timestamps(start_time, end_time);
948+
benchmark_end = InstrumentHooks::current_timestamp();
933949

934950
sync_threads(false);
935951
save_alloc_info();
@@ -972,7 +988,7 @@ impl<'a> BenchContext<'a> {
972988
let defer_slots_iter = defer_slots_slice.iter();
973989

974990
sync_threads(true);
975-
let start_time = InstrumentHooks::current_timestamp();
991+
benchmark_start = InstrumentHooks::current_timestamp();
976992
sample_start = UntaggedTimestamp::start(timer_kind);
977993

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

9891005
sample_end = UntaggedTimestamp::end(timer_kind);
990-
let end_time = InstrumentHooks::current_timestamp();
991-
instrument_hooks.add_benchmark_timestamps(start_time, end_time);
1006+
benchmark_end = InstrumentHooks::current_timestamp();
9921007
sync_threads(false);
9931008
save_alloc_info();
9941009

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

10301045
sync_threads(true);
1031-
let start_time = InstrumentHooks::current_timestamp();
1046+
benchmark_start = InstrumentHooks::current_timestamp();
10321047
sample_start = UntaggedTimestamp::start(timer_kind);
10331048

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

10411056
sample_end = UntaggedTimestamp::end(timer_kind);
1042-
let end_time = InstrumentHooks::current_timestamp();
1043-
instrument_hooks.add_benchmark_timestamps(start_time, end_time);
1057+
benchmark_end = InstrumentHooks::current_timestamp();
10441058
sync_threads(false);
10451059
save_alloc_info();
10461060

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

1068-
(interval, saved_alloc_info)
1082+
(interval, saved_alloc_info, [benchmark_start, benchmark_end])
10691083
}
10701084
}
10711085

‎crates/divan_compat/divan_fork/src/stats/sample.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub(crate) struct TimeSample {
2222
pub(crate) struct RawSample {
2323
pub start: Timestamp,
2424
pub end: Timestamp,
25+
pub benchmark_start: u64,
26+
pub benchmark_end: u64,
2527
pub timer: Timer,
2628
pub alloc_info: ThreadAllocInfo,
2729
pub counter_totals: [u128; KnownCounterKind::COUNT],

0 commit comments

Comments
 (0)