![]() |
CernVM-FS
2.12.0
|
#include <sqlitemem.h>
Classes | |
class | LookasideBufferArena |
Public Member Functions | |
~SqliteMemoryManager () | |
void | AssignGlobalArenas () |
void * | AssignLookasideBuffer (sqlite3 *db) |
void | ReleaseLookasideBuffer (void *buffer) |
Static Public Member Functions | |
static SqliteMemoryManager * | GetInstance () |
static void | CleanupInstance () |
static bool | HasInstance () |
Static Public Attributes | |
static const unsigned | kScratchSlotSize = 8192 |
static const unsigned | kScratchNoSlots = 8 |
static const unsigned | kScratchSize = kScratchSlotSize * kScratchNoSlots |
static const unsigned | kPageCacheSlotSize = 1300 |
static const unsigned | kPageCacheNoSlots = 4000 |
static const unsigned | kPageCacheSize = kPageCacheSlotSize * kPageCacheNoSlots |
static const unsigned | kLookasideSlotSize = 32 |
static const unsigned | kLookasideSlotsPerDb = 128 |
Private Member Functions | |
FRIEND_TEST (T_Sqlitemem, LookasideBuffer) | |
FRIEND_TEST (T_Sqlitemem, Malloc) | |
FRIEND_TEST (T_Sqlitemem, Realloc) | |
FRIEND_TEST (T_Sqlitemem, ReallocStress) | |
SqliteMemoryManager () | |
void * | GetLookasideBuffer () |
void | PutLookasideBuffer (void *buffer) |
void * | GetMemory (int size) |
void | PutMemory (void *ptr) |
int | GetMemorySize (void *ptr) |
Static Private Member Functions | |
static void * | xMalloc (int size) |
static void | xFree (void *ptr) |
static void * | xRealloc (void *ptr, int new_size) |
static int | xSize (void *ptr) |
static int | xRoundup (int size) |
static int | xInit (void *app_data) |
static void | xShutdown (void *app_data) |
Private Attributes | |
pthread_mutex_t | lock_ |
bool | assigned_ |
struct sqlite3_mem_methods | sqlite3_mem_vanilla_ |
struct sqlite3_mem_methods | mem_methods_ |
void * | page_cache_memory_ |
std::vector < LookasideBufferArena * > | lookaside_buffer_arenas_ |
std::vector< MallocArena * > | malloc_arenas_ |
unsigned | idx_last_arena_ |
Static Private Attributes | |
static const unsigned | kArenaSize = 8 * 1024 * 1024 |
static SqliteMemoryManager * | instance_ = NULL |
The MemoryManager uses the sqlite hooks to optimize memory allocations. It is tuned for reading the cvmfs file catalogs. It provides a page cache of a few MB and per-database lookaside buffers. It also contains a "general purpose" malloc/free implementation tailored to the behavior of sqlite memory allocation.
It is implemented as a singleton. GetInstance() will reserve memory blocks, AssignGlobalArenas will set the global sqlite configuration and CleanupInstance frees the memory blocks. These three routines are not thread-safe. AssignGlobalArenas has to be called as first sqlite operation, CleanupInstance has to be called after sqlite3_shutdown. If the singleton is alive, read-only sqlite databases will automatically use it for their lookaside buffers (see sql.h). Assignment of lookaside buffers is thread-safe.
Definition at line 38 of file sqlitemem.h.
SqliteMemoryManager::~SqliteMemoryManager | ( | ) |
Must be executed only after sqlite3_shutdown.
Definition at line 253 of file sqlitemem.cc.
|
private |
Definition at line 226 of file sqlitemem.cc.
Referenced by GetInstance().
void SqliteMemoryManager::AssignGlobalArenas | ( | ) |
Definition at line 137 of file sqlitemem.cc.
Referenced by FileSystem::SetupSqlite().
void * SqliteMemoryManager::AssignLookasideBuffer | ( | sqlite3 * | db | ) |
Needs to be the first operation on an opened sqlite database. Returns the location of the buffer.
Definition at line 158 of file sqlitemem.cc.
Referenced by sqlite::Database< DerivedT >::Configure().
|
static |
Definition at line 170 of file sqlitemem.cc.
Referenced by FileSystem::~FileSystem().
|
private |
|
private |
|
private |
|
private |
|
inlinestatic |
Definition at line 137 of file sqlitemem.h.
Referenced by sqlite::Database< DerivedT >::DatabaseRaiiWrapper::Close(), sqlite::Database< DerivedT >::Configure(), and FileSystem::SetupSqlite().
|
private |
Opens a new arena if necessary.
Definition at line 179 of file sqlitemem.cc.
Referenced by AssignLookasideBuffer().
|
private |
Opens new arenas as necessary.
Definition at line 205 of file sqlitemem.cc.
Referenced by xMalloc().
|
private |
Definition at line 197 of file sqlitemem.cc.
Referenced by xSize().
|
inlinestatic |
Definition at line 143 of file sqlitemem.h.
Referenced by sqlite::Database< DerivedT >::Configure().
|
private |
Only entirely empty arenas are freed to the system. In cvmfs, catalogs are gradually opened and sometimes close altogether when a new root catalog arrives. Hence there is no fragmentation.
Definition at line 277 of file sqlitemem.cc.
Referenced by ReleaseLookasideBuffer().
|
private |
Closes empty areas.
Definition at line 296 of file sqlitemem.cc.
Referenced by xFree().
void SqliteMemoryManager::ReleaseLookasideBuffer | ( | void * | buffer | ) |
To be used after an sqlite database has been closed.
Definition at line 317 of file sqlitemem.cc.
Referenced by sqlite::Database< DerivedT >::DatabaseRaiiWrapper::Close().
|
staticprivate |
Sqlite ensures that ptr != NULL.
Definition at line 95 of file sqlitemem.cc.
Referenced by SqliteMemoryManager(), and xRealloc().
|
staticprivate |
Definition at line 128 of file sqlitemem.cc.
Referenced by SqliteMemoryManager().
|
staticprivate |
SQlite memory callbacks need to be static because they are referenced by C function pointers. See https://www.sqlite.org/c3ref/mem_methods.html
Sqlite ensures that size > 0.
Definition at line 87 of file sqlitemem.cc.
Referenced by SqliteMemoryManager(), and xRealloc().
|
staticprivate |
Sqlite ensures that ptr != NULL and new_size > 0.
Definition at line 103 of file sqlitemem.cc.
Referenced by SqliteMemoryManager().
|
staticprivate |
Definition at line 123 of file sqlitemem.cc.
Referenced by SqliteMemoryManager().
|
staticprivate |
Definition at line 133 of file sqlitemem.cc.
Referenced by SqliteMemoryManager().
|
staticprivate |
Sqlite ensures that ptr != NULL.
Definition at line 118 of file sqlitemem.cc.
Referenced by SqliteMemoryManager(), and xRealloc().
|
private |
True if AssignGlobalArenas was called and the memory manager is used by sqlite.
Definition at line 188 of file sqlitemem.h.
Referenced by AssignGlobalArenas(), and ~SqliteMemoryManager().
|
private |
Where the last successful allocation took place.
Definition at line 203 of file sqlitemem.h.
Referenced by GetMemory(), and PutMemory().
|
staticprivate |
Definition at line 158 of file sqlitemem.h.
Referenced by CleanupInstance(), GetInstance(), HasInstance(), xFree(), xMalloc(), and xSize().
|
staticprivate |
Should be larger than 10 times the largest allocation, which for reading sqlite file catalogs is 64kB. An arena size of 8MB limits the total number of arenas (mapped blocks) to <40, given typical storage needs for 10,000 open catalogs. Has to be a power of 2MB (see MallocArena).
Definition at line 157 of file sqlitemem.h.
Referenced by GetMemory(), GetMemorySize(), PutMemory(), SqliteMemoryManager(), and SqliteMemoryManager::LookasideBufferArena::~LookasideBufferArena().
|
static |
32 bytes per slot is an empirically good value so that memory is not wasted (too much) and many allocations fit within the boundary.
Definition at line 79 of file sqlitemem.h.
Referenced by AssignLookasideBuffer().
|
static |
128 slots with 32bytes accumulate to 4kB. See LookasideBufferArena below.
Definition at line 83 of file sqlitemem.h.
Referenced by AssignLookasideBuffer().
|
static |
Number of pages that can be cached.
Definition at line 69 of file sqlitemem.h.
Referenced by AssignGlobalArenas().
|
static |
|
static |
Empricially, the largest page cache allocation is 1296B.
Definition at line 65 of file sqlitemem.h.
Referenced by AssignGlobalArenas().
|
static |
Sqlite ensures that never more slots than 2 times the number of threads are used. In practice, it's much lower.
Definition at line 56 of file sqlitemem.h.
|
static |
In total: 64kB scratch memory
Definition at line 60 of file sqlitemem.h.
|
static |
In practice, we hardly ever see scratch memory used. If it is used, the allocation size is <8kB. NOTE: The scratch memory allocator has been removed from sqlite as of version 3.21.
Definition at line 51 of file sqlitemem.h.
|
private |
Definition at line 182 of file sqlitemem.h.
Referenced by AssignLookasideBuffer(), ReleaseLookasideBuffer(), SqliteMemoryManager(), and ~SqliteMemoryManager().
|
private |
Definition at line 198 of file sqlitemem.h.
Referenced by GetLookasideBuffer(), PutLookasideBuffer(), SqliteMemoryManager(), and ~SqliteMemoryManager().
|
private |
Definition at line 199 of file sqlitemem.h.
Referenced by GetMemory(), PutMemory(), SqliteMemoryManager(), and ~SqliteMemoryManager().
|
private |
Definition at line 196 of file sqlitemem.h.
Referenced by AssignGlobalArenas(), and SqliteMemoryManager().
|
private |
Definition at line 197 of file sqlitemem.h.
Referenced by AssignGlobalArenas(), and ~SqliteMemoryManager().
|
private |
The standard memory allocator used by sqlite3. Saved in order to reset on destruction.
Definition at line 194 of file sqlitemem.h.
Referenced by AssignGlobalArenas(), SqliteMemoryManager(), and ~SqliteMemoryManager().