Skip to content

Commit

Permalink
Merge pull request #39 from vostok/INFRA-8181-fix_incorrect_usage_of_…
Browse files Browse the repository at this point in the history
…Environment.ProcessorCount_when_collect_metrics

Fix incorrect usage of environment.processor count when collect metrics
  • Loading branch information
BroEnotiKa authored Jul 19, 2023
2 parents 9902ecd + 8085d1d commit ea89f8b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.3.18 (26-06-2023):

Fixed incorrect usage of Environment.ProcessorCount value (use host cores instead).

[Backward compability was broken in .net6](https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/environment-processorcount-on-windows)

## 0.3.17 (28-09-2022):

Fixed unlimited cgroup limits.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private void CollectDisksUsage(HostMetrics metrics)
}
}

private static int? GetProcessorCount()
internal static int? GetProcessorCount()
{
{
try
Expand Down
2 changes: 1 addition & 1 deletion Vostok.Metrics.System/Process/CurrentProcessMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CurrentProcessMetrics
{
/// <summary>
/// <para>Number of CPU cores utilized by current process.</para>
/// <para>This metric has a value between 0 and <see cref="Environment.ProcessorCount"/>.</para>
/// <para>This metric has a value between 0 and the total number of logical processors on the machine.</para>
/// <para>This metric is an average value between two observation moments (current and previous).</para>
/// </summary>
public double CpuUtilizedCores { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using Vostok.Metrics.System.Helpers;
using Vostok.Metrics.System.Host;

// ReSharper disable FieldCanBeMadeReadOnly.Local
// ReSharper disable MemberCanBePrivate.Local
Expand Down Expand Up @@ -84,7 +85,10 @@ private void CollectCpuUtilization(CurrentProcessMetrics metrics)
var systemTime = systemKernel.ToUInt64() + systemUser.ToUInt64();
var processTime = processKernel.ToUInt64() + processUser.ToUInt64();

cpuCollector.Collect(metrics, systemTime, processTime);
// note: Environment.ProcessorCount from NET6+ respects process affinity and the job object's hard limit on CPU utilization
// https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/environment-processorcount-on-windows
// so we should get total number of system cores from NativeHostMetricsCollector_Windows instead
cpuCollector.Collect(metrics, systemTime, processTime, NativeHostMetricsCollector_Windows.GetProcessorCount());
}
catch (Exception error)
{
Expand Down

0 comments on commit ea89f8b

Please sign in to comment.