Skip to content

Commit f072684

Browse files
slabtop: implemented tui
1 parent 1d83d7a commit f072684

File tree

6 files changed

+333
-118
lines changed

6 files changed

+333
-118
lines changed

Cargo.lock

Lines changed: 58 additions & 0 deletions
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
@@ -50,8 +50,10 @@ clap = { version = "4.5.4", features = ["wrap_help", "cargo"] }
5050
clap_complete = "4.5.2"
5151
clap_mangen = "0.2.20"
5252
crossterm = "0.28.1"
53+
im = "15.1.0"
5354
libc = "0.2.154"
5455
nix = { version = "0.29", default-features = false, features = ["process"] }
56+
parking_lot = "0.12.3"
5557
phf = "0.11.2"
5658
phf_codegen = "0.11.2"
5759
prettytable-rs = "0.10.0"

src/uu/slabtop/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ categories = ["command-line-utilities"]
1313

1414

1515
[dependencies]
16-
uucore = { workspace = true }
1716
clap = { workspace = true }
17+
crossterm = { workspace = true }
18+
im = { workspace = true }
19+
parking_lot = { workspace = true }
20+
ratatui = { workspace = true }
21+
uucore = { workspace = true }
1822

1923
[lib]
2024
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)