Skip to content

Commit

Permalink
sysinfo: Move sysinfo feature to vm.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
n0toose committed Jul 8, 2024
1 parent 9552c9c commit eca16d7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 47 deletions.
35 changes: 0 additions & 35 deletions src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use sysinfo::System;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -16,37 +15,3 @@ pub mod aarch64;

#[cfg(target_arch = "aarch64")]
pub use self::aarch64::*;

pub fn detect_freq_from_sysinfo() -> std::result::Result<u32, FrequencyDetectionFailed> {
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
}
}
11 changes: 0 additions & 11 deletions src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = vec![0; MIN_PHYSMEM_SIZE];
Expand Down
39 changes: 38 additions & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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::*,
};

Expand All @@ -41,6 +42,27 @@ pub enum LoadKernelError {

pub type LoadKernelResult<T> = Result<T, LoadKernelError>;

pub fn detect_freq_from_sysinfo() -> std::result::Result<u32, FrequencyDetectionFailed> {
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")]
Expand Down Expand Up @@ -260,3 +282,18 @@ impl<VCpuType: VirtualCPU> fmt::Debug for UhyveVm<VCpuType> {
#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl<VCpuType: VirtualCPU> Send for UhyveVm<VCpuType> {}
unsafe impl<VCpuType: VirtualCPU> Sync for UhyveVm<VCpuType> {}

#[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);
}
}

0 comments on commit eca16d7

Please sign in to comment.