Skip to main content

read_container_stats

Function read_container_stats 

Source
pub async fn read_container_stats(cgroup_path: &Path) -> Result<ContainerStats>
Expand description

Read container statistics from cgroups v2 filesystem

Reads the following cgroup files:

  • cpu.stat for CPU usage (usage_usec field)
  • memory.current for current memory usage
  • memory.max for memory limit

§Arguments

  • cgroup_path - Path to the container’s cgroup directory

§Returns

  • Ok(ContainerStats) - Container statistics on success
  • Err(io::Error) - If any cgroup file cannot be read

§Errors

Returns an error if any cgroup file cannot be read.

§Example

use std::path::Path;
use zlayer_agent::cgroups_stats::read_container_stats;

let cgroup_path = Path::new("/sys/fs/cgroup/system.slice/zlayer-mycontainer.scope");
let stats = read_container_stats(cgroup_path).await?;
println!("CPU usage: {} usec", stats.cpu_usage_usec);
println!("Memory: {} bytes", stats.memory_bytes);