Skip to content

Commit e3066f0

Browse files
committed
Reset IncrementingPollingCounter statistics on update
1 parent 81976ed commit e3066f0

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-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: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,37 @@ public IncrementingPollingCounter(string name, EventSource eventSource, Func<dou
4343
private double _increment;
4444
private double _prevIncrement;
4545
private readonly Func<double> _totalValueProvider;
46+
private bool _resetStatistics;
47+
48+
internal void ResetStatistics()
49+
{
50+
_resetStatistics = true;
51+
}
4652

4753
/// <summary>
4854
/// Calls "_totalValueProvider" to enqueue the counter value to the queue.
4955
/// </summary>
50-
internal void UpdateMetric()
56+
private void UpdateMetric()
5157
{
52-
try
58+
lock (this)
5359
{
54-
lock (this)
60+
if (_resetStatistics)
5561
{
56-
_prevIncrement = _increment;
57-
_increment = _totalValueProvider();
62+
UpdateMetricInternal()
63+
_resetStatistics = false;
5864
}
65+
66+
UpdateMetricInternal();
67+
}
68+
}
69+
70+
private void UpdateMetricInternal()
71+
{
72+
Debug.Assert(Monitor.IsEntered(this));
73+
try
74+
{
75+
_prevIncrement = _increment;
76+
_increment = _totalValueProvider();
5977
}
6078
catch (Exception ex)
6179
{

0 commit comments

Comments
 (0)