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