Skip to content

Commit 3d64381

Browse files
committed
benchmark: Add riscv64 benchmark tests
As clippy command in our CI mandates: `cargo clippy --workspace --bins --examples --benches --all-features --all-targets -- -D warnings -D clippy::undocumented_unsafe_blocks`, add benchmarck test to pass clippy check. Signed-off-by: Ruoqing He <[email protected]>
1 parent d2993b9 commit 3d64381

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

benches/main.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ mod aarch64;
2121
#[cfg(target_arch = "aarch64")]
2222
use aarch64::*;
2323

24+
#[cfg(target_arch = "riscv64")]
25+
mod riscv64;
26+
#[cfg(target_arch = "riscv64")]
27+
use riscv64::*;
28+
2429
pub fn criterion_benchmark_nop(_: &mut Criterion) {}
2530

2631
criterion_group! {
@@ -51,6 +56,17 @@ criterion_group! {
5156
targets = criterion_benchmark_nop
5257
}
5358

59+
// NOP because the `criterion_main!` macro doesn't support cfg(feature)
60+
// macro expansions.
61+
#[cfg(all(target_arch = "riscv64", not(feature = "bzimage")))]
62+
criterion_group! {
63+
name = benches_bzimage;
64+
// Sample size must be >= 10.
65+
// https://github.com/bheisler/criterion.rs/blob/0.3.0/src/lib.rs#L757
66+
config = Criterion::default().sample_size(10);
67+
targets = criterion_benchmark_nop
68+
}
69+
5470
criterion_main! {
5571
benches,
5672
benches_bzimage

benches/riscv64/mod.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be
4+
// found in the LICENSE-BSD-3-Clause file.
5+
//
6+
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
7+
extern crate criterion;
8+
extern crate linux_loader;
9+
extern crate vm_memory;
10+
11+
use linux_loader::configurator::fdt::FdtBootConfigurator;
12+
use linux_loader::configurator::{BootConfigurator, BootParams};
13+
use vm_memory::{ByteValued, GuestAddress, GuestMemoryMmap};
14+
15+
use criterion::{black_box, Criterion};
16+
17+
const MEM_SIZE: usize = 0x100_0000;
18+
const FDT_MAX_SIZE: usize = 0x20;
19+
20+
fn create_guest_memory() -> GuestMemoryMmap {
21+
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), MEM_SIZE)]).unwrap()
22+
}
23+
24+
#[derive(Clone, Copy, Default)]
25+
#[allow(dead_code)]
26+
pub struct FdtPlaceholder([u8; FDT_MAX_SIZE]);
27+
28+
// SAFETY: The layout of the structure is fixed and can be initialized by
29+
// reading its content from byte array.
30+
unsafe impl ByteValued for FdtPlaceholder {}
31+
32+
fn build_fdt_boot_params() -> BootParams {
33+
let fdt = FdtPlaceholder([0u8; FDT_MAX_SIZE]);
34+
let fdt_addr = GuestAddress((MEM_SIZE - FDT_MAX_SIZE - 1) as u64);
35+
BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr)
36+
}
37+
38+
pub fn criterion_benchmark(c: &mut Criterion) {
39+
let guest_mem = create_guest_memory();
40+
let fdt_boot_params = build_fdt_boot_params();
41+
c.bench_function("configure_fdt", |b| {
42+
b.iter(|| {
43+
black_box(FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>(
44+
&fdt_boot_params,
45+
&guest_mem,
46+
))
47+
.unwrap();
48+
})
49+
});
50+
}

0 commit comments

Comments
 (0)