Skip to content

Commit

Permalink
Merge pull request #1060 from GuillaumeGomez/0.29-update
Browse files Browse the repository at this point in the history
Backport changes for 0.29.10
  • Loading branch information
GuillaumeGomez authored Sep 6, 2023
2 parents 1f872fa + aa620d8 commit e2a88f4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 45 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.29.10

* Linux: Correctly handle max memory value for cgroups.

# 0.29.9

* Linux: Fix memory usage retrieval for cgroups.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sysinfo"
version = "0.29.9"
version = "0.29.10"
authors = ["Guillaume Gomez <[email protected]>"]
description = "Library to get system information such as processes, CPUs, disks, components and networks"
repository = "https://github.com/GuillaumeGomez/sysinfo"
Expand Down
87 changes: 44 additions & 43 deletions src/linux/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
};

use libc::{self, c_char, c_int, sysconf, _SC_CLK_TCK, _SC_HOST_NAME_MAX, _SC_PAGESIZE};
use std::cmp::min;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
Expand Down Expand Up @@ -249,8 +250,8 @@ impl SystemExt for System {
}

fn refresh_memory(&mut self) {
let mut mem_available_found = false;
read_table("/proc/meminfo", ':', |key, value_kib| {
let mut mem_available_found = false;
let field = match key {
"MemTotal" => &mut self.mem_total,
"MemFree" => &mut self.mem_free,
Expand All @@ -268,52 +269,52 @@ impl SystemExt for System {
};
// /proc/meminfo reports KiB, though it says "kB". Convert it.
*field = value_kib.saturating_mul(1_024);
});

// Linux < 3.14 may not have MemAvailable in /proc/meminfo
// So it should fallback to the old way of estimating available memory
// https://github.com/KittyKatt/screenFetch/issues/386#issuecomment-249312716
if !mem_available_found {
self.mem_available = self
.mem_free
.saturating_add(self.mem_buffers)
.saturating_add(self.mem_page_cache)
.saturating_add(self.mem_slab_reclaimable)
.saturating_sub(self.mem_shmem);
}
// Linux < 3.14 may not have MemAvailable in /proc/meminfo
// So it should fallback to the old way of estimating available memory
// https://github.com/KittyKatt/screenFetch/issues/386#issuecomment-249312716
if !mem_available_found {
self.mem_available = self
.mem_free
.saturating_add(self.mem_buffers)
.saturating_add(self.mem_page_cache)
.saturating_add(self.mem_slab_reclaimable)
.saturating_sub(self.mem_shmem);
}

if let (Some(mem_cur), Some(mem_max)) = (
read_u64("/sys/fs/cgroup/memory.current"),
read_u64("/sys/fs/cgroup/memory.max"),
) {
// cgroups v2
self.mem_total = mem_max;
self.mem_free = mem_max.saturating_sub(mem_cur);
self.mem_available = self.mem_free;

if let Some(swap_cur) = read_u64("/sys/fs/cgroup/memory.swap.current") {
self.swap_free = self.swap_total.saturating_sub(swap_cur);
}
if let (Some(mem_cur), Some(mem_max)) = (
read_u64("/sys/fs/cgroup/memory.current"),
read_u64("/sys/fs/cgroup/memory.max"),
) {
// cgroups v2
self.mem_total = min(mem_max, self.mem_total);
self.mem_free = self.mem_total.saturating_sub(mem_cur);
self.mem_available = self.mem_free;

read_table("/sys/fs/cgroup/memory.stat", ' ', |key, value| {
let field = match key {
"slab_reclaimable" => &mut self.mem_slab_reclaimable,
"file" => &mut self.mem_page_cache,
"shmem" => &mut self.mem_shmem,
_ => return,
};
*field = value;
self.mem_free = self.mem_free.saturating_sub(value);
});
} else if let (Some(mem_cur), Some(mem_max)) = (
// cgroups v1
read_u64("/sys/fs/cgroup/memory/memory.usage_in_bytes"),
read_u64("/sys/fs/cgroup/memory/memory.limit_in_bytes"),
) {
self.mem_total = mem_max;
self.mem_free = mem_max.saturating_sub(mem_cur);
self.mem_available = self.mem_free;
if let Some(swap_cur) = read_u64("/sys/fs/cgroup/memory.swap.current") {
self.swap_free = self.swap_total.saturating_sub(swap_cur);
}
});

read_table("/sys/fs/cgroup/memory.stat", ' ', |key, value| {
let field = match key {
"slab_reclaimable" => &mut self.mem_slab_reclaimable,
"file" => &mut self.mem_page_cache,
"shmem" => &mut self.mem_shmem,
_ => return,
};
*field = value;
self.mem_free = self.mem_free.saturating_sub(value);
});
} else if let (Some(mem_cur), Some(mem_max)) = (
// cgroups v1
read_u64("/sys/fs/cgroup/memory/memory.usage_in_bytes"),
read_u64("/sys/fs/cgroup/memory/memory.limit_in_bytes"),
) {
self.mem_total = min(mem_max, self.mem_total);
self.mem_free = self.mem_total.saturating_sub(mem_cur);
self.mem_available = self.mem_free;
}
}

fn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind) {
Expand Down

0 comments on commit e2a88f4

Please sign in to comment.