proc_cgroup.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package procfs
  14. import (
  15. "bufio"
  16. "bytes"
  17. "fmt"
  18. "strconv"
  19. "strings"
  20. "github.com/prometheus/procfs/internal/util"
  21. )
  22. // Cgroup models one line from /proc/[pid]/cgroup. Each Cgroup struct describes the the placement of a PID inside a
  23. // specific control hierarchy. The kernel has two cgroup APIs, v1 and v2. v1 has one hierarchy per available resource
  24. // controller, while v2 has one unified hierarchy shared by all controllers. Regardless of v1 or v2, all hierarchies
  25. // contain all running processes, so the question answerable with a Cgroup struct is 'where is this process in
  26. // this hierarchy' (where==what path on the specific cgroupfs). By prefixing this path with the mount point of
  27. // *this specific* hierarchy, you can locate the relevant pseudo-files needed to read/set the data for this PID
  28. // in this hierarchy
  29. //
  30. // Also see http://man7.org/linux/man-pages/man7/cgroups.7.html
  31. type Cgroup struct {
  32. // HierarchyID that can be matched to a named hierarchy using /proc/cgroups. Cgroups V2 only has one
  33. // hierarchy, so HierarchyID is always 0. For cgroups v1 this is a unique ID number
  34. HierarchyID int
  35. // Controllers using this hierarchy of processes. Controllers are also known as subsystems. For
  36. // Cgroups V2 this may be empty, as all active controllers use the same hierarchy
  37. Controllers []string
  38. // Path of this control group, relative to the mount point of the cgroupfs representing this specific
  39. // hierarchy
  40. Path string
  41. }
  42. // parseCgroupString parses each line of the /proc/[pid]/cgroup file
  43. // Line format is hierarchyID:[controller1,controller2]:path.
  44. func parseCgroupString(cgroupStr string) (*Cgroup, error) {
  45. var err error
  46. fields := strings.SplitN(cgroupStr, ":", 3)
  47. if len(fields) < 3 {
  48. return nil, fmt.Errorf("at least 3 fields required, found %d fields in cgroup string: %s", len(fields), cgroupStr)
  49. }
  50. cgroup := &Cgroup{
  51. Path: fields[2],
  52. Controllers: nil,
  53. }
  54. cgroup.HierarchyID, err = strconv.Atoi(fields[0])
  55. if err != nil {
  56. return nil, fmt.Errorf("failed to parse hierarchy ID")
  57. }
  58. if fields[1] != "" {
  59. ssNames := strings.Split(fields[1], ",")
  60. cgroup.Controllers = append(cgroup.Controllers, ssNames...)
  61. }
  62. return cgroup, nil
  63. }
  64. // parseCgroups reads each line of the /proc/[pid]/cgroup file.
  65. func parseCgroups(data []byte) ([]Cgroup, error) {
  66. var cgroups []Cgroup
  67. scanner := bufio.NewScanner(bytes.NewReader(data))
  68. for scanner.Scan() {
  69. mountString := scanner.Text()
  70. parsedMounts, err := parseCgroupString(mountString)
  71. if err != nil {
  72. return nil, err
  73. }
  74. cgroups = append(cgroups, *parsedMounts)
  75. }
  76. err := scanner.Err()
  77. return cgroups, err
  78. }
  79. // Cgroups reads from /proc/<pid>/cgroups and returns a []*Cgroup struct locating this PID in each process
  80. // control hierarchy running on this system. On every system (v1 and v2), all hierarchies contain all processes,
  81. // so the len of the returned struct is equal to the number of active hierarchies on this system.
  82. func (p Proc) Cgroups() ([]Cgroup, error) {
  83. data, err := util.ReadFileNoStat(p.path("cgroup"))
  84. if err != nil {
  85. return nil, err
  86. }
  87. return parseCgroups(data)
  88. }