Skip to content

Commit 35cac4e

Browse files
committed
fb
1 parent b903a1f commit 35cac4e

File tree

4 files changed

+18
-65
lines changed

4 files changed

+18
-65
lines changed

src/Servers/HttpSys/src/HttpSysListener.cs

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

44
using System.Buffers;
55
using System.Diagnostics;
6-
using System.Diagnostics.Metrics;
76
using Microsoft.AspNetCore.Connections;
87
using Microsoft.AspNetCore.Http;
98
using Microsoft.AspNetCore.HttpSys.Internal;
@@ -35,13 +34,6 @@ internal sealed partial class HttpSysListener : IDisposable
3534
// 0.5 seconds per request. Respond with a 400 Bad Request.
3635
private const int UnknownHeaderLimit = 1000;
3736

38-
internal sealed class NoopMeterFactory : IMeterFactory
39-
{
40-
public Meter Create(MeterOptions options) => new Meter(options);
41-
42-
public void Dispose() { }
43-
}
44-
4537
internal MemoryPool<byte> MemoryPool { get; }
4638

4739
private volatile State _state; // m_State is set only within lock blocks, but often read outside locks.

src/Shared/Buffers.MemoryPool/DefaultMemoryPoolFactory.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
using System.Diagnostics;
77
using System.Diagnostics.Metrics;
88
using Microsoft.AspNetCore.Connections;
9+
using Microsoft.Extensions.Logging;
910

1011
namespace Microsoft.AspNetCore;
1112

1213
#nullable enable
1314

1415
internal sealed class DefaultMemoryPoolFactory : IMemoryPoolFactory<byte>, IAsyncDisposable
1516
{
16-
private readonly IMeterFactory _meterFactory;
17-
private readonly ConcurrentDictionary<PinnedBlockMemoryPool, PinnedBlockMemoryPool> _pools = new();
17+
private readonly IMeterFactory? _meterFactory;
18+
private readonly ConcurrentDictionary<PinnedBlockMemoryPool, bool> _pools = new();
1819
private readonly PeriodicTimer _timer;
1920
private readonly Task _timerTask;
2021

21-
public DefaultMemoryPoolFactory(IMeterFactory? meterFactory = null)
22+
public DefaultMemoryPoolFactory(IMeterFactory? meterFactory = null, ILogger<DefaultMemoryPoolFactory>? logger = null)
2223
{
23-
_meterFactory = meterFactory ?? NoopMeterFactory.Instance;
24+
_meterFactory = meterFactory;
2425
_timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
2526
_timerTask = Task.Run(async () =>
2627
{
@@ -36,7 +37,7 @@ public DefaultMemoryPoolFactory(IMeterFactory? meterFactory = null)
3637
}
3738
catch (Exception ex)
3839
{
39-
Debug.WriteLine(ex);
40+
logger?.LogCritical(ex, "Error while evicting memory from pools.");
4041
}
4142
});
4243
}
@@ -45,11 +46,11 @@ public MemoryPool<byte> Create()
4546
{
4647
var pool = new PinnedBlockMemoryPool(_meterFactory);
4748

48-
_pools.TryAdd(pool, pool);
49+
_pools.TryAdd(pool, true);
4950

5051
pool.OnPoolDisposed(static (state, self) =>
5152
{
52-
((ConcurrentDictionary<PinnedBlockMemoryPool, PinnedBlockMemoryPool>)state!).TryRemove(self, out _);
53+
((ConcurrentDictionary<PinnedBlockMemoryPool, bool>)state!).TryRemove(self, out _);
5354
}, _pools);
5455

5556
return pool;
@@ -60,15 +61,4 @@ public async ValueTask DisposeAsync()
6061
_timer.Dispose();
6162
await _timerTask;
6263
}
63-
64-
private sealed class NoopMeterFactory : IMeterFactory
65-
{
66-
public static NoopMeterFactory Instance = new NoopMeterFactory();
67-
68-
public Meter Create(MeterOptions options) => new Meter(options);
69-
70-
public void Dispose()
71-
{
72-
}
73-
}
7464
}

src/Shared/Buffers.MemoryPool/MemoryPoolFactory.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ internal static class TestMemoryPoolFactory
1212
{
1313
public static MemoryPool<byte> Create(IMeterFactory? meterFactory = null)
1414
{
15-
meterFactory ??= NoopMeterFactory.Instance;
1615
#if DEBUG
1716
return new DiagnosticMemoryPool(CreatePinnedBlockMemoryPool(meterFactory));
1817
#else
@@ -22,18 +21,6 @@ public static MemoryPool<byte> Create(IMeterFactory? meterFactory = null)
2221

2322
public static MemoryPool<byte> CreatePinnedBlockMemoryPool(IMeterFactory? meterFactory = null)
2423
{
25-
meterFactory ??= NoopMeterFactory.Instance;
2624
return new PinnedBlockMemoryPool(meterFactory);
2725
}
28-
29-
private sealed class NoopMeterFactory : IMeterFactory
30-
{
31-
public static NoopMeterFactory Instance = new NoopMeterFactory();
32-
33-
public Meter Create(MeterOptions options) => new Meter(options);
34-
35-
public void Dispose()
36-
{
37-
}
38-
}
3926
}

src/Shared/Buffers.MemoryPool/PinnedBlockMemoryPool.cs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal sealed class PinnedBlockMemoryPool : MemoryPool<byte>, IThreadPoolWorkI
4141
/// </summary>
4242
private bool _isDisposed; // To detect redundant calls
4343

44-
private readonly PinnedBlockMemoryPoolMetrics _metrics;
44+
private readonly PinnedBlockMemoryPoolMetrics? _metrics;
4545

4646
private long _currentMemory;
4747
private long _evictedMemory;
@@ -60,14 +60,9 @@ internal sealed class PinnedBlockMemoryPool : MemoryPool<byte>, IThreadPoolWorkI
6060
/// </summary>
6161
private const int AnySize = -1;
6262

63-
public PinnedBlockMemoryPool()
64-
: this(NoopMeterFactory.Instance)
63+
public PinnedBlockMemoryPool(IMeterFactory? meterFactory = null)
6564
{
66-
}
67-
68-
public PinnedBlockMemoryPool(IMeterFactory meterFactory)
69-
{
70-
_metrics = new(meterFactory);
65+
_metrics = meterFactory is null ? null : new PinnedBlockMemoryPoolMetrics(meterFactory);
7166
}
7267

7368
/// <summary>
@@ -95,16 +90,16 @@ public override IMemoryOwner<byte> Rent(int size = AnySize)
9590

9691
if (_blocks.TryDequeue(out var block))
9792
{
98-
_metrics.UpdateCurrentMemory(-block.Memory.Length);
99-
_metrics.Rent(block.Memory.Length);
93+
_metrics?.UpdateCurrentMemory(-block.Memory.Length);
94+
_metrics?.Rent(block.Memory.Length);
10095
Interlocked.Add(ref _currentMemory, -block.Memory.Length);
10196

10297
// block successfully taken from the stack - return it
10398
return block;
10499
}
105100

106-
_metrics.IncrementTotalMemory(BlockSize);
107-
_metrics.Rent(BlockSize);
101+
_metrics?.IncrementTotalMemory(BlockSize);
102+
_metrics?.Rent(BlockSize);
108103

109104
// We already counted this Rent call above, but since we're now allocating (need more blocks)
110105
// that means the pool is 'very' active and we probably shouldn't evict blocks, so we count again
@@ -134,7 +129,7 @@ internal void Return(MemoryPoolBlock block)
134129

135130
if (!_isDisposed)
136131
{
137-
_metrics.UpdateCurrentMemory(block.Memory.Length);
132+
_metrics?.UpdateCurrentMemory(block.Memory.Length);
138133
Interlocked.Add(ref _currentMemory, block.Memory.Length);
139134

140135
_blocks.Enqueue(block);
@@ -201,8 +196,8 @@ internal void PerformEviction()
201196
// Remove from queue and let GC clean the memory up
202197
while (burstAmount > 0 && _blocks.TryDequeue(out var block))
203198
{
204-
_metrics.UpdateCurrentMemory(-block.Memory.Length);
205-
_metrics.EvictBlock(block.Memory.Length);
199+
_metrics?.UpdateCurrentMemory(-block.Memory.Length);
200+
_metrics?.EvictBlock(block.Memory.Length);
206201
Interlocked.Add(ref _currentMemory, -block.Memory.Length);
207202
Interlocked.Add(ref _evictedMemory, block.Memory.Length);
208203

@@ -236,15 +231,4 @@ protected override void Dispose(bool disposing)
236231

237232
// Used for testing
238233
public int BlockCount() => _blocks.Count;
239-
240-
private sealed class NoopMeterFactory : IMeterFactory
241-
{
242-
public static NoopMeterFactory Instance { get; } = new();
243-
244-
public Meter Create(MeterOptions options) => new Meter(options);
245-
246-
public void Dispose()
247-
{
248-
}
249-
}
250234
}

0 commit comments

Comments
 (0)