dkl cg ls: more cols + customizable list

This commit is contained in:
Mikaël Cluseau
2026-04-14 09:20:05 +02:00
parent dc936f52ab
commit c19798f9f0
2 changed files with 32 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
use clap::{CommandFactory, Parser, Subcommand};
use eyre::{Result, format_err};
use eyre::{format_err, Result};
use human_units::Duration;
use log::{debug, error};
use std::net::SocketAddr;
@@ -99,6 +99,8 @@ enum CgCmd {
root: Option<PathBuf>,
#[arg(long, short = 'X')]
exclude: Vec<String>,
#[arg(long, short = 'C')]
cols: Option<String>,
},
}
@@ -179,7 +181,11 @@ async fn main() -> Result<()> {
.map(|_| ())?),
C::Cg { cmd } => match cmd {
CgCmd::Ls { root, exclude } => Ok(dkl::cgroup::ls(root, &exclude).await?),
CgCmd::Ls {
root,
exclude,
cols,
} => Ok(dkl::cgroup::ls(root, &exclude, cols.as_deref()).await?),
},
}
}

View File

@@ -8,17 +8,36 @@ use crate::{fs, human::Human};
pub const ROOT: &str = "/sys/fs/cgroup";
pub async fn ls(parent: Option<impl AsRef<StdPath>>, exclude: &[String]) -> fs::Result<()> {
pub async fn ls(
parent: Option<impl AsRef<StdPath>>,
exclude: &[String],
columns: Option<&str>,
) -> fs::Result<()> {
let mut root = PathBuf::from(ROOT);
if let Some(parent) = parent {
root = root.join(parent);
}
let mut todo = vec![(Cgroup::root(root).await?, vec![], true)];
let cols: [(&str, fn(&Cgroup) -> String); _] = [
("wset", |cg| cg.memory.working_set().human()),
("anon", |cg| cg.memory.stat.anon.human()),
("min", |cg| cg.memory.min.human()),
("low", |cg| cg.memory.low.human()),
("high", |cg| cg.memory.high.human()),
("max", |cg| cg.memory.max.human()),
];
let cols = if let Some(columns) = columns {
(cols.into_iter())
.filter(|(n, _)| columns.split(',').any(|col| &col == n))
.collect()
} else {
cols.to_vec()
};
let mut table = tabled::builder::Builder::new();
table.push_record(["cgroup", "workg set", "anon", "max"]);
table.push_record(["cgroup"].into_iter().chain(cols.iter().map(|(n, _)| *n)));
let mut todo = vec![(Cgroup::root(root).await?, vec![], true)];
while let Some((cg, p_lasts, last)) = todo.pop() {
let mut name = String::new();
for last in p_lasts.iter().skip(1) {
@@ -29,12 +48,7 @@ pub async fn ls(parent: Option<impl AsRef<StdPath>>, exclude: &[String]) -> fs::
}
name.push_str(&cg.name());
table.push_record([
name,
cg.memory.working_set().human(),
cg.memory.stat.anon.human(),
cg.memory.max.human(),
]);
table.push_record([name].into_iter().chain(cols.iter().map(|(_, f)| f(&cg))));
let mut p_lasts = p_lasts.clone();
p_lasts.push(last);
@@ -50,8 +64,8 @@ pub async fn ls(parent: Option<impl AsRef<StdPath>>, exclude: &[String]) -> fs::
}
use tabled::settings::{
Alignment, Modify,
object::{Column, Row},
Alignment, Modify,
};
let mut table = table.build();
table.with(tabled::settings::Style::psql());