Skip to content

[API Proposal]: PinnedBlockMemoryPool metrics #61594

Open
@BrennanConroy

Description

@BrennanConroy

Background and Motivation

Add metrics to the PinnedBlockMemoryPool that's used by Kestrel, IIS, and Http.Sys.

Proposed API

Microsoft.AspNetCore.MemoryPool

aspnetcore.memorypool.current_memory

Name Instrument Type Unit Description
aspnetcore.memorypool.current_memory UpDownCounter {bytes} Number of bytes that are currently pooled by the pool.
Attribute Type Description Examples Presence
owner string The name of part of the app using the pool instance. Sockets; NamedPipes; Application Recommended

aspnetcore.memorypool.total_allocated

Name Instrument Type Unit Description
aspnetcore.memorypool.total_allocated Counter {bytes} Total number of allocations made by the pool.
Attribute Type Description Examples Presence
owner string The name of part of the app using the pool instance. Sockets; NamedPipes; Application Recommended

aspnetcore.memorypool.evicted_memory

Name Instrument Type Unit Description
aspnetcore.memorypool.evicted_memory Counter {bytes} Total number of bytes that have been evicted.
Attribute Type Description Examples Presence
owner string The name of part of the app using the pool instance. Sockets; NamedPipes; Application Recommended

aspnetcore.memorypool.total_rented

Name Instrument Type Unit Description
aspnetcore.memorypool.total_rented Counter {bytes} Total number of rented bytes from the pool.
Attribute Type Description Examples Presence
owner string The name of part of the app using the pool instance. Sockets; NamedPipes; Application Recommended

Microsoft.AspNetCore.Connections.Abstractions.dll

namespace Microsoft.AspNetCore.Connections;

public interface IMemoryPoolFactory
{
-    MemoryPool<byte> CreatePool();
+    MemoryPool<byte> CreatePool(MemoryPoolOptions? options = null);
}

// Maybe not sealed to allow custom factory impls to pass through custom options?
+ public sealed class MemoryPoolOptions
+ {
+     public string? Owner { get; set; }
+ }

Usage

public class MyBackgroundService : BackgroundService
{
    private readonly MemoryPool<byte> _memoryPool;

    public MyBackgroundService(IMemoryPoolFactory factory)
    {
        _memoryPool = factory.CreatePool(new MemoryPoolOptions() { Owner = "BackgroundService" });
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            try
            {
                await Task.Delay(20, stoppingToken);
                var rented = _memoryPool.Rent(100);
                rented.Dispose();
            }
            catch (OperationCanceledException)
            {
                return;
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviewsarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions