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