Skip to content
Merged
Show file tree
Hide file tree
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
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
{
Expand All @@ -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]
Expand Down
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)
Expand Down
Loading