Skip to content

Commit f576ef2

Browse files
Alexandra Iordachealxiord
authored andcommitted
benchmark: add initial benchmark tests
These tests use the criterion plugin to collect performance data for a given pub fn (selected 2 PVH related functions for their relative complexity, as well as a simple test for ARM) and (locally) compare it with future runs. This commit is part of a larger story spanning the whole rust-vmm, kickstarting a centralized effort to add benchmarks to crates used in production VMMs. Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
1 parent b309c9d commit f576ef2

File tree

6 files changed

+190
-3
lines changed

6 files changed

+190
-3
lines changed

Cargo.toml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,28 @@ version = "0.1.0"
44
authors = ["Cathy Zhang <cathy.zhang@intel.com>"]
55
edition = "2018"
66
license = "Apache-2.0 AND BSD-3-Clause"
7+
autobenches = false
78

89
[features]
910
default = ["elf", "pe"]
10-
elf = []
1111
bzimage = []
12+
elf = []
1213
pe = []
1314

1415
[dependencies]
1516
vm-memory = ">=0.2.0"
1617

1718
[dev-dependencies]
19+
criterion = ">=0.3.0"
1820
vm-memory = {features = ["backend-mmap"]}
21+
22+
[[bench]]
23+
name = "main"
24+
harness = false
25+
26+
[lib]
27+
bench = false # https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options
28+
29+
[profile.bench]
30+
lto = true
31+
codegen-units = 1

benches/aarch64/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
pub struct FdtPlaceholder([u8; FDT_MAX_SIZE]);
26+
27+
unsafe impl ByteValued for FdtPlaceholder {}
28+
29+
fn build_fdt_boot_params() -> BootParams {
30+
let fdt = FdtPlaceholder([0u8; FDT_MAX_SIZE]);
31+
let fdt_addr = GuestAddress((MEM_SIZE - FDT_MAX_SIZE - 1) as u64);
32+
BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr)
33+
}
34+
35+
pub fn criterion_benchmark(c: &mut Criterion) {
36+
let guest_mem = create_guest_memory();
37+
let fdt_boot_params = build_fdt_boot_params();
38+
c.bench_function("configure_fdt", |b| {
39+
b.iter(|| {
40+
black_box(FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>(
41+
fdt_boot_params.clone(),
42+
&guest_mem,
43+
))
44+
.unwrap();
45+
})
46+
});
47+
}

benches/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 criterion::{criterion_group, criterion_main, Criterion};
12+
13+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
14+
mod x86_64;
15+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
16+
use x86_64::*;
17+
18+
#[cfg(target_arch = "aarch64")]
19+
mod aarch64;
20+
#[cfg(target_arch = "aarch64")]
21+
use aarch64::*;
22+
23+
criterion_group! {
24+
name = benches;
25+
config = Criterion::default().sample_size(500);
26+
targets = criterion_benchmark
27+
}
28+
29+
criterion_main! {
30+
benches
31+
}

benches/x86_64/mod.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
8+
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
9+
10+
extern crate linux_loader;
11+
extern crate vm_memory;
12+
13+
use linux_loader::configurator::pvh::PvhBootConfigurator;
14+
use linux_loader::configurator::{BootConfigurator, BootParams};
15+
use linux_loader::loader::elf::start_info::{hvm_memmap_table_entry, hvm_start_info};
16+
use linux_loader::loader::elf::Elf;
17+
use linux_loader::loader::KernelLoader;
18+
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
19+
20+
use std::io::Cursor;
21+
22+
use criterion::{black_box, Criterion};
23+
24+
const MEM_SIZE: usize = 0x100_0000;
25+
const E820_RAM: u32 = 1;
26+
const XEN_HVM_START_MAGIC_VALUE: u32 = 0x336ec578;
27+
28+
fn create_guest_memory() -> GuestMemoryMmap {
29+
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), MEM_SIZE)]).unwrap()
30+
}
31+
32+
fn create_elf_pvh_image() -> Vec<u8> {
33+
include_bytes!(concat!(
34+
env!("CARGO_MANIFEST_DIR"),
35+
"/src/loader/x86_64/elf/test_elfnote.bin"
36+
))
37+
.to_vec()
38+
}
39+
40+
fn build_boot_params() -> (hvm_start_info, Vec<hvm_memmap_table_entry>) {
41+
let mut start_info = hvm_start_info::default();
42+
let memmap_entry = hvm_memmap_table_entry {
43+
addr: 0x7000,
44+
size: 0,
45+
type_: E820_RAM,
46+
reserved: 0,
47+
};
48+
start_info.magic = XEN_HVM_START_MAGIC_VALUE;
49+
start_info.version = 1;
50+
start_info.nr_modules = 0;
51+
start_info.memmap_entries = 0;
52+
(start_info, vec![memmap_entry])
53+
}
54+
55+
fn build_pvh_boot_params() -> BootParams {
56+
let (mut start_info, memmap_entries) = build_boot_params();
57+
// Address in guest memory where the `start_info` struct will be written.
58+
let start_info_addr = GuestAddress(0x6000);
59+
// Address in guest memory where the memory map will be written.
60+
let memmap_addr = GuestAddress(0x7000);
61+
start_info.memmap_paddr = memmap_addr.raw_value();
62+
// Write boot parameters in guest memory.
63+
let mut boot_params = BootParams::new::<hvm_start_info>(&start_info, start_info_addr);
64+
boot_params.set_sections::<hvm_memmap_table_entry>(&memmap_entries, memmap_addr);
65+
boot_params
66+
}
67+
68+
pub fn criterion_benchmark(c: &mut Criterion) {
69+
let guest_mem = create_guest_memory();
70+
71+
let elf_pvh_image = create_elf_pvh_image();
72+
let pvh_boot_params = build_pvh_boot_params();
73+
74+
c.bench_function("load_elf_pvh", |b| {
75+
b.iter(|| {
76+
black_box(Elf::load(
77+
&guest_mem,
78+
None,
79+
&mut Cursor::new(&elf_pvh_image),
80+
None,
81+
))
82+
.unwrap();
83+
})
84+
});
85+
86+
c.bench_function("configure_pvh", |b| {
87+
b.iter(|| {
88+
black_box(PvhBootConfigurator::write_bootparams::<GuestMemoryMmap>(
89+
pvh_boot_params.clone(),
90+
&guest_mem,
91+
))
92+
.unwrap();
93+
})
94+
});
95+
}

coverage_config_aarch64.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"coverage_score": 80.7,
33
"exclude_path": "",
4-
"crate_features": "pe"
4+
"crate_features": "pe",
5+
"exclude_path": "benches/"
56
}

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"coverage_score": 80.8,
33
"exclude_path": "",
44
"crate_features": "bzimage,elf",
5-
"exclude_path": "loader_gen"
5+
"exclude_path": "benches/,loader_gen/"
66
}

0 commit comments

Comments
 (0)