summaryrefslogtreecommitdiffstats
path: root/src/app/data_harvester/memory/heim.rs
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2021-07-15 18:28:41 -0400
committerGitHub <noreply@github.com>2021-07-15 18:28:41 -0400
commit741054e84aa93d1343e19bbe754a01e6ca619c65 (patch)
treeb5a6aac878a1d6e41174fbaff98e9c100b06fd85 /src/app/data_harvester/memory/heim.rs
parentb0199d4d1cbcb330e9080d4b94c94a51068fe269 (diff)
bug: fix inaccuracy in memory usage/total on macOS and Linux (#545)
Fixes the accuracy of the memory widget for Linux and macOS, and uses binary prefixes instead to be more accurate. Regarding the first part, it turns out that the way I was calculating memory usage was *slightly* incorrect for a few reasons: - Regarding macOS, it seems like the way I was determining usage (`usage = total - available`) is not the most accurate. The better way of doing this is apparently `usage = wire + active`, where `wire` is memory always marked to stay in RAM, and `active` is memory currently in RAM. This seems to be much closer to other applications now. - Regarding Linux, this was somewhat due to two issues - one was that I should have used heim's own built-in function call to get how much memory was *used*, and the other is that when heim reads info from `meminfo`, it reads it in *kilobytes* - however, the values are actually in *kibibytes*. As such, to get the value in kibibytes, you want to actually take it in kilobytes. While I've filed an issue for the library, for now, I'll just manually bandaid over this. See https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/deployment_guide/s2-proc-meminfo for more info. Both changes take more advantage of platform-specific methods, and as such, the change unfortunately adds some ugly platform-specific code blocks. Side note, Windows Task Manager apparently (?) uses binary prefixes for the values behind the scenes, but displays decimal prefixes. As such, now that we've switched to binary prefixes, it'll "seem" like the two aren't matching anymore since the units won't match, despite the values matching.
Diffstat (limited to 'src/app/data_harvester/memory/heim.rs')
-rw-r--r--src/app/data_harvester/memory/heim.rs44
1 files changed, 38 insertions, 6 deletions
diff --git a/src/app/data_harvester/memory/heim.rs b/src/app/data_harvester/memory/heim.rs
index 5319b1b3..6e455510 100644
--- a/src/app/data_harvester/memory/heim.rs
+++ b/src/app/data_harvester/memory/heim.rs
@@ -33,14 +33,46 @@ pub async fn get_mem_data(
pub async fn get_ram_data() -> crate::utils::error::Result<Option<MemHarvest>> {
let memory = heim::memory::memory().await?;
- let mem_total_in_kb = memory.total().get::<heim::units::information::kibibyte>();
+ let (mem_total_in_kib, mem_used_in_kib) = {
+ #[cfg(target_os = "linux")]
+ {
+ // For Linux, the "kilobyte" value in the .total call is actually kibibytes - see
+ // https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/deployment_guide/s2-proc-meminfo
+ //
+ // Heim parses this as kilobytes (https://github.com/heim-rs/heim/blob/master/heim-memory/src/sys/linux/memory.rs#L82)
+ // even though it probably shouldn't...
+
+ use heim::memory::os::linux::MemoryExt;
+ (
+ memory.total().get::<heim::units::information::kilobyte>(),
+ memory.used().get::<heim::units::information::kibibyte>(),
+ )
+ }
+ #[cfg(target_os = "macos")]
+ {
+ use heim::memory::os::macos::MemoryExt;
+ (
+ memory.total().get::<heim::units::information::kibibyte>(),
+ memory.active().get::<heim::units::information::kibibyte>()
+ + memory.wire().get::<heim::units::information::kibibyte>(),
+ )
+ }
+ #[cfg(target_os = "windows")]
+ {
+ let mem_total_in_kib = memory.total().get::<heim::units::information::kibibyte>();
+ (
+ mem_total_in_kib,
+ mem_total_in_kib
+ - memory
+ .available()
+ .get::<heim::units::information::kibibyte>(),
+ )
+ }
+ };
Ok(Some(MemHarvest {
- mem_total_in_kib: mem_total_in_kb,
- mem_used_in_kib: mem_total_in_kb
- - memory
- .available()
- .get::<heim::units::information::kibibyte>(),
+ mem_total_in_kib,
+ mem_used_in_kib,
}))
}