cgroup2_linux.go 3.2 KB

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