cgroup2_linux.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "strings"
  7. cgroupsV2 "github.com/containerd/cgroups/v2"
  8. "github.com/containerd/containerd/pkg/userns"
  9. "github.com/opencontainers/runc/libcontainer/cgroups"
  10. "github.com/sirupsen/logrus"
  11. )
  12. func newV2(options ...Opt) *SysInfo {
  13. sysInfo := &SysInfo{
  14. CgroupUnified: true,
  15. cg2GroupPath: "/",
  16. }
  17. for _, o := range options {
  18. o(sysInfo)
  19. }
  20. ops := []infoCollector{
  21. applyNetworkingInfo,
  22. applyAppArmorInfo,
  23. applySeccompInfo,
  24. applyCgroupNsInfo,
  25. }
  26. m, err := cgroupsV2.LoadManager("/sys/fs/cgroup", sysInfo.cg2GroupPath)
  27. if err != nil {
  28. logrus.Warn(err)
  29. } else {
  30. sysInfo.cg2Controllers = make(map[string]struct{})
  31. controllers, err := m.Controllers()
  32. if err != nil {
  33. logrus.Warn(err)
  34. }
  35. for _, c := range controllers {
  36. sysInfo.cg2Controllers[c] = struct{}{}
  37. }
  38. ops = append(ops,
  39. applyMemoryCgroupInfoV2,
  40. applyCPUCgroupInfoV2,
  41. applyIOCgroupInfoV2,
  42. applyCPUSetCgroupInfoV2,
  43. applyPIDSCgroupInfoV2,
  44. applyDevicesCgroupInfoV2,
  45. )
  46. }
  47. for _, o := range ops {
  48. o(sysInfo)
  49. }
  50. return sysInfo
  51. }
  52. func getSwapLimitV2() bool {
  53. groups, err := cgroups.ParseCgroupFile("/proc/self/cgroup")
  54. if err != nil {
  55. return false
  56. }
  57. g := groups[""]
  58. if g == "" {
  59. return false
  60. }
  61. cGroupPath := path.Join("/sys/fs/cgroup", g, "memory.swap.max")
  62. if _, err = os.Stat(cGroupPath); os.IsNotExist(err) {
  63. return false
  64. }
  65. return true
  66. }
  67. func applyMemoryCgroupInfoV2(info *SysInfo) {
  68. if _, ok := info.cg2Controllers["memory"]; !ok {
  69. info.Warnings = append(info.Warnings, "Unable to find memory controller")
  70. return
  71. }
  72. info.MemoryLimit = true
  73. info.SwapLimit = getSwapLimitV2()
  74. info.MemoryReservation = true
  75. info.OomKillDisable = false
  76. info.MemorySwappiness = false
  77. info.KernelMemory = false
  78. info.KernelMemoryTCP = false
  79. }
  80. func applyCPUCgroupInfoV2(info *SysInfo) {
  81. if _, ok := info.cg2Controllers["cpu"]; !ok {
  82. info.Warnings = append(info.Warnings, "Unable to find cpu controller")
  83. return
  84. }
  85. info.CPUShares = true
  86. info.CPUCfs = true
  87. info.CPURealtime = false
  88. }
  89. func applyIOCgroupInfoV2(info *SysInfo) {
  90. if _, ok := info.cg2Controllers["io"]; !ok {
  91. info.Warnings = append(info.Warnings, "Unable to find io controller")
  92. return
  93. }
  94. info.BlkioWeight = true
  95. info.BlkioWeightDevice = true
  96. info.BlkioReadBpsDevice = true
  97. info.BlkioWriteBpsDevice = true
  98. info.BlkioReadIOpsDevice = true
  99. info.BlkioWriteIOpsDevice = true
  100. }
  101. func applyCPUSetCgroupInfoV2(info *SysInfo) {
  102. if _, ok := info.cg2Controllers["cpuset"]; !ok {
  103. info.Warnings = append(info.Warnings, "Unable to find cpuset controller")
  104. return
  105. }
  106. info.Cpuset = true
  107. cpus, err := ioutil.ReadFile(path.Join("/sys/fs/cgroup", info.cg2GroupPath, "cpuset.cpus.effective"))
  108. if err != nil {
  109. return
  110. }
  111. info.Cpus = strings.TrimSpace(string(cpus))
  112. mems, err := ioutil.ReadFile(path.Join("/sys/fs/cgroup", info.cg2GroupPath, "cpuset.mems.effective"))
  113. if err != nil {
  114. return
  115. }
  116. info.Mems = strings.TrimSpace(string(mems))
  117. }
  118. func applyPIDSCgroupInfoV2(info *SysInfo) {
  119. if _, ok := info.cg2Controllers["pids"]; !ok {
  120. info.Warnings = append(info.Warnings, "Unable to find pids controller")
  121. return
  122. }
  123. info.PidsLimit = true
  124. }
  125. func applyDevicesCgroupInfoV2(info *SysInfo) {
  126. info.CgroupDevicesEnabled = !userns.RunningInUserNS()
  127. }