From 9739850eebede51bebaf845bf4be45417584af62 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Tue, 1 Nov 2016 10:12:29 -0700 Subject: [PATCH] Add `--cpus` flag to control cpu resources This fix tries to address the proposal raised in 27921 and add `--cpus` flag for `docker run/create`. Basically, `--cpus` will allow user to specify a number (possibly partial) about how many CPUs the container will use. For example, on a 2-CPU system `--cpus 1.5` means the container will take 75% (1.5/2) of the CPU share. This fix adds a `NanoCPUs` field to `HostConfig` since swarmkit alreay have a concept of NanoCPUs for tasks. The `--cpus` flag will translate the number into reused `NanoCPUs` to be consistent. This fix adds integration tests to cover the changes. Related docs (`docker run` and Remote APIs) have been updated. This fix fixes 27921. Signed-off-by: Yong Tang Upstream-commit: 1cab3b32a68c0493843a0196fd50029553b160b6 Component: cli --- components/cli/command/service/opts.go | 32 ++------------------- components/cli/command/service/opts_test.go | 5 ++-- 2 files changed, 5 insertions(+), 32 deletions(-) diff --git a/components/cli/command/service/opts.go b/components/cli/command/service/opts.go index c89d40a767..2199e9f363 100644 --- a/components/cli/command/service/opts.go +++ b/components/cli/command/service/opts.go @@ -2,7 +2,6 @@ package service import ( "fmt" - "math/big" "strconv" "strings" "time" @@ -40,33 +39,6 @@ func (m *memBytes) Value() int64 { return int64(*m) } -type nanoCPUs int64 - -func (c *nanoCPUs) String() string { - return big.NewRat(c.Value(), 1e9).FloatString(3) -} - -func (c *nanoCPUs) Set(value string) error { - cpu, ok := new(big.Rat).SetString(value) - if !ok { - return fmt.Errorf("Failed to parse %v as a rational number", value) - } - nano := cpu.Mul(cpu, big.NewRat(1e9, 1)) - if !nano.IsInt() { - return fmt.Errorf("value is too precise") - } - *c = nanoCPUs(nano.Num().Int64()) - return nil -} - -func (c *nanoCPUs) Type() string { - return "NanoCPUs" -} - -func (c *nanoCPUs) Value() int64 { - return int64(*c) -} - // PositiveDurationOpt is an option type for time.Duration that uses a pointer. // It bahave similarly to DurationOpt but only allows positive duration values. type PositiveDurationOpt struct { @@ -156,9 +128,9 @@ type updateOptions struct { } type resourceOptions struct { - limitCPU nanoCPUs + limitCPU opts.NanoCPUs limitMemBytes memBytes - resCPU nanoCPUs + resCPU opts.NanoCPUs resMemBytes memBytes } diff --git a/components/cli/command/service/opts_test.go b/components/cli/command/service/opts_test.go index 26534cf0f5..aa2d999dcf 100644 --- a/components/cli/command/service/opts_test.go +++ b/components/cli/command/service/opts_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/docker/docker/api/types/container" + "github.com/docker/docker/opts" "github.com/docker/docker/pkg/testutil/assert" ) @@ -21,12 +22,12 @@ func TestMemBytesSetAndValue(t *testing.T) { } func TestNanoCPUsString(t *testing.T) { - var cpus nanoCPUs = 6100000000 + var cpus opts.NanoCPUs = 6100000000 assert.Equal(t, cpus.String(), "6.100") } func TestNanoCPUsSetAndValue(t *testing.T) { - var cpus nanoCPUs + var cpus opts.NanoCPUs assert.NilError(t, cpus.Set("0.35")) assert.Equal(t, cpus.Value(), int64(350000000)) }