fix docker cpu usage again

This commit is contained in:
Robin Appelman 2021-03-30 14:26:10 +02:00
commit 11837ee97b

View file

@ -1,4 +1,3 @@
use crate::sensors::clock_ticks;
use bollard::container::{Stats, StatsOptions}; use bollard::container::{Stats, StatsOptions};
use bollard::Docker; use bollard::Docker;
use color_eyre::Result; use color_eyre::Result;
@ -13,6 +12,18 @@ pub struct Container {
cpu_time: f64, cpu_time: f64,
} }
impl From<Stats> for Container {
fn from(stats: Stats) -> Self {
Container {
name: stats.name,
memory: stats.memory_stats.usage.unwrap_or_default(),
cpu_time: stats.cpu_stats.cpu_usage.total_usage as f64
/ 1_000_000_000.0
/ stats.cpu_stats.online_cpus.unwrap_or(1) as f64,
}
}
}
impl Container { impl Container {
pub fn write<W: Write>(&self, mut w: W, hostname: &str) { pub fn write<W: Write>(&self, mut w: W, hostname: &str) {
writeln!( writeln!(
@ -28,17 +39,6 @@ impl Container {
) )
.ok(); .ok();
} }
fn from(stats: Stats, ticks: u64) -> Self {
Container {
name: stats.name,
memory: stats.memory_stats.usage.unwrap_or_default(),
cpu_time: stats.cpu_stats.cpu_usage.total_usage as f64
/ 1_000_000.0
/ ticks as f64
/ stats.cpu_stats.online_cpus.unwrap_or(1) as f64,
}
}
} }
pub async fn get_docker() -> Option<Docker> { pub async fn get_docker() -> Option<Docker> {
@ -53,7 +53,6 @@ pub async fn get_docker() -> Option<Docker> {
} }
pub async fn stat(docker: Docker) -> Result<impl Stream<Item = Container>> { pub async fn stat(docker: Docker) -> Result<impl Stream<Item = Container>> {
let ticks = clock_ticks()?;
let containers = docker.list_containers::<String>(None).await?; let containers = docker.list_containers::<String>(None).await?;
Ok(containers Ok(containers
.into_iter() .into_iter()
@ -72,7 +71,7 @@ pub async fn stat(docker: Docker) -> Result<impl Stream<Item = Container>> {
.next() .next()
.await? .await?
.ok()?; .ok()?;
Some(Container::from(stats, ticks)) Some(stats.into())
} }
}) })
.collect::<FuturesUnordered<_>>() .collect::<FuturesUnordered<_>>()