NCBI C++ ToolKit
Classes | Macros | Functions | Variables
memory_man.cpp File Reference
#include "task_server_pch.hpp"
#include "threads_man.hpp"
#include "memory_man.hpp"
#include "srv_stat.hpp"
+ Include dependency graph for memory_man.cpp:

Go to the source code of this file.

Go to the SVN repository for this file.

Classes

struct  SMMBlocksPool
 
struct  SMMPageHeader
 
struct  SMMFreePageList
 
struct  SMMFreePageGrades
 
class  CMMFlusher
 
struct  SMMMemPoolsSet
 

Macros

#define __NC_MEMMAN_USE_MALLOC_HOOK   1
 
#define __NC_MEMMAN_DEBUG   0
 
#define __NC_MEMMAN_ALLPTR_COUNT   0
 
#define __NC_MEMMAN_PERTHREAD_STAT   0
 
#define __NC_MEMMAN_PERTHREAD_STAT_ARG(x)
 

Functions

Int8 GetMPageCount (void)
 
static void s_InitPoolsSet (SMMMemPoolsSet *pool_set)
 
static void s_LowLevelInit (void)
 
static void * s_DoMmap (size_t size)
 
static void s_DoUnmap (void *ptr, size_t size)
 
static bool s_TryUnmap (void *ptr, size_t size)
 
static void * s_SysAllocLongWay (size_t ptr, size_t size)
 
static void * s_SysAlloc (size_t size)
 
static void s_SysFree (void *ptr, size_t size)
 
static SMMPageHeaders_GetPageByPtr (void *ptr)
 
static bool s_IsInFreeList (SMMPageHeader *page)
 
static bool s_IsFreeListEmpty (SMMPageHeader *list_head)
 
static void s_FreeListRemove (SMMPageHeader *page)
 
static void s_FreeListAddHead (SMMPageHeader *list_head, SMMPageHeader *page)
 
static void s_FreeListAddTail (SMMPageHeader *list_head, SMMPageHeader *page)
 
static void s_IncPoolIdx (Uint2 &idx)
 
static void s_PutToFreeList (SMMPageHeader *page, Uint2 size_idx, bool to_head)
 
static bool s_RemoveFromFreeList (SMMPageHeader *page, Uint2 size_idx)
 
static SMMPageHeaders_AllocNewPage (Uint2 size_idx, SMMStat *)
 
static void * s_FillFromPage (SMMBlocksPool *pool, Uint2 size_idx, SMMPageHeader *page)
 
static void * s_FillFromFreePages (SMMBlocksPool *pool, Uint2 size_idx, SMMStat *stat)
 
static void s_ReleaseToFreePages (void **blocks, Uint2 cnt, Uint2 size_idx, SMMStat *)
 
static void s_FlushPoolSet (SMMMemPoolsSet *pool_set)
 
static void * s_FillPool (SMMBlocksPool *pool, SMMStat *stat)
 
static void * s_GetFromGlobal (Uint2 size_idx, SMMStat *stat)
 
static void s_DrainPool (SMMBlocksPool *pool, void *ptr, SMMStat *stat)
 
static void * s_GetFromPool (SMMBlocksPool *pool, SMMStat *stat)
 
static void s_PutToPool (SMMBlocksPool *pool, void *ptr, SMMStat *stat)
 
static size_t s_CalcBigPageSize (size_t size)
 
static void * s_AllocBigPage (size_t size, SMMStat *)
 
static void s_DeallocBigPage (SMMPageHeader *page, SMMStat *)
 
static Uint2 s_CalcSizeIndex (size_t size)
 
static SMMMemPoolsSets_GetCurPoolsSet (void)
 
static void * s_AllocMemory (size_t size)
 
static void s_DeallocMemory (void *ptr)
 
static void * s_ReallocMemory (void *ptr, size_t size)
 
size_t GetMemSize (void *ptr)
 
void InitMemoryMan (void)
 
void FinalizeMemoryMan (void)
 
void AssignThreadMemMgr (SSrvThread *thr)
 
void ReleaseThreadMemMgr (SSrvThread *thr)
 
static void s_PrintSizeDiff (CSrvPrintProxy &proxy, Uint2 size_idx, Int8 was_blocks, Int8 is_blocks)
 
static void s_PrintBlocksState (CSrvPrintProxy &proxy, Int8 *start_blocks, Int8 *end_blocks)
 
void * operator new (size_t size) noexcept(false)
 
void operator delete (void *ptr) throw ()
 
void * operator new[] (size_t size) noexcept(false)
 
void operator delete[] (void *ptr) throw ()
 

Variables

static const Uint2 kMMCntBlocksInPool = 100
 
static const Uint2 kMMDrainBatchSize = 35
 
static const Uint1 kMMCntFreeGrades = 8
 
static const int kMMFlushPeriod = 60
 
static const Uint4 kMMAllocPageSize = 65536
 If for some reason kMMAllocPageSize is changed then kMMMaxBlockSize will change too and thus probably kNCMaxBlobChunkSize in nc_db_info.hpp in NetCache should change correspondingly too. More...
 
static const size_t kMMAllocPageMask = ~size_t(kMMAllocPageSize - 1)
 
static const Uint2 kMMOSPageSize = 4096
 This is Linux standard on x86_64. More...
 
static const size_t kMMOSPageMask = ~size_t(kMMOSPageSize - 1)
 
static bool s_HadLowLevelInit = false
 
static bool s_HadMemMgrInit = false
 
static SMMMemPoolsSet s_GlobalPoolsSet
 
static SMMFreePageGrades s_FreePages [kMMCntBlockSizes]
 
static SMMMemPoolsSet s_MainPoolsSet
 
static CMMFlushers_Flusher
 
static Uint8 s_TotalSysMem = 0
 
static Int8 s_TotalPageCount = 0
 
static SMMStateStat s_StartState
 
Uint8 s_UserBlAlloced [kMMCntBlockSizes]
 
Uint8 s_UserBlFreed [kMMCntBlockSizes]
 
Uint8 s_SysBlAlloced [kMMCntBlockSizes]
 
Uint8 s_SysBlFreed [kMMCntBlockSizes]
 
Uint8 s_BigAllocedCnt = 0
 
Uint8 s_BigAllocedSize = 0
 
Uint8 s_BigFreedCnt = 0
 
Uint8 s_BigFreedSize = 0
 
static const Uint4 kMMPageDataSize = kMMAllocPageSize - sizeof(SMMPageHeader)
 
static const Uint2 kMMMaxBlockSize = (kMMPageDataSize / 2) & ~7
 
static const Uint2 kMMBlockSizes [kMMCntBlockSizes]
 
static Uint2 kMMSizeIndexes [kMMMaxBlockSize/8+1] = {0}
 
static Uint2 kMMCntForSize [kMMCntBlockSizes] = {0}
 

Macro Definition Documentation

◆ __NC_MEMMAN_ALLPTR_COUNT

#define __NC_MEMMAN_ALLPTR_COUNT   0

Definition at line 46 of file memory_man.cpp.

◆ __NC_MEMMAN_DEBUG

#define __NC_MEMMAN_DEBUG   0

Definition at line 45 of file memory_man.cpp.

◆ __NC_MEMMAN_PERTHREAD_STAT

#define __NC_MEMMAN_PERTHREAD_STAT   0

Definition at line 50 of file memory_man.cpp.

◆ __NC_MEMMAN_PERTHREAD_STAT_ARG

#define __NC_MEMMAN_PERTHREAD_STAT_ARG (   x)

Definition at line 55 of file memory_man.cpp.

◆ __NC_MEMMAN_USE_MALLOC_HOOK

#define __NC_MEMMAN_USE_MALLOC_HOOK   1

Definition at line 42 of file memory_man.cpp.

Function Documentation

◆ AssignThreadMemMgr()

void AssignThreadMemMgr ( SSrvThread thr)

Definition at line 1080 of file memory_man.cpp.

References abort(), s_HadLowLevelInit, s_InitPoolsSet(), s_LowLevelInit(), s_MainPoolsSet, and thr.

Referenced by s_AllocThread().

◆ FinalizeMemoryMan()

void FinalizeMemoryMan ( void  )

Definition at line 1065 of file memory_man.cpp.

References s_HadLowLevelInit.

Referenced by CTaskServer::Finalize().

◆ GetMemSize()

size_t GetMemSize ( void *  ptr)

Definition at line 1050 of file memory_man.cpp.

References SMMPageHeader::block_size, and s_GetPageByPtr().

Referenced by CSrvTask::ReleaseDiagCtx(), and CSrvTask::SetDiagCtx().

◆ GetMPageCount()

Int8 GetMPageCount ( void  )

Definition at line 254 of file memory_man.cpp.

References s_TotalPageCount.

Referenced by s_DoMmap(), and s_DoUnmap().

◆ InitMemoryMan()

void InitMemoryMan ( void  )

Definition at line 1057 of file memory_man.cpp.

References kMMFlushPeriod, CSrvTask::RunAfter(), s_Flusher, and s_HadMemMgrInit.

Referenced by CTaskServer::Initialize().

◆ operator delete()

void operator delete ( void *  ptr)
throw (
)

Definition at line 1526 of file memory_man.cpp.

References s_DeallocMemory().

◆ operator delete[]()

void operator delete[] ( void *  ptr)
throw (
)

Definition at line 1541 of file memory_man.cpp.

References s_DeallocMemory().

◆ operator new()

void* operator new ( size_t  size)
noexcept

Definition at line 1517 of file memory_man.cpp.

References s_AllocMemory(), and ncbi::grid::netcache::search::fields::size.

◆ operator new[]()

void* operator new[] ( size_t  size)
noexcept

Definition at line 1532 of file memory_man.cpp.

References s_AllocMemory(), and ncbi::grid::netcache::search::fields::size.

◆ ReleaseThreadMemMgr()

void ReleaseThreadMemMgr ( SSrvThread thr)

Definition at line 1106 of file memory_man.cpp.

References s_FlushPoolSet(), and thr.

Referenced by s_StopCurMgrThread().

◆ s_AllocBigPage()

static void* s_AllocBigPage ( size_t  size,
SMMStat  
)
static

◆ s_AllocMemory()

static void* s_AllocMemory ( size_t  size)
static

◆ s_AllocNewPage()

static SMMPageHeader* s_AllocNewPage ( Uint2  size_idx,
SMMStat  
)
static

◆ s_CalcBigPageSize()

static size_t s_CalcBigPageSize ( size_t  size)
inlinestatic

◆ s_CalcSizeIndex()

static Uint2 s_CalcSizeIndex ( size_t  size)
inlinestatic

◆ s_DeallocBigPage()

static void s_DeallocBigPage ( SMMPageHeader page,
SMMStat  
)
static

◆ s_DeallocMemory()

static void s_DeallocMemory ( void *  ptr)
static

◆ s_DoMmap()

static void* s_DoMmap ( size_t  size)
inlinestatic

◆ s_DoUnmap()

static void s_DoUnmap ( void *  ptr,
size_t  size 
)
inlinestatic

◆ s_DrainPool()

static void s_DrainPool ( SMMBlocksPool pool,
void *  ptr,
SMMStat stat 
)
static

◆ s_FillFromFreePages()

static void* s_FillFromFreePages ( SMMBlocksPool pool,
Uint2  size_idx,
SMMStat stat 
)
static

◆ s_FillFromPage()

static void* s_FillFromPage ( SMMBlocksPool pool,
Uint2  size_idx,
SMMPageHeader page 
)
static

◆ s_FillPool()

static void* s_FillPool ( SMMBlocksPool pool,
SMMStat stat 
)
static

◆ s_FlushPoolSet()

static void s_FlushPoolSet ( SMMMemPoolsSet pool_set)
static

◆ s_FreeListAddHead()

static void s_FreeListAddHead ( SMMPageHeader list_head,
SMMPageHeader page 
)
inlinestatic

◆ s_FreeListAddTail()

static void s_FreeListAddTail ( SMMPageHeader list_head,
SMMPageHeader page 
)
inlinestatic

◆ s_FreeListRemove()

static void s_FreeListRemove ( SMMPageHeader page)
inlinestatic

◆ s_GetCurPoolsSet()

static SMMMemPoolsSet* s_GetCurPoolsSet ( void  )
inlinestatic

◆ s_GetFromGlobal()

static void* s_GetFromGlobal ( Uint2  size_idx,
SMMStat stat 
)
static

◆ s_GetFromPool()

static void* s_GetFromPool ( SMMBlocksPool pool,
SMMStat stat 
)
static

◆ s_GetPageByPtr()

static SMMPageHeader* s_GetPageByPtr ( void *  ptr)
inlinestatic

Definition at line 470 of file memory_man.cpp.

References kMMAllocPageMask.

Referenced by GetMemSize(), s_DeallocMemory(), s_ReallocMemory(), and s_ReleaseToFreePages().

◆ s_IncPoolIdx()

static void s_IncPoolIdx ( Uint2 idx)
inlinestatic

Definition at line 524 of file memory_man.cpp.

References kMMCntBlocksInPool.

Referenced by s_GetFromPool(), and s_PutToPool().

◆ s_InitPoolsSet()

static void s_InitPoolsSet ( SMMMemPoolsSet pool_set)
inlinestatic

◆ s_IsFreeListEmpty()

static bool s_IsFreeListEmpty ( SMMPageHeader list_head)
inlinestatic

Definition at line 482 of file memory_man.cpp.

References SMMPageHeader::next_page.

Referenced by s_FillFromFreePages().

◆ s_IsInFreeList()

static bool s_IsInFreeList ( SMMPageHeader page)
inlinestatic

Definition at line 476 of file memory_man.cpp.

References SMMPageHeader::next_page, and NULL.

Referenced by s_RemoveFromFreeList().

◆ s_LowLevelInit()

static void s_LowLevelInit ( void  )
static

◆ s_PrintBlocksState()

static void s_PrintBlocksState ( CSrvPrintProxy proxy,
Int8 start_blocks,
Int8 end_blocks 
)
static

◆ s_PrintSizeDiff()

static void s_PrintSizeDiff ( CSrvPrintProxy proxy,
Uint2  size_idx,
Int8  was_blocks,
Int8  is_blocks 
)
static

Definition at line 1323 of file memory_man.cpp.

References g_ToSizeStr(), and kMMBlockSizes.

Referenced by s_PrintBlocksState(), and SMMStat::x_PrintUnstructured().

◆ s_PutToFreeList()

static void s_PutToFreeList ( SMMPageHeader page,
Uint2  size_idx,
bool  to_head 
)
static

◆ s_PutToPool()

static void s_PutToPool ( SMMBlocksPool pool,
void *  ptr,
SMMStat stat 
)
static

◆ s_ReallocMemory()

static void* s_ReallocMemory ( void *  ptr,
size_t  size 
)
static

◆ s_ReleaseToFreePages()

static void s_ReleaseToFreePages ( void **  blocks,
Uint2  cnt,
Uint2  size_idx,
SMMStat  
)
static

◆ s_RemoveFromFreeList()

static bool s_RemoveFromFreeList ( SMMPageHeader page,
Uint2  size_idx 
)
static

◆ s_SysAlloc()

static void* s_SysAlloc ( size_t  size)
static

◆ s_SysAllocLongWay()

static void* s_SysAllocLongWay ( size_t  ptr,
size_t  size 
)
static

◆ s_SysFree()

static void s_SysFree ( void *  ptr,
size_t  size 
)
static

◆ s_TryUnmap()

static bool s_TryUnmap ( void *  ptr,
size_t  size 
)
inlinestatic

Definition at line 352 of file memory_man.cpp.

References s_DoUnmap(), and ncbi::grid::netcache::search::fields::size.

Referenced by s_SysAllocLongWay().

Variable Documentation

◆ kMMAllocPageMask

const size_t kMMAllocPageMask = ~size_t(kMMAllocPageSize - 1)
static

Definition at line 71 of file memory_man.cpp.

Referenced by s_GetPageByPtr(), s_SysAlloc(), and s_SysAllocLongWay().

◆ kMMAllocPageSize

const Uint4 kMMAllocPageSize = 65536
static

If for some reason kMMAllocPageSize is changed then kMMMaxBlockSize will change too and thus probably kNCMaxBlobChunkSize in nc_db_info.hpp in NetCache should change correspondingly too.

Definition at line 70 of file memory_man.cpp.

Referenced by s_AllocNewPage(), s_ReleaseToFreePages(), and s_SysAllocLongWay().

◆ kMMBlockSizes

const Uint2 kMMBlockSizes[kMMCntBlockSizes]
static
Initial value:
=
{ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 96, 112, 128, 152, 176, 208, 248,
296, 352, 416, 496, 592, 704, 840, 1008, 1208, 1448, 1736, 2080, 2496,
(kMMPageDataSize / 11) & ~7,
(kMMPageDataSize / 9) & ~7,
(kMMPageDataSize / 8) & ~7,
(kMMPageDataSize / 7) & ~7,
(kMMPageDataSize / 6) & ~7,
(kMMPageDataSize / 5) & ~7,
(kMMPageDataSize / 4) & ~7,
(kMMPageDataSize / 3) & ~7,
}
static const Uint4 kMMPageDataSize
Definition: memory_man.cpp:235
static const Uint2 kMMMaxBlockSize
Definition: memory_man.cpp:236

Definition at line 237 of file memory_man.cpp.

Referenced by SMMStat::InitStartState(), s_AllocMemory(), s_AllocNewPage(), s_LowLevelInit(), s_PrintBlocksState(), s_PrintSizeDiff(), s_ReallocMemory(), SMMStat::SaveEndState(), and SMMStat::x_PrintUnstructured().

◆ kMMCntBlocksInPool

const Uint2 kMMCntBlocksInPool = 100
static

◆ kMMCntForSize

Uint2 kMMCntForSize[kMMCntBlockSizes] = {0}
static

◆ kMMCntFreeGrades

const Uint1 kMMCntFreeGrades = 8
static

Definition at line 64 of file memory_man.cpp.

Referenced by s_FillFromFreePages(), s_LowLevelInit(), and s_PutToFreeList().

◆ kMMDrainBatchSize

const Uint2 kMMDrainBatchSize = 35
static

Definition at line 63 of file memory_man.cpp.

Referenced by s_DrainPool(), s_FillFromPage(), and s_FillPool().

◆ kMMFlushPeriod

const int kMMFlushPeriod = 60
static

Definition at line 65 of file memory_man.cpp.

Referenced by CMMFlusher::ExecuteSlice(), and InitMemoryMan().

◆ kMMMaxBlockSize

const Uint2 kMMMaxBlockSize = (kMMPageDataSize / 2) & ~7
static

Definition at line 236 of file memory_man.cpp.

Referenced by s_AllocMemory(), s_DeallocMemory(), s_LowLevelInit(), and s_ReallocMemory().

◆ kMMOSPageMask

const size_t kMMOSPageMask = ~size_t(kMMOSPageSize - 1)
static

Definition at line 76 of file memory_man.cpp.

Referenced by s_CalcBigPageSize().

◆ kMMOSPageSize

const Uint2 kMMOSPageSize = 4096
static

This is Linux standard on x86_64.

If it ever changes or some portability will be desired then this constant will need to be obtained from OS during initialization.

Definition at line 75 of file memory_man.cpp.

Referenced by s_CalcBigPageSize().

◆ kMMPageDataSize

const Uint4 kMMPageDataSize = kMMAllocPageSize - sizeof(SMMPageHeader)
static

Definition at line 235 of file memory_man.cpp.

Referenced by s_LowLevelInit().

◆ kMMSizeIndexes

Uint2 kMMSizeIndexes[kMMMaxBlockSize/8+1] = {0}
static

Definition at line 250 of file memory_man.cpp.

Referenced by s_CalcSizeIndex(), and s_LowLevelInit().

◆ s_BigAllocedCnt

Uint8 s_BigAllocedCnt = 0

◆ s_BigAllocedSize

Uint8 s_BigAllocedSize = 0

◆ s_BigFreedCnt

Uint8 s_BigFreedCnt = 0

◆ s_BigFreedSize

Uint8 s_BigFreedSize = 0

◆ s_Flusher

CMMFlusher* s_Flusher
static

Definition at line 148 of file memory_man.cpp.

Referenced by InitMemoryMan().

◆ s_FreePages

SMMFreePageGrades s_FreePages[kMMCntBlockSizes]
static

◆ s_GlobalPoolsSet

SMMMemPoolsSet s_GlobalPoolsSet
static

◆ s_HadLowLevelInit

bool s_HadLowLevelInit = false
static

◆ s_HadMemMgrInit

bool s_HadMemMgrInit = false
static

Definition at line 143 of file memory_man.cpp.

Referenced by InitMemoryMan(), and s_GetCurPoolsSet().

◆ s_MainPoolsSet

SMMMemPoolsSet s_MainPoolsSet
static

◆ s_StartState

SMMStateStat s_StartState
static

Definition at line 152 of file memory_man.cpp.

Referenced by SMMStat::InitStartState().

◆ s_SysBlAlloced

Uint8 s_SysBlAlloced[kMMCntBlockSizes]

◆ s_SysBlFreed

Uint8 s_SysBlFreed[kMMCntBlockSizes]

◆ s_TotalPageCount

Int8 s_TotalPageCount = 0
static

◆ s_TotalSysMem

Uint8 s_TotalSysMem = 0
static

◆ s_UserBlAlloced

Uint8 s_UserBlAlloced[kMMCntBlockSizes]

◆ s_UserBlFreed

Uint8 s_UserBlFreed[kMMCntBlockSizes]
Modified on Sun Mar 03 03:15:29 2024 by modify_doxy.py rev. 669887