NCBI C++ ToolKit
Classes | Typedefs | Enumerations | Variables
nc_memory.hpp File Reference
#include <corelib/ncbithr.hpp>
#include "nc_utils.hpp"
+ Include dependency graph for nc_memory.hpp:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Go to the SVN repository for this file.

Classes

class  CNCMemManager
 Utility class for tuning memory usage in NetCache including database cache and all other memory allocations. More...
 
class  CNCMMMutex
 
class  CNCMMFreeChunkBase
 Base class for CNCMMFreeChunk. More...
 
class  CNCMMFreeChunk
 Representation of the free memory chunk, i.e. More...
 
class  CNCMMChunksPool_Getter
 Helper class to make CNCMMChunksPool available different for each thread. More...
 
class  CNCMMChunksPool
 Per-thread pool of free chunks for speeding up allocation and deallocation of individual chunks. More...
 
struct  SNCMMChainInfo
 Helper structure used to transfer information about free chunks chains between different classes. More...
 
class  CNCMMSlabBase
 Base class for memory slab containing all meta-information about it. More...
 
class  CNCMMSlab
 Representation of the memory slab - big piece of memory containing several memory chunks. More...
 
class  CNCMMBlocksSetBase
 Base class for set of equally-sized blocks. More...
 
class  CNCMMBlocksSet
 Set of equally-sized blocks with size less than size of the chunk (when subtracted size of meta-data of the set. More...
 
class  CNCMMBigBlockSet
 Special "set of equally-sized blocks" containing only one block which size is equal to size of memory chunk (a bit less actually) or more (several chunks combined). More...
 
class  CNCMMBigBlockSlab
 Special memory slab representing blocks bigger than kNCMMSlabSize (or actually kNCMMMaxCombinedSize). More...
 
class  CNCMMChainsPool_Getter
 Helper class to make CNCMMChainsPool available different for each thread. More...
 
class  CNCMMChainsPool
 Per-thread pool of free chains of chunks for speeding up their allocation and deallocation. More...
 
class  CNCMMReserve
 Central storage of all memory chunks that are not used by anybody but cannot be released to the system because of necessity to combine them in slabs. More...
 
class  CNCMMSizePool_Getter
 Helper class to make CNCMMSizePool available different for each thread. More...
 
class  CNCMMSizePool
 Per-thread pool of equally-sized memory blocks. More...
 
class  CNCMMStats_Getter
 Helper class to make CNCMMStats available different for each thread. More...
 
class  CNCMMStats
 Per-thread statistics of memory manager operations. More...
 
class  CNCMMCentral
 Central part of memory manager - all interaction with it not related to database cache starts here. More...
 

Typedefs

typedef CGuard< CNCMMMutexCNCMMMutexGuard
 

Enumerations

enum  ENCMMMode { eNCMemGrowing , eNCMemStable , eNCMemShrinking , eNCFinalized }
 Mode of operation of memory manager with regards to allocation of new memory chunks for database cache. More...
 

Variables

static const size_t kNCMMAlignSize = 2 * 1024 * 1024
 Alignment boundary to which manager will align all memory allocated from system. More...
 
static const size_t kNCMMAlignMask = ~(kNCMMAlignSize - 1)
 Mask to get aligned memory address from arbitrary memory address. More...
 
static const unsigned int kNCMMMaxThreadsCnt = 25
 Maximum number of threads when manager is the most effective in avoiding contention between threads for internal data structures. More...
 
static const size_t kNCMMChunkSize = 33280
 Size of memory chunks - minimum piece of memory operated by manager. More...
 
static const size_t kNCMMFreeDataSize
 Size of the unused data inside free memory chunk. More...
 
static const unsigned int kNCMMCntChunksInPool = 10
 Number of chunks stored in chunks pool to speed up requests for free chunks. More...
 
static const size_t kNCMMSlabSize = kNCMMAlignSize
 Size of memory slab - the minimum piece of memory allocated from system. More...
 
static const unsigned int kNCMMCntChunksInSlab
 Number of memory chunks that will fit inside one slab. More...
 
static const size_t kNCMMSetBaseSize = sizeof(CNCMMBlocksSetBase)
 Size of the blocks set meta-data. More...
 
static const size_t kNCMMSetDataSize = kNCMMChunkSize - kNCMMSetBaseSize
 Size of the real data part inside blocks set. More...
 
static const size_t kNCMMBlockExtraSize = sizeof(char*)
 Size of extra-information stored in the set for each block in addition to overall set meta-data. More...
 
static const size_t kNCMMMaxCombinedSize
 Maximum size of allocated block that can be achieved by combining several memory chunks together. More...
 
static const unsigned int kNCMMSlabEmptyGrades = 9
 Number of emptiness grades in which all slabs will be divided. More...
 
static const unsigned int kNCMMSetEmptyGrades = 5
 Number of emptiness grades in which all blocks sets will be divided. More...
 
static const unsigned int kNCMMTotalEmptyGrades
 Total number of emptiness grades that blocks sets will have. More...
 
static const unsigned int kNCMMCntSmallSizes = 39
 Number of distinct sizes (below size of memory chunk) that memory manager can allocate. More...
 

Typedef Documentation

◆ CNCMMMutexGuard

Definition at line 97 of file nc_memory.hpp.

Enumeration Type Documentation

◆ ENCMMMode

enum ENCMMMode

Mode of operation of memory manager with regards to allocation of new memory chunks for database cache.

Enumerator
eNCMemGrowing 

New memory chunks can be allocated for database cache without limits.

eNCMemStable 

Memory consumption should stay the same, so database cache should reuse memory from old pages.

eNCMemShrinking 

Memory consumption should be less, so some memory used for database cache should be freed.

eNCFinalized 

Memory manager was finalized, application will soon stop working.

Definition at line 1244 of file nc_memory.hpp.

Variable Documentation

◆ kNCMMAlignMask

const size_t kNCMMAlignMask = ~(kNCMMAlignSize - 1)
static

Mask to get aligned memory address from arbitrary memory address.

Definition at line 109 of file nc_memory.hpp.

Referenced by CNCMMCentral::x_AlignToBottom().

◆ kNCMMAlignSize

const size_t kNCMMAlignSize = 2 * 1024 * 1024
static

Alignment boundary to which manager will align all memory allocated from system.

2 Mb is not the most optimal value if amount of lost memory is taken into consideration (4 Mb will be the most optimal) but it's good enough. And it's of course far from optimal if amount of free memory not released to system is taken into consideration but it works great with maximum amount of memory allocated by NetCache and SQLite in one call to malloc (it's about 2000008 bytes).

Definition at line 107 of file nc_memory.hpp.

Referenced by CNCMMCentral::x_AlignToTop(), and CNCMMCentral::x_AllocAlignedLongWay().

◆ kNCMMBlockExtraSize

const size_t kNCMMBlockExtraSize = sizeof(char*)
static

Size of extra-information stored in the set for each block in addition to overall set meta-data.

For now it's just a pointer in the list of free blocks.

Definition at line 479 of file nc_memory.hpp.

Referenced by CNCMMCentral::x_Initialize().

◆ kNCMMChunkSize

const size_t kNCMMChunkSize = 33280
static

Size of memory chunks - minimum piece of memory operated by manager.

In ideal world manager would get memory from system only by this amount. But it's just a bit more than power of 2 and thus if manager always did allocate aligned memory of this amount then memory would be very sparse and could be very bad for the system. So manager allocates memory by slabs.

See also
CNCMMSlab

Definition at line 123 of file nc_memory.hpp.

Referenced by CNCMMStats::ChunksChainAlloced(), CNCMMStats::ChunksChainFreed(), CNCMMStats::ChunksPoolCleaned(), CNCMMStats::ChunksPoolRefilled(), CNCMMStats::FreeChunkCreated(), CNCMMStats::FreeChunkDeleted(), CNCMMBlocksSet::operator new(), CNCMMStats::Print(), CNCMMCentral::x_CalcMemoryMode(), CNCMMChainsPool::x_GetChain(), CNCMMChunksPool::x_GetChunk(), CNCMMSlab::x_GetChunkIndex(), CNCMMBigBlockSet::x_GetChunksCnt(), CNCMMSizePool::x_GetEmptySet(), CNCMMSizePool::x_NewEmptySet(), CNCMMChainsPool::x_PutChain(), CNCMMChunksPool::x_PutChunk(), and CNCMMChainsPool::x_ReleaseAllChains().

◆ kNCMMCntChunksInPool

const unsigned int kNCMMCntChunksInPool = 10
static

Number of chunks stored in chunks pool to speed up requests for free chunks.

The more this number the less requests is made to the central chunks storage and thus the less contention between threads, but the more memory is free and waiting for future use without releasing to system.

Definition at line 263 of file nc_memory.hpp.

Referenced by CNCMMReserve::DumpChunks(), CNCMMReserve::GetChunks(), CNCMMChunksPool::x_AdvanceChunkPtr(), CNCMMChunksPool::x_CleanPoolSpace(), CNCMMChunksPool::x_RefillPool(), and CNCMMChunksPool::x_ReleaseAllChunks().

◆ kNCMMCntChunksInSlab

const unsigned int kNCMMCntChunksInSlab
static
Initial value:
=
static_cast<unsigned int>(kNCMMSlabSize / kNCMMChunkSize)
static const size_t kNCMMChunkSize
Size of memory chunks - minimum piece of memory operated by manager.
Definition: nc_memory.hpp:123
static const size_t kNCMMSlabSize
Size of memory slab - the minimum piece of memory allocated from system.
Definition: nc_memory.hpp:356

Number of memory chunks that will fit inside one slab.

Definition at line 358 of file nc_memory.hpp.

Referenced by CNCMMStats::ChunksChainFreed(), CNCMMSlab::CNCMMSlab(), CNCMMReserve::DumpChain(), CNCMMChainsPool::GetChain(), CNCMMSlab::MarkChainFree(), CNCMMBigBlockSet::operator delete(), CNCMMBigBlockSet::operator new(), CNCMMStats::Print(), CNCMMChainsPool::PutChain(), CNCMMStats::x_CollectAllTo(), CNCMMReserve::x_CreateMergedChain(), and CNCMMChainsPool::x_ReleaseAllChains().

◆ kNCMMCntSmallSizes

const unsigned int kNCMMCntSmallSizes = 39
static

Number of distinct sizes (below size of memory chunk) that memory manager can allocate.

Number should correspond to number of elements in kNCMMSmallSize.

Definition at line 1022 of file nc_memory.hpp.

Referenced by CNCMMSizePool_Getter::CreateTlsObject(), CNCMMSizePool_Getter::DeleteTlsObject(), CNCMMBlocksSetBase::GetBlockSize(), CNCMMStats::Print(), CNCMMStats::x_CollectAllTo(), and CNCMMCentral::x_Initialize().

◆ kNCMMFreeDataSize

const size_t kNCMMFreeDataSize
static
Initial value:
Base class for CNCMMFreeChunk.
Definition: nc_memory.hpp:133

Size of the unused data inside free memory chunk.

Constant is necessary to make size of CNCMMFreeChunk exactly equal to kNCMMChunkSize.

Definition at line 171 of file nc_memory.hpp.

◆ kNCMMMaxCombinedSize

const size_t kNCMMMaxCombinedSize
static
Initial value:
=
Special memory slab representing blocks bigger than kNCMMSlabSize (or actually kNCMMMaxCombinedSize).
Definition: nc_memory.hpp:579

Maximum size of allocated block that can be achieved by combining several memory chunks together.

Size of this block can actually be a bit more than all chunks in slab combined but it definitely won't exceed size of standardly allocated slab.

Definition at line 615 of file nc_memory.hpp.

Referenced by CNCMMCentral::AllocMemory(), CNCMMCentral::DeallocMemory(), and CNCMMBigBlockSet::operator new().

◆ kNCMMMaxThreadsCnt

const unsigned int kNCMMMaxThreadsCnt = 25
static

◆ kNCMMSetBaseSize

const size_t kNCMMSetBaseSize = sizeof(CNCMMBlocksSetBase)
static

◆ kNCMMSetDataSize

const size_t kNCMMSetDataSize = kNCMMChunkSize - kNCMMSetBaseSize
static

Size of the real data part inside blocks set.

Full size of the set (meta-data + real data) should always be equal to size of memory chunk.

See also
kNCMMChunkSize

Definition at line 475 of file nc_memory.hpp.

Referenced by CNCMMBlocksSet::CNCMMBlocksSet(), and CNCMMCentral::x_Initialize().

◆ kNCMMSetEmptyGrades

const unsigned int kNCMMSetEmptyGrades = 5
static

Number of emptiness grades in which all blocks sets will be divided.

Number shouldn't be too big because all algorithms are linear. And this number is made power of 2 + 1 because 0 is always a separated grade, but all others will form power of 2.

Definition at line 731 of file nc_memory.hpp.

Referenced by CNCMMBlocksSet::x_CalcEmptyGrade(), and CNCMMCentral::x_Initialize().

◆ kNCMMSlabEmptyGrades

const unsigned int kNCMMSlabEmptyGrades = 9
static

Number of emptiness grades in which all slabs will be divided.

Number shouldn't be too big because all algorithms are linear. And this number is made power of 2 + 1 because 0 is always a separated grade, but all others will form power of 2.

See also
CNCMMReserve

Definition at line 726 of file nc_memory.hpp.

Referenced by CNCMMSlab::CNCMMSlab(), CNCMMReserve::GetChain(), CNCMMReserve::GetChunks(), CNCMMReserve::Initialize(), CNCMMReserve::IntroduceChain(), CNCMMSlab::MarkChainFree(), and CNCMMSlab::x_CalcEmptyGrade().

◆ kNCMMSlabSize

const size_t kNCMMSlabSize = kNCMMAlignSize
static

Size of memory slab - the minimum piece of memory allocated from system.

Definition at line 356 of file nc_memory.hpp.

Referenced by CNCMMSlab::operator delete(), CNCMMSlab::operator new(), CNCMMStats::SlabCreated(), and CNCMMStats::SlabDeleted().

◆ kNCMMTotalEmptyGrades

const unsigned int kNCMMTotalEmptyGrades
static
Initial value:
=
static const unsigned int kNCMMSetEmptyGrades
Number of emptiness grades in which all blocks sets will be divided.
Definition: nc_memory.hpp:731
static const unsigned int kNCMMSlabEmptyGrades
Number of emptiness grades in which all slabs will be divided.
Definition: nc_memory.hpp:726

Total number of emptiness grades that blocks sets will have.

They will be divided into groups by emptiness grades of slabs they belong to and sub-divided by their own grades.

See also
CNCMMSizePool

Definition at line 737 of file nc_memory.hpp.

Referenced by CNCMMSizePool::x_AllocateBlock(), CNCMMBlocksSet::x_CalcEmptyGrade(), and CNCMMSizePool::~CNCMMSizePool().

Modified on Wed May 15 15:03:16 2024 by modify_doxy.py rev. 669887