cgroups.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2017 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. const (
  24. // _cgroupFSType is the Linux CGroup file system type used in
  25. // `/proc/$PID/mountinfo`.
  26. _cgroupFSType = "cgroup"
  27. // _cgroupSubsysCPU is the CPU CGroup subsystem.
  28. _cgroupSubsysCPU = "cpu"
  29. // _cgroupSubsysCPUAcct is the CPU accounting CGroup subsystem.
  30. _cgroupSubsysCPUAcct = "cpuacct"
  31. // _cgroupSubsysCPUSet is the CPUSet CGroup subsystem.
  32. _cgroupSubsysCPUSet = "cpuset"
  33. // _cgroupSubsysMemory is the Memory CGroup subsystem.
  34. _cgroupSubsysMemory = "memory"
  35. // _cgroupCPUCFSQuotaUsParam is the file name for the CGroup CFS quota
  36. // parameter.
  37. _cgroupCPUCFSQuotaUsParam = "cpu.cfs_quota_us"
  38. // _cgroupCPUCFSPeriodUsParam is the file name for the CGroup CFS period
  39. // parameter.
  40. _cgroupCPUCFSPeriodUsParam = "cpu.cfs_period_us"
  41. )
  42. const (
  43. _procPathCGroup = "/proc/self/cgroup"
  44. _procPathMountInfo = "/proc/self/mountinfo"
  45. )
  46. // CGroups is a map that associates each CGroup with its subsystem name.
  47. type CGroups map[string]*CGroup
  48. // NewCGroups returns a new *CGroups from given `mountinfo` and `cgroup` files
  49. // under for some process under `/proc` file system (see also proc(5) for more
  50. // information).
  51. func NewCGroups(procPathMountInfo, procPathCGroup string) (CGroups, error) {
  52. cgroupSubsystems, err := parseCGroupSubsystems(procPathCGroup)
  53. if err != nil {
  54. return nil, err
  55. }
  56. cgroups := make(CGroups)
  57. newMountPoint := func(mp *MountPoint) error {
  58. if mp.FSType != _cgroupFSType {
  59. return nil
  60. }
  61. for _, opt := range mp.SuperOptions {
  62. subsys, exists := cgroupSubsystems[opt]
  63. if !exists {
  64. continue
  65. }
  66. cgroupPath, err := mp.Translate(subsys.Name)
  67. if err != nil {
  68. return err
  69. }
  70. cgroups[opt] = NewCGroup(cgroupPath)
  71. }
  72. return nil
  73. }
  74. if err := parseMountInfo(procPathMountInfo, newMountPoint); err != nil {
  75. return nil, err
  76. }
  77. return cgroups, nil
  78. }
  79. // NewCGroupsForCurrentProcess returns a new *CGroups instance for the current
  80. // process.
  81. func NewCGroupsForCurrentProcess() (CGroups, error) {
  82. return NewCGroups(_procPathMountInfo, _procPathCGroup)
  83. }
  84. // CPUQuota returns the CPU quota applied with the CPU cgroup controller.
  85. // It is a result of `cpu.cfs_quota_us / cpu.cfs_period_us`. If the value of
  86. // `cpu.cfs_quota_us` was not set (-1), the method returns `(-1, nil)`.
  87. func (cg CGroups) CPUQuota() (float64, bool, error) {
  88. cpuCGroup, exists := cg[_cgroupSubsysCPU]
  89. if !exists {
  90. return -1, false, nil
  91. }
  92. cfsQuotaUs, err := cpuCGroup.readInt(_cgroupCPUCFSQuotaUsParam)
  93. if defined := cfsQuotaUs > 0; err != nil || !defined {
  94. return -1, defined, err
  95. }
  96. cfsPeriodUs, err := cpuCGroup.readInt(_cgroupCPUCFSPeriodUsParam)
  97. if defined := cfsPeriodUs > 0; err != nil || !defined {
  98. return -1, defined, err
  99. }
  100. return float64(cfsQuotaUs) / float64(cfsPeriodUs), true, nil
  101. }