Skip to content

Commit

Permalink
Remove performance counters from RecyclableMemoryStream (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon authored Nov 4, 2022
1 parent f1cfdf2 commit d6e42bf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,63 +60,7 @@ public void Dispose() {}
internal sealed partial class RecyclableMemoryStreamManager
{
public static readonly Events EventsWriter = new Events();
private Counters Counter { get; }

public sealed class Counters : IDisposable
{
private ReadOnlyCollection<PollingCounter> Polls { get;}

public Counters(RecyclableMemoryStreamManager instance)
{
// ReSharper disable once UnusedParameter.Local
PollingCounter Create(string name, Func<double> poll, string description) =>
new PollingCounter(name, EventsWriter, poll)
// ReSharper disable once RedundantEmptyObjectOrCollectionInitializer
{
#if !NETSTANDARD2_0 && !NETFRAMEWORK
DisplayName = description
#endif
};


var polls = new List<PollingCounter>()
{
{ Create("blocks", () => _blocks, "Pooled blocks active")},
{ Create("large-buffers", () => _largeBuffers, "Large buffers active")},
{ Create("large-buffers-free", () => instance.LargeBuffersFree, "Large buffers free")},
{ Create("large-pool-inuse", () => instance.LargePoolInUseSize, "Large pool in use size")},
{ Create("small-pool-free", () => instance.SmallBlocksFree, "Small pool free blocks")},
{ Create("small-pool-inuse", () => instance.SmallPoolInUseSize, "Small pool in use size")},
{ Create("small-pool-free", () => instance.SmallPoolFreeSize, "Small pool free size")},
{ Create("small-pool-max", () => instance.MaximumFreeSmallPoolBytes, "Small pool max size")},
{ Create("memory-streams", () => _memoryStreams, "Active memory streams")},
};
Polls = new ReadOnlyCollection<PollingCounter>(polls);


}

private long _blocks;
internal void ReportBlockCreated() => Interlocked.Increment(ref _blocks);

internal void ReportBlockDiscarded() => Interlocked.Decrement(ref _blocks);

private long _largeBuffers;
internal void ReportLargeBufferCreated() => Interlocked.Increment(ref _largeBuffers);

internal void ReportLargeBufferDiscarded() => Interlocked.Decrement(ref _largeBuffers);

private long _memoryStreams;
internal void ReportStreamCreated() => Interlocked.Increment(ref _memoryStreams);

internal void ReportStreamDisposed() => Interlocked.Decrement(ref _memoryStreams);

public void Dispose()
{
foreach(var p in Polls) p.Dispose();
}
}


[EventSource(Name = "Elastic-Transport-RecyclableMemoryStream", Guid = "{AD44FDAC-D3FC-460A-9EBE-E55A3569A8F6}")]
public sealed class Events : EventSource
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ public RecyclableMemoryStreamManager(int blockSize, int largeBufferMultiple, int

for (var i = 0; i < _largePools.Length; ++i) _largePools[i] = new ConcurrentStack<byte[]>();

Counter = new Counters(this);
EventsWriter.MemoryStreamManagerInitialized(blockSize, largeBufferMultiple, maximumBufferSize);
}

Expand Down Expand Up @@ -479,41 +478,17 @@ internal void ReturnBlocks(ICollection<byte[]> blocks, string tag)
LargePoolFreeSize);
}

internal void ReportBlockCreated()
{
Counter.ReportBlockCreated();
BlockCreated?.Invoke();
}
internal void ReportBlockCreated() => BlockCreated?.Invoke();

internal void ReportBlockDiscarded()
{
Counter.ReportBlockDiscarded();
BlockDiscarded?.Invoke();
}
internal void ReportBlockDiscarded() => BlockDiscarded?.Invoke();

internal void ReportLargeBufferCreated()
{
Counter.ReportLargeBufferCreated();
LargeBufferCreated?.Invoke();
}
internal void ReportLargeBufferCreated() => LargeBufferCreated?.Invoke();

internal void ReportLargeBufferDiscarded(Events.MemoryStreamDiscardReason reason)
{
Counter.ReportLargeBufferDiscarded();
LargeBufferDiscarded?.Invoke(reason);
}
internal void ReportLargeBufferDiscarded(Events.MemoryStreamDiscardReason reason) => LargeBufferDiscarded?.Invoke(reason);

internal void ReportStreamCreated()
{
Counter.ReportStreamCreated();
StreamCreated?.Invoke();
}
internal void ReportStreamCreated() => StreamCreated?.Invoke();

internal void ReportStreamDisposed()
{
Counter.ReportStreamDisposed();
StreamDisposed?.Invoke();
}
internal void ReportStreamDisposed() => StreamDisposed?.Invoke();

internal void ReportStreamFinalized() => StreamFinalized?.Invoke();

Expand All @@ -536,14 +511,7 @@ private class ReportingMemoryStream : MemoryStream
{
private readonly RecyclableMemoryStreamManager _instance;

public ReportingMemoryStream(byte[] bytes, RecyclableMemoryStreamManager instance) : base(bytes)
{
_instance = instance;
_instance.Counter.ReportStreamCreated();
}

//NOTE DisposeAsync calls Dispose as well
protected override void Dispose(bool disposing) => _instance.Counter.ReportStreamDisposed();
public ReportingMemoryStream(byte[] bytes, RecyclableMemoryStreamManager instance) : base(bytes) => _instance = instance;
}

/// <summary>
Expand Down

0 comments on commit d6e42bf

Please sign in to comment.