Skip to content

Commit 02708a9

Browse files
committed
vmstat: fix meminfo unit
1 parent cd37fba commit 02708a9

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

src/uu/vmstat/src/parser.rs

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

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();
233+
let mem_total =
234+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("MemTotal").unwrap()).unwrap());
235+
let mem_free =
236+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("MemFree").unwrap()).unwrap());
226237
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();
238+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("MemAvailable").unwrap()).unwrap());
239+
let buffers =
240+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("Buffers").unwrap()).unwrap());
241+
let cached =
242+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("Cached").unwrap()).unwrap());
230243
let s_reclaimable =
231-
bytesize::ByteSize::from_str(proc_map.get("SReclaimable").unwrap()).unwrap();
244+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("SReclaimable").unwrap()).unwrap());
232245
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();
246+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("SwapCached").unwrap()).unwrap());
247+
let active =
248+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("Active").unwrap()).unwrap());
249+
let inactive =
250+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("Inactive").unwrap()).unwrap());
251+
let swap_total =
252+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("SwapTotal").unwrap()).unwrap());
253+
let swap_free =
254+
kb_to_kib(bytesize::ByteSize::from_str(proc_map.get("SwapFree").unwrap()).unwrap());
238255
Self {
239256
mem_total,
240257
mem_free,
@@ -258,7 +275,7 @@ pub struct DiskStatParseError;
258275
#[cfg(target_os = "linux")]
259276
impl Display for DiskStatParseError {
260277
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
261-
std::fmt::Debug::fmt("Failed to parse diskstat line", f)
278+
Debug::fmt("Failed to parse diskstat line", f)
262279
}
263280
}
264281

0 commit comments

Comments
 (0)