subsys.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. import (
  24. "bufio"
  25. "os"
  26. "strconv"
  27. "strings"
  28. )
  29. const (
  30. _cgroupSep = ":"
  31. _cgroupSubsysSep = ","
  32. )
  33. const (
  34. _csFieldIDID = iota
  35. _csFieldIDSubsystems
  36. _csFieldIDName
  37. _csFieldCount
  38. )
  39. // CGroupSubsys represents the data structure for entities in
  40. // `/proc/$PID/cgroup`. See also proc(5) for more information.
  41. type CGroupSubsys struct {
  42. ID int
  43. Subsystems []string
  44. Name string
  45. }
  46. // NewCGroupSubsysFromLine returns a new *CGroupSubsys by parsing a string in
  47. // the format of `/proc/$PID/cgroup`
  48. func NewCGroupSubsysFromLine(line string) (*CGroupSubsys, error) {
  49. fields := strings.SplitN(line, _cgroupSep, _csFieldCount)
  50. if len(fields) != _csFieldCount {
  51. return nil, cgroupSubsysFormatInvalidError{line}
  52. }
  53. id, err := strconv.Atoi(fields[_csFieldIDID])
  54. if err != nil {
  55. return nil, err
  56. }
  57. cgroup := &CGroupSubsys{
  58. ID: id,
  59. Subsystems: strings.Split(fields[_csFieldIDSubsystems], _cgroupSubsysSep),
  60. Name: fields[_csFieldIDName],
  61. }
  62. return cgroup, nil
  63. }
  64. // parseCGroupSubsystems parses procPathCGroup (usually at `/proc/$PID/cgroup`)
  65. // and returns a new map[string]*CGroupSubsys.
  66. func parseCGroupSubsystems(procPathCGroup string) (map[string]*CGroupSubsys, error) {
  67. cgroupFile, err := os.Open(procPathCGroup)
  68. if err != nil {
  69. return nil, err
  70. }
  71. defer cgroupFile.Close()
  72. scanner := bufio.NewScanner(cgroupFile)
  73. subsystems := make(map[string]*CGroupSubsys)
  74. for scanner.Scan() {
  75. cgroup, err := NewCGroupSubsysFromLine(scanner.Text())
  76. if err != nil {
  77. return nil, err
  78. }
  79. for _, subsys := range cgroup.Subsystems {
  80. subsystems[subsys] = cgroup
  81. }
  82. }
  83. if err := scanner.Err(); err != nil {
  84. return nil, err
  85. }
  86. return subsystems, nil
  87. }