21
21
// ---------------------------------------------------------------------
22
22
23
23
using System ;
24
+ using System . Collections . Generic ;
25
+ using System . Collections . ObjectModel ;
24
26
using System . Diagnostics . Tracing ;
25
27
using System . Threading ;
26
28
@@ -32,71 +34,76 @@ namespace Elasticsearch.Net
32
34
/// </summary>
33
35
internal class EventCounter
34
36
{
35
- public EventCounter ( string blocks , object eventsWriter ) { }
37
+ public EventCounter ( string blocks , RecyclableMemoryStreamManager . Events eventsWriter ) { }
36
38
37
39
public void WriteMetric ( long v ) { }
38
40
}
39
41
#endif
42
+ #if ! NETSTANDARD2_1
43
+ internal class PollingCounter : IDisposable
44
+ {
45
+ public PollingCounter ( string largeBuffers , RecyclableMemoryStreamManager . Events eventsWriter , Func < double > func ) { }
46
+
47
+ public void Dispose ( ) { }
48
+ }
49
+ #endif
40
50
41
51
internal sealed partial class RecyclableMemoryStreamManager
42
52
{
43
53
public static readonly Events EventsWriter = new Events ( ) ;
44
- public static readonly Counters Counter = new Counters ( ) ;
54
+ private Counters Counter { get ; }
45
55
46
- public sealed class Counters
56
+ public sealed class Counters : IDisposable
47
57
{
48
- public Counters ( )
58
+ private ReadOnlyCollection < PollingCounter > Polls { get ; }
59
+
60
+ public Counters ( RecyclableMemoryStreamManager instance )
49
61
{
50
- BlocksCounter = new EventCounter ( "blocks" , EventsWriter ) ;
51
- BufferCounter = new EventCounter ( "large-buffers" , EventsWriter ) ;
52
- MemoryStreamCounter = new EventCounter ( "memorystreams" , EventsWriter ) ;
53
- }
62
+ PollingCounter Create ( string name , Func < double > poll , string description ) =>
63
+ new PollingCounter ( name , EventsWriter , poll )
64
+ {
65
+ #if NETSTANDARD2_1
66
+ DisplayName = description
67
+ #endif
68
+ } ;
54
69
55
- private EventCounter BlocksCounter { get ; }
56
- private EventCounter BufferCounter { get ; }
57
- private EventCounter MemoryStreamCounter { get ; }
58
70
59
- private long _blocks = 0 ;
60
- internal void ReportBlockCreated ( )
61
- {
62
- var count = Interlocked . Increment ( ref _blocks ) ;
63
- BlocksCounter . WriteMetric ( count ) ;
71
+ var polls = new List < PollingCounter > ( )
72
+ {
73
+ { Create ( "blocks" , ( ) => _blocks , "Pooled blocks active" ) } ,
74
+ { Create ( "large-buffers" , ( ) => _largeBuffers , "Large buffers active" ) } ,
75
+ { Create ( "large-buffers-free" , ( ) => instance . LargeBuffersFree , "Large buffers free" ) } ,
76
+ { Create ( "large-pool-inuse" , ( ) => instance . LargePoolInUseSize , "Large pool in use size" ) } ,
77
+ { Create ( "small-pool-free" , ( ) => instance . SmallBlocksFree , "Small pool free blocks" ) } ,
78
+ { Create ( "small-pool-inuse" , ( ) => instance . SmallPoolInUseSize , "Small pool in use size" ) } ,
79
+ { Create ( "small-pool-free" , ( ) => instance . SmallPoolFreeSize , "Small pool free size" ) } ,
80
+ { Create ( "small-pool-max" , ( ) => instance . MaximumFreeSmallPoolBytes , "Small pool max size" ) } ,
81
+ { Create ( "memory-streams" , ( ) => _memoryStreams , "Active memory streams" ) } ,
82
+ } ;
83
+ Polls = new ReadOnlyCollection < PollingCounter > ( polls ) ;
84
+
64
85
65
86
}
66
87
67
- internal void ReportBlockDiscarded ( )
68
- {
69
- var count = Interlocked . Decrement ( ref _blocks ) ;
70
- BlocksCounter . WriteMetric ( count ) ;
88
+ private long _blocks = 0 ;
89
+ internal void ReportBlockCreated ( ) => Interlocked . Increment ( ref _blocks ) ;
71
90
72
- }
91
+ internal void ReportBlockDiscarded ( ) => Interlocked . Decrement ( ref _blocks ) ;
73
92
74
93
private long _largeBuffers = 0 ;
75
- internal void ReportLargeBufferCreated ( )
76
- {
77
- var count = Interlocked . Increment ( ref _largeBuffers ) ;
78
- BufferCounter . WriteMetric ( count ) ;
79
- }
94
+ internal void ReportLargeBufferCreated ( ) => Interlocked . Increment ( ref _largeBuffers ) ;
80
95
81
- internal void ReportLargeBufferDiscarded ( )
82
- {
83
- var count = Interlocked . Decrement ( ref _largeBuffers ) ;
84
- BufferCounter . WriteMetric ( count ) ;
85
- }
96
+ internal void ReportLargeBufferDiscarded ( ) => Interlocked . Decrement ( ref _largeBuffers ) ;
86
97
87
98
private long _memoryStreams = 0 ;
88
- internal void ReportStreamCreated ( )
89
- {
90
- var count = Interlocked . Decrement ( ref _memoryStreams ) ;
91
- MemoryStreamCounter . WriteMetric ( count ) ;
92
- }
99
+ internal void ReportStreamCreated ( ) => Interlocked . Increment ( ref _memoryStreams ) ;
93
100
94
- internal void ReportStreamDisposed ( )
101
+ internal void ReportStreamDisposed ( ) => Interlocked . Decrement ( ref _memoryStreams ) ;
102
+
103
+ public void Dispose ( )
95
104
{
96
- var count = Interlocked . Decrement ( ref _memoryStreams ) ;
97
- MemoryStreamCounter . WriteMetric ( count ) ;
105
+ foreach ( var p in Polls ) p . Dispose ( ) ;
98
106
}
99
-
100
107
}
101
108
102
109
[ EventSource ( Name = "Elasticsearch-Net-RecyclableMemoryStream" , Guid = "{AD44FDAC-D3FC-460A-9EBE-E55A3569A8F6}" ) ]
0 commit comments