Top Description Inners Fields Constructors Methods
io.netty5.buffer.pool

pack-priv final Class PoolChunk

extends Object
implements PoolChunkMetric
Class Inheritance
All Implemented Interfaces
io.netty5.buffer.pool.PoolChunkMetric
Imports
io.netty5.buffer.AllocatorControl, .Buffer, .Drop, .MemoryManager, io.netty5.buffer.internal.ArcDrop, .CleanerDrop, .DropCaptor, io.netty5.util.internal.LongLongHashMap, .LongPriorityQueue, io.netty5.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.util.PriorityQueue, java.util.concurrent.locks.ReentrantLock

Description of algorithm for PageRun/PoolSubpage allocation from PoolChunk Notation: The following terms are important to understand the code > page - a page is the smallest unit of memory chunk that can be allocated > run - a run is a collection of pages > chunk - a chunk is a collection of runs > in this code chunkSize = maxPages * pageSize To begin we allocate a byte array of size = chunkSize Whenever a ByteBuf of given size needs to be created we search for the first position in the byte array that has enough empty space to accommodate the requested size and return a (long) handle that encodes this offset information, (this memory segment is then marked as reserved, so it is always used by exactly one ByteBuf and no more) For simplicity all sizes are normalized according to PoolArena#size2SizeIdx(int) method. This ensures that when we request for memory segments of size > pageSize the normalizedCapacity equals the next nearest size in SizeClasses. A chunk has the following layout: /-----------------\ | run | | | | | |-----------------| | run | | | |-----------------| | unalloctated | | (freed) | | | |-----------------| | subpage | |-----------------| | unallocated | | (freed) | | ... | | ... | | ... | | | | | | | \-----------------/ handle: ------- a handle is a long number, the bit layout of a run looks like: oooooooo ooooooos ssssssss ssssssue bbbbbbbb bbbbbbbb bbbbbbbb bbbbbbbb o: runOffset (page offset in the chunk), 15bit s: size (number of pages) of this run, 15bit u: isUsed?, 1bit e: isSubpage?, 1bit b: bitmapIdx of subpage, zero if it's not subpage, 32bit runsAvailMap: ------ a map which manages all runs (used and not in used). For each run, the first runOffset and last runOffset are stored in runsAvailMap. key: runOffset value: handle runsAvail: ---------- an array of PriorityQueue. Each queue manages same size of runs. Runs are sorted by offset, so that we always allocate runs with smaller offset. Algorithm: ---------- As we allocate runs, we update values stored in runsAvailMap and runsAvail so that the property is maintained. Initialization - In the beginning we store the initial run which is the whole chunk. The initial run: runOffset = 0 size = chunkSize isUsed = no isSubpage = no bitmapIdx = 0 Algorithm: [allocateRun(size)] ---------- 1) find the first avail run using in runsAvails according to size 2) if pages of run is larger than request pages then split it, and save the tailing run for later using Algorithm: [allocateSubpage(size)] ---------- 1) find a not full subpage according to size. if it already exists just return, otherwise allocate a new PoolSubpage and call init() note that this subpage object is added to subpagesPool in the PoolArena when we init() it 2) call subpage.allocate() Algorithm: [free(handle, length, nioBuffer)] ---------- 1) if it is a subpage, return the slab back into this subpage 2) if the subpage is not used, or it is a run, then start free this run 3) merge continuous avail runs 4) save the merged run

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
pack-priv final PoolArena
pack-priv final Buffer
pack-priv final Drop<Buffer>
private static final int
private final int
private static final AllocatorControl
pack-priv int
private static final int
pack-priv static final int
pack-priv static final int
private static final InternalLogger
pack-priv final Object
pack-priv PoolChunk
private final int
private final int
pack-priv PoolChunkList
pack-priv int
pack-priv PoolChunk
pack-priv static final int
private final LongPriorityQueue[]
runsAvail

manage all avail runs

private final ReentrantLock
private final LongLongHashMap
runsAvailMap

store the first page and last page of each avail run

private static final int
pack-priv static final int
private static final int
private final PoolSubpage[]
subpages

manage all subpages in this chunk

Constructor Summary

AccessConstructor and Description
pack-priv
PoolChunk(PoolArena arena, int pageSize, int pageShifts, int chunkSize, int maxPageIdx)

Method Summary

Modifier and TypeMethod and Description
pack-priv UntetheredMemory
allocate(int size, int sizeIdx, PoolThreadCache cache)

pack-priv UntetheredMemory
allocateBuffer(long handle, int size, PoolThreadCache threadCache)

pack-priv UntetheredMemory
allocateBufferWithSubpage(long handle, int size, PoolThreadCache threadCache)

private long
allocateRun(int runSize)

private long

Returns:

index in memoryMap
allocateSubpage
(int
sizeIdx of normalized size
sizeIdx
)

Create / initialize a new PoolSubpage of normCapacity.

pack-priv static int
bitmapIdx(long handle)

private int
calculateRunSize(int sizeIdx)

public int
chunkSize()

Implements io.netty5.buffer.pool.PoolChunkMetric.chunkSize.

Return the size of the chunk in bytes, this is the maximum of bytes that can be served out of the chunk.
private long
collapseNext(long handle)

private long
collapsePast(long handle)

private long
collapseRuns(long handle)

pack-priv void
pack-priv void
free(long
handle to free
handle
,
int normCapacity)

Free a subpage, or a run of pages When a subpage is freed from PoolSubpage, it might be added back to subpage pool of the owning PoolArena.

public int
freeBytes()

Implements io.netty5.buffer.pool.PoolChunkMetric.freeBytes.

Return the number of free bytes in the chunk.
private long
getAvailRunByOffset(int runOffset)

private void
insertAvailRun(int runOffset, int pages, long handle)

private void
insertAvailRun0(int runOffset, long handle)

pack-priv static boolean
isSubpage(long handle)

pack-priv static boolean
isUsed(long handle)

private static int
lastPage(int runOffset, int pages)

private static LongPriorityQueue[]
public int
pinnedBytes()

Implements io.netty5.buffer.pool.PoolChunkMetric.pinnedBytes.

Return the number of pinned bytes in the chunk.
private void
removeAvailRun(long handle)

private void
removeAvailRun(LongPriorityQueue queue, long handle)

private int
runFirstBestFit(int pageIdx)

pack-priv static int
runOffset(long handle)

pack-priv static int
runPages(long handle)

pack-priv static int
runSize(int pageShifts, long handle)

private long
splitLargeRun(long handle, int needPages)

private static long
toRunHandle(int runOffset, int runPages, int inUsed)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public int
usage()

Implements io.netty5.buffer.pool.PoolChunkMetric.usage.

Return the percentage of the current usage of the chunk.
private int
usage(int freeBytes)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

arenaback to summary
pack-priv final PoolArena arena
baseback to summary
pack-priv final Buffer base
baseDropback to summary
pack-priv final Drop<Buffer> baseDrop
BITMAP_IDX_BIT_LENGTHback to summary
private static final int BITMAP_IDX_BIT_LENGTH
chunkSizeback to summary
private final int chunkSize
CONTROLback to summary
private static final AllocatorControl CONTROL
freeBytesback to summary
pack-priv int freeBytes
INUSED_BIT_LENGTHback to summary
private static final int INUSED_BIT_LENGTH
IS_SUBPAGE_SHIFTback to summary
pack-priv static final int IS_SUBPAGE_SHIFT
IS_USED_SHIFTback to summary
pack-priv static final int IS_USED_SHIFT
LOGGERback to summary
private static final InternalLogger LOGGER
memoryback to summary
pack-priv final Object memory
nextback to summary
pack-priv PoolChunk next
pageShiftsback to summary
private final int pageShifts
pageSizeback to summary
private final int pageSize
parentback to summary
pack-priv PoolChunkList parent
pinnedBytesback to summary
pack-priv int pinnedBytes
prevback to summary
pack-priv PoolChunk prev
RUN_OFFSET_SHIFTback to summary
pack-priv static final int RUN_OFFSET_SHIFT
runsAvailback to summary
private final LongPriorityQueue[] runsAvail

manage all avail runs

runsAvailLockback to summary
private final ReentrantLock runsAvailLock
runsAvailMapback to summary
private final LongLongHashMap runsAvailMap

store the first page and last page of each avail run

SIZE_BIT_LENGTHback to summary
private static final int SIZE_BIT_LENGTH
SIZE_SHIFTback to summary
pack-priv static final int SIZE_SHIFT
SUBPAGE_BIT_LENGTHback to summary
private static final int SUBPAGE_BIT_LENGTH
subpagesback to summary
private final PoolSubpage[] subpages

manage all subpages in this chunk

Constructor Detail

PoolChunkback to summary
pack-priv PoolChunk(PoolArena arena, int pageSize, int pageShifts, int chunkSize, int maxPageIdx)

Method Detail

allocateback to summary
pack-priv UntetheredMemory allocate(int size, int sizeIdx, PoolThreadCache cache)
allocateBufferback to summary
pack-priv UntetheredMemory allocateBuffer(long handle, int size, PoolThreadCache threadCache)
allocateBufferWithSubpageback to summary
pack-priv UntetheredMemory allocateBufferWithSubpage(long handle, int size, PoolThreadCache threadCache)
allocateRunback to summary
private long allocateRun(int runSize)
allocateSubpageback to summary
private long allocateSubpage(int sizeIdx)

Create / initialize a new PoolSubpage of normCapacity. Any PoolSubpage created / initialized here is added to subpage pool in the PoolArena that owns this PoolChunk

Parameters
sizeIdx:int

sizeIdx of normalized size

Returns:long

index in memoryMap

bitmapIdxback to summary
pack-priv static int bitmapIdx(long handle)
calculateRunSizeback to summary
private int calculateRunSize(int sizeIdx)
chunkSizeback to summary
public int chunkSize()

Implements io.netty5.buffer.pool.PoolChunkMetric.chunkSize.

Doc from io.netty5.buffer.pool.PoolChunkMetric.chunkSize.

Return the size of the chunk in bytes, this is the maximum of bytes that can be served out of the chunk.

Annotations
@Override
collapseNextback to summary
private long collapseNext(long handle)
collapsePastback to summary
private long collapsePast(long handle)
collapseRunsback to summary
private long collapseRuns(long handle)
destroyback to summary
pack-priv void destroy()
freeback to summary
pack-priv void free(long handle, int normCapacity)

Free a subpage, or a run of pages When a subpage is freed from PoolSubpage, it might be added back to subpage pool of the owning PoolArena. If the subpage pool in PoolArena has at least one other PoolSubpage of given elemSize, we can completely free the owning Page, so it is available for subsequent allocations.

Parameters
handle:long

handle to free

freeBytesback to summary
public int freeBytes()

Implements io.netty5.buffer.pool.PoolChunkMetric.freeBytes.

Doc from io.netty5.buffer.pool.PoolChunkMetric.freeBytes.

Return the number of free bytes in the chunk.

Annotations
@Override
getAvailRunByOffsetback to summary
private long getAvailRunByOffset(int runOffset)
insertAvailRunback to summary
private void insertAvailRun(int runOffset, int pages, long handle)
insertAvailRun0back to summary
private void insertAvailRun0(int runOffset, long handle)
isSubpageback to summary
pack-priv static boolean isSubpage(long handle)
isUsedback to summary
pack-priv static boolean isUsed(long handle)
lastPageback to summary
private static int lastPage(int runOffset, int pages)
newRunsAvailqueueArrayback to summary
private static LongPriorityQueue[] newRunsAvailqueueArray(int size)
pinnedBytesback to summary
public int pinnedBytes()

Implements io.netty5.buffer.pool.PoolChunkMetric.pinnedBytes.

Doc from io.netty5.buffer.pool.PoolChunkMetric.pinnedBytes.

Return the number of pinned bytes in the chunk.

Annotations
@Override
removeAvailRunback to summary
private void removeAvailRun(long handle)
removeAvailRunback to summary
private void removeAvailRun(LongPriorityQueue queue, long handle)
runFirstBestFitback to summary
private int runFirstBestFit(int pageIdx)
runOffsetback to summary
pack-priv static int runOffset(long handle)
runPagesback to summary
pack-priv static int runPages(long handle)
runSizeback to summary
pack-priv static int runSize(int pageShifts, long handle)
splitLargeRunback to summary
private long splitLargeRun(long handle, int needPages)
toRunHandleback to summary
private static long toRunHandle(int runOffset, int runPages, int inUsed)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
usageback to summary
public int usage()

Implements io.netty5.buffer.pool.PoolChunkMetric.usage.

Doc from io.netty5.buffer.pool.PoolChunkMetric.usage.

Return the percentage of the current usage of the chunk.

Annotations
@Override
usageback to summary
private int usage(int freeBytes)
io.netty5.buffer.pool back to summary

private final Class PoolChunk.UntetheredChunkAllocation

extends Object
implements UntetheredMemory
Class Inheritance
All Implemented Interfaces
io.netty5.buffer.pool.UntetheredMemory
Annotations
@SuppressWarnings:unchecked

Field Summary

Modifier and TypeField and Description
private final PoolChunk
private final long
private final int
private final Object
private final PoolThreadCache

Constructor Summary

AccessConstructor and Description
private
UntetheredChunkAllocation(Object memory, PoolChunk chunk, PoolThreadCache threadCache, long handle, int maxLength, int offset, int size)

Method Summary

Modifier and TypeMethod and Description
public <BufferType extends Buffer> Drop<BufferType>
drop()

Implements io.netty5.buffer.pool.UntetheredMemory.drop.

Produces the drop instance associated with this piece of untethered memory.
public <Memory> Memory
memory()

Implements io.netty5.buffer.pool.UntetheredMemory.memory.

Produces the recoverable memory object associated with this piece of untethered memory.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

chunkback to summary
private final PoolChunk chunk
handleback to summary
private final long handle
maxLengthback to summary
private final int maxLength
memoryback to summary
private final Object memory
threadCacheback to summary
private final PoolThreadCache threadCache

Constructor Detail

UntetheredChunkAllocationback to summary
private UntetheredChunkAllocation(Object memory, PoolChunk chunk, PoolThreadCache threadCache, long handle, int maxLength, int offset, int size)

Method Detail

dropback to summary
public <BufferType extends Buffer> Drop<BufferType> drop()

Implements io.netty5.buffer.pool.UntetheredMemory.drop.

Doc from io.netty5.buffer.pool.UntetheredMemory.drop.

Produces the drop instance associated with this piece of untethered memory.

Annotations
@Override
memoryback to summary
public <Memory> Memory memory()

Implements io.netty5.buffer.pool.UntetheredMemory.memory.

Doc from io.netty5.buffer.pool.UntetheredMemory.memory.

Produces the recoverable memory object associated with this piece of untethered memory.

Annotations
@Override