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