use core::time::Duration;
use std::time::Instant;
use crate::{Bytes, Error, Percent, Pressure, Rate, Result};
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct Memory {
pub total: Bytes,
pub used: Bytes,
pub free: Bytes,
pub host_total: Bytes,
pub host_free: Bytes,
pub unused: Option<Bytes>,
pub cached: Option<Bytes>,
pub limit: Option<MemoryLimit>,
pub swap: Swap,
pub source: MemorySource,
}
impl Memory {
#[must_use]
pub fn used_percent(&self) -> Percent {
self.used.percent_of(self.total)
}
#[must_use]
pub fn free_percent(&self) -> Percent {
self.free.percent_of(self.total)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum MemorySource {
Host,
Cgroup1,
Cgroup2,
JobObject,
Lxcfs,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct MemoryLimit {
pub hard: Bytes,
pub soft: Option<Bytes>,
pub swap: Option<Bytes>,
pub source: LimitSource,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum LimitSource {
Cgroup1,
Cgroup2,
JobObject,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub struct Swap {
pub total: Bytes,
pub used: Bytes,
pub free: Bytes,
}
impl Swap {
pub(crate) fn new(total: u64, free: u64) -> Swap {
let free = free.min(total);
Swap {
total: Bytes::new(total),
used: Bytes::new(total - free),
free: Bytes::new(free),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct MemWant {
pub(crate) limit: bool,
pub(crate) usage: bool,
pub(crate) swap: bool,
}
impl MemWant {
pub(crate) const HOST: MemWant = MemWant {
limit: false,
usage: false,
swap: false,
};
pub(crate) const TOTAL: MemWant = MemWant {
limit: true,
usage: false,
swap: false,
};
pub(crate) const USED: MemWant = MemWant {
limit: true,
usage: true,
swap: false,
};
pub(crate) const SWAP: MemWant = MemWant {
limit: false,
usage: false,
swap: true,
};
pub(crate) const ALL: MemWant = MemWant {
limit: true,
usage: true,
swap: true,
};
#[cfg(test)]
pub(crate) const ALL_FOR_TESTS: MemWant = MemWant::ALL;
}
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MemRaw {
pub(crate) host_total: u64,
pub(crate) host_available: u64,
pub(crate) host_unused: Option<u64>,
pub(crate) cached: Option<u64>,
pub(crate) limit: Option<MemoryLimit>,
pub(crate) limited_used: Option<u64>,
pub(crate) swap: Option<(u64, u64)>,
pub(crate) source: Option<MemorySource>,
}
pub(crate) fn from_raw(raw: MemRaw) -> Memory {
let host_total = raw.host_total;
let host_free = raw.host_available.min(host_total);
let limit = raw
.limit
.filter(|l| l.hard.raw() > 0 && l.hard.raw() < host_total);
let (total, used) = match (limit, raw.limited_used) {
(Some(l), Some(used)) => (l.hard.raw(), used.min(l.hard.raw())),
(Some(l), None) => (l.hard.raw(), (host_total - host_free).min(l.hard.raw())),
(None, _) => (host_total, host_total - host_free),
};
let source = match (limit, raw.source) {
(Some(l), _) => match l.source {
LimitSource::Cgroup1 => MemorySource::Cgroup1,
LimitSource::Cgroup2 => MemorySource::Cgroup2,
LimitSource::JobObject => MemorySource::JobObject,
},
(None, Some(MemorySource::Lxcfs)) => MemorySource::Lxcfs,
(None, _) => MemorySource::Host,
};
Memory {
total: Bytes::new(total),
used: Bytes::new(used),
free: Bytes::new(total - used),
host_total: Bytes::new(host_total),
host_free: Bytes::new(host_free),
unused: raw.host_unused.map(|u| Bytes::new(u.min(host_total))),
cached: raw.cached.map(|c| Bytes::new(c.min(host_total))),
limit,
swap: raw
.swap
.map_or_else(Swap::default, |(t, f)| Swap::new(t, f)),
source,
}
}
fn read(want: MemWant) -> Result<Memory> {
crate::sys::memory(want).map(from_raw)
}
pub fn total() -> Result<Bytes> {
read(MemWant::TOTAL).map(|m| m.total)
}
pub fn used() -> Result<Bytes> {
read(MemWant::USED).map(|m| m.used)
}
pub fn free() -> Result<Bytes> {
read(MemWant::USED).map(|m| m.free)
}
pub fn used_percent() -> Result<Percent> {
read(MemWant::USED).map(|m| m.used_percent())
}
pub fn free_percent() -> Result<Percent> {
read(MemWant::USED).map(|m| m.free_percent())
}
pub fn snapshot() -> Result<Memory> {
read(MemWant::ALL)
}
pub fn host_total() -> Result<Bytes> {
read(MemWant::HOST).map(|m| m.host_total)
}
pub fn host_free() -> Result<Bytes> {
read(MemWant::HOST).map(|m| m.host_free)
}
pub fn limit() -> Result<Option<MemoryLimit>> {
read(MemWant::TOTAL).map(|m| m.limit)
}
pub fn swap() -> Result<Swap> {
read(MemWant::SWAP).map(|m| m.swap)
}
#[must_use]
pub fn page_size() -> usize {
crate::sys::page_size()
}
pub fn pressure() -> Result<Option<Pressure>> {
crate::sys::mem_pressure()
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub struct MemoryEvents {
pub oom_kills: u64,
pub oom: u64,
pub max: u64,
pub high: u64,
pub scope: EventScope,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
#[non_exhaustive]
pub enum EventScope {
Cgroup,
#[default]
Host,
}
pub fn events() -> Result<Option<MemoryEvents>> {
crate::sys::mem_events()
}
#[derive(Clone, Copy, PartialEq, Debug)]
#[non_exhaustive]
pub struct Activity {
pub swap_in: Rate,
pub swap_out: Rate,
pub major_faults_per_sec: Option<f64>,
pub interval: Duration,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub(crate) struct PagingRaw {
pub(crate) swap_in: u64,
pub(crate) swap_out: u64,
pub(crate) major_faults: Option<u64>,
}
const ACTIVITY_WINDOW: Duration = Duration::from_secs(1);
pub fn activity() -> Result<Activity> {
let mut sampler = Sampler::new()?;
std::thread::sleep(ACTIVITY_WINDOW);
sampler.sample()
}
#[derive(Debug)]
pub struct Sampler {
previous: PagingRaw,
at: Instant,
}
impl Sampler {
pub fn new() -> Result<Sampler> {
Ok(Sampler {
previous: crate::sys::paging()?,
at: Instant::now(),
})
}
pub fn sample(&mut self) -> Result<Activity> {
let now = crate::sys::paging()?;
let at = Instant::now();
let interval = at.duration_since(self.at);
if interval.is_zero() {
return Err(Error::TooSoon {
elapsed: interval,
minimum: Duration::from_millis(1),
});
}
let delta = |new: u64, old: u64| new.saturating_sub(old);
let activity = Activity {
swap_in: Rate::from_delta(delta(now.swap_in, self.previous.swap_in), interval),
swap_out: Rate::from_delta(delta(now.swap_out, self.previous.swap_out), interval),
major_faults_per_sec: match (now.major_faults, self.previous.major_faults) {
(Some(n), Some(o)) => Some(delta(n, o) as f64 / interval.as_secs_f64()),
_ => None,
},
interval,
};
self.previous = now;
self.at = at;
Ok(activity)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct HugePages {
pub page_size: Bytes,
pub total: u64,
pub free: u64,
pub reserved: u64,
pub transparent: Option<TransparentHugePages>,
pub large_page_minimum: Option<Bytes>,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum TransparentHugePages {
Always,
Madvise,
Never,
}
pub fn huge_pages() -> Result<Option<HugePages>> {
crate::sys::huge_pages()
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MemoryModule {
pub size: Bytes,
pub kind: MemoryKind,
pub form: MemoryForm,
pub speed_mts: Option<u32>,
pub configured_speed_mts: Option<u32>,
pub locator: Option<String>,
pub manufacturer: Option<String>,
pub part_number: Option<String>,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum MemoryKind {
Ddr3,
Ddr4,
Ddr5,
Lpddr4,
Lpddr5,
Lpddr5x,
Hbm,
Other,
Unknown,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum MemoryForm {
Dimm,
SoDimm,
Soldered,
Other,
Unknown,
}
pub fn modules() -> Result<Vec<MemoryModule>> {
crate::sys::memory_modules()
}
#[cfg_attr(target_vendor = "apple", allow(dead_code))]
pub(crate) fn module_from(d: crate::parse::smbios::MemoryDevice) -> MemoryModule {
MemoryModule {
size: Bytes::new(d.bytes),
kind: match d.kind {
0x18 => MemoryKind::Ddr3,
0x1A => MemoryKind::Ddr4,
0x22 => MemoryKind::Ddr5,
0x1E => MemoryKind::Lpddr4,
0x23 => MemoryKind::Lpddr5,
0x20 | 0x21 | 0x24 => MemoryKind::Hbm,
0 | 2 => MemoryKind::Unknown,
_ => MemoryKind::Other,
},
form: match d.form {
0x09 | 0x0F => MemoryForm::Dimm,
0x0D => MemoryForm::SoDimm,
0x0B => MemoryForm::Soldered,
0 | 2 => MemoryForm::Unknown,
_ => MemoryForm::Other,
},
speed_mts: d.speed_mts,
configured_speed_mts: d.configured_speed_mts,
locator: d.locator,
manufacturer: d.manufacturer,
part_number: d.part_number,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn limit(hard: u64, source: LimitSource) -> MemoryLimit {
MemoryLimit {
hard: Bytes::new(hard),
soft: None,
swap: None,
source,
}
}
const GIB: u64 = 1 << 30;
#[test]
fn test_mem_host_values_without_a_limit() {
let m = from_raw(MemRaw {
host_total: 16 * GIB,
host_available: 10 * GIB,
host_unused: Some(2 * GIB),
..MemRaw::default()
});
assert_eq!(
(m.total.raw(), m.used.raw(), m.free.raw()),
(16 * GIB, 6 * GIB, 10 * GIB)
);
assert_eq!(m.source, MemorySource::Host);
assert_eq!(m.limit, None);
assert_eq!(m.swap, Swap::default());
assert_eq!(m.used_percent().to_string(), "37.5%");
}
#[test]
fn test_mem_limit_below_host_wins_with_its_own_usage() {
let m = from_raw(MemRaw {
host_total: 64 * GIB,
host_available: 60 * GIB,
limit: Some(limit(256 << 20, LimitSource::Cgroup2)),
limited_used: Some(100 << 20),
..MemRaw::default()
});
assert_eq!((m.total.raw(), m.used.raw()), (256 << 20, 100 << 20));
assert_eq!(m.host_total.raw(), 64 * GIB);
assert_eq!(m.source, MemorySource::Cgroup2);
}
#[test]
fn test_mem_limit_at_or_above_host_is_no_limit() {
let m = from_raw(MemRaw {
host_total: 64 * GIB,
host_available: 32 * GIB,
limit: Some(limit(1 << 40, LimitSource::Cgroup1)),
limited_used: Some(GIB),
..MemRaw::default()
});
assert_eq!((m.total.raw(), m.used.raw()), (64 * GIB, 32 * GIB));
assert_eq!((m.limit, m.source), (None, MemorySource::Host));
}
#[test]
fn test_mem_invariants_hold_for_inconsistent_readings() {
let m = from_raw(MemRaw {
host_total: 8 * GIB,
host_available: 9 * GIB,
cached: Some(20 * GIB),
limit: Some(limit(GIB, LimitSource::JobObject)),
limited_used: Some(2 * GIB),
swap: Some((GIB, 2 * GIB)),
..MemRaw::default()
});
assert_eq!(m.used.saturating_add(m.free), m.total);
assert!(m.used <= m.total && m.host_free <= m.host_total);
assert_eq!(m.cached, Some(Bytes::new(8 * GIB)));
assert_eq!(m.swap.used.saturating_add(m.swap.free), m.swap.total);
assert_eq!(m.source, MemorySource::JobObject);
}
#[test]
fn test_mem_lxcfs_source_kept_without_a_limit() {
let m = from_raw(MemRaw {
host_total: GIB,
host_available: GIB / 2,
source: Some(MemorySource::Lxcfs),
..MemRaw::default()
});
assert_eq!(m.source, MemorySource::Lxcfs);
}
#[test]
fn test_mem_live_readings_are_consistent() {
let m = snapshot().unwrap();
assert!(m.total.raw() > 0 && m.used <= m.total);
assert_eq!(m.used.saturating_add(m.free), m.total);
assert!(m.total <= m.host_total);
assert!(page_size().is_power_of_two() && page_size() >= 4096);
}
}