Add support for Linux cgrpoup v2, issue-4885
- Introduced ILinuxUtilizationParser interface to be able to choose between parsers during DI
- Introduced new class for cgroup v2 specifics LinuxUtilizationParserCgroupV2
- GetHostCpuUsageInNanoseconds() parses /sys/fs/cgroup/cpu.stat file and calculates cpu usage in microseconds
- TryGetCpuUnitsFromCgroups() parses cpu quota and period from a single file /sys/fs/cgroup/cpu.max
- TryGetCgroupRequestCpu() parses /sys/fs/cgroup/cpu.weight and calculates from cpu weight to shares (1024 share == 1 CPU core) in order to get the Pod's cpu request limit.
- Introduced new method GetMemoryUsageInBytesFromSlices() to calculate memory stats from slices (cgroup v2 specific)
- Introduced an overload in SystemResources struct where new metric is added guaranteedPodCpuUnits, which get cpu request limit (as per k8s terminology)
Microsoft Reviewers: Open in CodeFlow
@nezdali , I deployed your change into my own cluster that has cgroupsv2 and printed SystemResources object.
The cluster have following resources in yaml:
resources:
requests:
cpu: "300m"
memory: "1.5Gi"
limits:
cpu: "900m"
memory: "3.5Gi"
The printed values are following.
System Resources:
Requests:
CPU = 309m, (based on GuaranteedPodCpuUnits)
Memory = 3.5Gi, (based on GuaranteedMemoryInBytes)
Limits:
CPU = 900m, (based on MaximumCpuUnits)
Memory: 15.61508560180664Gi (based on MaximumMemoryInBytes)
While the requests for CPU corresponds to YAML specification, the memory seems to be not accurate. Memory limit (MaximumMemoryInBytes) seems to be showing the capacity of the host machine instead of the pod limit. The memory request (GuaranteedMemoryInBytes) contains the pod limit.
After more research it turns out that GuaranteedMemoryInBytes is the Resource Memory Limit (in k8s terms) which is read from /sys/fs/cgroup/memory.max And currently there is no way to get the Resource Memory Request. It is always 0 in /sys/fs/cgroup/memory.min , for cgroupv2 container, VM (AzureLinux, Ubuntu) K8s documentation on how memory.min populated https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2570-memory-qos/#readme