| | | 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.Collections.Generic; |
| | | 5 | | using System.Threading; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Runtime |
| | | 8 | | { |
| | | 9 | | internal abstract class InternalBufferManager |
| | | 10 | | { |
| | 0 | 11 | | protected InternalBufferManager() |
| | | 12 | | { |
| | 0 | 13 | | } |
| | | 14 | | |
| | | 15 | | public abstract byte[] TakeBuffer(int bufferSize); |
| | | 16 | | public abstract void ReturnBuffer(byte[] buffer); |
| | | 17 | | public abstract void Clear(); |
| | | 18 | | |
| | | 19 | | public static InternalBufferManager Create(long maxBufferPoolSize, int maxBufferSize) |
| | | 20 | | { |
| | 0 | 21 | | if (maxBufferPoolSize == 0) |
| | | 22 | | { |
| | 0 | 23 | | return GCBufferManager.Value; |
| | | 24 | | } |
| | | 25 | | else |
| | | 26 | | { |
| | | 27 | | Fx.Assert(maxBufferPoolSize > 0 && maxBufferSize >= 0, "bad params, caller should verify"); |
| | 0 | 28 | | return new PooledBufferManager(maxBufferPoolSize, maxBufferSize); |
| | | 29 | | } |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | private class PooledBufferManager : InternalBufferManager |
| | | 33 | | { |
| | | 34 | | private const int minBufferSize = 128; |
| | | 35 | | private const int maxMissesBeforeTuning = 8; |
| | | 36 | | private const int initialBufferCount = 1; |
| | | 37 | | private readonly object _tuningLock; |
| | | 38 | | private readonly int[] _bufferSizes; |
| | | 39 | | private readonly BufferPool[] _bufferPools; |
| | | 40 | | private readonly long _memoryLimit; |
| | | 41 | | private long _remainingMemory; |
| | | 42 | | private bool _areQuotasBeingTuned; |
| | | 43 | | private int _totalMisses; |
| | | 44 | | |
| | 0 | 45 | | public PooledBufferManager(long maxMemoryToPool, int maxBufferSize) |
| | | 46 | | { |
| | 0 | 47 | | _tuningLock = new object(); |
| | 0 | 48 | | _memoryLimit = maxMemoryToPool; |
| | 0 | 49 | | _remainingMemory = maxMemoryToPool; |
| | 0 | 50 | | List<BufferPool> bufferPoolList = new List<BufferPool>(); |
| | | 51 | | |
| | 0 | 52 | | for (int bufferSize = minBufferSize; ;) |
| | | 53 | | { |
| | 0 | 54 | | long bufferCountLong = _remainingMemory / bufferSize; |
| | | 55 | | |
| | 0 | 56 | | int bufferCount = bufferCountLong > int.MaxValue ? int.MaxValue : (int)bufferCountLong; |
| | | 57 | | |
| | 0 | 58 | | if (bufferCount > initialBufferCount) |
| | | 59 | | { |
| | 0 | 60 | | bufferCount = initialBufferCount; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | bufferPoolList.Add(BufferPool.CreatePool(bufferSize, bufferCount)); |
| | | 64 | | |
| | 0 | 65 | | _remainingMemory -= (long)bufferCount * bufferSize; |
| | | 66 | | |
| | 0 | 67 | | if (bufferSize >= maxBufferSize) |
| | | 68 | | { |
| | | 69 | | break; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | long newBufferSizeLong = (long)bufferSize * 2; |
| | | 73 | | |
| | 0 | 74 | | if (newBufferSizeLong > (long)maxBufferSize) |
| | | 75 | | { |
| | 0 | 76 | | bufferSize = maxBufferSize; |
| | | 77 | | } |
| | | 78 | | else |
| | | 79 | | { |
| | 0 | 80 | | bufferSize = (int)newBufferSizeLong; |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | _bufferPools = bufferPoolList.ToArray(); |
| | 0 | 85 | | _bufferSizes = new int[_bufferPools.Length]; |
| | 0 | 86 | | for (int i = 0; i < _bufferPools.Length; i++) |
| | | 87 | | { |
| | 0 | 88 | | _bufferSizes[i] = _bufferPools[i].BufferSize; |
| | | 89 | | } |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | public override void Clear() |
| | | 93 | | { |
| | 0 | 94 | | for (int i = 0; i < _bufferPools.Length; i++) |
| | | 95 | | { |
| | 0 | 96 | | BufferPool bufferPool = _bufferPools[i]; |
| | 0 | 97 | | bufferPool.Clear(); |
| | | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | private void ChangeQuota(ref BufferPool bufferPool, int delta) |
| | | 102 | | { |
| | | 103 | | //if (TraceCore.BufferPoolChangeQuotaIsEnabled(Fx.Trace)) |
| | | 104 | | //{ |
| | | 105 | | // TraceCore.BufferPoolChangeQuota(Fx.Trace, bufferPool.BufferSize, delta); |
| | | 106 | | //} |
| | | 107 | | |
| | 0 | 108 | | BufferPool oldBufferPool = bufferPool; |
| | 0 | 109 | | int newLimit = oldBufferPool.Limit + delta; |
| | 0 | 110 | | BufferPool newBufferPool = BufferPool.CreatePool(oldBufferPool.BufferSize, newLimit); |
| | 0 | 111 | | for (int i = 0; i < newLimit; i++) |
| | | 112 | | { |
| | 0 | 113 | | byte[] buffer = oldBufferPool.Take(); |
| | 0 | 114 | | if (buffer == null) |
| | | 115 | | { |
| | | 116 | | break; |
| | | 117 | | } |
| | 0 | 118 | | newBufferPool.Return(buffer); |
| | 0 | 119 | | newBufferPool.IncrementCount(); |
| | | 120 | | } |
| | 0 | 121 | | _remainingMemory -= oldBufferPool.BufferSize * delta; |
| | 0 | 122 | | bufferPool = newBufferPool; |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | private void DecreaseQuota(ref BufferPool bufferPool) |
| | | 126 | | { |
| | 0 | 127 | | ChangeQuota(ref bufferPool, -1); |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | private int FindMostExcessivePool() |
| | | 131 | | { |
| | 0 | 132 | | long maxBytesInExcess = 0; |
| | 0 | 133 | | int index = -1; |
| | | 134 | | |
| | 0 | 135 | | for (int i = 0; i < _bufferPools.Length; i++) |
| | | 136 | | { |
| | 0 | 137 | | BufferPool bufferPool = _bufferPools[i]; |
| | | 138 | | |
| | 0 | 139 | | if (bufferPool.Peak < bufferPool.Limit) |
| | | 140 | | { |
| | 0 | 141 | | long bytesInExcess = (bufferPool.Limit - bufferPool.Peak) * (long)bufferPool.BufferSize; |
| | | 142 | | |
| | 0 | 143 | | if (bytesInExcess > maxBytesInExcess) |
| | | 144 | | { |
| | 0 | 145 | | index = i; |
| | 0 | 146 | | maxBytesInExcess = bytesInExcess; |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | return index; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | private int FindMostStarvedPool() |
| | | 155 | | { |
| | 0 | 156 | | long maxBytesMissed = 0; |
| | 0 | 157 | | int index = -1; |
| | | 158 | | |
| | 0 | 159 | | for (int i = 0; i < _bufferPools.Length; i++) |
| | | 160 | | { |
| | 0 | 161 | | BufferPool bufferPool = _bufferPools[i]; |
| | | 162 | | |
| | 0 | 163 | | if (bufferPool.Peak == bufferPool.Limit) |
| | | 164 | | { |
| | 0 | 165 | | long bytesMissed = bufferPool.Misses * (long)bufferPool.BufferSize; |
| | | 166 | | |
| | 0 | 167 | | if (bytesMissed > maxBytesMissed) |
| | | 168 | | { |
| | 0 | 169 | | index = i; |
| | 0 | 170 | | maxBytesMissed = bytesMissed; |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | return index; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | private BufferPool FindPool(int desiredBufferSize) |
| | | 179 | | { |
| | 0 | 180 | | for (int i = 0; i < _bufferSizes.Length; i++) |
| | | 181 | | { |
| | 0 | 182 | | if (desiredBufferSize <= _bufferSizes[i]) |
| | | 183 | | { |
| | 0 | 184 | | return _bufferPools[i]; |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | return null; |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | private void IncreaseQuota(ref BufferPool bufferPool) |
| | | 192 | | { |
| | 0 | 193 | | ChangeQuota(ref bufferPool, 1); |
| | 0 | 194 | | } |
| | | 195 | | |
| | | 196 | | public override void ReturnBuffer(byte[] buffer) |
| | | 197 | | { |
| | | 198 | | Fx.Assert(buffer != null, "caller must verify"); |
| | 0 | 199 | | BufferPool bufferPool = FindPool(buffer.Length); |
| | 0 | 200 | | if (bufferPool != null) |
| | | 201 | | { |
| | 0 | 202 | | if (buffer.Length != bufferPool.BufferSize) |
| | | 203 | | { |
| | 0 | 204 | | throw Fx.Exception.Argument(nameof(buffer), SR.BufferIsNotRightSizeForBufferManager); |
| | | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | if (bufferPool.Return(buffer)) |
| | | 208 | | { |
| | 0 | 209 | | bufferPool.IncrementCount(); |
| | | 210 | | } |
| | | 211 | | } |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | public override byte[] TakeBuffer(int bufferSize) |
| | | 215 | | { |
| | | 216 | | Fx.Assert(bufferSize >= 0, "caller must ensure a non-negative argument"); |
| | | 217 | | |
| | 0 | 218 | | BufferPool bufferPool = FindPool(bufferSize); |
| | | 219 | | byte[] returnValue; |
| | 0 | 220 | | if (bufferPool != null) |
| | | 221 | | { |
| | 0 | 222 | | byte[] buffer = bufferPool.Take(); |
| | 0 | 223 | | if (buffer != null) |
| | | 224 | | { |
| | 0 | 225 | | bufferPool.DecrementCount(); |
| | 0 | 226 | | returnValue = buffer; |
| | | 227 | | } |
| | | 228 | | else |
| | | 229 | | { |
| | 0 | 230 | | if (bufferPool.Peak == bufferPool.Limit) |
| | | 231 | | { |
| | 0 | 232 | | bufferPool.Misses++; |
| | 0 | 233 | | if (++_totalMisses >= maxMissesBeforeTuning) |
| | | 234 | | { |
| | 0 | 235 | | TuneQuotas(); |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | //if (TraceCore.BufferPoolAllocationIsEnabled(Fx.Trace)) |
| | | 240 | | //{ |
| | | 241 | | // TraceCore.BufferPoolAllocation(Fx.Trace, bufferPool.BufferSize); |
| | | 242 | | //} |
| | | 243 | | |
| | 0 | 244 | | returnValue = Fx.AllocateByteArray(bufferPool.BufferSize); |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | else |
| | | 248 | | { |
| | | 249 | | //if (TraceCore.BufferPoolAllocationIsEnabled(Fx.Trace)) |
| | | 250 | | //{ |
| | | 251 | | // TraceCore.BufferPoolAllocation(Fx.Trace, bufferSize); |
| | | 252 | | //} |
| | | 253 | | |
| | 0 | 254 | | returnValue = Fx.AllocateByteArray(bufferSize); |
| | | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | return returnValue; |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | private void TuneQuotas() |
| | | 261 | | { |
| | 0 | 262 | | if (_areQuotasBeingTuned) |
| | | 263 | | { |
| | 0 | 264 | | return; |
| | | 265 | | } |
| | | 266 | | |
| | 0 | 267 | | bool lockHeld = false; |
| | | 268 | | try |
| | | 269 | | { |
| | 0 | 270 | | Monitor.TryEnter(_tuningLock, ref lockHeld); |
| | | 271 | | |
| | | 272 | | // Don't bother if another thread already has the lock |
| | 0 | 273 | | if (!lockHeld || _areQuotasBeingTuned) |
| | | 274 | | { |
| | 0 | 275 | | return; |
| | | 276 | | } |
| | | 277 | | |
| | 0 | 278 | | _areQuotasBeingTuned = true; |
| | 0 | 279 | | } |
| | | 280 | | finally |
| | | 281 | | { |
| | 0 | 282 | | if (lockHeld) |
| | | 283 | | { |
| | 0 | 284 | | Monitor.Exit(_tuningLock); |
| | | 285 | | } |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | // find the "poorest" pool |
| | 0 | 289 | | int starvedIndex = FindMostStarvedPool(); |
| | 0 | 290 | | if (starvedIndex >= 0) |
| | | 291 | | { |
| | 0 | 292 | | BufferPool starvedBufferPool = _bufferPools[starvedIndex]; |
| | | 293 | | |
| | 0 | 294 | | if (_remainingMemory < starvedBufferPool.BufferSize) |
| | | 295 | | { |
| | | 296 | | // find the "richest" pool |
| | 0 | 297 | | int excessiveIndex = FindMostExcessivePool(); |
| | 0 | 298 | | if (excessiveIndex >= 0) |
| | | 299 | | { |
| | | 300 | | // steal from the richest |
| | 0 | 301 | | DecreaseQuota(ref _bufferPools[excessiveIndex]); |
| | | 302 | | } |
| | | 303 | | } |
| | | 304 | | |
| | 0 | 305 | | if (_remainingMemory >= starvedBufferPool.BufferSize) |
| | | 306 | | { |
| | | 307 | | // give to the poorest |
| | 0 | 308 | | IncreaseQuota(ref _bufferPools[starvedIndex]); |
| | | 309 | | } |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | // reset statistics |
| | 0 | 313 | | for (int i = 0; i < _bufferPools.Length; i++) |
| | | 314 | | { |
| | 0 | 315 | | BufferPool bufferPool = _bufferPools[i]; |
| | 0 | 316 | | bufferPool.Misses = 0; |
| | | 317 | | } |
| | | 318 | | |
| | 0 | 319 | | _totalMisses = 0; |
| | 0 | 320 | | _areQuotasBeingTuned = false; |
| | 0 | 321 | | } |
| | | 322 | | |
| | | 323 | | private abstract class BufferPool |
| | | 324 | | { |
| | | 325 | | private int _count; |
| | | 326 | | |
| | 0 | 327 | | public BufferPool(int bufferSize, int limit) |
| | | 328 | | { |
| | 0 | 329 | | BufferSize = bufferSize; |
| | 0 | 330 | | Limit = limit; |
| | 0 | 331 | | } |
| | | 332 | | |
| | 0 | 333 | | public int BufferSize { get; } |
| | | 334 | | |
| | 0 | 335 | | public int Limit { get; } |
| | | 336 | | |
| | 0 | 337 | | public int Misses { get; set; } |
| | | 338 | | |
| | 0 | 339 | | public int Peak { get; private set; } |
| | | 340 | | |
| | | 341 | | public void Clear() |
| | | 342 | | { |
| | 0 | 343 | | OnClear(); |
| | 0 | 344 | | _count = 0; |
| | 0 | 345 | | } |
| | | 346 | | |
| | | 347 | | public void DecrementCount() |
| | | 348 | | { |
| | 0 | 349 | | int newValue = _count - 1; |
| | 0 | 350 | | if (newValue >= 0) |
| | | 351 | | { |
| | 0 | 352 | | _count = newValue; |
| | | 353 | | } |
| | 0 | 354 | | } |
| | | 355 | | |
| | | 356 | | public void IncrementCount() |
| | | 357 | | { |
| | 0 | 358 | | int newValue = _count + 1; |
| | 0 | 359 | | if (newValue <= Limit) |
| | | 360 | | { |
| | 0 | 361 | | _count = newValue; |
| | 0 | 362 | | if (newValue > Peak) |
| | | 363 | | { |
| | 0 | 364 | | Peak = newValue; |
| | | 365 | | } |
| | | 366 | | } |
| | 0 | 367 | | } |
| | | 368 | | |
| | | 369 | | internal abstract byte[] Take(); |
| | | 370 | | internal abstract bool Return(byte[] buffer); |
| | | 371 | | internal abstract void OnClear(); |
| | | 372 | | |
| | | 373 | | internal static BufferPool CreatePool(int bufferSize, int limit) |
| | | 374 | | { |
| | | 375 | | // To avoid many buffer drops during training of large objects which |
| | | 376 | | // get allocated on the LOH, we use the LargeBufferPool and for |
| | | 377 | | // bufferSize < 85000, the SynchronizedPool. However if bufferSize < 85000 |
| | | 378 | | // and (bufferSize + array-overhead) > 85000, this would still use |
| | | 379 | | // the SynchronizedPool even though it is allocated on the LOH. |
| | 0 | 380 | | if (bufferSize < 85000) |
| | | 381 | | { |
| | 0 | 382 | | return new SynchronizedBufferPool(bufferSize, limit); |
| | | 383 | | } |
| | | 384 | | else |
| | | 385 | | { |
| | 0 | 386 | | return new LargeBufferPool(bufferSize, limit); |
| | | 387 | | } |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | private class SynchronizedBufferPool : BufferPool |
| | | 391 | | { |
| | | 392 | | private readonly SynchronizedPool<byte[]> _innerPool; |
| | | 393 | | |
| | | 394 | | internal SynchronizedBufferPool(int bufferSize, int limit) |
| | 0 | 395 | | : base(bufferSize, limit) |
| | | 396 | | { |
| | 0 | 397 | | _innerPool = new SynchronizedPool<byte[]>(limit); |
| | 0 | 398 | | } |
| | | 399 | | |
| | | 400 | | internal override void OnClear() |
| | | 401 | | { |
| | 0 | 402 | | _innerPool.Clear(); |
| | 0 | 403 | | } |
| | | 404 | | |
| | | 405 | | internal override byte[] Take() |
| | | 406 | | { |
| | 0 | 407 | | return _innerPool.Take(); |
| | | 408 | | } |
| | | 409 | | |
| | | 410 | | internal override bool Return(byte[] buffer) |
| | | 411 | | { |
| | 0 | 412 | | return _innerPool.Return(buffer); |
| | | 413 | | } |
| | | 414 | | } |
| | | 415 | | |
| | | 416 | | private class LargeBufferPool : BufferPool |
| | | 417 | | { |
| | | 418 | | private readonly Stack<byte[]> _items; |
| | | 419 | | |
| | | 420 | | internal LargeBufferPool(int bufferSize, int limit) |
| | 0 | 421 | | : base(bufferSize, limit) |
| | | 422 | | { |
| | 0 | 423 | | _items = new Stack<byte[]>(limit); |
| | 0 | 424 | | } |
| | | 425 | | |
| | | 426 | | private object ThisLock |
| | | 427 | | { |
| | | 428 | | get |
| | | 429 | | { |
| | 0 | 430 | | return _items; |
| | | 431 | | } |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | internal override void OnClear() |
| | | 435 | | { |
| | 0 | 436 | | lock (ThisLock) |
| | | 437 | | { |
| | 0 | 438 | | _items.Clear(); |
| | 0 | 439 | | } |
| | 0 | 440 | | } |
| | | 441 | | |
| | | 442 | | internal override byte[] Take() |
| | | 443 | | { |
| | 0 | 444 | | lock (ThisLock) |
| | | 445 | | { |
| | 0 | 446 | | if (_items.Count > 0) |
| | | 447 | | { |
| | 0 | 448 | | return _items.Pop(); |
| | | 449 | | } |
| | 0 | 450 | | } |
| | | 451 | | |
| | 0 | 452 | | return null; |
| | 0 | 453 | | } |
| | | 454 | | |
| | | 455 | | internal override bool Return(byte[] buffer) |
| | | 456 | | { |
| | 0 | 457 | | lock (ThisLock) |
| | | 458 | | { |
| | 0 | 459 | | if (_items.Count < Limit) |
| | | 460 | | { |
| | 0 | 461 | | _items.Push(buffer); |
| | 0 | 462 | | return true; |
| | | 463 | | } |
| | 0 | 464 | | } |
| | | 465 | | |
| | 0 | 466 | | return false; |
| | 0 | 467 | | } |
| | | 468 | | } |
| | | 469 | | } |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | private class GCBufferManager : InternalBufferManager |
| | | 473 | | { |
| | 0 | 474 | | private GCBufferManager() |
| | | 475 | | { |
| | 0 | 476 | | } |
| | | 477 | | |
| | 0 | 478 | | public static GCBufferManager Value { get; } = new GCBufferManager(); |
| | | 479 | | |
| | | 480 | | public override void Clear() |
| | | 481 | | { |
| | 0 | 482 | | } |
| | | 483 | | |
| | | 484 | | public override byte[] TakeBuffer(int bufferSize) |
| | | 485 | | { |
| | 0 | 486 | | return Fx.AllocateByteArray(bufferSize); |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | public override void ReturnBuffer(byte[] buffer) |
| | | 490 | | { |
| | | 491 | | // do nothing, GC will reclaim this buffer |
| | 0 | 492 | | } |
| | | 493 | | } |
| | | 494 | | } |
| | | 495 | | } |