Skip to content

Commit

Permalink
more settings for systemd engine: network, no cpu bind
Browse files Browse the repository at this point in the history
  • Loading branch information
zc authored and CMGS committed Mar 6, 2020
1 parent 9dee8cb commit 0ce475f
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions engine/systemd/unit.go
Expand Up @@ -6,9 +6,11 @@ import (
"fmt"
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/docker/go-units"
"github.com/projecteru2/core/engine"
enginetypes "github.com/projecteru2/core/engine/types"
"github.com/projecteru2/core/utils"
)
Expand Down Expand Up @@ -67,7 +69,7 @@ func (b *unitBuilder) buildUnit() *unitBuilder {
return b
}

func (b *unitBuilder) buildPreExec() *unitBuilder {
func (b *unitBuilder) buildPreExec(cpuAmount int) *unitBuilder {
if b.err != nil {
return b
}
Expand All @@ -76,22 +78,44 @@ func (b *unitBuilder) buildPreExec() *unitBuilder {
fmt.Sprintf("ExecStartPre=/usr/bin/cgcreate -g memory,cpuset:%s", b.cgroupPath()),
)

return b.buildCPULimit().buildMemoryLimit()
return b.buildNetworkLimit().buildCPULimit(cpuAmount).buildMemoryLimit()
}

func (b *unitBuilder) buildCPULimit() *unitBuilder {
func (b *unitBuilder) buildNetworkLimit() *unitBuilder {
if b.err != nil {
return b
}

network := b.opts.Network
for net, _ := range b.opts.Networks {
if net != "" {
network = net
break
}
}

if network != "" && network != "host" {
b.err = errors.Wrapf(engine.NotImplementedError, "systemd engine doesn't support network %s", network)
return b
}
return b
}

func (b *unitBuilder) buildCPULimit(cpuAmount int) *unitBuilder {
if b.err != nil {
return b
}

cpusetCPUs := ""
if len(b.opts.CPU) > 0 {
allowedCPUs := []string{}
for CPU, _ := range b.opts.CPU {
allowedCPUs = append(allowedCPUs, CPU)
}
b.serviceBuffer = append(b.serviceBuffer,
fmt.Sprintf("ExecStartPre=/usr/bin/cgset -r cpuset.cpus=%s %s", strings.Join(allowedCPUs, ","), b.cgroupPath()),
)
cpusetCPUs = strings.Join(allowedCPUs, ",")
} else {

cpusetCPUs = fmt.Sprintf("0-%d", cpuAmount-1)
}

if b.opts.Quota > 0 {
Expand All @@ -105,6 +129,7 @@ func (b *unitBuilder) buildCPULimit() *unitBuilder {
numaNode = "0"
}
b.serviceBuffer = append(b.serviceBuffer,
fmt.Sprintf("ExecStartPre=/usr/bin/cgset -r cpuset.cpus=%s %s", cpusetCPUs, b.cgroupPath()),
fmt.Sprintf("ExecStartPre=/usr/bin/cgset -r cpuset.mems=%s %s", numaNode, b.cgroupPath()),
)

Expand Down

0 comments on commit 0ce475f

Please sign in to comment.