cgroup

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package cgroup provides a thin helper for placing a child process under a cgroup v2 group with CPU quota and I/O weight limits. Linux-only.

This is a building block for resource-throttled child supervision; the runner (internal/agent) does NOT consume it automatically yet. Callers pass an existing PID to AddPID after starting the child, then configure the relevant controller files. On child exit, call Remove to delete the cgroup directory.

Requirements:

  • cgroup v2 unified hierarchy (Linux 4.5+; ubiquitous on systemd hosts).
  • Write access to the parent cgroup directory. This typically means either running under a delegated subtree (e.g. via a systemd `Delegate=yes` user slice) or as root.

Errors:

  • ErrUnsupportedPlatform: returned by every constructor on non-Linux.
  • ErrCgroupV2Unavailable: cgroup v2 not mounted at /sys/fs/cgroup, or the parent path does not exist.
  • ErrPermission: filesystem permission denied (typically: not running under a delegated subtree).

Index

Constants

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

DefaultMountPoint is the conventional cgroup v2 unified-hierarchy mount.

Variables

View Source
var (
	ErrUnsupportedPlatform = errors.New("cgroup: unsupported platform (Linux only)")
	ErrCgroupV2Unavailable = errors.New("cgroup: cgroup v2 unified hierarchy not available")
	ErrPermission          = errors.New("cgroup: permission denied (run under a delegated subtree or as root)")
)

Sentinel errors.

Functions

This section is empty.

Types

type CPUMax

type CPUMax struct {
	Quota  int64 // microseconds; 0 = no limit
	Period int64 // microseconds; defaults to 100_000 (100ms) on the kernel side
}

CPUMax describes a CPU quota for a cgroup. The kernel format is "<quota> <period>" where quota is microseconds-of-CPU per period microseconds-of-wall-clock. Setting Quota=0 produces "max <period>" (no limit).

type Group

type Group struct {
	// Path is the absolute filesystem path to the cgroup directory,
	// e.g. /sys/fs/cgroup/cliwrap/proc-123.
	Path string
}

Group is a handle to a single cgroup directory. Methods write to the kernel-exposed control files under that directory.

func New

func New(parent, name string) (*Group, error)

New creates a new cgroup directory under parent (e.g. /sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/cliwrap.slice) with the given subgroup name. The parent directory must already exist and be writeable by the caller.

Returns ErrCgroupV2Unavailable if the unified hierarchy is not mounted or the parent path is missing; ErrPermission on filesystem permission errors; or a wrapped error for other failures.

func (*Group) AddPID

func (g *Group) AddPID(pid int) error

AddPID places pid into this cgroup by writing it to cgroup.procs. Subsequent threads of pid follow it automatically (cgroup v2 process migration semantics).

func (*Group) Remove

func (g *Group) Remove() error

Remove deletes the cgroup directory. The kernel rejects rmdir on a non-empty cgroup (i.e. while it still contains processes); callers must wait for the contained processes to exit before calling Remove.

func (*Group) SetCPUMax

func (g *Group) SetCPUMax(m CPUMax) error

SetCPUMax writes the cpu.max controller file. Quota=0 is interpreted as "max" (no limit). Period defaults to 100_000 µs when zero.

func (*Group) SetIOWeight

func (g *Group) SetIOWeight(w IOWeight) error

SetIOWeight writes io.weight (1..10000; default 100). Values outside the kernel-accepted range surface as a wrapped write error.

func (*Group) SetMemoryMax

func (g *Group) SetMemoryMax(m MemoryMax) error

SetMemoryMax writes the memory.max controller file. A zero value writes "max" (no limit).

type IOWeight

type IOWeight int

IOWeight is a relative weight 1..10000 (default 100). Lower = less I/O share. cgroup v2 only honors this when the io controller is enabled.

type MemoryMax

type MemoryMax uint64

MemoryMax is the memory hard limit in bytes. Zero = no limit.

Jump to

Keyboard shortcuts

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