Skip to main content

memory

Memory and machine-capacity telemetry.

read_memory takes one process and system memory reading, MemorySampler tracks the extremes of those readings over a window of work on a background thread, read_machine_capacity reports the machine's size, and read_process_resources sizes one process: its memory, threads and open files.

Every function here and the sampler's thread never raise: a reading that cannot be taken is None.

Module​

Functions​

read_machine_capacity​

def read_machine_capacity(cache_dir: Path) ‑> MachineCapacity:

Read the machine's memory, cores and free disk. Never raises.

Arguments

  • cache_dir: The cache directory whose volume's free space is reported.

Returns The capacity; a field that could not be read is None.

read_memory​

def read_memory() ‑> MemoryReading:

Read process and system memory. Never raises.

Returns The reading; a field that could not be read is None.

read_process_resources​

def read_process_resources(    pid: int | None = None,) ‑> ProcessResources:

Read a process's memory, thread and open-file counts. Never raises.

Arguments

  • pid: The process to read; None reads this one.

Returns The reading; a field that could not be read is None.

Classes​

MachineCapacity​

class MachineCapacity(    total_memory_bytes: int | None = None,    available_memory_bytes: int | None = None,    physical_cores: int | None = None,    logical_cores: int | None = None,    free_disk_bytes_cache_dir: int | None = None,    free_disk_bytes_temp_dir: int | None = None,):

The size of the machine, in bytes and core counts.

Attributes

  • total_memory_bytes: Total physical memory.
  • available_memory_bytes: Memory available when read.
  • physical_cores: Physical CPU cores.
  • logical_cores: Logical CPU cores.
  • free_disk_bytes_cache_dir: Free space on the cache directory's volume.
  • free_disk_bytes_temp_dir: Free space on the temp directory's volume.

Variables​

  • available_memory_bytes : int | None
  • free_disk_bytes_cache_dir : int | None
  • free_disk_bytes_temp_dir : int | None
  • logical_cores : int | None
  • physical_cores : int | None
  • total_memory_bytes : int | None

MemoryReading​

class MemoryReading(    rss_bytes: int | None = None,    system_available_bytes: int | None = None,    system_total_bytes: int | None = None,):

One reading of process and system memory, in bytes.

Attributes

  • rss_bytes: Resident set size of this process.
  • system_available_bytes: Memory the system can give out without swapping.
  • system_total_bytes: Total physical memory.

Variables​

  • rss_bytes : int | None
  • system_available_bytes : int | None
  • system_total_bytes : int | None

MemorySampler​

class MemorySampler(    *, interval: float = 1.0, read: Callable[[], MemoryReading] = <function read_memory>,):

Tracks memory extremes over a window of work.

Samples when started, every interval seconds on a daemon thread, and when stopped. Use as a context manager (the summary is then read from stop), or call start and stop directly; both are idempotent. Never raises.

Arguments

  • interval: Seconds between samples.
  • read: Where readings come from.

Methods​


start​

def start(self) ‑> None:

Take the first sample and begin sampling on a background thread.

stop​

def stop(self) ‑> StepMemorySummary:

Take a last sample, stop the thread and summarise.

Returns The summary; fields are None where no reading succeeded.

ProcessResources​

class ProcessResources(    rss_bytes: int | None = None, threads: int | None = None, fds: int | None = None,):

One reading of what a process holds: its memory, threads and open files.

A pod that stops for good leaves no precursor in its own events, so this is what sizes it from outside as well as from within: the compute reads its pod child with it, and the pod itself reads the process it runs in.

Attributes

  • rss_bytes: Resident set size of the process.
  • threads: Threads running in it.
  • fds: Files and sockets it holds open. On Windows this counts handles, which is a different thing but answers the same question: how many the process has open.

Variables​

  • fds : int | None
  • rss_bytes : int | None
  • threads : int | None

StepMemorySummary​

class StepMemorySummary(    rss_start_bytes: int | None = None,    rss_peak_bytes: int | None = None,    system_available_min_bytes: int | None = None,):

Memory over a step's window.

Attributes

  • rss_start_bytes: Process RSS when the window opened.
  • rss_peak_bytes: Highest process RSS sampled.
  • system_available_min_bytes: Lowest system-available memory sampled.

Variables​

  • rss_peak_bytes : int | None
  • rss_start_bytes : int | None
  • system_available_min_bytes : int | None