GCC Code Coverage Report


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