Skip to content

Commit 3d87f73

Browse files
author
evgenyfedorov2
committed
Update
1 parent 6da3c5a commit 3d87f73

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public string GetCgroupPath(string filename)
131131
}
132132

133133
// Extract the part after the last colon and cache it for future use
134-
ReadOnlySpan<char> trimmedPath = fileContent.Slice(colonIndex + 1);
134+
ReadOnlySpan<char> trimmedPath = fileContent[(colonIndex + 1)..];
135135
_cachedCgroupPath = "/sys/fs/cgroup" + trimmedPath.ToString().TrimEnd('/') + "/";
136136

137137
return $"{_cachedCgroupPath}{filename}";

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ public LinuxUtilizationProvider(IOptions<ResourceMonitoringOptions> options, ILi
9898
_cpuUtilizationLimit110PercentExceededCounter = meter.CreateCounter<long>("cpu_utilization_limit_110_percent_exceeded");
9999
_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.ContainerCpuLimitUtilization, observeValue: () => CpuUtilizationLimit(cpuLimit), unit: "1");
100100
_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.ContainerCpuRequestUtilization, observeValue: () => CpuUtilizationWithoutHostDelta() / cpuRequest, unit: "1");
101-
_ = meter.CreateObservableCounter(name: ResourceUtilizationInstruments.ContainerCpuTime, observeValues: GetCpuTime, unit: "s", description: "CPU time used by the container.");
102101
}
103102
else
104103
{
105104
_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.ContainerCpuLimitUtilization, observeValue: () => CpuUtilization() * _scaleRelativeToCpuLimit, unit: "1");
106105
_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.ContainerCpuRequestUtilization, observeValue: () => CpuUtilization() * _scaleRelativeToCpuRequest, unit: "1");
107106
_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.ProcessCpuUtilization, observeValue: () => CpuUtilization() * _scaleRelativeToCpuRequest, unit: "1");
108-
_ = meter.CreateObservableCounter(name: ResourceUtilizationInstruments.ContainerCpuTime, observeValues: GetCpuTime, unit: "s", description: "CPU time used by the container.");
109107
}
110108

109+
_ = meter.CreateObservableCounter(name: ResourceUtilizationInstruments.ContainerCpuTime, observeValues: GetCpuTime, unit: "s", description: "CPU time used by the container.");
110+
111111
_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.ContainerMemoryLimitUtilization, observeValue: MemoryUtilization, unit: "1");
112112
_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.ProcessMemoryUtilization, observeValue: MemoryUtilization, unit: "1");
113113

@@ -296,9 +296,9 @@ public Snapshot GetSnapshot()
296296
private IEnumerable<Measurement<double>> GetCpuTime()
297297
{
298298
long hostCpuTime = _parser.GetHostCpuUsageInNanoseconds();
299-
long cgroupCpuTime = _parser.GetCgroupCpuUsageInNanoseconds();
299+
double cgroupCpuTime = CpuUtilizationWithoutHostDelta();
300300

301-
yield return new(cgroupCpuTime / NanosecondsInSecond, [new KeyValuePair<string, object?>("cpu.mode", "user")]);
302-
yield return new(hostCpuTime / NanosecondsInSecond, [new KeyValuePair<string, object?>("cpu.mode", "system")]);
301+
yield return new Measurement<double>(cgroupCpuTime / NanosecondsInSecond, [new KeyValuePair<string, object?>("cpu.mode", "user")]);
302+
yield return new Measurement<double>(hostCpuTime / NanosecondsInSecond, [new KeyValuePair<string, object?>("cpu.mode", "system")]);
303303
}
304304
}

src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/WindowsContainerSnapshotProvider.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ internal WindowsContainerSnapshotProvider(
8787

8888
_timeProvider = timeProvider;
8989

90-
using var jobHandle = _createJobHandleObject();
90+
using IJobHandle jobHandle = _createJobHandleObject();
9191

92-
var memoryLimitLong = GetMemoryLimit(jobHandle);
92+
ulong memoryLimitLong = GetMemoryLimit(jobHandle);
9393
_memoryLimit = memoryLimitLong;
9494
_cpuLimit = GetCpuLimit(jobHandle, systemInfo);
9595

9696
// CPU request (aka guaranteed CPU units) is not supported on Windows, so we set it to the same value as CPU limit (aka maximum CPU units).
9797
// Memory request (aka guaranteed memory) is not supported on Windows, so we set it to the same value as memory limit (aka maximum memory).
98-
var cpuRequest = _cpuLimit;
99-
var memoryRequest = memoryLimitLong;
98+
double cpuRequest = _cpuLimit;
99+
ulong memoryRequest = memoryLimitLong;
100100
Resources = new SystemResources(cpuRequest, _cpuLimit, memoryRequest, memoryLimitLong);
101101
_logger.SystemResourcesInfo(_cpuLimit, cpuRequest, memoryLimitLong, memoryRequest);
102102

@@ -112,7 +112,7 @@ internal WindowsContainerSnapshotProvider(
112112
// We don't dispose the meter because IMeterFactory handles that
113113
// An issue on analyzer side: https://github.com/dotnet/roslyn-analyzers/issues/6912
114114
// Related documentation: https://github.com/dotnet/docs/pull/37170
115-
var meter = meterFactory.Create(ResourceUtilizationInstruments.MeterName);
115+
Meter meter = meterFactory.Create(ResourceUtilizationInstruments.MeterName);
116116
#pragma warning restore CA2000 // Dispose objects before losing scope
117117

118118
// Container based metrics:
@@ -158,7 +158,7 @@ private static double GetCpuLimit(IJobHandle jobHandle, ISystemInfo systemInfo)
158158
cpuRatio = cpuLimit.CpuRate / CpuCycles;
159159
}
160160

161-
var systemInfoValue = systemInfo.GetSystemInfo();
161+
SYSTEM_INFO systemInfoValue = systemInfo.GetSystemInfo();
162162

163163
// Multiply the cpu ratio by the number of processors to get you the portion
164164
// of processors used from the system.
@@ -175,7 +175,7 @@ private ulong GetMemoryLimit(IJobHandle jobHandle)
175175

176176
if (memoryLimitInBytes <= 0)
177177
{
178-
var memoryStatus = _memoryStatus.Value;
178+
MEMORYSTATUSEX memoryStatus = _memoryStatus.Value;
179179

180180
// Technically, the unconstrained limit is memoryStatus.TotalPageFile.
181181
// Leaving this at physical as it is more understandable to consumers.
@@ -187,7 +187,7 @@ private ulong GetMemoryLimit(IJobHandle jobHandle)
187187

188188
private double MemoryPercentage(Func<ulong> getMemoryUsage)
189189
{
190-
var now = _timeProvider.GetUtcNow();
190+
DateTimeOffset now = _timeProvider.GetUtcNow();
191191

192192
lock (_memoryLocker)
193193
{
@@ -197,7 +197,7 @@ private double MemoryPercentage(Func<ulong> getMemoryUsage)
197197
}
198198
}
199199

200-
var memoryUsage = getMemoryUsage();
200+
ulong memoryUsage = getMemoryUsage();
201201

202202
lock (_memoryLocker)
203203
{
@@ -216,11 +216,13 @@ private double MemoryPercentage(Func<ulong> getMemoryUsage)
216216

217217
private IEnumerable<Measurement<double>> GetCpuTime()
218218
{
219-
using var jobHandle = _createJobHandleObject();
219+
using IJobHandle jobHandle = _createJobHandleObject();
220220
var basicAccountingInfo = jobHandle.GetBasicAccountingInfo();
221221

222-
yield return new(basicAccountingInfo.TotalUserTime / TicksPerSecondDouble, [new KeyValuePair<string, object?>("cpu.mode", "user")]);
223-
yield return new(basicAccountingInfo.TotalKernelTime / TicksPerSecondDouble, [new KeyValuePair<string, object?>("cpu.mode", "system")]);
222+
yield return new Measurement<double>(basicAccountingInfo.TotalUserTime / TicksPerSecondDouble,
223+
[new KeyValuePair<string, object?>("cpu.mode", "user")]);
224+
yield return new Measurement<double>(basicAccountingInfo.TotalKernelTime / TicksPerSecondDouble,
225+
[new KeyValuePair<string, object?>("cpu.mode", "system")]);
224226
}
225227

226228
private double CpuPercentage()

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,6 @@ public Task ResourceUtilizationTracker_And_Metrics_Report_Same_Values_With_Cgrou
375375
[OSSkipCondition(OperatingSystems.Windows | OperatingSystems.MacOSX, SkipReason = "Linux specific tests")]
376376
public Task ResourceUtilizationTracker_And_Metrics_Report_Same_Values_With_Cgroupsv2_v2()
377377
{
378-
var cpuRefresh = TimeSpan.FromMinutes(13);
379-
var memoryRefresh = TimeSpan.FromMinutes(14);
380378
var fileSystem = new HardcodedValueFileSystem(new Dictionary<FileInfo, string>
381379
{
382380
{ new FileInfo("/proc/self/cgroup"), "0::/fakeslice"},
@@ -392,6 +390,8 @@ public Task ResourceUtilizationTracker_And_Metrics_Report_Same_Values_With_Cgrou
392390
using var listener = new MeterListener();
393391
var clock = new FakeTimeProvider(DateTimeOffset.UtcNow);
394392
var cpuFromGauge = 0.0d;
393+
var cpuUserTime = 0.0d;
394+
var cpuKernelTime = 0.0d;
395395
var cpuLimitFromGauge = 0.0d;
396396
var cpuRequestFromGauge = 0.0d;
397397
var memoryFromGauge = 0.0d;
@@ -401,8 +401,8 @@ public Task ResourceUtilizationTracker_And_Metrics_Report_Same_Values_With_Cgrou
401401
object? meterScope = null;
402402
listener.InstrumentPublished = (Instrument instrument, MeterListener meterListener)
403403
=> OnInstrumentPublished(instrument, meterListener, meterScope);
404-
listener.SetMeasurementEventCallback<double>((m, f, _, _)
405-
=> OnMeasurementReceived(m, f, ref cpuFromGauge, ref cpuLimitFromGauge, ref cpuRequestFromGauge, ref memoryFromGauge, ref memoryLimitFromGauge));
404+
listener.SetMeasurementEventCallback<double>((m, f, tags, _)
405+
=> OnMeasurementReceived(m, f, tags, ref cpuUserTime, ref cpuKernelTime, ref cpuFromGauge, ref cpuLimitFromGauge, ref cpuRequestFromGauge, ref memoryFromGauge, ref memoryLimitFromGauge));
406406
listener.Start();
407407

408408
using var host = FakeHost.CreateBuilder()
@@ -451,8 +451,6 @@ public Task ResourceUtilizationTracker_And_Metrics_Report_Same_Values_With_Cgrou
451451
[OSSkipCondition(OperatingSystems.Windows | OperatingSystems.MacOSX, SkipReason = "Linux specific tests")]
452452
public Task ResourceUtilizationTracker_And_Metrics_Report_Same_Values_With_Cgroupsv2_v2_Using_NrPeriods()
453453
{
454-
var cpuRefresh = TimeSpan.FromMinutes(13);
455-
var memoryRefresh = TimeSpan.FromMinutes(14);
456454
var fileSystem = new HardcodedValueFileSystem(new Dictionary<FileInfo, string>
457455
{
458456
{ new FileInfo("/proc/self/cgroup"), "0::/fakeslice"},
@@ -469,6 +467,8 @@ public Task ResourceUtilizationTracker_And_Metrics_Report_Same_Values_With_Cgrou
469467
var clock = new FakeTimeProvider(DateTimeOffset.UtcNow);
470468
var cpuFromGauge = 0.0d;
471469
var cpuLimitFromGauge = 0.0d;
470+
var cpuUserTime = 0.0d;
471+
var cpuKernelTime = 0.0d;
472472
var cpuRequestFromGauge = 0.0d;
473473
var memoryFromGauge = 0.0d;
474474
var memoryLimitFromGauge = 0.0d;
@@ -477,8 +477,8 @@ public Task ResourceUtilizationTracker_And_Metrics_Report_Same_Values_With_Cgrou
477477
object? meterScope = null;
478478
listener.InstrumentPublished = (Instrument instrument, MeterListener meterListener)
479479
=> OnInstrumentPublished(instrument, meterListener, meterScope);
480-
listener.SetMeasurementEventCallback<double>((m, f, _, _)
481-
=> OnMeasurementReceived(m, f, ref cpuFromGauge, ref cpuLimitFromGauge, ref cpuRequestFromGauge, ref memoryFromGauge, ref memoryLimitFromGauge));
480+
listener.SetMeasurementEventCallback<double>((m, f, tags, _)
481+
=> OnMeasurementReceived(m, f, tags, ref cpuUserTime, ref cpuKernelTime, ref cpuFromGauge, ref cpuLimitFromGauge, ref cpuRequestFromGauge, ref memoryFromGauge, ref memoryLimitFromGauge));
482482
listener.Start();
483483

484484
using var host = FakeHost.CreateBuilder()

0 commit comments

Comments
 (0)