|
| 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