dkl cg ls: more cols + customizable list
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
use clap::{CommandFactory, Parser, Subcommand};
|
use clap::{CommandFactory, Parser, Subcommand};
|
||||||
use eyre::{Result, format_err};
|
use eyre::{format_err, Result};
|
||||||
use human_units::Duration;
|
use human_units::Duration;
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
@@ -99,6 +99,8 @@ enum CgCmd {
|
|||||||
root: Option<PathBuf>,
|
root: Option<PathBuf>,
|
||||||
#[arg(long, short = 'X')]
|
#[arg(long, short = 'X')]
|
||||||
exclude: Vec<String>,
|
exclude: Vec<String>,
|
||||||
|
#[arg(long, short = 'C')]
|
||||||
|
cols: Option<String>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,7 +181,11 @@ async fn main() -> Result<()> {
|
|||||||
.map(|_| ())?),
|
.map(|_| ())?),
|
||||||
|
|
||||||
C::Cg { cmd } => match cmd {
|
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?),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,17 +8,36 @@ use crate::{fs, human::Human};
|
|||||||
|
|
||||||
pub const ROOT: &str = "/sys/fs/cgroup";
|
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);
|
let mut root = PathBuf::from(ROOT);
|
||||||
if let Some(parent) = parent {
|
if let Some(parent) = parent {
|
||||||
root = root.join(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();
|
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() {
|
while let Some((cg, p_lasts, last)) = todo.pop() {
|
||||||
let mut name = String::new();
|
let mut name = String::new();
|
||||||
for last in p_lasts.iter().skip(1) {
|
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());
|
name.push_str(&cg.name());
|
||||||
|
|
||||||
table.push_record([
|
table.push_record([name].into_iter().chain(cols.iter().map(|(_, f)| f(&cg))));
|
||||||
name,
|
|
||||||
cg.memory.working_set().human(),
|
|
||||||
cg.memory.stat.anon.human(),
|
|
||||||
cg.memory.max.human(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
let mut p_lasts = p_lasts.clone();
|
let mut p_lasts = p_lasts.clone();
|
||||||
p_lasts.push(last);
|
p_lasts.push(last);
|
||||||
@@ -50,8 +64,8 @@ pub async fn ls(parent: Option<impl AsRef<StdPath>>, exclude: &[String]) -> fs::
|
|||||||
}
|
}
|
||||||
|
|
||||||
use tabled::settings::{
|
use tabled::settings::{
|
||||||
Alignment, Modify,
|
|
||||||
object::{Column, Row},
|
object::{Column, Row},
|
||||||
|
Alignment, Modify,
|
||||||
};
|
};
|
||||||
let mut table = table.build();
|
let mut table = table.build();
|
||||||
table.with(tabled::settings::Style::psql());
|
table.with(tabled::settings::Style::psql());
|
||||||
|
|||||||
Reference in New Issue
Block a user