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