构造方法
PoolThreadCache(PoolArena<byte[]> heapArena, PoolArena<ByteBuffer> directArena, int tinyCacheSize, int smallCacheSize, int normalCacheSize, int maxCachedBufferCapacity, int freeSweepAllocationThreshold) { checkPositiveOrZero(maxCachedBufferCapacity, "maxCachedBufferCapacity"); this.freeSweepAllocationThreshold = freeSweepAllocationThreshold; this.heapArena = heapArena; this.directArena = directArena; if (directArena != null) { tinySubPageDirectCaches = createSubPageCaches( tinyCacheSize, PoolArena.numTinySubpagePools, SizeClass.Tiny); smallSubPageDirectCaches = createSubPageCaches( smallCacheSize, directArena.numSmallSubpagePools, SizeClass.Small); numShiftsNormalDirect = log2(directArena.pageSize); normalDirectCaches = createNormalCaches( normalCacheSize, maxCachedBufferCapacity, directArena); //arena的关联线程数+1 directArena.numThreadCaches.getAndIncrement(); } else { // No directArea is configured so just null out all caches tinySubPageDirectCaches = null; smallSubPageDirectCaches = null; normalDirectCaches = null; numShiftsNormalDirect = -1; } if (heapArena != null) { // Create the caches for the heap allocations tinySubPageHeapCaches = createSubPageCaches( tinyCacheSize, PoolArena.numTinySubpagePools, SizeClass.Tiny); smallSubPageHeapCaches = createSubPageCaches( smallCacheSize, heapArena.numSmallSubpagePools, SizeClass.Small); numShiftsNormalHeap = log2(heapArena.pageSize); normalHeapCaches = createNormalCaches( normalCacheSize, maxCachedBufferCapacity, heapArena); //arena的关联线程数+1 heapArena.numThreadCaches.getAndIncrement(); } else { // No heapArea is configured so just null out all caches tinySubPageHeapCaches = null; smallSubPageHeapCaches = null; normalHeapCaches = null; numShiftsNormalHeap = -1; } // Only check if there are caches in use. if ((tinySubPageDirectCaches != null || smallSubPageDirectCaches != null || normalDirectCaches != null || tinySubPageHeapCaches != null || smallSubPageHeapCaches != null || normalHeapCaches != null) && freeSweepAllocationThreshold < 1) { throw new IllegalArgumentException("freeSweepAllocationThreshold: " + freeSweepAllocationThreshold + " (expected: > 0)"); } }
属性分析
属性 含义 值 final PoolArena<byte[]> heapArena堆内存区域 final PoolArena<ByteBuffer> directArena直接内存区域 final MemoryRegionCache<byte[]>[] tinySubPageHeapCaches微小型子页堆内存缓存 数组长度为32 同tinySubPageDirectCaches final MemoryRegionCache<byte[]>[] smallSubPageHeapCaches小型子页堆内存缓存 数组长度为4 同smallSubPageDirectCaches final MemoryRegionCache<byte[]>[] normalHeapCaches普通型子页堆内存缓存 数组长度为3 同normalDirectCaches final MemoryRegionCache<ByteBuffer>[] tinySubPageDirectCaches微小型子页直接内存缓存 数组长度为32 PoolArena.numTinySubpagePools = 512 >>> 4 也就是32;
tinyCacheSize = 512
tinySubPageDirectCaches = createSubPageCaches(
tinyCacheSize, PoolArena.numTinySubpagePools, SizeClass.Tiny);private static <T> MemoryRegionCache<T>[] createSubPageCaches(
int cacheSize, int numCaches, SizeClass sizeClass) {
// cacheSize = 512 ; numCaches = 32
if (cacheSize > 0 && numCaches > 0) {
@SuppressWarnings("unchecked")
MemoryRegionCache<T>[] cache = new MemoryRegionCache[numCaches];
for (int i = 0; i < cache.length; i++) {
// TODO: maybe use cacheSize / cache.length
cache[i] = new SubPageMemoryRegionCache<T>(cacheSize, sizeClass);
}
return cache;
} else {
return null;
}
}final MemoryRegionCache<ByteBuffer>[] smallSubPageDirectCaches小型子页直接内存缓存 数组长度为4 directArena.numSmallSubpagePools = pageShifts - 9; 也就是13-9=4
smallCacheSize = 256
smallSubPageDirectCaches = createSubPageCaches(
smallCacheSize, directArena.numSmallSubpagePools, SizeClass.Small);private static <T> MemoryRegionCache<T>[] createSubPageCaches(
int cacheSize, int numCaches, SizeClass sizeClass) {
// cacheSize = 256 ; numCaches = 4
if (cacheSize > 0 && numCaches > 0) {
@SuppressWarnings("unchecked")
MemoryRegionCache<T>[] cache = new MemoryRegionCache[numCaches];
for (int i = 0; i < cache.length; i++) {
// TODO: maybe use cacheSize / cache.length
cache[i] = new SubPageMemoryRegionCache<T>(cacheSize, sizeClass);
}
return cache;
} else {
return null;
}
}final MemoryRegionCache<ByteBuffer>[] normalDirectCaches普通型子页直接内存缓存 数组长度为3 maxCachedBufferCapacity = 32 * 1024 是PooledByteBufAllocator.DEFAULT_MAX_CACHED_BUFFER_CAPACITY
normalCacheSize = 64
normalDirectCaches = createNormalCaches(
normalCacheSize, maxCachedBufferCapacity, directArena)private static <T> MemoryRegionCache<T>[] createNormalCaches(
int cacheSize, int maxCachedBufferCapacity, PoolArena<T> area) {
// cacheSize = 64 ; numCaches = 32 * 1024
if (cacheSize > 0 && maxCachedBufferCapacity > 0) {
//max = 32 * 1024
int max = Math.min(area.chunkSize, maxCachedBufferCapacity);
//arraySize = 3
int arraySize = Math.max(1, log2(max / area.pageSize) + 1);@SuppressWarnings("unchecked")
MemoryRegionCache<T>[] cache = new MemoryRegionCache[arraySize];
for (int i = 0; i < cache.length; i++) {
cache[i] = new NormalMemoryRegionCache<T>(cacheSize);
}
return cache;
} else {
return null;
}
}final int numShiftsNormalDirectpageSize的log2 也就是log2(8192) = 13 final int numShiftsNormalHeappageSize的log2 也就是log2(8192) = 13 final int freeSweepAllocationThreshold对应PooledByteBufAllocator的DEFAULT_CACHE_TRIM_INTERVAL 8192 final AtomicBoolean freed = new AtomicBoolean()是否free int allocations分配次数
内部类private abstract static class MemoryRegionCache<T>分析
private abstract static class MemoryRegionCache<T> { //定义queue的大小 微小型大小为512,小型大小为256,普通型大小为64 private final int size; private final Queue<Entry<T>> queue;//队列 private final SizeClass sizeClass;//类型,分为微小,小,普通 private int allocations; MemoryRegionCache(int size, SizeClass sizeClass) { this.size = MathUtil.safeFindNextPositivePowerOfTwo(size); queue = PlatformDependent.newFixedMpscQueue(this.size); this.sizeClass = sizeClass; } protected abstract void initBuf(PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle,PooledByteBuf<T> buf, int reqCapacity);子类
private static final class NormalMemoryRegionCache<T> extends MemoryRegionCache<T> { NormalMemoryRegionCache(int size) { super(size, SizeClass.Normal); } @Override protected void initBuf( PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle, PooledByteBuf<T> buf, int reqCapacity) { chunk.initBuf(buf, nioBuffer, handle, reqCapacity); } }private static final class SubPageMemoryRegionCache<T> extends MemoryRegionCache<T> { SubPageMemoryRegionCache(int size, SizeClass sizeClass) { super(size, sizeClass); } @Override protected void initBuf( PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle, PooledByteBuf<T> buf, int reqCapacity) { chunk.initBufWithSubpage(buf, nioBuffer, handle, reqCapacity); } }Entry<T>
static final class Entry<T> { final Handle<Entry<?>> recyclerHandle; PoolChunk<T> chunk; ByteBuffer nioBuffer; long handle = -1; Entry(Handle<Entry<?>> recyclerHandle) { this.recyclerHandle = recyclerHandle; } void recycle() { chunk = null; nioBuffer = null; handle = -1; recyclerHandle.recycle(this); } }
重要的数据结构



1941

被折叠的 条评论
为什么被折叠?



