forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add counters to System.Runtime.Caching.MemoryCache (dotnet#51760)
* Bring back counters for Core via EventSource et al. * Change turnover to a rate counter. Add a test for counters. * Exclude counter tests on wasm.
- Loading branch information
1 parent
10645f9
commit b2046c7
Showing
8 changed files
with
289 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/Counters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Tracing; | ||
using System.Threading; | ||
|
||
namespace System.Runtime.Caching | ||
{ | ||
internal sealed class Counters : EventSource | ||
{ | ||
#if NETCOREAPP3_1_OR_GREATER | ||
private const string EVENT_SOURCE_NAME_ROOT = "System.Runtime.Caching."; | ||
private const int NUM_COUNTERS = 7; | ||
|
||
private DiagnosticCounter[] _counters; | ||
private long[] _counterValues; | ||
|
||
internal Counters(string cacheName) : base(EVENT_SOURCE_NAME_ROOT + cacheName) | ||
{ | ||
if (cacheName == null) | ||
{ | ||
throw new ArgumentNullException(nameof(cacheName)); | ||
} | ||
|
||
InitDisposableMembers(cacheName); | ||
} | ||
|
||
private void InitDisposableMembers(string cacheName) | ||
{ | ||
bool dispose = true; | ||
|
||
try | ||
{ | ||
_counters = new DiagnosticCounter[NUM_COUNTERS]; | ||
_counterValues = new long[NUM_COUNTERS]; | ||
_counters[(int)CounterName.Entries] = CreatePollingCounter("entries", "Cache Entries", (int)CounterName.Entries); | ||
_counters[(int)CounterName.Hits] = CreatePollingCounter("hits", "Cache Hits", (int)CounterName.Hits); | ||
_counters[(int)CounterName.Misses] = CreatePollingCounter("misses", "Cache Misses", (int)CounterName.Misses); | ||
_counters[(int)CounterName.Trims] = CreatePollingCounter("trims", "Cache Trims", (int)CounterName.Trims); | ||
|
||
_counters[(int)CounterName.Turnover] = new IncrementingPollingCounter("turnover", this, | ||
() => (double)_counterValues[(int)CounterName.Turnover]) | ||
{ | ||
DisplayName = "Cache Turnover Rate", | ||
}; | ||
|
||
// This two-step dance with hit-ratio was an old perf-counter artifact. There only needs | ||
// to be one polling counter here, rather than the two-part perf counter. Still keeping array | ||
// indexes and raw counter values consistent between NetFx and Core code though. | ||
_counters[(int)CounterName.HitRatio] = new PollingCounter("hit-ratio", this, | ||
() =>((double)_counterValues[(int)CounterName.HitRatio]/(double)_counterValues[(int)CounterName.HitRatioBase]) * 100d) | ||
{ | ||
DisplayName = "Cache Hit Ratio", | ||
}; | ||
//_counters[(int)CounterName.HitRatioBase] = n/a; | ||
|
||
dispose = false; | ||
} | ||
finally | ||
{ | ||
if (dispose) | ||
Dispose(); | ||
} | ||
} | ||
|
||
private PollingCounter CreatePollingCounter(string name, string displayName, int counterIndex) | ||
{ | ||
return new PollingCounter(name, this, () => (double)_counterValues[counterIndex]) | ||
{ | ||
DisplayName = displayName, | ||
}; | ||
} | ||
|
||
public new void Dispose() | ||
{ | ||
DiagnosticCounter[] counters = _counters; | ||
|
||
// ensure this only happens once | ||
if (counters != null && Interlocked.CompareExchange(ref _counters, null, counters) == counters) | ||
{ | ||
for (int i = 0; i < NUM_COUNTERS; i++) | ||
{ | ||
var counter = counters[i]; | ||
if (counter != null) | ||
{ | ||
counter.Dispose(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal void Increment(CounterName name) | ||
{ | ||
int idx = (int)name; | ||
Interlocked.Increment(ref _counterValues[idx]); | ||
} | ||
internal void IncrementBy(CounterName name, long value) | ||
{ | ||
int idx = (int)name; | ||
Interlocked.Add(ref _counterValues[idx], value); | ||
} | ||
internal void Decrement(CounterName name) | ||
{ | ||
int idx = (int)name; | ||
Interlocked.Decrement(ref _counterValues[idx]); | ||
} | ||
#else | ||
internal Counters(string cacheName) | ||
{ | ||
} | ||
public new void Dispose() | ||
{ | ||
} | ||
internal void Increment(CounterName name) | ||
{ | ||
} | ||
internal void IncrementBy(CounterName name, long value) | ||
{ | ||
} | ||
internal void Decrement(CounterName name) | ||
{ | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/_shims.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.