|
84 | 84 | @UnstableApi |
85 | 85 | final class AdaptivePoolingAllocator { |
86 | 86 | private static final int LOW_MEM_THRESHOLD = 512 * 1024 * 1024; |
87 | | - private static final boolean IS_LOW_MEM = Runtime.getRuntime().maxMemory() <= LOW_MEM_THRESHOLD; |
| 87 | + private static final boolean IS_LOW_MEM = SystemPropertyUtil.getBoolean( |
| 88 | + "io.netty.allocator.lowMemory", |
| 89 | + Runtime.getRuntime().maxMemory() <= LOW_MEM_THRESHOLD); |
88 | 90 |
|
89 | 91 | /** |
90 | 92 | * Whether the IS_LOW_MEM setting should disable thread-local magazines. |
@@ -134,6 +136,27 @@ final class AdaptivePoolingAllocator { |
134 | 136 | static final int CHUNK_PURGE_THRESHOLD = Math.max(1, SystemPropertyUtil.getInt( |
135 | 137 | "io.netty.allocator.chunkPurgeThreshold", 3)); |
136 | 138 |
|
| 139 | + /** |
| 140 | + * Per-size-class upper bound (in bytes) on the thread-local chunk cache. |
| 141 | + * When a size class cache holds this many bytes worth of chunks, |
| 142 | + * further offers are rejected and the chunk is marked for immediate deallocation. |
| 143 | + * Chunks already in the cache are only evicted by the purge mechanism (they must be full and idle for |
| 144 | + * {@link #CHUNK_PURGE_THRESHOLD} consecutive purge cycles). |
| 145 | + */ |
| 146 | + static final int THREAD_LOCAL_CACHE_MAX_BYTES = Math.max(1, SystemPropertyUtil.getInt( |
| 147 | + "io.netty.allocator.threadLocalChunkCacheMaxBytes", 8 * 1024 * 1024)); |
| 148 | + |
| 149 | + /** |
| 150 | + * Per-size-class lower bound (in bytes) on the thread-local chunk cache. |
| 151 | + * The purge mechanism will not evict chunks below this retention floor, even if they are full and idle. |
| 152 | + * Clamped to {@link #THREAD_LOCAL_CACHE_MAX_BYTES} if the configured value exceeds it. |
| 153 | + * When equal to {@link #THREAD_LOCAL_CACHE_MAX_BYTES}, purge eviction is effectively disabled. |
| 154 | + */ |
| 155 | + static final int THREAD_LOCAL_CACHE_MIN_BYTES = Math.min(THREAD_LOCAL_CACHE_MAX_BYTES, |
| 156 | + Math.max(1, SystemPropertyUtil.getInt( |
| 157 | + "io.netty.allocator.threadLocalChunkCacheMinBytes", |
| 158 | + THREAD_LOCAL_CACHE_MAX_BYTES / 2))); |
| 159 | + |
137 | 160 | /** |
138 | 161 | * The capacity if the magazine local buffer queue. This queue just pools the outer ByteBuf instance and not |
139 | 162 | * the actual memory and so helps to reduce GC pressure. |
@@ -522,7 +545,8 @@ interface ChunkCache { |
522 | 545 | // 2. EVICTION: idle chunks with epoch > CHUNK_PURGE_THRESHOLD are evicted (markToDeallocate). |
523 | 546 | // Eviction is immediate — all segments are in, no outstanding references. |
524 | 547 | // Non-idle chunks are never evicted (deallocation would be deferred, not immediate). |
525 | | - // At least CHUNK_REUSE_QUEUE chunks are always retained (retention floor). |
| 548 | + // A retention floor prevents over-eviction: CHUNK_REUSE_QUEUE for the shared cache, |
| 549 | + // purgeRetentionFloor (from THREAD_LOCAL_CACHE_MIN_BYTES) for the thread-local cache. |
526 | 550 | // |
527 | 551 | // 3. SCAN RESET: scanForCapacity resets purgeEpoch = 0 on the chunk it picks. The scan |
528 | 552 | // knows the chunk is being used. The chunk gets allocated from, becomes non-idle, and |
@@ -538,8 +562,9 @@ interface ChunkCache { |
538 | 562 | // Shared: approximate, converges over multiple cycles (FIFO queue ordering, |
539 | 563 | // LRU preference in scan, retained counter in purge). |
540 | 564 | abstract static class SizeClassedChunkCache implements ChunkCache { |
541 | | - static SizeClassedChunkCache create(boolean isThreadLocal) { |
542 | | - return isThreadLocal ? new ThreadLocalSizeClassedChunkCache() : new SharedSizeClassedChunkCache(); |
| 565 | + static SizeClassedChunkCache create(boolean isThreadLocal, int chunkSize) { |
| 566 | + return isThreadLocal ? new ThreadLocalSizeClassedChunkCache(chunkSize) : |
| 567 | + new SharedSizeClassedChunkCache(); |
543 | 568 | } |
544 | 569 |
|
545 | 570 | @Override |
@@ -636,7 +661,7 @@ static SizeClassedChunkCache create(boolean isThreadLocal) { |
636 | 661 | * notEmptyCount=2, count=6 |
637 | 662 | * </pre> |
638 | 663 | * Idle chunks ({@code remainingCapacity == capacity}) age via purgeEpoch and are evicted |
639 | | - * past threshold, but at least {@link #CHUNK_REUSE_QUEUE} chunks are always retained. |
| 664 | + * past threshold, but at least {@code purgeRetentionFloor} chunks are always retained. |
640 | 665 | */ |
641 | 666 | static final class ThreadLocalSizeClassedChunkCache extends SizeClassedChunkCache { |
642 | 667 | SizeClassedChunk[] chunks; // package-private for testing |
@@ -645,10 +670,15 @@ static final class ThreadLocalSizeClassedChunkCache extends SizeClassedChunkCach |
645 | 670 | int count; |
646 | 671 | int notEmptyCount; |
647 | 672 | private long purgeBudget; |
| 673 | + final int maxCachedChunks; // package-private for testing |
| 674 | + final int purgeRetentionFloor; // package-private for testing |
648 | 675 |
|
649 | | - ThreadLocalSizeClassedChunkCache() { |
| 676 | + ThreadLocalSizeClassedChunkCache(int chunkSize) { |
650 | 677 | chunks = new SizeClassedChunk[8]; |
651 | 678 | purgeBudget = CHUNK_PURGE_POLLS_THREAD_LOCAL; |
| 679 | + maxCachedChunks = Math.max(1, THREAD_LOCAL_CACHE_MAX_BYTES / chunkSize); |
| 680 | + purgeRetentionFloor = Math.min(maxCachedChunks, |
| 681 | + Math.max(1, THREAD_LOCAL_CACHE_MIN_BYTES / chunkSize)); |
652 | 682 | } |
653 | 683 |
|
654 | 684 | @Override |
@@ -709,7 +739,7 @@ private void runPurgeScan() { |
709 | 739 | if (chunk.purgeEpoch > 0) { |
710 | 740 | assert chunk.hasFullCapacity(); |
711 | 741 | chunk.purgeEpoch++; |
712 | | - if (chunk.purgeEpoch > CHUNK_PURGE_THRESHOLD && survivors > CHUNK_REUSE_QUEUE) { |
| 742 | + if (chunk.purgeEpoch > CHUNK_PURGE_THRESHOLD && survivors > purgeRetentionFloor) { |
713 | 743 | chunk.markToDeallocate(); |
714 | 744 | chunks[readIdx] = null; |
715 | 745 | survivors--; |
@@ -787,6 +817,9 @@ private void partition(int size) { |
787 | 817 |
|
788 | 818 | @Override |
789 | 819 | public boolean offerChunk(Chunk chunk) { |
| 820 | + if (count >= maxCachedChunks) { |
| 821 | + return false; |
| 822 | + } |
790 | 823 | if (count == chunks.length) { |
791 | 824 | SizeClassedChunk[] newChunks = new SizeClassedChunk[chunks.length * 2]; |
792 | 825 | for (int i = 0; i < count; i++) { |
@@ -884,7 +917,7 @@ public boolean isEmpty() { |
884 | 917 | static final class SharedSizeClassedChunkCache extends SizeClassedChunkCache { |
885 | 918 | // Must exceed CHUNK_REUSE_QUEUE (the retention floor) to leave room for burst absorption. |
886 | 919 | // TODO replace with an unbounded concurrent collection once available. |
887 | | - private static final int SHARED_CACHE_CAPACITY = Math.max(128, CHUNK_REUSE_QUEUE * 2); |
| 920 | + private static final int SHARED_CACHE_CAPACITY = Math.max(16, CHUNK_REUSE_QUEUE * 2); |
888 | 921 | private final Queue<SizeClassedChunk> queue; |
889 | 922 | private final AtomicLong purgeBudget; |
890 | 923 | private final ArrayList<SizeClassedChunk> deferredBuffer = new ArrayList<>(); |
@@ -1163,7 +1196,7 @@ public ChunkController createController(MagazineGroup group) { |
1163 | 1196 |
|
1164 | 1197 | @Override |
1165 | 1198 | public ChunkCache createChunkCache(boolean isThreadLocal) { |
1166 | | - return SizeClassedChunkCache.create(isThreadLocal); |
| 1199 | + return SizeClassedChunkCache.create(isThreadLocal, chunkSize); |
1167 | 1200 | } |
1168 | 1201 | } |
1169 | 1202 |
|
|