From be6f63a740c857c1f6c9b5b569eedfa9e4fdc7c5 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Sat, 14 Mar 2026 15:28:14 +0100 Subject: [PATCH] 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 --- Cargo.lock | 1 + performance-metrics/Cargo.toml | 1 + performance-metrics/src/main.rs | 1 + performance-metrics/src/util.rs | 36 +++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 performance-metrics/src/util.rs diff --git a/Cargo.lock b/Cargo.lock index 083f42101..af88d4db9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1602,6 +1602,7 @@ dependencies = [ "serde_json", "test_infra", "thiserror 2.0.18", + "vmm-sys-util", ] [[package]] diff --git a/performance-metrics/Cargo.toml b/performance-metrics/Cargo.toml index 472f1159b..60be8f45a 100644 --- a/performance-metrics/Cargo.toml +++ b/performance-metrics/Cargo.toml @@ -11,6 +11,7 @@ serde = { workspace = true, features = ["derive", "rc"] } serde_json = { workspace = true } test_infra = { path = "../test_infra" } thiserror = { workspace = true } +vmm-sys-util = { workspace = true } [lints] workspace = true diff --git a/performance-metrics/src/main.rs b/performance-metrics/src/main.rs index cdc06fe10..bcfee6d97 100644 --- a/performance-metrics/src/main.rs +++ b/performance-metrics/src/main.rs @@ -5,6 +5,7 @@ // Custom harness to run performance tests mod performance_tests; +mod util; use std::process::Command; use std::sync::Arc; diff --git a/performance-metrics/src/util.rs b/performance-metrics/src/util.rs new file mode 100644 index 000000000..dcc225750 --- /dev/null +++ b/performance-metrics/src/util.rs @@ -0,0 +1,36 @@ +// 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}"), + } + } +}