| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/sqlitemem.cc |
| Date: | 2026-11-08 02:41:18 |
| 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 | 4748 | void *SqliteMemoryManager::LookasideBufferArena::GetBuffer() { | |
| 21 |
2/2✓ Branch 0 taken 11014 times.
✓ Branch 1 taken 39 times.
|
11053 | for (unsigned i = 0; i < kNoBitmaps; ++i) { |
| 22 | 11014 | const int bit_set = ffs(freemap_[i]); | |
| 23 |
2/2✓ Branch 0 taken 4709 times.
✓ Branch 1 taken 6305 times.
|
11014 | if (bit_set != 0) { |
| 24 | 4709 | freemap_[i] &= ~(1 << (bit_set - 1)); // set bit to zero | |
| 25 | 4709 | const int nbuffer = i * sizeof(int) * 8 + bit_set - 1; | |
| 26 | 4709 | return reinterpret_cast<char *>(arena_) + nbuffer * kBufferSize; | |
| 27 | } | ||
| 28 | } | ||
| 29 | 39 | return NULL; | |
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | 1729 | bool SqliteMemoryManager::LookasideBufferArena::IsEmpty() { | |
| 34 |
2/2✓ Branch 0 taken 4381 times.
✓ Branch 1 taken 52 times.
|
4433 | for (unsigned i = 0; i < kNoBitmaps; ++i) { |
| 35 |
2/2✓ Branch 0 taken 1677 times.
✓ Branch 1 taken 2704 times.
|
4381 | if (~freemap_[i] != 0) |
| 36 | 1677 | return false; | |
| 37 | } | ||
| 38 | 52 | return true; | |
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | 4748 | bool SqliteMemoryManager::LookasideBufferArena::Contains(void *buffer) { | |
| 43 |
3/4✓ Branch 0 taken 4748 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 4735 times.
|
4748 | if ((buffer == NULL) || (buffer < arena_)) |
| 44 | 13 | return false; | |
| 45 | 4735 | return (static_cast<uint64_t>((reinterpret_cast<char *>(buffer) | |
| 46 | 4735 | - reinterpret_cast<char *>(arena_))) | |
| 47 | 4735 | < kArenaSize); | |
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | 1398 | SqliteMemoryManager::LookasideBufferArena::LookasideBufferArena() | |
| 52 | 1398 | : arena_(sxmmap(kArenaSize)) { | |
| 53 | // All buffers unused, i.e. all bits set | ||
| 54 | 1398 | memset(freemap_, 0xFF, kNoBitmaps * sizeof(int)); | |
| 55 | 1398 | } | |
| 56 | |||
| 57 | |||
| 58 | 1394 | SqliteMemoryManager::LookasideBufferArena::~LookasideBufferArena() { | |
| 59 | 1394 | sxunmap(arena_, kArenaSize); | |
| 60 | 1394 | } | |
| 61 | |||
| 62 | |||
| 63 | 4709 | void SqliteMemoryManager::LookasideBufferArena::PutBuffer(void *buffer) { | |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4709 times.
|
4709 | assert(buffer >= arena_); |
| 65 | 4709 | const ptrdiff_t nbuffer = (reinterpret_cast<char *>(buffer) | |
| 66 | 4709 | - reinterpret_cast<char *>(arena_)) | |
| 67 | / kBufferSize; | ||
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4709 times.
|
4709 | assert(static_cast<uint64_t>(nbuffer) < kBuffersPerArena); |
| 69 | 4709 | const int nfreemap = nbuffer / (sizeof(int) * 8); | |
| 70 | 4709 | freemap_[nfreemap] |= 1 << (nbuffer % (sizeof(int) * 8)); | |
| 71 | 4709 | } | |
| 72 | |||
| 73 | |||
| 74 | //------------------------------------------------------------------------------ | ||
| 75 | |||
| 76 | |||
| 77 | SqliteMemoryManager *SqliteMemoryManager::instance_ = NULL; | ||
| 78 | |||
| 79 | |||
| 80 | /** | ||
| 81 | * Sqlite ensures that size > 0. | ||
| 82 | */ | ||
| 83 | 789741 | void *SqliteMemoryManager::xMalloc(int size) { | |
| 84 | 789741 | return instance_->GetMemory(size); | |
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | /** | ||
| 89 | * Sqlite ensures that ptr != NULL. | ||
| 90 | */ | ||
| 91 | 789648 | void SqliteMemoryManager::xFree(void *ptr) { instance_->PutMemory(ptr); } | |
| 92 | |||
| 93 | |||
| 94 | /** | ||
| 95 | * Sqlite ensures that ptr != NULL and new_size > 0. | ||
| 96 | */ | ||
| 97 | 303154 | void *SqliteMemoryManager::xRealloc(void *ptr, int new_size) { | |
| 98 | 303154 | const int old_size = xSize(ptr); | |
| 99 |
2/2✓ Branch 0 taken 7756 times.
✓ Branch 1 taken 295398 times.
|
303154 | if (old_size >= new_size) |
| 100 | 7756 | return ptr; | |
| 101 | |||
| 102 | 295398 | void *new_ptr = xMalloc(new_size); | |
| 103 | 295398 | memcpy(new_ptr, ptr, old_size); | |
| 104 | 295398 | xFree(ptr); | |
| 105 | 295398 | return new_ptr; | |
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | /** | ||
| 110 | * Sqlite ensures that ptr != NULL. | ||
| 111 | */ | ||
| 112 | 1404577 | int SqliteMemoryManager::xSize(void *ptr) { | |
| 113 | 1404577 | return instance_->GetMemorySize(ptr); | |
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | 537484 | int SqliteMemoryManager::xRoundup(int size) { return RoundUp8(size); } | |
| 118 | |||
| 119 | |||
| 120 | 1307 | int SqliteMemoryManager::xInit(void *app_data __attribute__((unused))) { | |
| 121 | 1307 | return SQLITE_OK; | |
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | 1303 | void SqliteMemoryManager::xShutdown(void *app_data __attribute__((unused))) { } | |
| 126 | |||
| 127 | |||
| 128 | 1307 | void SqliteMemoryManager::AssignGlobalArenas() { | |
| 129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1307 times.
|
1307 | if (assigned_) |
| 130 | ✗ | return; | |
| 131 | int retval; | ||
| 132 | |||
| 133 | 1307 | retval = sqlite3_config(SQLITE_CONFIG_PAGECACHE, page_cache_memory_, | |
| 134 | kPageCacheSlotSize, kPageCacheNoSlots); | ||
| 135 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1307 times.
|
1307 | assert(retval == SQLITE_OK); |
| 136 | |||
| 137 | 1307 | retval = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &sqlite3_mem_vanilla_); | |
| 138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1307 times.
|
1307 | assert(retval == SQLITE_OK); |
| 139 | 1307 | retval = sqlite3_config(SQLITE_CONFIG_MALLOC, &mem_methods_); | |
| 140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1307 times.
|
1307 | assert(retval == SQLITE_OK); |
| 141 | |||
| 142 | 1307 | 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 | 523 | void *SqliteMemoryManager::AssignLookasideBuffer(sqlite3 *db) { | |
| 151 | 523 | const MutexLockGuard lock_guard(lock_); | |
| 152 | |||
| 153 |
1/2✓ Branch 1 taken 523 times.
✗ Branch 2 not taken.
|
523 | void *buffer = GetLookasideBuffer(); |
| 154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 523 times.
|
523 | assert(buffer != NULL); |
| 155 |
1/2✓ Branch 1 taken 523 times.
✗ Branch 2 not taken.
|
523 | 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 523 times.
|
523 | assert(retval == SQLITE_OK); |
| 159 | 523 | return buffer; | |
| 160 | 523 | } | |
| 161 | |||
| 162 | |||
| 163 | 1368 | void SqliteMemoryManager::CleanupInstance() { | |
| 164 |
1/2✓ Branch 0 taken 1368 times.
✗ Branch 1 not taken.
|
1368 | delete instance_; |
| 165 | 1368 | instance_ = NULL; | |
| 166 | 1368 | } | |
| 167 | |||
| 168 | |||
| 169 | /** | ||
| 170 | * Opens a new arena if necessary. | ||
| 171 | */ | ||
| 172 | 2200 | void *SqliteMemoryManager::GetLookasideBuffer() { | |
| 173 | void *result; | ||
| 174 | vector<LookasideBufferArena *>::reverse_iterator | ||
| 175 | 2200 | reverse_iter = lookaside_buffer_arenas_.rbegin(); | |
| 176 | const vector<LookasideBufferArena *>::reverse_iterator | ||
| 177 | 2200 | i_rend = lookaside_buffer_arenas_.rend(); | |
| 178 |
3/4✓ Branch 2 taken 2213 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2200 times.
✓ Branch 5 taken 13 times.
|
2213 | for (; reverse_iter != i_rend; ++reverse_iter) { |
| 179 | 2200 | result = (*reverse_iter)->GetBuffer(); | |
| 180 |
2/2✓ Branch 0 taken 2187 times.
✓ Branch 1 taken 13 times.
|
2200 | if (result != NULL) |
| 181 | 2187 | return result; | |
| 182 | } | ||
| 183 | |||
| 184 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | LookasideBufferArena *new_arena = new LookasideBufferArena(); |
| 185 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | lookaside_buffer_arenas_.push_back(new_arena); |
| 186 | 13 | return new_arena->GetBuffer(); | |
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | 1404629 | int SqliteMemoryManager::GetMemorySize(void *ptr) { | |
| 191 | 1404629 | return MallocArena::GetMallocArena(ptr, kArenaSize)->GetSize(ptr); | |
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | /** | ||
| 196 | * Opens new arenas as necessary. | ||
| 197 | */ | ||
| 198 | 1049819 | void *SqliteMemoryManager::GetMemory(int size) { | |
| 199 |
1/2✓ Branch 2 taken 1049819 times.
✗ Branch 3 not taken.
|
1049819 | void *p = malloc_arenas_[idx_last_arena_]->Malloc(size); |
| 200 |
2/2✓ Branch 0 taken 1049806 times.
✓ Branch 1 taken 13 times.
|
1049819 | if (p != NULL) |
| 201 | 1049806 | return p; | |
| 202 | 13 | const unsigned N = malloc_arenas_.size(); | |
| 203 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
|
26 | for (unsigned i = 0; i < N; ++i) { |
| 204 |
1/2✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 | p = malloc_arenas_[i]->Malloc(size); |
| 205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (p != NULL) { |
| 206 | ✗ | idx_last_arena_ = i; | |
| 207 | ✗ | return p; | |
| 208 | } | ||
| 209 | } | ||
| 210 | 13 | idx_last_arena_ = N; | |
| 211 |
2/4✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | MallocArena *M = new MallocArena(kArenaSize); |
| 212 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | malloc_arenas_.push_back(M); |
| 213 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | p = M->Malloc(size); |
| 214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | assert(p != NULL); |
| 215 | 13 | return p; | |
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | 1372 | SqliteMemoryManager::SqliteMemoryManager() | |
| 220 | 1372 | : assigned_(false) | |
| 221 | 1372 | , page_cache_memory_(sxmmap(kPageCacheSize)) | |
| 222 | 1372 | , idx_last_arena_(0) { | |
| 223 | 1372 | memset(&sqlite3_mem_vanilla_, 0, sizeof(sqlite3_mem_vanilla_)); | |
| 224 | 1372 | const int retval = pthread_mutex_init(&lock_, NULL); | |
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1372 times.
|
1372 | assert(retval == 0); |
| 226 | |||
| 227 |
2/4✓ Branch 1 taken 1372 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1372 times.
✗ Branch 6 not taken.
|
1372 | lookaside_buffer_arenas_.push_back(new LookasideBufferArena()); |
| 228 |
3/6✓ Branch 1 taken 1372 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1372 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1372 times.
✗ Branch 8 not taken.
|
1372 | malloc_arenas_.push_back(new MallocArena(kArenaSize)); |
| 229 | |||
| 230 | 1372 | memset(&mem_methods_, 0, sizeof(mem_methods_)); | |
| 231 | 1372 | mem_methods_.xMalloc = xMalloc; | |
| 232 | 1372 | mem_methods_.xFree = xFree; | |
| 233 | 1372 | mem_methods_.xRealloc = xRealloc; | |
| 234 | 1372 | mem_methods_.xSize = xSize; | |
| 235 | 1372 | mem_methods_.xRoundup = xRoundup; | |
| 236 | 1372 | mem_methods_.xInit = xInit; | |
| 237 | 1372 | mem_methods_.xShutdown = xShutdown; | |
| 238 | 1372 | mem_methods_.pAppData = NULL; | |
| 239 | 1372 | } | |
| 240 | |||
| 241 | |||
| 242 | /** | ||
| 243 | * Must be executed only after sqlite3_shutdown. | ||
| 244 | */ | ||
| 245 | 1368 | SqliteMemoryManager::~SqliteMemoryManager() { | |
| 246 |
2/2✓ Branch 0 taken 1303 times.
✓ Branch 1 taken 65 times.
|
1368 | if (assigned_) { |
| 247 | // Reset sqlite to default values | ||
| 248 | int retval; | ||
| 249 | 1303 | retval = sqlite3_config(SQLITE_CONFIG_PAGECACHE, NULL, 0, 0); | |
| 250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1303 times.
|
1303 | assert(retval == SQLITE_OK); |
| 251 | 1303 | retval = sqlite3_config(SQLITE_CONFIG_MALLOC, &sqlite3_mem_vanilla_); | |
| 252 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1303 times.
|
1303 | assert(retval == SQLITE_OK); |
| 253 | } | ||
| 254 | |||
| 255 | 1368 | sxunmap(page_cache_memory_, kPageCacheSize); | |
| 256 |
2/2✓ Branch 1 taken 1368 times.
✓ Branch 2 taken 1368 times.
|
2736 | for (unsigned i = 0; i < lookaside_buffer_arenas_.size(); ++i) |
| 257 |
1/2✓ Branch 1 taken 1368 times.
✗ Branch 2 not taken.
|
1368 | delete lookaside_buffer_arenas_[i]; |
| 258 |
2/2✓ Branch 1 taken 1368 times.
✓ Branch 2 taken 1368 times.
|
2736 | for (unsigned i = 0; i < malloc_arenas_.size(); ++i) |
| 259 |
1/2✓ Branch 1 taken 1368 times.
✗ Branch 2 not taken.
|
1368 | delete malloc_arenas_[i]; |
| 260 | 1368 | pthread_mutex_destroy(&lock_); | |
| 261 | 1368 | } | |
| 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 | 2200 | void SqliteMemoryManager::PutLookasideBuffer(void *buffer) { | |
| 270 | 2200 | const unsigned N = lookaside_buffer_arenas_.size(); | |
| 271 |
1/2✓ Branch 0 taken 2200 times.
✗ Branch 1 not taken.
|
2200 | for (unsigned i = 0; i < N; ++i) { |
| 272 |
1/2✓ Branch 2 taken 2200 times.
✗ Branch 3 not taken.
|
2200 | if (lookaside_buffer_arenas_[i]->Contains(buffer)) { |
| 273 | 2200 | lookaside_buffer_arenas_[i]->PutBuffer(buffer); | |
| 274 |
6/6✓ Branch 0 taken 1664 times.
✓ Branch 1 taken 536 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 1651 times.
✓ Branch 6 taken 13 times.
✓ Branch 7 taken 2187 times.
|
2200 | if ((N > 1) && lookaside_buffer_arenas_[i]->IsEmpty()) { |
| 275 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | delete lookaside_buffer_arenas_[i]; |
| 276 |
1/2✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | lookaside_buffer_arenas_.erase(lookaside_buffer_arenas_.begin() + i); |
| 277 | } | ||
| 278 | 2200 | return; | |
| 279 | } | ||
| 280 | } | ||
| 281 | ✗ | PANIC(NULL); | |
| 282 | } | ||
| 283 | |||
| 284 | |||
| 285 | /** | ||
| 286 | * Closes empty areas. | ||
| 287 | */ | ||
| 288 | 789700 | void SqliteMemoryManager::PutMemory(void *ptr) { | |
| 289 | 789700 | MallocArena *M = MallocArena::GetMallocArena(ptr, kArenaSize); | |
| 290 | 789700 | M->Free(ptr); | |
| 291 | 789700 | const unsigned N = malloc_arenas_.size(); | |
| 292 |
5/6✓ Branch 0 taken 13 times.
✓ Branch 1 taken 789687 times.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 789687 times.
|
789700 | if ((N > 1) && M->IsEmpty()) { |
| 293 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | for (unsigned i = 0; i < N; ++i) { |
| 294 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 13 times.
|
26 | if (malloc_arenas_[i] == M) { |
| 295 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | delete malloc_arenas_[i]; |
| 296 |
1/2✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | malloc_arenas_.erase(malloc_arenas_.begin() + i); |
| 297 | 13 | idx_last_arena_ = 0; | |
| 298 | 13 | return; | |
| 299 | } | ||
| 300 | } | ||
| 301 | ✗ | PANIC(NULL); | |
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | |||
| 306 | /** | ||
| 307 | * To be used after an sqlite database has been closed. | ||
| 308 | */ | ||
| 309 | 523 | void SqliteMemoryManager::ReleaseLookasideBuffer(void *buffer) { | |
| 310 | 523 | const MutexLockGuard lock_guard(lock_); | |
| 311 |
1/2✓ Branch 1 taken 523 times.
✗ Branch 2 not taken.
|
523 | PutLookasideBuffer(buffer); |
| 312 | 523 | } | |
| 313 |