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
- func ActorLeafMemory(actorUID string) (int64, error)
- func Delegate(ctx context.Context) error
- func KillActorLeaf(ctx context.Context, name string) error
- func KillLeafUnder(ctx context.Context, root, name string) error
- func LiftActorLeafMemory(actorUID string) (func(), error)
- func LimitActorLeafGrowth(actorUID string, headroom, ceiling, borrow int64) (func(), error)
- func PSIEnabled(root string) bool
- func PodMemory() (limit, unreclaimable int64, err error)
- func RemoveActorLeaf(actorUID string) error
- type ActorLeaf
- type Kill
- type MemoryGuard
- type MemoryWatch
Constants ¶
const MemoryGuardInterval = 250 * time.Millisecond
MemoryGuardInterval is how often a MemoryGuard should be checked.
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
ActorLeafMemory returns the actor's leaf memory.current.
func Delegate ¶
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 ¶
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 ¶
KillLeafUnder is KillActorLeaf for a leaf under root rather than Root.
func LiftActorLeafMemory ¶ added in v1.0.0
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
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
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
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 ¶
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 ¶
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 ¶
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
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
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.