Skip to content

Commit ab8a322

Browse files
committed
improve benchmark
1 parent f747405 commit ab8a322

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ optional = true
2222
[dev-dependencies]
2323
criterion = "0.3"
2424
ctor = "0.1.23"
25+
rand = "0.8.5"
26+
rand_chacha = "0.3.1"
2527

2628
[[bench]]
2729
name = "memory_allocator_benchmark"

benches/memory_allocator_benchmark.rs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[macro_use]
22
extern crate alloc;
3-
43
#[macro_use]
54
extern crate ctor;
65

@@ -14,41 +13,56 @@ use alloc::alloc::Layout;
1413
use buddy_system_allocator::LockedHeap;
1514
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1615

17-
// use for first three benchmark
16+
const SMALL_SIZE: usize = 8;
17+
const LARGE_SIZE: usize = 1024 * 1024; // 1M
1818
const ALIGN: usize = 8;
1919

20+
/// Alloc small object
2021
#[inline]
2122
pub fn small_alloc<const ORDER: usize>(heap: &LockedHeap<ORDER>) {
22-
const SMALL_SIZE: usize = 8;
2323
let layout = unsafe { Layout::from_size_align_unchecked(SMALL_SIZE, ALIGN) };
2424
unsafe {
2525
let addr = heap.alloc(layout);
2626
heap.dealloc(addr, layout);
2727
}
2828
}
2929

30+
/// Alloc large object
3031
#[inline]
3132
pub fn large_alloc<const ORDER: usize>(heap: &LockedHeap<ORDER>) {
32-
const LARGE_SIZE: usize = 1024;
3333
let layout = unsafe { Layout::from_size_align_unchecked(LARGE_SIZE, ALIGN) };
3434
unsafe {
3535
let addr = heap.alloc(layout);
3636
heap.dealloc(addr, layout);
3737
}
3838
}
3939

40+
/// Multithreads alloc random sizes of object
4041
#[inline]
41-
pub fn mutil_thread_alloc<const ORDER: usize>(heap: &'static LockedHeap<ORDER>) {
42+
pub fn mutil_thread_random_size<const ORDER: usize>(heap: &'static LockedHeap<ORDER>) {
4243
const THREAD_SIZE: usize = 10;
44+
45+
use rand::prelude::*;
46+
use rand::{Rng, SeedableRng};
47+
use rand_chacha::ChaCha8Rng;
48+
4349
let mut threads = Vec::with_capacity(THREAD_SIZE);
4450
let alloc = Arc::new(heap);
4551
for i in 0..THREAD_SIZE {
4652
let prethread_alloc = alloc.clone();
4753
let handle = thread::spawn(move || {
48-
let layout = unsafe { Layout::from_size_align_unchecked(i * THREAD_SIZE, ALIGN) };
49-
let addr;
50-
unsafe { addr = prethread_alloc.alloc(layout) }
54+
// generate a random size of object use seed `i` to ensure the fixed
55+
// result of each turn
56+
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(i as u64);
57+
// generate a random object size in range of [SMALL_SIZE ..= LARGE_SIZE]
58+
let layout = unsafe {
59+
Layout::from_size_align_unchecked(rng.gen_range(SMALL_SIZE..=LARGE_SIZE), ALIGN)
60+
};
61+
let addr = unsafe { prethread_alloc.alloc(layout) };
62+
63+
// sleep for a while
5164
sleep(Duration::from_nanos((THREAD_SIZE - i) as u64));
65+
5266
unsafe { prethread_alloc.dealloc(addr, layout) }
5367
});
5468
threads.push(handle);
@@ -86,7 +100,7 @@ pub fn mutil_thread_alloc<const ORDER: usize>(heap: &'static LockedHeap<ORDER>)
86100
#[inline]
87101
pub fn thread_test() {
88102
const N_ITERATIONS: usize = 50;
89-
const N_OBJECTS: usize = 3000;
103+
const N_OBJECTS: usize = 30000;
90104
const N_THREADS: usize = 10;
91105
const OBJECT_SIZE: usize = 1;
92106

@@ -102,11 +116,21 @@ pub fn thread_test() {
102116
let handle = thread::spawn(move || {
103117
// let a = new Foo * [nobjects / nthreads];
104118
let mut a = Vec::with_capacity(N_OBJECTS / N_THREADS);
105-
for _j in 0..N_ITERATIONS {
119+
for j in 0..N_ITERATIONS {
106120
// inner object:
107121
// a[i] = new Foo[objSize];
108-
for _k in 0..(N_OBJECTS / N_THREADS) {
109-
a.push(vec![Foo { a: 0, b: 1 }; OBJECT_SIZE]);
122+
for k in 0..(N_OBJECTS / N_THREADS) {
123+
a.push(vec![
124+
Foo {
125+
a: k as i32,
126+
b: j as i32
127+
};
128+
OBJECT_SIZE
129+
]);
130+
131+
// in order to prevent optimization delete allocation directly
132+
// FIXME: don't know whether it works or not
133+
a[k][0].a += a[k][0].b;
110134
}
111135
}
112136
// auto drop here
@@ -121,7 +145,9 @@ pub fn thread_test() {
121145

122146
const ORDER: usize = 32;
123147
const MACHINE_ALIGN: usize = core::mem::size_of::<usize>();
124-
const KERNEL_HEAP_SIZE: usize = 16 * 1024 * 1024;
148+
/// for now 128M is needed
149+
/// TODO: reduce memory use
150+
const KERNEL_HEAP_SIZE: usize = 128 * 1024 * 1024;
125151
const HEAP_BLOCK: usize = KERNEL_HEAP_SIZE / MACHINE_ALIGN;
126152
static mut HEAP: [usize; HEAP_BLOCK] = [0; HEAP_BLOCK];
127153

@@ -149,6 +175,7 @@ fn init_heap() {
149175
}
150176
}
151177

178+
/// Entry of benchmarks
152179
pub fn criterion_benchmark(c: &mut Criterion) {
153180
// run benchmark
154181
c.bench_function("small alloc", |b| {
@@ -157,8 +184,8 @@ pub fn criterion_benchmark(c: &mut Criterion) {
157184
c.bench_function("large alloc", |b| {
158185
b.iter(|| large_alloc(black_box(&HEAP_ALLOCATOR)))
159186
});
160-
c.bench_function("mutil thread alloc", |b| {
161-
b.iter(|| mutil_thread_alloc(black_box(&HEAP_ALLOCATOR)))
187+
c.bench_function("mutil thread random size", |b| {
188+
b.iter(|| mutil_thread_random_size(black_box(&HEAP_ALLOCATOR)))
162189
});
163190
c.bench_function("threadtest", |b| b.iter(|| thread_test()));
164191
}

0 commit comments

Comments
 (0)