Package io.netty.buffer
Class PoolThreadCache
- java.lang.Object
-
- io.netty.buffer.PoolThreadCache
-
final class PoolThreadCache extends java.lang.Object
Acts a Thread cache for allocations. This implementation is moduled after jemalloc and the descripted technics of Scalable memory allocation using jemalloc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
PoolThreadCache.MemoryRegionCache<T>
private static class
PoolThreadCache.NormalMemoryRegionCache<T>
Cache used for buffers which are backed by NORMAL size.private static class
PoolThreadCache.SubPageMemoryRegionCache<T>
Cache used for buffers which are backed by TINY or SMALL size.
-
Field Summary
Fields Modifier and Type Field Description private int
allocations
(package private) PoolArena<java.nio.ByteBuffer>
directArena
private java.util.concurrent.atomic.AtomicBoolean
freed
private int
freeSweepAllocationThreshold
(package private) PoolArena<byte[]>
heapArena
private static int
INTEGER_SIZE_MINUS_ONE
private static InternalLogger
logger
private PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[]
normalDirectCaches
private PoolThreadCache.MemoryRegionCache<byte[]>[]
normalHeapCaches
private int
numShiftsNormalDirect
private int
numShiftsNormalHeap
private PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[]
smallSubPageDirectCaches
private PoolThreadCache.MemoryRegionCache<byte[]>[]
smallSubPageHeapCaches
private PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[]
tinySubPageDirectCaches
private PoolThreadCache.MemoryRegionCache<byte[]>[]
tinySubPageHeapCaches
-
Constructor Summary
Constructors Constructor Description PoolThreadCache(PoolArena<byte[]> heapArena, PoolArena<java.nio.ByteBuffer> directArena, int tinyCacheSize, int smallCacheSize, int normalCacheSize, int maxCachedBufferCapacity, int freeSweepAllocationThreshold)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) boolean
add(PoolArena<?> area, PoolChunk chunk, java.nio.ByteBuffer nioBuffer, long handle, int normCapacity, PoolArena.SizeClass sizeClass)
AddPoolChunk
andhandle
to the cache if there is enough room.private boolean
allocate(PoolThreadCache.MemoryRegionCache<?> cache, PooledByteBuf buf, int reqCapacity)
(package private) boolean
allocateNormal(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int normCapacity)
Try to allocate a small buffer out of the cache.(package private) boolean
allocateSmall(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int normCapacity)
Try to allocate a small buffer out of the cache.(package private) boolean
allocateTiny(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int normCapacity)
Try to allocate a tiny buffer out of the cache.private PoolThreadCache.MemoryRegionCache<?>
cache(PoolArena<?> area, int normCapacity, PoolArena.SizeClass sizeClass)
private static <T> PoolThreadCache.MemoryRegionCache<T>
cache(PoolThreadCache.MemoryRegionCache<T>[] cache, int idx)
private PoolThreadCache.MemoryRegionCache<?>
cacheForNormal(PoolArena<?> area, int normCapacity)
private PoolThreadCache.MemoryRegionCache<?>
cacheForSmall(PoolArena<?> area, int normCapacity)
private PoolThreadCache.MemoryRegionCache<?>
cacheForTiny(PoolArena<?> area, int normCapacity)
private static <T> PoolThreadCache.MemoryRegionCache<T>[]
createNormalCaches(int cacheSize, int maxCachedBufferCapacity, PoolArena<T> area)
private static <T> PoolThreadCache.MemoryRegionCache<T>[]
createSubPageCaches(int cacheSize, int numCaches, PoolArena.SizeClass sizeClass)
protected void
finalize()
(package private) void
free(boolean finalizer)
Should be called if the Thread that uses this cache is about to exist to release resources out of the cacheprivate static int
free(PoolThreadCache.MemoryRegionCache<?>[] caches, boolean finalizer)
private static int
free(PoolThreadCache.MemoryRegionCache<?> cache, boolean finalizer)
private static int
log2(int val)
(package private) void
trim()
private static void
trim(PoolThreadCache.MemoryRegionCache<?> cache)
private static void
trim(PoolThreadCache.MemoryRegionCache<?>[] caches)
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
INTEGER_SIZE_MINUS_ONE
private static final int INTEGER_SIZE_MINUS_ONE
- See Also:
- Constant Field Values
-
heapArena
final PoolArena<byte[]> heapArena
-
directArena
final PoolArena<java.nio.ByteBuffer> directArena
-
tinySubPageHeapCaches
private final PoolThreadCache.MemoryRegionCache<byte[]>[] tinySubPageHeapCaches
-
smallSubPageHeapCaches
private final PoolThreadCache.MemoryRegionCache<byte[]>[] smallSubPageHeapCaches
-
tinySubPageDirectCaches
private final PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[] tinySubPageDirectCaches
-
smallSubPageDirectCaches
private final PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[] smallSubPageDirectCaches
-
normalHeapCaches
private final PoolThreadCache.MemoryRegionCache<byte[]>[] normalHeapCaches
-
normalDirectCaches
private final PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[] normalDirectCaches
-
numShiftsNormalDirect
private final int numShiftsNormalDirect
-
numShiftsNormalHeap
private final int numShiftsNormalHeap
-
freeSweepAllocationThreshold
private final int freeSweepAllocationThreshold
-
freed
private final java.util.concurrent.atomic.AtomicBoolean freed
-
allocations
private int allocations
-
-
Method Detail
-
createSubPageCaches
private static <T> PoolThreadCache.MemoryRegionCache<T>[] createSubPageCaches(int cacheSize, int numCaches, PoolArena.SizeClass sizeClass)
-
createNormalCaches
private static <T> PoolThreadCache.MemoryRegionCache<T>[] createNormalCaches(int cacheSize, int maxCachedBufferCapacity, PoolArena<T> area)
-
log2
private static int log2(int val)
-
allocateTiny
boolean allocateTiny(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int normCapacity)
Try to allocate a tiny buffer out of the cache. Returnstrue
if successfulfalse
otherwise
-
allocateSmall
boolean allocateSmall(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int normCapacity)
Try to allocate a small buffer out of the cache. Returnstrue
if successfulfalse
otherwise
-
allocateNormal
boolean allocateNormal(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int normCapacity)
Try to allocate a small buffer out of the cache. Returnstrue
if successfulfalse
otherwise
-
allocate
private boolean allocate(PoolThreadCache.MemoryRegionCache<?> cache, PooledByteBuf buf, int reqCapacity)
-
add
boolean add(PoolArena<?> area, PoolChunk chunk, java.nio.ByteBuffer nioBuffer, long handle, int normCapacity, PoolArena.SizeClass sizeClass)
AddPoolChunk
andhandle
to the cache if there is enough room. Returnstrue
if it fit into the cachefalse
otherwise.
-
cache
private PoolThreadCache.MemoryRegionCache<?> cache(PoolArena<?> area, int normCapacity, PoolArena.SizeClass sizeClass)
-
finalize
protected void finalize() throws java.lang.Throwable
- Overrides:
finalize
in classjava.lang.Object
- Throws:
java.lang.Throwable
-
free
void free(boolean finalizer)
Should be called if the Thread that uses this cache is about to exist to release resources out of the cache
-
free
private static int free(PoolThreadCache.MemoryRegionCache<?>[] caches, boolean finalizer)
-
free
private static int free(PoolThreadCache.MemoryRegionCache<?> cache, boolean finalizer)
-
trim
void trim()
-
trim
private static void trim(PoolThreadCache.MemoryRegionCache<?>[] caches)
-
trim
private static void trim(PoolThreadCache.MemoryRegionCache<?> cache)
-
cacheForTiny
private PoolThreadCache.MemoryRegionCache<?> cacheForTiny(PoolArena<?> area, int normCapacity)
-
cacheForSmall
private PoolThreadCache.MemoryRegionCache<?> cacheForSmall(PoolArena<?> area, int normCapacity)
-
cacheForNormal
private PoolThreadCache.MemoryRegionCache<?> cacheForNormal(PoolArena<?> area, int normCapacity)
-
cache
private static <T> PoolThreadCache.MemoryRegionCache<T> cache(PoolThreadCache.MemoryRegionCache<T>[] cache, int idx)
-
-