Skip to content

Commit ac51c5c

Browse files
committed
bench: check jemalloc liveness only when the library selected jemalloc
The alloc_overhead benchmark asserted on jemalloc's counters whenever the jemalloc feature was on, but lib.rs selects jemalloc only when mimalloc was not also requested and the target is not MSVC. With jemalloc,mimalloc the library falls back to the system allocator and the bench aborted before measuring anything. Export the selection lib.rs made as comet::ALLOCATOR_BACKEND, one NAME per backend module, and have the bench announce it and run the jemalloc assertion only when the library reports jemalloc. The predicate stays written once, so the guard cannot drift from the selection it verifies.
1 parent ca2f8c8 commit ac51c5c

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎native/core/benches/alloc_overhead.rs‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,49 @@ use std::hint::black_box;
5050
use std::thread;
5151
use std::time::Instant;
5252

53-
/// Guards against measuring the wrong allocator. jemalloc keeps its own count of bytes it has
54-
/// served; if it is not the global allocator of this binary that count stays at zero, and a
55-
/// "jemalloc" baseline would in fact be the system allocator.
56-
#[cfg(feature = "jemalloc")]
53+
/// Guards against measuring the wrong allocator, and says which one is being measured.
54+
///
55+
/// Which backend is in effect is `lib.rs`'s decision, not this crate's feature flags': with
56+
/// `jemalloc,mimalloc` together the library deliberately falls back to the system allocator, and
57+
/// jemalloc on MSVC is not selected at all. So the check asks the library which backend it chose
58+
/// rather than re-deriving that from the feature set, and can never disagree with the selection it
59+
/// is meant to verify.
5760
fn assert_backend_is_live() {
61+
static ANNOUNCE: std::sync::Once = std::sync::Once::new();
62+
ANNOUNCE.call_once(|| {
63+
eprintln!(
64+
"alloc_overhead: measuring the `{}` allocator backend",
65+
comet::ALLOCATOR_BACKEND
66+
)
67+
});
68+
if comet::ALLOCATOR_BACKEND == "jemalloc" {
69+
assert_jemalloc_is_live();
70+
}
71+
}
72+
73+
/// jemalloc keeps its own count of bytes it has served; if it is not the global allocator of this
74+
/// binary that count stays at zero, and a "jemalloc" baseline would in fact be the system
75+
/// allocator.
76+
#[cfg(feature = "jemalloc")]
77+
fn assert_jemalloc_is_live() {
5878
use tikv_jemalloc_ctl::{epoch, stats};
5979
let held: Vec<u8> = black_box(vec![1u8; 8 * 1024 * 1024]);
6080
black_box(&held);
6181
epoch::advance().expect("jemalloc epoch");
6282
let allocated = stats::allocated::read().expect("jemalloc stats.allocated");
6383
assert!(
6484
allocated >= 8 * 1024 * 1024,
65-
"the jemalloc feature is enabled but jemalloc is not the global allocator of this binary \
85+
"the library selected jemalloc but jemalloc is not the global allocator of this binary \
6686
(stats.allocated = {allocated}); the numbers below would be meaningless"
6787
);
6888
drop(held);
6989
}
7090

91+
/// Without the feature the library cannot have selected jemalloc, so this is never reached.
7192
#[cfg(not(feature = "jemalloc"))]
72-
fn assert_backend_is_live() {}
93+
fn assert_jemalloc_is_live() {
94+
unreachable!("the library reports the jemalloc backend but the feature is not enabled");
95+
}
7396

7497
/// Guards against measuring nothing. If the wrapper were not actually installed in the benchmark
7598
/// binary, every "with the feature" number would silently be a second baseline run.

‎native/core/src/lib.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub mod debug;
8383
mod backend {
8484
pub type Backend = tikv_jemallocator::Jemalloc;
8585
pub const BACKEND: Backend = tikv_jemallocator::Jemalloc;
86+
pub const NAME: &str = "jemalloc";
8687

8788
#[cfg(not(feature = "alloc-accounting"))]
8889
#[global_allocator]
@@ -97,6 +98,7 @@ mod backend {
9798
mod backend {
9899
pub type Backend = mimalloc::MiMalloc;
99100
pub const BACKEND: Backend = mimalloc::MiMalloc;
101+
pub const NAME: &str = "mimalloc";
100102

101103
#[cfg(not(feature = "alloc-accounting"))]
102104
#[global_allocator]
@@ -123,8 +125,15 @@ mod backend {
123125
mod backend {
124126
pub type Backend = std::alloc::System;
125127
pub const BACKEND: Backend = std::alloc::System;
128+
pub const NAME: &str = "system";
126129
}
127130

131+
/// The name of the allocator backend this build selected: `"jemalloc"`, `"mimalloc"` or
132+
/// `"system"`. This is the one place the selection is decided, so anything that needs to know
133+
/// which allocator is in effect (the `alloc_overhead` benchmark's liveness check, for instance)
134+
/// reads it from here rather than re-deriving it from the feature set.
135+
pub use backend::NAME as ALLOCATOR_BACKEND;
136+
128137
#[cfg(feature = "alloc-accounting")]
129138
#[global_allocator]
130139
static GLOBAL: alloc_accounting::AccountingAllocator<backend::Backend> =

0 commit comments

Comments
 (0)