Skip to content

Commit

Permalink
Add ability to limit cpu usage rate by CFS cgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
criyle committed Dec 13, 2020
1 parent b1e9e0d commit 4bf6f86
Show file tree
Hide file tree
Showing 20 changed files with 257 additions and 163 deletions.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ The `executorserver` need root privilege to create `cgroup`. Either creates sub-
- The default CGroup prefix is `executor_server`, Can be specified with `-cgroup-prefix` flag.
- `-auth-token` to add token-based authentication to REST / gRPC
- `-src-prefix` to restrict `src` copyIn path (need to be absolute path)
- `-time-limit-checker-interval` specifies time limit checker interval (default 100ms)
- `-time-limit-checker-interval` specifies time limit checker interval (default 100ms) (valid value: \[1ms, 1s\])
- `-output-limit` specifies size limit of POSIX rlimit of output
- `-cpuset` specifies `cpuset.cpus` cgroup for each container
- `-container-cred-start` specifies container `setuid` / `setgid` credential start point (default: 10000)
- for example, by default container 0 will run with 10001 uid & gid and container 1 will run with 10002 uid & gid...
- `-enable-cpu-rate` enabled `cpu` cgroup to control cpu rate using cfs_quota & cfs_period control
- `-cpu-cfs-period` specifies cfs_period if cpu rate is enabled (default 100ms) (valid value: \[1ms, 1s\])

#### Environment Variables

Expand Down Expand Up @@ -593,3 +595,41 @@ Compile On Windows (cygwin):
}
]
```

Infinite loop with cpu rate control:

```json
{
"cmd": [{
"args": ["/usr/bin/python3", "1.py"],
"env": ["PATH=/usr/bin:/bin"],
"files": [{"content": ""}, {"name": "stdout","max": 10240}, {"name": "stderr","max": 10240}],
"cpuLimit": 3000000000,
"realCpuLimit": 4000000000,
"memoryLimit": 104857600,
"procLimit": 50,
"cpuRate": 0.1,
"copyIn": {
"1.py": {
"content": "while True:\n pass"
}
}
}]
}
```

```json
[
{
"status": "Time Limit Exceeded",
"exitStatus": 9,
"time": 414803599,
"memory": 3657728,
"runTime": 4046054900,
"files": {
"stderr": "",
"stdout": ""
}
}
]
```
2 changes: 2 additions & 0 deletions cmd/executorserver/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Config struct {
ExtraMemoryLimit *envexec.Size `flagUsage:"specifies extra memory buffer for check memory limit" default:"16k"`
OutputLimit *envexec.Size `flagUsage:"specifies POSIX rlimit for output for each command" default:"256m"`
Cpuset string `flagUsage:"control the usage of cpuset for all containerd process"`
EnableCPURate bool `flagUsage:"enable cpu cgroup rate control"`
CPUCfsPeriod time.Duration `flagUsage:"set cpu.cfs_period" default:"100ms"`

// server config
HTTPAddr string `flagUsage:"specifies the http binding address" default:":5050"`
Expand Down
1 change: 1 addition & 0 deletions cmd/executorserver/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func convertPBCmd(c *pb.Request_CmdType, srcPrefix string) (cm worker.Cmd, strea
MemoryLimit: c.GetMemoryLimit(),
StackLimit: c.GetStackLimit(),
ProcLimit: c.GetProcLimit(),
CPURateLimit: c.GetCPURateLimit(),
CopyOut: c.GetCopyOut(),
CopyOutCached: c.GetCopyOutCached(),
CopyOutMax: c.GetCopyOutMax(),
Expand Down
2 changes: 2 additions & 0 deletions cmd/executorserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func main() {
CgroupPrefix: conf.CgroupPrefix,
Cpuset: conf.Cpuset,
ContainerCredStart: conf.ContainerCredStart,
EnableCPURate: conf.EnableCPURate,
CPUCfsPeriod: conf.CPUCfsPeriod,
Logger: logger.Sugar(),
})
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions cmd/executorserver/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ type Cmd struct {
Files []*CmdFile `json:"files,omitempty"`
TTY bool `json:"tty,omitempty"`

CPULimit uint64 `json:"cpuLimit"`
RealCPULimit uint64 `json:"realCpuLimit"`
MemoryLimit uint64 `json:"memoryLimit"`
StackLimit uint64 `json:"stackLimit"`
ProcLimit uint64 `json:"procLimit"`
CPULimit uint64 `json:"cpuLimit"`
RealCPULimit uint64 `json:"realCpuLimit"`
MemoryLimit uint64 `json:"memoryLimit"`
StackLimit uint64 `json:"stackLimit"`
ProcLimit uint64 `json:"procLimit"`
CPURateLimit float64 `json:"cpuRateLimit"`

CopyIn map[string]CmdFile `json:"copyIn"`

Expand Down Expand Up @@ -164,6 +165,7 @@ func convertCmd(c Cmd, srcPrefix string) (worker.Cmd, error) {
MemoryLimit: c.MemoryLimit,
StackLimit: c.StackLimit,
ProcLimit: c.ProcLimit,
CPURateLimit: c.CPURateLimit,
CopyOut: c.CopyOut,
CopyOutCached: c.CopyOutCached,
CopyOutMax: c.CopyOutMax,
Expand Down
2 changes: 1 addition & 1 deletion cmd/executorserver/version.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions env/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package env

import "time"

// Logger defines logger to print logs
type Logger interface {
Debug(args ...interface{})
Expand All @@ -17,5 +19,7 @@ type Config struct {
CgroupPrefix string
Cpuset string
ContainerCredStart int
EnableCPURate bool
CPUCfsPeriod time.Duration
Logger
}
7 changes: 5 additions & 2 deletions env/env_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func NewBuilder(c Config) (pool.EnvBuilder, error) {
if c.Cpuset != "" {
cgb = cgb.WithCPUSet()
}
if c.EnableCPURate {
cgb = cgb.WithCPU()
}
cgb, err = cgb.FilterByEnv()
if err != nil {
return nil, err
Expand All @@ -106,9 +109,9 @@ func NewBuilder(c Config) (pool.EnvBuilder, error) {

var cgroupPool pool.CgroupPool
if cgb != nil {
cgroupPool = pool.NewFakeCgroupPool(cgb)
cgroupPool = pool.NewFakeCgroupPool(cgb, c.CPUCfsPeriod)
}
return pool.NewEnvBuilder(b, cgroupPool, workDir, c.Cpuset), nil
return pool.NewEnvBuilder(b, cgroupPool, workDir, c.Cpuset, c.EnableCPURate), nil
}

type credGen struct {
Expand Down
Loading

0 comments on commit 4bf6f86

Please sign in to comment.