Files
cloud-hypervisor/performance-metrics/src/util.rs
Anatol Belski be6f63a740 performance-metrics: Add util module with shared micro benchmark helpers
These factor out common setup and synchronization patterns used by block
layer micro benchmarks.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-17 22:12:50 +00:00

37 lines
1020 B
Rust

// Copyright 2026 The Cloud Hypervisor Authors. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//! Shared benchmark helpers.
use std::io::ErrorKind;
use std::thread;
use std::time::Duration;
use vmm_sys_util::eventfd::EventFd;
use vmm_sys_util::tempfile::TempFile;
pub const BLOCK_SIZE: u64 = 4096;
/// Create a temporary file pre sized to hold `num_blocks` blocks.
pub fn sized_tempfile(num_blocks: usize) -> TempFile {
let tmp = TempFile::new().expect("failed to create tempfile");
tmp.as_file()
.set_len(BLOCK_SIZE * num_blocks as u64)
.expect("failed to set file length");
tmp
}
/// Spin and wait until the given eventfd becomes readable.
pub fn wait_for_eventfd(notifier: &EventFd) {
loop {
match notifier.read() {
Ok(_) => return,
Err(e) if e.kind() == ErrorKind::WouldBlock => {
thread::sleep(Duration::from_micros(50));
}
Err(e) => panic!("eventfd read failed: {e}"),
}
}
}