From 7f37afa8924f081f045fb3c0f599053ffdd2c977 Mon Sep 17 00:00:00 2001 From: Boris Popovschi Date: Tue, 25 Feb 2020 14:50:55 +0200 Subject: [PATCH] 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 +}