< Summary

Line coverage
1%
Covered lines: 1
Uncovered lines: 54
Coverable lines: 55
Total lines: 146
Line coverage: 1.8%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
BufferRented(...)100%110%
BufferAllocated(...)100%110%
BufferReturned(...)100%110%
BufferTrimmed(...)100%110%
BufferTrimPoll(...)100%110%
BufferDropped(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Buffers/ArrayPoolEventSource.cs

#LineLine coverage
 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
 4using System.Diagnostics.CodeAnalysis;
 5using System.Diagnostics.Tracing;
 6
 7namespace System.Buffers
 8{
 9    [EventSource(Guid = "0866B2B8-5CEF-5DB9-2612-0C0FFD814A44", Name = "System.Buffers.ArrayPoolEventSource")]
 10    internal sealed partial class ArrayPoolEventSource : EventSource
 11    {
 12        private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe"
 113        internal static readonly ArrayPoolEventSource Log = new ArrayPoolEventSource();
 14
 15        /// <summary>Bucket ID used when renting/returning an array that's too large for a pool.</summary>
 16        internal const int NoBucketId = -1;
 17
 18        /// <summary>The reason for a BufferAllocated event.</summary>
 19        internal enum BufferAllocatedReason : int
 20        {
 21            /// <summary>The pool is allocating a buffer to be pooled in a bucket.</summary>
 22            Pooled,
 23            /// <summary>The requested buffer size was too large to be pooled.</summary>
 24            OverMaximumSize,
 25            /// <summary>The pool has already allocated for pooling as many buffers of a particular size as it's allowed
 26            PoolExhausted
 27        }
 28
 29        /// <summary>The reason for a BufferDropped event.</summary>
 30        internal enum BufferDroppedReason : int
 31        {
 32            /// <summary>The pool is full for buffers of the specified size.</summary>
 33            Full,
 34            /// <summary>The buffer size was too large to be pooled.</summary>
 35            OverMaximumSize,
 36        }
 37
 38        /// <summary>
 39        /// Event for when a buffer is rented.  This is invoked once for every successful call to Rent,
 40        /// regardless of whether a buffer is allocated or a buffer is taken from the pool.  In a
 41        /// perfect situation where all rented buffers are returned, we expect to see the number
 42        /// of BufferRented events exactly match the number of BuferReturned events, with the number
 43        /// of BufferAllocated events being less than or equal to those numbers (ideally significantly
 44        /// less than).
 45        /// </summary>
 46        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
 47                   Justification = EventSourceSuppressMessage)]
 48        [Event(1, Level = EventLevel.Verbose)]
 49        internal unsafe void BufferRented(int bufferId, int bufferSize, int poolId, int bucketId)
 50        {
 051            EventData* payload = stackalloc EventData[4];
 052            payload[0].Size = sizeof(int);
 053            payload[0].DataPointer = ((IntPtr)(&bufferId));
 054            payload[0].Reserved = 0;
 055            payload[1].Size = sizeof(int);
 056            payload[1].DataPointer = ((IntPtr)(&bufferSize));
 057            payload[1].Reserved = 0;
 058            payload[2].Size = sizeof(int);
 059            payload[2].DataPointer = ((IntPtr)(&poolId));
 060            payload[2].Reserved = 0;
 061            payload[3].Size = sizeof(int);
 062            payload[3].DataPointer = ((IntPtr)(&bucketId));
 063            payload[3].Reserved = 0;
 064            WriteEventCore(1, 4, payload);
 065        }
 66
 67        /// <summary>
 68        /// Event for when a buffer is allocated by the pool.  In an ideal situation, the number
 69        /// of BufferAllocated events is significantly smaller than the number of BufferRented and
 70        /// BufferReturned events.
 71        /// </summary>
 72        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
 73                   Justification = EventSourceSuppressMessage)]
 74        [Event(2, Level = EventLevel.Informational)]
 75        internal unsafe void BufferAllocated(int bufferId, int bufferSize, int poolId, int bucketId, BufferAllocatedReas
 76        {
 077            EventData* payload = stackalloc EventData[5];
 078            payload[0].Size = sizeof(int);
 079            payload[0].DataPointer = ((IntPtr)(&bufferId));
 080            payload[0].Reserved = 0;
 081            payload[1].Size = sizeof(int);
 082            payload[1].DataPointer = ((IntPtr)(&bufferSize));
 083            payload[1].Reserved = 0;
 084            payload[2].Size = sizeof(int);
 085            payload[2].DataPointer = ((IntPtr)(&poolId));
 086            payload[2].Reserved = 0;
 087            payload[3].Size = sizeof(int);
 088            payload[3].DataPointer = ((IntPtr)(&bucketId));
 089            payload[3].Reserved = 0;
 090            payload[4].Size = sizeof(BufferAllocatedReason);
 091            payload[4].DataPointer = ((IntPtr)(&reason));
 092            payload[4].Reserved = 0;
 093            WriteEventCore(2, 5, payload);
 094        }
 95
 96        /// <summary>
 97        /// Event raised when a buffer is returned to the pool.  This event is raised regardless of whether
 98        /// the returned buffer is stored or dropped.  In an ideal situation, the number of BufferReturned
 99        /// events exactly matches the number of BufferRented events.
 100        /// </summary>
 101        [Event(3, Level = EventLevel.Verbose)]
 0102        internal void BufferReturned(int bufferId, int bufferSize, int poolId) => WriteEvent(3, bufferId, bufferSize, po
 103
 104        /// <summary>
 105        /// Event raised when we attempt to free a buffer due to inactivity or memory pressure (by no longer
 106        /// referencing it). It is possible (although not common) this buffer could be rented as we attempt
 107        /// to free it. A rent event before or after this event for the same ID, is a rare, but expected case.
 108        /// </summary>
 109        [Event(4, Level = EventLevel.Informational)]
 0110        internal void BufferTrimmed(int bufferId, int bufferSize, int poolId) => WriteEvent(4, bufferId, bufferSize, poo
 111
 112        /// <summary>
 113        /// Event raised when we check to trim buffers.
 114        /// </summary>
 115        [Event(5, Level = EventLevel.Informational)]
 0116        internal void BufferTrimPoll(int milliseconds, int pressure) => WriteEvent(5, milliseconds, pressure);
 117
 118        /// <summary>
 119        /// Event raised when a buffer returned to the pool is dropped.
 120        /// </summary>
 121        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
 122                   Justification = EventSourceSuppressMessage)]
 123        [Event(6, Level = EventLevel.Informational)]
 124        internal unsafe void BufferDropped(int bufferId, int bufferSize, int poolId, int bucketId, BufferDroppedReason r
 125        {
 0126            EventData* payload = stackalloc EventData[5];
 0127            payload[0].Size = sizeof(int);
 0128            payload[0].DataPointer = ((IntPtr)(&bufferId));
 0129            payload[0].Reserved = 0;
 0130            payload[1].Size = sizeof(int);
 0131            payload[1].DataPointer = ((IntPtr)(&bufferSize));
 0132            payload[1].Reserved = 0;
 0133            payload[2].Size = sizeof(int);
 0134            payload[2].DataPointer = ((IntPtr)(&poolId));
 0135            payload[2].Reserved = 0;
 0136            payload[3].Size = sizeof(int);
 0137            payload[3].DataPointer = ((IntPtr)(&bucketId));
 0138            payload[3].Reserved = 0;
 0139            payload[4].Size = sizeof(BufferDroppedReason);
 0140            payload[4].DataPointer = ((IntPtr)(&reason));
 0141            payload[4].Reserved = 0;
 0142            WriteEventCore(6, 5, payload);
 0143        }
 144    }
 145}
 146