Documentation
¶
Overview ¶
Package memusage reads and logs Linux process and container memory accounting. Log and ReadContainer read accounting on each call and keep no shared state. Heartbeat samples in the background and logs while the container is at or over half its memory limit. All functions are safe for concurrent use.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Heartbeat ¶ added in v1.58.1
Heartbeat logs the container's memory use while it is at or over half its limit, until ctx is done. Cloud Run SIGKILLs an instance that reaches its limit, and when the memory is tmpfs files or child processes it logs no out-of-memory event, so these lines are the record of what filled memory. Each line splits usage into anonymous memory (Go heap and child processes), tmpfs files, and page cache. Only the first call in a process samples; later calls return at once, so every entry point can start it.
func Log ¶
Log records Linux kernel memory high-water marks with the context logger. Missing or unsupported accounting is logged as null with an error, not zero. The process RSS excludes children and unmapped tmpfs; the cgroup peak includes all memory charged to the container, including child processes and scratch files. These are lifetime peaks, not per-call measurements. Log is safe for concurrent use.
Types ¶
type Container ¶ added in v1.56.0
type Container struct {
// Current is the memory charged to the container now: cgroup v2
// memory.current or v1 memory.usage_in_bytes.
Current *uint64
CurrentErr error
// Limit is the memory limit: cgroup v2 memory.max or v1
// memory.limit_in_bytes. A v2 limit of "max" is unknown.
Limit *uint64
LimitErr error
// Peak is the lifetime high-water mark: cgroup v2 memory.peak or v1
// memory.max_usage_in_bytes. It only rises, so it shows how close the
// container came to its limit even between samples.
Peak *uint64
PeakErr error
// Anon, Shmem, and File split Current by kind, from memory.stat.
// Anon is anonymous memory of every process in the container, the Go heap
// and child processes alike (v2 anon, v1 total_rss). Shmem is files in
// tmpfs mounts such as Cloud Run's in-memory /tmp (v2 shmem, v1
// total_shmem). File is the page cache, and it includes Shmem (v2 file, v1
// total_cache). On v1, the total_ keys include child cgroups, as
// memory.usage_in_bytes does.
Anon *uint64
AnonErr error
Shmem *uint64
ShmemErr error
File *uint64
FileErr error
// InactiveFile is the page cache the kernel reclaims first under
// pressure (v2 inactive_file, v1 total_inactive_file).
InactiveFile *uint64
InactiveFileErr error
}
Container is a snapshot of the container cgroup's memory accounting, read from the controller mounted at the current process's root. A nil value is unknown, and its error says why; it is never reported as zero.
func ReadContainer ¶ added in v1.56.0
func ReadContainer() Container
ReadContainer reads the container cgroup's current usage, limit, lifetime peak, and the memory.stat breakdown of current usage. Each read is bounded and verified as Log's peak read is.