cgroup

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 14 Imported by: 6

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

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

CreateSubCgroupPath creates path for controller with given group and prefix

func CreateV1ControllerPathName added in v0.9.0

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

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

func EnsureDirExists(path string) error

EnsureDirExists creates directories if the path not exists

func GetAvailableControllerV1 added in v0.9.0

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

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

func GetAvailableControllerV2 added in v0.9.0

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

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

func GetCgroupV1Info added in v0.9.0

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

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

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

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

func (b *Builder) WithCPU() *Builder

WithCPU includes cpu cgroup

func (*Builder) WithCPUAcct

func (b *Builder) WithCPUAcct() *Builder

WithCPUAcct includes cpuacct cgroup

func (*Builder) WithCPUSet added in v0.5.3

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 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) AddProc added in v0.9.0

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

AddProc writes cgroup.procs to all controller

func (*CgroupV1) CPUUsage added in v0.9.0

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

CpuacctUsage read cpuacct.usage in ns

func (*CgroupV1) Destroy added in v0.9.0

func (c *CgroupV1) Destroy() error

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

func (*CgroupV1) FindMemoryStatProperty added in v0.9.0

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

FindMemoryStatProperty find certain property from memory.stat

func (*CgroupV1) MemoryMaxUsage added in v0.9.0

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

MemoryMaxUsage read memory.max_usage_in_bytes

func (*CgroupV1) MemoryMemswMaxUsageInBytes added in v0.9.0

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

MemoryMemswMaxUsageInBytes read memory.memsw.max_usage_in_bytes

func (*CgroupV1) MemoryUsage added in v0.9.0

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

MemoryUsage read memory.usage_in_bytes

func (*CgroupV1) SetCPUBandwidth added in v0.9.0

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

func (*CgroupV1) SetCPUCfsPeriod added in v0.9.0

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

SetCPUCfsPeriod set cpu.cfs_period_us in us

func (*CgroupV1) SetCPUCfsQuota added in v0.9.0

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

SetCPUCfsQuota set cpu.cfs_quota_us in us

func (*CgroupV1) SetCPUSet added in v0.9.0

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

SetCPUSet set cpuset.cpus

func (*CgroupV1) SetCpuacctUsage added in v0.9.0

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

SetCpuacctUsage write cpuacct.usage in ns

func (*CgroupV1) SetCpusetMems added in v0.9.0

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

SetCpusetMems set cpuset.mems

func (*CgroupV1) SetMemoryLimit added in v0.9.0

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

SetMemoryLimit write memory.limit_in_bytes

func (*CgroupV1) SetMemoryMaxUsageInBytes added in v0.9.0

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

SetMemoryMaxUsageInBytes write cpuacct.usage in ns

func (*CgroupV1) SetMemoryMemswLimitInBytes added in v0.9.0

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

SetMemoryMemswLimitInBytes write memory.memsw.limit_in_bytes

func (*CgroupV1) SetProcLimit added in v0.9.0

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

SetProcLimit write pids.max

type CgroupV2 added in v0.9.0

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

func (*CgroupV2) AddProc added in v0.9.0

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

func (*CgroupV2) CPUUsage added in v0.9.0

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

CPUUsage reads cpu.stat usage_usec

func (*CgroupV2) Destroy added in v0.9.0

func (c *CgroupV2) Destroy() error

func (*CgroupV2) MemoryMaxUsage added in v0.9.0

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

MemoryMaxUsage not exist, use rusage.max_rss instead

func (*CgroupV2) MemoryUsage added in v0.9.0

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

MemoryUsage reads memory.current

func (*CgroupV2) ReadFile added in v0.9.0

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

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

ReadUint read uint64 from given file

func (*CgroupV2) SetCPUBandwidth added in v0.9.0

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

SetCPUBandwidth set cpu.max quota period

func (*CgroupV2) SetCPUSet added in v0.9.0

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

SetCPUSet sets cpuset.cpus

func (*CgroupV2) SetMemoryLimit added in v0.9.0

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

SetMemoryLimit memory.max

func (*CgroupV2) SetProcLimit added in v0.9.0

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

SetProcLimit pids.max

func (*CgroupV2) WriteFile added in v0.9.0

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

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

WriteUint writes uint64 into given file

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

Jump to

Keyboard shortcuts

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