PoolArena#sizeClass#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
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final PoolArena | |
| pack-priv final Object | |
| private static final int | |
| private final Deque | |
| private final int | |
| pack-priv int | |
| private static final int | |
| pack-priv static final int | |
| pack-priv static final int | |
| pack-priv final T | |
| pack-priv PoolChunk | |
| private final int | |
| private final int | |
| pack-priv PoolChunkList | |
| private final LongCounter | pinnedBytes
Accounting of pinned memory – memory that is currently in use by ByteBuf instances. |
| pack-priv PoolChunk | |
| pack-priv static final int | |
| private final IntPriorityQueue[] | 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 |
| pack-priv final boolean |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv boolean | |
| private long | |
| private long | Returns: index in memoryMapsizeIdx of normalized size sizeIdx, PoolSubpage<T> head of subpages head)Create / initialize a new PoolSubpage of normCapacity. |
| pack-priv static int | |
| private int | |
| public int | chunkSize()
Implements io. |
| private long | |
| private long | |
| private long | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | free(long
handle to free handle, int normCapacity, ByteBuffer nioBuffer)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. |
| private long | |
| pack-priv void | |
| pack-priv void | initBuf(PooledByteBuf<T> buf, ByteBuffer nioBuffer, long handle, int reqCapacity, PoolThreadCache threadCache)
|
| pack-priv void | initBufWithSubpage(PooledByteBuf<T> buf, ByteBuffer nioBuffer, long handle, int reqCapacity, PoolThreadCache threadCache)
|
| private void | |
| private void | |
| pack-priv static boolean | |
| pack-priv static boolean | |
| pack-priv static boolean | |
| private static int | |
| private static IntPriorityQueue[] | |
| public int | |
| private void | |
| private void | |
| private int | |
| pack-priv static int | |
| pack-priv static int | |
| pack-priv static int | |
| private long | |
| private static long | |
| public String | |
| public int | usage()
Implements io. |
| private int |
| arena | back to summary |
|---|---|
| pack-priv final PoolArena<T> arena | |
| base | back to summary |
|---|---|
| pack-priv final Object base | |
| BITMAP_IDX_BIT_LENGTH | back to summary |
|---|---|
| private static final int BITMAP_IDX_BIT_LENGTH | |
| cachedNioBuffers | back to summary |
|---|---|
| private final Deque<ByteBuffer> cachedNioBuffers | |
| chunkSize | back to summary |
|---|---|
| private final int chunkSize | |
| freeBytes | back to summary |
|---|---|
| pack-priv int freeBytes | |
| INUSED_BIT_LENGTH | back to summary |
|---|---|
| private static final int INUSED_BIT_LENGTH | |
| IS_SUBPAGE_SHIFT | back to summary |
|---|---|
| pack-priv static final int IS_SUBPAGE_SHIFT | |
| IS_USED_SHIFT | back to summary |
|---|---|
| pack-priv static final int IS_USED_SHIFT | |
| memory | back to summary |
|---|---|
| pack-priv final T memory | |
| next | back to summary |
|---|---|
| pack-priv PoolChunk<T> next | |
| pageShifts | back to summary |
|---|---|
| private final int pageShifts | |
| pageSize | back to summary |
|---|---|
| private final int pageSize | |
| parent | back to summary |
|---|---|
| pack-priv PoolChunkList<T> parent | |
| pinnedBytes | back to summary |
|---|---|
| private final LongCounter pinnedBytes Accounting of pinned memory – memory that is currently in use by ByteBuf instances. | |
| prev | back to summary |
|---|---|
| pack-priv PoolChunk<T> prev | |
| RUN_OFFSET_SHIFT | back to summary |
|---|---|
| pack-priv static final int RUN_OFFSET_SHIFT | |
| runsAvail | back to summary |
|---|---|
| private final IntPriorityQueue[] runsAvail manage all avail runs | |
| runsAvailLock | back to summary |
|---|---|
| private final ReentrantLock runsAvailLock | |
| runsAvailMap | back to summary |
|---|---|
| private final LongLongHashMap runsAvailMap store the first page and last page of each avail run | |
| SIZE_BIT_LENGTH | back to summary |
|---|---|
| private static final int SIZE_BIT_LENGTH | |
| SIZE_SHIFT | back to summary |
|---|---|
| pack-priv static final int SIZE_SHIFT | |
| SUBPAGE_BIT_LENGTH | back to summary |
|---|---|
| private static final int SUBPAGE_BIT_LENGTH | |
| subpages | back to summary |
|---|---|
| private final PoolSubpage<T>[] subpages manage all subpages in this chunk | |
| unpooled | back to summary |
|---|---|
| pack-priv final boolean unpooled | |
| PoolChunk | back to summary |
|---|---|
pack-priv PoolChunk(PoolArena<T> arena, Object base, T memory, int pageSize, int pageShifts, int chunkSize, int maxPageIdx)
| |
| PoolChunk | back to summary |
|---|---|
| pack-priv PoolChunk(PoolArena<T> arena, Object base, T memory, int size) Creates a special chunk that is not pooled. | |
| allocate | back to summary |
|---|---|
| pack-priv boolean allocate(PooledByteBuf<T> buf, int reqCapacity, int sizeIdx, PoolThreadCache cache) | |
| allocateRun | back to summary |
|---|---|
| private long allocateRun(int runSize) | |
| allocateSubpage | back to summary |
|---|---|
| private long allocateSubpage(int sizeIdx, PoolSubpage<T> head) Create / initialize a new PoolSubpage of normCapacity. Any PoolSubpage created / initialized here is added to subpage pool in the PoolArena that owns this PoolChunk.
| |
| bitmapIdx | back to summary |
|---|---|
| pack-priv static int bitmapIdx(long handle) | |
| calculateRunSize | back to summary |
|---|---|
| private int calculateRunSize(int sizeIdx) | |
| chunkSize | back to summary |
|---|---|
| public int chunkSize() Implements io. Doc from io. Return the size of the chunk in bytes, this is the maximum of bytes that can be served out of the chunk.
| |
| collapseNext | back to summary |
|---|---|
| private long collapseNext(long handle) | |
| collapsePast | back to summary |
|---|---|
| private long collapsePast(long handle) | |
| collapseRuns | back to summary |
|---|---|
| private long collapseRuns(long handle) | |
| decrementPinnedMemory | back to summary |
|---|---|
| pack-priv void decrementPinnedMemory(int delta) | |
| destroy | back to summary |
|---|---|
| pack-priv void destroy() | |
| free | back to summary |
|---|---|
| pack-priv void free(long handle, int normCapacity, ByteBuffer nioBuffer) 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
| |
| freeBytes | back to summary |
|---|---|
| public int freeBytes() Implements io. Doc from io. Return the number of free bytes in the chunk.
| |
| getAvailRunByOffset | back to summary |
|---|---|
| private long getAvailRunByOffset(int runOffset) | |
| incrementPinnedMemory | back to summary |
|---|---|
| pack-priv void incrementPinnedMemory(int delta) | |
| initBuf | back to summary |
|---|---|
| pack-priv void initBuf(PooledByteBuf<T> buf, ByteBuffer nioBuffer, long handle, int reqCapacity, PoolThreadCache threadCache) | |
| initBufWithSubpage | back to summary |
|---|---|
| pack-priv void initBufWithSubpage(PooledByteBuf<T> buf, ByteBuffer nioBuffer, long handle, int reqCapacity, PoolThreadCache threadCache) | |
| insertAvailRun | back to summary |
|---|---|
| private void insertAvailRun(int runOffset, int pages, long handle) | |
| insertAvailRun0 | back to summary |
|---|---|
| private void insertAvailRun0(int runOffset, long handle) | |
| isRun | back to summary |
|---|---|
| pack-priv static boolean isRun(long handle) | |
| isSubpage | back to summary |
|---|---|
| pack-priv static boolean isSubpage(long handle) | |
| isUsed | back to summary |
|---|---|
| pack-priv static boolean isUsed(long handle) | |
| lastPage | back to summary |
|---|---|
| private static int lastPage(int runOffset, int pages) | |
| newRunsAvailqueueArray | back to summary |
|---|---|
| private static IntPriorityQueue[] newRunsAvailqueueArray(int size) | |
| pinnedBytes | back to summary |
|---|---|
| public int pinnedBytes() | |
| removeAvailRun | back to summary |
|---|---|
| private void removeAvailRun(long handle) | |
| removeAvailRun0 | back to summary |
|---|---|
| private void removeAvailRun0(long handle) | |
| runFirstBestFit | back to summary |
|---|---|
| private int runFirstBestFit(int pageIdx) | |
| runOffset | back to summary |
|---|---|
| pack-priv static int runOffset(long handle) | |
| runPages | back to summary |
|---|---|
| pack-priv static int runPages(long handle) | |
| runSize | back to summary |
|---|---|
| pack-priv static int runSize(int pageShifts, long handle) | |
| splitLargeRun | back to summary |
|---|---|
| private long splitLargeRun(long handle, int needPages) | |
| toRunHandle | back to summary |
|---|---|
| private static long toRunHandle(int runOffset, int runPages, int inUsed) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| usage | back to summary |
|---|---|
| public int usage() Implements io. Doc from io. Return the percentage of the current usage of the chunk.
| |
| usage | back to summary |
|---|---|
| private int usage(int freeBytes) | |