-
Notifications
You must be signed in to change notification settings - Fork 841
Add support for Linux cgrpoup v2, issue-4885 #5068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
87261f3
f32c9ad
da44f5f
3f75b40
87e64b8
cda49b5
a09bfc8
0bc23ba
e80f7fb
de0ca6b
0a4d70e
89ee8fe
c124d5a
cca3d60
1f9c963
742d0a4
c188df2
af86bc6
9ac1be5
5baf6ba
d950ad3
91b0a4b
d24f868
881f54b
e4c69ae
46cc5bf
507f2ed
4f6eb8b
05a0994
5484960
00f46f5
d099770
031caa7
d191d28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux; | ||
|
|
||
| /// <summary> | ||
| /// An interface to be implemented by a parser that reads files and extracts resource utilization data from them. | ||
| /// For both versions of cgroup controllers, the parser reads files from the /sys/fs/cgroup directory. | ||
| /// </summary> | ||
| internal interface ILinuxUtilizationParser | ||
| { | ||
| /// <summary> | ||
| /// Reads file /sys/fs/cgroup/memory.max, which is used to check the maximum amount of memory that can be used by a cgroup. | ||
| /// It is part of the cgroup v2 memory controller. | ||
| /// </summary> | ||
| /// <returns>maybeMemory.</returns> | ||
| ulong GetAvailableMemoryInBytes(); | ||
|
|
||
| /// <summary> | ||
| /// Reads the file /sys/fs/cgroup/cpu.stat, which is part of the cgroup v2 CPU controller. | ||
| /// It provides statistics about the CPU usage of a cgroup. | ||
| /// </summary> | ||
| /// <returns>nanoseconds.</returns> | ||
| long GetCgroupCpuUsageInNanoseconds(); | ||
|
|
||
| /// <summary> | ||
| /// Reads the file /sys/fs/cgroup/cpu.max, which is part of the cgroup v2 CPU controller. | ||
| /// It is used to set the maximum amount of CPU time that can be used by a cgroup. | ||
| /// The file contains two fields, separated by a space. | ||
| /// The first field is the quota, which specifies the maximum amount of CPU time (in microseconds) that can be used by the cgroup during one period. | ||
| /// The second value is the period, which specifies the length of a period in microseconds. | ||
| /// </summary> | ||
| /// <returns>cpuUnits.</returns> | ||
| float GetCgroupLimitedCpus(); | ||
|
|
||
| /// <summary> | ||
| /// Reads the file /proc/stat, which provides information about the system’s memory usage. | ||
| /// It contains information about the total amount of installed memory, the amount of free and used memory, and the amount of memory used by the kernel and buffers/cache. | ||
| /// </summary> | ||
| /// <returns>memory.</returns> | ||
| ulong GetHostAvailableMemory(); | ||
|
|
||
| /// <summary> | ||
| /// Reads the file /sys/fs/cgroup/cpuset.cpus.effective, which is part of the cgroup v2 cpuset controller. | ||
| /// It shows the effective cpus that the cgroup can use. | ||
| /// </summary> | ||
| /// <returns>cpuCount.</returns> | ||
| float GetHostCpuCount(); | ||
|
|
||
| /// <summary> | ||
| /// Reads the file /sys/fs/cgroup/cpu.stat, which is part of the cgroup v2 CPU controller. | ||
| /// It provides statistics about the CPU usage of a cgroup. | ||
| /// The file contains several fields, including usage_usec, which shows the total CPU time (in microseconds). | ||
| /// </summary> | ||
| /// <returns>total / (double)_userHz * NanosecondsInSecond.</returns> | ||
| long GetHostCpuUsageInNanoseconds(); | ||
|
|
||
| /// <summary> | ||
| /// Reads the file /sys/fs/cgroup/memory.current, which is a file that contains the current memory usage of a cgroup in bytes. | ||
| /// </summary> | ||
| /// <returns>memoryUsage.</returns> | ||
| ulong GetMemoryUsageInBytes(); | ||
|
|
||
| /// <summary> | ||
| /// Reads the file /sys/fs/cgroup/cpu.weight. And calculates the Pod CPU Request in millicores. | ||
| /// </summary> | ||
| /// <returns>cpuPodRequest.</returns> | ||
| float GetCgroupRequestCpu(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,10 @@ namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux; | |
| /// This class is not thread safe. | ||
| /// When the same instance is called by multiple threads it may return corrupted data. | ||
| /// </remarks> | ||
| internal sealed class LinuxUtilizationParser | ||
| internal sealed class LinuxUtilizationParser : ILinuxUtilizationParser | ||
|
||
| { | ||
| private const float CpuShares = 1024; | ||
|
|
||
| /// <remarks> | ||
| /// File contains the amount of CPU time (in microseconds) available to the group during each accounting period. | ||
| /// </remarks> | ||
|
|
@@ -76,6 +78,11 @@ internal sealed class LinuxUtilizationParser | |
| /// </remarks> | ||
| private static readonly FileInfo _cpuacctUsage = new("/sys/fs/cgroup/cpuacct/cpuacct.usage"); | ||
|
|
||
| /// <summary> | ||
| /// CPU weights, also known as shares in cgroup v1, is used for resource allocation. | ||
| /// </summary> | ||
| private static readonly FileInfo _cpuPodWeight = new("/sys/fs/cgroup/cpu/cpu.shares"); | ||
|
|
||
| private readonly IFileSystem _fileSystem; | ||
| private readonly long _userHz; | ||
| private readonly BufferWriter<char> _buffer = new(); | ||
|
|
@@ -160,6 +167,17 @@ public float GetCgroupLimitedCpus() | |
| return GetHostCpuCount(); | ||
| } | ||
|
|
||
| public float GetCgroupRequestCpu() | ||
| { | ||
| if (TryGetCgroupRequestCpu(_fileSystem, out var cpuUnits)) | ||
| { | ||
| return cpuUnits; | ||
| } | ||
|
|
||
| // If we can't read the CPU weight, we assume that the pod request is 1 core. | ||
| return 1; | ||
| } | ||
|
|
||
| public ulong GetAvailableMemoryInBytes() | ||
| { | ||
| const long UnsetCgroupMemoryLimit = 9_223_372_036_854_771_712; | ||
|
|
@@ -425,4 +443,38 @@ private bool TryGetCpuUnitsFromCgroups(IFileSystem fileSystem, out float cpuUnit | |
| cpuUnits = (float)quota / period; | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// In cgroup v1 the CPU shares is used to determine the CPU allocation. | ||
| /// in cgroup v2 the CPU weight is used to determine the CPU allocation. | ||
| /// To calculete CPU request in cgroup v2 we need to read the CPU weight and convert it to CPU shares. | ||
| /// But for cgroup v1 we can read the CPU shares directly from the file. | ||
| /// 1024 equals 1 CPU core. | ||
| /// In cgroup v1 on some systems the location of the CPU shares file is different. | ||
| /// </summary> | ||
| private bool TryGetCgroupRequestCpu(IFileSystem fileSystem, out float cpuUnits) | ||
| { | ||
| if (!_cpuPodWeight.Exists) | ||
| { | ||
| cpuUnits = 1; | ||
| return false; | ||
| } | ||
|
|
||
| fileSystem.ReadFirstLine(_cpuPodWeight, _buffer); | ||
| var cpuPodWeightBuffer = _buffer.WrittenSpan; | ||
| _ = GetNextNumber(cpuPodWeightBuffer, out var cpuPodWeight); | ||
|
|
||
| if (cpuPodWeightBuffer.IsEmpty || (cpuPodWeightBuffer.Length == 2 && cpuPodWeightBuffer[0] == '-' && cpuPodWeightBuffer[1] == '1')) | ||
| { | ||
| _buffer.Reset(); | ||
| Throw.InvalidOperationException($"Could not parse '{_cpuPodWeight}' content. Expected to find CPU weight but got '{new string(cpuPodWeightBuffer)}' instead."); | ||
| cpuUnits = -1; | ||
| return false; | ||
| } | ||
|
|
||
| _buffer.Reset(); | ||
| var result = cpuPodWeight / CpuShares; | ||
| cpuUnits = result; | ||
| return true; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.