Skip to content

Commit

Permalink
Merge pull request #644 from cyphar/fix-pids-max-unlimited
Browse files Browse the repository at this point in the history
libcontainer: cgroups: deal with unlimited case for pids.max
  • Loading branch information
Mrunal Patel committed Mar 21, 2016
2 parents 4856ed1 + a6d5179 commit 4d79292
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
14 changes: 12 additions & 2 deletions libcontainer/cgroups/fs/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package fs

import (
"fmt"
"path/filepath"
"strconv"

"github.com/opencontainers/runc/libcontainer/cgroups"
Expand Down Expand Up @@ -52,12 +53,21 @@ func (s *PidsGroup) GetStats(path string, stats *cgroups.Stats) error {
return fmt.Errorf("failed to parse pids.current - %s", err)
}

max, err := getCgroupParamUint(path, "pids.max")
maxString, err := getCgroupParamString(path, "pids.max")
if err != nil {
return fmt.Errorf("failed to parse pids.max - %s", err)
}

// Default if pids.max == "max" is 0 -- which represents "no limit".
var max uint64
if maxString != "max" {
max, err = parseUint(maxString, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse pids.max - unable to parse %q as a uint from Cgroup file %q", maxString, filepath.Join(path, "pids.max"))
}
}

stats.PidsStats.Current = current
stats.PidsStats.Max = max
stats.PidsStats.Limit = max
return nil
}
28 changes: 26 additions & 2 deletions libcontainer/cgroups/fs/pids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,31 @@ func TestPidsStats(t *testing.T) {
t.Fatalf("Expected %d, got %d for pids.current", 1337, stats.PidsStats.Current)
}

if stats.PidsStats.Max != maxLimited {
t.Fatalf("Expected %d, got %d for pids.max", maxLimited, stats.PidsStats.Max)
if stats.PidsStats.Limit != maxLimited {
t.Fatalf("Expected %d, got %d for pids.max", maxLimited, stats.PidsStats.Limit)
}
}

func TestPidsStatsUnlimited(t *testing.T) {
helper := NewCgroupTestUtil("pids", t)
defer helper.cleanup()

helper.writeFileContents(map[string]string{
"pids.current": strconv.Itoa(4096),
"pids.max": "max",
})

pids := &PidsGroup{}
stats := *cgroups.NewStats()
if err := pids.GetStats(helper.CgroupPath, &stats); err != nil {
t.Fatal(err)
}

if stats.PidsStats.Current != 4096 {
t.Fatalf("Expected %d, got %d for pids.current", 4096, stats.PidsStats.Current)
}

if stats.PidsStats.Limit != 0 {
t.Fatalf("Expected %d, got %d for pids.max", 0, stats.PidsStats.Limit)
}
}
2 changes: 1 addition & 1 deletion libcontainer/cgroups/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type PidsStats struct {
// number of pids in the cgroup
Current uint64 `json:"current,omitempty"`
// active pids hard limit
Max uint64 `json:"max,omitempty"`
Limit uint64 `json:"limit,omitempty"`
}

type BlkioStatEntry struct {
Expand Down

0 comments on commit 4d79292

Please sign in to comment.