GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/tube.h
Date: 2026-11-08 02:41:18
Exec Total Coverage
Lines: 130 130 100.0%
Branches: 50 74 67.6%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_UTIL_TUBE_H_
6 #define CVMFS_UTIL_TUBE_H_
7
8 #include <pthread.h>
9 #include <stdint.h>
10
11 #include <atomic>
12 #include <cassert>
13 #include <cstddef>
14 #include <vector>
15
16 #include "util/mutex.h"
17 #include "util/single_copy.h"
18
19 /**
20 * A thread-safe, doubly linked list of links containing pointers to ItemT. The
21 * ItemT elements are not owned by the Tube. FIFO or LIFO semantics. Using
22 * Slice(), items at arbitrary locations in the tube can be removed, too.
23 *
24 *
25 * The layout of the linked list is as follows:
26 *
27 * --------------------------------------------------------------
28 * | |
29 * --> I$n$ (back) <--> I2 <--> ... <--> I1 (front) <--> HEAD <--
30 *
31 * The tube links the steps in the file processing pipeline. It connects
32 * multiple producers to multiple consumers and can throttle the producers if a
33 * limit for the tube size is set.
34 *
35 * Internally, uses conditional variables to block when threads try to pop from
36 * the empty tube or insert into the full tube.
37 */
38 template<class ItemT>
39 class Tube : SingleCopy {
40 public:
41 class Link : SingleCopy {
42 friend class Tube<ItemT>;
43
44 public:
45 53836341 explicit Link(ItemT *item) : item_(item), next_(NULL), prev_(NULL) { }
46 26 ItemT *item() { return item_; }
47
48 private:
49 ItemT *item_;
50 Link *next_;
51 Link *prev_;
52 };
53
54 84066 Tube() : limit_(uint64_t(-1)), size_(0) { Init(); }
55 1013 explicit Tube(uint64_t limit) : limit_(limit), size_(0) { Init(); }
56 87208 ~Tube() {
57 87325 Link *cursor = head_;
58 do {
59 87340 Link *prev = cursor->prev_;
60
1/2
✓ Branch 0 taken 44545 times.
✗ Branch 1 not taken.
87340 delete cursor;
61 87340 cursor = prev;
62
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 44530 times.
87340 } while (cursor != head_);
63 87325 pthread_cond_destroy(&cond_populated_);
64 87325 pthread_cond_destroy(&cond_capacious_);
65 87325 pthread_cond_destroy(&cond_empty_);
66 87325 pthread_mutex_destroy(&lock_);
67 87325 }
68
69 /**
70 * Push an item to the back of the queue. Block if queue is currently full.
71 */
72 47691014 Link *EnqueueBack(ItemT *item) {
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25980612 times.
47691014 assert(item != NULL);
74 47691014 MutexLockGuard lock_guard(&lock_);
75
2/2
✓ Branch 0 taken 1345026 times.
✓ Branch 1 taken 25992667 times.
50405056 while (size_ == limit_)
76
1/2
✓ Branch 1 taken 1344642 times.
✗ Branch 2 not taken.
2690052 pthread_cond_wait(&cond_capacious_, &lock_);
77
78
1/2
✓ Branch 1 taken 25999365 times.
✗ Branch 2 not taken.
47715004 Link *link = new Link(item);
79 47700518 link->next_ = head_->next_;
80 47700518 link->prev_ = head_;
81 47700518 head_->next_->prev_ = link;
82 47700518 head_->next_ = link;
83 47700518 size_++;
84 47700518 int retval = pthread_cond_signal(&cond_populated_);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26003952 times.
47737574 assert(retval == 0);
86 47737178 return link;
87 47737574 }
88
89 /**
90 * Push an item to the front of the queue. Block if queue currently full.
91 */
92 6036134 Link *EnqueueFront(ItemT *item) {
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6035087 times.
6036134 assert(item != NULL);
94 6036134 MutexLockGuard lock_guard(&lock_);
95
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 6035102 times.
6036209 while (size_ == limit_)
96
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
60 pthread_cond_wait(&cond_capacious_, &lock_);
97
98
1/2
✓ Branch 1 taken 6035102 times.
✗ Branch 2 not taken.
6036149 Link *link = new Link(item);
99 6036119 link->next_ = head_;
100 6036119 link->prev_ = head_->prev_;
101 6036119 head_->prev_->next_ = link;
102 6036119 head_->prev_ = link;
103 6036119 size_++;
104 6036119 int retval = pthread_cond_signal(&cond_populated_);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6035027 times.
6036074 assert(retval == 0);
106 6036119 return link;
107 6036074 }
108
109 /**
110 * Remove any link from the queue and return its item, including first/last
111 * element.
112 */
113 39 ItemT *Slice(Link *link) {
114 39 MutexLockGuard lock_guard(&lock_);
115 39 return SliceUnlocked(link);
116 39 }
117
118 /**
119 * Remove and return the first element from the queue. Block if tube is
120 * empty.
121 */
122 53259132 ItemT *PopFront() {
123 53259132 MutexLockGuard lock_guard(&lock_);
124
2/2
✓ Branch 0 taken 10988732 times.
✓ Branch 1 taken 31531744 times.
73399900 while (size_ == 0)
125
1/2
✓ Branch 1 taken 10988020 times.
✗ Branch 2 not taken.
20131076 pthread_cond_wait(&cond_populated_, &lock_);
126 106536895 return SliceUnlocked(head_->prev_);
127 53239083 }
128
129 /**
130 * Remove and return the first element from the queue if there is any.
131 * Equivalent to an antomic
132 * ItemT item = NULL;
133 * if (!IsEmpty())
134 * item = PopFront();
135 */
136 6031945 ItemT *TryPopFront() {
137 6031945 MutexLockGuard lock_guard(&lock_);
138 // Note that we don't need to wait for a signal to arrive
139
2/2
✓ Branch 0 taken 5524412 times.
✓ Branch 1 taken 509513 times.
6033925 if (size_ == 0)
140 5524412 return NULL;
141 509513 return SliceUnlocked(head_->prev_);
142 6033925 }
143
144 /**
145 * Remove and return the last element from the queue. Block if tube is
146 * empty.
147 */
148 2263 ItemT *PopBack() {
149 2263 MutexLockGuard lock_guard(&lock_);
150
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 1216 times.
2863 while (size_ == 0)
151
1/2
✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
600 pthread_cond_wait(&cond_populated_, &lock_);
152 4526 return SliceUnlocked(head_->next_);
153 2263 }
154
155 /**
156 * Blocks until the tube is empty
157 */
158 879 void Wait() {
159 879 MutexLockGuard lock_guard(&lock_);
160
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 879 times.
1045 while (size_ > 0)
161
1/2
✓ Branch 1 taken 166 times.
✗ Branch 2 not taken.
166 pthread_cond_wait(&cond_empty_, &lock_);
162 879 }
163
164 4688 bool IsEmpty() {
165 4688 MutexLockGuard lock_guard(&lock_);
166 9376 return size_ == 0;
167 4688 }
168
169 351 uint64_t size() {
170 351 MutexLockGuard lock_guard(&lock_);
171 702 return size_;
172 351 }
173
174 private:
175 85624 void Init() {
176 85624 Link *sentinel = new Link(NULL);
177 85624 head_ = sentinel;
178 85624 head_->next_ = head_->prev_ = sentinel;
179
180 85624 int retval = pthread_mutex_init(&lock_, NULL);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44606 times.
85624 assert(retval == 0);
182 85624 retval = pthread_cond_init(&cond_populated_, NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44606 times.
85624 assert(retval == 0);
184 85624 retval = pthread_cond_init(&cond_capacious_, NULL);
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44606 times.
85624 assert(retval == 0);
186 85624 retval = pthread_cond_init(&cond_empty_, NULL);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44606 times.
85624 assert(retval == 0);
188 85624 }
189
190 53764816 ItemT *SliceUnlocked(Link *link) {
191 // Cannot delete the sentinel link
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32034077 times.
53764816 assert(link != head_);
193 53764816 link->prev_->next_ = link->next_;
194 53764816 link->next_->prev_ = link->prev_;
195 53764816 ItemT *item = link->item_;
196
2/2
✓ Branch 0 taken 32007231 times.
✓ Branch 1 taken 26846 times.
53764816 delete link;
197 53813514 size_--;
198 53813514 int retval = pthread_cond_signal(&cond_capacious_);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32032266 times.
53761211 assert(retval == 0);
200
2/2
✓ Branch 0 taken 11471729 times.
✓ Branch 1 taken 20560537 times.
53761211 if (size_ == 0) {
201 20483587 retval = pthread_cond_broadcast(&cond_empty_);
202
2/2
✓ Branch 0 taken 567 times.
✓ Branch 1 taken 11471051 times.
20483365 assert(retval == 0);
203 }
204 53759855 return item;
205 }
206
207
208 /**
209 * Adding new item blocks as long as limit_ == size_
210 */
211 uint64_t limit_;
212 /**
213 * The current number of links in the list
214 */
215 uint64_t size_;
216 /**
217 * Sentinel element in front of the first (front) element
218 */
219 Link *head_;
220 /**
221 * Protects all internal state
222 */
223 pthread_mutex_t lock_;
224 /**
225 * Signals if there are items enqueued
226 */
227 pthread_cond_t cond_populated_;
228 /**
229 * Signals if there is space to enqueue more items
230 */
231 pthread_cond_t cond_capacious_;
232 /**
233 * Signals if the queue runs empty
234 */
235 pthread_cond_t cond_empty_;
236 };
237
238
239 /**
240 * A tube group manages a fixed set of Tubes and dispatches items among them in
241 * such a way that items with the same tag (a positive integer) are all sent
242 * to the same tube.
243 */
244 template<class ItemT>
245 class TubeGroup : SingleCopy {
246 public:
247 7381 TubeGroup() : is_active_(false) { round_robin_.store(0); }
248
249 7370 ~TubeGroup() {
250
2/2
✓ Branch 1 taken 40825 times.
✓ Branch 2 taken 4546 times.
86973 for (unsigned i = 0; i < tubes_.size(); ++i)
251
1/2
✓ Branch 1 taken 40825 times.
✗ Branch 2 not taken.
79603 delete tubes_[i];
252 7370 }
253
254 79748 void TakeTube(Tube<ItemT> *t) {
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40898 times.
79748 assert(!is_active_);
256 79748 tubes_.push_back(t);
257 79748 }
258
259 7381 void Activate() {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4552 times.
7381 assert(!is_active_);
261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4552 times.
7381 assert(!tubes_.empty());
262 7381 is_active_ = true;
263 7381 }
264
265 /**
266 * Like Tube::EnqueueBack(), but pick a tube according to ItemT::tag()
267 */
268 18971741 typename Tube<ItemT>::Link *Dispatch(ItemT *item) {
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18971741 times.
18971741 assert(is_active_);
270
2/2
✓ Branch 1 taken 14458399 times.
✓ Branch 2 taken 4508014 times.
18971741 unsigned tube_idx = (tubes_.size() == 1) ? 0
271 14458399 : (item->tag() % tubes_.size());
272 18951797 return tubes_[tube_idx]->EnqueueBack(item);
273 }
274
275 /**
276 * Like Tube::EnqueueBack(), use tubes one after another
277 */
278 1501030 typename Tube<ItemT>::Link *DispatchAny(ItemT *item) {
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1501030 times.
1501030 assert(is_active_);
280
2/2
✓ Branch 1 taken 1501004 times.
✓ Branch 2 taken 26 times.
1501030 unsigned tube_idx = (tubes_.size() == 1)
281 ? 0
282 3002008 : (round_robin_.fetch_add(1) % tubes_.size());
283 1501030 return tubes_[tube_idx]->EnqueueBack(item);
284 }
285
286 private:
287 bool is_active_;
288 std::vector<Tube<ItemT> *> tubes_;
289 std::atomic<int32_t> round_robin_;
290 };
291
292 #endif // CVMFS_UTIL_TUBE_H_
293