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 <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn 2025-04-18 17:24:47 +08:00
parent bf93b5daf1
commit 97a1942f86
2 changed files with 5 additions and 3 deletions

View File

@ -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" }

View File

@ -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;