-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspscq.cpp
1807 lines (1604 loc) · 50.7 KB
/
spscq.cpp
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018 Universita' di Pisa
* Copyright (C) 2018 Vincenzo Maffione
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <cstdlib>
#include <fstream>
#include <cstring>
#include <string>
#include <unistd.h>
#include <ctime>
#include <cerrno>
#include <assert.h>
#include <cstring>
#include <thread>
#include <map>
#include <random>
#include <algorithm>
#include <iostream>
#include <functional>
#include <chrono>
#include <signal.h>
#include <sstream>
#include <sys/mman.h>
#include "mlib.h"
/* This must be defined before including spscq.h. */
static unsigned short *smap = nullptr;
#if 1
#define SMAP(x) smap[x]
#else
#define SMAP(x) x
#endif
#include "spscq.h"
//#define RATE_LIMITING_CONSUMER /* Enable support for rate limiting consumer */
#undef QDEBUG /* dump queue state at each operation */
#define ONEBILLION (1000LL * 1000000LL) /* 1 billion */
static int stop = 0;
static void
sigint_handler(int signum)
{
ACCESS_ONCE(stop) = 1;
}
/* Alloc zeroed cacheline-aligned memory, aborting on failure. */
static void *
szalloc(size_t size, bool hugepages)
{
void *p = NULL;
if (hugepages) {
p = mmap(NULL, size, PROT_WRITE | PROT_READ,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
if (p == MAP_FAILED) {
printf("mmap allocation failure: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
assert(reinterpret_cast<uint64_t>(p) % SPSCQ_ALIGN_SIZE == 0);
} else {
int ret = posix_memalign(&p, SPSCQ_ALIGN_SIZE, size);
if (ret) {
printf("allocation failure: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
memset(p, 0, size);
}
return p;
}
static void
sfree(void *ptr, size_t size, bool hugepages)
{
if (hugepages) {
munmap(ptr, size);
} else {
free(ptr);
}
}
unsigned int
ilog2(unsigned int x)
{
unsigned int probe = 0x00000001U;
unsigned int ret = 0;
unsigned int c;
assert(x != 0);
for (c = 0; probe != 0; probe <<= 1, c++) {
if (x & probe) {
ret = c;
}
}
return ret;
}
struct RateLimitedStats {
std::chrono::system_clock::time_point last;
long long int step = 30ULL * 1000000ULL;
long long int thresh = 0;
RateLimitedStats(long long int th)
: last(std::chrono::system_clock::now()), thresh(th - step)
{
}
inline void stat(long long int left)
{
if (unlikely(left < thresh)) {
std::chrono::system_clock::time_point now =
std::chrono::system_clock::now();
long long unsigned int ndiff =
std::chrono::duration_cast<std::chrono::nanoseconds>(now - last)
.count();
double mpps;
mpps = step * 1000.0 / ndiff;
printf("%3.3f Mpps\n", mpps);
if (ndiff < 2 * ONEBILLION) {
step <<= 1;
} else if (ndiff > 3 * ONEBILLION) {
step >>= 1;
}
thresh -= step;
last = now;
}
}
};
enum class MbufMode {
NoAccess = 0,
LinearAccess,
};
struct Mbuf {
unsigned int len;
unsigned int __padding[7];
#define MBUF_LEN_MAX (4096 + SPSCQ_CACHELINE_SIZE - 8 * sizeof(unsigned int))
char buf[MBUF_LEN_MAX];
};
enum class RateLimitMode {
None = 0,
Limit,
};
enum class EmulatedOverhead {
None = 0,
SpinCycles,
};
struct Iffq;
struct Global {
static constexpr int DFLT_BATCH = 32;
static constexpr int DFLT_QLEN = 256;
static constexpr int DFLT_LINE_ENTRIES = 32;
static constexpr int DFLT_D = 10;
/* Test length as a number of packets. */
long long unsigned int num_packets = 0; /* infinite */
/* Length of the SPSC queue. */
unsigned int qlen = DFLT_QLEN;
/* How many entries for each line in iffq. */
unsigned int line_entries = DFLT_LINE_ENTRIES;
/* Max batch for producer and consumer operation. */
unsigned int prod_batch = DFLT_BATCH;
unsigned int cons_batch = DFLT_BATCH;
/* Affinity for producer and consumer. */
int p_core = 0, c_core = 1;
/* Emulated per-packet load for the producer and consumer side,
* in TSC ticks (initially in nanoseconds). */
uint64_t prod_spin_ticks = 0, cons_spin_ticks = 0;
uint64_t cons_rate_limit_cycles = 0;
bool online_rate = false;
bool perf_counters = false;
/* Test duration in seconds. */
unsigned int duration = DFLT_D;
/* Type of queue used. */
std::string test_type = "lq";
/* If true we do a latency test; if false we do a throughput test. */
bool latency = false;
/* Try to keep hardware prefetcher disabled for the accesses to the
* queue slots. This should reduce the noise in the cache miss
* behaviour. */
bool deceive_hw_data_prefetcher = false;
/* Allocate memory from hugepages. */
bool hugepages = false;
MbufMode mbuf_mode = MbufMode::NoAccess;
/* Timestamp to compute experiment statistics. */
std::chrono::system_clock::time_point begin, end;
/* Checksum for when -M is used. */
unsigned int csum;
/* When -M is used, we need a variable to increment that depends on
* something contained inside the mbuf; this is needed to make sure
* that spin_for() has a data dependency on the mbuf_get() or
* mbuf_put(), so that spin_for() does not run in the parallel
* with mbuf_put() or mbuf_get().
* To avoid the compiler optimizing out this variable, P and C
* save it to the 'trash' global variable here. */
unsigned int trash;
/* Packet count written back by consumers. It's safer for it
* to have its own cacheline. */
SPSCQ_CACHELINE_ALIGNED
volatile long long unsigned pkt_cnt = 0;
/* Average batches as seen by producer and consumer. */
SPSCQ_CACHELINE_ALIGNED
long long int producer_batches = 0;
long long int consumer_batches = 0;
/* L1 dcache miss rates in M/sec. */
float prod_read_miss_rate = 0.0;
float cons_read_miss_rate = 0.0;
float prod_write_miss_rate = 0.0;
float cons_write_miss_rate = 0.0;
/* CPU instruction rate in B/sec. */
float prod_insn_rate = 0.0;
float cons_insn_rate = 0.0;
/* The lamport-like queue. */
Blq *blq = nullptr;
Blq *blq_back = nullptr;
/* The ff-like queue. */
Iffq *ffq = nullptr;
Iffq *ffq_back = nullptr;
/* A pool of preallocated mbufs (only accessed by P). */
Mbuf *pool = nullptr;
/* Index in the mbuf pool array (only accessed by P). */
unsigned int pool_idx = 0;
/* Maks for the mbuf pool array (only accessed by P). */
unsigned int pool_mask;
void producer_header();
void producer_footer();
void consumer_header();
void consumer_footer();
void print_results();
};
static void
miss_rate_print(const char *prefix, double mpps, float read_miss_rate,
float write_miss_rate)
{
printf("[%s] L1 d-cache %.3f rmisses/packet,"
"%.3f wmisses/packet\n",
prefix, read_miss_rate / mpps, write_miss_rate / mpps);
}
void
Global::print_results()
{
double mpps =
pkt_cnt * 1000.0 /
std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin)
.count();
if (csum) {
unsigned int expect = static_cast<unsigned int>(
static_cast<long long unsigned>(pkt_cnt) * (pkt_cnt - 1) / 2);
// printf("PKTS %llu\n", pkt_cnt);
printf("[C] csum = %x, expect = %x, diff = %x\n", csum, expect,
expect - csum);
}
if (prod_insn_rate != 0.0) {
printf("[P] %.2f Ginsn/s %.2f CPU insn per packet\n", prod_insn_rate,
prod_insn_rate * 1000.0 / mpps);
}
if (cons_insn_rate != 0.0) {
printf("[C] %.2f Ginsn/s %.2f CPU insn per packet\n", cons_insn_rate,
cons_insn_rate * 1000.0 / mpps);
}
if (producer_batches) {
printf("[P] avg batch = %.3f\n",
static_cast<double>(pkt_cnt) /
static_cast<double>(producer_batches));
}
if (consumer_batches) {
printf("[C] avg batch = %.3f\n",
static_cast<double>(pkt_cnt) /
static_cast<double>(consumer_batches));
}
miss_rate_print("P", mpps, prod_read_miss_rate, prod_write_miss_rate);
miss_rate_print("C", mpps, cons_read_miss_rate, cons_write_miss_rate);
printf("%3.3f Mpps %2.3f Pmpp %2.3f Cmpp\n", mpps,
(prod_read_miss_rate + prod_write_miss_rate) / mpps,
(cons_read_miss_rate + cons_write_miss_rate) / mpps);
}
void
Global::producer_header()
{
runon("P", p_core);
begin = std::chrono::system_clock::now();
pool_idx = 0;
}
void
Global::producer_footer()
{
}
void
Global::consumer_header()
{
runon("C", c_core);
}
void
Global::consumer_footer()
{
end = std::chrono::system_clock::now();
}
static Mbuf gm[2];
template <MbufMode kMbufMode>
static inline Mbuf *
mbuf_get(Global *const g, unsigned int trash)
{
if (kMbufMode == MbufMode::NoAccess) {
return &gm[trash & 1];
} else {
Mbuf *m = &g->pool[g->pool_idx & g->pool_mask];
/* We want that m->len depends on trash but we
* don't want to put trash inside m->len (to
* preserve the checksum). */
m->len = g->pool_idx++ + !!(trash == 0xdeadbeef);
return m;
}
}
template <MbufMode kMbufMode>
static inline void
mbuf_put(Mbuf *const m, unsigned int *csum, unsigned int *trash)
{
*trash += reinterpret_cast<uintptr_t>(m);
if (kMbufMode != MbufMode::NoAccess) {
*csum += m->len;
}
}
static void
qslotmap_init(unsigned short *qslotmap, unsigned qlen, bool shuffle)
{
if (!shuffle) {
for (unsigned i = 0; i < qlen; i++) {
qslotmap[i] = i;
}
return;
}
/* Prepare support for shuffled queue slots to disable the effect of hw
* prefetching on the queue slots.
* K is the number of entries per cacheline. */
unsigned int K = SPSCQ_CACHELINE_SIZE / sizeof(uintptr_t);
std::vector<unsigned short> v(qlen / K);
std::random_device rd;
std::mt19937 gen(rd());
/* First create a vector of qlen/K elements (i.e. the number of cache
* lines in the queue), containing a random permutation of
* K*[0..qlen/K[. */
for (size_t i = 0; i < v.size(); i++) {
v[i] = K * i;
}
std::shuffle(v.begin(), v.end(), gen);
for (unsigned j = 0; j < qlen / K; j++) {
std::vector<unsigned short> u(K);
/* Generate slot indices for the #j cacheline, as a random
* permutation of [v[j]..v[j]+K[ */
for (size_t i = 0; i < K; i++) {
u[i] = v[j] + i;
}
std::shuffle(u.begin(), u.end(), gen);
for (size_t i = 0; i < K; i++) {
qslotmap[j * K + i] = u[i];
}
}
}
static Blq *
blq_create(int qlen, bool hugepages)
{
Blq *blq = static_cast<Blq *>(szalloc(blq_size(qlen), hugepages));
int ret;
ret = blq_init(blq, qlen);
if (ret) {
return NULL;
}
assert(reinterpret_cast<uintptr_t>(blq) % SPSCQ_ALIGN_SIZE == 0);
assert((reinterpret_cast<uintptr_t>(&blq->write)) -
(reinterpret_cast<uintptr_t>(&blq->write_priv)) ==
SPSCQ_ALIGN_SIZE);
assert((reinterpret_cast<uintptr_t>(&blq->read_priv)) -
(reinterpret_cast<uintptr_t>(&blq->write)) ==
SPSCQ_ALIGN_SIZE);
assert((reinterpret_cast<uintptr_t>(&blq->read)) -
(reinterpret_cast<uintptr_t>(&blq->read_priv)) ==
SPSCQ_ALIGN_SIZE);
assert((reinterpret_cast<uintptr_t>(&blq->qlen)) -
(reinterpret_cast<uintptr_t>(&blq->read)) ==
SPSCQ_ALIGN_SIZE);
assert((reinterpret_cast<uintptr_t>(&blq->q[0])) -
(reinterpret_cast<uintptr_t>(&blq->qlen)) ==
SPSCQ_ALIGN_SIZE);
return blq;
}
static void
blq_free(Blq *blq, bool hugepages)
{
memset(blq, 0, sizeof(*blq));
sfree(blq, blq_size(blq->qlen), hugepages);
}
template <MbufMode kMbufMode>
static inline void
spin_for(uint64_t spin, unsigned int *trash)
{
uint64_t when = rdtsc() + spin;
while (rdtsc() < when) {
(*trash)++;
compiler_barrier();
}
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
lq_producer(Global *const g)
{
const uint64_t spin = g->prod_spin_ticks;
Blq *const blq = g->blq;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
g->producer_header();
while (!ACCESS_ONCE(stop)) {
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
Mbuf *m = mbuf_get<kMbufMode>(g, trash);
#ifdef QDEBUG
blq_dump("P", blq);
#endif
while (lq_write(blq, (uintptr_t)m)) {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
}
++batch_packets;
}
g->producer_batches = batches;
g->producer_footer();
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
lq_consumer(Global *const g)
{
const uint64_t spin = g->cons_spin_ticks;
const uint64_t rate_limit = g->cons_rate_limit_cycles;
Blq *const blq = g->blq;
unsigned int csum = 0;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
Mbuf *m;
g->consumer_header();
for (;;) {
#ifdef QDEBUG
blq_dump("C", blq);
#endif
m = (Mbuf *)lq_read(blq);
if (m) {
++g->pkt_cnt;
++batch_packets;
mbuf_put<kMbufMode>(m, &csum, &trash);
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
} else {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
if (kRateLimitMode == RateLimitMode::Limit) {
tsc_sleep_till(rdtsc() + rate_limit);
}
if (unlikely(ACCESS_ONCE(stop))) {
break;
}
}
}
g->consumer_batches = batches;
g->csum = csum;
g->trash = trash;
g->consumer_footer();
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
llq_producer(Global *const g)
{
const uint64_t spin = g->prod_spin_ticks;
Blq *const blq = g->blq;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
g->producer_header();
while (!ACCESS_ONCE(stop)) {
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
Mbuf *m = mbuf_get<kMbufMode>(g, trash);
#ifdef QDEBUG
blq_dump("P", blq);
#endif
while (llq_write(blq, (uintptr_t)m)) {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
}
++batch_packets;
}
g->producer_batches = batches;
g->producer_footer();
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
llq_consumer(Global *const g)
{
const uint64_t spin = g->cons_spin_ticks;
const uint64_t rate_limit = g->cons_rate_limit_cycles;
Blq *const blq = g->blq;
unsigned int csum = 0;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
Mbuf *m;
g->consumer_header();
for (;;) {
#ifdef QDEBUG
blq_dump("C", blq);
#endif
m = (Mbuf *)llq_read(blq);
if (m) {
++g->pkt_cnt;
++batch_packets;
mbuf_put<kMbufMode>(m, &csum, &trash);
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
} else {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
if (kRateLimitMode == RateLimitMode::Limit) {
tsc_sleep_till(rdtsc() + rate_limit);
}
if (unlikely(ACCESS_ONCE(stop))) {
break;
}
}
}
g->consumer_batches = batches;
g->csum = csum;
g->trash = trash;
g->consumer_footer();
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
blq_producer(Global *const g)
{
const uint64_t spin = g->prod_spin_ticks;
const unsigned int batch = g->prod_batch;
Blq *const blq = g->blq;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
g->producer_header();
while (!ACCESS_ONCE(stop)) {
unsigned int avail = blq_wspace(blq, batch);
#ifdef QDEBUG
blq_dump("P", blq);
#endif
if (avail) {
if (avail > batch) {
avail = batch;
}
batch_packets += avail;
for (; avail > 0; avail--) {
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
Mbuf *m = mbuf_get<kMbufMode>(g, trash);
blq_write_local(blq, (uintptr_t)m);
}
blq_write_publish(blq);
} else {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
}
}
g->producer_batches = batches;
g->producer_footer();
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
blq_consumer(Global *const g)
{
const uint64_t spin = g->cons_spin_ticks;
const uint64_t rate_limit = g->cons_rate_limit_cycles;
const unsigned int batch = g->cons_batch;
Blq *const blq = g->blq;
unsigned int csum = 0;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
Mbuf *m;
g->consumer_header();
for (;;) {
unsigned int avail = blq_rspace(blq, batch);
#ifdef QDEBUG
blq_dump("C", blq);
#endif
if (avail) {
if (avail > batch) {
avail = batch;
}
batch_packets += avail;
g->pkt_cnt += avail;
for (; avail > 0; avail--) {
m = (Mbuf *)blq_read_local(blq);
mbuf_put<kMbufMode>(m, &csum, &trash);
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
}
blq_read_publish(blq);
} else {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
if (kRateLimitMode == RateLimitMode::Limit) {
tsc_sleep_till(rdtsc() + rate_limit);
}
if (unlikely(ACCESS_ONCE(stop))) {
break;
}
}
}
g->csum = csum;
g->trash = trash;
g->consumer_batches = batches;
g->consumer_footer();
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
ffq_producer(Global *const g)
{
const uint64_t spin = g->prod_spin_ticks;
Iffq *const ffq = g->ffq;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
g->producer_header();
while (!ACCESS_ONCE(stop)) {
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
Mbuf *m = mbuf_get<kMbufMode>(g, trash);
if (kMbufMode != MbufMode::NoAccess) {
compiler_barrier();
}
while (ffq_write(ffq, (uintptr_t)m)) {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
}
++batch_packets;
}
g->producer_batches = batches;
g->producer_footer();
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
ffq_consumer(Global *const g)
{
const uint64_t spin = g->cons_spin_ticks;
const uint64_t rate_limit = g->cons_rate_limit_cycles;
Iffq *const ffq = g->ffq;
unsigned int csum = 0;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
Mbuf *m;
g->consumer_header();
for (;;) {
m = (Mbuf *)ffq_read(ffq);
if (m) {
++batch_packets;
++g->pkt_cnt;
mbuf_put<kMbufMode>(m, &csum, &trash);
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
} else {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
if (kRateLimitMode == RateLimitMode::Limit) {
tsc_sleep_till(rdtsc() + rate_limit);
}
if (unlikely(ACCESS_ONCE(stop))) {
break;
}
}
}
g->csum = csum;
g->trash = trash;
g->consumer_batches = batches;
g->consumer_footer();
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/**
* iffq_create - create a new mailbox
* @entries: the number of entries
* @line_size: the line size in bytes (not in entries)
*
* Both entries and line_size must be a power of 2.
*/
Iffq *
__iffq_create(unsigned int entries, unsigned int line_size, bool hugepages,
bool improved)
{
Iffq *ffq;
int err;
ffq = static_cast<Iffq *>(szalloc(iffq_size(entries), hugepages));
err = iffq_init(ffq, entries, line_size, improved);
if (err) {
free(ffq);
return NULL;
}
assert(reinterpret_cast<uintptr_t>(ffq) % SPSCQ_ALIGN_SIZE == 0);
assert(((reinterpret_cast<uintptr_t>(&ffq->cons_clear)) -
(reinterpret_cast<uintptr_t>(&ffq->prod_write))) %
SPSCQ_ALIGN_SIZE ==
0);
assert((reinterpret_cast<uintptr_t>(&ffq->q[0])) -
(reinterpret_cast<uintptr_t>(&ffq->cons_clear)) ==
SPSCQ_ALIGN_SIZE);
return ffq;
}
Iffq *
iffq_create(unsigned int entries, unsigned int line_size, bool hugepages)
{
return __iffq_create(entries, line_size, hugepages, /*improved=*/true);
}
Iffq *
ffq_create(unsigned int entries, unsigned int line_size, bool hugepages)
{
return __iffq_create(entries, line_size, hugepages, /*improved=*/false);
}
/**
* iffq_free - delete a mailbox
* @ffq: the mailbox to be deleted
*/
void
iffq_free(Iffq *ffq, bool hugepages)
{
sfree(ffq, iffq_size(ffq->entry_mask + 1), hugepages);
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
iffq_producer(Global *const g)
{
const uint64_t spin = g->prod_spin_ticks;
struct Iffq *const ffq = g->ffq;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
g->producer_header();
while (!ACCESS_ONCE(stop)) {
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
Mbuf *m = mbuf_get<kMbufMode>(g, trash);
#ifdef QDEBUG
iffq_dump("P", ffq);
#endif
if (kMbufMode != MbufMode::NoAccess) {
/* Here we need a StoreStore barrier, to prevent writes to the
* mbufs to be reordered after the write to the queue slot. */
compiler_barrier();
}
while (iffq_insert(ffq, (uintptr_t)m)) {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
}
++batch_packets;
}
g->producer_batches = batches;
g->producer_footer();
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
iffq_consumer(Global *const g)
{
const uint64_t spin = g->cons_spin_ticks;
const uint64_t rate_limit = g->cons_rate_limit_cycles;
Iffq *const ffq = g->ffq;
unsigned int csum = 0;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
Mbuf *m;
g->consumer_header();
for (;;) {
#ifdef QDEBUG
iffq_dump("C", ffq);
#endif
m = (Mbuf *)iffq_extract(ffq);
if (m) {
++g->pkt_cnt;
++batch_packets;
mbuf_put<kMbufMode>(m, &csum, &trash);
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
iffq_clear(ffq);
} else {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
if (kRateLimitMode == RateLimitMode::Limit) {
tsc_sleep_till(rdtsc() + rate_limit);
}
if (unlikely(ACCESS_ONCE(stop))) {
break;
}
}
}
g->csum = csum;
g->trash = trash;
g->consumer_batches = batches;
g->consumer_footer();
}
template <MbufMode kMbufMode, RateLimitMode kRateLimitMode,
EmulatedOverhead kEmulatedOverhead>
static void
biffq_producer(Global *const g)
{
const uint64_t spin = g->prod_spin_ticks;
const unsigned int batch = g->prod_batch;
Iffq *const ffq = g->ffq;
unsigned int batch_packets = 0;
unsigned int batches = 0;
unsigned int trash = 0;
g->producer_header();
while (!ACCESS_ONCE(stop)) {
unsigned int avail = iffq_wspace(ffq, batch);
#ifdef QDEBUG
iffq_dump("P", ffq);
#endif
if (avail) {
if (avail > batch) {
avail = batch;
}
batch_packets += avail;
for (; avail > 0; avail--) {
if (kEmulatedOverhead == EmulatedOverhead::SpinCycles) {
spin_for<kMbufMode>(spin, &trash);
}
Mbuf *m = mbuf_get<kMbufMode>(g, trash);
iffq_insert_local(ffq, (uintptr_t)m);
}
if (kMbufMode != MbufMode::NoAccess) {
compiler_barrier();
}
iffq_insert_publish(ffq);
} else {
batches += (batch_packets != 0) ? 1 : 0;
batch_packets = 0;
}
}
g->producer_batches = batches;
g->producer_footer();
}
#define biffq_consumer iffq_consumer
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/*
* Support for latency tests.
*/
template <MbufMode kMbufMode>
void