Class UpfrontAllocatingPageSource
- All Implemented Interfaces:
PageSource
This buffer source implementation allocates all of its required storage up-front in fixed size chunks. Runtime allocations are then satisfied using slices from these initial chunks.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classstatic enum -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringprivate intprivate final List<ByteBuffer> private static final org.slf4j.Loggerprivate static final doubleprivate static final longprivate static final Comparator<Page> private final List<PowerOfTwoAllocator> private final List<PowerOfTwoAllocator> private final List<NavigableSet<Page>> -
Constructor Summary
ConstructorsModifierConstructorDescriptionUpfrontAllocatingPageSource(BufferSource source, long toAllocate, int chunkSize) Create an up-front allocating buffer source oftoAllocatetotal bytes, inchunkSizebyte chunks.UpfrontAllocatingPageSource(BufferSource source, long toAllocate, int maxChunk, int minChunk) Create an up-front allocating buffer source oftoAllocatetotal bytes, in maximally sized chunks, within the given bounds.privateUpfrontAllocatingPageSource(BufferSource source, long toAllocate, int maxChunk, int minChunk, boolean fixed) Create an up-front allocating buffer source oftoAllocatetotal bytes, in maximally sized chunks, within the given bounds. -
Method Summary
Modifier and TypeMethodDescriptionaddAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction, long threshold, Runnable action) Adds an allocation threshold action.allocate(int size, boolean thief, boolean victim, OffHeapStorageArea owner) Allocates a byte buffer of at least the given size.private PageallocateAsThief(int size, boolean victim, OffHeapStorageArea owner) private static Collection<ByteBuffer> allocateBackingBuffers(BufferSource source, long toAllocate, int maxChunk, int minChunk, boolean fixed) Allocate multiple buffers to fulfill the requested memorytoAllocate.private PageallocateFromFree(int size, boolean victim, OffHeapStorageArea owner) private static Collection<ByteBuffer> bufferAllocation(BufferSource source, int toAllocate, int minChunk, boolean fixed, PrintStream allocatorLog, long start) private static PrintStreamcreateAllocatorLog(long max, int maxChunk, int minChunk) findVictimPages(int chunk, int address, int size) private voidfireThresholds(long incoming, long outgoing) voidFrees the supplied buffer.longlonglongReturn the total allocated capacity, used or notprivate booleanisUnavailable(int size) private voidprivate voidmarkUnavailable(int size) removeAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction, long threshold) Removes an allocation threshold action.toString()private static <T> TuninterruptibleGet(Future<T> future)
-
Field Details
-
ALLOCATION_LOG_LOCATION
-
LOGGER
private static final org.slf4j.Logger LOGGER -
PROGRESS_LOGGING_STEP_SIZE
private static final double PROGRESS_LOGGING_STEP_SIZE- See Also:
-
PROGRESS_LOGGING_THRESHOLD
private static final long PROGRESS_LOGGING_THRESHOLD -
REGION_COMPARATOR
-
risingThresholds
-
fallingThresholds
-
sliceAllocators
-
victimAllocators
-
buffers
-
victims
-
availableSet
private volatile int availableSet
-
-
Constructor Details
-
UpfrontAllocatingPageSource
Create an up-front allocating buffer source oftoAllocatetotal bytes, inchunkSizebyte chunks.- Parameters:
source- source from which initial buffers will be allocatedtoAllocate- total space to allocate in byteschunkSize- chunkSize size to allocate in bytes
-
UpfrontAllocatingPageSource
public UpfrontAllocatingPageSource(BufferSource source, long toAllocate, int maxChunk, int minChunk) Create an up-front allocating buffer source oftoAllocatetotal bytes, in maximally sized chunks, within the given bounds.- Parameters:
source- source from which initial buffers will be allocatedtoAllocate- total space to allocate in bytesmaxChunk- the largest chunk size in bytesminChunk- the smallest chunk size in bytes
-
UpfrontAllocatingPageSource
private UpfrontAllocatingPageSource(BufferSource source, long toAllocate, int maxChunk, int minChunk, boolean fixed) Create an up-front allocating buffer source oftoAllocatetotal bytes, in maximally sized chunks, within the given bounds.By default we try to allocate chunks of
maxChunksize. However, unlessfixedis true, in case of allocation failure, we will try to allocate half-smaller chunks. We do not allocate chunks smaller thanminChunkthough.- Parameters:
source- source from which initial buffers will be allocatedtoAllocate- total space to allocate in bytesmaxChunk- the largest chunk size in bytesminChunk- the smallest chunk size in bytesfixed- if the chunks should all be of sizemaxChunkor can be smaller
-
-
Method Details
-
getCapacity
public long getCapacity()Return the total allocated capacity, used or not- Returns:
- the total capacity
-
allocate
Allocates a byte buffer of at least the given size.This
BufferSourceis limited to allocating regions that are a power of two in size. Supplied sizes are therefore rounded up to the next largest power of two.- Specified by:
allocatein interfacePageSource- Parameters:
size- size of page to allocatethief-trueif the allocation can steal space from victimsvictim-trueif the allocated page should be eligible for stealingowner- owner from which subsequent steal should occur- Returns:
- a buffer of at least the given size
-
allocateAsThief
-
findVictimPages
-
allocateFromFree
-
free
Frees the supplied buffer.If the given buffer was not allocated by this source or has already been freed then an
AssertionErroris thrown.- Specified by:
freein interfacePageSource
-
getAllocatedSize
public long getAllocatedSize() -
getAllocatedSizeUnSync
public long getAllocatedSizeUnSync() -
markAllAvailable
private void markAllAvailable() -
toString
-
fireThresholds
private void fireThresholds(long incoming, long outgoing) -
addAllocationThreshold
public Runnable addAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction, long threshold, Runnable action) Adds an allocation threshold action.There can be only a single action associated with each unique direction and threshold combination. If an action is already associated with the supplied combination then the action is replaced by the new action and the old action is returned.
Actions are fired on passing through the supplied threshold and are called synchronously with the triggering allocation. This means care must be taken to avoid mutating any map that uses this page source from within the action otherwise deadlocks may result. Exceptions thrown by the action will be caught and logged by the page source and will not be propagated on the allocating thread.
- Parameters:
direction- new actions directionthreshold- new actions threshold levelaction- fired on breaching the threshold- Returns:
- the replaced action or
nullif no action was present.
-
removeAllocationThreshold
public Runnable removeAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction, long threshold) Removes an allocation threshold action.Removes the allocation threshold action for the given level and direction.
- Parameters:
direction- registered actions directionthreshold- registered actions threshold level- Returns:
- the removed condition or
nullif no action was present.
-
allocateBackingBuffers
private static Collection<ByteBuffer> allocateBackingBuffers(BufferSource source, long toAllocate, int maxChunk, int minChunk, boolean fixed) Allocate multiple buffers to fulfill the requested memorytoAllocate. We first dividetoAllocatein chunks of sizemaxChunkand try to allocate them in parallel on all available processors. If one chunk fails to be allocated, we try to allocate two chunks ofmaxChunk / 2. If this allocation fails, we continue dividing until we reach of size ofminChunk. If at that moment, the allocation still fails, anIllegalArgumentExceptionis thrown.When
fixedis requested, we will only allocated buffers ofmaxChunksize. If allocation fails, anIllegalArgumentExceptionis thrown without any division.If the allocation is interrupted, the method will ignore it and continue allocation. It will then return with the interrupt flag is set.
- Parameters:
source- source used to allocate memory bufferstoAllocate- total amount of memory to allocatemaxChunk- maximum size of a buffer. This is the targeted size for all buffers if everything goes wellminChunk- minimum buffer size allowedfixed- if all buffers should have a the same size (except the last one withtoAllocate % maxChunk != 0, if true,minChunkisn't used- Returns:
- the list of allocated buffers
- Throws:
IllegalArgumentException- when we fail to allocate the requested memory
-
bufferAllocation
private static Collection<ByteBuffer> bufferAllocation(BufferSource source, int toAllocate, int minChunk, boolean fixed, PrintStream allocatorLog, long start) -
uninterruptibleGet
-
createAllocatorLog
-