Skip to content

Commit ef2f62f

Browse files
authored
Server memory pool metrics updates (#63032)
1 parent a0991ea commit ef2f62f

27 files changed

+317
-146
lines changed

src/Servers/Connections.Abstractions/src/IMemoryPoolFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface IMemoryPoolFactory<T>
1313
/// <summary>
1414
/// Creates a new instance of a memory pool.
1515
/// </summary>
16+
/// <param name="options">Options for configuring the memory pool.</param>
1617
/// <returns>A new memory pool instance.</returns>
17-
MemoryPool<T> Create();
18+
MemoryPool<T> Create(MemoryPoolOptions? options = null);
1819
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Connections;
5+
6+
/// <summary>
7+
/// Options for configuring a memory pool.
8+
/// </summary>
9+
public class MemoryPoolOptions
10+
{
11+
/// <summary>
12+
/// Gets or sets the owner of the memory pool. This is used for logging and diagnostics purposes.
13+
/// </summary>
14+
public string? Owner { get; set; }
15+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#nullable enable
22
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>
3-
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create() -> System.Buffers.MemoryPool<T>!
3+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create(Microsoft.AspNetCore.Connections.MemoryPoolOptions? options = null) -> System.Buffers.MemoryPool<T>!
4+
Microsoft.AspNetCore.Connections.MemoryPoolOptions
5+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.MemoryPoolOptions() -> void
6+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.Owner.get -> string?
7+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.Owner.set -> void
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#nullable enable
22
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>
3-
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create() -> System.Buffers.MemoryPool<T>!
3+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create(Microsoft.AspNetCore.Connections.MemoryPoolOptions? options = null) -> System.Buffers.MemoryPool<T>!
4+
Microsoft.AspNetCore.Connections.MemoryPoolOptions
5+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.MemoryPoolOptions() -> void
6+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.Owner.get -> string?
7+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.Owner.set -> void
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#nullable enable
22
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>
3-
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create() -> System.Buffers.MemoryPool<T>!
3+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create(Microsoft.AspNetCore.Connections.MemoryPoolOptions? options = null) -> System.Buffers.MemoryPool<T>!
4+
Microsoft.AspNetCore.Connections.MemoryPoolOptions
5+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.MemoryPoolOptions() -> void
6+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.Owner.get -> string?
7+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.Owner.set -> void
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#nullable enable
22
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>
3-
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create() -> System.Buffers.MemoryPool<T>!
3+
Microsoft.AspNetCore.Connections.IMemoryPoolFactory<T>.Create(Microsoft.AspNetCore.Connections.MemoryPoolOptions? options = null) -> System.Buffers.MemoryPool<T>!
4+
Microsoft.AspNetCore.Connections.MemoryPoolOptions
5+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.MemoryPoolOptions() -> void
6+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.Owner.get -> string?
7+
Microsoft.AspNetCore.Connections.MemoryPoolOptions.Owner.set -> void

src/Servers/HttpSys/src/HttpSysListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public HttpSysListener(HttpSysOptions options, IMemoryPoolFactory<byte> memoryPo
5555
throw new PlatformNotSupportedException();
5656
}
5757

58-
MemoryPool = memoryPoolFactory.Create();
58+
MemoryPool = memoryPoolFactory.Create(new MemoryPoolOptions { Owner = "httpsys" });
5959

6060
Options = options;
6161

src/Servers/IIS/IIS/src/Core/IISHttpServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public IISHttpServer(
6666
ILogger<IISHttpServer> logger
6767
)
6868
{
69-
_memoryPool = memoryPoolFactory.Create();
69+
_memoryPool = memoryPoolFactory.Create(new MemoryPoolOptions { Owner = "iis" });
7070
_nativeApplication = nativeApplication;
7171
_applicationLifetime = applicationLifetime;
7272
_logger = logger;

src/Servers/IIS/IIS/src/WebHostBuilderIISExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static IWebHostBuilder UseIIS(this IWebHostBuilder hostBuilder)
5757
);
5858

5959
services.TryAddSingleton<IMemoryPoolFactory<byte>, DefaultMemoryPoolFactory>();
60+
services.TryAddSingleton<MemoryPoolMetrics>();
6061
});
6162
}
6263

src/Servers/Kestrel/Core/src/Internal/PinnedBlockMemoryPoolFactory.cs

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

44
using System.Buffers;
55
using System.Collections.Concurrent;
6-
using System.Diagnostics.Metrics;
76
using Microsoft.AspNetCore.Connections;
87
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
98
using Microsoft.Extensions.Logging;
@@ -12,22 +11,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
1211

1312
internal sealed class PinnedBlockMemoryPoolFactory : IMemoryPoolFactory<byte>, IHeartbeatHandler
1413
{
15-
private readonly IMeterFactory _meterFactory;
14+
private readonly MemoryPoolMetrics _metrics;
1615
private readonly ILogger? _logger;
1716
private readonly TimeProvider _timeProvider;
1817
// micro-optimization: Using nuint as the value type to avoid GC write barriers; could replace with ConcurrentHashSet if that becomes available
1918
private readonly ConcurrentDictionary<PinnedBlockMemoryPool, nuint> _pools = new();
2019

21-
public PinnedBlockMemoryPoolFactory(IMeterFactory meterFactory, TimeProvider? timeProvider = null, ILogger<PinnedBlockMemoryPoolFactory>? logger = null)
20+
public PinnedBlockMemoryPoolFactory(MemoryPoolMetrics metrics, TimeProvider? timeProvider = null, ILogger<PinnedBlockMemoryPoolFactory>? logger = null)
2221
{
2322
_timeProvider = timeProvider ?? TimeProvider.System;
24-
_meterFactory = meterFactory;
23+
_metrics = metrics;
2524
_logger = logger;
2625
}
2726

28-
public MemoryPool<byte> Create()
27+
public MemoryPool<byte> Create(MemoryPoolOptions? options = null)
2928
{
30-
var pool = new PinnedBlockMemoryPool(_meterFactory, _logger);
29+
var pool = new PinnedBlockMemoryPool(options?.Owner, _metrics, _logger);
3130

3231
_pools.TryAdd(pool, nuint.Zero);
3332

0 commit comments

Comments
 (0)