Skip to content

Commit fa44301

Browse files
committed
vm/gvisor: allow to set a number of cpu-s
Signed-off-by: Andrei Vagin <avagin@google.com>
1 parent 0919b50 commit fa44301

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

vm/gvisor/gvisor.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"os/exec"
1616
"path/filepath"
17+
"runtime"
1718
"strings"
1819
"syscall"
1920
"time"
@@ -37,6 +38,7 @@ type Config struct {
3738
Count int `json:"count"` // number of VMs to use
3839
RunscArgs string `json:"runsc_args"`
3940
MemoryTotalBytes uint64 `json:"memory_total_bytes"`
41+
CPUs uint64 `json:"cpus"`
4042
}
4143

4244
type Pool struct {
@@ -106,7 +108,13 @@ func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {
106108
if pool.cfg.MemoryTotalBytes == 0 {
107109
memoryLimit = -1
108110
}
109-
vmConfig := fmt.Sprintf(configTempl, imageDir, caps, name, memoryLimit)
111+
cpuPeriod := uint64(100000)
112+
cpus := pool.cfg.CPUs
113+
if cpus == 0 {
114+
cpus = uint64(runtime.NumCPU())
115+
}
116+
cpuLimit := cpuPeriod * cpus
117+
vmConfig := fmt.Sprintf(configTempl, imageDir, caps, name, memoryLimit, cpuLimit, cpuPeriod)
110118
if err := osutil.WriteFile(filepath.Join(bundleDir, "config.json"), []byte(vmConfig)); err != nil {
111119
return nil, err
112120
}
@@ -164,7 +172,7 @@ func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {
164172
osutil.Run(time.Minute, inst.runscCmd("delete", "-force", inst.name))
165173
time.Sleep(3 * time.Second)
166174

167-
cmd := inst.runscCmd("--panic-log", panicLog, "run", "-bundle", bundleDir, inst.name)
175+
cmd := inst.runscCmd("--panic-log", panicLog, "--cpu-num-from-quota", "run", "-bundle", bundleDir, inst.name)
168176
cmd.Stdout = wpipe
169177
cmd.Stderr = wpipe
170178
if err := cmd.Start(); err != nil {
@@ -424,7 +432,9 @@ const configTempl = `
424432
"cgroupsPath": "%[3]v",
425433
"resources": {
426434
"cpu": {
427-
"shares": 1024
435+
"shares": 1024,
436+
"period": %[6]d,
437+
"quota": %[5]d
428438
},
429439
"memory": {
430440
"limit": %[4]d,

0 commit comments

Comments
 (0)