| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/cache_ram.cc |
| Date: | 2026-02-15 02:35:50 |
| 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 | 589 | RamCacheManager::RamCacheManager(uint64_t max_size, | |
| 31 | unsigned max_entries, | ||
| 32 | MemoryKvStore::MemoryAllocator alloc, | ||
| 33 | 589 | perf::StatisticsTemplate statistics) | |
| 34 | 589 | : max_size_(max_size) | |
| 35 |
1/2✓ Branch 1 taken 589 times.
✗ Branch 2 not taken.
|
589 | , 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 589 times.
✗ Branch 2 not taken.
|
589 | , regular_entries_(max_entries, |
| 39 | alloc, | ||
| 40 | max_size, | ||
| 41 |
2/4✓ Branch 2 taken 589 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 589 times.
✗ Branch 6 not taken.
|
1178 | perf::StatisticsTemplate("kv.regular", statistics)) |
| 42 |
1/2✓ Branch 1 taken 589 times.
✗ Branch 2 not taken.
|
589 | , volatile_entries_(max_entries, |
| 43 | alloc, | ||
| 44 | max_size, | ||
| 45 |
2/4✓ Branch 2 taken 589 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 589 times.
✗ Branch 6 not taken.
|
1178 | perf::StatisticsTemplate("kv.volatile", statistics)) |
| 46 |
2/4✓ Branch 3 taken 589 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 589 times.
✗ Branch 7 not taken.
|
1178 | , counters_(statistics) { |
| 47 | 589 | const int retval = pthread_rwlock_init(&rwlock_, NULL); | |
| 48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 589 times.
|
589 | assert(retval == 0); |
| 49 |
1/2✓ Branch 1 taken 589 times.
✗ Branch 2 not taken.
|
589 | LogCvmfs(kLogCache, kLogDebug, "max %lu B, %u entries", max_size, |
| 50 | max_entries); | ||
| 51 |
1/2✓ Branch 1 taken 589 times.
✗ Branch 2 not taken.
|
589 | LogCvmfs(kLogCache, kLogDebug | kLogSyslogWarn, |
| 52 | "DEPRECATION WARNING: The RAM cache manager is depcreated and " | ||
| 53 | "will be removed from future releases."); | ||
| 54 | 589 | } | |
| 55 | |||
| 56 | |||
| 57 | 1876 | RamCacheManager::~RamCacheManager() { pthread_rwlock_destroy(&rwlock_); } | |
| 58 | |||
| 59 | |||
| 60 | 220860 | int RamCacheManager::AddFd(const ReadOnlyHandle &handle) { | |
| 61 | 220860 | const int result = fd_table_.OpenFd(handle); | |
| 62 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 220840 times.
|
220860 | if (result == -ENFILE) { |
| 63 | 20 | LogCvmfs(kLogCache, kLogDebug, "too many open files"); | |
| 64 | 20 | perf::Inc(counters_.n_enfile); | |
| 65 | } | ||
| 66 | 220860 | return result; | |
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | 189 | bool RamCacheManager::AcquireQuotaManager(QuotaManager *quota_mgr) { | |
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 189 times.
|
189 | assert(quota_mgr != NULL); |
| 72 | 189 | quota_mgr_ = quota_mgr; | |
| 73 | 189 | LogCvmfs(kLogCache, kLogDebug, "set quota manager"); | |
| 74 | 189 | return true; | |
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | 460 | int RamCacheManager::Open(const LabeledObject &object) { | |
| 79 | 460 | const WriteLockGuard guard(rwlock_); | |
| 80 |
1/2✓ Branch 1 taken 460 times.
✗ Branch 2 not taken.
|
920 | return DoOpen(object.id); |
| 81 | 460 | } | |
| 82 | |||
| 83 | |||
| 84 | 640 | int RamCacheManager::DoOpen(const shash::Any &id) { | |
| 85 | bool ok; | ||
| 86 | bool is_volatile; | ||
| 87 |
1/2✓ Branch 1 taken 640 times.
✗ Branch 2 not taken.
|
640 | const MemoryBuffer buf; |
| 88 | |||
| 89 |
3/4✓ Branch 1 taken 640 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 320 times.
✓ Branch 4 taken 320 times.
|
640 | if (regular_entries_.Contains(id)) { |
| 90 | 320 | is_volatile = false; | |
| 91 |
3/4✓ Branch 1 taken 320 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 280 times.
|
320 | } else if (volatile_entries_.Contains(id)) { |
| 92 | 40 | is_volatile = true; | |
| 93 | } else { | ||
| 94 |
2/4✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 280 times.
✗ Branch 6 not taken.
|
280 | LogCvmfs(kLogCache, kLogDebug, "miss for %s", id.ToString().c_str()); |
| 95 | 280 | perf::Inc(counters_.n_openmiss); | |
| 96 | 280 | return -ENOENT; | |
| 97 | } | ||
| 98 | 360 | const ReadOnlyHandle generic_handle(id, is_volatile); | |
| 99 |
1/2✓ Branch 1 taken 360 times.
✗ Branch 2 not taken.
|
360 | const int fd = AddFd(generic_handle); |
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
|
360 | 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 40 times.
✓ Branch 1 taken 320 times.
|
360 | if (is_volatile) { |
| 106 |
1/2✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | LogCvmfs(kLogCache, kLogDebug, "hit in volatile entries for %s", |
| 107 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
80 | id.ToString().c_str()); |
| 108 | 40 | perf::Inc(counters_.n_openvolatile); | |
| 109 | } else { | ||
| 110 |
1/2✓ Branch 2 taken 320 times.
✗ Branch 3 not taken.
|
320 | LogCvmfs(kLogCache, kLogDebug, "hit in regular entries for %s", |
| 111 |
1/2✓ Branch 1 taken 320 times.
✗ Branch 2 not taken.
|
640 | id.ToString().c_str()); |
| 112 | 320 | perf::Inc(counters_.n_openregular); | |
| 113 | } | ||
| 114 |
1/2✓ Branch 2 taken 360 times.
✗ Branch 3 not taken.
|
360 | ok = GetStore(generic_handle)->IncRef(id); |
| 115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
|
360 | assert(ok); |
| 116 | 360 | return fd; | |
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | 120 | int64_t RamCacheManager::GetSize(int fd) { | |
| 121 | 120 | const ReadLockGuard guard(rwlock_); | |
| 122 |
1/2✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
|
120 | const ReadOnlyHandle generic_handle = fd_table_.GetHandle(fd); |
| 123 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
|
120 | if (generic_handle.handle == kInvalidHandle) { |
| 124 | ✗ | LogCvmfs(kLogCache, kLogDebug, "bad fd %d on GetSize", fd); | |
| 125 | ✗ | return -EBADF; | |
| 126 | } | ||
| 127 | 120 | perf::Inc(counters_.n_getsize); | |
| 128 |
1/2✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
|
120 | return GetStore(generic_handle)->GetSize(generic_handle.handle); |
| 129 | 120 | } | |
| 130 | |||
| 131 | |||
| 132 | 220720 | int RamCacheManager::Close(int fd) { | |
| 133 | bool rc; | ||
| 134 | |||
| 135 | 220720 | const WriteLockGuard guard(rwlock_); | |
| 136 |
1/2✓ Branch 1 taken 220720 times.
✗ Branch 2 not taken.
|
220720 | const ReadOnlyHandle generic_handle = fd_table_.GetHandle(fd); |
| 137 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 220720 times.
|
220720 | 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 220720 times.
✗ Branch 3 not taken.
|
220720 | rc = GetStore(generic_handle)->Unref(generic_handle.handle); |
| 142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220720 times.
|
220720 | assert(rc); |
| 143 | |||
| 144 |
1/2✓ Branch 1 taken 220720 times.
✗ Branch 2 not taken.
|
220720 | const int rc_int = fd_table_.CloseFd(fd); |
| 145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220720 times.
|
220720 | assert(rc_int == 0); |
| 146 |
1/2✓ Branch 1 taken 220720 times.
✗ Branch 2 not taken.
|
220720 | LogCvmfs(kLogCache, kLogDebug, "closed fd %d", fd); |
| 147 | 220720 | perf::Inc(counters_.n_close); | |
| 148 | 220720 | return 0; | |
| 149 | 220720 | } | |
| 150 | |||
| 151 | |||
| 152 | 80 | int64_t RamCacheManager::Pread(int fd, | |
| 153 | void *buf, | ||
| 154 | uint64_t size, | ||
| 155 | uint64_t offset) { | ||
| 156 | 80 | const ReadLockGuard guard(rwlock_); | |
| 157 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | const ReadOnlyHandle generic_handle = fd_table_.GetHandle(fd); |
| 158 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
|
80 | if (generic_handle.handle == kInvalidHandle) { |
| 159 | ✗ | LogCvmfs(kLogCache, kLogDebug, "bad fd %d on Pread", fd); | |
| 160 | ✗ | return -EBADF; | |
| 161 | } | ||
| 162 | 80 | perf::Inc(counters_.n_pread); | |
| 163 | return GetStore(generic_handle) | ||
| 164 |
1/2✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | ->Read(generic_handle.handle, buf, size, offset); |
| 165 | 80 | } | |
| 166 | |||
| 167 | |||
| 168 | 220520 | int RamCacheManager::Dup(int fd) { | |
| 169 | bool ok; | ||
| 170 | int rc; | ||
| 171 | 220520 | const WriteLockGuard guard(rwlock_); | |
| 172 |
1/2✓ Branch 1 taken 220520 times.
✗ Branch 2 not taken.
|
220520 | const ReadOnlyHandle generic_handle = fd_table_.GetHandle(fd); |
| 173 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 220500 times.
|
220520 | if (generic_handle.handle == kInvalidHandle) { |
| 174 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | LogCvmfs(kLogCache, kLogDebug, "bad fd %d on Dup", fd); |
| 175 | 20 | return -EBADF; | |
| 176 | } | ||
| 177 |
1/2✓ Branch 1 taken 220500 times.
✗ Branch 2 not taken.
|
220500 | rc = AddFd(generic_handle); |
| 178 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 220480 times.
|
220500 | if (rc < 0) |
| 179 | 20 | return rc; | |
| 180 |
1/2✓ Branch 2 taken 220480 times.
✗ Branch 3 not taken.
|
220480 | ok = GetStore(generic_handle)->IncRef(generic_handle.handle); |
| 181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220480 times.
|
220480 | assert(ok); |
| 182 |
1/2✓ Branch 1 taken 220480 times.
✗ Branch 2 not taken.
|
220480 | LogCvmfs(kLogCache, kLogDebug, "dup fd %d", fd); |
| 183 | 220480 | perf::Inc(counters_.n_dup); | |
| 184 | 220480 | return rc; | |
| 185 | 220520 | } | |
| 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 | 740 | int RamCacheManager::StartTxn(const shash::Any &id, uint64_t size, void *txn) { | |
| 205 |
1/2✓ Branch 2 taken 740 times.
✗ Branch 3 not taken.
|
740 | LogCvmfs(kLogCache, kLogDebug, "new transaction with id %s", |
| 206 | 1480 | id.ToString().c_str()); | |
| 207 | 740 | Transaction *transaction = new (txn) Transaction(); | |
| 208 | 740 | transaction->buffer.id = id; | |
| 209 | 740 | transaction->pos = 0; | |
| 210 | 740 | transaction->expected_size = size; | |
| 211 |
1/2✓ Branch 0 taken 740 times.
✗ Branch 1 not taken.
|
740 | transaction->buffer.size = (size == kSizeUnknown) ? kPageSize : size; |
| 212 | 740 | transaction->buffer.address = malloc(transaction->buffer.size); | |
| 213 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 740 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
740 | 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 | 740 | perf::Inc(counters_.n_starttxn); | |
| 219 | 740 | return 0; | |
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | 160 | void RamCacheManager::CtrlTxn(const Label &label, const int /* flags */, | |
| 224 | void *txn) { | ||
| 225 | 160 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 226 | 160 | transaction->description = label.GetDescription(); | |
| 227 | 160 | transaction->buffer.object_flags = label.flags; | |
| 228 |
1/2✓ Branch 2 taken 160 times.
✗ Branch 3 not taken.
|
160 | LogCvmfs(kLogCache, kLogDebug, "modified transaction %s", |
| 229 | 320 | transaction->buffer.id.ToString().c_str()); | |
| 230 | 160 | } | |
| 231 | |||
| 232 | |||
| 233 | 760 | int64_t RamCacheManager::Write(const void *buf, uint64_t size, void *txn) { | |
| 234 | 760 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 235 | |||
| 236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 760 times.
|
760 | assert(transaction->pos <= transaction->buffer.size); |
| 237 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 740 times.
|
760 | if (transaction->pos + size > transaction->buffer.size) { |
| 238 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | 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 | 20 | LogCvmfs(kLogCache, kLogDebug, | |
| 255 | "attempted to write more than requested (%lu>%zu)", size, | ||
| 256 | transaction->buffer.size); | ||
| 257 | 20 | return -EFBIG; | |
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 |
2/4✓ Branch 0 taken 740 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 740 times.
✗ Branch 3 not taken.
|
740 | if (transaction->buffer.address && buf) { |
| 262 | // LogCvmfs(kLogCache, kLogDebug, "copy %u bytes of transaction %s", | ||
| 263 | // size, transaction->id.ToString().c_str()); | ||
| 264 | 740 | memcpy(static_cast<char *>(transaction->buffer.address) + transaction->pos, | |
| 265 | buf, size); | ||
| 266 | } | ||
| 267 | 740 | transaction->pos += size; | |
| 268 | 740 | perf::Inc(counters_.n_write); | |
| 269 | 740 | return size; | |
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | 40 | int RamCacheManager::Reset(void *txn) { | |
| 274 | 40 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 275 | 40 | transaction->pos = 0; | |
| 276 |
1/2✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | LogCvmfs(kLogCache, kLogDebug, "reset transaction %s", |
| 277 | 80 | transaction->buffer.id.ToString().c_str()); | |
| 278 | 40 | perf::Inc(counters_.n_reset); | |
| 279 | 40 | return 0; | |
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | 200 | int RamCacheManager::OpenFromTxn(void *txn) { | |
| 284 | 200 | const WriteLockGuard guard(rwlock_); | |
| 285 | 200 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 286 |
1/2✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
|
200 | const int64_t retval = CommitToKvStore(transaction); |
| 287 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 180 times.
|
200 | if (retval < 0) { |
| 288 |
1/2✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
40 | LogCvmfs(kLogCache, kLogDebug, |
| 289 | "error while committing transaction on %s: %s", | ||
| 290 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
40 | transaction->buffer.id.ToString().c_str(), strerror(-retval)); |
| 291 | 20 | return retval; | |
| 292 | } | ||
| 293 |
1/2✓ Branch 2 taken 180 times.
✗ Branch 3 not taken.
|
180 | LogCvmfs(kLogCache, kLogDebug, "open pending transaction for %s", |
| 294 |
1/2✓ Branch 1 taken 180 times.
✗ Branch 2 not taken.
|
360 | transaction->buffer.id.ToString().c_str()); |
| 295 | 180 | perf::Inc(counters_.n_committxn); | |
| 296 |
1/2✓ Branch 1 taken 180 times.
✗ Branch 2 not taken.
|
180 | return DoOpen(transaction->buffer.id); |
| 297 | 200 | } | |
| 298 | |||
| 299 | |||
| 300 | 40 | int RamCacheManager::AbortTxn(void *txn) { | |
| 301 | 40 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 302 | 40 | free(transaction->buffer.address); | |
| 303 |
1/2✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | LogCvmfs(kLogCache, kLogDebug, "abort transaction %s", |
| 304 | 80 | transaction->buffer.id.ToString().c_str()); | |
| 305 | 40 | perf::Inc(counters_.n_aborttxn); | |
| 306 | 40 | return 0; | |
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | 580 | int RamCacheManager::CommitTxn(void *txn) { | |
| 311 | 580 | const WriteLockGuard guard(rwlock_); | |
| 312 | 580 | Transaction *transaction = reinterpret_cast<Transaction *>(txn); | |
| 313 | 580 | perf::Inc(counters_.n_committxn); | |
| 314 |
1/2✓ Branch 1 taken 580 times.
✗ Branch 2 not taken.
|
580 | const int64_t rc = CommitToKvStore(transaction); |
| 315 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 560 times.
|
580 | if (rc < 0) |
| 316 | 20 | return rc; | |
| 317 | 560 | free(transaction->buffer.address); | |
| 318 | 560 | return rc; | |
| 319 | 580 | } | |
| 320 | |||
| 321 | |||
| 322 | 780 | int64_t RamCacheManager::CommitToKvStore(Transaction *transaction) { | |
| 323 | MemoryKvStore *store; | ||
| 324 | |||
| 325 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 720 times.
|
780 | if (transaction->buffer.object_flags & CacheManager::kLabelVolatile) { |
| 326 | 60 | store = &volatile_entries_; | |
| 327 | } else { | ||
| 328 | 720 | store = ®ular_entries_; | |
| 329 | } | ||
| 330 |
2/2✓ Branch 0 taken 760 times.
✓ Branch 1 taken 20 times.
|
780 | if ((transaction->buffer.object_flags & CacheManager::kLabelPinned) |
| 331 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 760 times.
|
760 | || (transaction->buffer.object_flags & CacheManager::kLabelCatalog)) { |
| 332 | 20 | transaction->buffer.refcount = 1; | |
| 333 | } else { | ||
| 334 | 760 | transaction->buffer.refcount = 0; | |
| 335 | } | ||
| 336 | |||
| 337 | 780 | const int64_t regular_size = regular_entries_.GetUsed(); | |
| 338 | 780 | const int64_t volatile_size = volatile_entries_.GetUsed(); | |
| 339 | 780 | int64_t overrun = regular_size + volatile_size + transaction->buffer.size | |
| 340 | 780 | - max_size_; | |
| 341 | |||
| 342 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 660 times.
|
780 | if (overrun > 0) { |
| 343 | // if we're going to clean the cache, try to remove at least 25% | ||
| 344 | 120 | overrun = max(overrun, (int64_t)max_size_ >> 2); | |
| 345 | 120 | perf::Inc(counters_.n_overrun); | |
| 346 |
1/2✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
|
120 | volatile_entries_.ShrinkTo(max((int64_t)0, volatile_size - overrun)); |
| 347 | } | ||
| 348 | 780 | overrun -= volatile_size - volatile_entries_.GetUsed(); | |
| 349 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 680 times.
|
780 | if (overrun > 0) { |
| 350 |
1/2✓ Branch 2 taken 100 times.
✗ Branch 3 not taken.
|
100 | regular_entries_.ShrinkTo(max((int64_t)0, regular_size - overrun)); |
| 351 | } | ||
| 352 | 780 | overrun -= regular_size - regular_entries_.GetUsed(); | |
| 353 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 740 times.
|
780 | if (overrun > 0) { |
| 354 |
1/2✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | LogCvmfs(kLogCache, kLogDebug, |
| 355 | "transaction for %s would overrun the cache limit by %ld", | ||
| 356 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
80 | transaction->buffer.id.ToString().c_str(), overrun); |
| 357 | 40 | perf::Inc(counters_.n_full); | |
| 358 | 40 | return -ENOSPC; | |
| 359 | } | ||
| 360 | |||
| 361 |
1/2✓ Branch 1 taken 740 times.
✗ Branch 2 not taken.
|
740 | const int rc = store->Commit(transaction->buffer); |
| 362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 740 times.
|
740 | 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 740 times.
✗ Branch 3 not taken.
|
740 | LogCvmfs(kLogCache, kLogDebug, "committed %s to cache", |
| 368 |
1/2✓ Branch 1 taken 740 times.
✗ Branch 2 not taken.
|
1480 | transaction->buffer.id.ToString().c_str()); |
| 369 | 740 | return 0; | |
| 370 | } | ||
| 371 |