cgroup

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 14 Imported by: 7

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

View Source
const (
	CPU     = "cpu"
	CPUAcct = "cpuacct"
	CPUSet  = "cpuset"
	Memory  = "memory"
	Pids    = "pids"
)

Cgroup constants

View Source
const (
	TypeV1 = iota + 1
	TypeV2
)

Type enum for cgroup

Variables

View Source
var DetectedCgroupType = DetectType()

DetectedCgroupType defines the current cgroup type of the system

View Source
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

func AddProcesses(path string, procs []int) error

AddProcesses add processes into cgroup.procs file

func CreateV1ControllerPath added in v0.9.0

func CreateV1ControllerPath(controller, prefix string) (string, error)

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

func EnsureDirExists(path string) error

EnsureDirExists creates directories if the path not exists

func GetCgroupV1Info added in v0.9.0

func GetCgroupV1Info() (map[string]Info, error)

GetCgroupV1Info read /proc/cgroups and return the result

func GetCurrentCgroupPrefix added in v0.10.0

func GetCurrentCgroupPrefix() (string, error)

GetCurrentCgroupPrefix returns the cgroup prefix of current process

func ReadProcesses added in v0.10.0

func ReadProcesses(path string) ([]int, error)

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)

	// 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)
}

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

type Controllers struct {
	CPU     bool
	CPUSet  bool
	CPUAcct bool
	Memory  bool
	Pids    bool
}

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 Info added in v0.5.8

type Info struct {
	Hierarchy  int
	NumCgroups int
	Enabled    bool
}

Info reads the cgroup mount info from /proc/cgroups

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

func (Type) String added in v0.10.1

func (t Type) String() string

type V1 added in v0.10.1

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

V1 is the combination of v1 controllers

func (*V1) AddProc added in v0.10.1

func (c *V1) AddProc(pids ...int) error

AddProc writes cgroup.procs to all controller

func (*V1) CPUUsage added in v0.10.1

func (c *V1) CPUUsage() (uint64, error)

CPUUsage read cpuacct.usage in ns

func (*V1) Destroy added in v0.10.1

func (c *V1) Destroy() error

Destroy removes dir for controllers recursively, errors are ignored if remove one failed

func (*V1) Existing added in v0.10.1

func (c *V1) Existing() bool

Existing returns true if the cgroup was opened rather than created

func (*V1) FindMemoryStatProperty added in v0.10.1

func (c *V1) FindMemoryStatProperty(prop string) (uint64, error)

FindMemoryStatProperty find certain property from memory.stat

func (*V1) MemoryMaxUsage added in v0.10.1

func (c *V1) MemoryMaxUsage() (uint64, error)

MemoryMaxUsage read memory.max_usage_in_bytes

func (*V1) MemoryMemswMaxUsageInBytes added in v0.10.1

func (c *V1) MemoryMemswMaxUsageInBytes() (uint64, error)

MemoryMemswMaxUsageInBytes read memory.memsw.max_usage_in_bytes

func (*V1) MemoryUsage added in v0.10.1

func (c *V1) MemoryUsage() (uint64, error)

MemoryUsage read memory.usage_in_bytes

func (*V1) Nest added in v0.10.1

func (c *V1) Nest(name string) (Cgroup, error)

Nest creates a sub-cgroup, moves current process into that cgroup

func (*V1) New added in v0.10.1

func (c *V1) New(name string) (cg Cgroup, err error)

New creates a sub-cgroup based on the existing one

func (*V1) Processes added in v0.10.1

func (c *V1) Processes() ([]int, error)

Processes lists all existing process pid from the cgroup

func (*V1) Random added in v0.10.1

func (c *V1) Random(pattern string) (Cgroup, error)

Random creates a sub-cgroup based on the existing one but the name is randomly generated

func (*V1) SetCPUBandwidth added in v0.10.1

func (c *V1) SetCPUBandwidth(quota, period uint64) error

SetCPUBandwidth set cpu quota via cfs interface

func (*V1) SetCPUCfsPeriod added in v0.10.1

func (c *V1) SetCPUCfsPeriod(p uint64) error

SetCPUCfsPeriod set cpu.cfs_period_us in us

func (*V1) SetCPUCfsQuota added in v0.10.1

func (c *V1) SetCPUCfsQuota(p uint64) error

SetCPUCfsQuota set cpu.cfs_quota_us in us

func (*V1) SetCPUSet added in v0.10.1

func (c *V1) SetCPUSet(b []byte) error

SetCPUSet set cpuset.cpus

func (*V1) SetCpuacctUsage added in v0.10.1

func (c *V1) SetCpuacctUsage(i uint64) error

SetCpuacctUsage write cpuacct.usage in ns

func (*V1) SetCpusetMems added in v0.10.1

func (c *V1) SetCpusetMems(b []byte) error

SetCpusetMems set cpuset.mems

func (*V1) SetMemoryLimit added in v0.10.1

func (c *V1) SetMemoryLimit(i uint64) error

SetMemoryLimit write memory.limit_in_bytes

func (*V1) SetMemoryMaxUsageInBytes added in v0.10.1

func (c *V1) SetMemoryMaxUsageInBytes(i uint64) error

SetMemoryMaxUsageInBytes write cpuacct.usage in ns

func (*V1) SetMemoryMemswLimitInBytes added in v0.10.1

func (c *V1) SetMemoryMemswLimitInBytes(i uint64) error

SetMemoryMemswLimitInBytes write memory.memsw.limit_in_bytes

func (*V1) SetProcLimit added in v0.10.1

func (c *V1) SetProcLimit(i uint64) error

SetProcLimit write pids.max

func (*V1) String added in v0.10.1

func (c *V1) String() string

type V2 added in v0.10.1

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

V2 provides cgroup interface for v2

func (*V2) AddProc added in v0.10.1

func (c *V2) AddProc(pids ...int) error

AddProc adds processes into the cgroup

func (*V2) CPUUsage added in v0.10.1

func (c *V2) CPUUsage() (uint64, error)

CPUUsage reads cpu.stat usage_usec

func (*V2) Destroy added in v0.10.1

func (c *V2) Destroy() error

Destroy destroys the cgroup

func (*V2) Existing added in v0.10.1

func (c *V2) Existing() bool

Existing returns true if the cgroup was opened rather than created

func (*V2) MemoryMaxUsage added in v0.10.1

func (c *V2) MemoryMaxUsage() (uint64, error)

MemoryMaxUsage reads memory.peak

func (*V2) MemoryUsage added in v0.10.1

func (c *V2) MemoryUsage() (uint64, error)

MemoryUsage reads memory.current

func (*V2) Nest added in v0.10.1

func (c *V2) Nest(name string) (Cgroup, error)

Nest creates a sub-cgroup, moves current process into that cgroup

func (*V2) New added in v0.10.1

func (c *V2) New(name string) (Cgroup, error)

New creates a sub-cgroup based on the existing one

func (*V2) Processes added in v0.10.1

func (c *V2) Processes() ([]int, error)

Processes returns all processes within the cgroup

func (*V2) Random added in v0.10.1

func (c *V2) Random(pattern string) (Cgroup, error)

Random creates a sub-cgroup based on the existing one but the name is randomly generated

func (*V2) ReadFile added in v0.10.1

func (c *V2) ReadFile(name string) ([]byte, error)

ReadFile reads cgroup file and handles potential EINTR error while read to the slow device (cgroup)

func (*V2) ReadUint added in v0.10.1

func (c *V2) ReadUint(filename string) (uint64, error)

ReadUint read uint64 from given file

func (*V2) SetCPUBandwidth added in v0.10.1

func (c *V2) SetCPUBandwidth(quota, period uint64) error

SetCPUBandwidth set cpu.max quota period

func (*V2) SetCPUSet added in v0.10.1

func (c *V2) SetCPUSet(content []byte) error

SetCPUSet sets cpuset.cpus

func (*V2) SetMemoryLimit added in v0.10.1

func (c *V2) SetMemoryLimit(l uint64) error

SetMemoryLimit memory.max

func (*V2) SetProcLimit added in v0.10.1

func (c *V2) SetProcLimit(l uint64) error

SetProcLimit pids.max

func (*V2) String added in v0.10.1

func (c *V2) String() string

func (*V2) WriteFile added in v0.10.1

func (c *V2) WriteFile(name string, content []byte) error

WriteFile writes cgroup file and handles potential EINTR error while writes to the slow device (cgroup)

func (*V2) WriteUint added in v0.10.1

func (c *V2) WriteUint(filename string, i uint64) error

WriteUint writes uint64 into given file

Jump to

Keyboard shortcuts

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