Documentation
¶
Overview ¶
Package cgroup provices builder to create cgroup under systemd defined mount path (i.e.,sys/fs/cgroup) including v1 and v2 implementation.
Available cgroup controller:
cpu cpuset cpuacct memory pids
Current not available: devices, freezer, net_cls, blkio, perf_event, net_prio, huge_tlb, rdma
Index ¶
- Constants
- Variables
- func CreateV1ControllerPath(controller, prefix string) (string, error)
- func CreateV1ControllerPathName(controller, prefix, name string) (string, error)
- func EnableV2Nesting() error
- func EnsureDirExists(path string) error
- func GetAvailableControllerV1() (map[string]bool, error)
- func GetAvailableControllerV2() (map[string]bool, error)
- func GetCgroupV1Info() (map[string]Info, error)
- func NewV1Controller(p string) *v1controller
- type Builder
- func (b *Builder) Build(name string) (Cgroup, error)
- func (b *Builder) FilterByEnv() (*Builder, error)
- func (b *Builder) Random(pattern string) (Cgroup, error)
- func (b *Builder) String() string
- func (b *Builder) WithCPU() *Builder
- func (b *Builder) WithCPUAcct() *Builder
- func (b *Builder) WithCPUSet() *Builder
- func (b *Builder) WithMemory() *Builder
- func (b *Builder) WithPids() *Builder
- func (b *Builder) WithType(t CgroupType) *Builder
- type Cgroup
- type CgroupType
- type CgroupV1
- func (c *CgroupV1) AddProc(pid int) error
- func (c *CgroupV1) CPUUsage() (uint64, error)
- func (c *CgroupV1) Destroy() error
- func (c *CgroupV1) FindMemoryStatProperty(prop string) (uint64, error)
- func (c *CgroupV1) MemoryMaxUsage() (uint64, error)
- func (c *CgroupV1) MemoryMemswMaxUsageInBytes() (uint64, error)
- func (c *CgroupV1) MemoryUsage() (uint64, error)
- func (c *CgroupV1) SetCPUBandwidth(quota, period uint64) error
- func (c *CgroupV1) SetCPUCfsPeriod(p uint64) error
- func (c *CgroupV1) SetCPUCfsQuota(p uint64) error
- func (c *CgroupV1) SetCPUSet(b []byte) error
- func (c *CgroupV1) SetCpuacctUsage(i uint64) error
- func (c *CgroupV1) SetCpusetMems(b []byte) error
- func (c *CgroupV1) SetMemoryLimit(i uint64) error
- func (c *CgroupV1) SetMemoryMaxUsageInBytes(i uint64) error
- func (c *CgroupV1) SetMemoryMemswLimitInBytes(i uint64) error
- func (c *CgroupV1) SetProcLimit(i uint64) error
- type CgroupV2
- func (c *CgroupV2) AddProc(pid int) error
- func (c *CgroupV2) CPUUsage() (uint64, error)
- func (c *CgroupV2) Destroy() error
- func (c *CgroupV2) MemoryMaxUsage() (uint64, error)
- func (c *CgroupV2) MemoryUsage() (uint64, error)
- func (c *CgroupV2) ReadFile(name string) ([]byte, error)
- func (c *CgroupV2) ReadUint(filename string) (uint64, error)
- func (c *CgroupV2) SetCPUBandwidth(quota, period uint64) error
- func (c *CgroupV2) SetCPUSet(content []byte) error
- func (c *CgroupV2) SetMemoryLimit(l uint64) error
- func (c *CgroupV2) SetProcLimit(l uint64) error
- func (c *CgroupV2) WriteFile(name string, content []byte) error
- func (c *CgroupV2) WriteUint(filename string, i uint64) error
- type Info
Constants ¶
const ( CgroupTypeV1 = iota + 1 CgroupTypeV2 )
Variables ¶
var ErrNotInitialized = errors.New("cgroup was not initialized")
ErrNotInitialized returned when trying to read from not initialized cgroup
Functions ¶
func CreateV1ControllerPath ¶ added in v0.9.0
CreateSubCgroupPath creates path for controller with given group and prefix
func CreateV1ControllerPathName ¶ added in v0.9.0
CreateV1ControllerPathName create path for controller with given group, prefix and name
func EnableV2Nesting ¶ added in v0.9.1
func EnableV2Nesting() error
EnableV2Nesting migrates all process in the container to nested /init path and enables all available controllers in the root cgroup
func EnsureDirExists ¶
EnsureDirExists creates directories if the path not exists
func GetAvailableControllerV1 ¶ added in v0.9.0
GetAvailableControllerV1 reads /proc/cgroups and get all available controller as set
func GetAvailableControllerV2 ¶ added in v0.9.0
GetAvailableControllerV2 reads /sys/fs/cgroup/cgroup.controllers to get all controller
func GetCgroupV1Info ¶ added in v0.9.0
GetCgroupV1Info read /proc/cgroups and return the result
func NewV1Controller ¶ added in v0.9.0
func NewV1Controller(p string) *v1controller
NewV1Controller creates a cgroup accessor with given path (path needs to be created in advance)
Types ¶
type Builder ¶
type Builder struct { Prefix string Type CgroupType CPU bool CPUSet bool CPUAcct bool Memory bool Pids bool // contains filtered or unexported fields }
Builder builds cgroup directories available: cpuacct, memory, pids
func NewBuilder ¶
NewBuilder return a dumb builder without any controller
func (*Builder) FilterByEnv ¶
FilterByEnv reads /proc/cgroups and filter out non-exists ones
func (*Builder) Random ¶ added in v0.9.0
Random creates a cgroup with random directory, similar to os.MkdirTemp
func (*Builder) WithCPUAcct ¶
WithCPUAcct includes cpuacct cgroup
func (*Builder) WithCPUSet ¶ added in v0.5.3
WithCPUSet includes cpuset cgroup
func (*Builder) WithMemory ¶
WithMemory includes memory cgroup
func (*Builder) WithType ¶ added in v0.9.0
func (b *Builder) WithType(t CgroupType) *Builder
type Cgroup ¶ added in v0.2.0
type Cgroup interface { // AddProc add a process into the cgroup AddProc(pid int) error // Destroy deletes the cgroup Destroy() error // CPUUsage reads total cpu usage of cgroup CPUUsage() (uint64, error) // MemoryUsage reads current total memory usage MemoryUsage() (uint64, error) // MemoryMaxUsageInBytes reads max total memory usage. Not exist in cgroup v2 MemoryMaxUsage() (uint64, error) // SetCPUBandwidth sets the cpu bandwidth. Times in ns SetCPUBandwidth(quota, period uint64) error // SetCpusetCpus sets the availabile cpu to use (cpuset.cpus). SetCPUSet([]byte) error // SetMemoryLimit sets memory.limit_in_bytes SetMemoryLimit(uint64) error // SetProcLimit sets pids.max SetProcLimit(uint64) error }
Cgroup defines the common interface to control cgroups including v1 and v2 implementations. TODO: implement systemd integration
type CgroupType ¶ added in v0.9.0
type CgroupType int
func DetectType ¶ added in v0.9.1
func DetectType() CgroupType
DetectType detects current mounted cgroup type in systemd default path
func (CgroupType) String ¶ added in v0.9.0
func (t CgroupType) String() string
type CgroupV1 ¶ added in v0.9.0
type CgroupV1 struct {
// contains filtered or unexported fields
}
CgroupV1 is the combination of v1 controllers
func (*CgroupV1) Destroy ¶ added in v0.9.0
Destroy removes dir for controller, errors are ignored if remove one failed
func (*CgroupV1) FindMemoryStatProperty ¶ added in v0.9.0
FindMemoryStatProperty find certain property from memory.stat
func (*CgroupV1) MemoryMaxUsage ¶ added in v0.9.0
MemoryMaxUsage read memory.max_usage_in_bytes
func (*CgroupV1) MemoryMemswMaxUsageInBytes ¶ added in v0.9.0
MemoryMemswMaxUsageInBytes read memory.memsw.max_usage_in_bytes
func (*CgroupV1) MemoryUsage ¶ added in v0.9.0
MemoryUsage read memory.usage_in_bytes
func (*CgroupV1) SetCPUBandwidth ¶ added in v0.9.0
func (*CgroupV1) SetCPUCfsPeriod ¶ added in v0.9.0
SetCPUCfsPeriod set cpu.cfs_period_us in us
func (*CgroupV1) SetCPUCfsQuota ¶ added in v0.9.0
SetCPUCfsQuota set cpu.cfs_quota_us in us
func (*CgroupV1) SetCpuacctUsage ¶ added in v0.9.0
SetCpuacctUsage write cpuacct.usage in ns
func (*CgroupV1) SetCpusetMems ¶ added in v0.9.0
SetCpusetMems set cpuset.mems
func (*CgroupV1) SetMemoryLimit ¶ added in v0.9.0
SetMemoryLimit write memory.limit_in_bytes
func (*CgroupV1) SetMemoryMaxUsageInBytes ¶ added in v0.9.0
SetMemoryMaxUsageInBytes write cpuacct.usage in ns
func (*CgroupV1) SetMemoryMemswLimitInBytes ¶ added in v0.9.0
SetMemoryMemswLimitInBytes write memory.memsw.limit_in_bytes
func (*CgroupV1) SetProcLimit ¶ added in v0.9.0
SetProcLimit write pids.max
type CgroupV2 ¶ added in v0.9.0
type CgroupV2 struct {
// contains filtered or unexported fields
}
func (*CgroupV2) MemoryMaxUsage ¶ added in v0.9.0
MemoryMaxUsage not exist, use rusage.max_rss instead
func (*CgroupV2) MemoryUsage ¶ added in v0.9.0
MemoryUsage reads memory.current
func (*CgroupV2) ReadFile ¶ added in v0.9.0
ReadFile reads cgroup file and handles potential EINTR error while read to the slow device (cgroup)
func (*CgroupV2) SetCPUBandwidth ¶ added in v0.9.0
SetCPUBandwidth set cpu.max quota period
func (*CgroupV2) SetMemoryLimit ¶ added in v0.9.0
SetMemoryLimit memory.max
func (*CgroupV2) SetProcLimit ¶ added in v0.9.0
SetProcLimit pids.max