MemoryManager Class Reference

This class is used to manage/track blocks of data and MemoryFileBlock objects. More...

List of all members.

Public Member Functions

virtual simba_uint32 AllocateBlocks (const IMemoryContext &in_memContext, MemoryToken in_reserveToken, simba_uint32 in_numberOfBlocks, simba_uint64 in_blockSize, bool in_reserved=true)
 Allocate the input number of blocks for the caller. The caller is responsible for making sure that they do not exceed this amount of memory. Best practice is to call ReserveBlock before calling AllocateBlocks.
virtual
Simba::DSI::MemoryFileBlock
AllocateMemoryFileBlock (simba_size_t in_blockSize)
 Allocate a MemoryFileBlock.
virtual bool AllocateUnreservedBlock (const IMemoryContext &in_memContext, MemoryToken in_reserveToken, simba_uint64 in_blockSize)
 Allocate a block of memory. It is the responsibility of the caller that it does no exceed this memory limit.
virtual bool CanSwap ()
 Ask the MemoryManager if the memory strategy allows for external swapping.
virtual simba_uint64 ChooseBlockCount (simba_uint64 in_blockCount)
 Choose the block count to use.
void CleanupMemoryRecords (MemoryToken in_querySpecificID)
 Cleans up the records and the stats associated with a query. If there was a memory reserved/allocated but not released by the requester, this method cleans up any memory allocated to any requested within a query. This method should only be called when a query is going out of context (destroyed.).
virtual simba_uint16 GetMemoryStrategy ()
 Get the memory strategy used by the MemoryManager.
MemoryToken GetUniqueMemoryToken ()
 Generates a unique token for a memory consumer to be used.
virtual bool IsMemoryAvailable (simba_uint64 in_requestSize)
 Check to see if a certain amount of memory is available for allocation.
virtual void NotifyCancellation ()
 Notify all the memory requesters that are waiting that a cancel has been called. It is the responsibility of the requester to take proper action when waking up for the wait.
virtual bool ReleaseBlock (Simba::DSI::MemoryFileBlock *in_memoryFileBlock)
 Release a MemoryFileBlock.
virtual void ReleaseBlock (MemoryToken in_reserveToken, simba_uint64 in_size)
 Free the allocated block.
void ReleaseMemoryResources (MemoryToken in_reserveToken)
 Release the allocated and the reserved blocks.
void ReleaseMemoryToken (MemoryToken in_toRelease)
 Releases a token previously returned from GetUniqueMemoryToken().
virtual void ReserveBlocks (const IMemoryContext &in_memContext, MemoryToken in_reserveToken, simba_uint32 in_numberOfBlocks, simba_uint64 in_blockSize)
 Reserve blocks in the memory manager without actually allocating them, allowing for later release and actual allocation. This method will make the requester wait if there is hope for memory to become available. The requester is able to cancel waiting if SQLCancel is called on it.
virtual simba_uint64 ReserveMax (MemoryToken in_reserveToken)
 Reserve as much amount of memory as possible.
void TransferAllocationBetweenRequesters (MemoryToken in_sourceToken, MemoryToken in_targetToken, const IMemoryContext &in_targetContext, simba_uint64 in_bytesToTransfer)
 Transfer previously allocated memory from the requester identified by in_sourceToken to the requester identified by in_targetToken.
virtual ~MemoryManager ()
 Destructor.

Static Public Member Functions

static Simba::DSI::MemoryManagerGetInstance ()
 Singleton accessor.
static bool IsValidMemoryStrategy (simba_uint16 in_memStrategy)
 Ask the MemoryManager if the memory strategy value is valid.
static void SetInstance (Simba::DSI::MemoryManager *in_instance)
 Singleton setter.

Protected Member Functions

 MemoryManager (simba_unsigned_native in_maximumMemorySize, simba_uint16 in_thresholdPercent)
 Protected constructor.

Detailed Description

This class is used to manage/track blocks of data and MemoryFileBlock objects.

THE MEMORY MANAGER HAS BEEN UPDATED TO TRACK ALL THE REQUESTERS SEPARATELY AS WELL AS TOGETHER AS A REQUSTER'S WITHIN A QUERY GROUP. ANY MEMORY REQUESTER SHOULD REGISTER ITSELF ON EVERY RESERVE AND ALLOCATION VIA A POINTER TO ITSELF AND A MEMORY CONTEXT THAT PROVIDES A UNIQUE QUERY SPECIFIC ID.

The caller allocates MemoryFileBlocks using the recommended AllocateBlocks method or the AllocateUnreservedBlocks method. There are also two depreciated AllocateBlock methods that should not be used.

Allocate ReservedBlocks method should be called after the ReserveBlock method. The ReserveBlock method can make the requester wait until enough memory becomes available, this is the recommended approach one parallel queries are being processed.

The returned pointers are NOT OWNED by the caller. The pointers must never be deleted by the caller. The caller is responsible for calling the ReleaseBlock methods to properly dispose of the allocated memory.

The current implementation of this class allows for allocation/deallocation of and MemoryFileBlocks. It manages the total memory usage for these kinds of objects. It does not currently keep track of the allocated pointers for validation during deallocation or to clean up memory. It is the responsibility of the users of this class to pass in valid MemoryFileBlocks for deallocation and they MUST NOT delete the and MemoryFileBlocks that they allocate via the MemoryManager.

The caller can also request a conceptual 'block' of data of a specified amount. In this case the caller is responsible for allocating / deallocated their own memory and making sure that they track the memory usage and make sure it does not exceed the requested amount. They are also responsible for calling ReleaseBlock with the amount that they have requested.

The development of the DSI_MEM_STRATEGY_LARGE_MEM_DISK_SWAP memory strategy has introduced an issue when executing queries that requires sorting of an amount of data larger than the memory limit available to the MemoryManager. The initial SwapManager ends up allocating all memory leading to a creation failure of the destination SwapManager at the beginning of the sort. To solve this issue, a memory threshold is added to the memory manager. Once the total allocated memory reaches this threshold, the MemoryManager enters a low memory mode. It means that it only accepts to allocate blocks for newly created SwapManager. To differentiate both allocation, a ReserveBlock() method followed by AllocateBlocks() method should be used to llocate the initial blocks of a SwapManager (see SwapManager constructor for more information). The AllocateUnreservedBlock() should still be used as before to allocate blocks each time one is needed when doing an Append() or MoveTo() operation on a SwapManager.

This class can be subclassed and methods overridden to provide custom behaviour. An instance of the subclass should be created and passed into a call to MemoryManager::SetInstance(). It should be done before any call to GetInstance(). It is best done in a DSII's driver subclass.


Constructor & Destructor Documentation

virtual ~MemoryManager (  )  [virtual]

Destructor.

MemoryManager ( simba_unsigned_native  in_maximumMemorySize,
simba_uint16  in_thresholdPercent 
) [protected]

Protected constructor.

MemoryManager is a singleton and only MemoryManager::GetInstance() can instantiate an instance.

Parameters:
in_maximumMemorySize The maximum memory size that MemoryManager can allocate.
in_thresholdPercent The percent of used memory after which the MemoryManager goes into low memory mode.

Member Function Documentation

virtual simba_uint32 AllocateBlocks ( const IMemoryContext in_memContext,
MemoryToken  in_reserveToken,
simba_uint32  in_numberOfBlocks,
simba_uint64  in_blockSize,
bool  in_reserved = true 
) [virtual]

Allocate the input number of blocks for the caller. The caller is responsible for making sure that they do not exceed this amount of memory. Best practice is to call ReserveBlock before calling AllocateBlocks.

The result is the number of block that have been allocated at the specified block size.

Parameters:
in_memContext The Memory context. If the query has already reserved memory before allocation, the context should be equal to the reservation context.
in_reserveToken The token to associated with the reserved tokens, specified in ReserveBlocks(). Should never be null (NOT OWN)
in_numberOfBlocks The number of blocks to allocate.
in_blockSize The size of each block.
in_reserved If set to true it indicates that a reservation already exists. The requester can allocate more than what reserved but this is not good practice, if set to false new allocation is created if possible.
Returns:
The number of blocks that have been successfully allocated.
virtual Simba::DSI::MemoryFileBlock* AllocateMemoryFileBlock ( simba_size_t  in_blockSize  )  [virtual]

Allocate a MemoryFileBlock.

NOTE: The MemoryFileBlock pointer returned should not be deleted by the caller. ReleaseBlock() must be called to deallocate the memory correctly.

Parameters:
in_blockSize The size in bytes of the MemoryFileBlock to create.
Returns:
a pointer to the allocated MemoryFileBlock. (OWN)
virtual bool AllocateUnreservedBlock ( const IMemoryContext in_memContext,
MemoryToken  in_reserveToken,
simba_uint64  in_blockSize 
) [virtual]

Allocate a block of memory. It is the responsibility of the caller that it does no exceed this memory limit.

The allocation is refused in case of low memory mode.

Parameters:
in_memContext The Memory context, can be null if this node has previously reserved the memory, if provided should be equal to the context reserved under. CAN NOT BE NULL IF the token has not reserved before.
in_reserveToken The token to associated with the reserved tokens. specified in ReserveBlocks(). Should never be null (NOT OWN)
in_blockSize The size of the block to allocate.
Returns:
true if the block can be allocated; false otherwise.
virtual bool CanSwap (  )  [virtual]

Ask the MemoryManager if the memory strategy allows for external swapping.

Returns:
true if external swapping is allowed.
virtual simba_uint64 ChooseBlockCount ( simba_uint64  in_blockCount  )  [virtual]

Choose the block count to use.

The fixed block count is passed in. If the memory strategy allows for more memory allocation, VARIABLE_BLOCK_COUNT will be returned otherwise the value passed in is returned.

Parameters:
in_blockCount The fixed block count to use if variable allocation is not supported by the current memory strategy.
Returns:
a fixed block count or VARIABLE_BLOCK_COUNT.
void CleanupMemoryRecords ( MemoryToken  in_querySpecificID  ) 

Cleans up the records and the stats associated with a query. If there was a memory reserved/allocated but not released by the requester, this method cleans up any memory allocated to any requested within a query. This method should only be called when a query is going out of context (destroyed.).

Parameters:
in_querySpecificID The query specific id to be cleaned up from the records. NO THROW Guarantee.
static Simba::DSI::MemoryManager* GetInstance (  )  [static]

Singleton accessor.

Returns:
pointer to the Memory Manager instance. (NOT OWN)
virtual simba_uint16 GetMemoryStrategy (  )  [virtual]

Get the memory strategy used by the MemoryManager.

The following values will be returned: DSI_MEM_STRATEGY_MEMORY_ONLY When used, MemoryManager and related classes will use memory only. They will allocate as much memory as needed up to the maximum amount and will perform no external swapping. DSI_MEM_STRATEGY_REG_MEM_DISK_SWAP When used, MemoryManager and related classes will use the original swapping implementation. They will allocate a fixed amount of memory before performing external swapping. DSI_MEM_STRATEGY_LARGE_MEM_DISK_SWAP When used, MemoryManager and related classes will allocate as much memory as needed up to the maximum amount and will perform external swapping when memory is not available.

Returns:
the memory strategy as a simba_uint16.
MemoryToken GetUniqueMemoryToken (  ) 

Generates a unique token for a memory consumer to be used.

The returned token must be freed with ReleaseMemoryToken() when it is no longer needed.

virtual bool IsMemoryAvailable ( simba_uint64  in_requestSize  )  [virtual]

Check to see if a certain amount of memory is available for allocation.

Parameters:
in_requestSize The requested size of memory in bytes.
Returns:
true if requested memory is available otherwise false.
static bool IsValidMemoryStrategy ( simba_uint16  in_memStrategy  )  [static]

Ask the MemoryManager if the memory strategy value is valid.

See comment for GetMemoryStrategy() for valid values.

Returns:
true if memory strategy value is valid.
virtual void NotifyCancellation (  )  [virtual]

Notify all the memory requesters that are waiting that a cancel has been called. It is the responsibility of the requester to take proper action when waking up for the wait.

virtual bool ReleaseBlock ( Simba::DSI::MemoryFileBlock in_memoryFileBlock  )  [virtual]

Release a MemoryFileBlock.

Parameters:
in_memoryFileBlock The MemoryFileBlock to be released. (NOT OWN)
Returns:
true if MemoryFileBlock released otherwise false.
virtual void ReleaseBlock ( MemoryToken  in_reserveToken,
simba_uint64  in_size 
) [virtual]

Free the allocated block.

Parameters:
in_reserveToken The token the block was allocated on.
in_size The size of the memory to release.
void ReleaseMemoryResources ( MemoryToken  in_reserveToken  ) 

Release the allocated and the reserved blocks.

Parameters:
in_reserveToken The id of the requester.
void ReleaseMemoryToken ( MemoryToken  in_toRelease  ) 

Releases a token previously returned from GetUniqueMemoryToken().

virtual void ReserveBlocks ( const IMemoryContext in_memContext,
MemoryToken  in_reserveToken,
simba_uint32  in_numberOfBlocks,
simba_uint64  in_blockSize 
) [virtual]

Reserve blocks in the memory manager without actually allocating them, allowing for later release and actual allocation. This method will make the requester wait if there is hope for memory to become available. The requester is able to cancel waiting if SQLCancel is called on it.

Parameters:
in_memContext The query specific context the following token is a emerging from.
in_reserveToke The token to associated with the specific requester (NOT OWN).
in_numberOfBlocks The number of blocks to reserve.
in_blockSize The size of each block to reserve.
virtual simba_uint64 ReserveMax ( MemoryToken  in_reserveToken  )  [virtual]

Reserve as much amount of memory as possible.

Parameters:
in_reserveToken The token to associated with the reserved tokens. (NOT OWN) (NOT NULL)
Returns:
the amount reserved.
static void SetInstance ( Simba::DSI::MemoryManager in_instance  )  [static]

Singleton setter.

Parameters:
in_instance The Memory Manager instance to manage in the singleton. (OWN)
void TransferAllocationBetweenRequesters ( MemoryToken  in_sourceToken,
MemoryToken  in_targetToken,
const IMemoryContext in_targetContext,
simba_uint64  in_bytesToTransfer 
)

Transfer previously allocated memory from the requester identified by in_sourceToken to the requester identified by in_targetToken.

Note: The source may only transfer allocated memory, _NOT_ reserved memory.

Parameters:
in_sourceToken The requester to transfer FROM.
in_targetToken The requester to transfer TO.
in_targetContext The memory context for the target requester.
in_bytesToTranser The number of bytes of memory allocation to transfer between the requesters.
Exceptions:
If in_sourceToken == in_targetToken.
If either in_sourceToken or in_targetToken are not valid requesters.
If the requester has not allocated at least in_bytesToTransfer.

The documentation for this class was generated from the following file:

Generated on Wed May 17 14:21:16 2017 for SimbaEngine 10.1.3.1011 by simba