Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/uu/vmstat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bytesize = { workspace = true }
clap = { workspace = true }
jiff = { workspace = true }
terminal_size = { workspace = true }
uucore = { workspace = true }
uucore = { workspace = true, features = ["libc"] }

uu_slabtop = {path = "../slabtop"}

Expand Down
37 changes: 34 additions & 3 deletions src/uu/vmstat/src/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ mod tests {
}
}

#[test]
fn swap_rate_uses_unit() {
// 483022 pages of 4 KiB swapped in over 107985.81 seconds is about
// 18321 bytes per second
let rate = |args: &[&str]| {
let matches = crate::uu_app().get_matches_from(args);
swap_rate(483_022, 4096, 107_985.81, &matches)
};
assert_eq!(rate(&["vmstat"]), 17);
assert_eq!(rate(&["vmstat", "-S", "K"]), 17);
assert_eq!(rate(&["vmstat", "-S", "k"]), 18);
assert_eq!(rate(&["vmstat", "-S", "M"]), 0);
}

#[test]
fn stats_user_ticks_use_raw_user() {
for (user, nice) in [(100, 20), (10, 20)] {
Expand Down Expand Up @@ -443,11 +457,18 @@ fn get_memory_info(
]
}

/// Memory swapped per second, from a number of pages: like the memory
/// columns, `si` and `so` are shown in the `--unit` size, KiB by default.
#[cfg(target_os = "linux")]
fn swap_rate(pages: u64, page_size: u64, period: f64, matches: &ArgMatches) -> u64 {
with_unit(((pages * page_size) as f64 / period) as u64, matches)
}

#[cfg(target_os = "linux")]
fn get_swap_info(
proc_data: &ProcData,
proc_data_before: Option<&ProcData>,
_matches: &ArgMatches,
matches: &ArgMatches,
) -> Vec<(usize, String)> {
let period = diff!(proc_data, proc_data_before, uptime.0);
let swap_in = diff!(
Expand All @@ -461,9 +482,19 @@ fn get_swap_info(
vmstat.get("pswpout").unwrap().parse::<u64>().unwrap()
);

// The kernel counts swapped pages.
// SAFETY: sysconf has no preconditions.
let page_size = unsafe { uucore::libc::sysconf(uucore::libc::_SC_PAGESIZE) } as u64;

vec![
(4, format!("{:.0}", swap_in as f64 / period)),
(4, format!("{:.0}", swap_out as f64 / period)),
(
4,
format!("{}", swap_rate(swap_in, page_size, period, matches)),
),
(
4,
format!("{}", swap_rate(swap_out, page_size, period, matches)),
),
]
}

Expand Down
Loading