Skip to content

Commit

Permalink
slabtop: add -o/--once
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed May 9, 2024
1 parent 1f0ae84 commit 2c68b9c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 28 deletions.
62 changes: 35 additions & 27 deletions src/uu/slabtop/src/slabtop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// file that was distributed with this source code.

use crate::parse::SlabInfo;
use clap::{arg, crate_version, Command};
use clap::{arg, crate_version, ArgAction, Command};
use uucore::{error::UResult, format_usage, help_about, help_usage};

const ABOUT: &str = help_about!("slabtop.md");
Expand All @@ -24,12 +24,43 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let slabinfo = SlabInfo::new()?.sort(*sort_flag, false);

if matches.get_flag("once") {
output_header(&slabinfo);
println!();
output_list(&slabinfo);

Check warning on line 30 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L28-L30

Added lines #L28 - L30 were not covered by tests
} else {
// TODO: implement TUI
output_header(&slabinfo);
println!();
output_list(&slabinfo);

Check warning on line 35 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L33-L35

Added lines #L33 - L35 were not covered by tests
}

Ok(())

Check warning on line 38 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L38

Added line #L38 was not covered by tests
}

fn to_kb(byte: u64) -> f64 {
byte as f64 / 1024.0
}

Check warning on line 43 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L41-L43

Added lines #L41 - L43 were not covered by tests

fn percentage(numerator: u64, denominator: u64) -> f64 {

Check warning on line 45 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L45

Added line #L45 was not covered by tests
if denominator == 0 {
return 0.0;

Check warning on line 47 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L47

Added line #L47 was not covered by tests
}

let numerator = numerator as f64;
let denominator = denominator as f64;

Check warning on line 51 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L50-L51

Added lines #L50 - L51 were not covered by tests

(numerator / denominator) * 100.0
}

Check warning on line 54 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L53-L54

Added lines #L53 - L54 were not covered by tests

fn output_header(slabinfo: &SlabInfo) {

Check warning on line 56 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L56

Added line #L56 was not covered by tests
println!(
r" Active / Total Objects (% used) : {} / {} ({:.1}%)",
slabinfo.total_active_objs(),
slabinfo.total_objs(),
percentage(slabinfo.total_active_objs(), slabinfo.total_objs())
);

println!(
r" Active / Total Slabs (% used) : {} / {} ({:.1}%)",
slabinfo.total_active_slabs(),
Expand Down Expand Up @@ -58,38 +89,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
to_kb(slabinfo.object_avg()),
to_kb(slabinfo.object_maximum())
);
}

Check warning on line 92 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L92

Added line #L92 was not covered by tests

// separate header info and slab list
println!();

// TODO: TUI Implementation
fn output_list(info: &SlabInfo) {

Check warning on line 94 in src/uu/slabtop/src/slabtop.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/slabtop/src/slabtop.rs#L94

Added line #L94 was not covered by tests
let title = format!(
"{:>6} {:>6} {:>4} {:>8} {:>6} {:>8} {:>10} {:<}",
"OBJS", "ACTIVE", "USE", "OBJ SIZE", "SLABS", "OBJ/SLAB", "CACHE SIZE", "NAME"
);
println!("{}", title);

output(&slabinfo);

Ok(())
}

fn to_kb(byte: u64) -> f64 {
byte as f64 / 1024.0
}

fn percentage(numerator: u64, denominator: u64) -> f64 {
if denominator == 0 {
return 0.0;
}

let numerator = numerator as f64;
let denominator = denominator as f64;

(numerator / denominator) * 100.0
}

fn output(info: &SlabInfo) {
for name in info.names() {
let objs = info.fetch(name, "num_objs").unwrap_or_default();
let active = info.fetch(name, "active_objs").unwrap_or_default();
Expand Down Expand Up @@ -121,7 +129,7 @@ pub fn uu_app() -> Command {
.infer_long_args(true)
.args([
// arg!(-d --delay <secs> "delay updates"),
// arg!(-o --once "only display once, then exit"),
arg!(-o --once "only display once, then exit").action(ArgAction::SetTrue),
arg!(-s --sort <char> "specify sort criteria by character (see below)"),
])
.after_help(
Expand Down
59 changes: 58 additions & 1 deletion tests/by-util/test_slabtop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#[cfg(target_os = "linux")]
use crate::common::util::run_ucmd_as_root;
use crate::common::util::TestScenario;

#[test]
Expand All @@ -11,6 +13,61 @@ fn test_invalid_arg() {
}

#[test]
fn test_slabtop() {
fn test_help() {
new_ucmd!().arg("--help").succeeds().code_is(0);
}

#[cfg(target_os = "linux")]
#[test]
fn test_without_args_as_non_root() {
new_ucmd!()
.fails()
.code_is(1)
.stderr_contains("Permission denied");
}

// TODO: tests some temporary behavior; in the future a TUI should be used
// if there are no args
#[cfg(target_os = "linux")]
#[test]
fn test_without_args_as_root() {
let ts = TestScenario::new(util_name!());

if let Ok(result) = run_ucmd_as_root(&ts, &[]) {
result

Check warning on line 37 in tests/by-util/test_slabtop.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_slabtop.rs#L37

Added line #L37 was not covered by tests
.success()
.stdout_contains("Active / Total Objects")
.stdout_contains("OBJS");
} else {

Check warning on line 41 in tests/by-util/test_slabtop.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_slabtop.rs#L41

Added line #L41 was not covered by tests
print!("Test skipped; requires root user");
}
}

#[cfg(target_os = "linux")]
#[test]
fn test_once_as_non_root() {
for arg in ["-o", "--once"] {
new_ucmd!()
.arg(arg)
.fails()
.code_is(1)
.stderr_contains("Permission denied");
}
}

#[cfg(target_os = "linux")]
#[test]
fn test_once_as_root() {
let ts = TestScenario::new(util_name!());

for arg in ["-o", "--once"] {
if let Ok(result) = run_ucmd_as_root(&ts, &[arg]) {
result

Check warning on line 65 in tests/by-util/test_slabtop.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_slabtop.rs#L65

Added line #L65 was not covered by tests
.success()
.stdout_contains("Active / Total Objects")
.stdout_contains("OBJS");
} else {

Check warning on line 69 in tests/by-util/test_slabtop.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_slabtop.rs#L69

Added line #L69 was not covered by tests
print!("Test skipped; requires root user");
}
}
}

0 comments on commit 2c68b9c

Please sign in to comment.