rdma.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "math"
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
  21. specs "github.com/opencontainers/runtime-spec/specs-go"
  22. )
  23. type rdmaController struct {
  24. root string
  25. }
  26. func (p *rdmaController) Name() Name {
  27. return Rdma
  28. }
  29. func (p *rdmaController) Path(path string) string {
  30. return filepath.Join(p.root, path)
  31. }
  32. func NewRdma(root string) *rdmaController {
  33. return &rdmaController{
  34. root: filepath.Join(root, string(Rdma)),
  35. }
  36. }
  37. func createCmdString(device string, limits *specs.LinuxRdma) string {
  38. var cmdString string
  39. cmdString = device
  40. if limits.HcaHandles != nil {
  41. cmdString = cmdString + " " + "hca_handle=" + strconv.FormatUint(uint64(*limits.HcaHandles), 10)
  42. }
  43. if limits.HcaObjects != nil {
  44. cmdString = cmdString + " " + "hca_object=" + strconv.FormatUint(uint64(*limits.HcaObjects), 10)
  45. }
  46. return cmdString
  47. }
  48. func (p *rdmaController) Create(path string, resources *specs.LinuxResources) error {
  49. if err := os.MkdirAll(p.Path(path), defaultDirPerm); err != nil {
  50. return err
  51. }
  52. for device, limit := range resources.Rdma {
  53. if device != "" && (limit.HcaHandles != nil || limit.HcaObjects != nil) {
  54. limit := limit
  55. return os.WriteFile(
  56. filepath.Join(p.Path(path), "rdma.max"),
  57. []byte(createCmdString(device, &limit)),
  58. defaultFilePerm,
  59. )
  60. }
  61. }
  62. return nil
  63. }
  64. func (p *rdmaController) Update(path string, resources *specs.LinuxResources) error {
  65. return p.Create(path, resources)
  66. }
  67. func parseRdmaKV(raw string, entry *v1.RdmaEntry) {
  68. var value uint64
  69. var err error
  70. parts := strings.Split(raw, "=")
  71. switch len(parts) {
  72. case 2:
  73. if parts[1] == "max" {
  74. value = math.MaxUint32
  75. } else {
  76. value, err = parseUint(parts[1], 10, 32)
  77. if err != nil {
  78. return
  79. }
  80. }
  81. if parts[0] == "hca_handle" {
  82. entry.HcaHandles = uint32(value)
  83. } else if parts[0] == "hca_object" {
  84. entry.HcaObjects = uint32(value)
  85. }
  86. }
  87. }
  88. func toRdmaEntry(strEntries []string) []*v1.RdmaEntry {
  89. var rdmaEntries []*v1.RdmaEntry
  90. for i := range strEntries {
  91. parts := strings.Fields(strEntries[i])
  92. switch len(parts) {
  93. case 3:
  94. entry := new(v1.RdmaEntry)
  95. entry.Device = parts[0]
  96. parseRdmaKV(parts[1], entry)
  97. parseRdmaKV(parts[2], entry)
  98. rdmaEntries = append(rdmaEntries, entry)
  99. default:
  100. continue
  101. }
  102. }
  103. return rdmaEntries
  104. }
  105. func (p *rdmaController) Stat(path string, stats *v1.Metrics) error {
  106. currentData, err := os.ReadFile(filepath.Join(p.Path(path), "rdma.current"))
  107. if err != nil {
  108. return err
  109. }
  110. currentPerDevices := strings.Split(string(currentData), "\n")
  111. maxData, err := os.ReadFile(filepath.Join(p.Path(path), "rdma.max"))
  112. if err != nil {
  113. return err
  114. }
  115. maxPerDevices := strings.Split(string(maxData), "\n")
  116. // If device got removed between reading two files, ignore returning
  117. // stats.
  118. if len(currentPerDevices) != len(maxPerDevices) {
  119. return nil
  120. }
  121. currentEntries := toRdmaEntry(currentPerDevices)
  122. maxEntries := toRdmaEntry(maxPerDevices)
  123. stats.Rdma = &v1.RdmaStat{
  124. Current: currentEntries,
  125. Limit: maxEntries,
  126. }
  127. return nil
  128. }