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