Add minimum CPU number and only lower CPUs on --cpu-num-from-quota

* Add `--cpu-num-min` flag to control minimum CPUs
* Only lower CPU count
* Fix comments
This commit is contained in:
Aleksandr Razumov
2019-12-17 13:27:13 +03:00
parent 8782f0e287
commit b661434202
3 changed files with 21 additions and 5 deletions
+10 -2
View File
@@ -254,8 +254,14 @@ type Config struct {
// CPUNumFromQuota sets CPU number count to available CPU quota, using
// least integer value greater than or equal to quota.
//
// E.g. 0.2 CPU quota would result in 1, and 1.9 in 2.
// E.g. 0.2 CPU quota will result in 1, and 1.9 in 2.
CPUNumFromQuota bool
// CPUNumMin is minimum value of CPU number setting when CPUNumFromQuota
// strategy is active.
//
// E.g. when CPUNumMin is 2, 0.2 CPU quota will result in 2 instead of 1.
CPUNumMin int
}
// ToFlags returns a slice of flags that correspond to the given Config.
@@ -289,7 +295,9 @@ func (c *Config) ToFlags() []string {
"--overlayfs-stale-read=" + strconv.FormatBool(c.OverlayfsStaleRead),
}
if c.CPUNumFromQuota {
f = append(f, "--cpu-num-from-quota")
f = append(f, "--cpu-num-from-quota",
"--cpu-num-min="+strconv.Itoa(c.CPUNumMin),
)
}
// Only include these if set since it is never to be used by users.
if c.TestOnlyAllowRunAsCurrentUserWithoutChroot {
+3 -1
View File
@@ -82,7 +82,8 @@ var (
numNetworkChannels = flag.Int("num-network-channels", 1, "number of underlying channels(FDs) to use for network link endpoints.")
rootless = flag.Bool("rootless", false, "it allows the sandbox to be started with a user that is not root. Sandbox and Gofer processes may run with same privileges as current user.")
referenceLeakMode = flag.String("ref-leak-mode", "disabled", "sets reference leak check mode: disabled (default), log-names, log-traces.")
cpuNumFromQuota = flag.Bool("cpu-num-from-quota", false, "set cpu number to cpu quota (least integer greater than quota value)")
cpuNumFromQuota = flag.Bool("cpu-num-from-quota", false, "set cpu number to cpu quota (least integer greater or equal to quota value)")
cpuNumMin = flag.Int("cpu-num-min", 2, "minimum number of cpu to use with --cpu-num-from-quota")
// Test flags, not to be used outside tests, ever.
testOnlyAllowRunAsCurrentUserWithoutChroot = flag.Bool("TESTONLY-unsafe-nonroot", false, "TEST ONLY; do not ever use! This skips many security measures that isolate the host from the sandbox.")
@@ -227,6 +228,7 @@ func main() {
ReferenceLeakMode: refsLeakMode,
OverlayfsStaleRead: *overlayfsStaleRead,
CPUNumFromQuota: *cpuNumFromQuota,
CPUNumMin: *cpuNumMin,
TestOnlyAllowRunAsCurrentUserWithoutChroot: *testOnlyAllowRunAsCurrentUserWithoutChroot,
TestOnlyTestNameEnv: *testOnlyTestNameEnv,
+8 -2
View File
@@ -637,8 +637,14 @@ func (s *Sandbox) createSandboxProcess(conf *boot.Config, args *Args, startSyncF
if err != nil {
return fmt.Errorf("getting cpu qouta from cgroups: %v", err)
}
if quota > 0 {
cpuNum = int(math.Ceil(quota))
if n := int(math.Ceil(quota)); n > 0 {
if n < conf.CPUNumMin {
n = conf.CPUNumMin
}
if n < cpuNum {
// Only lower the cpu number.
cpuNum = n
}
}
}
cmd.Args = append(cmd.Args, "--cpu-num", strconv.Itoa(cpuNum))