Added HugeTlb controller for cgroupv2

Signed-off-by: Boris Popovschi <zyqsempai@mail.ru>
This commit is contained in:
Boris Popovschi 2020-02-25 14:50:55 +02:00
parent 688cf6d43c
commit 7f37afa892
2 changed files with 58 additions and 0 deletions

View File

@ -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)

View File

@ -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
}