Skip to content

Commit 468ab4f

Browse files
slabtop: implemented tui
1 parent 39cb446 commit 468ab4f

6 files changed

Lines changed: 334 additions & 119 deletions

File tree

Cargo.lock

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ clap_complete = "4.5.2"
5858
clap_mangen = "0.2.20"
5959
crossterm = "0.29.0"
6060
ctor = "0.4.1"
61+
im = "15.1.0"
6162
libc = "0.2.154"
6263
nix = { version = "0.30", default-features = false, features = ["process"] }
64+
parking_lot = "0.12.3"
6365
phf = "0.11.2"
6466
phf_codegen = "0.11.2"
6567
prettytable-rs = "0.10.0"

src/uu/slabtop/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ license.workspace = true
1111
version.workspace = true
1212

1313
[dependencies]
14-
uucore = { workspace = true }
1514
clap = { workspace = true }
15+
crossterm = { workspace = true }
16+
im = { workspace = true }
17+
parking_lot = { workspace = true }
18+
ratatui = { workspace = true }
19+
uucore = { workspace = true }
1620

1721
[lib]
1822
path = "src/slabtop.rs"

src/uu/slabtop/src/parse.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,29 @@ use std::{
99
io::{Error, ErrorKind},
1010
};
1111

12-
#[derive(Debug, Default)]
12+
#[derive(Debug, Default, Clone)]
1313
pub(crate) struct SlabInfo {
14-
pub(crate) meta: Vec<String>,
15-
pub(crate) data: Vec<(String, Vec<u64>)>,
14+
pub(crate) meta: im::Vector<String>,
15+
pub(crate) data: im::Vector<(String, Vec<u64>)>,
1616
}
1717

1818
impl SlabInfo {
1919
// parse slabinfo from /proc/slabinfo
20+
//
2021
// need root permission
2122
pub fn new() -> Result<SlabInfo, Error> {
2223
let content = fs::read_to_string("/proc/slabinfo")?;
2324

24-
Self::parse(&content).ok_or(ErrorKind::Unsupported.into())
25+
Self::with_slabinfo(&content).ok_or(ErrorKind::Unsupported.into())
2526
}
2627

27-
pub fn parse(content: &str) -> Option<SlabInfo> {
28+
pub fn with_slabinfo(content: &str) -> Option<SlabInfo> {
2829
let mut lines: Vec<&str> = content.lines().collect();
2930

3031
let _ = parse_version(lines.remove(0))?;
3132
let meta = parse_meta(lines.remove(0));
32-
let data: Vec<(String, Vec<u64>)> = lines.into_iter().filter_map(parse_data).collect();
33+
let data: im::Vector<(String, Vec<u64>)> =
34+
lines.into_iter().filter_map(parse_data).collect();
3335

3436
Some(SlabInfo { meta, data })
3537
}
@@ -43,6 +45,7 @@ impl SlabInfo {
4345
item.get(offset).copied()
4446
}
4547

48+
// Get all processes name
4649
pub fn names(&self) -> Vec<&String> {
4750
self.data.iter().map(|(k, _)| k).collect()
4851
}
@@ -190,15 +193,17 @@ impl SlabInfo {
190193
return 0;
191194
};
192195

193-
let iter = self.data.iter().filter_map(|(_, data)| data.get(offset));
194-
195-
let count = iter.clone().count();
196-
let sum = iter.sum::<u64>();
196+
let objsize = self
197+
.data
198+
.iter()
199+
.filter_map(|(_, data)| data.get(offset))
200+
.collect::<im::Vector<_>>();
197201

202+
let count = objsize.clone().iter().count();
198203
if count == 0 {
199204
0
200205
} else {
201-
(sum) / (count as u64)
206+
(objsize.into_iter().sum::<u64>()) / (count as u64)
202207
}
203208
}
204209

@@ -266,7 +271,7 @@ pub(crate) fn parse_version(line: &str) -> Option<String> {
266271
.map(String::from)
267272
}
268273

269-
pub(crate) fn parse_meta(line: &str) -> Vec<String> {
274+
pub(crate) fn parse_meta(line: &str) -> im::Vector<String> {
270275
line.replace(['#', ':'], " ")
271276
.split_whitespace()
272277
.filter(|it| it.starts_with('<') && it.ends_with('>'))
@@ -310,8 +315,8 @@ mod tests {
310315
let result = parse_meta(test);
311316

312317
assert_eq!(
313-
result,
314-
[
318+
result.into_iter().collect::<Vec<_>>(),
319+
vec![
315320
"active_objs",
316321
"num_objs",
317322
"objsize",
@@ -348,7 +353,7 @@ mod tests {
348353
#[test]
349354
fn test_parse() {
350355
let test = include_str!("../../../../tests/fixtures/slabtop/data.txt");
351-
let result = SlabInfo::parse(test.into()).unwrap();
356+
let result = SlabInfo::with_slabinfo(test).unwrap();
352357

353358
assert_eq!(result.fetch("nf_conntrack_expect", "objsize").unwrap(), 208);
354359
assert_eq!(

0 commit comments

Comments
 (0)