cgroup

package
v0.0.0-...-c269f4c Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 14 Imported by: 0

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

View Source
const (
	CgroupTypeV1 = iota + 1
	CgroupTypeV2
)

Variables

View Source
var ErrNotInitialized = errors.New("cgroup was not initialized")

ErrNotInitialized returned when trying to read from not initialized cgroup

Functions

func CreateV1ControllerPath

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

CreateSubCgroupPath creates path for controller with given group and prefix

func CreateV1ControllerPathName

func CreateV1ControllerPathName(controller, prefix, name string) (string, error)

CreateV1ControllerPathName create path for controller with given group, prefix and name

func EnableV2Nesting

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 GetAvailableControllerV1

func GetAvailableControllerV1() (map[string]bool, error)

GetAvailableControllerV1 reads /proc/cgroups and get all available controller as set

func GetAvailableControllerV2

func GetAvailableControllerV2() (map[string]bool, error)

GetAvailableControllerV2 reads /sys/fs/cgroup/cgroup.controllers to get all controller

func GetCgroupV1Info

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

GetCgroupV1Info read /proc/cgroups and return the result

func NewV1Controller

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

func NewBuilder(prefix string) *Builder

NewBuilder return a dumb builder without any controller

func (*Builder) Build

func (b *Builder) Build(name string) (Cgroup, error)

Build creates new cgrouup directories

func (*Builder) FilterByEnv

func (b *Builder) FilterByEnv() (*Builder, error)

FilterByEnv reads /proc/cgroups and filter out non-exists ones

func (*Builder) Random

func (b *Builder) Random(pattern string) (Cgroup, error)

Random creates a cgroup with random directory, similar to os.MkdirTemp

func (*Builder) String

func (b *Builder) String() string

String prints the build properties

func (*Builder) WithCPU

func (b *Builder) WithCPU() *Builder

WithCPU includes cpu cgroup

func (*Builder) WithCPUAcct

func (b *Builder) WithCPUAcct() *Builder

WithCPUAcct includes cpuacct cgroup

func (*Builder) WithCPUSet

func (b *Builder) WithCPUSet() *Builder

WithCPUSet includes cpuset cgroup

func (*Builder) WithMemory

func (b *Builder) WithMemory() *Builder

WithMemory includes memory cgroup

func (*Builder) WithPids

func (b *Builder) WithPids() *Builder

WithPids includes pids cgroup

func (*Builder) WithType

func (b *Builder) WithType(t CgroupType) *Builder

type Cgroup

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

type CgroupType int

func DetectType

func DetectType() CgroupType

DetectType detects current mounted cgroup type in systemd default path

func (CgroupType) String

func (t CgroupType) String() string

type CgroupV1

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

CgroupV1 is the combination of v1 controllers

func (*CgroupV1) AddProc

func (c *CgroupV1) AddProc(pid int) error

AddProc writes cgroup.procs to all controller

func (*CgroupV1) CPUUsage

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

CpuacctUsage read cpuacct.usage in ns

func (*CgroupV1) Destroy

func (c *CgroupV1) Destroy() error

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

func (*CgroupV1) FindMemoryStatProperty

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

FindMemoryStatProperty find certain property from memory.stat

func (*CgroupV1) MemoryMaxUsage

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

MemoryMaxUsage read memory.max_usage_in_bytes

func (*CgroupV1) MemoryMemswMaxUsageInBytes

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

MemoryMemswMaxUsageInBytes read memory.memsw.max_usage_in_bytes

func (*CgroupV1) MemoryUsage

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

MemoryUsage read memory.usage_in_bytes

func (*CgroupV1) SetCPUBandwidth

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

func (*CgroupV1) SetCPUCfsPeriod

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

SetCPUCfsPeriod set cpu.cfs_period_us in us

func (*CgroupV1) SetCPUCfsQuota

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

SetCPUCfsQuota set cpu.cfs_quota_us in us

func (*CgroupV1) SetCPUSet

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

SetCPUSet set cpuset.cpus

func (*CgroupV1) SetCpuacctUsage

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

SetCpuacctUsage write cpuacct.usage in ns

func (*CgroupV1) SetCpusetMems

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

SetCpusetMems set cpuset.mems

func (*CgroupV1) SetMemoryLimit

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

SetMemoryLimit write memory.limit_in_bytes

func (*CgroupV1) SetMemoryMaxUsageInBytes

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

SetMemoryMaxUsageInBytes write cpuacct.usage in ns

func (*CgroupV1) SetMemoryMemswLimitInBytes

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

SetMemoryMemswLimitInBytes write memory.memsw.limit_in_bytes

func (*CgroupV1) SetProcLimit

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

SetProcLimit write pids.max

type CgroupV2

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

func (*CgroupV2) AddProc

func (c *CgroupV2) AddProc(pid int) error

func (*CgroupV2) CPUUsage

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

CPUUsage reads cpu.stat usage_usec

func (*CgroupV2) Destroy

func (c *CgroupV2) Destroy() error

func (*CgroupV2) MemoryMaxUsage

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

MemoryMaxUsage not exist, use rusage.max_rss instead

func (*CgroupV2) MemoryUsage

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

MemoryUsage reads memory.current

func (*CgroupV2) ReadFile

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

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

func (*CgroupV2) ReadUint

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

ReadUint read uint64 from given file

func (*CgroupV2) SetCPUBandwidth

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

SetCPUBandwidth set cpu.max quota period

func (*CgroupV2) SetCPUSet

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

SetCPUSet sets cpuset.cpus

func (*CgroupV2) SetMemoryLimit

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

SetMemoryLimit memory.max

func (*CgroupV2) SetProcLimit

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

SetProcLimit pids.max

func (*CgroupV2) WriteFile

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

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

func (*CgroupV2) WriteUint

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

WriteUint writes uint64 into given file

type Info

type Info struct {
	Hierarchy  int
	NumCgroups int
	Enabled    bool
}

Info reads the cgroup mount info from /proc/cgroups

Jump to

Keyboard shortcuts

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