rdma.go 3.5 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 cgroups
  14. import (
  15. "io/ioutil"
  16. "math"
  17. "os"
  18. "path/filepath"
  19. "strconv"
  20. "strings"
  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. return ioutil.WriteFile(
  55. filepath.Join(p.Path(path), "rdma.max"),
  56. []byte(createCmdString(device, &limit)),
  57. defaultFilePerm,
  58. )
  59. }
  60. }
  61. return nil
  62. }
  63. func (p *rdmaController) Update(path string, resources *specs.LinuxResources) error {
  64. return p.Create(path, resources)
  65. }
  66. func parseRdmaKV(raw string, entry *RdmaEntry) {
  67. var value uint64
  68. var err error
  69. parts := strings.Split(raw, "=")
  70. switch len(parts) {
  71. case 2:
  72. if parts[1] == "max" {
  73. value = math.MaxUint32
  74. } else {
  75. value, err = parseUint(parts[1], 10, 32)
  76. if err != nil {
  77. return
  78. }
  79. }
  80. if parts[0] == "hca_handle" {
  81. entry.HcaHandles = uint32(value)
  82. } else if parts[0] == "hca_object" {
  83. entry.HcaObjects = uint32(value)
  84. }
  85. }
  86. }
  87. func toRdmaEntry(strEntries []string) []*RdmaEntry {
  88. var rdmaEntries []*RdmaEntry
  89. for i := range strEntries {
  90. parts := strings.Fields(strEntries[i])
  91. switch len(parts) {
  92. case 3:
  93. entry := new(RdmaEntry)
  94. entry.Device = parts[0]
  95. parseRdmaKV(parts[1], entry)
  96. parseRdmaKV(parts[2], entry)
  97. rdmaEntries = append(rdmaEntries, entry)
  98. default:
  99. continue
  100. }
  101. }
  102. return rdmaEntries
  103. }
  104. func (p *rdmaController) Stat(path string, stats *Metrics) error {
  105. currentData, err := ioutil.ReadFile(filepath.Join(p.Path(path), "rdma.current"))
  106. if err != nil {
  107. return err
  108. }
  109. currentPerDevices := strings.Split(string(currentData), "\n")
  110. maxData, err := ioutil.ReadFile(filepath.Join(p.Path(path), "rdma.max"))
  111. if err != nil {
  112. return err
  113. }
  114. maxPerDevices := strings.Split(string(maxData), "\n")
  115. // If device got removed between reading two files, ignore returning
  116. // stats.
  117. if len(currentPerDevices) != len(maxPerDevices) {
  118. return nil
  119. }
  120. currentEntries := toRdmaEntry(currentPerDevices)
  121. maxEntries := toRdmaEntry(maxPerDevices)
  122. stats.Rdma = &RdmaStat{
  123. Current: currentEntries,
  124. Limit: maxEntries,
  125. }
  126. return nil
  127. }