diff --git a/container/docker/handler.go b/container/docker/handler.go index b9d7c1f6..a5c49e91 100644 --- a/container/docker/handler.go +++ b/container/docker/handler.go @@ -227,12 +227,7 @@ func libcontainerConfigToContainerSpec(config *libcontainer.Config, mi *info.Mac if config.Cgroups.CpuShares != 0 { spec.Cpu.Limit = uint64(config.Cgroups.CpuShares) } - if config.Cgroups.CpusetCpus == "" { - // All cores are active. - spec.Cpu.Mask = fmt.Sprintf("0-%d", mi.NumCores-1) - } else { - spec.Cpu.Mask = config.Cgroups.CpusetCpus - } + spec.Cpu.Mask = utils.FixCpuMask(config.Cgroups.CpusetCpus, mi.NumCores) spec.HasNetwork = true spec.HasDiskIo = true diff --git a/container/raw/handler.go b/container/raw/handler.go index 5edfb9b4..155dc95b 100644 --- a/container/raw/handler.go +++ b/container/raw/handler.go @@ -217,10 +217,8 @@ func (self *rawContainerHandler) GetSpec() (info.ContainerSpec, error) { if ok { if utils.FileExists(cpusetRoot) { spec.HasCpu = true - spec.Cpu.Mask = readString(cpusetRoot, "cpuset.cpus") - if spec.Cpu.Mask == "" { - spec.Cpu.Mask = fmt.Sprintf("0-%d", mi.NumCores-1) - } + mask := readString(cpusetRoot, "cpuset.cpus") + spec.Cpu.Mask = utils.FixCpuMask(mask, mi.NumCores) } } diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 00000000..9458a4c5 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,29 @@ +// Copyright 2015 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package utils + +import "fmt" + +// Returns a mask of all cores on the machine if the passed-in mask is empty. +func FixCpuMask(mask string, cores int) string { + if mask == "" { + if cores > 1 { + mask = fmt.Sprintf("0-%d", cores-1) + } else { + mask = "0" + } + } + return mask +}