Skip to content

Commit 46b2b1b

Browse files
Merge pull request #668 from Bluemangoo/fix/vmstat-meminfo
vmstat: fix meminfo unit
2 parents 75e5833 + 251f9b3 commit 46b2b1b

1 file changed

Lines changed: 30 additions & 12 deletions

File tree

src/uu/vmstat/src/parser.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ pub struct Meminfo {
211211
pub swap_total: bytesize::ByteSize,
212212
pub swap_free: bytesize::ByteSize,
213213
}
214+
215+
#[cfg(target_os = "linux")]
216+
fn kb_to_kib(mut size: bytesize::ByteSize) -> bytesize::ByteSize {
217+
// "kB" means KiB instead of KB
218+
// Convert from 1000-based(parsed by bytesize from "kB" ended string) to 1024-based(which it actually is)
219+
// See more at https://github.com/uutils/procps/issues/667
220+
size.0 = size.0 / 1000 * 1024;
221+
size
222+
}
223+
214224
#[cfg(target_os = "linux")]
215225
impl Meminfo {
216226
pub fn current() -> Self {
@@ -221,20 +231,28 @@ impl Meminfo {
221231
pub fn from_proc_map(proc_map: &HashMap<String, String>) -> Self {
222232
use std::str::FromStr;
223233

224-
let mem_total = bytesize::ByteSize::from_str(proc_map.get("MemTotal").unwrap()).unwrap();
225-
let mem_free = bytesize::ByteSize::from_str(proc_map.get("MemFree").unwrap()).unwrap();
234+
let mem_total =
235+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("MemTotal").unwrap()).unwrap());
236+
let mem_free =
237+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("MemFree").unwrap()).unwrap());
226238
let mem_available =
227-
bytesize::ByteSize::from_str(proc_map.get("MemAvailable").unwrap()).unwrap();
228-
let buffers = bytesize::ByteSize::from_str(proc_map.get("Buffers").unwrap()).unwrap();
229-
let cached = bytesize::ByteSize::from_str(proc_map.get("Cached").unwrap()).unwrap();
239+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("MemAvailable").unwrap()).unwrap());
240+
let buffers =
241+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("Buffers").unwrap()).unwrap());
242+
let cached =
243+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("Cached").unwrap()).unwrap());
230244
let s_reclaimable =
231-
bytesize::ByteSize::from_str(proc_map.get("SReclaimable").unwrap()).unwrap();
245+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("SReclaimable").unwrap()).unwrap());
232246
let swap_cached =
233-
bytesize::ByteSize::from_str(proc_map.get("SwapCached").unwrap()).unwrap();
234-
let active = bytesize::ByteSize::from_str(proc_map.get("Active").unwrap()).unwrap();
235-
let inactive = bytesize::ByteSize::from_str(proc_map.get("Inactive").unwrap()).unwrap();
236-
let swap_total = bytesize::ByteSize::from_str(proc_map.get("SwapTotal").unwrap()).unwrap();
237-
let swap_free = bytesize::ByteSize::from_str(proc_map.get("SwapFree").unwrap()).unwrap();
247+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("SwapCached").unwrap()).unwrap());
248+
let active =
249+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("Active").unwrap()).unwrap());
250+
let inactive =
251+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("Inactive").unwrap()).unwrap());
252+
let swap_total =
253+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("SwapTotal").unwrap()).unwrap());
254+
let swap_free =
255+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("SwapFree").unwrap()).unwrap());
238256
Self {
239257
mem_total,
240258
mem_free,
@@ -258,7 +276,7 @@ pub struct DiskStatParseError;
258276
#[cfg(target_os = "linux")]
259277
impl Display for DiskStatParseError {
260278
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
261-
std::fmt::Debug::fmt("Failed to parse diskstat line", f)
279+
Debug::fmt("Failed to parse diskstat line", f)
262280
}
263281
}
264282

0 commit comments

Comments
 (0)