Skip to content

Commit 995013e

Browse files
committed
vmstat: impl --unit
1 parent a95ff29 commit 995013e

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/uu/vmstat/src/picker.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#[cfg(target_os = "linux")]
77
use crate::{CpuLoad, Meminfo, ProcData};
88
use clap::ArgMatches;
9+
use uucore::error::USimpleError;
910

1011
#[cfg(target_os = "linux")]
1112
pub type Picker = (
@@ -64,6 +65,22 @@ pub fn get_pickers(matches: &ArgMatches) -> Vec<Picker> {
6465
]
6566
}
6667

68+
fn with_unit(x: u64, arg: &ArgMatches) -> u64 {
69+
if let Some(unit) = arg.get_one::<String>("unit") {
70+
return match unit.as_str() {
71+
"k" => x / bytesize::KB,
72+
"K" => x / bytesize::KIB,
73+
"m" => x / bytesize::MB,
74+
"M" => x / bytesize::MIB,
75+
_ => panic!(
76+
"{:?}",
77+
USimpleError::new(1, "-S requires k, K, m or M (default is KiB)",)
78+
),
79+
};
80+
}
81+
x / bytesize::KIB
82+
}
83+
6784
#[cfg(target_os = "linux")]
6885
fn concat_helper(
6986
title: (String, String),
@@ -123,16 +140,18 @@ fn get_memory_info(
123140
_proc_data_before: Option<&ProcData>,
124141
matches: &ArgMatches,
125142
) -> Vec<(usize, String)> {
126-
use bytesize::*;
127143
let len = if matches.get_flag("wide") { 12 } else { 6 };
128144
let memory_info = Meminfo::from_proc_map(&proc_data.meminfo);
129145

130-
let swap_used = (memory_info.swap_total - memory_info.swap_free).as_u64() / KB;
131-
let free = memory_info.mem_free.as_u64() / KB;
146+
let swap_used = with_unit(
147+
(memory_info.swap_total - memory_info.swap_free).as_u64(),
148+
matches,
149+
);
150+
let free = with_unit(memory_info.mem_free.as_u64(), matches);
132151

133152
if matches.get_flag("active") {
134-
let inactive = memory_info.inactive.as_u64() / KB;
135-
let active = memory_info.active.as_u64() / KB;
153+
let inactive = with_unit(memory_info.inactive.as_u64(), matches);
154+
let active = with_unit(memory_info.active.as_u64(), matches);
136155
return vec![
137156
(len, format!("{}", swap_used)),
138157
(len, format!("{}", free)),
@@ -141,8 +160,8 @@ fn get_memory_info(
141160
];
142161
}
143162

144-
let buffer = memory_info.buffers.as_u64() / KB;
145-
let cache = memory_info.cached.as_u64() / KB;
163+
let buffer = with_unit(memory_info.buffers.as_u64(), matches);
164+
let cache = with_unit(memory_info.cached.as_u64(), matches);
146165

147166
vec![
148167
(len, format!("{}", swap_used)),

src/uu/vmstat/src/vmstat.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ pub fn uu_app() -> Command {
136136
// arg!(-d --disk "Report disk statistics"),
137137
// arg!(-D --"disk-sum" "Report some summary statistics about disk activity"),
138138
// arg!(-p --partition <device> "Detailed statistics about partition"),
139-
// arg!(-S --unit <character> "Switches outputs between 1000 (k), 1024 (K), 1000000 (m), or 1048576 (M) bytes"),
139+
arg!(-S --unit <character> "Switches outputs between 1000 (k), 1024 (K), 1000000 (m), or 1048576 (M) bytes"),
140140
// arg!(-t --timestamp "Append timestamp to each line"),
141-
// arg!(-w --wide "Wide output mode"),
142141
arg!(-w --wide "Wide output mode"),
143142
arg!(-y --"no-first" "Omits first report with statistics since system boot"),
144143
])

0 commit comments

Comments
 (0)