Documentation ¶
Overview ¶
Package proclimit provides a consistent cross-platform API for limiting CPU and memory resources to processes. It also provides a wrapper Cmd type that mimics exec.Cmd, while applying resource limits upon starting the commands.
Index ¶
Examples ¶
Constants ¶
const ( Byte Memory = 1 Kilobyte = 1024 * Byte Megabyte = 1024 * Kilobyte Gigabyte = 1024 * Megabyte )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cgroup ¶
type Cgroup struct { Name string LinuxResources *specs.LinuxResources // contains filtered or unexported fields }
Cgroup represents a cgroup in a Linux system. Resource limits can be configured by modifying LinuxResources through Options. Modifying LinuxResources after calling New(...) will have no effect.
func New ¶
New creates a new Cgroup. Resource limits and the name of the Cgroup can be defined using Option arguments.
Example ¶
package main import ( "github.com/aoldershaw/proclimit" "os" ) func main() { limiter, err := proclimit.New( proclimit.WithName("my-cgroup"), proclimit.WithCPULimit(proclimit.Percent(50)), proclimit.WithMemoryLimit(512*proclimit.Megabyte), ) if err != nil { // handle err } defer limiter.Close() cmd := limiter.Command("stress", "--cpu", "2", "--timeout", "10") cmd.Stdout = os.Stdout err = cmd.Run() if err != nil { // handle err } }
Output:
func (*Cgroup) Command ¶
Command constructs a wrapped Cmd struct to execute the named program with the given arguments. This wrapped Cmd will be added to the Cgroup when it is started.
type Cmd ¶
Cmd represents an external command being prepared or run. This command will be limited by the provided Limiter.
It can be used exactly as an exec.Cmd can be used.
func (*Cmd) CombinedOutput ¶
CombinedOutput runs the command (with limits) and returns its combined standard output and standard error.
func (*Cmd) Start ¶
Start begins the execution of a Cmd, and applies the limits defined by the associated Limiter. If the Limiter fails to apply limits, the process will be killed.
Note that the Cmd will start before the limits are applied, so there will be a brief period where the limits are not enforced.
type Limiter ¶
type Limiter interface { // Limit applies limits to a running process by its pid. // The specific limits to apply are defined by the implementation. Limit(pid int) error }
Limiter allows limiting a running process' resources
type Option ¶
type Option func(cgroup *Cgroup)
Option allows for customizing the behaviour of the Cgroup limiter.
Options will typically perform operations on cgroup.LinuxResources.
func WithCPULimit ¶
WithCPULimit sets the maximum CPU limit (as a percentage) allowed for all processes within the Cgroup. The percentage is based on a single CPU core. That is to say, 50 allows for the use of half of a core, 200 allows for the use of two cores, etc.
`cpu.cfs_period_us` will be set to 100000 (100ms) unless it has been overridden (e.g. by an Option that is added before this Option. Note that no such Option has been implemented currently).
`cpu.cfs_quota_us` will be set to cpuLimit percent of `cpu.cfs_period_us`.
func WithMemoryLimit ¶
WithMemoryLimit sets the maximum amount of virtual memory allowed for all processes within the Cgroup.
`memory.max_usage_in_bytes` is set to memory