#[cfg(any(target_os = "linux", target_os = "android"))]
use std::path::Path;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct MemoryCapacity {
pub physical_bytes: Option<u64>,
pub available_bytes: Option<u64>,
pub limit_bytes: Option<u64>,
pub limit_source: &'static str,
pub used_in_limit_bytes: Option<u64>,
pub charged_in_limit_bytes: Option<u64>,
}
impl MemoryCapacity {
#[must_use]
pub fn effective_limit(&self) -> Option<u64> {
match (self.limit_bytes, self.physical_bytes) {
(Some(limit), Some(physical)) => Some(limit.min(physical)),
(limit, physical) => limit.or(physical),
}
}
#[must_use]
pub fn headroom(&self) -> Option<u64> {
let in_limit = self
.limit_bytes
.zip(self.used_in_limit_bytes)
.map(|(limit, used)| limit.saturating_sub(used));
match (self.available_bytes, in_limit) {
(Some(available), Some(in_limit)) => Some(available.min(in_limit)),
(available, in_limit) => available.or(in_limit),
}
}
}
#[must_use]
pub fn memory_capacity() -> MemoryCapacity {
#[cfg(any(target_os = "linux", target_os = "android"))]
{
memory_capacity_at(Path::new("/"))
}
#[cfg(any(target_vendor = "apple", windows))]
{
crate::platform::memory_capacity()
}
#[cfg(not(any(
target_os = "linux",
target_os = "android",
target_vendor = "apple",
windows
)))]
{
MemoryCapacity {
limit_source: "physical",
..MemoryCapacity::default()
}
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[must_use]
pub fn memory_capacity_at(root: &Path) -> MemoryCapacity {
let meminfo = std::fs::read_to_string(root.join("proc/meminfo")).unwrap_or_default();
let kib = |key: &str| {
meminfo
.lines()
.find_map(|line| line.strip_prefix(key))
.and_then(|rest| rest.trim().strip_suffix("kB"))
.and_then(|value| value.trim().parse::<u64>().ok())
.map(|value| value.saturating_mul(1_024))
};
let physical = kib("MemTotal:");
let available = kib("MemAvailable:");
match cgroup_memory_at(root) {
Some(cgroup) if physical.is_none_or(|physical| cgroup.limit_bytes < physical) => {
MemoryCapacity {
physical_bytes: physical,
available_bytes: available,
limit_bytes: Some(cgroup.limit_bytes),
limit_source: cgroup.source,
used_in_limit_bytes: cgroup.working_set_bytes,
charged_in_limit_bytes: cgroup.charged_bytes,
}
}
_ => MemoryCapacity {
physical_bytes: physical,
available_bytes: available,
limit_bytes: physical,
limit_source: "physical",
used_in_limit_bytes: None,
charged_in_limit_bytes: None,
},
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CgroupMemory {
pub limit_bytes: u64,
pub charged_bytes: Option<u64>,
pub working_set_bytes: Option<u64>,
pub source: &'static str,
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[must_use]
pub fn cgroup_memory() -> Option<CgroupMemory> {
cgroup_memory_at(Path::new("/"))
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[must_use]
pub fn cgroup_memory_at(root: &Path) -> Option<CgroupMemory> {
let level = Level::lowest(root)?;
level.read(0)
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[derive(Debug)]
pub struct CgroupMemoryWatch {
root: std::path::PathBuf,
level: Option<Level>,
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl CgroupMemoryWatch {
pub const STAT_FROM_PERCENT: u64 = 90;
#[must_use]
pub fn new() -> Self {
Self::at(Path::new("/"))
}
#[must_use]
pub fn at(root: &Path) -> Self {
Self {
root: root.to_owned(),
level: None,
}
}
pub fn read(&mut self) -> Option<CgroupMemory> {
if self.level.is_none() {
self.level = Level::lowest(&self.root);
}
let memory = self
.level
.as_ref()?
.read(Self::STAT_FROM_PERCENT)
.filter(|memory| memory.charged_bytes.is_some());
if memory.is_none() {
self.level = None;
}
memory
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Default for CgroupMemoryWatch {
fn default() -> Self {
Self::new()
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[derive(Clone, Debug)]
struct Level {
directory: std::path::PathBuf,
files: [&'static str; 2],
cache: [&'static str; 2],
source: &'static str,
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Level {
const V2: ([&'static str; 2], [&'static str; 2], &'static str) = (
["memory.max", "memory.current"],
["inactive_file", "active_file"],
"cgroup-v2",
);
const V1: ([&'static str; 2], [&'static str; 2], &'static str) = (
["memory.limit_in_bytes", "memory.usage_in_bytes"],
["total_inactive_file", "total_active_file"],
"cgroup-v1",
);
fn lowest(root: &Path) -> Option<Self> {
let membership = std::fs::read_to_string(root.join("proc/self/cgroup")).ok()?;
if let Some(relative) = membership.lines().find_map(|line| line.strip_prefix("0::"))
&& let Some(level) = Self::lowest_under(&root.join("sys/fs/cgroup"), relative, Self::V2)
{
return Some(level);
}
let relative = membership.lines().find_map(|line| {
let mut fields = line.splitn(3, ':');
let _ = fields.next()?;
let controllers = fields.next()?;
controllers
.split(',')
.any(|controller| controller == "memory")
.then(|| fields.next())
.flatten()
})?;
Self::lowest_under(&root.join("sys/fs/cgroup/memory"), relative, Self::V1)
}
fn lowest_under(
mount: &Path,
relative: &str,
(files, cache, source): ([&'static str; 2], [&'static str; 2], &'static str),
) -> Option<Self> {
let mut best: Option<(u64, std::path::PathBuf)> = None;
let mut path = Some(Path::new(relative.trim_start_matches('/')));
while let Some(level) = path {
let directory = mount.join(level);
if let Some(limit) = read_limit(&directory.join(files[0]))
&& best.as_ref().is_none_or(|(known, _)| limit < *known)
{
best = Some((limit, directory));
}
path = level.parent();
}
best.map(|(_, directory)| Self {
directory,
files,
cache,
source,
})
}
fn read(&self, stat_from_percent: u64) -> Option<CgroupMemory> {
let limit = read_limit(&self.directory.join(self.files[0]))?;
let charged = read_number(&self.directory.join(self.files[1]));
let cached = charged
.filter(|charged| {
charged.saturating_mul(100) >= limit.saturating_mul(stat_from_percent)
})
.and_then(|_| self.cached());
Some(CgroupMemory {
limit_bytes: limit,
charged_bytes: charged,
working_set_bytes: charged
.map(|charged| cached.map_or(charged, |cached| charged.saturating_sub(cached))),
source: self.source,
})
}
fn cached(&self) -> Option<u64> {
let stat = std::fs::read_to_string(self.directory.join("memory.stat")).ok()?;
self.cache.iter().try_fold(0_u64, |sum, key| {
let value = stat.lines().find_map(|line| {
line.strip_prefix(key)?
.strip_prefix(' ')?
.trim()
.parse::<u64>()
.ok()
})?;
Some(sum.saturating_add(value))
})
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn read_number(path: &Path) -> Option<u64> {
std::fs::read_to_string(path)
.ok()
.and_then(|text| text.trim().parse::<u64>().ok())
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn read_limit(path: &Path) -> Option<u64> {
read_number(path).filter(|limit| *limit < (1_u64 << 60))
}
#[cfg(test)]
#[path = "memory_tests.rs"]
mod tests;