Class UpfrontAllocatingPageSource
- java.lang.Object
-
- org.terracotta.offheapstore.paging.UpfrontAllocatingPageSource
-
- All Implemented Interfaces:
PageSource
public class UpfrontAllocatingPageSource extends java.lang.Object implements PageSource
An upfront allocating direct byte buffer source.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 Classes Modifier and Type Class Description (package private) static classUpfrontAllocatingPageSource.AllocatedRegionstatic classUpfrontAllocatingPageSource.ThresholdDirection
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringALLOCATION_LOG_LOCATIONprivate intavailableSetprivate java.util.List<java.nio.ByteBuffer>buffersprivate java.util.SortedMap<java.lang.Long,java.lang.Runnable>fallingThresholdsprivate static org.slf4j.LoggerLOGGERprivate static doublePROGRESS_LOGGING_STEP_SIZEprivate static longPROGRESS_LOGGING_THRESHOLDprivate static java.util.Comparator<Page>REGION_COMPARATORprivate java.util.SortedMap<java.lang.Long,java.lang.Runnable>risingThresholdsprivate java.util.List<PowerOfTwoAllocator>sliceAllocatorsprivate java.util.List<PowerOfTwoAllocator>victimAllocatorsprivate java.util.List<java.util.NavigableSet<Page>>victims
-
Constructor Summary
Constructors Modifier Constructor Description UpfrontAllocatingPageSource(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.RunnableaddAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction, long threshold, java.lang.Runnable action)Adds an allocation threshold action.Pageallocate(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 java.util.Collection<java.nio.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 java.util.Collection<java.nio.ByteBuffer>bufferAllocation(BufferSource source, int toAllocate, int minChunk, boolean fixed, java.io.PrintStream allocatorLog, long start)private static java.io.PrintStreamcreateAllocatorLog(long max, int maxChunk, int minChunk)private java.util.List<Page>findVictimPages(int chunk, int address, int size)private voidfireThresholds(long incoming, long outgoing)voidfree(Page page)Frees the supplied buffer.longgetAllocatedSize()longgetAllocatedSizeUnSync()longgetCapacity()Return the total allocated capacity, used or notprivate booleanisUnavailable(int size)private voidmarkAllAvailable()private voidmarkUnavailable(int size)java.lang.RunnableremoveAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction, long threshold)Removes an allocation threshold action.java.lang.StringtoString()private static <T> TuninterruptibleGet(java.util.concurrent.Future<T> future)
-
-
-
Field Detail
-
ALLOCATION_LOG_LOCATION
public static final java.lang.String 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:
- Constant Field Values
-
PROGRESS_LOGGING_THRESHOLD
private static final long PROGRESS_LOGGING_THRESHOLD
-
REGION_COMPARATOR
private static final java.util.Comparator<Page> REGION_COMPARATOR
-
risingThresholds
private final java.util.SortedMap<java.lang.Long,java.lang.Runnable> risingThresholds
-
fallingThresholds
private final java.util.SortedMap<java.lang.Long,java.lang.Runnable> fallingThresholds
-
sliceAllocators
private final java.util.List<PowerOfTwoAllocator> sliceAllocators
-
victimAllocators
private final java.util.List<PowerOfTwoAllocator> victimAllocators
-
buffers
private final java.util.List<java.nio.ByteBuffer> buffers
-
victims
private final java.util.List<java.util.NavigableSet<Page>> victims
-
availableSet
private volatile int availableSet
-
-
Constructor Detail
-
UpfrontAllocatingPageSource
public UpfrontAllocatingPageSource(BufferSource source, long toAllocate, int chunkSize)
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 Detail
-
getCapacity
public long getCapacity()
Return the total allocated capacity, used or not- Returns:
- the total capacity
-
allocate
public Page allocate(int size, boolean thief, boolean victim, OffHeapStorageArea owner)
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
private Page allocateAsThief(int size, boolean victim, OffHeapStorageArea owner)
-
findVictimPages
private java.util.List<Page> findVictimPages(int chunk, int address, int size)
-
allocateFromFree
private Page allocateFromFree(int size, boolean victim, OffHeapStorageArea owner)
-
free
public void free(Page page)
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()
-
isUnavailable
private boolean isUnavailable(int size)
-
markAllAvailable
private void markAllAvailable()
-
markUnavailable
private void markUnavailable(int size)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
fireThresholds
private void fireThresholds(long incoming, long outgoing)
-
addAllocationThreshold
public java.lang.Runnable addAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction, long threshold, java.lang.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 java.lang.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 java.util.Collection<java.nio.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:
java.lang.IllegalArgumentException- when we fail to allocate the requested memory
-
bufferAllocation
private static java.util.Collection<java.nio.ByteBuffer> bufferAllocation(BufferSource source, int toAllocate, int minChunk, boolean fixed, java.io.PrintStream allocatorLog, long start)
-
uninterruptibleGet
private static <T> T uninterruptibleGet(java.util.concurrent.Future<T> future)
-
createAllocatorLog
private static java.io.PrintStream createAllocatorLog(long max, int maxChunk, int minChunk)
-
-