GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/tube.h
Date: 2025-11-30 02:35:17
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 350133055 explicit Link(ItemT *item) : item_(item), next_(NULL), prev_(NULL) { }
46 46 ItemT *item() { return item_; }
47
48 private:
49 ItemT *item_;
50 Link *next_;
51 Link *prev_;
52 };
53
54 221693 Tube() : limit_(uint64_t(-1)), size_(0) { Init(); }
55 1876 explicit Tube(uint64_t limit) : limit_(limit), size_(0) { Init(); }
56 228864 ~Tube() {
57 228966 Link *cursor = head_;
58 do {
59 228971 Link *prev = cursor->prev_;
60
1/2
✓ Branch 0 taken 114984 times.
✗ Branch 1 not taken.
228971 delete cursor;
61 228971 cursor = prev;
62
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 114979 times.
228971 } while (cursor != head_);
63 228966 pthread_cond_destroy(&cond_populated_);
64 228966 pthread_cond_destroy(&cond_capacious_);
65 228966 pthread_cond_destroy(&cond_empty_);
66 228966 pthread_mutex_destroy(&lock_);
67 228966 }
68
69 /**
70 * Push an item to the back of the queue. Block if queue is currently full.
71 */
72 349631129 Link *EnqueueBack(ItemT *item) {
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190237215 times.
349631129 assert(item != NULL);
74 349631129 MutexLockGuard lock_guard(&lock_);
75
2/2
✓ Branch 0 taken 9497390 times.
✓ Branch 1 taken 189537601 times.
367218815 while (size_ == limit_)
76
1/2
✓ Branch 1 taken 9460774 times.
✗ Branch 2 not taken.
18994780 pthread_cond_wait(&cond_capacious_, &lock_);
77
78
1/2
✓ Branch 1 taken 189738023 times.
✗ Branch 2 not taken.
348224035 Link *link = new Link(item);
79 346937691 link->next_ = head_->next_;
80 346937691 link->prev_ = head_;
81 346937691 head_->next_->prev_ = link;
82 346937691 head_->next_ = link;
83 346937691 size_++;
84 346937691 int retval = pthread_cond_signal(&cond_populated_);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190719157 times.
350587147 assert(retval == 0);
86 349639271 return link;
87 350587147 }
88
89 /**
90 * Push an item to the front of the queue. Block if queue currently full.
91 */
92 2014201 Link *EnqueueFront(ItemT *item) {
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2013293 times.
2014201 assert(item != NULL);
94 2014201 MutexLockGuard lock_guard(&lock_);
95
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 2013278 times.
2014316 while (size_ == limit_)
96
1/2
✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
130 pthread_cond_wait(&cond_capacious_, &lock_);
97
98
1/2
✓ Branch 1 taken 2013283 times.
✗ Branch 2 not taken.
2014186 Link *link = new Link(item);
99 2014191 link->next_ = head_;
100 2014191 link->prev_ = head_->prev_;
101 2014191 head_->prev_->next_ = link;
102 2014191 head_->prev_ = link;
103 2014191 size_++;
104 2014191 int retval = pthread_cond_signal(&cond_populated_);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2013288 times.
2014196 assert(retval == 0);
106 2014201 return link;
107 2014196 }
108
109 /**
110 * Remove any link from the queue and return its item, including first/last
111 * element.
112 */
113 61 ItemT *Slice(Link *link) {
114 61 MutexLockGuard lock_guard(&lock_);
115 61 return SliceUnlocked(link);
116 61 }
117
118 /**
119 * Remove and return the first element from the queue. Block if tube is
120 * empty.
121 */
122 350991560 ItemT *PopFront() {
123 350991560 MutexLockGuard lock_guard(&lock_);
124
2/2
✓ Branch 0 taken 70397102 times.
✓ Branch 1 taken 191596618 times.
488933847 while (size_ == 0)
125
1/2
✓ Branch 1 taken 70432508 times.
✗ Branch 2 not taken.
138433920 pthread_cond_wait(&cond_populated_, &lock_);
126 700324609 return SliceUnlocked(head_->prev_);
127 348831921 }
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 2011397 ItemT *TryPopFront() {
137 2011397 MutexLockGuard lock_guard(&lock_);
138 // Note that we don't need to wait for a signal to arrive
139
2/2
✓ Branch 0 taken 1842326 times.
✓ Branch 1 taken 169956 times.
2012282 if (size_ == 0)
140 1842326 return NULL;
141 169956 return SliceUnlocked(head_->prev_);
142 2012282 }
143
144 /**
145 * Remove and return the last element from the queue. Block if tube is
146 * empty.
147 */
148 2011 ItemT *PopBack() {
149 2011 MutexLockGuard lock_guard(&lock_);
150
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 1103 times.
2323 while (size_ == 0)
151
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
312 pthread_cond_wait(&cond_populated_, &lock_);
152 4022 return SliceUnlocked(head_->next_);
153 2011 }
154
155 /**
156 * Blocks until the tube is empty
157 */
158 2110 void Wait() {
159 2110 MutexLockGuard lock_guard(&lock_);
160
2/2
✓ Branch 0 taken 1106 times.
✓ Branch 1 taken 2110 times.
3216 while (size_ > 0)
161
1/2
✓ Branch 1 taken 1106 times.
✗ Branch 2 not taken.
1106 pthread_cond_wait(&cond_empty_, &lock_);
162 2110 }
163
164 2741 bool IsEmpty() {
165 2741 MutexLockGuard lock_guard(&lock_);
166 5482 return size_ == 0;
167 2741 }
168
169 719 uint64_t size() {
170 719 MutexLockGuard lock_guard(&lock_);
171 1438 return size_;
172 719 }
173
174 private:
175 225037 void Init() {
176 225037 Link *sentinel = new Link(NULL);
177 225037 head_ = sentinel;
178 225037 head_->next_ = head_->prev_ = sentinel;
179
180 225037 int retval = pthread_mutex_init(&lock_, NULL);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115055 times.
225037 assert(retval == 0);
182 225037 retval = pthread_cond_init(&cond_populated_, NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115055 times.
225037 assert(retval == 0);
184 225037 retval = pthread_cond_init(&cond_capacious_, NULL);
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115055 times.
225037 assert(retval == 0);
186 225037 retval = pthread_cond_init(&cond_empty_, NULL);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115055 times.
225037 assert(retval == 0);
188 225037 }
189
190 349871893 ItemT *SliceUnlocked(Link *link) {
191 // Cannot delete the sentinel link
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 191367243 times.
349871893 assert(link != head_);
193 349871893 link->prev_->next_ = link->next_;
194 349871893 link->next_->prev_ = link->prev_;
195 349871893 ItemT *item = link->item_;
196
2/2
✓ Branch 0 taken 190845102 times.
✓ Branch 1 taken 522141 times.
349871893 delete link;
197 352280989 size_--;
198 352280989 int retval = pthread_cond_signal(&cond_capacious_);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192242098 times.
351621537 assert(retval == 0);
200
2/2
✓ Branch 0 taken 76908971 times.
✓ Branch 1 taken 115333127 times.
351621537 if (size_ == 0) {
201 149791333 retval = pthread_cond_broadcast(&cond_empty_);
202
2/2
✓ Branch 0 taken 807728 times.
✓ Branch 1 taken 76112974 times.
149814795 assert(retval == 0);
203 }
204 350029543 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 18959 TubeGroup() : is_active_(false) { atomic_init32(&round_robin_); }
248
249 18948 ~TubeGroup() {
250
2/2
✓ Branch 1 taken 108564 times.
✓ Branch 2 taken 11245 times.
231756 for (unsigned i = 0; i < tubes_.size(); ++i)
251
1/2
✓ Branch 1 taken 108564 times.
✗ Branch 2 not taken.
212808 delete tubes_[i];
252 18948 }
253
254 212953 void TakeTube(Tube<ItemT> *t) {
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108637 times.
212953 assert(!is_active_);
256 212953 tubes_.push_back(t);
257 212953 }
258
259 18959 void Activate() {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11251 times.
18959 assert(!is_active_);
261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11251 times.
18959 assert(!tubes_.empty());
262 18959 is_active_ = true;
263 18959 }
264
265 /**
266 * Like Tube::EnqueueBack(), but pick a tube according to ItemT::tag()
267 */
268 141694155 typename Tube<ItemT>::Link *Dispatch(ItemT *item) {
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 141694155 times.
141694155 assert(is_active_);
270
2/2
✓ Branch 1 taken 109843955 times.
✓ Branch 2 taken 31706726 times.
141694155 unsigned tube_idx = (tubes_.size() == 1) ? 0
271 109843955 : (item->tag() % tubes_.size());
272 140892559 return tubes_[tube_idx]->EnqueueBack(item);
273 }
274
275 /**
276 * Like Tube::EnqueueBack(), use tubes one after another
277 */
278 11503338 typename Tube<ItemT>::Link *DispatchAny(ItemT *item) {
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11503338 times.
11503338 assert(is_active_);
280
2/2
✓ Branch 1 taken 11503246 times.
✓ Branch 2 taken 92 times.
11503338 unsigned tube_idx = (tubes_.size() == 1)
281 ? 0
282 11503246 : (atomic_xadd32(&round_robin_, 1) % tubes_.size());
283 11503338 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