MCPcopy Create free account
hub / github.com/containerd/cgroups / ToResources

Function ToResources

cgroup2/utils.go:183–264  ·  view source on GitHub ↗

ToResources converts the oci LinuxResources struct into a v2 Resources type for use with this package. converting cgroups configuration from v1 to v2 ref: https://github.com/containers/crun/blob/master/crun.1.md#cgroup-v2

(spec *specs.LinuxResources)

Source from the content-addressed store, hash-verified

181// converting cgroups configuration from v1 to v2
182// ref: https://github.com/containers/crun/blob/master/crun.1.md#cgroup-v2
183func ToResources(spec *specs.LinuxResources) *Resources {
184 var resources Resources
185 if cpu := spec.CPU; cpu != nil {
186 resources.CPU = &CPU{
187 Cpus: cpu.Cpus,
188 Mems: cpu.Mems,
189 }
190 if shares := cpu.Shares; shares != nil {
191 convertedWeight := ConvertCPUSharesToCgroupV2Value(*shares)
192 resources.CPU.Weight = &convertedWeight
193 }
194 if period := cpu.Period; period != nil {
195 resources.CPU.Max = NewCPUMax(cpu.Quota, period)
196 }
197 }
198 if mem := spec.Memory; mem != nil {
199 resources.Memory = &Memory{}
200 if swap := mem.Swap; swap != nil {
201 resources.Memory.Swap = swap
202 if l := mem.Limit; l != nil {
203 reduce := *swap - *l
204 resources.Memory.Swap = &reduce
205 }
206 }
207 if l := mem.Limit; l != nil {
208 resources.Memory.Max = l
209 }
210 if l := mem.Reservation; l != nil {
211 resources.Memory.Low = l
212 }
213 }
214 if hugetlbs := spec.HugepageLimits; hugetlbs != nil {
215 hugeTlbUsage := HugeTlb{}
216 for _, hugetlb := range hugetlbs {
217 hugeTlbUsage = append(hugeTlbUsage, HugeTlbEntry{
218 HugePageSize: hugetlb.Pagesize,
219 Limit: hugetlb.Limit,
220 })
221 }
222 resources.HugeTlb = &hugeTlbUsage
223 }
224 if pids := spec.Pids; pids != nil && pids.Limit != nil {
225 resources.Pids = &Pids{
226 Max: *pids.Limit,
227 }
228 }
229 if i := spec.BlockIO; i != nil {
230 resources.IO = &IO{}
231 if i.Weight != nil {
232 resources.IO.BFQ.Weight = 1 + (*i.Weight-10)*9999/990
233 }
234 for t, devices := range map[IOType][]specs.LinuxThrottleDevice{
235 ReadBPS: i.ThrottleReadBpsDevice,
236 WriteBPS: i.ThrottleWriteBpsDevice,
237 ReadIOPS: i.ThrottleReadIOPSDevice,
238 WriteIOPS: i.ThrottleWriteIOPSDevice,
239 } {
240 for _, d := range devices {

Callers 4

TestToResourcesFunction · 0.85
TestKillFunction · 0.85
TestMoveToFunction · 0.85
TestCgroupTypeFunction · 0.85

Calls 2

NewCPUMaxFunction · 0.85

Tested by 4

TestToResourcesFunction · 0.68
TestKillFunction · 0.68
TestMoveToFunction · 0.68
TestCgroupTypeFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…