Skip to content

Commit a95ff29

Browse files
committed
vmstat: impl --active --wide
1 parent 1e63a38 commit a95ff29

File tree

3 files changed

+104
-30
lines changed

3 files changed

+104
-30
lines changed

src/uu/vmstat/src/parser.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ pub struct Meminfo {
136136
pub buffers: bytesize::ByteSize,
137137
pub cached: bytesize::ByteSize,
138138
pub swap_cached: bytesize::ByteSize,
139+
pub active: bytesize::ByteSize,
140+
pub inactive: bytesize::ByteSize,
139141
pub swap_total: bytesize::ByteSize,
140142
pub swap_free: bytesize::ByteSize,
141143
}
@@ -157,6 +159,8 @@ impl Meminfo {
157159
let cached = bytesize::ByteSize::from_str(proc_map.get("Cached").unwrap()).unwrap();
158160
let swap_cached =
159161
bytesize::ByteSize::from_str(proc_map.get("SwapCached").unwrap()).unwrap();
162+
let active = bytesize::ByteSize::from_str(proc_map.get("Active").unwrap()).unwrap();
163+
let inactive = bytesize::ByteSize::from_str(proc_map.get("Inactive").unwrap()).unwrap();
160164
let swap_total = bytesize::ByteSize::from_str(proc_map.get("SwapTotal").unwrap()).unwrap();
161165
let swap_free = bytesize::ByteSize::from_str(proc_map.get("SwapFree").unwrap()).unwrap();
162166
Self {
@@ -166,6 +170,8 @@ impl Meminfo {
166170
buffers,
167171
cached,
168172
swap_cached,
173+
active,
174+
inactive,
169175
swap_total,
170176
swap_free,
171177
}

src/uu/vmstat/src/picker.rs

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,60 @@
55

66
#[cfg(target_os = "linux")]
77
use crate::{CpuLoad, Meminfo, ProcData};
8+
use clap::ArgMatches;
89

910
#[cfg(target_os = "linux")]
1011
pub type Picker = (
1112
(String, String),
12-
Box<dyn Fn(&ProcData, Option<&ProcData>, &mut Vec<String>, &mut usize)>,
13+
Box<dyn Fn(&ProcData, Option<&ProcData>, &ArgMatches, &mut Vec<String>, &mut usize)>,
1314
);
1415

1516
#[cfg(target_os = "linux")]
16-
pub fn get_pickers() -> Vec<Picker> {
17+
pub fn get_pickers(matches: &ArgMatches) -> Vec<Picker> {
18+
let wide = matches.get_flag("wide");
1719
vec![
18-
concat_helper(("procs".into(), " r b".into()), get_process_info),
1920
concat_helper(
20-
(
21-
"-----------memory----------".into(),
22-
" swpd free buff cache".into(),
23-
),
21+
if wide {
22+
("--procs--".into(), " r b".into())
23+
} else {
24+
("procs".into(), " r b".into())
25+
},
26+
get_process_info,
27+
),
28+
concat_helper(
29+
if wide {
30+
(
31+
"-----------------------memory----------------------".into(),
32+
if matches.get_flag("active") {
33+
" swpd free inact active".into()
34+
} else {
35+
" swpd free buff cache".into()
36+
},
37+
)
38+
} else {
39+
(
40+
"-----------memory----------".into(),
41+
if matches.get_flag("active") {
42+
" swpd free inact active".into()
43+
} else {
44+
" swpd free buff cache".into()
45+
},
46+
)
47+
},
2448
get_memory_info,
2549
),
2650
concat_helper(("---swap--".into(), " si so".into()), get_swap_info),
2751
concat_helper(("-----io----".into(), " bi bo".into()), get_io_info),
2852
concat_helper(("-system--".into(), " in cs".into()), get_system_info),
2953
concat_helper(
30-
("-------cpu-------".into(), "us sy id wa st gu".into()),
54+
if wide {
55+
(
56+
"----------cpu----------".into(),
57+
" us sy id wa st gu".into(),
58+
)
59+
} else {
60+
("-------cpu-------".into(), "us sy id wa st gu".into())
61+
},
3162
get_cpu_info,
3263
),
3364
]
@@ -36,16 +67,17 @@ pub fn get_pickers() -> Vec<Picker> {
3667
#[cfg(target_os = "linux")]
3768
fn concat_helper(
3869
title: (String, String),
39-
func: impl Fn(&ProcData, Option<&ProcData>) -> Vec<(usize, String)> + 'static,
70+
func: impl Fn(&ProcData, Option<&ProcData>, &ArgMatches) -> Vec<(usize, String)> + 'static,
4071
) -> Picker {
4172
(
4273
title,
4374
Box::from(
4475
move |proc_data: &ProcData,
4576
proc_data_before: Option<&ProcData>,
77+
matches: &ArgMatches,
4678
data: &mut Vec<String>,
4779
data_len_excess: &mut usize| {
48-
let output = func(proc_data, proc_data_before);
80+
let output = func(proc_data, proc_data_before, matches);
4981
output.iter().for_each(|(len, value)| {
5082
let len = if *data_len_excess > *len {
5183
0
@@ -76,39 +108,55 @@ macro_rules! diff {
76108
fn get_process_info(
77109
proc_data: &ProcData,
78110
_proc_data_before: Option<&ProcData>,
111+
matches: &ArgMatches,
79112
) -> Vec<(usize, String)> {
80113
let runnable = proc_data.stat.get("procs_running").unwrap();
81114
let blocked = proc_data.stat.get("procs_blocked").unwrap();
115+
let len = if matches.get_flag("wide") { 4 } else { 2 };
82116

83-
vec![(2, runnable.to_string()), (2, blocked.to_string())]
117+
vec![(len, runnable.to_string()), (len, blocked.to_string())]
84118
}
85119

86120
#[cfg(target_os = "linux")]
87121
fn get_memory_info(
88122
proc_data: &ProcData,
89123
_proc_data_before: Option<&ProcData>,
124+
matches: &ArgMatches,
90125
) -> Vec<(usize, String)> {
91126
use bytesize::*;
92-
127+
let len = if matches.get_flag("wide") { 12 } else { 6 };
93128
let memory_info = Meminfo::from_proc_map(&proc_data.meminfo);
94129

95130
let swap_used = (memory_info.swap_total - memory_info.swap_free).as_u64() / KB;
96131
let free = memory_info.mem_free.as_u64() / KB;
132+
133+
if matches.get_flag("active") {
134+
let inactive = memory_info.inactive.as_u64() / KB;
135+
let active = memory_info.active.as_u64() / KB;
136+
return vec![
137+
(len, format!("{}", swap_used)),
138+
(len, format!("{}", free)),
139+
(len, format!("{}", inactive)),
140+
(len, format!("{}", active)),
141+
];
142+
}
143+
97144
let buffer = memory_info.buffers.as_u64() / KB;
98145
let cache = memory_info.cached.as_u64() / KB;
99146

100147
vec![
101-
(6, format!("{}", swap_used)),
102-
(6, format!("{}", free)),
103-
(6, format!("{}", buffer)),
104-
(6, format!("{}", cache)),
148+
(len, format!("{}", swap_used)),
149+
(len, format!("{}", free)),
150+
(len, format!("{}", buffer)),
151+
(len, format!("{}", cache)),
105152
]
106153
}
107154

108155
#[cfg(target_os = "linux")]
109156
fn get_swap_info(
110157
proc_data: &ProcData,
111158
proc_data_before: Option<&ProcData>,
159+
_matches: &ArgMatches,
112160
) -> Vec<(usize, String)> {
113161
let period = diff!(proc_data, proc_data_before, uptime.0);
114162
let swap_in = diff!(
@@ -129,7 +177,11 @@ fn get_swap_info(
129177
}
130178

131179
#[cfg(target_os = "linux")]
132-
fn get_io_info(proc_data: &ProcData, proc_data_before: Option<&ProcData>) -> Vec<(usize, String)> {
180+
fn get_io_info(
181+
proc_data: &ProcData,
182+
proc_data_before: Option<&ProcData>,
183+
_matches: &ArgMatches,
184+
) -> Vec<(usize, String)> {
133185
let period = diff!(proc_data, proc_data_before, uptime.0);
134186
let read_bytes = diff!(
135187
proc_data,
@@ -152,6 +204,7 @@ fn get_io_info(proc_data: &ProcData, proc_data_before: Option<&ProcData>) -> Vec
152204
fn get_system_info(
153205
proc_data: &ProcData,
154206
proc_data_before: Option<&ProcData>,
207+
_matches: &ArgMatches,
155208
) -> Vec<(usize, String)> {
156209
let period = diff!(proc_data, proc_data_before, uptime.0);
157210

@@ -182,15 +235,18 @@ fn get_system_info(
182235
fn get_cpu_info(
183236
proc_data: &ProcData,
184237
_proc_data_before: Option<&ProcData>,
238+
matches: &ArgMatches,
185239
) -> Vec<(usize, String)> {
240+
let len = if matches.get_flag("wide") { 3 } else { 2 };
241+
186242
let cpu_load = CpuLoad::from_proc_map(&proc_data.stat);
187243

188244
vec![
189-
(2, format!("{:.0}", cpu_load.user)),
190-
(2, format!("{:.0}", cpu_load.system)),
191-
(2, format!("{:.0}", cpu_load.idle)),
192-
(2, format!("{:.0}", cpu_load.io_wait)),
193-
(2, format!("{:.0}", cpu_load.steal_time)),
194-
(2, format!("{:.0}", cpu_load.guest)),
245+
(len, format!("{:.0}", cpu_load.user)),
246+
(len, format!("{:.0}", cpu_load.system)),
247+
(len, format!("{:.0}", cpu_load.idle)),
248+
(len, format!("{:.0}", cpu_load.io_wait)),
249+
(len, format!("{:.0}", cpu_load.steal_time)),
250+
(len, format!("{:.0}", cpu_load.guest)),
195251
]
196252
}

src/uu/vmstat/src/vmstat.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod picker;
88

99
#[cfg(target_os = "linux")]
1010
use crate::picker::{get_pickers, Picker};
11-
use clap::{arg, crate_version, Command};
11+
use clap::{arg, crate_version, ArgMatches, Command};
1212
#[allow(unused_imports)]
1313
pub use parser::*;
1414
#[allow(unused_imports)]
@@ -57,13 +57,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5757
}
5858
}
5959

60-
let pickers = get_pickers();
60+
let pickers = get_pickers(&matches);
6161
let mut proc_data = ProcData::new();
6262

6363
let mut line_count = 0;
6464
print_header(&pickers);
6565
if !no_first {
66-
print_data(&pickers, &proc_data, None);
66+
print_data(&pickers, &proc_data, None, &matches);
6767
line_count += 1;
6868
}
6969

@@ -77,7 +77,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
7777
if !one_header && term_height > 0 && ((line_count + 3) % term_height as i64 == 0) {
7878
print_header(&pickers);
7979
}
80-
print_data(&pickers, &proc_data_now, Some(&proc_data));
80+
print_data(&pickers, &proc_data_now, Some(&proc_data), &matches);
8181
line_count += 1;
8282
proc_data = proc_data_now;
8383
}
@@ -98,11 +98,22 @@ fn print_header(pickers: &[Picker]) {
9898
println!("{}", title.join(" "));
9999
}
100100
#[cfg(target_os = "linux")]
101-
fn print_data(pickers: &[Picker], proc_data: &ProcData, proc_data_before: Option<&ProcData>) {
101+
fn print_data(
102+
pickers: &[Picker],
103+
proc_data: &ProcData,
104+
proc_data_before: Option<&ProcData>,
105+
matches: &ArgMatches,
106+
) {
102107
let mut data: Vec<String> = vec![];
103108
let mut data_len_excess = 0;
104109
pickers.iter().for_each(|f| {
105-
f.1(proc_data, proc_data_before, &mut data, &mut data_len_excess);
110+
f.1(
111+
proc_data,
112+
proc_data_before,
113+
matches,
114+
&mut data,
115+
&mut data_len_excess,
116+
);
106117
});
107118
println!("{}", data.join(" "));
108119
}
@@ -117,7 +128,7 @@ pub fn uu_app() -> Command {
117128
.args([
118129
arg!(<delay> "The delay between updates in seconds").required(false),
119130
arg!(<count> "Number of updates").required(false),
120-
// arg!(-a --active "Display active and inactive memory"),
131+
arg!(-a --active "Display active and inactive memory"),
121132
// arg!(-f --forks "switch displays the number of forks since boot"),
122133
// arg!(-m --slabs "Display slabinfo"),
123134
arg!(-n --"one-header" "Display the header only once rather than periodically"),
@@ -128,6 +139,7 @@ pub fn uu_app() -> Command {
128139
// arg!(-S --unit <character> "Switches outputs between 1000 (k), 1024 (K), 1000000 (m), or 1048576 (M) bytes"),
129140
// arg!(-t --timestamp "Append timestamp to each line"),
130141
// arg!(-w --wide "Wide output mode"),
142+
arg!(-w --wide "Wide output mode"),
131143
arg!(-y --"no-first" "Omits first report with statistics since system boot"),
132144
])
133145
}

0 commit comments

Comments
 (0)