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