forked from supranational/supra_seal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring_t.hpp
574 lines (492 loc) · 11.9 KB
/
ring_t.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright Supranational LLC
#ifndef __RING_T_HPP__
#define __RING_T_HPP__
#include <stdint.h>
#include <stdio.h>
#include <vector>
#include <atomic>
#include <mutex>
#include <assert.h>
void ring_spdk_free(void *);
void* ring_spdk_alloc(size_t bytes);
// Single producer / Single consumer lock free fifo
template <class T>
class mt_fifo_t {
std::vector<T*> store;
// If we allocated the contents store the pointer
T *contents;
// 'head' tracks the next unused element.
std::atomic<size_t> head;
// 'tail' tracks the last used element
std::atomic<size_t> tail;
public:
mt_fifo_t() : head(0), tail(0) {
contents = nullptr;
}
~mt_fifo_t() {
#ifndef NO_SPDK
ring_spdk_free(contents);
#endif
}
int create(const char *name, size_t count) {
return create(count);
}
int create(size_t _count) {
// Create a pool to hold the desired size
store.resize(_count + 1);
return 0;
}
size_t capacity() {
return store.size() - 1;
}
int enqueue_nocheck(T *obj) {
//assert (!is_full());
size_t h = head;
store[h] = obj;
if (h == store.size() - 1) {
head = 0;
} else {
head = h + 1;
}
return 0;
}
int enqueue(T *obj) {
assert (!is_full());
size_t h = head;
store[h] = obj;
if (h == store.size() - 1) {
head = 0;
} else {
head = h + 1;
}
return 0;
}
T* dequeue() {
if (size() != 0) {
size_t t = tail;
T *obj = store[t];
if (t == store.size() - 1) {
tail = 0;
} else {
tail = t + 1;
}
return obj;
}
return nullptr;
}
// Fill the pool
// One element of store is left empty since the pool can hold
// size() - 1 of usable data.
#ifndef NO_SPDK
int fill() {
contents = (T*)ring_spdk_alloc(sizeof(T) * (store.size() - 1));
if (contents == nullptr) {
return 1;
}
for (size_t i = 0; i < store.size() - 1; i++) {
store[i] = &contents[i];
}
head = store.size() - 1;
return 0;
}
#endif
// Number of used entries in the ring
size_t size() {
// Load values so we can perform a consistent calculation
size_t h = head;
size_t t = tail;
if (h >= t) {
return h - t;
} else {
return (store.size() + h) - t;
}
}
// Get entry at index
T& operator[](size_t i) {
return *store[i];
}
bool is_full() {
return size() == store.size() - 1;
}
inline size_t free_count() {
return capacity() - size();
}
void print() {
size_t h = head;
size_t t = tail;
printf("mt_ring_t: tail %ld, head %ld\n", t, h);
}
};
// Non-multithread safe pool using a ring buffer
template <class T>
class pool_t {
std::vector<T*> store;
// If we allocated the contents store the pointer
T *contents;
// 'head' tracks the next unused element.
size_t head;
// 'tail' tracks the last used element
size_t tail;
public:
pool_t() {
contents = nullptr;
head = 0;
tail = 0;
}
~pool_t() {
#ifndef NO_SPDK
ring_spdk_free(contents);
#endif
}
int create(size_t _count) {
// Create a pool to hold the desired size
store.resize(_count + 1);
return 0;
}
int enqueue(T *obj) {
assert (!is_full());
store[head++] = obj;
if (head >= store.size()) {
head = 0;
}
return 0;
}
T* dequeue() {
if (size() != 0) {
T *obj = store[tail++];
if (tail >= store.size()) {
tail = 0;
}
return obj;
}
return nullptr;
}
// Dequeue a block of contiguous elements
// Returns null if not enough elements or elements are non-contiguous
T** dequeue_bulk(size_t count) {
if (size() >= count && tail + count <= store.size()) {
T **obj = &store[tail];
tail += count;
if (tail >= store.size()) {
tail -= store.size();
}
return obj;
}
return nullptr;
}
// Fill the pool
// One element of store is left empty since the pool can hold
// size() - 1 of usable data.
#ifndef NO_SPDK
int fill() {
contents = (T*)ring_spdk_alloc(sizeof(T) * (store.size() - 1));
if (contents == nullptr) {
return 1;
}
for (size_t i = 0; i < store.size() - 1; i++) {
store[i] = &contents[i];
}
head = store.size() - 1;
return 0;
}
#endif
// Number of used entries in the ring
size_t size() {
if (head >= tail) {
return head - tail;
} else {
return (store.size() + head) - tail;
}
}
// Get entry at index
T& operator[](size_t i) {
return *store[i];
}
bool is_full() {
return size() == store.size() - 1;
}
size_t capacity() {
return store.size() - 1;
}
inline size_t free_count() {
return capacity() - size();
}
};
template <class T>
class mtx_fifo_t {
pool_t<T> pool;
std::mutex mtx;
public:
int create(size_t _count) {
std::unique_lock<std::mutex> lock(mtx);
return pool.create(_count);
}
int enqueue(T *obj) {
std::unique_lock<std::mutex> lock(mtx);
return pool.enqueue(obj);
}
T* dequeue() {
std::unique_lock<std::mutex> lock(mtx);
return pool.dequeue();
}
// Number of used entries in the ring
size_t size() {
std::unique_lock<std::mutex> lock(mtx);
return pool.size();
}
// Get entry at index
T& operator[](size_t i) {
std::unique_lock<std::mutex> lock(mtx);
return pool[i];
}
bool is_full() {
std::unique_lock<std::mutex> lock(mtx);
return pool.is_full();
}
size_t capacity() {
std::unique_lock<std::mutex> lock(mtx);
return pool.capacity();
}
inline size_t free_count() {
std::unique_lock<std::mutex> lock(mtx);
return pool.free_count();
}
};
// Ring buffer for data from disk
// Safe for single producer / single consumer.
// On the producer side, the flow is:
// - Advance the head to reserve an element. The element is marked as invalid.
// - Initiate a disk read for the data
// - Sometime later the disk DMA completes and we are notified. At this point
// the data is expected to be in memory.
// - Mark the entry as valid, indicating it may be read
// On the consumer side
// - Data is consumed from the tail. Once consumed tail is advanced
// - Data is only consumed when marked as valid.
typedef std::atomic<uint64_t> ring_buffer_valid_t;
template <class T, unsigned int _VALID_THRESHOLD, int SIZE>
class ring_buffer_t {
public:
typedef T* T_ptr;
const unsigned int VALID_THRESHOLD = _VALID_THRESHOLD;
// Number of entries
static const size_t count = SIZE;
private:
// Array of entries
T_ptr entries; // 4k pages - 1536 pages to saturate drives
// Store which entries are valid
ring_buffer_valid_t* valid;
// pointers move right ----->
// tail head_valid head
// | | |
// -----------------------------------------------------------------------
// | | | | | | | | | | | | | | |
// -----------------------------------------------------------------------
// 0 1 1 1 1 1 1 1 1 1 0 1 0 0
// valid
// 'head' tracks the next unused element.
size_t head;
// 'head_valid' tracks the point at which all previous elements are valid
size_t head_valid;
// 'tail' tracks the last used element
size_t tail;
// track size
size_t cur_size;
public:
ring_buffer_t() {
entries = nullptr;
valid = nullptr;
head = 0;
head_valid = 0;
tail = 0;
cur_size = 0;
}
~ring_buffer_t() {
delete [] valid;
valid = nullptr;
}
// Usable size will be count - 1
// 'entries' is an array of count entries
int create(T *_entries) {
entries = _entries;
valid = new ring_buffer_valid_t[count];
for (size_t i = 0; i < count; i++) {
valid[i] = 0;
}
return 0;
}
// Number of elements of storage
inline size_t storage() {
return count;
}
inline size_t capacity() {
return count - 1;
}
public:
inline size_t size() {
//return sub(head, tail);
return cur_size;
}
inline size_t free_count() {
//return capacity() - size();
return capacity() - cur_size;
}
inline bool is_full() {
return cur_size == capacity();
}
inline T *get_entry(size_t idx) {
return &entries[idx];
}
// Reserve the next free element for use, advance head
// Do not perform the safety check for a full buffer
inline T *reserve_nocheck(size_t &idx) {
// Store index
idx = head;
// Advance head
size_t next_head = incr(head);
valid[next_head] = 0;
head = next_head;
cur_size++;
return &entries[idx];
}
// Reserve the next free element for use, advance head
inline T *reserve(size_t &idx) {
if (is_full()) {
return nullptr;
}
// Store index
idx = head;
// Advance head
size_t next_head = incr(head);
valid[next_head] = 0;
head = next_head;
cur_size++;
return &entries[idx];
}
// Reserve the next free element for use, advance head
// Do not perform the safety check for a full buffer
inline void reserve_batch_nocheck(size_t count, size_t &idx, T** batch) {
// Store index
idx = head;
for (size_t i = 0; i < count; i++) {
batch[i] = &entries[head];
// Advance head
size_t next_head = incr(head);
valid[next_head] = 0;
head = next_head;
}
cur_size += count;
}
// Mark element as valid
inline void incr_valid(size_t idx, uint64_t amount = 1) {
valid[idx].fetch_add(amount);
}
inline ring_buffer_valid_t get_valid(size_t idx) {
return valid[idx].load();
}
inline bool is_valid(size_t idx) {
return valid[idx].load() >= VALID_THRESHOLD;
}
inline ring_buffer_valid_t* get_valid_ptr(size_t idx) {
return &valid[idx];
}
inline size_t get_head() {
return head;
}
inline size_t get_tail() {
return tail;
}
inline bool is_tail_valid() {
return valid[tail].load() >= VALID_THRESHOLD;
}
inline size_t incr(size_t idx) {
idx = (idx == count - 1) ? 0 : idx + 1;
return idx;
}
inline size_t decr(size_t idx) {
return (idx == 0) ? count - 1 : idx - 1;
}
// Returns a - b, taking into account wraparound
inline size_t sub(size_t a, size_t b) {
if (a >= b) {
return a - b;
}
return (count + a) - b;
}
// Returns a + b, taking into account wraparound
inline size_t add(size_t a, size_t b) {
size_t res = a + b;
if (res >= count) {
res -= count;
}
return res;
}
// Advance the head_valid pointer
inline size_t advance_valid() {
size_t cnt = 0;
while (valid[head_valid].load() >= VALID_THRESHOLD) {
cnt++;
head_valid = incr(head_valid);
}
return cnt;
}
// Release the tail element to unused state
inline void release() {
// Advance tail
//assert (size() > 0);
tail = incr(tail);
cur_size--;
}
// Release valid tail elements to unused state
inline size_t release_valid() {
size_t count = 0;
// Advance tail
while (valid[tail].load() >= VALID_THRESHOLD) {
valid[tail] = 0;
count++;
tail = incr(tail);
cur_size--;
}
return count;
}
// Print debug information
void print() {
printf("count %ld, tail %ld, head_valid %ld, head %ld, size %ld, full %d, free_count %ld\n",
count, tail, head_valid, head, size(), is_full(), free_count());
}
};
template<typename ABS_TYPE, int RING_SIZE, int RING_DIVISOR>
class ring_counter_t {
private:
ABS_TYPE _abs;
size_t _idx;
public:
ring_counter_t(ABS_TYPE& abs) : _abs(abs) {
_idx = 0;
}
ABS_TYPE& abs() {
return _abs;
}
size_t idx() {
return _idx / RING_DIVISOR;
}
size_t offset() {
return _idx % RING_DIVISOR;
}
void operator ++(int) {
_abs++;
_idx++;
if (_idx == RING_SIZE) {
_idx = 0;
}
}
ring_counter_t& operator+=(const size_t rhs) {
_abs += rhs;
_idx += rhs;
if (_idx >= RING_SIZE) {
_idx -= RING_SIZE;
}
return *this;
}
};
#endif