Skip to content

Commit 28c263c

Browse files
committed
memory: Directly convert reported memory usage into bytes
`/proc/meminfo` states that it's report values are in KB, when they actually are in KiB. Previously, this inconsistency leaked into the whole code for this block (which had to add `* 1024` after nearly every assignment). Now this inconsistency is contained to the `Memstate` structure.
1 parent 6b52efa commit 28c263c

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/blocks/memory.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
112112
loop {
113113
let mem_state = Memstate::new().await?;
114114

115-
let mem_total = mem_state.mem_total as f64 * 1024.;
116-
let mem_free = mem_state.mem_free as f64 * 1024.;
115+
let mem_total = mem_state.mem_total as f64;
116+
let mem_free = mem_state.mem_free as f64;
117117

118118
// TODO: possibly remove this as it is confusing to have `mem_total_used` and `mem_used`
119119
// htop and such only display equivalent of `mem_used`
@@ -126,8 +126,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
126126
min(mem_state.mem_available, mem_state.mem_total)
127127
} else {
128128
mem_state.mem_free
129-
} as f64
130-
* 1024.;
129+
} as f64;
131130

132131
// While zfs_arc_cache can be considered "available" memory,
133132
// it can only free a maximum of (zfs_arc_cache - zfs_arc_min) amount.
@@ -137,14 +136,14 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
137136
.saturating_sub(mem_state.zfs_arc_min) as f64;
138137
let mem_avail = mem_avail + zfs_shrinkable_size;
139138

140-
let pagecache = mem_state.pagecache as f64 * 1024.;
141-
let reclaimable = mem_state.s_reclaimable as f64 * 1024.;
142-
let shmem = mem_state.shmem as f64 * 1024.;
139+
let pagecache = mem_state.pagecache as f64;
140+
let reclaimable = mem_state.s_reclaimable as f64;
141+
let shmem = mem_state.shmem as f64;
143142

144143
// See https://lore.kernel.org/lkml/[email protected]/
145144
let cached = pagecache + reclaimable - shmem + zfs_shrinkable_size;
146145

147-
let buffers = mem_state.buffers as f64 * 1024.;
146+
let buffers = mem_state.buffers as f64;
148147

149148
// same logic as htop
150149
let used_diff = mem_free + buffers + pagecache + reclaimable;
@@ -157,14 +156,14 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
157156
// account for ZFS ARC cache
158157
let mem_used = mem_used - zfs_shrinkable_size;
159158

160-
let swap_total = mem_state.swap_total as f64 * 1024.;
161-
let swap_free = mem_state.swap_free as f64 * 1024.;
162-
let swap_cached = mem_state.swap_cached as f64 * 1024.;
159+
let swap_total = mem_state.swap_total as f64;
160+
let swap_free = mem_state.swap_free as f64;
161+
let swap_cached = mem_state.swap_cached as f64;
163162
let swap_used = swap_total - swap_free - swap_cached;
164163

165164
// Zswap usage
166-
let zswap_compressed = mem_state.zswap_compressed as f64 * 1024.;
167-
let zswap_decompressed = mem_state.zswap_decompressed as f64 * 1024.;
165+
let zswap_compressed = mem_state.zswap_compressed as f64;
166+
let zswap_decompressed = mem_state.zswap_decompressed as f64;
168167

169168
let zswap_comp_ratio = if zswap_compressed != 0.0 {
170169
zswap_decompressed / zswap_compressed
@@ -310,19 +309,23 @@ impl Memstate {
310309
.and_then(|x| u64::from_str(x).ok())
311310
.error("failed to parse /proc/meminfo")?;
312311

312+
// These values are reported as “kB” but are actually “kiB”.
313+
// Convert them into bytes to avoid having to handle this later.
314+
// Source: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/6/html/deployment_guide/s2-proc-meminfo#s2-proc-meminfo
315+
const KIB: u64 = 1024;
313316
match name {
314-
"MemTotal:" => mem_state.mem_total = val,
315-
"MemFree:" => mem_state.mem_free = val,
316-
"MemAvailable:" => mem_state.mem_available = val,
317-
"Buffers:" => mem_state.buffers = val,
318-
"Cached:" => mem_state.pagecache = val,
319-
"SReclaimable:" => mem_state.s_reclaimable = val,
320-
"Shmem:" => mem_state.shmem = val,
321-
"SwapTotal:" => mem_state.swap_total = val,
322-
"SwapFree:" => mem_state.swap_free = val,
323-
"SwapCached:" => mem_state.swap_cached = val,
324-
"Zswap:" => mem_state.zswap_compressed = val,
325-
"Zswapped:" => mem_state.zswap_decompressed = val,
317+
"MemTotal:" => mem_state.mem_total = val * KIB,
318+
"MemFree:" => mem_state.mem_free = val * KIB,
319+
"MemAvailable:" => mem_state.mem_available = val * KIB,
320+
"Buffers:" => mem_state.buffers = val * KIB,
321+
"Cached:" => mem_state.pagecache = val * KIB,
322+
"SReclaimable:" => mem_state.s_reclaimable = val * KIB,
323+
"Shmem:" => mem_state.shmem = val * KIB,
324+
"SwapTotal:" => mem_state.swap_total = val * KIB,
325+
"SwapFree:" => mem_state.swap_free = val * KIB,
326+
"SwapCached:" => mem_state.swap_cached = val * KIB,
327+
"Zswap:" => mem_state.zswap_compressed = val * KIB,
328+
"Zswapped:" => mem_state.zswap_decompressed = val * KIB,
326329
_ => (),
327330
}
328331

0 commit comments

Comments
 (0)