| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using NGql.Core.Observability; |
| | | 3 | | |
| | | 4 | | namespace NGql.Core.Pooling; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Centralized thread-local memory management with integrated observability. |
| | | 8 | | /// Provides thread-affinity optimizations and comprehensive telemetry. |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class ThreadLocalMemoryManager |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides statistics about thread-local cache efficiency |
| | | 14 | | /// </summary> |
| | | 15 | | public readonly struct CacheStats |
| | | 16 | | { |
| | | 17 | | public readonly long ThreadLocalHits; |
| | | 18 | | public readonly long GlobalPoolHits; |
| | | 19 | | public readonly long Allocations; |
| | | 20 | | public readonly double ThreadLocalHitRatio; |
| | | 21 | | |
| | | 22 | | internal CacheStats(long threadLocalHits, long globalPoolHits, long allocations) |
| | | 23 | | { |
| | 87 | 24 | | ThreadLocalHits = threadLocalHits; |
| | 87 | 25 | | GlobalPoolHits = globalPoolHits; |
| | 87 | 26 | | Allocations = allocations; |
| | 87 | 27 | | var totalRequests = threadLocalHits + globalPoolHits + allocations; |
| | 87 | 28 | | ThreadLocalHitRatio = totalRequests > 0 ? (double)threadLocalHits / totalRequests : 0.0; |
| | 87 | 29 | | } |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | // Thread-local counters for performance monitoring |
| | | 33 | | [ThreadStatic] |
| | | 34 | | private static long _threadLocalHits; |
| | | 35 | | [ThreadStatic] |
| | | 36 | | private static long _globalPoolHits; |
| | | 37 | | [ThreadStatic] |
| | | 38 | | private static long _allocations; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Records a thread-local cache hit with observability |
| | | 42 | | /// </summary> |
| | | 43 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 44 | | internal static void RecordThreadLocalHit(string poolType) |
| | | 45 | | { |
| | 7548 | 46 | | _threadLocalHits++; |
| | 7548 | 47 | | PoolingObservability.RecordThreadLocalHit(poolType); |
| | 7548 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Records a global pool hit with observability |
| | | 52 | | /// </summary> |
| | | 53 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 54 | | internal static void RecordGlobalPoolHit(string poolType, int poolSize = -1) |
| | | 55 | | { |
| | 1854 | 56 | | _globalPoolHits++; |
| | 1854 | 57 | | PoolingObservability.RecordGlobalPoolHit(poolType, poolSize); |
| | 1854 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Records a new allocation with observability |
| | | 62 | | /// </summary> |
| | | 63 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 64 | | internal static void RecordAllocation(string poolType) |
| | | 65 | | { |
| | 1239 | 66 | | _allocations++; |
| | 1239 | 67 | | PoolingObservability.RecordAllocation(poolType); |
| | 1239 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets cache performance statistics for the current thread |
| | | 72 | | /// </summary> |
| | | 73 | | internal static CacheStats GetThreadStats() |
| | | 74 | | { |
| | 72 | 75 | | return new CacheStats(_threadLocalHits, _globalPoolHits, _allocations); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Resets performance counters for the current thread (useful for benchmarking) |
| | | 80 | | /// </summary> |
| | | 81 | | internal static void ResetThreadStats() |
| | | 82 | | { |
| | 57 | 83 | | _threadLocalHits = 0; |
| | 57 | 84 | | _globalPoolHits = 0; |
| | 57 | 85 | | _allocations = 0; |
| | 57 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Reports pool efficiency for monitoring and alerting |
| | | 90 | | /// </summary> |
| | | 91 | | internal static void ReportPoolEfficiency(string poolType) |
| | | 92 | | { |
| | 9 | 93 | | var stats = GetThreadStats(); |
| | 9 | 94 | | var efficiency = new PoolingObservability.PoolEfficiencyStats( |
| | 9 | 95 | | stats.ThreadLocalHits, |
| | 9 | 96 | | stats.GlobalPoolHits, |
| | 9 | 97 | | stats.Allocations); |
| | | 98 | | |
| | 9 | 99 | | PoolingObservability.RecordPoolEfficiency(poolType, efficiency); |
| | 9 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Warms up thread-local caches with observability tracking |
| | | 104 | | /// </summary> |
| | | 105 | | internal static void WarmupThreadLocalCaches() |
| | | 106 | | { |
| | 6 | 107 | | using var activity = NGqlActivity.StartQuery("cache_warmup") |
| | 6 | 108 | | .WithTag("operation.type", "warmup"); |
| | | 109 | | |
| | 6 | 110 | | activity.WithObservability(() => |
| | 6 | 111 | | { |
| | 6 | 112 | | // Pre-populate caches with a few instances to avoid allocation spikes |
| | 6 | 113 | | var dict1 = LockFreeArgumentsPool.GetPooled(new Dictionary<string, object?>()); |
| | 6 | 114 | | var dict2 = LockFreeArgumentsPool.GetPooled(new Dictionary<string, object?>()); |
| | 6 | 115 | | dict1.Dispose(); |
| | 6 | 116 | | dict2.Dispose(); |
| | 6 | 117 | | |
| | 6 | 118 | | var sb1 = LockFreeStringBuilderPool.GetPooled(); |
| | 6 | 119 | | var sb2 = LockFreeStringBuilderPool.GetPooled(); |
| | 6 | 120 | | sb1.Dispose(); |
| | 6 | 121 | | sb2.Dispose(); |
| | 6 | 122 | | |
| | 6 | 123 | | var set1 = LockFreeHashSetPool.GetPooled(); |
| | 6 | 124 | | set1.Dispose(); |
| | 12 | 125 | | }, "warmup_caches"); |
| | 12 | 126 | | } |
| | | 127 | | |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Enhanced lock-free stack with performance monitoring integration |
| | | 132 | | /// </summary> |
| | | 133 | | /// <typeparam name="T">Type of items in the stack</typeparam> |
| | | 134 | | internal sealed class MonitoredLockFreeStack<T> where T : class |
| | | 135 | | { |
| | | 136 | | private volatile Node? _head; |
| | | 137 | | |
| | | 138 | | private sealed class Node |
| | | 139 | | { |
| | | 140 | | public readonly T Item; |
| | | 141 | | public Node? Next; |
| | | 142 | | |
| | | 143 | | public Node(T item) => Item = item; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Pushes an item onto the stack using compare-and-swap |
| | | 148 | | /// </summary> |
| | | 149 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 150 | | public void Push(T item) |
| | | 151 | | { |
| | | 152 | | var newNode = new Node(item); |
| | | 153 | | Node? currentHead; |
| | | 154 | | do |
| | | 155 | | { |
| | | 156 | | currentHead = _head; |
| | | 157 | | newNode.Next = currentHead; |
| | | 158 | | } while (Interlocked.CompareExchange(ref _head, newNode, currentHead) != currentHead); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Attempts to pop an item from the stack using compare-and-swap with monitoring |
| | | 163 | | /// </summary> |
| | | 164 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 165 | | public bool TryPop(out T item) |
| | | 166 | | { |
| | | 167 | | Node? currentHead; |
| | | 168 | | do |
| | | 169 | | { |
| | | 170 | | currentHead = _head; |
| | | 171 | | if (currentHead == null) |
| | | 172 | | { |
| | | 173 | | item = default!; |
| | | 174 | | return false; |
| | | 175 | | } |
| | | 176 | | } while (Interlocked.CompareExchange(ref _head, currentHead.Next, currentHead) != currentHead); |
| | | 177 | | |
| | | 178 | | item = currentHead.Item; |
| | | 179 | | ThreadLocalMemoryManager.RecordGlobalPoolHit("unknown", -1); |
| | | 180 | | return true; |
| | | 181 | | } |
| | | 182 | | } |