Categorygithub.com/cloudlinux/cgroups
modulepackage
0.0.0-20220906131834-70f86747f3f1
Repository: https://github.com/cloudlinux/cgroups.git
Documentation: pkg.go.dev

# README

cgroups

Build Status codecov GoDoc Go Report Card

Go package for creating, managing, inspecting, and destroying cgroups. The resources format for settings on the cgroup uses the OCI runtime-spec found here.

Examples

Create a new cgroup

This creates a new cgroup using a static path for all subsystems under /test.

  • /sys/fs/cgroup/cpu/test
  • /sys/fs/cgroup/memory/test
  • etc....

It uses a single hierarchy and specifies cpu shares as a resource constraint and uses the v1 implementation of cgroups.

shares := uint64(100)
control, err := cgroups.New(cgroups.V1, cgroups.StaticPath("/test"), &specs.LinuxResources{
    CPU: &specs.CPU{
        Shares: &shares,
    },
})
defer control.Delete()

Create with systemd slice support

control, err := cgroups.New(cgroups.Systemd, cgroups.Slice("system.slice", "runc-test"), &specs.LinuxResources{
    CPU: &specs.CPU{
        Shares: &shares,
    },
})

Load an existing cgroup

control, err = cgroups.Load(cgroups.V1, cgroups.StaticPath("/test"))

Add a process to the cgroup

if err := control.Add(cgroups.Process{Pid:1234}); err != nil {
}

Update the cgroup

To update the resources applied in the cgroup

shares = uint64(200)
if err := control.Update(&specs.LinuxResources{
    CPU: &specs.LinuxCPU{
        Shares: &shares,
    },
}); err != nil {
}

Freeze and Thaw the cgroup

if err := control.Freeze(); err != nil {
}
if err := control.Thaw(); err != nil {
}

List all processes in the cgroup or recursively

processes, err := control.Processes(cgroups.Devices, recursive)

Get Stats on the cgroup

stats, err := control.Stat()

By adding cgroups.IgnoreNotExist all non-existent files will be ignored, e.g. swap memory stats without swap enabled

stats, err := control.Stat(cgroups.IgnoreNotExist)

Move process across cgroups

This allows you to take processes from one cgroup and move them to another.

err := control.MoveTo(destination)

Create subcgroup

subCgroup, err := control.New("child", resources)

Registering for memory events

This allows you to get notified by an eventfd for v1 memory cgroups events.

event := cgroups.MemoryThresholdEvent(50 * 1024 * 1024, false)
efd, err := control.RegisterMemoryEvent(event)
event := cgroups.MemoryPressureEvent(cgroups.MediumPressure, cgroups.DefaultMode)
efd, err := control.RegisterMemoryEvent(event)
efd, err := control.OOMEventFD()
// or by using RegisterMemoryEvent
event := cgroups.OOMEvent()
efd, err := control.RegisterMemoryEvent(event)

Attention

All static path should not include /sys/fs/cgroup/ prefix, it should start with your own cgroups name

Project details

Cgroups is a containerd sub-project, licensed under the Apache 2.0 license. As a containerd sub-project, you will find the:

information in our containerd/project repository.

# Packages

No description provided by the author
No description provided by the author
Devicefilter containes eBPF device filter program The implementation is based on https://github.com/containers/crun/blob/0.10.2/src/libcrun/ebpf.c Although ebpf.c is originally licensed under LGPL-3.0-or-later, the author (Giuseppe Scrivano) agreed to relicense the file in Apache License 2.0: https://github.com/opencontainers/runc/issues/2144#issuecomment-543116397 This particular Go implementation based on runc version https://github.com/opencontainers/runc/blob/master/libcontainer/cgroups/ebpf/devicefilter/devicefilter.go.

# Functions

AllowAny allows any subsystem errors to be skipped.
IgnoreModules configure the memory controller to not read memory metrics for some module names (e.g.
IgnoreNotExist ignores any errors that are for not existing files.
Load will load an existing cgroup and allow it to be controlled All static path should not include `/sys/fs/cgroup/` prefix, it should start with your own cgroups name.
MemoryPressureEvent returns a new memory pressure event to be used with RegisterMemoryEvent.
MemoryThresholdEvent returns a new memory threshold event to be used with RegisterMemoryEvent.
Mode returns the cgroups mode running on the host.
NestedPath will nest the cgroups based on the calling processes cgroup placing its child processes inside its own path.
New returns a new control via the cgroup cgroups interface.
NewBlkio returns a Blkio controller given the root folder of cgroups.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewMemory returns a Memory controller given the root folder of cgroups.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
OOMEvent returns a new oom event to be used with RegisterMemoryEvent.
OptionalSwap allows the memory controller to not fail if cgroups is not accounting Swap memory (there are no memory.memsw.* entries).
PidPath will return the correct cgroup paths for an existing process running inside a cgroup This is commonly used for the Load function to restore an existing container.
ProcRoot overrides the default location of the "/proc" filesystem.
RequireDevices requires the device subsystem but no others.
No description provided by the author
RunningInUserNS detects whether we are currently running in a user namespace.
SingleSubsystem returns a single cgroup subsystem within the base Hierarchy.
No description provided by the author
StaticPath returns a static path to use for all cgroups.
Subsystems returns a complete list of the default cgroups available on most linux systems.
No description provided by the author
V1 returns all the groups in the default cgroups mountpoint in a single hierarchy.
V1MountPoint returns the mount point where the cgroup mountpoints are mounted in a single hiearchy.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
The three memory pressure levels are as follows.
There are three optional modes that specify different propagation behavior: - "default": this is the default behavior specified above.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
There are three optional modes that specify different propagation behavior: - "default": this is the default behavior specified above.
No description provided by the author
Hybrid with cgroups v1 and v2 controllers mounted.
Legacy cgroups v1.
There are three optional modes that specify different propagation behavior: - "default": this is the default behavior specified above.
The three memory pressure levels are as follows.
The three memory pressure levels are as follows.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Unavailable cgroup mountpoint.
Unified with only cgroups v2 mounted.
No description provided by the author

# Variables

No description provided by the author
ErrControllerNotActive is returned when a controller is not supported or enabled.
ErrDevicesRequired is returned when the devices subsystem is required but does not exist or is not active.
No description provided by the author
ErrIgnoreSubsystem allows the specific subsystem to be skipped.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

InitConfig provides configuration options for the creation or loading of a cgroup and its subsystems.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

Cgroup handles interactions with the individual groups to perform actions on them as them main interface to this cgroup package.
MemoryEvent is an interface that V1 memory Cgroup notifications implement.
No description provided by the author

# Type aliases

CGMode is the cgroups mode of the host system.
ErrorHandler is a function that handles and acts on errors.
EventNotificationMode corresponds to the notification modes for the memory cgroups pressure level notifications.
Hierarchy enables both unified and split hierarchy for cgroups.
InitCheck allows subsystems errors to be checked when initialized or loaded.
InitOpts allows configuration for the creation or loading of a cgroup.
MemoryPressureLevel corresponds to the memory pressure levels defined for memory cgroups.
Name is a typed name for a cgroup subsystem.
No description provided by the author
State is a type that represents the state of the current cgroup.