Skip to content

Commit ca2f8c8

Browse files
committed
bench: link comet into alloc_overhead so the baseline uses the selected allocator
An `--extern` crate that nothing names is dropped from the crate graph, and the accounting-off run of this benchmark named nothing in `comet`, so the `#[global_allocator]` in `lib.rs` never reached the binary: the "jemalloc" baseline was measuring glibc malloc. The accounting-on run names `comet::alloc_accounting`, so it did link the crate, and every off/on comparison so far was glibc against jemalloc plus the wrapper. Add `extern crate comet` so the crate is always linked, and a jemalloc liveness assertion next to the existing accounting one, so a run against the wrong allocator fails instead of producing a plausible number.
1 parent 2d78f87 commit ca2f8c8

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

‎native/core/benches/alloc_overhead.rs‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
//! cargo bench --bench alloc_overhead --features alloc-accounting -- --baseline off
2525
//! ```
2626
//!
27-
//! The benchmark relies on the `#[global_allocator]` that `lib.rs` installs, which is linked in
28-
//! through the `rlib`. `assert_accounting_is_live` fails the run if the wrapper is somehow not in
29-
//! effect — without it, a "with the feature" run could silently be a second baseline.
27+
//! The benchmark relies on the `#[global_allocator]` that `lib.rs` installs, which reaches this
28+
//! binary through the `rlib`. That only happens if the crate is actually linked, and an `--extern`
29+
//! crate that nothing names is dropped from the crate graph along with its allocator, so the
30+
//! `extern crate` below is load-bearing: without it a baseline run that never touches `comet`
31+
//! silently measures the system allocator instead of jemalloc. The two liveness checks fail the run
32+
//! if either the selected backend or the wrapper is somehow not in effect, because a number
33+
//! measured against the wrong allocator would be worse than no number.
3034
//!
3135
//! `small_churn` is the worst case for the thread-local path: allocations so small that the
3236
//! wrapper's bookkeeping is a meaningful fraction of the allocator's own work. `threshold_churn` is
@@ -37,11 +41,36 @@
3741
//! a batch-sized buffer dwarfs the bookkeeping. Real query workloads sit at or below
3842
//! `arrow_sized_churn`, because they do actual work between allocations.
3943
44+
// Pulls `comet`, and with it the `#[global_allocator]` selected by its feature set, into this
45+
// binary even when the feature set leaves nothing here that names the crate.
46+
extern crate comet;
47+
4048
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
4149
use std::hint::black_box;
4250
use std::thread;
4351
use std::time::Instant;
4452

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")]
57+
fn assert_backend_is_live() {
58+
use tikv_jemalloc_ctl::{epoch, stats};
59+
let held: Vec<u8> = black_box(vec![1u8; 8 * 1024 * 1024]);
60+
black_box(&held);
61+
epoch::advance().expect("jemalloc epoch");
62+
let allocated = stats::allocated::read().expect("jemalloc stats.allocated");
63+
assert!(
64+
allocated >= 8 * 1024 * 1024,
65+
"the jemalloc feature is enabled but jemalloc is not the global allocator of this binary \
66+
(stats.allocated = {allocated}); the numbers below would be meaningless"
67+
);
68+
drop(held);
69+
}
70+
71+
#[cfg(not(feature = "jemalloc"))]
72+
fn assert_backend_is_live() {}
73+
4574
/// Guards against measuring nothing. If the wrapper were not actually installed in the benchmark
4675
/// binary, every "with the feature" number would silently be a second baseline run.
4776
#[cfg(feature = "alloc-accounting")]
@@ -67,6 +96,7 @@ fn assert_accounting_is_live() {}
6796
/// Allocation sizes that stay under the 64 KiB settle threshold, so most iterations exercise only
6897
/// the thread-local fast path rather than the atomic flush.
6998
fn small_churn(c: &mut Criterion) {
99+
assert_backend_is_live();
70100
assert_accounting_is_live();
71101
let mut group = c.benchmark_group("alloc_overhead");
72102
for size in [16usize, 256, 4096] {
@@ -133,6 +163,7 @@ fn alloc_free(size: usize) {
133163
/// Times are reported per alloc/free pair per thread, so a parallel number equal to its
134164
/// single-threaded counterpart means the threads did not slow each other down at all.
135165
fn threshold_churn(c: &mut Criterion) {
166+
assert_backend_is_live();
136167
assert_accounting_is_live();
137168
let threads = thread::available_parallelism().map_or(4, |n| n.get());
138169
let mut group = c.benchmark_group("alloc_overhead");

0 commit comments

Comments
 (0)