Skip to content

Commit

Permalink
Cgroups: check for cpuset.cpus before set the value
Browse files Browse the repository at this point in the history
Assume we put some CPUs offline and then bring them online again, since
there is not a mechanism that to help docker to refresh the state of CPUs,
all the following containers will never use those off-and-on CPUs again.

So we should always check whether there has already a change of CPUs before
we set it to the container.

Signed-off-by: Hu Keping <hukeping@huawei.com>
  • Loading branch information
HuKeping committed Sep 8, 2015
1 parent 0f85e4e commit 26547ec
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions libcontainer/cgroups/fs/cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func (s *CpusetGroup) Apply(d *data) error {

func (s *CpusetGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.CpusetCpus != "" {
// We should check whether there has already a change of CPUs before we specify it
if err := s.amend(path, cgroup); err != nil {
return err
}

if err := writeFile(path, "cpuset.cpus", cgroup.CpusetCpus); err != nil {
return err
}
Expand Down Expand Up @@ -133,3 +138,25 @@ func (s *CpusetGroup) copyIfNeeded(current, parent string) error {
func (s *CpusetGroup) isEmpty(b []byte) bool {
return len(bytes.Trim(b, "\n")) == 0
}

func (s *CpusetGroup) amend(path string, cgroup *configs.Cgroup) error {

var newCPUs string
var err error

parentDir := filepath.Dir(path)
grandParentDir := filepath.Dir(parentDir)

// Generally it should be /sys/fs/cgroup/cpuset
if newCPUs, err = readFile(grandParentDir, "cpuset.cpus"); err != nil {
return err
}
if newCPUs != cgroup.CpusetCpus {
// Generally it should be /sys/fs/cgroup/cpuset/docker
if err := writeFile(parentDir, "cpuset.cpus", newCPUs); err != nil {
return err
}
}

return nil
}

0 comments on commit 26547ec

Please sign in to comment.