Skip to content

[API Proposal]: PinnedBlockMemoryPool metrics #61594

@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.pooled

Name Instrument Type Unit Description
aspnetcore.memorypool.pooled UpDownCounter By Number of bytes currently pooled and available for reuse.
Attribute Type Description Examples Presence
owner string The name of the library or subsystem using the memory pool instance. kestrel; iis if owner is specified when the memory pool is created.

aspnetcore.memorypool.allocated

Name Instrument Type Unit Description
aspnetcore.memorypool.allocated Counter By Total number of bytes allocated by the memory pool. Allocation occurs when a memory rental request exceeds the available pooled memory.
Attribute Type Description Examples Presence
owner string The name of the library or subsystem using the memory pool instance. kestrel; iis if owner is specified when the memory pool is created.

aspnetcore.memorypool.evicted

Name Instrument Type Unit Description
aspnetcore.memorypool.evicted Counter By Total number of bytes evicted from the memory pool. Eviction occurs when idle pooled memory is reclaimed.
Attribute Type Description Examples Presence
owner string The name of the library or subsystem using the memory pool instance. kestrel; iis if owner is specified when the memory pool is created.

aspnetcore.memorypool.rented

Name Instrument Type Unit Description
aspnetcore.memorypool.rented Counter By Total number of rented bytes from the pool.
Attribute Type Description Examples Presence
owner string The name of the library or subsystem using the memory pool instance. kestrel; iis if owner is specified when the memory pool is created.

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