Skip to content

Memory usage in CG Slice path could be 0 #6110

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

Merged
merged 6 commits into from
Mar 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -257,11 +257,11 @@ public long GetMemoryUsageInBytesFromSlices(string pattern)
ReadOnlySpan<char> memoryUsageFile = bufferWriter.Buffer.WrittenSpan;
int next = GetNextNumber(memoryUsageFile, out long containerMemoryUsage);

if (containerMemoryUsage == 0 || containerMemoryUsage == -1)
if (containerMemoryUsage == -1)
{
memoryUsageInBytesTotal = 0;
Throw.InvalidOperationException(
$"We tried to read '{memoryUsageInBytesFile}', and we expected to get a positive number but instead it was: '{memoryUsageFile}'.");
$"We tried to read '{memoryUsageInBytesFile}', and we expected to get a non-negative number but instead it was: '{memoryUsageFile}'.");
}

memoryUsageInBytesTotal += containerMemoryUsage;
Original file line number Diff line number Diff line change
@@ -168,6 +168,7 @@ public Task Throws_When_UsageInBytes_Doesnt_Contain_A_Number()
[ConditionalFact]
public void Returns_Memory_Usage_When_Memory_Usage_Is_Valid()
{
// When memory usage is a positive number
var regexPatternforSlices = @"\w+.slice";
var f = new HardcodedValueFileSystem(new Dictionary<FileInfo, string>
{
@@ -178,6 +179,16 @@ public void Returns_Memory_Usage_When_Memory_Usage_Is_Valid()
var r = p.GetMemoryUsageInBytesFromSlices(regexPatternforSlices);

Assert.Equal(5_342_342, r);

// When memory usage is zero
f = new HardcodedValueFileSystem(new Dictionary<FileInfo, string>
{
{ new FileInfo("/sys/fs/cgroup/system.slice/memory.current"), "0"},
});

p = new LinuxUtilizationParserCgroupV2(f, new FakeUserHz(100));
r = p.GetMemoryUsageInBytesFromSlices(regexPatternforSlices);
Assert.Equal(0, r);
}

[ConditionalTheory]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
Type: InvalidOperationException,
Message: We tried to read '/sys/fs/cgroup/system.slice/memory.current', and we expected to get a positive number but instead it was: 'dasda'.,
Message: We tried to read '/sys/fs/cgroup/system.slice/memory.current', and we expected to get a non-negative number but instead it was: 'dasda'.,
StackTrace:
at Microsoft.Shared.Diagnostics.Throw.InvalidOperationException(String message)
at Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux.LinuxUtilizationParserCgroupV2.GetMemoryUsageInBytesFromSlices(String pattern)