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