GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/tube.h
Date: 2026-08-02 02:40:19
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 <cassert>
12 #include <cstddef>
13 #include <vector>
14
15 #include "util/atomic.h"
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 39609004 explicit Link(ItemT *item) : item_(item), next_(NULL), prev_(NULL) { }
46 3 ItemT *item() { return item_; }
47
48 private:
49 ItemT *item_;
50 Link *next_;
51 Link *prev_;
52 };
53
54 138671 Tube() : limit_(uint64_t(-1)), size_(0) { Init(); }
55 1989 explicit Tube(uint64_t limit) : limit_(limit), size_(0) { Init(); }
56 144205 ~Tube() {
57 144480 Link *cursor = head_;
58 do {
59 144519 Link *prev = cursor->prev_;
60
1/2
✓ Branch 0 taken 74560 times.
✗ Branch 1 not taken.
144519 delete cursor;
61 144519 cursor = prev;
62
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 74521 times.
144519 } while (cursor != head_);
63 144480 pthread_cond_destroy(&cond_populated_);
64 144480 pthread_cond_destroy(&cond_capacious_);
65 144480 pthread_cond_destroy(&cond_empty_);
66 144480 pthread_mutex_destroy(&lock_);
67 144480 }
68
69 /**
70 * Push an item to the back of the queue. Block if queue is currently full.
71 */
72 23764717 Link *EnqueueBack(ItemT *item) {
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13179813 times.
23764717 assert(item != NULL);
74 23764717 MutexLockGuard lock_guard(&lock_);
75
2/2
✓ Branch 0 taken 449832 times.
✓ Branch 1 taken 13185750 times.
24676143 while (size_ == limit_)
76
1/2
✓ Branch 1 taken 449836 times.
✗ Branch 2 not taken.
899664 pthread_cond_wait(&cond_capacious_, &lock_);
77
78
1/2
✓ Branch 1 taken 13188171 times.
✗ Branch 2 not taken.
23776479 Link *link = new Link(item);
79 23757325 link->next_ = head_->next_;
80 23757325 link->prev_ = head_;
81 23757325 head_->next_->prev_ = link;
82 23757325 head_->next_ = link;
83 23757325 size_++;
84 23757325 int retval = pthread_cond_signal(&cond_populated_);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13181384 times.
23767747 assert(retval == 0);
86 23772015 return link;
87 23767747 }
88
89 /**
90 * Push an item to the front of the queue. Block if queue currently full.
91 */
92 15697575 Link *EnqueueFront(ItemT *item) {
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15694882 times.
15697575 assert(item != NULL);
94 15697575 MutexLockGuard lock_guard(&lock_);
95
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 15694921 times.
15698004 while (size_ == limit_)
96
1/2
✓ Branch 1 taken 195 times.
✗ Branch 2 not taken.
390 pthread_cond_wait(&cond_capacious_, &lock_);
97
98
1/2
✓ Branch 1 taken 15694921 times.
✗ Branch 2 not taken.
15697614 Link *link = new Link(item);
99 15697536 link->next_ = head_;
100 15697536 link->prev_ = head_->prev_;
101 15697536 head_->prev_->next_ = link;
102 15697536 head_->prev_ = link;
103 15697536 size_++;
104 15697536 int retval = pthread_cond_signal(&cond_populated_);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15694804 times.
15697497 assert(retval == 0);
106 15697536 return link;
107 15697497 }
108
109 /**
110 * Remove any link from the queue and return its item, including first/last
111 * element.
112 */
113 40 ItemT *Slice(Link *link) {
114 40 MutexLockGuard lock_guard(&lock_);
115 40 return SliceUnlocked(link);
116 40 }
117
118 /**
119 * Remove and return the first element from the queue. Block if tube is
120 * empty.
121 */
122 38129656 ItemT *PopFront() {
123 38129656 MutexLockGuard lock_guard(&lock_);
124
2/2
✓ Branch 0 taken 4778043 times.
✓ Branch 1 taken 27556714 times.
47099293 while (size_ == 0)
125
1/2
✓ Branch 1 taken 4780385 times.
✗ Branch 2 not taken.
8947496 pthread_cond_wait(&cond_populated_, &lock_);
126 76274256 return SliceUnlocked(head_->prev_);
127 38134627 }
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 15688596 ItemT *TryPopFront() {
137 15688596 MutexLockGuard lock_guard(&lock_);
138 // Note that we don't need to wait for a signal to arrive
139
2/2
✓ Branch 0 taken 14366619 times.
✓ Branch 1 taken 1325058 times.
15691677 if (size_ == 0)
140 14366619 return NULL;
141 1325058 return SliceUnlocked(head_->prev_);
142 15691677 }
143
144 /**
145 * Remove and return the last element from the queue. Block if tube is
146 * empty.
147 */
148 5867 ItemT *PopBack() {
149 5867 MutexLockGuard lock_guard(&lock_);
150
2/2
✓ Branch 0 taken 624 times.
✓ Branch 1 taken 3174 times.
7115 while (size_ == 0)
151
1/2
✓ Branch 1 taken 624 times.
✗ Branch 2 not taken.
1248 pthread_cond_wait(&cond_populated_, &lock_);
152 11734 return SliceUnlocked(head_->next_);
153 5867 }
154
155 /**
156 * Blocks until the tube is empty
157 */
158 1487 void Wait() {
159 1487 MutexLockGuard lock_guard(&lock_);
160
2/2
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 1487 times.
1797 while (size_ > 0)
161
1/2
✓ Branch 1 taken 310 times.
✗ Branch 2 not taken.
310 pthread_cond_wait(&cond_empty_, &lock_);
162 1487 }
163
164 14417 bool IsEmpty() {
165 14417 MutexLockGuard lock_guard(&lock_);
166 28834 return size_ == 0;
167 14417 }
168
169 443 uint64_t size() {
170 443 MutexLockGuard lock_guard(&lock_);
171 886 return size_;
172 443 }
173
174 private:
175 141549 void Init() {
176 141549 Link *sentinel = new Link(NULL);
177 141549 head_ = sentinel;
178 141549 head_->next_ = head_->prev_ = sentinel;
179
180 141549 int retval = pthread_mutex_init(&lock_, NULL);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74597 times.
141549 assert(retval == 0);
182 141549 retval = pthread_cond_init(&cond_populated_, NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74597 times.
141549 assert(retval == 0);
184 141549 retval = pthread_cond_init(&cond_capacious_, NULL);
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74597 times.
141549 assert(retval == 0);
186 141549 retval = pthread_cond_init(&cond_empty_, NULL);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74597 times.
141549 assert(retval == 0);
188 141549 }
189
190 39466529 ItemT *SliceUnlocked(Link *link) {
191 // Cannot delete the sentinel link
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28875523 times.
39466529 assert(link != head_);
193 39466529 link->prev_->next_ = link->next_;
194 39466529 link->next_->prev_ = link->prev_;
195 39466529 ItemT *item = link->item_;
196
2/2
✓ Branch 0 taken 28867079 times.
✓ Branch 1 taken 8444 times.
39466529 delete link;
197 39482795 size_--;
198 39482795 int retval = pthread_cond_signal(&cond_capacious_);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28876833 times.
39469149 assert(retval == 0);
200
2/2
✓ Branch 0 taken 5596484 times.
✓ Branch 1 taken 23280349 times.
39469149 if (size_ == 0) {
201 9231047 retval = pthread_cond_broadcast(&cond_empty_);
202
2/2
✓ Branch 0 taken 461 times.
✓ Branch 1 taken 5596099 times.
9231199 assert(retval == 0);
203 }
204 39468379 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 12358 TubeGroup() : is_active_(false) { atomic_init32(&round_robin_); }
248
249 12347 ~TubeGroup() {
250
2/2
✓ Branch 1 taken 66132 times.
✓ Branch 2 taken 7595 times.
141245 for (unsigned i = 0; i < tubes_.size(); ++i)
251
1/2
✓ Branch 1 taken 66132 times.
✗ Branch 2 not taken.
128898 delete tubes_[i];
252 12347 }
253
254 129043 void TakeTube(Tube<ItemT> *t) {
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66205 times.
129043 assert(!is_active_);
256 129043 tubes_.push_back(t);
257 129043 }
258
259 12358 void Activate() {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7601 times.
12358 assert(!is_active_);
261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7601 times.
12358 assert(!tubes_.empty());
262 12358 is_active_ = true;
263 12358 }
264
265 /**
266 * Like Tube::EnqueueBack(), but pick a tube according to ItemT::tag()
267 */
268 8195443 typename Tube<ItemT>::Link *Dispatch(ItemT *item) {
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8195443 times.
8195443 assert(is_active_);
270
2/2
✓ Branch 1 taken 4856703 times.
✓ Branch 2 taken 3337748 times.
8195443 unsigned tube_idx = (tubes_.size() == 1) ? 0
271 4856703 : (item->tag() % tubes_.size());
272 8191383 return tubes_[tube_idx]->EnqueueBack(item);
273 }
274
275 /**
276 * Like Tube::EnqueueBack(), use tubes one after another
277 */
278 501379 typename Tube<ItemT>::Link *DispatchAny(ItemT *item) {
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 501379 times.
501379 assert(is_active_);
280
2/2
✓ Branch 1 taken 501301 times.
✓ Branch 2 taken 78 times.
501379 unsigned tube_idx = (tubes_.size() == 1)
281 ? 0
282 501301 : (atomic_xadd32(&round_robin_, 1) % tubes_.size());
283 501379 return tubes_[tube_idx]->EnqueueBack(item);
284 }
285
286 private:
287 bool is_active_;
288 std::vector<Tube<ItemT> *> tubes_;
289 atomic_int32 round_robin_;
290 };
291
292 #endif // CVMFS_UTIL_TUBE_H_
293