GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/glue_buffer.cc
Date: 2025-11-09 02:35:23
Exec Total Coverage
Lines: 201 273 73.6%
Branches: 75 180 41.7%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #define __STDC_FORMAT_MACROS
6
7
8 #include "glue_buffer.h"
9
10 #include <errno.h>
11 #include <poll.h>
12 #include <unistd.h>
13
14 #include <cassert>
15 #include <cstdlib>
16 #include <cstring>
17 #include <string>
18 #include <vector>
19
20 #include "util/exception.h"
21 #include "util/logging.h"
22 #include "util/mutex.h"
23 #include "util/platform.h"
24 #include "util/posix.h"
25 #include "util/smalloc.h"
26
27 using namespace std; // NOLINT
28
29 namespace glue {
30
31 PathStore &PathStore::operator=(const PathStore &other) {
32 if (&other == this)
33 return *this;
34
35 delete string_heap_;
36 CopyFrom(other);
37 return *this;
38 }
39
40
41 PathStore::PathStore(const PathStore &other) { CopyFrom(other); }
42
43
44 void PathStore::CopyFrom(const PathStore &other) {
45 map_ = other.map_;
46
47 string_heap_ = new StringHeap(other.string_heap_->used());
48 const shash::Md5 empty_path = map_.empty_key();
49 for (unsigned i = 0; i < map_.capacity(); ++i) {
50 if (map_.keys()[i] != empty_path) {
51 (map_.values() + i)->name = string_heap_->AddString(
52 map_.values()[i].name.length(), map_.values()[i].name.data());
53 }
54 }
55 }
56
57
58 //------------------------------------------------------------------------------
59
60
61 506 void InodeTracker::InitLock() {
62 506 lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
63 506 const int retval = pthread_mutex_init(lock_, NULL);
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 506 times.
506 assert(retval == 0);
65 506 }
66
67
68 void InodeTracker::CopyFrom(const InodeTracker &other) {
69 assert(other.version_ == kVersion);
70 version_ = kVersion;
71 path_map_ = other.path_map_;
72 inode_ex_map_ = other.inode_ex_map_;
73 inode_references_ = other.inode_references_;
74 statistics_ = other.statistics_;
75 }
76
77
78
2/4
✓ Branch 2 taken 506 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 506 times.
✗ Branch 6 not taken.
506 InodeTracker::InodeTracker() {
79 506 version_ = kVersion;
80 506 InitLock();
81 506 }
82
83
84 InodeTracker::InodeTracker(const InodeTracker &other) {
85 CopyFrom(other);
86 InitLock();
87 }
88
89
90 InodeTracker &InodeTracker::operator=(const InodeTracker &other) {
91 if (&other == this)
92 return *this;
93
94 CopyFrom(other);
95 return *this;
96 }
97
98
99 504 InodeTracker::~InodeTracker() {
100 504 pthread_mutex_destroy(lock_);
101 504 free(lock_);
102 504 }
103
104
105 //------------------------------------------------------------------------------
106
107 382 DentryTracker::DentryTracker() : version_(kVersion), is_active_(true) {
108 382 pipe_terminate_[0] = pipe_terminate_[1] = -1;
109 382 cleaning_interval_ms_ = -1;
110 382 InitLock();
111 382 }
112
113
114 409 DentryTracker::~DentryTracker() {
115
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 394 times.
409 if (pipe_terminate_[1] >= 0) {
116 15 char t = 'T';
117 15 WritePipe(pipe_terminate_[1], &t, 1);
118 15 pthread_join(thread_cleaner_, NULL);
119 15 ClosePipe(pipe_terminate_);
120 }
121 409 pthread_mutex_destroy(lock_);
122 409 free(lock_);
123 409 }
124
125
126 44 DentryTracker::DentryTracker(const DentryTracker &other) {
127
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 CopyFrom(other);
128 44 pipe_terminate_[0] = pipe_terminate_[1] = -1;
129 44 cleaning_interval_ms_ = -1;
130 44 InitLock();
131 44 }
132
133
134 DentryTracker &DentryTracker::operator=(const DentryTracker &other) {
135 if (&other == this)
136 return *this;
137
138 Lock();
139 CopyFrom(other);
140 Unlock();
141 return *this;
142 }
143
144
145 44 void DentryTracker::CopyFrom(const DentryTracker &other) {
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 assert(other.version_ == kVersion);
147
148 44 version_ = kVersion;
149 44 statistics_ = other.statistics_;
150 44 is_active_ = other.is_active_;
151 44 entries_ = other.entries_;
152 44 }
153
154
155 44 DentryTracker *DentryTracker::Move() {
156 44 Lock();
157
1/2
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
44 DentryTracker *new_tracker = new DentryTracker(*this);
158 44 statistics_.num_remove += entries_.size();
159 44 entries_.Clear();
160 44 Unlock();
161 44 return new_tracker;
162 }
163
164
165 15 void DentryTracker::SpawnCleaner(unsigned interval_s) {
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 assert(pipe_terminate_[0] == -1);
167 15 cleaning_interval_ms_ = interval_s * 1000;
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (cleaning_interval_ms_ == 0)
169 cleaning_interval_ms_ = -1;
170 15 MakePipe(pipe_terminate_);
171 15 const int retval = pthread_create(&thread_cleaner_, NULL, MainCleaner, this);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 assert(retval == 0);
173 15 }
174
175
176 15 void *DentryTracker::MainCleaner(void *data) {
177 15 DentryTracker *tracker = reinterpret_cast<DentryTracker *>(data);
178
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 LogCvmfs(kLogCvmfs, kLogDebug, "starting negative entry cache cleaner");
179
180 struct pollfd watch_term;
181 15 watch_term.fd = tracker->pipe_terminate_[0];
182 15 watch_term.events = POLLIN | POLLPRI;
183 15 int timeout_ms = tracker->cleaning_interval_ms_;
184 ;
185 15 uint64_t deadline = platform_monotonic_time() + timeout_ms / 1000;
186 while (true) {
187 60 watch_term.revents = 0;
188
1/2
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
60 const int retval = poll(&watch_term, 1, timeout_ms);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (retval < 0) {
190 if (errno == EINTR) {
191 if (timeout_ms >= 0) {
192 const uint64_t now = platform_monotonic_time();
193 timeout_ms = (now > deadline) ? 0 : (deadline - now) * 1000;
194 }
195 45 continue;
196 }
197 abort();
198 }
199 60 timeout_ms = tracker->cleaning_interval_ms_;
200 60 deadline = platform_monotonic_time() + timeout_ms / 1000;
201
202
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 15 times.
60 if (retval == 0) {
203
1/2
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
45 LogCvmfs(kLogCvmfs, kLogDebug, "negative entry cleaner: pruning");
204
1/2
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
45 tracker->Prune();
205 45 continue;
206 }
207
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 assert(watch_term.revents != 0);
209
210 15 char c = 0;
211
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 ReadPipe(tracker->pipe_terminate_[0], &c, 1);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 assert(c == 'T');
213 15 break;
214 45 }
215
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 LogCvmfs(kLogCvmfs, kLogDebug, "stopping negative entry cache cleaner");
216 15 return NULL;
217 }
218
219
220 426 void DentryTracker::InitLock() {
221 426 lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
222 426 const int retval = pthread_mutex_init(lock_, NULL);
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 426 times.
426 assert(retval == 0);
224 426 }
225
226
227 87 void DentryTracker::Prune() {
228 87 Lock();
229 87 DoPrune(platform_monotonic_time());
230 87 Unlock();
231 87 }
232
233
234 136 DentryTracker::Cursor DentryTracker::BeginEnumerate() {
235 136 Entry *head = NULL;
236 136 Lock();
237
1/2
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
136 entries_.Peek(&head);
238 136 return Cursor(head);
239 }
240
241
242 11694 bool DentryTracker::NextEntry(Cursor *cursor, uint64_t *inode_parent,
243 NameString *name) {
244
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 11608 times.
11694 if (cursor->head == NULL)
245 86 return false;
246
2/2
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 11567 times.
11608 if (cursor->pos >= entries_.size())
247 41 return false;
248 11567 Entry *e = cursor->head + cursor->pos;
249 11567 *inode_parent = e->inode_parent;
250 11567 *name = e->name;
251 11567 cursor->pos++;
252 11567 return true;
253 }
254
255
256 136 void DentryTracker::EndEnumerate(Cursor * /* cursor */) { Unlock(); }
257
258
259 //------------------------------------------------------------------------------
260
261
262
1/2
✓ Branch 3 taken 355 times.
✗ Branch 4 not taken.
355 PageCacheTracker::PageCacheTracker() : version_(kVersion), is_active_(true) {
263
1/2
✓ Branch 1 taken 355 times.
✗ Branch 2 not taken.
355 map_.Init(16, 0, hasher_inode);
264 355 InitLock();
265 355 }
266
267
268 353 PageCacheTracker::~PageCacheTracker() {
269 353 pthread_mutex_destroy(lock_);
270 353 free(lock_);
271 353 }
272
273
274 PageCacheTracker::PageCacheTracker(const PageCacheTracker &other) {
275 CopyFrom(other);
276 InitLock();
277 }
278
279
280 PageCacheTracker &PageCacheTracker::operator=(const PageCacheTracker &other) {
281 if (&other == this)
282 return *this;
283
284 const MutexLockGuard guard(lock_);
285 CopyFrom(other);
286 return *this;
287 }
288
289
290 void PageCacheTracker::CopyFrom(const PageCacheTracker &other) {
291 assert(other.version_ == kVersion);
292
293 version_ = kVersion;
294 is_active_ = other.is_active_;
295 statistics_ = other.statistics_;
296
297 map_.Init(16, 0, hasher_inode);
298 map_ = other.map_;
299 stat_store_ = other.stat_store_;
300 }
301
302
303 355 void PageCacheTracker::InitLock() {
304 355 lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
305 355 const int retval = pthread_mutex_init(lock_, NULL);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355 times.
355 assert(retval == 0);
307 355 }
308
309 156 PageCacheTracker::OpenDirectives PageCacheTracker::Open(
310 uint64_t inode, const shash::Any &hash, const struct stat &info)
311 {
312 156 OpenDirectives open_directives;
313 // Old behavior: always flush page cache on open
314
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 141 times.
156 if (!is_active_)
315 15 return open_directives;
316
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141 times.
141 if (inode != info.st_ino) {
318 PANIC(kLogStderr | kLogDebug,
319 "invalid entry on open: %" PRIu64 " with st_ino=%" PRIu64,
320 " hash=%s size=%" PRIu64, inode, info.st_ino, hash.ToString().c_str(),
321 info.st_size);
322 }
323
324 141 const MutexLockGuard guard(lock_);
325
326
1/2
✓ Branch 1 taken 141 times.
✗ Branch 2 not taken.
141 Entry entry;
327
1/2
✓ Branch 1 taken 141 times.
✗ Branch 2 not taken.
141 const bool retval = map_.Lookup(inode, &entry);
328
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 94 times.
141 if (!retval) {
329 47 open_directives.keep_cache = true;
330 47 open_directives.direct_io = false;
331 47 statistics_.n_insert++;
332 47 statistics_.n_open_cached++;
333
334 47 entry.nopen = 1;
335
1/2
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
47 entry.idx_stat = stat_store_.Add(info);
336 47 entry.hash = hash;
337
1/2
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
47 map_.Insert(inode, entry);
338 47 return open_directives;
339 }
340
341
2/2
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 32 times.
94 if (entry.hash == hash) {
342 62 open_directives.direct_io = false;
343
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 47 times.
62 if (entry.nopen < 0) {
344 // The page cache is still in the transition phase and may contain old
345 // content. So trigger a flush of the cache in any case.
346 15 open_directives.keep_cache = false;
347 15 statistics_.n_open_flush++;
348 15 entry.nopen--;
349
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 map_.Insert(inode, entry);
350 15 return open_directives;
351 } else {
352 47 open_directives.keep_cache = true;
353 47 statistics_.n_open_cached++;
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (entry.nopen++ == 0)
355 entry.idx_stat = stat_store_.Add(info);
356
1/2
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
47 map_.Insert(inode, entry);
357 47 return open_directives;
358 }
359 }
360
361 // Page cache mismatch and old data has still open file attached to it,
362 // circumvent the page cache entirely and use direct I/O. In this case,
363 // cvmfs_close() will _not_ call Close().
364
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 15 times.
32 if (entry.nopen != 0) {
365 17 open_directives.keep_cache = true;
366 17 open_directives.direct_io = true;
367 17 statistics_.n_open_direct++;
368 17 return open_directives;
369 }
370
371 // Stale data in the page cache, start the transition phase in which newly
372 // opened files flush the page cache and re-populate it with the new hash.
373 // The first file to reach Close() will finish the transition phase and
374 // mark the new hash as committed.
375 15 open_directives.direct_io = false;
376 15 open_directives.keep_cache = false;
377 15 statistics_.n_open_flush++;
378 15 entry.hash = hash;
379
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 entry.idx_stat = stat_store_.Add(info);
380 15 entry.nopen = -1;
381
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 map_.Insert(inode, entry);
382 15 return open_directives;
383 141 }
384
385 PageCacheTracker::OpenDirectives PageCacheTracker::OpenDirect() {
386 OpenDirectives open_directives(true, true);
387 // Old behavior: always flush page cache on open
388 if (!is_active_)
389 return open_directives;
390
391 const MutexLockGuard guard(lock_);
392 statistics_.n_open_direct++;
393 return open_directives;
394 }
395
396 122 void PageCacheTracker::Close(uint64_t inode) {
397
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 107 times.
122 if (!is_active_)
398 15 return;
399
400 107 const MutexLockGuard guard(lock_);
401
1/2
✓ Branch 1 taken 107 times.
✗ Branch 2 not taken.
107 Entry entry;
402
1/2
✓ Branch 1 taken 107 times.
✗ Branch 2 not taken.
107 bool retval = map_.Lookup(inode, &entry);
403
404 107 if (!AssertOrLog(retval, kLogCvmfs, kLogSyslogWarn | kLogDebug,
405 "PageCacheTracker::Close Race condition? "
406 "Did not find inode %lu",
407 inode)
408
3/6
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 107 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 107 times.
107 || !AssertOrLog(entry.nopen != 0, kLogCvmfs, kLogSyslogWarn | kLogDebug,
409 "PageCacheTracker::Close Race condition? "
410 "Inode %lu has no open entries",
411 inode)) {
412 return;
413 }
414
415 107 const int32_t old_open = entry.nopen;
416
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 92 times.
107 if (entry.nopen < 0) {
417 // At this point we know that any stale data has been flushed from the
418 // cache and only data related to the currently booked content hash
419 // can be present. So clear the transition bit (sign bit).
420 15 entry.nopen = -entry.nopen;
421 }
422 107 entry.nopen--;
423
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 61 times.
107 if (entry.nopen == 0) {
424 // File closed, remove struct stat information
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (entry.idx_stat < 0) {
426 PANIC(kLogSyslogErr | kLogDebug,
427 "page cache tracker: missing stat entry! Entry info: inode %" PRIu64
428 " - open counter %d - hash %s",
429 inode, old_open, entry.hash.ToString().c_str());
430 }
431
1/2
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
46 const uint64_t inode_update = stat_store_.Erase(entry.idx_stat);
432
1/2
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
46 Entry entry_update;
433
1/2
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
46 retval = map_.Lookup(inode_update, &entry_update);
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (!retval) {
435 PANIC(kLogSyslogErr | kLogDebug,
436 "invalid inode in page cache tracker: inode %" PRIu64
437 ", replacing %" PRIu64,
438 inode_update, inode);
439 }
440 46 entry_update.idx_stat = entry.idx_stat;
441
1/2
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
46 map_.Insert(inode_update, entry_update);
442 46 entry.idx_stat = -1;
443 }
444
1/2
✓ Branch 1 taken 107 times.
✗ Branch 2 not taken.
107 map_.Insert(inode, entry);
445
1/2
✓ Branch 1 taken 107 times.
✗ Branch 2 not taken.
107 }
446
447 30 PageCacheTracker::EvictRaii::EvictRaii(PageCacheTracker *t) : tracker_(t) {
448 30 const int retval = pthread_mutex_lock(tracker_->lock_);
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 assert(retval == 0);
450 30 }
451
452 30 PageCacheTracker::EvictRaii::~EvictRaii() {
453 30 const int retval = pthread_mutex_unlock(tracker_->lock_);
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 assert(retval == 0);
455 30 }
456
457 30 void PageCacheTracker::EvictRaii::Evict(uint64_t inode) {
458
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 if (!tracker_->is_active_)
459 15 return;
460
461 15 const bool contained_inode = tracker_->map_.Erase(inode);
462
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (contained_inode)
463 15 tracker_->statistics_.n_remove++;
464 }
465
466 } // namespace glue
467