From 7f37afa8924f081f045fb3c0f599053ffdd2c977 Mon Sep 17 00:00:00 2001 From: Boris Popovschi Date: Tue, 25 Feb 2020 14:50:55 +0200 Subject: [PATCH 1/3] Added HugeTlb controller for cgroupv2 Signed-off-by: Boris Popovschi --- libcontainer/cgroups/fs2/fs2.go | 12 ++++++++ libcontainer/cgroups/fs2/hugetlb.go | 46 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 libcontainer/cgroups/fs2/hugetlb.go diff --git a/libcontainer/cgroups/fs2/fs2.go b/libcontainer/cgroups/fs2/fs2.go index 4bb7091a..069ff235 100644 --- a/libcontainer/cgroups/fs2/fs2.go +++ b/libcontainer/cgroups/fs2/fs2.go @@ -124,6 +124,12 @@ func (m *manager) GetStats() (*cgroups.Stats, error) { errs = append(errs, err) } } + // hugetlb (since kernel 5.6) + if _, ok := m.controllers["hugetlb"]; ok { + if err := statHugeTlb(m.dirPath, &st, m.config); err != nil { + errs = append(errs, err) + } + } if len(errs) > 0 && !m.rootless { return &st, errors.Errorf("error while statting cgroup v2: %+v", errs) } @@ -198,6 +204,12 @@ func (m *manager) Set(container *configs.Config) error { errs = append(errs, err) } } + // hugetlb (since kernel 5.6) + if _, ok := m.controllers["hugetlb"]; ok { + if err := setHugeTlb(m.dirPath, container.Cgroups); err != nil { + errs = append(errs, err) + } + } // freezer (since kernel 5.2, pseudo-controller) if err := setFreezer(m.dirPath, container.Cgroups.Freezer); err != nil { errs = append(errs, err) diff --git a/libcontainer/cgroups/fs2/hugetlb.go b/libcontainer/cgroups/fs2/hugetlb.go new file mode 100644 index 00000000..ed51592c --- /dev/null +++ b/libcontainer/cgroups/fs2/hugetlb.go @@ -0,0 +1,46 @@ +// +build linux + +package fs2 + +import ( + "fmt" + "strconv" + "strings" + + "github.com/opencontainers/runc/libcontainer/cgroups" + "github.com/opencontainers/runc/libcontainer/cgroups/fscommon" + "github.com/opencontainers/runc/libcontainer/configs" +) + +func setHugeTlb(dirPath string, cgroup *configs.Cgroup) error { + for _, hugetlb := range cgroup.Resources.HugetlbLimit { + if err := fscommon.WriteFile(dirPath, strings.Join([]string{"hugetlb", hugetlb.Pagesize, "max"}, "."), strconv.FormatUint(hugetlb.Limit, 10)); err != nil { + return err + } + } + + return nil +} + +func statHugeTlb(dirPath string, stats *cgroups.Stats, cgroup *configs.Cgroup) error { + hugetlbStats := cgroups.HugetlbStats{} + for _, entry := range cgroup.Resources.HugetlbLimit { + max := strings.Join([]string{"hugetlb", entry.Pagesize, "max"}, ".") + value, err := fscommon.GetCgroupParamUint(dirPath, max) + if err != nil { + return fmt.Errorf("failed to parse %s - %v", max, err) + } + hugetlbStats.Usage = value + + usage := strings.Join([]string{"hugetlb", entry.Pagesize, "current"}, ".") + value, err = fscommon.GetCgroupParamUint(dirPath, usage) + if err != nil { + return fmt.Errorf("failed to parse %s - %v", usage, err) + } + hugetlbStats.Usage = value + + stats.HugetlbStats[entry.Pagesize] = hugetlbStats + } + + return nil +} From d804611d053c9e03ae4775eb3e4b3ec0a68d9fa0 Mon Sep 17 00:00:00 2001 From: Boris Popovschi Date: Tue, 10 Mar 2020 15:19:44 +0200 Subject: [PATCH 2/3] Added failcnt stats Signed-off-by: Boris Popovschi --- libcontainer/cgroups/fs2/hugetlb.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/libcontainer/cgroups/fs2/hugetlb.go b/libcontainer/cgroups/fs2/hugetlb.go index ed51592c..7975b10d 100644 --- a/libcontainer/cgroups/fs2/hugetlb.go +++ b/libcontainer/cgroups/fs2/hugetlb.go @@ -3,10 +3,13 @@ package fs2 import ( - "fmt" + "io/ioutil" + "path/filepath" "strconv" "strings" + "github.com/pkg/errors" + "github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups/fscommon" "github.com/opencontainers/runc/libcontainer/configs" @@ -24,20 +27,26 @@ func setHugeTlb(dirPath string, cgroup *configs.Cgroup) error { func statHugeTlb(dirPath string, stats *cgroups.Stats, cgroup *configs.Cgroup) error { hugetlbStats := cgroups.HugetlbStats{} + for _, entry := range cgroup.Resources.HugetlbLimit { - max := strings.Join([]string{"hugetlb", entry.Pagesize, "max"}, ".") - value, err := fscommon.GetCgroupParamUint(dirPath, max) + usage := strings.Join([]string{"hugetlb", entry.Pagesize, "current"}, ".") + value, err := fscommon.GetCgroupParamUint(dirPath, usage) if err != nil { - return fmt.Errorf("failed to parse %s - %v", max, err) + return errors.Wrapf(err, "failed to parse hugetlb.%s.current file", entry.Pagesize) } hugetlbStats.Usage = value - usage := strings.Join([]string{"hugetlb", entry.Pagesize, "current"}, ".") - value, err = fscommon.GetCgroupParamUint(dirPath, usage) + fileName := strings.Join([]string{"hugetlb", entry.Pagesize, "events"}, ".") + filePath := filepath.Join(dirPath, fileName) + contents, err := ioutil.ReadFile(filePath) if err != nil { - return fmt.Errorf("failed to parse %s - %v", usage, err) + return errors.Wrapf(err, "failed to parse hugetlb.%s.events file", entry.Pagesize) } - hugetlbStats.Usage = value + _, value, err = fscommon.GetCgroupParamKeyValue(string(contents)) + if err != nil { + return errors.Wrapf(err, "failed to parse hugetlb.%s.events file", entry.Pagesize) + } + hugetlbStats.Failcnt = value stats.HugetlbStats[entry.Pagesize] = hugetlbStats } From 89a87adb38c7d53b80eb0e5e6bc44ae2e70018a5 Mon Sep 17 00:00:00 2001 From: Boris Popovschi Date: Tue, 10 Mar 2020 15:28:45 +0200 Subject: [PATCH 3/3] Changed hugetlb pagesizes info source Signed-off-by: Boris Popovschi --- libcontainer/cgroups/fs2/fs2.go | 2 +- libcontainer/cgroups/fs2/hugetlb.go | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/libcontainer/cgroups/fs2/fs2.go b/libcontainer/cgroups/fs2/fs2.go index 069ff235..76ec3c0c 100644 --- a/libcontainer/cgroups/fs2/fs2.go +++ b/libcontainer/cgroups/fs2/fs2.go @@ -126,7 +126,7 @@ func (m *manager) GetStats() (*cgroups.Stats, error) { } // hugetlb (since kernel 5.6) if _, ok := m.controllers["hugetlb"]; ok { - if err := statHugeTlb(m.dirPath, &st, m.config); err != nil { + if err := statHugeTlb(m.dirPath, &st); err != nil { errs = append(errs, err) } } diff --git a/libcontainer/cgroups/fs2/hugetlb.go b/libcontainer/cgroups/fs2/hugetlb.go index 7975b10d..c7610c0a 100644 --- a/libcontainer/cgroups/fs2/hugetlb.go +++ b/libcontainer/cgroups/fs2/hugetlb.go @@ -25,30 +25,34 @@ func setHugeTlb(dirPath string, cgroup *configs.Cgroup) error { return nil } -func statHugeTlb(dirPath string, stats *cgroups.Stats, cgroup *configs.Cgroup) error { +func statHugeTlb(dirPath string, stats *cgroups.Stats) error { + hugePageSizes, err := cgroups.GetHugePageSize() + if err != nil { + return errors.Wrap(err, "failed to fetch hugetlb info") + } hugetlbStats := cgroups.HugetlbStats{} - for _, entry := range cgroup.Resources.HugetlbLimit { - usage := strings.Join([]string{"hugetlb", entry.Pagesize, "current"}, ".") + for _, pagesize := range hugePageSizes { + usage := strings.Join([]string{"hugetlb", pagesize, "current"}, ".") value, err := fscommon.GetCgroupParamUint(dirPath, usage) if err != nil { - return errors.Wrapf(err, "failed to parse hugetlb.%s.current file", entry.Pagesize) + return errors.Wrapf(err, "failed to parse hugetlb.%s.current file", pagesize) } hugetlbStats.Usage = value - fileName := strings.Join([]string{"hugetlb", entry.Pagesize, "events"}, ".") + fileName := strings.Join([]string{"hugetlb", pagesize, "events"}, ".") filePath := filepath.Join(dirPath, fileName) contents, err := ioutil.ReadFile(filePath) if err != nil { - return errors.Wrapf(err, "failed to parse hugetlb.%s.events file", entry.Pagesize) + return errors.Wrapf(err, "failed to parse hugetlb.%s.events file", pagesize) } _, value, err = fscommon.GetCgroupParamKeyValue(string(contents)) if err != nil { - return errors.Wrapf(err, "failed to parse hugetlb.%s.events file", entry.Pagesize) + return errors.Wrapf(err, "failed to parse hugetlb.%s.events file", pagesize) } hugetlbStats.Failcnt = value - stats.HugetlbStats[entry.Pagesize] = hugetlbStats + stats.HugetlbStats[pagesize] = hugetlbStats } return nil