From 5b391396f199bb1f3fd175c9712ecef840ab1fa9 Mon Sep 17 00:00:00 2001 From: David Mason Date: Thu, 23 Mar 2023 02:48:28 -0700 Subject: [PATCH] Treat enable with interval as 0 as disable --- .../Diagnostics/Tracing/CounterGroup.cs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs index 7a50a35c7e9a4..a39717f1d840d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs @@ -53,9 +53,7 @@ private void OnEventSourceCommand(object? sender, EventCommandEventArgs e) if (e.Command == EventCommand.Enable) { Debug.Assert(e.Arguments != null); - Debug.Assert(_refCount >= 0); - ++_refCount; float intervalValue = 1.0f; if (e.Arguments.TryGetValue("EventCounterIntervalSec", out string? valueStr) && float.TryParse(valueStr, out float value)) @@ -63,7 +61,18 @@ private void OnEventSourceCommand(object? sender, EventCommandEventArgs e) intervalValue = value; } - EnableTimer(intervalValue); + if (intervalValue > 0) + { + Debug.Assert(_refCount >= 0); + ++_refCount; + EnableTimer(intervalValue); + } + else + { + Debug.Assert(_refCount >= 1); + --_refCount; + DisableTimer(); + } } else if (e.Command == EventCommand.Disable) { @@ -140,11 +149,7 @@ internal static CounterGroup GetCounterGroup(EventSource eventSource) private void EnableTimer(float pollingIntervalInSeconds) { Debug.Assert(Monitor.IsEntered(s_counterGroupLock)); - if (pollingIntervalInSeconds <= 0) - { - DisableTimer(); - } - else if (_pollingIntervalInMilliseconds == 0 || pollingIntervalInSeconds * 1000 < _pollingIntervalInMilliseconds) + if (pollingIntervalInSeconds * 1000 < _pollingIntervalInMilliseconds) { _pollingIntervalInMilliseconds = (int)(pollingIntervalInSeconds * 1000); ResetCounters(); // Reset statistics for counters before we start the thread.