|
| 1 | +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; |
| 5 | +use vm_memory::{GuestAddress, GuestMemory}; |
| 6 | +use vmm::resources::VmResources; |
| 7 | +use vmm::vmm_config::machine_config::{HugePageConfig, VmConfig}; |
| 8 | + |
| 9 | +fn bench_single_page_fault(c: &mut Criterion, configuration: VmResources) { |
| 10 | + c.bench_function("page_fault", |b| { |
| 11 | + b.iter_batched( |
| 12 | + || { |
| 13 | + let memory = configuration.allocate_guest_memory().unwrap(); |
| 14 | + let ptr = memory |
| 15 | + .get_slice(GuestAddress(0), 1) |
| 16 | + .unwrap() |
| 17 | + .ptr_guard_mut() |
| 18 | + .as_ptr(); |
| 19 | + |
| 20 | + // fine to return both here, because ptr is not a reference into `memory` (e.g. no |
| 21 | + // self-referential structs are happening here) |
| 22 | + (memory, ptr) |
| 23 | + }, |
| 24 | + |(_, ptr)| unsafe { |
| 25 | + // Cause a single page fault |
| 26 | + ptr.write_volatile(1); |
| 27 | + }, |
| 28 | + BatchSize::SmallInput, |
| 29 | + ) |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +pub fn bench_4k_page_fault(c: &mut Criterion) { |
| 34 | + bench_single_page_fault( |
| 35 | + c, |
| 36 | + VmResources { |
| 37 | + vm_config: VmConfig { |
| 38 | + vcpu_count: 1, |
| 39 | + mem_size_mib: 2, |
| 40 | + ..Default::default() |
| 41 | + }, |
| 42 | + ..Default::default() |
| 43 | + }, |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +pub fn bench_2m_page_fault(c: &mut Criterion) { |
| 48 | + bench_single_page_fault( |
| 49 | + c, |
| 50 | + VmResources { |
| 51 | + vm_config: VmConfig { |
| 52 | + vcpu_count: 1, |
| 53 | + mem_size_mib: 2, |
| 54 | + huge_pages: HugePageConfig::Hugetlbfs2M, |
| 55 | + ..Default::default() |
| 56 | + }, |
| 57 | + ..Default::default() |
| 58 | + }, |
| 59 | + ) |
| 60 | +} |
| 61 | + |
| 62 | +criterion_group! { |
| 63 | + name = memory_access_benches; |
| 64 | + config = Criterion::default().noise_threshold(0.05); |
| 65 | + targets = bench_4k_page_fault, bench_2m_page_fault |
| 66 | +} |
| 67 | + |
| 68 | +criterion_main! { |
| 69 | + memory_access_benches |
| 70 | +} |
0 commit comments