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 <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2016-11-01 10:12:29 -07:00 committed by Vincent Demeester
parent 3375ba2acd
commit 17e9503bbb
1 changed files with 33 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package opts
import ( import (
"fmt" "fmt"
"math/big"
"net" "net"
"regexp" "regexp"
"strings" "strings"
@ -319,3 +320,35 @@ func (o *FilterOpt) Type() string {
func (o *FilterOpt) Value() filters.Args { func (o *FilterOpt) Value() filters.Args {
return o.filter return o.filter
} }
// NanoCPUs is a type for fixed point fractional number.
type NanoCPUs int64
// String returns the string format of the number
func (c *NanoCPUs) String() string {
return big.NewRat(c.Value(), 1e9).FloatString(3)
}
// Set sets the value of the NanoCPU by passing a string
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
}
// Type returns the type
func (c *NanoCPUs) Type() string {
return "NanoCPUs"
}
// Value returns the value in int64
func (c *NanoCPUs) Value() int64 {
return int64(*c)
}