cgroup

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cgroup delegates the worker pod's cgroup to the worker so each actor can get a leaf of its own.

Index

Constants

View Source
const MemoryGuardInterval = 250 * time.Millisecond

MemoryGuardInterval is how often a MemoryGuard should be checked.

View Source
const Root = "/sys/fs/cgroup"

Root is the worker pod's cgroup scope inside its private cgroup namespace.

Variables

This section is empty.

Functions

func ActorLeafMemory added in v1.0.0

func ActorLeafMemory(actorUID string) (int64, error)

ActorLeafMemory returns the actor's leaf memory.current.

func Delegate

func Delegate(ctx context.Context) error

Delegate prepares the worker pod's cgroup so the runtime can create per-actor leaves under it with real cpu/memory/pids accounting. A worker outside a private cgroup namespace fails here rather than running actors without their cgroups.

The unprivileged worker runs in a private cgroup namespace, so /sys/fs/cgroup is the pod's own cgroup scope rather than the host root. Two things must be arranged before runsc can nest container cgroups here:

  • The cgroup v2 "no internal processes" rule forbids a cgroup from holding processes directly while also delegating controllers to children. The pod scope is not the true cgroup root, so the exemption does not apply: we move the worker's own processes into a dedicated "worker" leaf.
  • Controllers are only available to children if enabled in the scope's cgroup.subtree_control. We enable everything the parent delegated to us.

The runtime bind-mounts /sys/fs/cgroup read-only for unprivileged pods. The worker holds CAP_SYS_ADMIN with no user namespace, so the ro flag is not locked: clear it and leave it writable (the runtime writes here on every create/restore).

func KillActorLeaf

func KillActorLeaf(ctx context.Context, name string) error

KillActorLeaf kills every process in the named leaf, including any they forked, and waits until the leaf is empty or ctx is done. A missing leaf, or a kernel without cgroup.kill, has nothing done.

func KillLeafUnder

func KillLeafUnder(ctx context.Context, root, name string) error

KillLeafUnder is KillActorLeaf for a leaf under root rather than Root.

func LiftActorLeafMemory added in v1.0.0

func LiftActorLeafMemory(actorUID string) (func(), error)

LiftActorLeafMemory clears memory.high on the actor's leaf, and returns a func that puts back the memory.high it replaced.

func LimitActorLeafGrowth added in v1.0.0

func LimitActorLeafGrowth(actorUID string, headroom, ceiling, borrow int64) (func(), error)

LimitActorLeafGrowth sets memory.high on the actor's leaf to ceiling bytes, or to its current usage plus headroom bytes if that is more, plus borrow bytes, and returns a func that puts back the memory.high it replaced. Past memory.high the kernel reclaims the leaf's page cache and throttles whoever is allocating, where at the worker pod's memory.max it may OOM-kill instead.

func PSIEnabled added in v1.0.0

func PSIEnabled(root string) bool

PSIEnabled reports whether the kernel tracks memory pressure for cgroups under root, which a MemoryWatch needs to tell a stuck cgroup from a busy one.

func PodMemory added in v1.0.0

func PodMemory() (limit, unreclaimable int64, err error)

PodMemory returns the worker pod's memory limit and what it uses besides clean page cache, which the kernel can reclaim. Both are 0 when the pod has no limit.

func RemoveActorLeaf

func RemoveActorLeaf(actorUID string) error

RemoveActorLeaf deletes the actor's leaf. Its processes must have exited.

Types

type ActorLeaf

type ActorLeaf struct {
	// contains filtered or unexported fields
}

ActorLeaf is a cgroup of its own for one actor's host processes, so actors on one worker share its CPU fairly and each stays within its declared limit.

func OpenActorLeaf

func OpenActorLeaf(actorUID string, milliCPU, memoryHigh int64) (*ActorLeaf, error)

OpenActorLeaf creates the actor's leaf under the delegated scope, or reuses a leftover one, caps it at milliCPU and sets memory.high to memoryHigh bytes when those are set. Close it once its processes are started.

func (*ActorLeaf) Close

func (l *ActorLeaf) Close() error

Close releases the leaf's handle. The leaf itself stays until RemoveActorLeaf.

func (*ActorLeaf) SysProcAttr

func (l *ActorLeaf) SysProcAttr() *syscall.SysProcAttr

SysProcAttr starts a process directly inside the leaf.

type Kill added in v1.0.0

type Kill struct {
	Limit, Current uint64
	// Done receives the kill's result once the cgroup is empty or the kill
	// gives up.
	Done <-chan error
}

Kill is a kill a MemoryGuard started.

type MemoryGuard added in v1.0.0

type MemoryGuard struct {
	// contains filtered or unexported fields
}

MemoryGuard kills one cgroup once a MemoryWatch finds it stuck at its memory.high. It does nothing until Resume arms it with the limit.

func (*MemoryGuard) Check added in v1.0.0

func (g *MemoryGuard) Check(ctx context.Context, root, name string) (Kill, bool)

Check kills the cgroup root/name in the background if it is stuck at its memory.high, and reports whether it did. A guard that is paused, already killing, or has killed does nothing.

func (*MemoryGuard) Killed added in v1.0.0

func (g *MemoryGuard) Killed() bool

Killed reports whether the guard killed its cgroup.

func (*MemoryGuard) Pause added in v1.0.0

func (g *MemoryGuard) Pause() uint64

Pause stops the guard, waiting out a kill in progress, and returns the limit to resume with.

func (*MemoryGuard) Resume added in v1.0.0

func (g *MemoryGuard) Resume(limit uint64)

Resume arms the guard for limit, from a fresh window. A limit of 0 leaves it paused.

type MemoryWatch added in v1.0.0

type MemoryWatch struct {
	// contains filtered or unexported fields
}

MemoryWatch decides when a cgroup with memory.high at its limit is stuck there and should be killed, from how much of the recent past it spent stalled on memory. Check it on a timer, one MemoryWatch per cgroup.

func (*MemoryWatch) Check added in v1.0.0

func (w *MemoryWatch) Check(dir string, limit uint64, now time.Time) (bool, uint64, error)

Check reports whether the cgroup in dir is stuck at limit, along with its memory.current. Page cache aside, it is stuck when within 1/16 of the limit while stalled on memory for at least memoryStallKill of the last memoryStallWindow and hitting its own memory.high, or when over the limit by more than 1/16 of it or memoryOvershootMax at all.

Stalls rather than memory.current over the limit, because reclaim keeps pulling a throttled cgroup back under memory.high between allocations, and at only a little over it a cgroup stalls about half the time. "some" rather than "full", because busy threads alongside a throttled one hide a full stall. Its own memory.high events, so stalls from node-wide reclaim don't count.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL