cgroups2.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (c) 2022 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. //go:build linux
  21. // +build linux
  22. package cgroups
  23. import (
  24. "bufio"
  25. "errors"
  26. "fmt"
  27. "io"
  28. "os"
  29. "path"
  30. "strconv"
  31. "strings"
  32. )
  33. const (
  34. // _cgroupv2CPUMax is the file name for the CGroup-V2 CPU max and period
  35. // parameter.
  36. _cgroupv2CPUMax = "cpu.max"
  37. // _cgroupFSType is the Linux CGroup-V2 file system type used in
  38. // `/proc/$PID/mountinfo`.
  39. _cgroupv2FSType = "cgroup2"
  40. _cgroupv2MountPoint = "/sys/fs/cgroup"
  41. _cgroupV2CPUMaxDefaultPeriod = 100000
  42. _cgroupV2CPUMaxQuotaMax = "max"
  43. )
  44. const (
  45. _cgroupv2CPUMaxQuotaIndex = iota
  46. _cgroupv2CPUMaxPeriodIndex
  47. )
  48. // ErrNotV2 indicates that the system is not using cgroups2.
  49. var ErrNotV2 = errors.New("not using cgroups2")
  50. // CGroups2 provides access to cgroups data for systems using cgroups2.
  51. type CGroups2 struct {
  52. mountPoint string
  53. groupPath string
  54. cpuMaxFile string
  55. }
  56. // NewCGroups2ForCurrentProcess builds a CGroups2 for the current process.
  57. //
  58. // This returns ErrNotV2 if the system is not using cgroups2.
  59. func NewCGroups2ForCurrentProcess() (*CGroups2, error) {
  60. return newCGroups2From(_procPathMountInfo, _procPathCGroup)
  61. }
  62. func newCGroups2From(mountInfoPath, procPathCGroup string) (*CGroups2, error) {
  63. isV2, err := isCGroupV2(mountInfoPath)
  64. if err != nil {
  65. return nil, err
  66. }
  67. if !isV2 {
  68. return nil, ErrNotV2
  69. }
  70. subsystems, err := parseCGroupSubsystems(procPathCGroup)
  71. if err != nil {
  72. return nil, err
  73. }
  74. // Find v2 subsystem by looking for the `0` id
  75. var v2subsys *CGroupSubsys
  76. for _, subsys := range subsystems {
  77. if subsys.ID == 0 {
  78. v2subsys = subsys
  79. break
  80. }
  81. }
  82. if v2subsys == nil {
  83. return nil, ErrNotV2
  84. }
  85. return &CGroups2{
  86. mountPoint: _cgroupv2MountPoint,
  87. groupPath: v2subsys.Name,
  88. cpuMaxFile: _cgroupv2CPUMax,
  89. }, nil
  90. }
  91. func isCGroupV2(procPathMountInfo string) (bool, error) {
  92. var (
  93. isV2 bool
  94. newMountPoint = func(mp *MountPoint) error {
  95. isV2 = isV2 || (mp.FSType == _cgroupv2FSType && mp.MountPoint == _cgroupv2MountPoint)
  96. return nil
  97. }
  98. )
  99. if err := parseMountInfo(procPathMountInfo, newMountPoint); err != nil {
  100. return false, err
  101. }
  102. return isV2, nil
  103. }
  104. // CPUQuota returns the CPU quota applied with the CPU cgroup2 controller.
  105. // It is a result of reading cpu quota and period from cpu.max file.
  106. // It will return `cpu.max / cpu.period`. If cpu.max is set to max, it returns
  107. // (-1, false, nil)
  108. func (cg *CGroups2) CPUQuota() (float64, bool, error) {
  109. cpuMaxParams, err := os.Open(path.Join(cg.mountPoint, cg.groupPath, cg.cpuMaxFile))
  110. if err != nil {
  111. if os.IsNotExist(err) {
  112. return -1, false, nil
  113. }
  114. return -1, false, err
  115. }
  116. defer cpuMaxParams.Close()
  117. scanner := bufio.NewScanner(cpuMaxParams)
  118. if scanner.Scan() {
  119. fields := strings.Fields(scanner.Text())
  120. if len(fields) == 0 || len(fields) > 2 {
  121. return -1, false, fmt.Errorf("invalid format")
  122. }
  123. if fields[_cgroupv2CPUMaxQuotaIndex] == _cgroupV2CPUMaxQuotaMax {
  124. return -1, false, nil
  125. }
  126. max, err := strconv.Atoi(fields[_cgroupv2CPUMaxQuotaIndex])
  127. if err != nil {
  128. return -1, false, err
  129. }
  130. var period int
  131. if len(fields) == 1 {
  132. period = _cgroupV2CPUMaxDefaultPeriod
  133. } else {
  134. period, err = strconv.Atoi(fields[_cgroupv2CPUMaxPeriodIndex])
  135. if err != nil {
  136. return -1, false, err
  137. }
  138. if period == 0 {
  139. return -1, false, errors.New("zero value for period is not allowed")
  140. }
  141. }
  142. return float64(max) / float64(period), true, nil
  143. }
  144. if err := scanner.Err(); err != nil {
  145. return -1, false, err
  146. }
  147. return 0, false, io.ErrUnexpectedEOF
  148. }