helpers.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package libcontainer
  15. import (
  16. "fmt"
  17. info "github.com/google/cadvisor/info/v1"
  18. "github.com/google/cadvisor/container"
  19. "github.com/opencontainers/runc/libcontainer/cgroups"
  20. "k8s.io/klog"
  21. )
  22. type CgroupSubsystems struct {
  23. // Cgroup subsystem mounts.
  24. // e.g.: "/sys/fs/cgroup/cpu" -> ["cpu", "cpuacct"]
  25. Mounts []cgroups.Mount
  26. // Cgroup subsystem to their mount location.
  27. // e.g.: "cpu" -> "/sys/fs/cgroup/cpu"
  28. MountPoints map[string]string
  29. }
  30. // Get information about the cgroup subsystems those we want
  31. func GetCgroupSubsystems(includedMetrics container.MetricSet) (CgroupSubsystems, error) {
  32. // Get all cgroup mounts.
  33. allCgroups, err := cgroups.GetCgroupMounts(true)
  34. if err != nil {
  35. return CgroupSubsystems{}, err
  36. }
  37. disableCgroups := map[string]struct{}{}
  38. //currently we only support disable blkio subsystem
  39. if !includedMetrics.Has(container.DiskIOMetrics) {
  40. disableCgroups["blkio"] = struct{}{}
  41. }
  42. return getCgroupSubsystemsHelper(allCgroups, disableCgroups)
  43. }
  44. // Get information about all the cgroup subsystems.
  45. func GetAllCgroupSubsystems() (CgroupSubsystems, error) {
  46. // Get all cgroup mounts.
  47. allCgroups, err := cgroups.GetCgroupMounts(true)
  48. if err != nil {
  49. return CgroupSubsystems{}, err
  50. }
  51. emptyDisableCgroups := map[string]struct{}{}
  52. return getCgroupSubsystemsHelper(allCgroups, emptyDisableCgroups)
  53. }
  54. func getCgroupSubsystemsHelper(allCgroups []cgroups.Mount, disableCgroups map[string]struct{}) (CgroupSubsystems, error) {
  55. if len(allCgroups) == 0 {
  56. return CgroupSubsystems{}, fmt.Errorf("failed to find cgroup mounts")
  57. }
  58. // Trim the mounts to only the subsystems we care about.
  59. supportedCgroups := make([]cgroups.Mount, 0, len(allCgroups))
  60. recordedMountpoints := make(map[string]struct{}, len(allCgroups))
  61. mountPoints := make(map[string]string, len(allCgroups))
  62. for _, mount := range allCgroups {
  63. for _, subsystem := range mount.Subsystems {
  64. if _, exists := disableCgroups[subsystem]; exists {
  65. continue
  66. }
  67. if _, ok := supportedSubsystems[subsystem]; !ok {
  68. // Unsupported subsystem
  69. continue
  70. }
  71. if _, ok := mountPoints[subsystem]; ok {
  72. // duplicate mount for this subsystem; use the first one we saw
  73. klog.V(5).Infof("skipping %s, already using mount at %s", mount.Mountpoint, mountPoints[subsystem])
  74. continue
  75. }
  76. if _, ok := recordedMountpoints[mount.Mountpoint]; !ok {
  77. // avoid appending the same mount twice in e.g. `cpu,cpuacct` case
  78. supportedCgroups = append(supportedCgroups, mount)
  79. recordedMountpoints[mount.Mountpoint] = struct{}{}
  80. }
  81. mountPoints[subsystem] = mount.Mountpoint
  82. }
  83. }
  84. return CgroupSubsystems{
  85. Mounts: supportedCgroups,
  86. MountPoints: mountPoints,
  87. }, nil
  88. }
  89. // Cgroup subsystems we support listing (should be the minimal set we need stats from).
  90. var supportedSubsystems map[string]struct{} = map[string]struct{}{
  91. "cpu": {},
  92. "cpuacct": {},
  93. "memory": {},
  94. "cpuset": {},
  95. "blkio": {},
  96. "devices": {},
  97. }
  98. func DiskStatsCopy0(major, minor uint64) *info.PerDiskStats {
  99. disk := info.PerDiskStats{
  100. Major: major,
  101. Minor: minor,
  102. }
  103. disk.Stats = make(map[string]uint64)
  104. return &disk
  105. }
  106. type DiskKey struct {
  107. Major uint64
  108. Minor uint64
  109. }
  110. func DiskStatsCopy1(disk_stat map[DiskKey]*info.PerDiskStats) []info.PerDiskStats {
  111. i := 0
  112. stat := make([]info.PerDiskStats, len(disk_stat))
  113. for _, disk := range disk_stat {
  114. stat[i] = *disk
  115. i++
  116. }
  117. return stat
  118. }
  119. func DiskStatsCopy(blkio_stats []cgroups.BlkioStatEntry) (stat []info.PerDiskStats) {
  120. if len(blkio_stats) == 0 {
  121. return
  122. }
  123. disk_stat := make(map[DiskKey]*info.PerDiskStats)
  124. for i := range blkio_stats {
  125. major := blkio_stats[i].Major
  126. minor := blkio_stats[i].Minor
  127. disk_key := DiskKey{
  128. Major: major,
  129. Minor: minor,
  130. }
  131. diskp, ok := disk_stat[disk_key]
  132. if !ok {
  133. diskp = DiskStatsCopy0(major, minor)
  134. disk_stat[disk_key] = diskp
  135. }
  136. op := blkio_stats[i].Op
  137. if op == "" {
  138. op = "Count"
  139. }
  140. diskp.Stats[op] = blkio_stats[i].Value
  141. }
  142. return DiskStatsCopy1(disk_stat)
  143. }