| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/cache_ram.cc |
| Date: | 2025-11-02 02:35:35 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 191 | 229 | 83.4% |
| Branches: | 100 | 208 | 48.1% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "cache_ram.h" | ||
| 6 | |||
| 7 | #include <errno.h> | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cassert> | ||
| 11 | #include <cstring> | ||
| 12 | #include <new> | ||
| 13 | |||
| 14 | #include "kvstore.h" | ||
| 15 | #include "util/concurrency.h" | ||
| 16 | #include "util/logging.h" | ||
| 17 | #include "util/posix.h" | ||
| 18 | #include "util/string.h" | ||
| 19 | |||
| 20 | using namespace std; // NOLINT | ||
| 21 | |||
| 22 | const shash::Any RamCacheManager::kInvalidHandle; | ||
| 23 | |||
| 24 | ✗ | string RamCacheManager::Describe() { | |
| 25 | return "Internal in-memory cache manager (size " | ||
| 26 | ✗ | + StringifyInt(max_size_ / (1024 * 1024)) + "MB)\n"; | |
| 27 | } | ||
| 28 | |||
| 29 | |||
| 30 | 992 | RamCacheManager::RamCacheManager(uint64_t max_size, | |
| 31 | unsigned max_entries, | ||
| 32 | MemoryKvStore::MemoryAllocator alloc, | ||
| 33 | 992 | perf::StatisticsTemplate statistics) | |
| 34 | 992 | : max_size_(max_size) | |
| 35 |
1/2✓ Branch 1 taken 992 times.
✗ Branch 2 not taken.
|
992 | , fd_table_(max_entries, ReadOnlyHandle()) |
| 36 | // TODO(jblomer): the number of slots in the kv-stores should _not_ be the | ||
| 37 | // number of open files. | ||
| 38 |
1/2✓ Branch 1 taken 992 times.
✗ Branch 2 not taken.
|
992 | , regular_entries_(max_entries, |
| 39 | alloc, | ||
| 40 | max_size, | ||
| 41 |
2/4✓ Branch 2 taken 992 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 992 times.
✗ Branch 6 not taken.
|
1984 | perf::StatisticsTemplate("kv.regular", statistics)) |
| 42 |
1/2✓ Branch 1 taken 992 times.
✗ Branch 2 not taken.
|
992 | , volatile_entries_(max_entries, |
| 43 | alloc, | ||
| 44 | max_size, | ||
| 45 |
2/4✓ Branch 2 taken 992 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 992 times.
✗ Branch 6 not taken.
|
1984 | perf::StatisticsTemplate("kv.volatile", statistics)) |
| 46 |
2/4✓ Branch 3 taken 992 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 992 times.
✗ Branch 7 not taken.
|
1984 | , counters_(statistics) { |
| 47 | 992 | const int retval = pthread_rwlock_init(&rwlock_, NULL); | |
| 48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 992 times.
|
992 | assert(retval == 0); |
| 49 |
1/2✓ Branch 1 taken 992 times.
✗ Branch 2 not taken.
|
992 | LogCvmfs(kLogCache, kLogDebug, "max %lu B, %u entries", max_size, |
| 50 | max_entries); | ||
| 51 |
1/2✓ Branch 1 taken 992 times.
✗ Branch 2 not taken.
|
992 | LogCvmfs(kLogCache, kLogDebug | kLogSyslogWarn, |
| 52 | "DEPRECATION WARNING: The RAM cache manager is depcreated and " | ||
| 53 | "will be removed from future releases."); | ||
| 54 | 992 | } | |
| 55 | |||
| 56 | |||
| 57 | 3008 | RamCacheManager::~RamCacheManager() { pthread_rwlock_destroy(&rwlock_); } | |
| 58 | |||
| 59 | |||
| 60 | 441636 | int RamCacheManager::AddFd(const ReadOnlyHandle &handle) { | |
| 61 | 441636 | const int result = fd_table_.OpenFd(handle); | |
| 62 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 441596 times.
|
441636 | if (result == -ENFILE) { |
| 63 | 40 | LogCvmfs(kLogCache, kLogDebug, "too many open files"); | |
| 64 | 40 | perf::Inc(counters_.n_enfile); | |
| 65 | } | ||
| 66 | 441636 | return result; | |
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | 288 | bool RamCacheManager::AcquireQuotaManager(QuotaManager *quota_mgr) { | |
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
|
288 | assert(quota_mgr != NULL); |
| 72 | 288 | quota_mgr_ = quota_mgr; | |
| 73 | 288 | LogCvmfs(kLogCache, kLogDebug, "set quota manager"); | |
| 74 | 288 | return true; | |
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | 728 | int RamCacheManager::Open(const LabeledObject &object) { | |
| 79 | 728 | const WriteLockGuard guard(rwlock_); | |
| 80 |
1/2✓ Branch 1 taken 728 times.
✗ Branch 2 not taken.
|
1456 | return DoOpen(object.id); |
| 81 | 728 | } | |
| 82 | |||
| 83 | |||
| 84 | 1076 | int RamCacheManager::DoOpen(const shash::Any &id) { | |
| 85 | bool ok; | ||
| 86 | bool is_volatile; | ||
| 87 |
1/2✓ Branch 1 taken 1076 times.
✗ Branch 2 not taken.
|
1076 | const MemoryBuffer buf; |
| 88 | |||
| 89 |
3/4✓ Branch 1 taken 1076 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 580 times.
✓ Branch 4 taken 496 times.
|
1076 | if (regular_entries_.Contains(id)) { |
| 90 | 580 | is_volatile = false; | |
| 91 |
3/4✓ Branch 1 taken 496 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 440 times.
|
496 | } else if (volatile_entries_.Contains(id)) { |
| 92 | 56 | is_volatile = true; | |
| 93 | } else { | ||
| 94 |
2/4✓ Branch 1 taken 440 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 440 times.
✗ Branch 6 not taken.
|
440 | LogCvmfs(kLogCache, kLogDebug, "miss for %s", id.ToString().c_str()); |
| 95 | 440 | perf::Inc(counters_.n_openmiss); | |
| 96 | 440 | return -ENOENT; | |
| 97 | } | ||
| 98 | 636 | const ReadOnlyHandle generic_handle(id, is_volatile); | |
| 99 |
1/2✓ Branch 1 taken 636 times.
✗ Branch 2 not taken.
|
636 | const int fd = AddFd(generic_handle); |
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 636 times.
|
636 | if (fd < 0) { |
| 101 | ✗ | LogCvmfs(kLogCache, kLogDebug, "error while opening %s: %s", | |
| 102 | ✗ | id.ToString().c_str(), strerror(-fd)); | |
| 103 | ✗ | return fd; | |
| 104 | } | ||
| 105 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 580 times.
|
636 | if (is_volatile) { |
| 106 |
1/2✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
|
56 | LogCvmfs(kLogCache, kLogDebug, "hit in volatile entries for %s", |
| 107 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
112 | id.ToString().c_str()); |
| 108 | 56 | perf::Inc(counters_.n_openvolatile); | |
| 109 | } else { | ||
| 110 |
1/2✓ Branch 2 taken 580 times.
✗ Branch 3 not taken.
|
580 | LogCvmfs(kLogCache, kLogDebug, "hit in regular entries for %s", |
| 111 |
1/2✓ Branch 1 taken 580 times.
✗ Branch 2 not taken.
|
1160 | id.ToString().c_str()); |
| 112 | 580 | perf::Inc(counters_.n_openregular); | |
| 113 | } | ||
| 114 |
1/2✓ Branch 2 taken 636 times.
✗ Branch 3 not taken.
|
636 | ok = GetStore(generic_handle)->IncRef(id); |
| 115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 636 times.
|
636 | assert(ok); |
| 116 | 636 | return fd; | |
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | 204 | int64_t RamCacheManager::GetSize(int fd) { | |
| 121 | 204 | const ReadLockGuard guard(rwlock_); | |
| 122 |
1/2✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
|
204 | const ReadOnlyHandle generic_handle = fd_table_.GetHandle(fd); |
| 123 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
|
204 | if (generic_handle.handle == kInvalidHandle) { |
| 124 | ✗ | LogCvmfs(kLogCache, kLogDebug, "bad fd %d on GetSize", fd); | |
| 125 | ✗ | return -EBADF; | |
| 126 | } | ||
| 127 | 204 | perf::Inc(counters_.n_getsize); | |
| 128 |
1/2✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
|
204 | return GetStore(generic_handle)->GetSize(generic_handle.handle); |
| 129 | 204 | } | |
| 130 | |||
| 131 | |||
| 132 | 441356 | int RamCacheManager::Close(int fd) { | |
| 133 | bool rc; | ||
| 134 | |||
| 135 | 441356 | const WriteLockGuard guard(rwlock_); | |
| 136 |
1/2✓ Branch 1 taken 441356 times.
✗ Branch 2 not taken.
|
441356 | const ReadOnlyHandle generic_handle = fd_table_.GetHandle(fd); |
| 137 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 441356 times.
|
441356 | if (generic_handle.handle == kInvalidHandle) { |
| 138 | ✗ | LogCvmfs(kLogCache, kLogDebug, "bad fd %d on Close", fd); | |
| 139 | ✗ | return -EBADF; | |
| 140 | } | ||
| 141 |
1/2✓ Branch 2 taken 441356 times.
✗ Branch 3 not taken.
|
441356 | rc = GetStore(generic_handle)->Unref(generic_handle.handle); |
| 142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 441356 times.
|
441356 | assert(rc); |
| 143 | |||
| 144 |
1/2✓ Branch 1 taken 441356 times.
✗ Branch 2 not taken.
|
441356 | const int rc_int = fd_table_.CloseFd(fd); |
| 145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 441356 times.
|
441356 | assert(rc_int == 0); |
| 146 |
1/2✓ Branch 1 taken 441356 times.
✗ Branch 2 not taken.
|
441356 | LogCvmfs(kLogCache, kLogDebug, "closed fd %d", fd); |
| 147 | 441356 | perf::Inc(counters_.n_close); | |
| 148 | 441356 | return 0; | |
| 149 | 441356 | } | |
| 150 | |||
| 151 | |||
| 152 | 124 | int64_t RamCacheManager::Pread(int fd, | |
| 153 | void *buf, | ||
| 154 | uint64_t size, | ||
| 155 | uint64_t offset) { | ||
| 156 | 124 | const ReadLockGuard guard(rwlock_); | |
| 157 |
1/2✓ Branch 1 taken 124 times.
✗ Branch 2 not taken.
|
124 | const ReadOnlyHandle generic_handle = fd_table_.GetHandle(fd); |
| 158 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
|
124 | if (generic_handle.handle == kInvalidHandle) { |
| 159 | ✗ | LogCvmfs(kLogCache, kLogDebug, "bad fd %d on Pread", fd); | |
| 160 | ✗ | return -EBADF; | |
| 161 | } | ||
| 162 | 124 | perf::Inc(counters_.n_pread); | |
| 163 | return GetStore(generic_handle) | ||
| 164 |
1/2✓ Branch 2 taken 124 times.
✗ Branch 3 not taken.
|
124 | ->Read(generic_handle.handle, buf, size, offset); |
| 165 | 124 | } | |
| 166 | |||
| 167 | |||
| 168 | 441040 | int RamCacheManager::Dup(int fd) { | |
| 169 | bool ok; | ||
| 170 | int rc; | ||
| 171 | 441040 | const WriteLockGuard guard(rwlock_); | |
| 172 |
1/2✓ Branch 1 taken 441040 times.
✗ Branch 2 not taken.
|
441040 | const ReadOnlyHandle generic_handle = fd_table_.GetHandle(fd); |
| 173 |
2/2✓ Branch 1 taken 40 times.
✓ Branch 2 taken 441000 times.
|
441040 | if (generic_handle.handle == kInvalidHandle) { |
| 174 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | LogCvmfs(kLogCache, kLogDebug, "bad fd %d on Dup", fd); |
| 175 | 40 | return -EBADF; | |
| 176 | } | ||
| 177 |
1/2✓ Branch 1 taken 441000 times.
✗ Branch 2 not taken.
|
441000 | rc = AddFd(generic_handle); |
| 178 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 440960 times.
|
441000 | if (rc < 0) |
| 179 | 40 | return rc; | |
| 180 |
1/2✓ Branch 2 taken 440960 times.
✗ Branch 3 not taken.
|
440960 | ok = GetStore(generic_handle)->IncRef(generic_handle.handle); |
| 181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 440960 times.
|
440960 | assert(ok); |
| 182 |
1/2✓ Branch 1 taken 440960 times.
✗ Branch 2 not taken.
|
440960 | LogCvmfs(kLogCache, kLogDebug, "dup fd %d", fd); |
| 183 | 440960 | perf::Inc(counters_.n_dup); | |
| 184 | 440960 | return rc; | |
| 185 | 441040 | } | |
| 186 | |||
| 187 | |||
| 188 | /** | ||
| 189 | * For a RAM cache, read-ahead is a no-op. | ||
| 190 | */ | ||
| 191 | ✗ | int RamCacheManager::Readahead(int fd) { | |
| 192 | ✗ | const ReadLockGuard guard(rwlock_); | |
| 193 | ✗ | const ReadOnlyHandle generic_handle = fd_table_.GetHandle(fd); | |
| 194 | ✗ | if (generic_handle.handle == kInvalidHandle) { | |
| 195 | ✗ | LogCvmfs(kLogCache, kLogDebug, "bad fd %d on Readahead", fd); | |
| 196 | ✗ | return -EBADF; | |
| 197 | } | ||
| 198 | ✗ | LogCvmfs(kLogCache, kLogDebug, "readahead (no-op) on %d", fd); | |
| 199 | ✗ | perf::Inc(counters_.n_readahead); | |
| 200 | ✗ | return 0; | |
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | 1396 | int RamCacheManager::StartTxn(const shash::Any &id, uint64_t size, void *txn) { | |
| 205 |
1/2✓ Branch 2 taken 1396 times.
✗ Branch 3 not taken.
|
1396 | LogCvmfs(kLogCache, kLogDebug, "new transaction with id %s", |
| 206 | 2792 | id.ToString().c_str()); | |
| 207 | 1396 | Transaction *transaction = new (txn) Transaction(); | |
| 208 | 1396 | transaction->buffer.id = id; | |
| 209 | 1396 | transaction->pos = 0; | |
| 210 | 1396 | transaction->expected_size = size; | |
| 211 |
1/2✓ Branch 0 taken 1396 times.
✗ Branch 1 not taken.
|
1396 | transaction->buffer.size = (size == kSizeUnknown) ? kPageSize : size; |
| 212 | 1396 | transaction->buffer.address = malloc(transaction->buffer.size); | |
| 213 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1396 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1396 | if (!transaction->buffer.address && size > 0) { |
| 214 | ✗ | LogCvmfs(kLogCache, kLogDebug, "failed to allocate %lu B for %s", size, | |
| 215 | ✗ | id.ToString().c_str()); | |
| 216 | ✗ | return -errno; | |
| 217 | } | ||
| 218 | 1396 | perf::Inc(counters_.n_starttxn); | |
| 219 | 1396 | return 0; | |
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | 248 | void RamCacheManager::CtrlTxn(const Label &label, const int /* flags */, | |
| 224 | void *txn) { | ||
| 225 | 248 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 226 | 248 | transaction->description = label.GetDescription(); | |
| 227 | 248 | transaction->buffer.object_flags = label.flags; | |
| 228 |
1/2✓ Branch 2 taken 248 times.
✗ Branch 3 not taken.
|
248 | LogCvmfs(kLogCache, kLogDebug, "modified transaction %s", |
| 229 | 496 | transaction->buffer.id.ToString().c_str()); | |
| 230 | 248 | } | |
| 231 | |||
| 232 | |||
| 233 | 1448 | int64_t RamCacheManager::Write(const void *buf, uint64_t size, void *txn) { | |
| 234 | 1448 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 235 | |||
| 236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1448 times.
|
1448 | assert(transaction->pos <= transaction->buffer.size); |
| 237 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1408 times.
|
1448 | if (transaction->pos + size > transaction->buffer.size) { |
| 238 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (transaction->expected_size == kSizeUnknown) { |
| 239 | ✗ | perf::Inc(counters_.n_realloc); | |
| 240 | ✗ | const size_t new_size = max(2 * transaction->buffer.size, | |
| 241 | ✗ | static_cast<size_t>(size + transaction->pos)); | |
| 242 | ✗ | LogCvmfs(kLogCache, kLogDebug, "reallocate transaction for %s to %lu B", | |
| 243 | ✗ | transaction->buffer.id.ToString().c_str(), | |
| 244 | transaction->buffer.size); | ||
| 245 | ✗ | void *new_ptr = realloc(transaction->buffer.address, new_size); | |
| 246 | ✗ | if (!new_ptr) { | |
| 247 | ✗ | LogCvmfs(kLogCache, kLogDebug, "failed to allocate %lu B for %s", | |
| 248 | ✗ | new_size, transaction->buffer.id.ToString().c_str()); | |
| 249 | ✗ | return -EIO; | |
| 250 | } | ||
| 251 | ✗ | transaction->buffer.address = new_ptr; | |
| 252 | ✗ | transaction->buffer.size = new_size; | |
| 253 | } else { | ||
| 254 | 40 | LogCvmfs(kLogCache, kLogDebug, | |
| 255 | "attempted to write more than requested (%lu>%zu)", size, | ||
| 256 | transaction->buffer.size); | ||
| 257 | 40 | return -EFBIG; | |
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 |
2/4✓ Branch 0 taken 1408 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1408 times.
✗ Branch 3 not taken.
|
1408 | if (transaction->buffer.address && buf) { |
| 262 | // LogCvmfs(kLogCache, kLogDebug, "copy %u bytes of transaction %s", | ||
| 263 | // size, transaction->id.ToString().c_str()); | ||
| 264 | 1408 | memcpy(static_cast<char *>(transaction->buffer.address) + transaction->pos, | |
| 265 | buf, size); | ||
| 266 | } | ||
| 267 | 1408 | transaction->pos += size; | |
| 268 | 1408 | perf::Inc(counters_.n_write); | |
| 269 | 1408 | return size; | |
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | 68 | int RamCacheManager::Reset(void *txn) { | |
| 274 | 68 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 275 | 68 | transaction->pos = 0; | |
| 276 |
1/2✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
|
68 | LogCvmfs(kLogCache, kLogDebug, "reset transaction %s", |
| 277 | 136 | transaction->buffer.id.ToString().c_str()); | |
| 278 | 68 | perf::Inc(counters_.n_reset); | |
| 279 | 68 | return 0; | |
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | 388 | int RamCacheManager::OpenFromTxn(void *txn) { | |
| 284 | 388 | const WriteLockGuard guard(rwlock_); | |
| 285 | 388 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 286 |
1/2✓ Branch 1 taken 388 times.
✗ Branch 2 not taken.
|
388 | const int64_t retval = CommitToKvStore(transaction); |
| 287 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 348 times.
|
388 | if (retval < 0) { |
| 288 |
1/2✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
|
80 | LogCvmfs(kLogCache, kLogDebug, |
| 289 | "error while committing transaction on %s: %s", | ||
| 290 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
80 | transaction->buffer.id.ToString().c_str(), strerror(-retval)); |
| 291 | 40 | return retval; | |
| 292 | } | ||
| 293 |
1/2✓ Branch 2 taken 348 times.
✗ Branch 3 not taken.
|
348 | LogCvmfs(kLogCache, kLogDebug, "open pending transaction for %s", |
| 294 |
1/2✓ Branch 1 taken 348 times.
✗ Branch 2 not taken.
|
696 | transaction->buffer.id.ToString().c_str()); |
| 295 | 348 | perf::Inc(counters_.n_committxn); | |
| 296 |
1/2✓ Branch 1 taken 348 times.
✗ Branch 2 not taken.
|
348 | return DoOpen(transaction->buffer.id); |
| 297 | 388 | } | |
| 298 | |||
| 299 | |||
| 300 | 68 | int RamCacheManager::AbortTxn(void *txn) { | |
| 301 | 68 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 302 | 68 | free(transaction->buffer.address); | |
| 303 |
1/2✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
|
68 | LogCvmfs(kLogCache, kLogDebug, "abort transaction %s", |
| 304 | 136 | transaction->buffer.id.ToString().c_str()); | |
| 305 | 68 | perf::Inc(counters_.n_aborttxn); | |
| 306 | 68 | return 0; | |
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | 1088 | int RamCacheManager::CommitTxn(void *txn) { | |
| 311 | 1088 | const WriteLockGuard guard(rwlock_); | |
| 312 | 1088 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 313 | 1088 | perf::Inc(counters_.n_committxn); | |
| 314 |
1/2✓ Branch 1 taken 1088 times.
✗ Branch 2 not taken.
|
1088 | const int64_t rc = CommitToKvStore(transaction); |
| 315 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1048 times.
|
1088 | if (rc < 0) |
| 316 | 40 | return rc; | |
| 317 | 1048 | free(transaction->buffer.address); | |
| 318 | 1048 | return rc; | |
| 319 | 1088 | } | |
| 320 | |||
| 321 | |||
| 322 | 1476 | int64_t RamCacheManager::CommitToKvStore(Transaction *transaction) { | |
| 323 | MemoryKvStore *store; | ||
| 324 | |||
| 325 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 1380 times.
|
1476 | if (transaction->buffer.object_flags & CacheManager::kLabelVolatile) { |
| 326 | 96 | store = &volatile_entries_; | |
| 327 | } else { | ||
| 328 | 1380 | store = ®ular_entries_; | |
| 329 | } | ||
| 330 |
2/2✓ Branch 0 taken 1436 times.
✓ Branch 1 taken 40 times.
|
1476 | if ((transaction->buffer.object_flags & CacheManager::kLabelPinned) |
| 331 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1436 times.
|
1436 | || (transaction->buffer.object_flags & CacheManager::kLabelCatalog)) { |
| 332 | 40 | transaction->buffer.refcount = 1; | |
| 333 | } else { | ||
| 334 | 1436 | transaction->buffer.refcount = 0; | |
| 335 | } | ||
| 336 | |||
| 337 | 1476 | const int64_t regular_size = regular_entries_.GetUsed(); | |
| 338 | 1476 | const int64_t volatile_size = volatile_entries_.GetUsed(); | |
| 339 | 1476 | int64_t overrun = regular_size + volatile_size + transaction->buffer.size | |
| 340 | 1476 | - max_size_; | |
| 341 | |||
| 342 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 1236 times.
|
1476 | if (overrun > 0) { |
| 343 | // if we're going to clean the cache, try to remove at least 25% | ||
| 344 | 240 | overrun = max(overrun, (int64_t)max_size_ >> 2); | |
| 345 | 240 | perf::Inc(counters_.n_overrun); | |
| 346 |
1/2✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
|
240 | volatile_entries_.ShrinkTo(max((int64_t)0, volatile_size - overrun)); |
| 347 | } | ||
| 348 | 1476 | overrun -= volatile_size - volatile_entries_.GetUsed(); | |
| 349 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1276 times.
|
1476 | if (overrun > 0) { |
| 350 |
1/2✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
|
200 | regular_entries_.ShrinkTo(max((int64_t)0, regular_size - overrun)); |
| 351 | } | ||
| 352 | 1476 | overrun -= regular_size - regular_entries_.GetUsed(); | |
| 353 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 1396 times.
|
1476 | if (overrun > 0) { |
| 354 |
1/2✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | LogCvmfs(kLogCache, kLogDebug, |
| 355 | "transaction for %s would overrun the cache limit by %ld", | ||
| 356 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
160 | transaction->buffer.id.ToString().c_str(), overrun); |
| 357 | 80 | perf::Inc(counters_.n_full); | |
| 358 | 80 | return -ENOSPC; | |
| 359 | } | ||
| 360 | |||
| 361 |
1/2✓ Branch 1 taken 1396 times.
✗ Branch 2 not taken.
|
1396 | const int rc = store->Commit(transaction->buffer); |
| 362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1396 times.
|
1396 | if (rc < 0) { |
| 363 | ✗ | LogCvmfs(kLogCache, kLogDebug, "commit on %s failed", | |
| 364 | ✗ | transaction->buffer.id.ToString().c_str()); | |
| 365 | ✗ | return rc; | |
| 366 | } | ||
| 367 |
1/2✓ Branch 2 taken 1396 times.
✗ Branch 3 not taken.
|
1396 | LogCvmfs(kLogCache, kLogDebug, "committed %s to cache", |
| 368 |
1/2✓ Branch 1 taken 1396 times.
✗ Branch 2 not taken.
|
2792 | transaction->buffer.id.ToString().c_str()); |
| 369 | 1396 | return 0; | |
| 370 | } | ||
| 371 |