GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/tube.h
Date: 2026-02-08 02:36:20
Exec Total Coverage
Lines: 129 130 99.2%
Branches: 48 74 64.9%

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 247425236 explicit Link(ItemT *item) : item_(item), next_(NULL), prev_(NULL) { }
46 32 ItemT *item() { return item_; }
47
48 private:
49 ItemT *item_;
50 Link *next_;
51 Link *prev_;
52 };
53
54 220015 Tube() : limit_(uint64_t(-1)), size_(0) { Init(); }
55 2455 explicit Tube(uint64_t limit) : limit_(limit), size_(0) { Init(); }
56 228108 ~Tube() {
57 228367 Link *cursor = head_;
58 do {
59 228416 Link *prev = cursor->prev_;
60
1/2
✓ Branch 0 taken 117143 times.
✗ Branch 1 not taken.
228416 delete cursor;
61 228416 cursor = prev;
62
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 117094 times.
228416 } while (cursor != head_);
63 228367 pthread_cond_destroy(&cond_populated_);
64 228367 pthread_cond_destroy(&cond_capacious_);
65 228367 pthread_cond_destroy(&cond_empty_);
66 228367 pthread_mutex_destroy(&lock_);
67 228367 }
68
69 /**
70 * Push an item to the back of the queue. Block if queue is currently full.
71 */
72 228466963 Link *EnqueueBack(ItemT *item) {
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124343049 times.
228466963 assert(item != NULL);
74 228466963 MutexLockGuard lock_guard(&lock_);
75
2/2
✓ Branch 0 taken 5687160 times.
✓ Branch 1 taken 124274507 times.
239696369 while (size_ == limit_)
76
1/2
✓ Branch 1 taken 5702160 times.
✗ Branch 2 not taken.
11374320 pthread_cond_wait(&cond_capacious_, &lock_);
77
78
1/2
✓ Branch 1 taken 124314103 times.
✗ Branch 2 not taken.
228322049 Link *link = new Link(item);
79 226022697 link->next_ = head_->next_;
80 226022697 link->prev_ = head_;
81 226022697 head_->next_->prev_ = link;
82 226022697 head_->next_ = link;
83 226022697 size_++;
84 226022697 int retval = pthread_cond_signal(&cond_populated_);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124557241 times.
228887517 assert(retval == 0);
86 227664211 return link;
87 228887517 }
88
89 /**
90 * Push an item to the front of the queue. Block if queue currently full.
91 */
92 19721383 Link *EnqueueFront(ItemT *item) {
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19719078 times.
19721383 assert(item != NULL);
94 19721383 MutexLockGuard lock_guard(&lock_);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19719029 times.
19721334 while (size_ == limit_)
96 pthread_cond_wait(&cond_capacious_, &lock_);
97
98
1/2
✓ Branch 1 taken 19719029 times.
✗ Branch 2 not taken.
19721334 Link *link = new Link(item);
99 19721334 link->next_ = head_;
100 19721334 link->prev_ = head_->prev_;
101 19721334 head_->prev_->next_ = link;
102 19721334 head_->prev_ = link;
103 19721334 size_++;
104 19721334 int retval = pthread_cond_signal(&cond_populated_);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19719029 times.
19721334 assert(retval == 0);
106 19721383 return link;
107 19721334 }
108
109 /**
110 * Remove any link from the queue and return its item, including first/last
111 * element.
112 */
113 60 ItemT *Slice(Link *link) {
114 60 MutexLockGuard lock_guard(&lock_);
115 60 return SliceUnlocked(link);
116 60 }
117
118 /**
119 * Remove and return the first element from the queue. Block if tube is
120 * empty.
121 */
122 245318723 ItemT *PopFront() {
123 245318723 MutexLockGuard lock_guard(&lock_);
124
2/2
✓ Branch 0 taken 45464339 times.
✓ Branch 1 taken 142450250 times.
335938008 while (size_ == 0)
125
1/2
✓ Branch 1 taken 45893359 times.
✗ Branch 2 not taken.
89315905 pthread_cond_wait(&cond_populated_, &lock_);
126 490725371 return SliceUnlocked(head_->prev_);
127 243923170 }
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 19709291 ItemT *TryPopFront() {
137 19709291 MutexLockGuard lock_guard(&lock_);
138 // Note that we don't need to wait for a signal to arrive
139
2/2
✓ Branch 0 taken 18051560 times.
✓ Branch 1 taken 1664934 times.
19716494 if (size_ == 0)
140 18051560 return NULL;
141 1664934 return SliceUnlocked(head_->prev_);
142 19716494 }
143
144 /**
145 * Remove and return the last element from the queue. Block if tube is
146 * empty.
147 */
148 4974 ItemT *PopBack() {
149 4974 MutexLockGuard lock_guard(&lock_);
150
2/2
✓ Branch 0 taken 627 times.
✓ Branch 1 taken 2669 times.
6228 while (size_ == 0)
151
1/2
✓ Branch 1 taken 627 times.
✗ Branch 2 not taken.
1254 pthread_cond_wait(&cond_populated_, &lock_);
152 9948 return SliceUnlocked(head_->next_);
153 4974 }
154
155 /**
156 * Blocks until the tube is empty
157 */
158 2164 void Wait() {
159 2164 MutexLockGuard lock_guard(&lock_);
160
2/2
✓ Branch 0 taken 698 times.
✓ Branch 1 taken 2164 times.
2862 while (size_ > 0)
161
1/2
✓ Branch 1 taken 698 times.
✗ Branch 2 not taken.
698 pthread_cond_wait(&cond_empty_, &lock_);
162 2164 }
163
164 17989 bool IsEmpty() {
165 17989 MutexLockGuard lock_guard(&lock_);
166 35978 return size_ == 0;
167 17989 }
168
169 588 uint64_t size() {
170 588 MutexLockGuard lock_guard(&lock_);
171 1176 return size_;
172 588 }
173
174 private:
175 223889 void Init() {
176 223889 Link *sentinel = new Link(NULL);
177 223889 head_ = sentinel;
178 223889 head_->next_ = head_->prev_ = sentinel;
179
180 223889 int retval = pthread_mutex_init(&lock_, NULL);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117170 times.
223889 assert(retval == 0);
182 223889 retval = pthread_cond_init(&cond_populated_, NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117170 times.
223889 assert(retval == 0);
184 223889 retval = pthread_cond_init(&cond_capacious_, NULL);
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117170 times.
223889 assert(retval == 0);
186 223889 retval = pthread_cond_init(&cond_empty_, NULL);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117170 times.
223889 assert(retval == 0);
188 223889 }
189
190 246722084 ItemT *SliceUnlocked(Link *link) {
191 // Cannot delete the sentinel link
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143331767 times.
246722084 assert(link != head_);
193 246722084 link->prev_->next_ = link->next_;
194 246722084 link->next_->prev_ = link->prev_;
195 246722084 ItemT *item = link->item_;
196
2/2
✓ Branch 0 taken 142958906 times.
✓ Branch 1 taken 372861 times.
246722084 delete link;
197 248168376 size_--;
198 248168376 int retval = pthread_cond_signal(&cond_capacious_);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143204555 times.
246467628 assert(retval == 0);
200
2/2
✓ Branch 0 taken 51808641 times.
✓ Branch 1 taken 91395914 times.
246467628 if (size_ == 0) {
201 99628030 retval = pthread_cond_broadcast(&cond_empty_);
202
2/2
✓ Branch 0 taken 42765 times.
✓ Branch 1 taken 51765859 times.
99628045 assert(retval == 0);
203 }
204 246382113 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 18683 TubeGroup() : is_active_(false) { atomic_init32(&round_robin_); }
248
249 18672 ~TubeGroup() {
250
2/2
✓ Branch 1 taken 106120 times.
✓ Branch 2 taken 11326 times.
225928 for (unsigned i = 0; i < tubes_.size(); ++i)
251
1/2
✓ Branch 1 taken 106120 times.
✗ Branch 2 not taken.
207256 delete tubes_[i];
252 18672 }
253
254 207401 void TakeTube(Tube<ItemT> *t) {
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106193 times.
207401 assert(!is_active_);
256 207401 tubes_.push_back(t);
257 207401 }
258
259 18683 void Activate() {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11332 times.
18683 assert(!is_active_);
261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11332 times.
18683 assert(!tubes_.empty());
262 18683 is_active_ = true;
263 18683 }
264
265 /**
266 * Like Tube::EnqueueBack(), but pick a tube according to ItemT::tag()
267 */
268 92280253 typename Tube<ItemT>::Link *Dispatch(ItemT *item) {
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92280253 times.
92280253 assert(is_active_);
270
2/2
✓ Branch 1 taken 71498095 times.
✓ Branch 2 taken 20781738 times.
92280253 unsigned tube_idx = (tubes_.size() == 1) ? 0
271 71498095 : (item->tag() % tubes_.size());
272 91940263 return tubes_[tube_idx]->EnqueueBack(item);
273 }
274
275 /**
276 * Like Tube::EnqueueBack(), use tubes one after another
277 */
278 7502575 typename Tube<ItemT>::Link *DispatchAny(ItemT *item) {
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7502575 times.
7502575 assert(is_active_);
280
2/2
✓ Branch 1 taken 7502511 times.
✓ Branch 2 taken 64 times.
7502575 unsigned tube_idx = (tubes_.size() == 1)
281 ? 0
282 7502511 : (atomic_xadd32(&round_robin_, 1) % tubes_.size());
283 7502575 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