Skip to content

Commit 486047d

Browse files
Alexandra Iordachealxiord
authored andcommitted
bench: add bzImage loader benchmark
Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
1 parent f576ef2 commit 486047d

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

benches/main.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// found in the LICENSE-BSD-3-Clause file.
55
//
66
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
7+
78
extern crate criterion;
89
extern crate linux_loader;
910
extern crate vm_memory;
@@ -20,12 +21,37 @@ mod aarch64;
2021
#[cfg(target_arch = "aarch64")]
2122
use aarch64::*;
2223

24+
pub fn criterion_benchmark_nop(_: &mut Criterion) {}
25+
2326
criterion_group! {
2427
name = benches;
2528
config = Criterion::default().sample_size(500);
2629
targets = criterion_benchmark
2730
}
2831

32+
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "bzimage"))]
33+
// Explicit (arch, feature) tuple required as clippy complains about
34+
// `criterion_benchmark_bzimage` missing on aarch64.
35+
criterion_group! {
36+
name = benches_bzimage;
37+
// Only ~125 runs fit in 5 seconds. Either extend the duration, or reduce
38+
// the number of iterations.
39+
config = Criterion::default().sample_size(100);
40+
targets = criterion_benchmark_bzimage
41+
}
42+
43+
// NOP because the `criterion_main!` macro doesn't support cfg(feature)
44+
// macro expansions.
45+
#[cfg(any(target_arch = "aarch64", not(feature = "bzimage")))]
46+
criterion_group! {
47+
name = benches_bzimage;
48+
// Sample size must be >= 10.
49+
// https://github.com/bheisler/criterion.rs/blob/0.3.0/src/lib.rs#L757
50+
config = Criterion::default().sample_size(10);
51+
targets = criterion_benchmark_nop
52+
}
53+
2954
criterion_main! {
30-
benches
55+
benches,
56+
benches_bzimage
3157
}

benches/x86_64/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extern crate vm_memory;
1212

1313
use linux_loader::configurator::pvh::PvhBootConfigurator;
1414
use linux_loader::configurator::{BootConfigurator, BootParams};
15+
#[cfg(feature = "bzimage")]
16+
use linux_loader::loader::bzimage::BzImage;
1517
use linux_loader::loader::elf::start_info::{hvm_memmap_table_entry, hvm_start_info};
1618
use linux_loader::loader::elf::Elf;
1719
use linux_loader::loader::KernelLoader;
@@ -65,6 +67,15 @@ fn build_pvh_boot_params() -> BootParams {
6567
boot_params
6668
}
6769

70+
#[cfg(feature = "bzimage")]
71+
fn create_bzimage() -> Vec<u8> {
72+
include_bytes!(concat!(
73+
env!("CARGO_MANIFEST_DIR"),
74+
"/src/loader/x86_64/bzimage/bzimage"
75+
))
76+
.to_vec()
77+
}
78+
6879
pub fn criterion_benchmark(c: &mut Criterion) {
6980
let guest_mem = create_guest_memory();
7081

@@ -93,3 +104,21 @@ pub fn criterion_benchmark(c: &mut Criterion) {
93104
})
94105
});
95106
}
107+
108+
#[cfg(feature = "bzimage")]
109+
pub fn criterion_benchmark_bzimage(c: &mut Criterion) {
110+
let guest_mem = create_guest_memory();
111+
let bzimage = create_bzimage();
112+
113+
c.bench_function("load_bzimage", |b| {
114+
b.iter(|| {
115+
black_box(BzImage::load(
116+
&guest_mem,
117+
None,
118+
&mut Cursor::new(&bzimage),
119+
None,
120+
))
121+
.unwrap();
122+
})
123+
});
124+
}

0 commit comments

Comments
 (0)