cgroups

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2021 License: MIT Imports: 8 Imported by: 3

README

go-cgroups

GoDoc

Package go-cgroups provides primitives to work with Linux Control Groups via pseudo file system /sys/fs/cgroup.

Installation

go get github.com/0xef53/go-cgroups

Documentation

Use Godoc documentation for reference and usage.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCfsNotEnabled = errors.New("Make sure that CONFIG_CFS_BANDWIDTH option is enabled in your kernel")
	ErrRtNotEnabled  = errors.New("Make sure that CONFIG_RT_GROUP_SCHED option is enabled in your kernel")
)
View Source
var (
	ErrCgroupRemoved = errors.New("Unable to continue. Control group already removed")
)

Functions

func DestroyCgroup

func DestroyCgroup(path string) error

DestroyCgroup destroys the cgroup located on the given path.

func GetCgroupPathByPid

func GetCgroupPathByPid(pid int, subsystem string) (string, error)

GetCgroupPathByPid returns a path to the cgroup containing the specified pid.

func GetEnabledSubsystems

func GetEnabledSubsystems() (map[string]int, error)

GetEnabledSubsystems returns a map with all the subsystems supported by the kernel.

func GetProcessCgroups

func GetProcessCgroups(pid int) (map[string]string, error)

GetProcessCgroups returns a map with the all cgroup subsystems and their relative paths to which the specified pid belongs.

func GetSubsystemMountpoint

func GetSubsystemMountpoint(subsystem string) (string, error)

GetSubsystemMountpoint returns a path where a given subsystem is mounted.

func IsCgroupsNotFoundError

func IsCgroupsNotFoundError(err error) bool

func IsMountpointError

func IsMountpointError(err error) bool

func IsNotInSubsystemError

func IsNotInSubsystemError(err error) bool

func IsUnsupportedError

func IsUnsupportedError(err error) bool

func NewCgroupsNotFoundError

func NewCgroupsNotFoundError(pid int) error

func NewMountpointError

func NewMountpointError(subsystem string) error

func NewNotInSubsystemError

func NewNotInSubsystemError(pid int, subsystem string) error

func NewUnsupportedError

func NewUnsupportedError(subsystem string) error

Types

type Cgroup

type Cgroup interface {
	// Sets the cgroup parameters represented by *Config variable
	Set(*Config) error

	// Gets the actual cgroup parameters and stores them to the pointer of *Config
	Get(*Config) error

	// Gets the cgroup stat information and store its to the pointer of *Stats
	GetStats(*Stats) error

	// Returns full path of the cgroup relative to the filesystem root
	GetPath() string
}

Cgroup is an uniform interface for the cgroups.

func LookupCgroupByPid

func LookupCgroupByPid(pid int, subsystem string) (Cgroup, error)

LookupCgroupByPid tries to find the group containing the specified pid.

If the specified subsystem is not supported by this package, an error of type *UnsupportedSubsystemError will be returned.

func NewCpuGroup

func NewCpuGroup(subpath string, pid int) (Cgroup, error)

NewCpuGroup creates new subpath in the CPU subsystem directory and puts the specified pid into it.

The path of subsystem directory is determined using the /proc/self/mountinfo file.

type CgroupsNotFoundError

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

func (*CgroupsNotFoundError) Error

func (e *CgroupsNotFoundError) Error() string

type Config

type Config struct {
	// CPU shares (relative weight vs. other tasks)
	CpuShares int64 `json:"cpu_shares"`

	// CPU hardcap limit (in usecs). Allowed cpu time in a given period.
	CpuQuota int64 `json:"cpu_quota"`

	// CPU period to be used for hardcapping (in usecs).
	CpuPeriod int64 `json:"cpu_period"`

	// How many time CPU will use in realtime scheduling (in usecs).
	CpuRtRuntime int64 `json:"cpu_rt_runtime"`

	// CPU period to be used for realtime scheduling (in usecs).
	CpuRtPeriod int64 `json:"cpu_rt_period"`
}

Config specifies parameters for the various subsystems.

type CpuGroup

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

CpuGroup is an implementation of the common Cgroup interface.

func (*CpuGroup) Get

func (g *CpuGroup) Get(c *Config) error

Get returns actual parameters and limits for the current cgroup.

func (*CpuGroup) GetPath

func (g *CpuGroup) GetPath() string

GetPath return a full path to the directory of the current cgroup.

func (*CpuGroup) GetStats

func (g *CpuGroup) GetStats(stats *Stats) error

GetStats returns usage statistics for the current cgroup.

func (*CpuGroup) Set

func (g *CpuGroup) Set(c *Config) error

Set applies the parameters specified in the config to the current cgroup.

type CpuStats

type CpuStats struct {
	CpuUsage       CpuUsage       `json:"cpu_usage,omitempty"`
	ThrottlingData ThrottlingData `json:"throttling_data,omitempty"`
}

CpuStats contains various CPU statistics and accounting information associated with a cgroup.

type CpuUsage

type CpuUsage struct {
	// Total CPU time consumed (in nanoseconds).
	TotalUsage uint64 `json:"total_usage,omitempty"`

	// Total CPU time consumed per core (in nanoseconds).
	PercpuUsage []uint64 `json:"percpu_usage,omitempty"`

	// Time spent by tasks of the cgroup in kernel mode (in nanoseconds).
	UsageInKernelmode uint64 `json:"usage_in_kernelmode"`

	// Time spent by tasks of the cgroup in user mode (in nanoseconds).
	UsageInUsermode uint64 `json:"usage_in_usermode"`
}

type Manager

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

Manager is a wrapper around the several cgroups to more convenient using.

func LoadManager

func LoadManager(pid int) (*Manager, error)

LoadManager tries to find all cgroup in which the specified pid is placed and returns a new Manager on success to manage them.

func NewManager

func NewManager(pid int, subpath string, subsystems ...string) (*Manager, error)

NewManager creates a new cgroup for the specified pid in each of the included subsystems (determined using the /proc/cgroups file).

The function returns a new Manager object to manage the cgroup set.

func (*Manager) Destroy

func (m *Manager) Destroy() error

Destroy destroys the cgroup set.

func (*Manager) Get

func (m *Manager) Get(c *Config) error

Get returns actual parameters and limits for the cgroup set.

func (*Manager) GetStats

func (m *Manager) GetStats(stats *Stats) error

GetStats returns usage statistics for the cgroup set.

func (*Manager) Set

func (m *Manager) Set(c *Config) error

Set applies the parameters specified in the config to the cgroup set.

type MountpointError

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

func (*MountpointError) Error

func (e *MountpointError) Error() string

type NotInSubsystemError

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

func (*NotInSubsystemError) Error

func (e *NotInSubsystemError) Error() string

type Stats

type Stats struct {
	CpuStats CpuStats `json:"cpu_stats,omitempty"`
}

Stats contains metrics and limits from each of the cgroup subsystems.

type ThrottlingData

type ThrottlingData struct {
	// Number of periods with throttling active.
	Periods uint64 `json:"nr_periods,omitempty"`

	// Number of times when the tasks have been throttled.
	ThrottledPeriods uint64 `json:"nr_throttled,omitempty"`

	// Aggregate time when the tasks have been throttled (in nanoseconds).
	ThrottledTime uint64 `json:"throttled_time,omitempty"`
}

type UnsupportedError

type UnsupportedError struct {
	Subsystem string
}

func (*UnsupportedError) Error

func (e *UnsupportedError) Error() string

Jump to

Keyboard shortcuts

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