| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/sqlitemem.cc |
| Date: | 2025-10-26 02:35:25 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 154 | 159 | 96.9% |
| Branches: | 80 | 122 | 65.6% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #define __STDC_FORMAT_MACROS | ||
| 6 | |||
| 7 | |||
| 8 | #include "sqlitemem.h" | ||
| 9 | |||
| 10 | #include <cassert> | ||
| 11 | #include <cstddef> | ||
| 12 | #include <cstring> | ||
| 13 | #include <new> | ||
| 14 | |||
| 15 | #include "malloc_arena.h" | ||
| 16 | #include "util/concurrency.h" | ||
| 17 | #include "util/exception.h" | ||
| 18 | #include "util/smalloc.h" | ||
| 19 | |||
| 20 | using namespace std; // NOLINT | ||
| 21 | |||
| 22 | |||
| 23 | 16557 | void *SqliteMemoryManager::LookasideBufferArena::GetBuffer() { | |
| 24 |
2/2✓ Branch 0 taken 41204 times.
✓ Branch 1 taken 147 times.
|
41351 | for (unsigned i = 0; i < kNoBitmaps; ++i) { |
| 25 | 41204 | const int bit_set = ffs(freemap_[i]); | |
| 26 |
2/2✓ Branch 0 taken 16410 times.
✓ Branch 1 taken 24794 times.
|
41204 | if (bit_set != 0) { |
| 27 | 16410 | freemap_[i] &= ~(1 << (bit_set - 1)); // set bit to zero | |
| 28 | 16410 | const int nbuffer = i * sizeof(int) * 8 + bit_set - 1; | |
| 29 | 16410 | return reinterpret_cast<char *>(arena_) + nbuffer * kBufferSize; | |
| 30 | } | ||
| 31 | } | ||
| 32 | 147 | return NULL; | |
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | 6517 | bool SqliteMemoryManager::LookasideBufferArena::IsEmpty() { | |
| 37 |
2/2✓ Branch 0 taken 16513 times.
✓ Branch 1 taken 196 times.
|
16709 | for (unsigned i = 0; i < kNoBitmaps; ++i) { |
| 38 |
2/2✓ Branch 0 taken 6321 times.
✓ Branch 1 taken 10192 times.
|
16513 | if (~freemap_[i] != 0) |
| 39 | 6321 | return false; | |
| 40 | } | ||
| 41 | 196 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | 16557 | bool SqliteMemoryManager::LookasideBufferArena::Contains(void *buffer) { | |
| 46 |
3/4✓ Branch 0 taken 16557 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 16508 times.
|
16557 | if ((buffer == NULL) || (buffer < arena_)) |
| 47 | 49 | return false; | |
| 48 | 16508 | return (static_cast<uint64_t>((reinterpret_cast<char *>(buffer) | |
| 49 | 16508 | - reinterpret_cast<char *>(arena_))) | |
| 50 | 16508 | < kArenaSize); | |
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | 1821 | SqliteMemoryManager::LookasideBufferArena::LookasideBufferArena() | |
| 55 | 1821 | : arena_(sxmmap(kArenaSize)) { | |
| 56 | // All buffers unused, i.e. all bits set | ||
| 57 | 1821 | memset(freemap_, 0xFF, kNoBitmaps * sizeof(int)); | |
| 58 | 1821 | } | |
| 59 | |||
| 60 | |||
| 61 | 1817 | SqliteMemoryManager::LookasideBufferArena::~LookasideBufferArena() { | |
| 62 | 1817 | sxunmap(arena_, kArenaSize); | |
| 63 | 1817 | } | |
| 64 | |||
| 65 | |||
| 66 | 16410 | void SqliteMemoryManager::LookasideBufferArena::PutBuffer(void *buffer) { | |
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16410 times.
|
16410 | assert(buffer >= arena_); |
| 68 | 16410 | const ptrdiff_t nbuffer = (reinterpret_cast<char *>(buffer) | |
| 69 | 16410 | - reinterpret_cast<char *>(arena_)) | |
| 70 | / kBufferSize; | ||
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16410 times.
|
16410 | assert(static_cast<uint64_t>(nbuffer) < kBuffersPerArena); |
| 72 | 16410 | const int nfreemap = nbuffer / (sizeof(int) * 8); | |
| 73 | 16410 | freemap_[nfreemap] |= 1 << (nbuffer % (sizeof(int) * 8)); | |
| 74 | 16410 | } | |
| 75 | |||
| 76 | |||
| 77 | //------------------------------------------------------------------------------ | ||
| 78 | |||
| 79 | |||
| 80 | SqliteMemoryManager *SqliteMemoryManager::instance_ = NULL; | ||
| 81 | |||
| 82 | |||
| 83 | /** | ||
| 84 | * Sqlite ensures that size > 0. | ||
| 85 | */ | ||
| 86 | 1582641 | void *SqliteMemoryManager::xMalloc(int size) { | |
| 87 | 1582641 | return instance_->GetMemory(size); | |
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | /** | ||
| 92 | * Sqlite ensures that ptr != NULL. | ||
| 93 | */ | ||
| 94 | 1582548 | void SqliteMemoryManager::xFree(void *ptr) { instance_->PutMemory(ptr); } | |
| 95 | |||
| 96 | |||
| 97 | /** | ||
| 98 | * Sqlite ensures that ptr != NULL and new_size > 0. | ||
| 99 | */ | ||
| 100 | 1029621 | void *SqliteMemoryManager::xRealloc(void *ptr, int new_size) { | |
| 101 | 1029621 | const int old_size = xSize(ptr); | |
| 102 |
2/2✓ Branch 0 taken 25174 times.
✓ Branch 1 taken 1004447 times.
|
1029621 | if (old_size >= new_size) |
| 103 | 25174 | return ptr; | |
| 104 | |||
| 105 | 1004447 | void *new_ptr = xMalloc(new_size); | |
| 106 | 1004447 | memcpy(new_ptr, ptr, old_size); | |
| 107 | 1004447 | xFree(ptr); | |
| 108 | 1004447 | return new_ptr; | |
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | /** | ||
| 113 | * Sqlite ensures that ptr != NULL. | ||
| 114 | */ | ||
| 115 | 2316241 | int SqliteMemoryManager::xSize(void *ptr) { | |
| 116 | 2316241 | return instance_->GetMemorySize(ptr); | |
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | 627766 | int SqliteMemoryManager::xRoundup(int size) { return RoundUp8(size); } | |
| 121 | |||
| 122 | |||
| 123 | 1478 | int SqliteMemoryManager::xInit(void *app_data __attribute__((unused))) { | |
| 124 | 1478 | return SQLITE_OK; | |
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | 1474 | void SqliteMemoryManager::xShutdown(void *app_data __attribute__((unused))) { } | |
| 129 | |||
| 130 | |||
| 131 | 1478 | void SqliteMemoryManager::AssignGlobalArenas() { | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1478 times.
|
1478 | if (assigned_) |
| 133 | ✗ | return; | |
| 134 | int retval; | ||
| 135 | |||
| 136 | 1478 | retval = sqlite3_config(SQLITE_CONFIG_PAGECACHE, page_cache_memory_, | |
| 137 | kPageCacheSlotSize, kPageCacheNoSlots); | ||
| 138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1478 times.
|
1478 | assert(retval == SQLITE_OK); |
| 139 | |||
| 140 | 1478 | retval = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &sqlite3_mem_vanilla_); | |
| 141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1478 times.
|
1478 | assert(retval == SQLITE_OK); |
| 142 | 1478 | retval = sqlite3_config(SQLITE_CONFIG_MALLOC, &mem_methods_); | |
| 143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1478 times.
|
1478 | assert(retval == SQLITE_OK); |
| 144 | |||
| 145 | 1478 | assigned_ = true; | |
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | /** | ||
| 150 | * Needs to be the first operation on an opened sqlite database. Returns the | ||
| 151 | * location of the buffer. | ||
| 152 | */ | ||
| 153 | 632 | void *SqliteMemoryManager::AssignLookasideBuffer(sqlite3 *db) { | |
| 154 | 632 | const MutexLockGuard lock_guard(lock_); | |
| 155 | |||
| 156 |
1/2✓ Branch 1 taken 632 times.
✗ Branch 2 not taken.
|
632 | void *buffer = GetLookasideBuffer(); |
| 157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
|
632 | assert(buffer != NULL); |
| 158 |
1/2✓ Branch 1 taken 632 times.
✗ Branch 2 not taken.
|
632 | const int retval = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, buffer, |
| 159 | kLookasideSlotSize, | ||
| 160 | kLookasideSlotsPerDb); | ||
| 161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
|
632 | assert(retval == SQLITE_OK); |
| 162 | 632 | return buffer; | |
| 163 | 632 | } | |
| 164 | |||
| 165 | |||
| 166 | 1719 | void SqliteMemoryManager::CleanupInstance() { | |
| 167 |
1/2✓ Branch 0 taken 1719 times.
✗ Branch 1 not taken.
|
1719 | delete instance_; |
| 168 | 1719 | instance_ = NULL; | |
| 169 | 1719 | } | |
| 170 | |||
| 171 | |||
| 172 | /** | ||
| 173 | * Opens a new arena if necessary. | ||
| 174 | */ | ||
| 175 | 6953 | void *SqliteMemoryManager::GetLookasideBuffer() { | |
| 176 | void *result; | ||
| 177 | vector<LookasideBufferArena *>::reverse_iterator | ||
| 178 | 6953 | reverse_iter = lookaside_buffer_arenas_.rbegin(); | |
| 179 | const vector<LookasideBufferArena *>::reverse_iterator | ||
| 180 | 6953 | i_rend = lookaside_buffer_arenas_.rend(); | |
| 181 |
3/4✓ Branch 2 taken 7002 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6953 times.
✓ Branch 5 taken 49 times.
|
7002 | for (; reverse_iter != i_rend; ++reverse_iter) { |
| 182 | 6953 | result = (*reverse_iter)->GetBuffer(); | |
| 183 |
2/2✓ Branch 0 taken 6904 times.
✓ Branch 1 taken 49 times.
|
6953 | if (result != NULL) |
| 184 | 6904 | return result; | |
| 185 | } | ||
| 186 | |||
| 187 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | LookasideBufferArena *new_arena = new LookasideBufferArena(); |
| 188 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | lookaside_buffer_arenas_.push_back(new_arena); |
| 189 | 49 | return new_arena->GetBuffer(); | |
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | 2316437 | int SqliteMemoryManager::GetMemorySize(void *ptr) { | |
| 194 | 2316437 | return MallocArena::GetMallocArena(ptr, kArenaSize)->GetSize(ptr); | |
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | /** | ||
| 199 | * Opens new arenas as necessary. | ||
| 200 | */ | ||
| 201 | 2562935 | void *SqliteMemoryManager::GetMemory(int size) { | |
| 202 |
1/2✓ Branch 2 taken 2562935 times.
✗ Branch 3 not taken.
|
2562935 | void *p = malloc_arenas_[idx_last_arena_]->Malloc(size); |
| 203 |
2/2✓ Branch 0 taken 2562886 times.
✓ Branch 1 taken 49 times.
|
2562935 | if (p != NULL) |
| 204 | 2562886 | return p; | |
| 205 | 49 | const unsigned N = malloc_arenas_.size(); | |
| 206 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 49 times.
|
98 | for (unsigned i = 0; i < N; ++i) { |
| 207 |
1/2✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
|
49 | p = malloc_arenas_[i]->Malloc(size); |
| 208 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (p != NULL) { |
| 209 | ✗ | idx_last_arena_ = i; | |
| 210 | ✗ | return p; | |
| 211 | } | ||
| 212 | } | ||
| 213 | 49 | idx_last_arena_ = N; | |
| 214 |
2/4✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
|
49 | MallocArena *M = new MallocArena(kArenaSize); |
| 215 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | malloc_arenas_.push_back(M); |
| 216 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | p = M->Malloc(size); |
| 217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | assert(p != NULL); |
| 218 | 49 | return p; | |
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | 1723 | SqliteMemoryManager::SqliteMemoryManager() | |
| 223 | 1723 | : assigned_(false) | |
| 224 | 1723 | , page_cache_memory_(sxmmap(kPageCacheSize)) | |
| 225 | 1723 | , idx_last_arena_(0) { | |
| 226 | 1723 | memset(&sqlite3_mem_vanilla_, 0, sizeof(sqlite3_mem_vanilla_)); | |
| 227 | 1723 | const int retval = pthread_mutex_init(&lock_, NULL); | |
| 228 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1723 times.
|
1723 | assert(retval == 0); |
| 229 | |||
| 230 |
2/4✓ Branch 1 taken 1723 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1723 times.
✗ Branch 6 not taken.
|
1723 | lookaside_buffer_arenas_.push_back(new LookasideBufferArena()); |
| 231 |
3/6✓ Branch 1 taken 1723 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1723 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1723 times.
✗ Branch 8 not taken.
|
1723 | malloc_arenas_.push_back(new MallocArena(kArenaSize)); |
| 232 | |||
| 233 | 1723 | memset(&mem_methods_, 0, sizeof(mem_methods_)); | |
| 234 | 1723 | mem_methods_.xMalloc = xMalloc; | |
| 235 | 1723 | mem_methods_.xFree = xFree; | |
| 236 | 1723 | mem_methods_.xRealloc = xRealloc; | |
| 237 | 1723 | mem_methods_.xSize = xSize; | |
| 238 | 1723 | mem_methods_.xRoundup = xRoundup; | |
| 239 | 1723 | mem_methods_.xInit = xInit; | |
| 240 | 1723 | mem_methods_.xShutdown = xShutdown; | |
| 241 | 1723 | mem_methods_.pAppData = NULL; | |
| 242 | 1723 | } | |
| 243 | |||
| 244 | |||
| 245 | /** | ||
| 246 | * Must be executed only after sqlite3_shutdown. | ||
| 247 | */ | ||
| 248 | 1719 | SqliteMemoryManager::~SqliteMemoryManager() { | |
| 249 |
2/2✓ Branch 0 taken 1474 times.
✓ Branch 1 taken 245 times.
|
1719 | if (assigned_) { |
| 250 | // Reset sqlite to default values | ||
| 251 | int retval; | ||
| 252 | 1474 | retval = sqlite3_config(SQLITE_CONFIG_PAGECACHE, NULL, 0, 0); | |
| 253 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1474 times.
|
1474 | assert(retval == SQLITE_OK); |
| 254 | 1474 | retval = sqlite3_config(SQLITE_CONFIG_MALLOC, &sqlite3_mem_vanilla_); | |
| 255 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1474 times.
|
1474 | assert(retval == SQLITE_OK); |
| 256 | } | ||
| 257 | |||
| 258 | 1719 | sxunmap(page_cache_memory_, kPageCacheSize); | |
| 259 |
2/2✓ Branch 1 taken 1719 times.
✓ Branch 2 taken 1719 times.
|
3438 | for (unsigned i = 0; i < lookaside_buffer_arenas_.size(); ++i) |
| 260 |
1/2✓ Branch 1 taken 1719 times.
✗ Branch 2 not taken.
|
1719 | delete lookaside_buffer_arenas_[i]; |
| 261 |
2/2✓ Branch 1 taken 1719 times.
✓ Branch 2 taken 1719 times.
|
3438 | for (unsigned i = 0; i < malloc_arenas_.size(); ++i) |
| 262 |
1/2✓ Branch 1 taken 1719 times.
✗ Branch 2 not taken.
|
1719 | delete malloc_arenas_[i]; |
| 263 | 1719 | pthread_mutex_destroy(&lock_); | |
| 264 | 1719 | } | |
| 265 | |||
| 266 | |||
| 267 | /** | ||
| 268 | * Only entirely empty arenas are freed to the system. In cvmfs, catalogs | ||
| 269 | * are gradually opened and sometimes close altogether when a new root catalog | ||
| 270 | * arrives. Hence there is no fragmentation. | ||
| 271 | */ | ||
| 272 | 6953 | void SqliteMemoryManager::PutLookasideBuffer(void *buffer) { | |
| 273 | 6953 | const unsigned N = lookaside_buffer_arenas_.size(); | |
| 274 |
1/2✓ Branch 0 taken 6953 times.
✗ Branch 1 not taken.
|
6953 | for (unsigned i = 0; i < N; ++i) { |
| 275 |
1/2✓ Branch 2 taken 6953 times.
✗ Branch 3 not taken.
|
6953 | if (lookaside_buffer_arenas_[i]->Contains(buffer)) { |
| 276 | 6953 | lookaside_buffer_arenas_[i]->PutBuffer(buffer); | |
| 277 |
6/6✓ Branch 0 taken 6272 times.
✓ Branch 1 taken 681 times.
✓ Branch 4 taken 49 times.
✓ Branch 5 taken 6223 times.
✓ Branch 6 taken 49 times.
✓ Branch 7 taken 6904 times.
|
6953 | if ((N > 1) && lookaside_buffer_arenas_[i]->IsEmpty()) { |
| 278 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | delete lookaside_buffer_arenas_[i]; |
| 279 |
1/2✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
|
49 | lookaside_buffer_arenas_.erase(lookaside_buffer_arenas_.begin() + i); |
| 280 | } | ||
| 281 | 6953 | return; | |
| 282 | } | ||
| 283 | } | ||
| 284 | ✗ | PANIC(NULL); | |
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | /** | ||
| 289 | * Closes empty areas. | ||
| 290 | */ | ||
| 291 | 1582744 | void SqliteMemoryManager::PutMemory(void *ptr) { | |
| 292 | 1582744 | MallocArena *M = MallocArena::GetMallocArena(ptr, kArenaSize); | |
| 293 | 1582744 | M->Free(ptr); | |
| 294 | 1582744 | const unsigned N = malloc_arenas_.size(); | |
| 295 |
5/6✓ Branch 0 taken 49 times.
✓ Branch 1 taken 1582695 times.
✓ Branch 3 taken 49 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 49 times.
✓ Branch 6 taken 1582695 times.
|
1582744 | if ((N > 1) && M->IsEmpty()) { |
| 296 |
1/2✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
|
98 | for (unsigned i = 0; i < N; ++i) { |
| 297 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 49 times.
|
98 | if (malloc_arenas_[i] == M) { |
| 298 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | delete malloc_arenas_[i]; |
| 299 |
1/2✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
|
49 | malloc_arenas_.erase(malloc_arenas_.begin() + i); |
| 300 | 49 | idx_last_arena_ = 0; | |
| 301 | 49 | return; | |
| 302 | } | ||
| 303 | } | ||
| 304 | ✗ | PANIC(NULL); | |
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | |||
| 309 | /** | ||
| 310 | * To be used after an sqlite database has been closed. | ||
| 311 | */ | ||
| 312 | 632 | void SqliteMemoryManager::ReleaseLookasideBuffer(void *buffer) { | |
| 313 | 632 | const MutexLockGuard lock_guard(lock_); | |
| 314 |
1/2✓ Branch 1 taken 632 times.
✗ Branch 2 not taken.
|
632 | PutLookasideBuffer(buffer); |
| 315 | 632 | } | |
| 316 |