GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/glue_buffer.cc
Date: 2025-06-29 02:35:41
Exec Total Coverage
Lines: 203 275 73.8%
Branches: 77 184 41.8%

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 902 void InodeTracker::InitLock() {
62 902 lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
63 902 const int retval = pthread_mutex_init(lock_, NULL);
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 902 times.
902 assert(retval == 0);
65 902 }
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 902 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 902 times.
✗ Branch 6 not taken.
902 InodeTracker::InodeTracker() {
79 902 version_ = kVersion;
80 902 InitLock();
81 902 }
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 900 InodeTracker::~InodeTracker() {
100 900 pthread_mutex_destroy(lock_);
101 900 free(lock_);
102 900 }
103
104
105 //------------------------------------------------------------------------------
106
107 546 DentryTracker::DentryTracker() : version_(kVersion), is_active_(true) {
108 546 pipe_terminate_[0] = pipe_terminate_[1] = -1;
109 546 cleaning_interval_ms_ = -1;
110 546 InitLock();
111 546 }
112
113
114 684 DentryTracker::~DentryTracker() {
115
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 640 times.
684 if (pipe_terminate_[1] >= 0) {
116 44 char t = 'T';
117 44 WritePipe(pipe_terminate_[1], &t, 1);
118 44 pthread_join(thread_cleaner_, NULL);
119 44 ClosePipe(pipe_terminate_);
120 }
121 684 pthread_mutex_destroy(lock_);
122 684 free(lock_);
123 684 }
124
125
126 182 DentryTracker::DentryTracker(const DentryTracker &other) {
127
1/2
✓ Branch 1 taken 182 times.
✗ Branch 2 not taken.
182 CopyFrom(other);
128 182 pipe_terminate_[0] = pipe_terminate_[1] = -1;
129 182 cleaning_interval_ms_ = -1;
130 182 InitLock();
131 182 }
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 182 void DentryTracker::CopyFrom(const DentryTracker &other) {
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
182 assert(other.version_ == kVersion);
147
148 182 version_ = kVersion;
149 182 statistics_ = other.statistics_;
150 182 is_active_ = other.is_active_;
151 182 entries_ = other.entries_;
152 182 }
153
154
155 182 DentryTracker *DentryTracker::Move() {
156 182 Lock();
157
1/2
✓ Branch 2 taken 182 times.
✗ Branch 3 not taken.
182 DentryTracker *new_tracker = new DentryTracker(*this);
158 182 statistics_.num_remove += entries_.size();
159 182 entries_.Clear();
160 182 Unlock();
161 182 return new_tracker;
162 }
163
164
165 44 void DentryTracker::SpawnCleaner(unsigned interval_s) {
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 assert(pipe_terminate_[0] == -1);
167 44 cleaning_interval_ms_ = interval_s * 1000;
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (cleaning_interval_ms_ == 0)
169 cleaning_interval_ms_ = -1;
170 44 MakePipe(pipe_terminate_);
171 44 const int retval = pthread_create(&thread_cleaner_, NULL, MainCleaner, this);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 assert(retval == 0);
173 44 }
174
175
176 44 void *DentryTracker::MainCleaner(void *data) {
177 44 DentryTracker *tracker = reinterpret_cast<DentryTracker *>(data);
178
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 LogCvmfs(kLogCvmfs, kLogDebug, "starting negative entry cache cleaner");
179
180 struct pollfd watch_term;
181 44 watch_term.fd = tracker->pipe_terminate_[0];
182 44 watch_term.events = POLLIN | POLLPRI;
183 44 int timeout_ms = tracker->cleaning_interval_ms_;
184 ;
185 44 uint64_t deadline = platform_monotonic_time() + timeout_ms / 1000;
186 while (true) {
187 176 watch_term.revents = 0;
188
1/2
✓ Branch 1 taken 176 times.
✗ Branch 2 not taken.
176 const int retval = poll(&watch_term, 1, timeout_ms);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
176 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 132 continue;
196 }
197 abort();
198 }
199 176 timeout_ms = tracker->cleaning_interval_ms_;
200 176 deadline = platform_monotonic_time() + timeout_ms / 1000;
201
202
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 44 times.
176 if (retval == 0) {
203
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 LogCvmfs(kLogCvmfs, kLogDebug, "negative entry cleaner: pruning");
204
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 tracker->Prune();
205 132 continue;
206 }
207
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 assert(watch_term.revents != 0);
209
210 44 char c = 0;
211
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 ReadPipe(tracker->pipe_terminate_[0], &c, 1);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 assert(c == 'T');
213 44 break;
214 132 }
215
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 LogCvmfs(kLogCvmfs, kLogDebug, "stopping negative entry cache cleaner");
216 44 return NULL;
217 }
218
219
220 728 void DentryTracker::InitLock() {
221 728 lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
222 728 const int retval = pthread_mutex_init(lock_, NULL);
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
728 assert(retval == 0);
224 728 }
225
226
227 314 void DentryTracker::Prune() {
228 314 Lock();
229 314 DoPrune(platform_monotonic_time());
230 314 Unlock();
231 314 }
232
233
234 446 DentryTracker::Cursor DentryTracker::BeginEnumerate() {
235 446 Entry *head = NULL;
236 446 Lock();
237
1/2
✓ Branch 1 taken 446 times.
✗ Branch 2 not taken.
446 entries_.Peek(&head);
238 446 return Cursor(head);
239 }
240
241
242 59412 bool DentryTracker::NextEntry(Cursor *cursor, uint64_t *inode_parent,
243 NameString *name) {
244
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 59146 times.
59412 if (cursor->head == NULL)
245 266 return false;
246
2/2
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 59012 times.
59146 if (cursor->pos >= entries_.size())
247 134 return false;
248 59012 Entry *e = cursor->head + cursor->pos;
249 59012 *inode_parent = e->inode_parent;
250 59012 *name = e->name;
251 59012 cursor->pos++;
252 59012 return true;
253 }
254
255
256 446 void DentryTracker::EndEnumerate(Cursor * /* cursor */) { Unlock(); }
257
258
259 //------------------------------------------------------------------------------
260
261
262
1/2
✓ Branch 3 taken 410 times.
✗ Branch 4 not taken.
410 PageCacheTracker::PageCacheTracker() : version_(kVersion), is_active_(true) {
263
1/2
✓ Branch 1 taken 410 times.
✗ Branch 2 not taken.
410 map_.Init(16, 0, hasher_inode);
264 410 InitLock();
265 410 }
266
267
268 408 PageCacheTracker::~PageCacheTracker() {
269 408 pthread_mutex_destroy(lock_);
270 408 free(lock_);
271 408 }
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 410 void PageCacheTracker::InitLock() {
304 410 lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
305 410 const int retval = pthread_mutex_init(lock_, NULL);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410 times.
410 assert(retval == 0);
307 410 }
308
309 446 PageCacheTracker::OpenDirectives PageCacheTracker::Open(
310 uint64_t inode, const shash::Any &hash, const struct stat &info) {
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446 times.
446 assert(inode == info.st_ino);
312
313 446 OpenDirectives open_directives;
314 // Old behavior: always flush page cache on open
315
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 402 times.
446 if (!is_active_)
316 44 return open_directives;
317
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 402 times.
402 if (inode != info.st_ino) {
319 PANIC(kLogStderr | kLogDebug,
320 "invalid entry on open: %" PRIu64 " with st_ino=%" PRIu64,
321 " hash=%s size=%" PRIu64, inode, info.st_ino, hash.ToString().c_str(),
322 info.st_size);
323 }
324
325 402 const MutexLockGuard guard(lock_);
326
327
1/2
✓ Branch 1 taken 402 times.
✗ Branch 2 not taken.
402 Entry entry;
328
1/2
✓ Branch 1 taken 402 times.
✗ Branch 2 not taken.
402 const bool retval = map_.Lookup(inode, &entry);
329
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 268 times.
402 if (!retval) {
330 134 open_directives.keep_cache = true;
331 134 open_directives.direct_io = false;
332 134 statistics_.n_insert++;
333 134 statistics_.n_open_cached++;
334
335 134 entry.nopen = 1;
336
1/2
✓ Branch 1 taken 134 times.
✗ Branch 2 not taken.
134 entry.idx_stat = stat_store_.Add(info);
337 134 entry.hash = hash;
338
1/2
✓ Branch 1 taken 134 times.
✗ Branch 2 not taken.
134 map_.Insert(inode, entry);
339 134 return open_directives;
340 }
341
342
2/2
✓ Branch 1 taken 178 times.
✓ Branch 2 taken 90 times.
268 if (entry.hash == hash) {
343 178 open_directives.direct_io = false;
344
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 134 times.
178 if (entry.nopen < 0) {
345 // The page cache is still in the transition phase and may contain old
346 // content. So trigger a flush of the cache in any case.
347 44 open_directives.keep_cache = false;
348 44 statistics_.n_open_flush++;
349 44 entry.nopen--;
350
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 map_.Insert(inode, entry);
351 44 return open_directives;
352 } else {
353 134 open_directives.keep_cache = true;
354 134 statistics_.n_open_cached++;
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
134 if (entry.nopen++ == 0)
356 entry.idx_stat = stat_store_.Add(info);
357
1/2
✓ Branch 1 taken 134 times.
✗ Branch 2 not taken.
134 map_.Insert(inode, entry);
358 134 return open_directives;
359 }
360 }
361
362 // Page cache mismatch and old data has still open file attached to it,
363 // circumvent the page cache entirely and use direct I/O. In this case,
364 // cvmfs_close() will _not_ call Close().
365
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 44 times.
90 if (entry.nopen != 0) {
366 46 open_directives.keep_cache = true;
367 46 open_directives.direct_io = true;
368 46 statistics_.n_open_direct++;
369 46 return open_directives;
370 }
371
372 // Stale data in the page cache, start the transition phase in which newly
373 // opened files flush the page cache and re-populate it with the new hash.
374 // The first file to reach Close() will finish the transition phase and
375 // mark the new hash as committed.
376 44 open_directives.direct_io = false;
377 44 open_directives.keep_cache = false;
378 44 statistics_.n_open_flush++;
379 44 entry.hash = hash;
380
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 entry.idx_stat = stat_store_.Add(info);
381 44 entry.nopen = -1;
382
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 map_.Insert(inode, entry);
383 44 return open_directives;
384 402 }
385
386 PageCacheTracker::OpenDirectives PageCacheTracker::OpenDirect() {
387 OpenDirectives open_directives(true, true);
388 // Old behavior: always flush page cache on open
389 if (!is_active_)
390 return open_directives;
391
392 const MutexLockGuard guard(lock_);
393 statistics_.n_open_direct++;
394 return open_directives;
395 }
396
397 354 void PageCacheTracker::Close(uint64_t inode) {
398
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 310 times.
354 if (!is_active_)
399 44 return;
400
401 310 const MutexLockGuard guard(lock_);
402
1/2
✓ Branch 1 taken 310 times.
✗ Branch 2 not taken.
310 Entry entry;
403
1/2
✓ Branch 1 taken 310 times.
✗ Branch 2 not taken.
310 bool retval = map_.Lookup(inode, &entry);
404
405 310 if (!AssertOrLog(retval, kLogCvmfs, kLogSyslogWarn | kLogDebug,
406 "PageCacheTracker::Close Race condition? "
407 "Did not find inode %lu",
408 inode)
409
3/6
✓ Branch 0 taken 310 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 310 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 310 times.
310 || !AssertOrLog(entry.nopen != 0, kLogCvmfs, kLogSyslogWarn | kLogDebug,
410 "PageCacheTracker::Close Race condition? "
411 "Inode %lu has no open entries",
412 inode)) {
413 return;
414 }
415
416 310 const int32_t old_open = entry.nopen;
417
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 266 times.
310 if (entry.nopen < 0) {
418 // At this point we know that any stale data has been flushed from the
419 // cache and only data related to the currently booked content hash
420 // can be present. So clear the transition bit (sign bit).
421 44 entry.nopen = -entry.nopen;
422 }
423 310 entry.nopen--;
424
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 177 times.
310 if (entry.nopen == 0) {
425 // File closed, remove struct stat information
426
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (entry.idx_stat < 0) {
427 PANIC(kLogSyslogErr | kLogDebug,
428 "page cache tracker: missing stat entry! Entry info: inode %" PRIu64
429 " - open counter %d - hash %s",
430 inode, old_open, entry.hash.ToString().c_str());
431 }
432
1/2
✓ Branch 1 taken 133 times.
✗ Branch 2 not taken.
133 const uint64_t inode_update = stat_store_.Erase(entry.idx_stat);
433
1/2
✓ Branch 1 taken 133 times.
✗ Branch 2 not taken.
133 Entry entry_update;
434
1/2
✓ Branch 1 taken 133 times.
✗ Branch 2 not taken.
133 retval = map_.Lookup(inode_update, &entry_update);
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (!retval) {
436 PANIC(kLogSyslogErr | kLogDebug,
437 "invalid inode in page cache tracker: inode %" PRIu64
438 ", replacing %" PRIu64,
439 inode_update, inode);
440 }
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 assert(retval);
442 133 entry_update.idx_stat = entry.idx_stat;
443
1/2
✓ Branch 1 taken 133 times.
✗ Branch 2 not taken.
133 map_.Insert(inode_update, entry_update);
444 133 entry.idx_stat = -1;
445 }
446
1/2
✓ Branch 1 taken 310 times.
✗ Branch 2 not taken.
310 map_.Insert(inode, entry);
447
1/2
✓ Branch 1 taken 310 times.
✗ Branch 2 not taken.
310 }
448
449 88 PageCacheTracker::EvictRaii::EvictRaii(PageCacheTracker *t) : tracker_(t) {
450 88 const int retval = pthread_mutex_lock(tracker_->lock_);
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 assert(retval == 0);
452 88 }
453
454 88 PageCacheTracker::EvictRaii::~EvictRaii() {
455 88 const int retval = pthread_mutex_unlock(tracker_->lock_);
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 assert(retval == 0);
457 88 }
458
459 88 void PageCacheTracker::EvictRaii::Evict(uint64_t inode) {
460
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
88 if (!tracker_->is_active_)
461 44 return;
462
463 44 const bool contained_inode = tracker_->map_.Erase(inode);
464
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (contained_inode)
465 44 tracker_->statistics_.n_remove++;
466 }
467
468 } // namespace glue
469