hugetlb.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // +build linux
  2. package fs
  3. import (
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "github.com/opencontainers/runc/libcontainer/cgroups"
  8. "github.com/opencontainers/runc/libcontainer/configs"
  9. )
  10. type HugetlbGroup struct {
  11. }
  12. func (s *HugetlbGroup) Name() string {
  13. return "hugetlb"
  14. }
  15. func (s *HugetlbGroup) Apply(d *cgroupData) error {
  16. _, err := d.join("hugetlb")
  17. if err != nil && !cgroups.IsNotFound(err) {
  18. return err
  19. }
  20. return nil
  21. }
  22. func (s *HugetlbGroup) Set(path string, cgroup *configs.Cgroup) error {
  23. for _, hugetlb := range cgroup.Resources.HugetlbLimit {
  24. if err := writeFile(path, strings.Join([]string{"hugetlb", hugetlb.Pagesize, "limit_in_bytes"}, "."), strconv.FormatUint(hugetlb.Limit, 10)); err != nil {
  25. return err
  26. }
  27. }
  28. return nil
  29. }
  30. func (s *HugetlbGroup) Remove(d *cgroupData) error {
  31. return removePath(d.path("hugetlb"))
  32. }
  33. func (s *HugetlbGroup) GetStats(path string, stats *cgroups.Stats) error {
  34. hugetlbStats := cgroups.HugetlbStats{}
  35. for _, pageSize := range HugePageSizes {
  36. usage := strings.Join([]string{"hugetlb", pageSize, "usage_in_bytes"}, ".")
  37. value, err := getCgroupParamUint(path, usage)
  38. if err != nil {
  39. return fmt.Errorf("failed to parse %s - %v", usage, err)
  40. }
  41. hugetlbStats.Usage = value
  42. maxUsage := strings.Join([]string{"hugetlb", pageSize, "max_usage_in_bytes"}, ".")
  43. value, err = getCgroupParamUint(path, maxUsage)
  44. if err != nil {
  45. return fmt.Errorf("failed to parse %s - %v", maxUsage, err)
  46. }
  47. hugetlbStats.MaxUsage = value
  48. failcnt := strings.Join([]string{"hugetlb", pageSize, "failcnt"}, ".")
  49. value, err = getCgroupParamUint(path, failcnt)
  50. if err != nil {
  51. return fmt.Errorf("failed to parse %s - %v", failcnt, err)
  52. }
  53. hugetlbStats.Failcnt = value
  54. stats.HugetlbStats[pageSize] = hugetlbStats
  55. }
  56. return nil
  57. }