From eca16d74d83ca7b01a2e96399a5ddda72c966be0 Mon Sep 17 00:00:00 2001 From: "Panagiotis \"Ivory\" Vasilopoulos" Date: Tue, 2 Jul 2024 16:40:47 +0200 Subject: [PATCH] sysinfo: Move sysinfo feature to vm.rs --- src/arch/mod.rs | 35 ----------------------------------- src/arch/x86_64/mod.rs | 11 ----------- src/vm.rs | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 47 deletions(-) diff --git a/src/arch/mod.rs b/src/arch/mod.rs index 8b7147c64..be5323c44 100644 --- a/src/arch/mod.rs +++ b/src/arch/mod.rs @@ -1,4 +1,3 @@ -use sysinfo::System; use thiserror::Error; #[derive(Error, Debug)] @@ -16,37 +15,3 @@ pub mod aarch64; #[cfg(target_arch = "aarch64")] pub use self::aarch64::*; - -pub fn detect_freq_from_sysinfo() -> std::result::Result { - debug!("Trying to detect CPU frequency using sysinfo"); - - let mut system = System::new(); - system.refresh_cpu_frequency(); - - let frequency = system.cpus().first().unwrap().frequency(); - - if !system.cpus().iter().all(|cpu| cpu.frequency() == frequency) { - // Even if the CPU frequencies are not all equal, the - // frequency of the "first" CPU is treated as "authoritative". - eprintln!("CPU frequencies are not all equal"); - } - - if frequency > 0 { - Ok(frequency.try_into().unwrap()) - } else { - Err(FrequencyDetectionFailed) - } -} - -#[cfg(test)] -mod tests { - #[test] - // derived from test_get_cpu_frequency_from_os() in src/arch/x86_64/mod.rs - fn test_detect_freq_from_sysinfo() { - let freq_res = crate::detect_freq_from_sysinfo(); - assert!(freq_res.is_ok()); - let freq = freq_res.unwrap(); - assert!(freq > 0); - assert!(freq < 10000); // just like in the original test, more than 10Ghz is probably wrong - } -} diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs index 4c6341fa3..30626bae6 100644 --- a/src/arch/x86_64/mod.rs +++ b/src/arch/x86_64/mod.rs @@ -318,17 +318,6 @@ mod tests { assert!(freq < 10000); } - #[test] - fn detect_freq_from_sysinfo() { - let freq_res = crate::detect_freq_from_sysinfo(); - assert!(freq_res.is_ok()); - // The unit of the value for the first core must be in MHz. - // We presume that more than 10 GHz is incorrect. - let freq = freq_res.unwrap(); - assert!(freq > 0); - assert!(freq < 10000); - } - #[test] fn test_pagetable_initialization() { let mut mem: Vec = vec![0; MIN_PHYSMEM_SIZE]; diff --git a/src/vm.rs b/src/vm.rs index ef513bcc2..fc7ff9bc6 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -14,6 +14,7 @@ use hermit_entry::{ elf::{KernelObject, LoadedKernel, ParseKernelError}, }; use log::{error, warn}; +use sysinfo::System; use thiserror::Error; #[cfg(target_arch = "x86_64")] @@ -23,7 +24,7 @@ use crate::arch::x86_64::{ #[cfg(all(target_arch = "x86_64", target_os = "linux"))] use crate::linux::x86_64::kvm_cpu::initialize_kvm; use crate::{ - arch, arch::detect_freq_from_sysinfo, consts::*, mem::MmapMemory, os::HypervisorError, + arch, arch::FrequencyDetectionFailed, consts::*, mem::MmapMemory, os::HypervisorError, params::Params, vcpu::VirtualCPU, virtio::*, }; @@ -41,6 +42,27 @@ pub enum LoadKernelError { pub type LoadKernelResult = Result; +pub fn detect_freq_from_sysinfo() -> std::result::Result { + debug!("Trying to detect CPU frequency using sysinfo"); + + let mut system = System::new(); + system.refresh_cpu_frequency(); + + let frequency = system.cpus().first().unwrap().frequency(); + + if !system.cpus().iter().all(|cpu| cpu.frequency() == frequency) { + // Even if the CPU frequencies are not all equal, the + // frequency of the "first" CPU is treated as "authoritative". + eprintln!("CPU frequencies are not all equal"); + } + + if frequency > 0 { + Ok(frequency.try_into().unwrap()) + } else { + Err(FrequencyDetectionFailed) + } +} + // TODO: move to architecture specific section fn detect_cpu_freq() -> u32 { #[cfg(target_arch = "aarch64")] @@ -260,3 +282,18 @@ impl fmt::Debug for UhyveVm { #[allow(clippy::non_send_fields_in_send_ty)] unsafe impl Send for UhyveVm {} unsafe impl Sync for UhyveVm {} + +#[cfg(test)] +mod tests { + #[test] + // derived from test_get_cpu_frequency_from_os() in src/arch/x86_64/mod.rs + fn test_detect_freq_from_sysinfo() { + let freq_res = crate::vm::detect_freq_from_sysinfo(); + assert!(freq_res.is_ok()); + let freq = freq_res.unwrap(); + // The unit of the value for the first core must be in MHz. + // We presume that more than 10 GHz is incorrect. + assert!(freq > 0); + assert!(freq < 10000); + } +}