diff options
| author | Konstantin Bogomolov <bogomolov@google.com> | 2023-10-05 22:13:06 +0000 |
|---|---|---|
| committer | Andrei Vagin <avagin@google.com> | 2023-10-06 23:29:19 +0000 |
| commit | 5e837c76f52db819969341086d19d650156d1dc1 (patch) | |
| tree | 6c1afc76261643fe907ab4b9c7114607635ad0b4 /vm/gvisor | |
| parent | ea12a9187acad46e97f58d5ee56a47b503e7434f (diff) | |
vm/gvisor: apply memory limit to gvisor instances
Diffstat (limited to 'vm/gvisor')
| -rw-r--r-- | vm/gvisor/gvisor.go | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/vm/gvisor/gvisor.go b/vm/gvisor/gvisor.go index 69b852b0d..e5cdefb35 100644 --- a/vm/gvisor/gvisor.go +++ b/vm/gvisor/gvisor.go @@ -29,8 +29,9 @@ func init() { } type Config struct { - Count int `json:"count"` // number of VMs to use - RunscArgs string `json:"runsc_args"` + Count int `json:"count"` // number of VMs to use + RunscArgs string `json:"runsc_args"` + MemoryTotalBytes uint64 `json:"memory_total_bytes"` } type Pool struct { @@ -60,6 +61,12 @@ func ctor(env *vmimpl.Env) (vmimpl.Pool, error) { if cfg.Count < 1 || cfg.Count > 128 { return nil, fmt.Errorf("invalid config param count: %v, want [1, 128]", cfg.Count) } + hostTotalMemory := osutil.SystemMemorySize() + minMemory := uint64(cfg.Count) * 10_000_000 + if cfg.MemoryTotalBytes != 0 && (cfg.MemoryTotalBytes < minMemory || cfg.MemoryTotalBytes > hostTotalMemory) { + return nil, fmt.Errorf("invalid config param memory_total_bytes: %v, want [%d,%d]", + minMemory, cfg.MemoryTotalBytes, hostTotalMemory) + } if env.Debug && cfg.Count > 1 { log.Logf(0, "limiting number of VMs from %v to 1 in debug mode", cfg.Count) cfg.Count = 1 @@ -94,7 +101,11 @@ func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) { caps += "\"" + c + "\"" } name := fmt.Sprintf("%v-%v", pool.env.Name, index) - vmConfig := fmt.Sprintf(configTempl, imageDir, caps, name) + memoryLimit := int64(pool.cfg.MemoryTotalBytes / uint64(pool.Count())) + if pool.cfg.MemoryTotalBytes == 0 { + memoryLimit = -1 + } + vmConfig := fmt.Sprintf(configTempl, imageDir, caps, name, memoryLimit) if err := osutil.WriteFile(filepath.Join(bundleDir, "config.json"), []byte(vmConfig)); err != nil { return nil, err } @@ -414,6 +425,11 @@ const configTempl = ` "resources": { "cpu": { "shares": 1024 + }, + "memory": { + "limit": %[4]d, + "reservation": %[4]d, + "disableOOMKiller": false } } }, |
