Documentation
¶
Overview ¶
Package memctl provides periodic memory sampling with a caller-defined callback. The controller reads system, process, and cgroup memory metrics and calls a function with the results. Policy (what to do about memory pressure) lives in the callback — the controller is intentionally dumb.
On Linux, reads /proc/meminfo (MemAvailable), /proc/self/status (VmRSS), and cgroup v2 memory.current/memory.max. On other platforms, only Go runtime metrics are available.
The callback must be fast and non-blocking. Callbacks are invoked serially — no overlapping invocations. If a callback exceeds the interval, subsequent ticks are coalesced.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Watch ¶
Watch blocks, sampling memory every interval and calling fn with the results until ctx is canceled. Returns nil on clean cancellation. Returns an error if options are invalid.
Callbacks are invoked serially — no overlapping invocations. If a callback exceeds the interval, subsequent ticks are coalesced.
Types ¶
type MemInfo ¶
type MemInfo struct {
At time.Time // when this sample was taken
SystemAvailable uint64 // /proc/meminfo MemAvailable (bytes)
SystemAvailableOK bool
ProcessRSS uint64 // /proc/self/status VmRSS (bytes)
ProcessRSSOK bool
GoRuntimeTotal uint64 // runtime/metrics /memory/classes/total:bytes
GoRuntimeTotalOK bool
CgroupCurrent uint64 // cgroup v2 memory.current (bytes)
CgroupLimit uint64 // cgroup v2 memory.max (bytes; 0 = unlimited)
CgroupOK bool // true when cgroup v2 is detected and readable
}
MemInfo holds process and system memory information. Each metric has an OK flag — false means unavailable on this platform or a read error occurred, not "value is zero."
type Options ¶
type Options struct {
// Interval between memory samples. Must be > 0.
Interval time.Duration
// Immediate, when true, takes a sample immediately on entry
// before waiting for the first tick. Catches startup spikes.
Immediate bool
// OnPanic is called if the callback panics. If nil, the panic
// is re-raised (default: don't mask bugs). If non-nil, Watch
// logs the panic via OnPanic and continues sampling.
OnPanic func(any)
}
Options configures Watch.