Documentation
¶
Overview ¶
Package cgroup provides 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 AddProcesses(path string, procs []int) error
- func CreateV1ControllerPath(controller, prefix string) (string, error)
- func EnableV2Nesting() error
- func EnsureDirExists(path string) error
- func GetCgroupV1Info() (map[string]Info, error)
- func GetCurrentCgroupPrefix() (string, error)
- func ReadProcesses(path string) ([]int, error)
- type Cgroup
- type Controllers
- type Info
- type Type
- type V1
- func (c *V1) AddProc(pids ...int) error
- func (c *V1) CPUUsage() (uint64, error)
- func (c *V1) Destroy() error
- func (c *V1) Existing() bool
- func (c *V1) FindMemoryStatProperty(prop string) (uint64, error)
- func (c *V1) MemoryMaxUsage() (uint64, error)
- func (c *V1) MemoryMemswMaxUsageInBytes() (uint64, error)
- func (c *V1) MemoryUsage() (uint64, error)
- func (c *V1) Nest(name string) (Cgroup, error)
- func (c *V1) New(name string) (cg Cgroup, err error)
- func (c *V1) Open() (*os.File, error)
- func (c *V1) ProcessPeak() (uint64, error)
- func (c *V1) Processes() ([]int, error)
- func (c *V1) Random(pattern string) (Cgroup, error)
- func (c *V1) SetCPUBandwidth(quota, period uint64) error
- func (c *V1) SetCPUCfsPeriod(p uint64) error
- func (c *V1) SetCPUCfsQuota(p uint64) error
- func (c *V1) SetCPUSet(b []byte) error
- func (c *V1) SetCpuacctUsage(i uint64) error
- func (c *V1) SetCpusetMems(b []byte) error
- func (c *V1) SetMemoryLimit(i uint64) error
- func (c *V1) SetMemoryMaxUsageInBytes(i uint64) error
- func (c *V1) SetMemoryMemswLimitInBytes(i uint64) error
- func (c *V1) SetProcLimit(i uint64) error
- func (c *V1) String() string
- type V2
- func (c *V2) AddProc(pids ...int) error
- func (c *V2) CPUUsage() (uint64, error)
- func (c *V2) Destroy() error
- func (c *V2) Existing() bool
- func (c *V2) MemoryMaxUsage() (uint64, error)
- func (c *V2) MemoryUsage() (uint64, error)
- func (c *V2) Nest(name string) (Cgroup, error)
- func (c *V2) New(name string) (Cgroup, error)
- func (c *V2) Open() (*os.File, error)
- func (c *V2) ProcessPeak() (uint64, error)
- func (c *V2) Processes() ([]int, error)
- func (c *V2) Random(pattern string) (Cgroup, error)
- func (c *V2) ReadFile(name string) ([]byte, error)
- func (c *V2) ReadUint(filename string) (uint64, error)
- func (c *V2) SetCPUBandwidth(quota, period uint64) error
- func (c *V2) SetCPUSet(content []byte) error
- func (c *V2) SetMemoryLimit(l uint64) error
- func (c *V2) SetProcLimit(l uint64) error
- func (c *V2) String() string
- func (c *V2) WriteFile(name string, content []byte) error
- func (c *V2) WriteUint(filename string, i uint64) error
Constants ¶
const ( CPU = "cpu" CPUAcct = "cpuacct" CPUSet = "cpuset" Memory = "memory" Pids = "pids" )
Cgroup constants
const ( TypeV1 = iota + 1 TypeV2 )
Type enum for cgroup
Variables ¶
var DetectedCgroupType = DetectType()
DetectedCgroupType defines the current cgroup type of the system
var ErrNotInitialized = errors.New("cgroup was not initialized")
ErrNotInitialized returned when trying to read from not initialized cgroup
Functions ¶
func AddProcesses ¶ added in v0.10.0
AddProcesses add processes into cgroup.procs file
func CreateV1ControllerPath ¶ added in v0.9.0
CreateV1ControllerPath create path for controller with given group, prefix
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 GetCgroupV1Info ¶ added in v0.9.0
GetCgroupV1Info read /proc/cgroups and return the result
func GetCurrentCgroupPrefix ¶ added in v0.10.0
GetCurrentCgroupPrefix returns the cgroup prefix of current process
func ReadProcesses ¶ added in v0.10.0
ReadProcesses reads cgroup.procs file and return pids individually
Types ¶
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 // Existing returns true if the cgroup was opened rather than created Existing() bool //Nest creates a sub-cgroup, moves current process into that cgroup Nest(name string) (Cgroup, 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 with kernel version < 5.19 MemoryMaxUsage() (uint64, error) // ProcessPeak reads maximum number of process ever existed in cgroup. Not exist in cgroup v1 or kernel < 6.1 ProcessPeak() (uint64, error) // SetCPUBandwidth sets the cpu bandwidth. Times in ns SetCPUBandwidth(quota, period uint64) error // SetCpusetCpus sets the available cpu to use (cpuset.cpus). SetCPUSet([]byte) error // SetMemoryLimit sets memory.limit_in_bytes SetMemoryLimit(uint64) error // SetProcLimit sets pids.max SetProcLimit(uint64) error // Processes lists all existing process pid from the cgroup Processes() ([]int, error) // New creates a sub-cgroup based on the existing one New(string) (Cgroup, error) // Random creates a sub-cgroup based on the existing one but the name is randomly generated Random(string) (Cgroup, error) // Open opens the cgroup directory on V2 (used for clone3) Open() (*os.File, error) }
Cgroup defines the common interface to control cgroups including v1 and v2 implementations. TODO: implement systemd integration
func New ¶ added in v0.10.0
func New(prefix string, ct *Controllers) (Cgroup, error)
New creates a new cgroup with provided prefix, it opens existing one if existed
func OpenExisting ¶ added in v0.10.0
func OpenExisting(prefix string, ct *Controllers) (Cgroup, error)
OpenExisting opens a existing cgroup with provided prefix
type Controllers ¶ added in v0.10.0
Controllers defines enabled controller of a cgroup
func GetAvailableController ¶ added in v0.10.0
func GetAvailableController() (*Controllers, error)
GetAvailableController returns available cgroup controller in the system
func GetAvailableControllerV1 ¶ added in v0.9.0
func GetAvailableControllerV1() (*Controllers, error)
GetAvailableControllerV1 reads /proc/cgroups and get all available controller as set
func GetAvailableControllerV2 ¶ added in v0.9.0
func GetAvailableControllerV2() (*Controllers, error)
GetAvailableControllerV2 reads /sys/fs/cgroup/cgroup.controllers to get all controller
func GetAvailableControllerWithPrefix ¶ added in v0.10.0
func GetAvailableControllerWithPrefix(prefix string) (*Controllers, error)
GetAvailableControllerWithPrefix returns available cgroup controller within the cgroup prefix
func (*Controllers) Contains ¶ added in v0.10.0
func (c *Controllers) Contains(o *Controllers) bool
Contains returns true if the current controller enabled all controllers in the other controller
func (*Controllers) Intersect ¶ added in v0.10.0
func (c *Controllers) Intersect(o *Controllers)
Intersect reset the specific controller if it is not enabled in the other
func (*Controllers) Names ¶ added in v0.10.0
func (c *Controllers) Names() []string
Names returns a list of string of all enabled container names
func (*Controllers) Set ¶ added in v0.10.0
func (c *Controllers) Set(ct string, value bool)
Set changes the enabled status of a specific controller
func (*Controllers) String ¶ added in v0.10.0
func (c *Controllers) String() string
type Type ¶ added in v0.10.1
type Type int
Type defines the version of cgroup
func DetectType ¶ added in v0.9.1
func DetectType() Type
DetectType detects current mounted cgroup type in systemd default path
type V1 ¶ added in v0.10.1
type V1 struct {
// contains filtered or unexported fields
}
V1 is the combination of v1 controllers
func (*V1) Destroy ¶ added in v0.10.1
Destroy removes dir for controllers recursively, errors are ignored if remove one failed
func (*V1) Existing ¶ added in v0.10.1
Existing returns true if the cgroup was opened rather than created
func (*V1) FindMemoryStatProperty ¶ added in v0.10.1
FindMemoryStatProperty find certain property from memory.stat
func (*V1) MemoryMaxUsage ¶ added in v0.10.1
MemoryMaxUsage read memory.max_usage_in_bytes
func (*V1) MemoryMemswMaxUsageInBytes ¶ added in v0.10.1
MemoryMemswMaxUsageInBytes read memory.memsw.max_usage_in_bytes
func (*V1) MemoryUsage ¶ added in v0.10.1
MemoryUsage read memory.usage_in_bytes
func (*V1) Nest ¶ added in v0.10.1
Nest creates a sub-cgroup, moves current process into that cgroup
func (*V1) ProcessPeak ¶ added in v0.10.7
ProcessPeak implements Cgroup.
func (*V1) Random ¶ added in v0.10.1
Random creates a sub-cgroup based on the existing one but the name is randomly generated
func (*V1) SetCPUBandwidth ¶ added in v0.10.1
SetCPUBandwidth set cpu quota via cfs interface
func (*V1) SetCPUCfsPeriod ¶ added in v0.10.1
SetCPUCfsPeriod set cpu.cfs_period_us in us
func (*V1) SetCPUCfsQuota ¶ added in v0.10.1
SetCPUCfsQuota set cpu.cfs_quota_us in us
func (*V1) SetCpuacctUsage ¶ added in v0.10.1
SetCpuacctUsage write cpuacct.usage in ns
func (*V1) SetCpusetMems ¶ added in v0.10.1
SetCpusetMems set cpuset.mems
func (*V1) SetMemoryLimit ¶ added in v0.10.1
SetMemoryLimit write memory.limit_in_bytes
func (*V1) SetMemoryMaxUsageInBytes ¶ added in v0.10.1
SetMemoryMaxUsageInBytes write cpuacct.usage in ns
func (*V1) SetMemoryMemswLimitInBytes ¶ added in v0.10.1
SetMemoryMemswLimitInBytes write memory.memsw.limit_in_bytes
func (*V1) SetProcLimit ¶ added in v0.10.1
SetProcLimit write pids.max
type V2 ¶ added in v0.10.1
type V2 struct {
// contains filtered or unexported fields
}
V2 provides cgroup interface for v2
func (*V2) Existing ¶ added in v0.10.1
Existing returns true if the cgroup was opened rather than created
func (*V2) MemoryMaxUsage ¶ added in v0.10.1
MemoryMaxUsage reads memory.peak
func (*V2) MemoryUsage ¶ added in v0.10.1
MemoryUsage reads memory.current
func (*V2) Nest ¶ added in v0.10.1
Nest creates a sub-cgroup, moves current process into that cgroup
func (*V2) ProcessPeak ¶ added in v0.10.7
ProcessPeak reads pids.peak
func (*V2) Random ¶ added in v0.10.1
Random creates a sub-cgroup based on the existing one but the name is randomly generated
func (*V2) ReadFile ¶ added in v0.10.1
ReadFile reads cgroup file and handles potential EINTR error while read to the slow device (cgroup)
func (*V2) SetCPUBandwidth ¶ added in v0.10.1
SetCPUBandwidth set cpu.max quota period
func (*V2) SetMemoryLimit ¶ added in v0.10.1
SetMemoryLimit memory.max
func (*V2) SetProcLimit ¶ added in v0.10.1
SetProcLimit pids.max