Skip to content

Commit 96e8967

Browse files
committed
Add allocation measurement to stress2 proper
1 parent a0d51f2 commit 96e8967

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

benchmarks/stress2/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ event_log = ["sled/event_log"]
2020
compression = ["sled/compression"]
2121
no_logs = ["sled/no_logs"]
2222
metrics = ["sled/metrics"]
23-
measure_allocs = ["sled/measure_allocs"]
2423
jemalloc = ["jemallocator"]
2524
logging = ["env_logger", "log", "color-backtrace"]
2625
dh = ["dhat"]
2726
memshred = []
27+
measure_allocs = []
2828

2929
[dependencies]
3030
rand = "0.7.3"

benchmarks/stress2/src/main.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ use rand::{thread_rng, Rng};
1414

1515
#[cfg(feature = "jemalloc")]
1616
mod alloc {
17-
use std::alloc::Layout;
1817
use jemallocator::Jemalloc;
18+
use std::alloc::Layout;
1919

2020
#[global_allocator]
2121
static ALLOCATOR: Jemalloc = Jemalloc;
2222
}
2323

2424
#[cfg(feature = "memshred")]
2525
mod alloc {
26-
use std::alloc::{System, Layout};
26+
use std::alloc::{Layout, System};
2727

2828
#[global_allocator]
2929
static ALLOCATOR: Alloc = Alloc;
@@ -42,11 +42,35 @@ mod alloc {
4242
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
4343
std::ptr::write_bytes(ptr, 0xde, layout.size());
4444
System.dealloc(ptr, layout)
45-
4645
}
4746
}
4847
}
4948

49+
#[cfg(feature = "measure_allocs")]
50+
mod alloc {
51+
use std::alloc::{Layout, System};
52+
use std::sync::atomic::{AtomicUsize, Ordering::Release};
53+
54+
pub static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);
55+
pub static ALLOCATED_BYTES: AtomicUsize = AtomicUsize::new(0);
56+
57+
#[global_allocator]
58+
static ALLOCATOR: Alloc = Alloc;
59+
60+
#[derive(Default, Debug, Clone, Copy)]
61+
struct Alloc;
62+
63+
unsafe impl std::alloc::GlobalAlloc for Alloc {
64+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
65+
ALLOCATIONS.fetch_add(1, Release);
66+
ALLOCATED_BYTES.fetch_add(layout.size(), Release);
67+
System.alloc(layout)
68+
}
69+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
70+
System.dealloc(ptr, layout)
71+
}
72+
}
73+
}
5074

5175
#[global_allocator]
5276
#[cfg(feature = "dh")]
@@ -385,6 +409,17 @@ fn main() {
385409
((ops * 1_000) / (time * 1_000)).to_formatted_string(&Locale::en)
386410
);
387411

412+
#[cfg(feature = "measure_allocs")]
413+
println!(
414+
"allocated {} bytes in {} allocations",
415+
alloc::ALLOCATED_BYTES
416+
.load(Ordering::Acquire)
417+
.to_formatted_string(&Locale::en),
418+
alloc::ALLOCATIONS
419+
.load(Ordering::Acquire)
420+
.to_formatted_string(&Locale::en),
421+
);
422+
388423
#[cfg(feature = "metrics")]
389424
sled::print_profile();
390425
}

0 commit comments

Comments
 (0)