From 796050f1dac9349a4acbf776c0f5dcb763dab274 Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Thu, 21 Jul 2022 13:18:11 -0700 Subject: [PATCH] kvm: limit number of vCPU-s We see a regression of the startup benchmark. The main reason is that the maximum number of vCPUs in the kernel has been increased from 256 to 1024. In most cases, we don't need so many vCPU-s. PiperOrigin-RevId: 462455491 --- pkg/sentry/platform/kvm/machine_amd64.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/sentry/platform/kvm/machine_amd64.go b/pkg/sentry/platform/kvm/machine_amd64.go index c99009f0d..fa3152cd3 100644 --- a/pkg/sentry/platform/kvm/machine_amd64.go +++ b/pkg/sentry/platform/kvm/machine_amd64.go @@ -21,6 +21,7 @@ import ( "fmt" "math/big" "reflect" + "runtime" "runtime/debug" "golang.org/x/sys/unix" @@ -478,6 +479,16 @@ func (m *machine) getMaxVCPU() { } else { m.maxVCPUs = int(maxVCPUs) } + + // The goal here is to avoid vCPU contentions for reasonable workloads. + // But "reasonable" isn't defined well in this case. Let's say that CPU + // overcommit with factor 2 is still acceptable. We allocate a set of + // vCPU for each goruntime processor (P) and two sets of vCPUs to run + // user code. + rCPUs := runtime.GOMAXPROCS(0) + if 3*rCPUs < m.maxVCPUs { + m.maxVCPUs = 3 * rCPUs + } } func archPhysicalRegions(physicalRegions []physicalRegion) []physicalRegion {