hugetlb.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package fs
  2. import (
  3. "strconv"
  4. "github.com/opencontainers/runc/libcontainer/cgroups"
  5. "github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
  6. "github.com/opencontainers/runc/libcontainer/configs"
  7. )
  8. type HugetlbGroup struct{}
  9. func (s *HugetlbGroup) Name() string {
  10. return "hugetlb"
  11. }
  12. func (s *HugetlbGroup) Apply(path string, _ *configs.Resources, pid int) error {
  13. return apply(path, pid)
  14. }
  15. func (s *HugetlbGroup) Set(path string, r *configs.Resources) error {
  16. for _, hugetlb := range r.HugetlbLimit {
  17. if err := cgroups.WriteFile(path, "hugetlb."+hugetlb.Pagesize+".limit_in_bytes", strconv.FormatUint(hugetlb.Limit, 10)); err != nil {
  18. return err
  19. }
  20. }
  21. return nil
  22. }
  23. func (s *HugetlbGroup) GetStats(path string, stats *cgroups.Stats) error {
  24. if !cgroups.PathExists(path) {
  25. return nil
  26. }
  27. hugetlbStats := cgroups.HugetlbStats{}
  28. for _, pageSize := range cgroups.HugePageSizes() {
  29. usage := "hugetlb." + pageSize + ".usage_in_bytes"
  30. value, err := fscommon.GetCgroupParamUint(path, usage)
  31. if err != nil {
  32. return err
  33. }
  34. hugetlbStats.Usage = value
  35. maxUsage := "hugetlb." + pageSize + ".max_usage_in_bytes"
  36. value, err = fscommon.GetCgroupParamUint(path, maxUsage)
  37. if err != nil {
  38. return err
  39. }
  40. hugetlbStats.MaxUsage = value
  41. failcnt := "hugetlb." + pageSize + ".failcnt"
  42. value, err = fscommon.GetCgroupParamUint(path, failcnt)
  43. if err != nil {
  44. return err
  45. }
  46. hugetlbStats.Failcnt = value
  47. stats.HugetlbStats[pageSize] = hugetlbStats
  48. }
  49. return nil
  50. }