GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/tube.h
Date: 2025-07-27 02:42:09
Exec Total Coverage
Lines: 130 130 100.0%
Branches: 49 74 66.2%

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 134573719 explicit Link(ItemT *item) : item_(item), next_(NULL), prev_(NULL) { }
46 1 ItemT *item() { return item_; }
47
48 private:
49 ItemT *item_;
50 Link *next_;
51 Link *prev_;
52 };
53
54 110542 Tube() : limit_(uint64_t(-1)), size_(0) { Init(); }
55 745 explicit Tube(uint64_t limit) : limit_(limit), size_(0) { Init(); }
56 113305 ~Tube() {
57 113315 Link *cursor = head_;
58 do {
59 113357 Link *prev = cursor->prev_;
60
1/2
✓ Branch 0 taken 59070 times.
✗ Branch 1 not taken.
113357 delete cursor;
61 113357 cursor = prev;
62
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 59028 times.
113357 } while (cursor != head_);
63 113315 pthread_cond_destroy(&cond_populated_);
64 113315 pthread_cond_destroy(&cond_capacious_);
65 113315 pthread_cond_destroy(&cond_empty_);
66 113315 pthread_mutex_destroy(&lock_);
67 113315 }
68
69 /**
70 * Push an item to the back of the queue. Block if queue is currently full.
71 */
72 117834907 Link *EnqueueBack(ItemT *item) {
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64030634 times.
117834907 assert(item != NULL);
74 117834907 MutexLockGuard lock_guard(&lock_);
75
2/2
✓ Branch 0 taken 3021200 times.
✓ Branch 1 taken 64130171 times.
124074045 while (size_ == limit_)
76
1/2
✓ Branch 1 taken 3028544 times.
✗ Branch 2 not taken.
6042400 pthread_cond_wait(&cond_capacious_, &lock_);
77
78
1/2
✓ Branch 1 taken 64165782 times.
✗ Branch 2 not taken.
118031645 Link *link = new Link(item);
79 116754961 link->next_ = head_->next_;
80 116754961 link->prev_ = head_;
81 116754961 head_->next_->prev_ = link;
82 116754961 head_->next_ = link;
83 116754961 size_++;
84 116754961 int retval = pthread_cond_signal(&cond_populated_);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64312901 times.
118397105 assert(retval == 0);
86 117595743 return link;
87 118397105 }
88
89 /**
90 * Push an item to the front of the queue. Block if queue currently full.
91 */
92 16901164 Link *EnqueueFront(ItemT *item) {
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16901092 times.
16901164 assert(item != NULL);
94 16901164 MutexLockGuard lock_guard(&lock_);
95
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16901134 times.
16901214 while (size_ == limit_)
96
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 pthread_cond_wait(&cond_capacious_, &lock_);
97
98
1/2
✓ Branch 1 taken 16901134 times.
✗ Branch 2 not taken.
16901206 Link *link = new Link(item);
99 16901080 link->next_ = head_;
100 16901080 link->prev_ = head_->prev_;
101 16901080 head_->prev_->next_ = link;
102 16901080 head_->prev_ = link;
103 16901080 size_++;
104 16901080 int retval = pthread_cond_signal(&cond_populated_);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16900840 times.
16900912 assert(retval == 0);
106 16900996 return link;
107 16900912 }
108
109 /**
110 * Remove any link from the queue and return its item, including first/last
111 * element.
112 */
113 20 ItemT *Slice(Link *link) {
114 20 MutexLockGuard lock_guard(&lock_);
115 20 return SliceUnlocked(link);
116 20 }
117
118 /**
119 * Remove and return the first element from the queue. Block if tube is
120 * empty.
121 */
122 132984156 ItemT *PopFront() {
123 132984156 MutexLockGuard lock_guard(&lock_);
124
2/2
✓ Branch 0 taken 23307040 times.
✓ Branch 1 taken 79744763 times.
179761892 while (size_ == 0)
125
1/2
✓ Branch 1 taken 23531401 times.
✗ Branch 2 not taken.
45974805 pthread_cond_wait(&cond_populated_, &lock_);
126 266073155 return SliceUnlocked(head_->prev_);
127 132393751 }
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 16891222 ItemT *TryPopFront() {
137 16891222 MutexLockGuard lock_guard(&lock_);
138 // Note that we don't need to wait for a signal to arrive
139
2/2
✓ Branch 0 taken 15473745 times.
✓ Branch 1 taken 1427137 times.
16900882 if (size_ == 0)
140 15473745 return NULL;
141 1427137 return SliceUnlocked(head_->prev_);
142 16900882 }
143
144 /**
145 * Remove and return the last element from the queue. Block if tube is
146 * empty.
147 */
148 391 ItemT *PopBack() {
149 391 MutexLockGuard lock_guard(&lock_);
150
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 319 times.
423 while (size_ == 0)
151
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
32 pthread_cond_wait(&cond_populated_, &lock_);
152 782 return SliceUnlocked(head_->next_);
153 391 }
154
155 /**
156 * Blocks until the tube is empty
157 */
158 1151 void Wait() {
159 1151 MutexLockGuard lock_guard(&lock_);
160
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 1151 times.
1459 while (size_ > 0)
161
1/2
✓ Branch 1 taken 308 times.
✗ Branch 2 not taken.
308 pthread_cond_wait(&cond_empty_, &lock_);
162 1151 }
163
164 14565 bool IsEmpty() {
165 14565 MutexLockGuard lock_guard(&lock_);
166 29130 return size_ == 0;
167 14565 }
168
169 109 uint64_t size() {
170 109 MutexLockGuard lock_guard(&lock_);
171 218 return size_;
172 109 }
173
174 private:
175 111992 void Init() {
176 111992 Link *sentinel = new Link(NULL);
177 111992 head_ = sentinel;
178 111992 head_->next_ = head_->prev_ = sentinel;
179
180 111992 int retval = pthread_mutex_init(&lock_, NULL);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59104 times.
111992 assert(retval == 0);
182 111992 retval = pthread_cond_init(&cond_populated_, NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59104 times.
111992 assert(retval == 0);
184 111992 retval = pthread_cond_init(&cond_capacious_, NULL);
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59104 times.
111992 assert(retval == 0);
186 111992 retval = pthread_cond_init(&cond_empty_, NULL);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59104 times.
111992 assert(retval == 0);
188 111992 }
189
190 134405539 ItemT *SliceUnlocked(Link *link) {
191 // Cannot delete the sentinel link
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80767655 times.
134405539 assert(link != head_);
193 134405539 link->prev_->next_ = link->next_;
194 134405539 link->next_->prev_ = link->prev_;
195 134405539 ItemT *item = link->item_;
196
2/2
✓ Branch 0 taken 80596952 times.
✓ Branch 1 taken 170703 times.
134405539 delete link;
197 135159813 size_--;
198 135159813 int retval = pthread_cond_signal(&cond_capacious_);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80656613 times.
134183505 assert(retval == 0);
200
2/2
✓ Branch 0 taken 27386239 times.
✓ Branch 1 taken 53270374 times.
134183505 if (size_ == 0) {
201 52438534 retval = pthread_cond_broadcast(&cond_empty_);
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27425038 times.
52441710 assert(retval == 0);
203 }
204 134261095 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 8445 TubeGroup() : is_active_(false) { atomic_init32(&round_robin_); }
248
249 8434 ~TubeGroup() {
250
2/2
✓ Branch 1 taken 52145 times.
✓ Branch 2 taken 4906 times.
111237 for (unsigned i = 0; i < tubes_.size(); ++i)
251
1/2
✓ Branch 1 taken 52145 times.
✗ Branch 2 not taken.
102803 delete tubes_[i];
252 8434 }
253
254 102948 void TakeTube(Tube<ItemT> *t) {
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52218 times.
102948 assert(!is_active_);
256 102948 tubes_.push_back(t);
257 102948 }
258
259 8445 void Activate() {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4912 times.
8445 assert(!is_active_);
261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4912 times.
8445 assert(!tubes_.empty());
262 8445 is_active_ = true;
263 8445 }
264
265 /**
266 * Like Tube::EnqueueBack(), but pick a tube according to ItemT::tag()
267 */
268 48277154 typename Tube<ItemT>::Link *Dispatch(ItemT *item) {
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48277154 times.
48277154 assert(is_active_);
270
2/2
✓ Branch 1 taken 37960232 times.
✓ Branch 2 taken 10236330 times.
48277154 unsigned tube_idx = (tubes_.size() == 1) ? 0
271 37960232 : (item->tag() % tubes_.size());
272 47936738 return tubes_[tube_idx]->EnqueueBack(item);
273 }
274
275 /**
276 * Like Tube::EnqueueBack(), use tubes one after another
277 */
278 4001503 typename Tube<ItemT>::Link *DispatchAny(ItemT *item) {
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4001503 times.
4001503 assert(is_active_);
280
2/2
✓ Branch 1 taken 4001501 times.
✓ Branch 2 taken 2 times.
4001503 unsigned tube_idx = (tubes_.size() == 1)
281 ? 0
282 4001501 : (atomic_xadd32(&round_robin_, 1) % tubes_.size());
283 4001503 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