Skip to content

Commit d0ecb01

Browse files
committed
Reset IncrementingPollingCounter statistics on update
1 parent 991ae97 commit d0ecb01

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private void ResetCounters()
203203
}
204204
else if (counter is IncrementingPollingCounter ipCounter)
205205
{
206-
ipCounter.UpdateMetric();
206+
ipCounter.ResetStatistics();
207207
}
208208
else if (counter is EventCounter eCounter)
209209
{

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/IncrementingPollingCounter.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using System.Runtime.Versioning;
6+
using System.Threading;
67

78
namespace System.Diagnostics.Tracing
89
{
@@ -43,19 +44,40 @@ public IncrementingPollingCounter(string name, EventSource eventSource, Func<dou
4344
private double _increment;
4445
private double _prevIncrement;
4546
private readonly Func<double> _totalValueProvider;
47+
private bool _resetStatistics;
48+
49+
internal void ResetStatistics()
50+
{
51+
lock (this)
52+
{
53+
_resetStatistics = true;
54+
}
55+
}
4656

4757
/// <summary>
4858
/// Calls "_totalValueProvider" to enqueue the counter value to the queue.
4959
/// </summary>
50-
internal void UpdateMetric()
60+
private void UpdateMetric()
5161
{
52-
try
62+
lock (this)
5363
{
54-
lock (this)
64+
if (_resetStatistics)
5565
{
56-
_prevIncrement = _increment;
57-
_increment = _totalValueProvider();
66+
UpdateMetricInternal();
67+
_resetStatistics = false;
5868
}
69+
70+
UpdateMetricInternal();
71+
}
72+
}
73+
74+
private void UpdateMetricInternal()
75+
{
76+
Debug.Assert(Monitor.IsEntered(this));
77+
try
78+
{
79+
_prevIncrement = _increment;
80+
_increment = _totalValueProvider();
5981
}
6082
catch (Exception ex)
6183
{

0 commit comments

Comments
 (0)