PooledByteBuf分配及回收之二PoolThreadCache初始化

PooledByteBufAllocator初始化

PoolThreadCache初始化

PoolAerna初始化

PoolChunk初始化

PoolSubpage初始化

PooledUnsafeDirectByteBuf初始化

分配微小型PooledByteBuf(未命中缓存场景)

分配小型PooledByteBuf(未命中缓存场景)

分配普通型PooledByteBuf(未命中缓存场景)

PoolChunkList源码解析

ReferenceCountUpdater源码解析

Recycler及基内部类初始化

Recycler.Stack<T> 压入对象

Recycler.Stack<T> 弹出对象

PooledByteBuf的回收

构造方法

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 numShiftsNormalDirect
pageSize的log2 也就是log2(8192) = 13
final int numShiftsNormalHeap
pageSize的log2 也就是log2(8192) = 13
final int freeSweepAllocationThreshold
对应PooledByteBufAllocator的DEFAULT_CACHE_TRIM_INTERVAL8192
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);
            }
        }

 

 

重要的数据结构

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值