From 97a1942f86c4752384168b34e25ed0ed743ff81d Mon Sep 17 00:00:00 2001 From: "alex.lyn" Date: Fri, 18 Apr 2025 17:24:47 +0800 Subject: [PATCH] kata-types: Optimize memory adjuesting by only gathering memory info The Coniguration initialization was observed to be significantly slow due to the extensive system information gathering performed by `sysinfo::System::new_all()`. This function collects data on CPU, memory, disks, and network, most of which is unnecessary for Kata's memory adjusting config phase, where only the total system memory is required. This commit optimizes the initialization process by implementing a more targeted approach to retrieve only the total system memory. This avoids the overhead of collecting a large amount of irrelevant data, resulting in a noticeable performance improvement. Fixes #11165 Signed-off-by: alex.lyn --- src/libs/kata-types/Cargo.toml | 2 +- src/libs/kata-types/src/config/hypervisor/mod.rs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libs/kata-types/Cargo.toml b/src/libs/kata-types/Cargo.toml index ecd86cec4..2879dc5a2 100644 --- a/src/libs/kata-types/Cargo.toml +++ b/src/libs/kata-types/Cargo.toml @@ -26,7 +26,7 @@ serde_json = "1.0.73" thiserror = "1.0" toml = "0.5.8" serde-enum-str = "0.4" -sysinfo = "0.30.5" +sysinfo = "0.34.2" oci-spec = { version = "0.6.8", features = ["runtime"] } safe-path = { path = "../safe-path" } diff --git a/src/libs/kata-types/src/config/hypervisor/mod.rs b/src/libs/kata-types/src/config/hypervisor/mod.rs index c9a1487fc..ef7d9773d 100644 --- a/src/libs/kata-types/src/config/hypervisor/mod.rs +++ b/src/libs/kata-types/src/config/hypervisor/mod.rs @@ -33,7 +33,7 @@ use std::collections::HashMap; use std::io::{self, Result}; use std::path::Path; use std::sync::{Arc, Mutex}; -use sysinfo::System; +use sysinfo::{MemoryRefreshKind, RefreshKind, System}; mod dragonball; pub use self::dragonball::{DragonballConfig, HYPERVISOR_NAME_DRAGONBALL}; @@ -751,7 +751,9 @@ impl MemoryInfo { "Memory backend file {} is invalid: {}" )?; if self.default_maxmemory == 0 { - let s = System::new_all(); + let s = System::new_with_specifics( + RefreshKind::nothing().with_memory(MemoryRefreshKind::everything()), + ); self.default_maxmemory = Byte::from_u64(s.total_memory()) .get_adjusted_unit(Unit::MiB) .get_value() as u32;