Skip to content

Commit 2bb24da

Browse files
authored
Memory usage in CG Slice path could be 0 (#6110)
* Memory usage in Slice path could be 0 * Update memory usage test to handle zero values and refine method name * Update error message for memory usage exception to clarify non-negative expectation * Merge test cases
1 parent 162d8e1 commit 2bb24da

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Linux/LinuxUtilizationParserCgroupV2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ public long GetMemoryUsageInBytesFromSlices(string pattern)
257257
ReadOnlySpan<char> memoryUsageFile = bufferWriter.Buffer.WrittenSpan;
258258
int next = GetNextNumber(memoryUsageFile, out long containerMemoryUsage);
259259

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

267267
memoryUsageInBytesTotal += containerMemoryUsage;

test/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Tests/Linux/LinuxUtilizationParserCgroupV2Tests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public Task Throws_When_UsageInBytes_Doesnt_Contain_A_Number()
168168
[ConditionalFact]
169169
public void Returns_Memory_Usage_When_Memory_Usage_Is_Valid()
170170
{
171+
// When memory usage is a positive number
171172
var regexPatternforSlices = @"\w+.slice";
172173
var f = new HardcodedValueFileSystem(new Dictionary<FileInfo, string>
173174
{
@@ -178,6 +179,16 @@ public void Returns_Memory_Usage_When_Memory_Usage_Is_Valid()
178179
var r = p.GetMemoryUsageInBytesFromSlices(regexPatternforSlices);
179180

180181
Assert.Equal(5_342_342, r);
182+
183+
// When memory usage is zero
184+
f = new HardcodedValueFileSystem(new Dictionary<FileInfo, string>
185+
{
186+
{ new FileInfo("/sys/fs/cgroup/system.slice/memory.current"), "0"},
187+
});
188+
189+
p = new LinuxUtilizationParserCgroupV2(f, new FakeUserHz(100));
190+
r = p.GetMemoryUsageInBytesFromSlices(regexPatternforSlices);
191+
Assert.Equal(0, r);
181192
}
182193

183194
[ConditionalTheory]

test/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Tests/Linux/Verified/LinuxUtilizationParserCgroupV2Tests.Throws_When_UsageInBytes_Doesnt_Contain_A_Number.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
Type: InvalidOperationException,
3-
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'.,
3+
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'.,
44
StackTrace:
55
at Microsoft.Shared.Diagnostics.Throw.InvalidOperationException(String message)
66
at Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux.LinuxUtilizationParserCgroupV2.GetMemoryUsageInBytesFromSlices(String pattern)

0 commit comments

Comments
 (0)