2019-01-12 04:58:27 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2020-05-04 20:46:48 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
2019-01-12 04:58:27 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
)
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
type HugetlbGroup struct{}
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
func (s *HugetlbGroup) Name() string {
|
|
|
|
return "hugetlb"
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (s *HugetlbGroup) Apply(path string, d *cgroupData) error {
|
|
|
|
return join(path, d.pid)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 20:46:09 +00:00
|
|
|
func (s *HugetlbGroup) Set(path string, r *configs.Resources) error {
|
|
|
|
for _, hugetlb := range r.HugetlbLimit {
|
2021-07-02 08:43:15 +00:00
|
|
|
if err := cgroups.WriteFile(path, "hugetlb."+hugetlb.Pagesize+".limit_in_bytes", strconv.FormatUint(hugetlb.Limit, 10)); err != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HugetlbGroup) GetStats(path string, stats *cgroups.Stats) error {
|
|
|
|
hugetlbStats := cgroups.HugetlbStats{}
|
2020-08-10 17:43:49 +00:00
|
|
|
if !cgroups.PathExists(path) {
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
for _, pageSize := range HugePageSizes {
|
2021-04-14 18:11:13 +00:00
|
|
|
usage := "hugetlb." + pageSize + ".usage_in_bytes"
|
2020-05-04 20:46:48 +00:00
|
|
|
value, err := fscommon.GetCgroupParamUint(path, usage)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse %s - %v", usage, err)
|
|
|
|
}
|
|
|
|
hugetlbStats.Usage = value
|
|
|
|
|
2021-04-14 18:11:13 +00:00
|
|
|
maxUsage := "hugetlb." + pageSize + ".max_usage_in_bytes"
|
2020-05-04 20:46:48 +00:00
|
|
|
value, err = fscommon.GetCgroupParamUint(path, maxUsage)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse %s - %v", maxUsage, err)
|
|
|
|
}
|
|
|
|
hugetlbStats.MaxUsage = value
|
|
|
|
|
2021-04-14 18:11:13 +00:00
|
|
|
failcnt := "hugetlb." + pageSize + ".failcnt"
|
2020-05-04 20:46:48 +00:00
|
|
|
value, err = fscommon.GetCgroupParamUint(path, failcnt)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse %s - %v", failcnt, err)
|
|
|
|
}
|
|
|
|
hugetlbStats.Failcnt = value
|
|
|
|
|
|
|
|
stats.HugetlbStats[pageSize] = hugetlbStats
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|