From 9ff399f619f6ff9d690d7116cfc6a59ac0f08645 Mon Sep 17 00:00:00 2001 From: "d.stukov" Date: Mon, 26 Jun 2023 14:36:32 +0500 Subject: [PATCH 1/3] fix incorrect usage of Environment.ProcessorCount value when collect windows cpu metrics --- .../Host/NativeHostMetricsCollector_Windows.cs | 2 +- .../Process/NativeMetricsCollector_Windows.cs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Vostok.Metrics.System/Host/NativeHostMetricsCollector_Windows.cs b/Vostok.Metrics.System/Host/NativeHostMetricsCollector_Windows.cs index 90bc94b..f320969 100644 --- a/Vostok.Metrics.System/Host/NativeHostMetricsCollector_Windows.cs +++ b/Vostok.Metrics.System/Host/NativeHostMetricsCollector_Windows.cs @@ -200,7 +200,7 @@ private void CollectDisksUsage(HostMetrics metrics) } } - private static int? GetProcessorCount() + internal static int? GetProcessorCount() { { try diff --git a/Vostok.Metrics.System/Process/NativeMetricsCollector_Windows.cs b/Vostok.Metrics.System/Process/NativeMetricsCollector_Windows.cs index d5e7830..52142b5 100644 --- a/Vostok.Metrics.System/Process/NativeMetricsCollector_Windows.cs +++ b/Vostok.Metrics.System/Process/NativeMetricsCollector_Windows.cs @@ -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 @@ -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) { From 133a22f064c8839d5fcdc2b8cde4aa7c9e05cad2 Mon Sep 17 00:00:00 2001 From: "d.stukov" Date: Mon, 26 Jun 2023 14:45:40 +0500 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6915f3..f9e314f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. From 8085d1df5ad2455733642c8781ebf783737aad87 Mon Sep 17 00:00:00 2001 From: "d.stukov" Date: Wed, 28 Jun 2023 13:52:48 +0500 Subject: [PATCH 3/3] review: update CpuUtilizedCores docs --- Vostok.Metrics.System/Process/CurrentProcessMetrics.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Vostok.Metrics.System/Process/CurrentProcessMetrics.cs b/Vostok.Metrics.System/Process/CurrentProcessMetrics.cs index 617ca95..b32f00e 100644 --- a/Vostok.Metrics.System/Process/CurrentProcessMetrics.cs +++ b/Vostok.Metrics.System/Process/CurrentProcessMetrics.cs @@ -8,7 +8,7 @@ public class CurrentProcessMetrics { /// /// Number of CPU cores utilized by current process. - /// This metric has a value between 0 and . + /// This metric has a value between 0 and the total number of logical processors on the machine. /// This metric is an average value between two observation moments (current and previous). /// public double CpuUtilizedCores { get; set; }