hugetlb.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroup1
  14. import (
  15. "os"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
  20. specs "github.com/opencontainers/runtime-spec/specs-go"
  21. )
  22. func NewHugetlb(root string) (*hugetlbController, error) {
  23. sizes, err := hugePageSizes()
  24. if err != nil {
  25. return nil, err
  26. }
  27. return &hugetlbController{
  28. root: filepath.Join(root, string(Hugetlb)),
  29. sizes: sizes,
  30. }, nil
  31. }
  32. type hugetlbController struct {
  33. root string
  34. sizes []string
  35. }
  36. func (h *hugetlbController) Name() Name {
  37. return Hugetlb
  38. }
  39. func (h *hugetlbController) Path(path string) string {
  40. return filepath.Join(h.root, path)
  41. }
  42. func (h *hugetlbController) Create(path string, resources *specs.LinuxResources) error {
  43. if err := os.MkdirAll(h.Path(path), defaultDirPerm); err != nil {
  44. return err
  45. }
  46. for _, limit := range resources.HugepageLimits {
  47. if err := os.WriteFile(
  48. filepath.Join(h.Path(path), strings.Join([]string{"hugetlb", limit.Pagesize, "limit_in_bytes"}, ".")),
  49. []byte(strconv.FormatUint(limit.Limit, 10)),
  50. defaultFilePerm,
  51. ); err != nil {
  52. return err
  53. }
  54. }
  55. return nil
  56. }
  57. func (h *hugetlbController) Stat(path string, stats *v1.Metrics) error {
  58. for _, size := range h.sizes {
  59. s, err := h.readSizeStat(path, size)
  60. if err != nil {
  61. return err
  62. }
  63. stats.Hugetlb = append(stats.Hugetlb, s)
  64. }
  65. return nil
  66. }
  67. func (h *hugetlbController) readSizeStat(path, size string) (*v1.HugetlbStat, error) {
  68. s := v1.HugetlbStat{
  69. Pagesize: size,
  70. }
  71. for _, t := range []struct {
  72. name string
  73. value *uint64
  74. }{
  75. {
  76. name: "usage_in_bytes",
  77. value: &s.Usage,
  78. },
  79. {
  80. name: "max_usage_in_bytes",
  81. value: &s.Max,
  82. },
  83. {
  84. name: "failcnt",
  85. value: &s.Failcnt,
  86. },
  87. } {
  88. v, err := readUint(filepath.Join(h.Path(path), strings.Join([]string{"hugetlb", size, t.name}, ".")))
  89. if err != nil {
  90. return nil, err
  91. }
  92. *t.value = v
  93. }
  94. return &s, nil
  95. }