| | | 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 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Diagnostics.Tracing; |
| | | 6 | | |
| | | 7 | | namespace 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" |
| | 1 | 13 | | 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 | | { |
| | 0 | 51 | | EventData* payload = stackalloc EventData[4]; |
| | 0 | 52 | | payload[0].Size = sizeof(int); |
| | 0 | 53 | | payload[0].DataPointer = ((IntPtr)(&bufferId)); |
| | 0 | 54 | | payload[0].Reserved = 0; |
| | 0 | 55 | | payload[1].Size = sizeof(int); |
| | 0 | 56 | | payload[1].DataPointer = ((IntPtr)(&bufferSize)); |
| | 0 | 57 | | payload[1].Reserved = 0; |
| | 0 | 58 | | payload[2].Size = sizeof(int); |
| | 0 | 59 | | payload[2].DataPointer = ((IntPtr)(&poolId)); |
| | 0 | 60 | | payload[2].Reserved = 0; |
| | 0 | 61 | | payload[3].Size = sizeof(int); |
| | 0 | 62 | | payload[3].DataPointer = ((IntPtr)(&bucketId)); |
| | 0 | 63 | | payload[3].Reserved = 0; |
| | 0 | 64 | | WriteEventCore(1, 4, payload); |
| | 0 | 65 | | } |
| | | 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 | | { |
| | 0 | 77 | | EventData* payload = stackalloc EventData[5]; |
| | 0 | 78 | | payload[0].Size = sizeof(int); |
| | 0 | 79 | | payload[0].DataPointer = ((IntPtr)(&bufferId)); |
| | 0 | 80 | | payload[0].Reserved = 0; |
| | 0 | 81 | | payload[1].Size = sizeof(int); |
| | 0 | 82 | | payload[1].DataPointer = ((IntPtr)(&bufferSize)); |
| | 0 | 83 | | payload[1].Reserved = 0; |
| | 0 | 84 | | payload[2].Size = sizeof(int); |
| | 0 | 85 | | payload[2].DataPointer = ((IntPtr)(&poolId)); |
| | 0 | 86 | | payload[2].Reserved = 0; |
| | 0 | 87 | | payload[3].Size = sizeof(int); |
| | 0 | 88 | | payload[3].DataPointer = ((IntPtr)(&bucketId)); |
| | 0 | 89 | | payload[3].Reserved = 0; |
| | 0 | 90 | | payload[4].Size = sizeof(BufferAllocatedReason); |
| | 0 | 91 | | payload[4].DataPointer = ((IntPtr)(&reason)); |
| | 0 | 92 | | payload[4].Reserved = 0; |
| | 0 | 93 | | WriteEventCore(2, 5, payload); |
| | 0 | 94 | | } |
| | | 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)] |
| | 0 | 102 | | 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)] |
| | 0 | 110 | | 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)] |
| | 0 | 116 | | 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 | | { |
| | 0 | 126 | | EventData* payload = stackalloc EventData[5]; |
| | 0 | 127 | | payload[0].Size = sizeof(int); |
| | 0 | 128 | | payload[0].DataPointer = ((IntPtr)(&bufferId)); |
| | 0 | 129 | | payload[0].Reserved = 0; |
| | 0 | 130 | | payload[1].Size = sizeof(int); |
| | 0 | 131 | | payload[1].DataPointer = ((IntPtr)(&bufferSize)); |
| | 0 | 132 | | payload[1].Reserved = 0; |
| | 0 | 133 | | payload[2].Size = sizeof(int); |
| | 0 | 134 | | payload[2].DataPointer = ((IntPtr)(&poolId)); |
| | 0 | 135 | | payload[2].Reserved = 0; |
| | 0 | 136 | | payload[3].Size = sizeof(int); |
| | 0 | 137 | | payload[3].DataPointer = ((IntPtr)(&bucketId)); |
| | 0 | 138 | | payload[3].Reserved = 0; |
| | 0 | 139 | | payload[4].Size = sizeof(BufferDroppedReason); |
| | 0 | 140 | | payload[4].DataPointer = ((IntPtr)(&reason)); |
| | 0 | 141 | | payload[4].Reserved = 0; |
| | 0 | 142 | | WriteEventCore(6, 5, payload); |
| | 0 | 143 | | } |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |