Skip to navigation

nemo_gym.telemetry.memory

View as MarkdownOpen in Claude

Linux process-tree memory sampling for Gym-managed servers.

Module Contents

Classes

NameDescription
HostMemoryCurrent host memory capacity and pressure.
MemoryProfilerPeriodically report host memory and Gym-managed server process trees.
ProcessCpuTimesCPU time one process and the children it waited for have used since it started.
ProcessTreeMemoryOne process tree’s current memory.
ServerMemoryTargetOne configured Gym server and the root process Gym spawned for it.

Functions

NameDescription
_child_pidsThe children of every thread of pid that can be read; a process that has gone has none.
_descendant_pidsReturn a snapshot of a Linux process tree, including its root, each process before its children.
_get_instruments-
_has_endedWhether /proc/<pid>/stat fields describe a process that has exited and has no thread still running.
_read_process_cpuOne process’s identity, CPU times and parent pid from one /proc/<pid>/stat read.
_read_process_memoryReturn (rss, pss) bytes for one process, or None after an exit race.
_stat_fieldsThe fields of /proc/<pid>/stat after the command name; raises FileNotFoundError once the pid is gone.
advance_cpu_baselineThe CPU time a tree used since previous, and the baseline for the next sample.
collect_host_memoryMeasure current host memory using psutil’s cross-platform accounting.
collect_process_tree_cpuCPU time used so far by a root process and each of its live descendants (Linux procfs).
collect_process_tree_memoryMeasure current RSS and PSS for a root process and its descendants.
cpu_time_sinceCPU time the tree used between two samples: each live process’s increase, all of it for a new process.
current_parent_pidThe parent pid the process key names reports now, or None if it cannot be read or its pid was reused.
fold_exited_processesprevious without the processes that have exited, each one’s counted time moved onto an ancestor’s baseline.
process_has_exitedWhether the process key names is confirmed gone: its pid is free, belongs to a later process, or is dead.
process_tree_memory_supportedReturn whether this host exposes Linux procfs memory accounting.
record_host_cpu_capacityRecord the host’s logical and physical CPU counts and the CPUs this process may run on.
record_host_memoryRecord current host memory and its sampled peak through Lens.
record_server_cpuAdd a server process tree’s CPU time since the previous sample to gym.server.cpu.time_total.
record_server_memoryRecord current and sampled-peak process-tree memory through Lens.

Data

ProcessKey

_CLOCK_TICKS

_INSTRUMENTS

logger

API

class nemo_gym.telemetry.memory.HostMemory(
used_bytes: int,
available_bytes: int,
total_bytes: int
)
Dataclass

Current host memory capacity and pressure.

available_bytes
int
total_bytes
int
used_bytes
int
class nemo_gym.telemetry.memory.MemoryProfiler(
interval_seconds: float,
collector: collections.abc.Callable[[int], nemo_gym.telemetry.memory.ProcessTreeMemory] = collect_process_tree_memory,
host_collector: collections.abc.Callable[[], nemo_gym.telemetry.memory.HostMemory] = collect_host_memory,
cpu_collector: collections.abc.Callable[[int], dict[nemo_gym.telemetry.memory.ProcessKey, nemo_gym.telemetry.memory.ProcessCpuTimes]] = collect_process_tree_cpu
)

Periodically report host memory and Gym-managed server process trees.

_cpu_seen
dict[str, dict[ProcessKey, ProcessCpuTimes]] = {}
_host_peaks
dict[str, int] = {}
_peaks
dict[tuple[str, str], int] = {}
_stop
= threading.Event()
_thread
Thread | None = None
nemo_gym.telemetry.memory.MemoryProfiler._run() -> None
nemo_gym.telemetry.memory.MemoryProfiler.sample_once() -> None

Record one host sample and one sample for every configured server.

nemo_gym.telemetry.memory.MemoryProfiler.start() -> None

Start background sampling after recording an initial sample.

nemo_gym.telemetry.memory.MemoryProfiler.stop() -> None

Stop sampling and record final live host and process-tree values.

class nemo_gym.telemetry.memory.ProcessCpuTimes(
user_seconds: float,
system_seconds: float,
parent: nemo_gym.telemetry.memory.ProcessKey | None = None,
parent_pid: int | None = None
)
Dataclass

CPU time one process and the children it waited for have used since it started.

parent_pid is the parent pid the process reported when it was read. parent is that parent’s identity, or None when the parent is not a known process of the tree.

parent
ProcessKey | None = None
parent_pid
int | None = None
system_seconds
float
user_seconds
float
class nemo_gym.telemetry.memory.ProcessTreeMemory(
rss_bytes: int,
pss_bytes: int | None,
process_count: int
)
Dataclass

One process tree’s current memory.

process_count
int
pss_bytes
int | None
rss_bytes
int
class nemo_gym.telemetry.memory.ServerMemoryTarget(
name: str,
server_type: str,
pid: int
)
Dataclass

One configured Gym server and the root process Gym spawned for it.

name
str
pid
int
server_type
str
nemo_gym.telemetry.memory._child_pids(
pid: int
) -> list[int]

The children of every thread of pid that can be read; a process that has gone has none.

nemo_gym.telemetry.memory._descendant_pids(
root_pid: int
) -> list[int]

Return a snapshot of a Linux process tree, including its root, each process before its children.

nemo_gym.telemetry.memory._get_instruments(
meter
) -> dict
nemo_gym.telemetry.memory._has_ended(
fields: list[str]
) -> bool

Whether /proc/<pid>/stat fields describe a process that has exited and has no thread still running.

A child being reaped shows X once its parent’s children times already include it, so it is not counted as well. A zombie main thread whose other threads still run is a running process.

nemo_gym.telemetry.memory._read_process_cpu(
pid: int

One process’s identity, CPU times and parent pid from one /proc/<pid>/stat read.

None if it cannot be read or has exited. The times include cutime/cstime: the kernel adds a child’s own and children times to its parent’s when the parent waits for it, so a child that starts and exits between two samples is still counted.

nemo_gym.telemetry.memory._read_process_memory(
pid: int
) -> tuple[int, int | None] | None

Return (rss, pss) bytes for one process, or None after an exit race.

nemo_gym.telemetry.memory._stat_fields(
pid: int
) -> list[str]

The fields of /proc/<pid>/stat after the command name; raises FileNotFoundError once the pid is gone.

nemo_gym.telemetry.memory.advance_cpu_baseline(
has_exited: collections.abc.Callable[[ProcessKey], bool] = process_has_exited,
parent_pid_of: collections.abc.Callable[[ProcessKey], int | None] = current_parent_pid

The CPU time a tree used since previous, and the baseline for the next sample.

A baseline never goes down, so time moved onto a parent before the parent’s own times show it (the parent was read just before it waited) is held back until they do, and the total never goes down either.

A parent that could not be read in this walk but still runs keeps its baseline, so a child naming it is linked to it: a running process’s reported parent pid always names its live parent. A process missing from this walk that still runs is linked to the parent it reports now, such as a child subreaper that adopted it, or to none when it was reparented outside the tree.

nemo_gym.telemetry.memory.collect_host_memory() -> nemo_gym.telemetry.memory.HostMemory

Measure current host memory using psutil’s cross-platform accounting.

nemo_gym.telemetry.memory.collect_process_tree_cpu(
root_pid: int

CPU time used so far by a root process and each of its live descendants (Linux procfs).

nemo_gym.telemetry.memory.collect_process_tree_memory(
root_pid: int

Measure current RSS and PSS for a root process and its descendants.

CPU time the tree used between two samples: each live process’s increase, all of it for a new process.

nemo_gym.telemetry.memory.current_parent_pid(
) -> int | None

The parent pid the process key names reports now, or None if it cannot be read or its pid was reused.

nemo_gym.telemetry.memory.fold_exited_processes(
has_exited: collections.abc.Callable[[ProcessKey], bool] = process_has_exited

previous without the processes that have exited, each one’s counted time moved onto an ancestor’s baseline.

A process can drop out of one walk (an unreadable children file, a race with a fork); keeping its baseline means it is not counted again from zero when it reappears. Only a process that has exited is forgotten.

When a parent waits for an exited child, the kernel adds the child’s whole total to the parent’s children times. The part already counted is added to the baseline of the child’s nearest ancestor that has not exited (its parent, or further up when the parent exited in the same interval after waiting for it), so it is not counted twice. An ancestor that never received it (one that ignores SIGCHLD, or a child orphaned and reaped outside the tree) then counts less than the tree used. A process whose ancestors all left the tree is not moved.

nemo_gym.telemetry.memory.process_has_exited(
) -> bool

Whether the process key names is confirmed gone: its pid is free, belongs to a later process, or is dead.

An entry that cannot be read for another reason (permissions, I/O) is not proof of an exit, so it counts as running.

nemo_gym.telemetry.memory.process_tree_memory_supported() -> bool

Return whether this host exposes Linux procfs memory accounting.

nemo_gym.telemetry.memory.record_host_cpu_capacity() -> None

Record the host’s logical and physical CPU counts and the CPUs this process may run on.

nemo_gym.telemetry.memory.record_host_memory(
peaks: dict[str, int]
) -> None

Record current host memory and its sampled peak through Lens.

nemo_gym.telemetry.memory.record_server_cpu(
) -> None

Add a server process tree’s CPU time since the previous sample to gym.server.cpu.time_total.

nemo_gym.telemetry.memory.record_server_memory(
peaks: dict[tuple[str, str], int]
) -> None

Record current and sampled-peak process-tree memory through Lens.

nemo_gym.telemetry.memory.ProcessKey = tuple[int, int]
nemo_gym.telemetry.memory._CLOCK_TICKS = os.sysconf('SC_CLK_TCK') if hasattr(os, 'sysconf') else 100
nemo_gym.telemetry.memory._INSTRUMENTS: WeakKeyDictionary = weakref.WeakKeyDictionary()
nemo_gym.telemetry.memory.logger = logging.getLogger(__name__)