wcq 0.1.0

A Scalable, Portable, and Memory-Efficient Lock-Free FIFO Queue (DISC '19)
Documentation
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
diff -urN benchmark.orig/align.h benchmark/align.h
--- benchmark.orig/align.h	2020-12-22 17:17:27.685566626 -0500
+++ benchmark/align.h	2020-12-22 17:14:37.945420646 -0500
@@ -16,7 +16,7 @@
 
   int ret = posix_memalign(&ptr, align, size);
   if (ret != 0) {
-    fprintf(stderr, strerror(ret));
+    fprintf(stderr, "error: %s\n", strerror(ret));
     abort();
   }
 
diff -urN benchmark.orig/cas.c benchmark/cas.c
--- benchmark.orig/cas.c	1969-12-31 19:00:00.000000000 -0500
+++ benchmark/cas.c	2019-10-22 10:31:31.124409717 -0400
@@ -0,0 +1,26 @@
+#include "queue.h"
+#include "primitives.h"
+
+void queue_init(queue_t * q, int nprocs) {}
+void queue_register(queue_t * q, handle_t * hd, int id)
+{
+  *hd = id + 1;
+}
+
+void enqueue(queue_t * q, handle_t * th, void * val)
+{
+  long p = q->P;
+  while (!CAS(&q->P, &p, p + 1))
+    ;
+}
+
+void * dequeue(queue_t * q, handle_t * th)
+{
+  long c = q->C;
+  while (!CAS(&q->C, &c, c + 1))
+    ;
+  return (void *) (long) *th;
+}
+
+void queue_free(queue_t * q, handle_t * h) {}
+
diff -urN benchmark.orig/halfhalf.c benchmark/halfhalf.c
--- benchmark.orig/halfhalf.c	2019-10-22 10:44:25.757993930 -0400
+++ benchmark/halfhalf.c	2019-10-22 10:31:31.124409717 -0400
@@ -25,7 +25,8 @@
 
   printf("  Number of operations: %ld\n", nops);
 
-  q = align_malloc(PAGE_SIZE, sizeof(queue_t));
+  // FIXME: sizeof(queue_t) varies, allocate 4MB
+  q = align_malloc(PAGE_SIZE, 4194304);
   queue_init(q, nprocs);
 
   hds = align_malloc(PAGE_SIZE, sizeof(handle_t * [nprocs]));
@@ -60,7 +61,7 @@
     else
       dequeue(q, th);
 
-    delay_exec(&state);
+//    delay_exec(&state);
   }
 
   return val;
diff -urN benchmark.orig/lcrq.c benchmark/lcrq.c
--- benchmark.orig/lcrq.c	2019-10-22 10:44:25.761993916 -0400
+++ benchmark/lcrq.c	2019-10-22 10:31:31.124409717 -0400
@@ -112,6 +112,7 @@
 alloc:
       nrq = handle->next;
 
+      void *org_nrq = nrq;
       if (nrq == NULL) {
         nrq = align_malloc(PAGE_SIZE, sizeof(RingQueue));
         init_ring(nrq);
@@ -127,6 +128,9 @@
         handle->next = NULL;
         return;
       }
+
+      // Did not succeed, free the buffer
+      if (org_nrq == NULL) free(nrq);
       continue;
     }
 
diff -urN benchmark.orig/Makefile benchmark/Makefile
--- benchmark.orig/Makefile	2019-10-22 10:44:25.757993930 -0400
+++ benchmark/Makefile	2019-10-22 10:31:31.124409717 -0400
@@ -1,8 +1,9 @@
-TESTS = wfqueue wfqueue0 lcrq ccqueue msqueue faa delay
+TESTS = wfqueue wfqueue0 lcrq ccqueue msqueue faa delay cas scq scq2 scqd ncq
 
+# if using clang, please also specify -mcx16 for x86-64
 CC = gcc
 CFLAGS = -g -Wall -O3 -pthread -D_GNU_SOURCE
-LDLIBS = -lpthread -lm
+LDLIBS = -ljemalloc -lpthread -lm
 
 ifeq (${VERIFY}, 1)
 	CFLAGS += -DVERIFY
@@ -39,7 +40,12 @@
 ccqueue: CFLAGS += -DCCQUEUE
 msqueue: CFLAGS += -DMSQUEUE
 faa: CFLAGS += -DFAAQ
+cas: CFLAGS += -DFAAQ
 delay: CFLAGS += -DDELAY
+scq: CFLAGS += -DSCQ
+scqd: CFLAGS += -DSCQD
+scq2: CFLAGS += -DSCQ2
+ncq: CFLAGS += -DNCQ
 
 $(TESTS): harness.o
 ifeq (${HALFHALF}, 1)
diff -urN benchmark.orig/ncq.c benchmark/ncq.c
--- benchmark.orig/ncq.c	1969-12-31 19:00:00.000000000 -0500
+++ benchmark/ncq.c	2019-10-22 10:31:31.124409717 -0400
@@ -0,0 +1,29 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "ncq.h"
+
+void queue_init(queue_t * q, int nprocs)
+{
+  lfring_init_empty((struct lfring *) q->ring, NCQ_ORDER);
+}
+
+
+void queue_register(queue_t * q, handle_t * th, int id)
+{
+}
+
+void enqueue(queue_t * q, handle_t * th, void * val)
+{
+  size_t eidx = (size_t) val;
+  lfring_enqueue((struct lfring *) q->ring, NCQ_ORDER, eidx, false);
+}
+
+void * dequeue(queue_t * q, handle_t * th)
+{
+  return (void *) lfring_dequeue((struct lfring *) q->ring, NCQ_ORDER, false);
+}
+
+void queue_free(queue_t * q, handle_t * h)
+{
+}
diff -urN benchmark.orig/ncq.h benchmark/ncq.h
--- benchmark.orig/ncq.h	1969-12-31 19:00:00.000000000 -0500
+++ benchmark/ncq.h	2019-10-22 10:31:31.124409717 -0400
@@ -0,0 +1,23 @@
+#ifndef NCQ_H
+#define NCQ_H
+
+#ifdef NCQ
+
+#include <stddef.h>
+#include "../lfring_naive.h"
+#include "align.h"
+
+#define NCQ_ORDER 16
+#define EMPTY (void *) LFRING_EMPTY
+
+typedef struct _queue_t {
+  char ring[LFRING_SIZE(NCQ_ORDER)];
+} queue_t DOUBLE_CACHE_ALIGNED;
+
+typedef struct _handle_t {
+  int pad;
+} handle_t DOUBLE_CACHE_ALIGNED;
+
+#endif
+
+#endif /* end of include guard: NCQ_H */
diff -urN benchmark.orig/pairwise.c benchmark/pairwise.c
--- benchmark.orig/pairwise.c	2019-10-22 10:44:25.761993916 -0400
+++ benchmark/pairwise.c	2019-10-22 10:31:31.124409717 -0400
@@ -26,7 +26,8 @@
 
   printf("  Number of operations: %ld\n", nops);
 
-  q = align_malloc(PAGE_SIZE, sizeof(queue_t));
+  // FIXME: sizeof(queue_t) varies, allocate 4MB
+  q = align_malloc(PAGE_SIZE, 4194304);
   queue_init(q, nprocs);
 
   hds = align_malloc(PAGE_SIZE, sizeof(handle_t * [nprocs]));
@@ -47,10 +48,10 @@
   int i;
   for (i = 0; i < nops / nprocs; ++i) {
     enqueue(q, th, val);
-    delay_exec(&state);
+//    delay_exec(&state);
 
     val = dequeue(q, th);
-    delay_exec(&state);
+//    delay_exec(&state);
   }
 
   return val;
diff -urN benchmark.orig/queue.h benchmark/queue.h
--- benchmark.orig/queue.h	2019-10-22 10:44:25.761993916 -0400
+++ benchmark/queue.h	2019-10-22 10:31:31.124409717 -0400
@@ -28,6 +28,18 @@
 typedef int queue_t;
 typedef int handle_t;
 
+#elif NCQ
+#include "ncq.h"
+
+#elif SCQ
+#include "scq.h"
+
+#elif SCQ
+#include "scqd.h"
+
+#elif SCQ2
+#include "scq2.h"
+
 #else
 #error "Please specify a queue implementation."
 
diff -urN benchmark.orig/README.md benchmark/README.md
--- benchmark.orig/README.md	2019-10-22 10:44:25.757993930 -0400
+++ benchmark/README.md	2019-10-22 10:43:39.170156671 -0400
@@ -1,4 +1,9 @@
-# Fast Wait Free Queue
+# Benchmark
+
+The benchmark is forked from the "Fast Wait Free Queue" paper. The
+original code is
+available [here](https://github.com/chaoran/fast-wait-free-queue).
+See the original README file below.
 
 This is a benchmark framework for evaluating the performance of concurrent queues. Currently, it contains four concurrent queue implementations. They are:
 
diff -urN benchmark.orig/scq2.c benchmark/scq2.c
--- benchmark.orig/scq2.c	1969-12-31 19:00:00.000000000 -0500
+++ benchmark/scq2.c	2019-10-22 10:31:31.124409717 -0400
@@ -0,0 +1,34 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "scq2.h"
+
+void queue_init(queue_t * q, int nprocs)
+{
+  lfring_ptr_init_empty((struct lfring_ptr *) q->ring, SCQ2_ORDER);
+}
+
+
+void queue_register(queue_t * q, handle_t * th, int id)
+{
+}
+
+void enqueue(queue_t * q, handle_t * th, void * val)
+{
+  lfring_ptr_enqueue((struct lfring_ptr *) q->ring, SCQ2_ORDER, val + 1,
+    false, true);
+}
+
+void * dequeue(queue_t * q, handle_t * th)
+{
+  void *ptr;
+  if (!lfring_ptr_dequeue((struct lfring_ptr *) q->ring, SCQ2_ORDER,
+      &ptr, false))
+    return EMPTY;
+  ptr--;
+  return ptr;
+}
+
+void queue_free(queue_t * q, handle_t * h)
+{
+}
diff -urN benchmark.orig/scq2.h benchmark/scq2.h
--- benchmark.orig/scq2.h	1969-12-31 19:00:00.000000000 -0500
+++ benchmark/scq2.h	2019-10-22 10:31:31.124409717 -0400
@@ -0,0 +1,23 @@
+#ifndef SCQ2_H
+#define SCQ2_H
+
+#ifdef SCQ2
+
+#include <stddef.h>
+#include "../lfring_cas2.h"
+#include "align.h"
+
+#define SCQ2_ORDER 15
+#define EMPTY (void *) -1
+
+typedef struct _queue_t {
+  char ring[LFRING_PTR_SIZE(SCQ2_ORDER)];
+} queue_t DOUBLE_CACHE_ALIGNED;
+
+typedef struct _handle_t {
+  int pad;
+} handle_t DOUBLE_CACHE_ALIGNED;
+
+#endif
+
+#endif /* end of include guard: SCQ_H */
diff -urN benchmark.orig/scq.c benchmark/scq.c
--- benchmark.orig/scq.c	1969-12-31 19:00:00.000000000 -0500
+++ benchmark/scq.c	2019-10-22 10:31:31.124409717 -0400
@@ -0,0 +1,29 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "scq.h"
+
+void queue_init(queue_t * q, int nprocs)
+{
+  lfring_init_empty((struct lfring *) q->ring, SCQ_ORDER);
+}
+
+
+void queue_register(queue_t * q, handle_t * th, int id)
+{
+}
+
+void enqueue(queue_t * q, handle_t * th, void * val)
+{
+  size_t eidx = (size_t) val;
+  lfring_enqueue((struct lfring *) q->ring, SCQ_ORDER, eidx, false);
+}
+
+void * dequeue(queue_t * q, handle_t * th)
+{
+  return (void *) lfring_dequeue((struct lfring *) q->ring, SCQ_ORDER, false);
+}
+
+void queue_free(queue_t * q, handle_t * h)
+{
+}
diff -urN benchmark.orig/scqd.c benchmark/scqd.c
--- benchmark.orig/scqd.c	1969-12-31 19:00:00.000000000 -0500
+++ benchmark/scqd.c	2019-10-22 10:31:31.128409708 -0400
@@ -0,0 +1,39 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "scqd.h"
+
+void queue_init(queue_t * q, int nprocs)
+{
+  lfring_init_empty((struct lfring *) q->aq, SCQD_ORDER);
+  lfring_init_full((struct lfring *) q->fq, SCQD_ORDER);
+}
+
+
+void queue_register(queue_t * q, handle_t * th, int id)
+{
+}
+
+void enqueue(queue_t * q, handle_t * th, void * val)
+{
+  size_t eidx;
+  eidx = lfring_dequeue((struct lfring *) q->fq, SCQD_ORDER, true);
+  if (eidx == LFRING_EMPTY) return;
+  q->val[eidx] = val;
+  lfring_enqueue((struct lfring *) q->aq, SCQD_ORDER, eidx, false);
+}
+
+void * dequeue(queue_t * q, handle_t * th)
+{
+  size_t eidx;
+  void *val;
+  eidx = lfring_dequeue((struct lfring *) q->aq, SCQD_ORDER, false);
+  if (eidx == LFRING_EMPTY) return EMPTY;
+  val = q->val[eidx];
+  lfring_enqueue((struct lfring *) q->fq, SCQD_ORDER, eidx, true);
+  return val;
+}
+
+void queue_free(queue_t * q, handle_t * h)
+{
+}
diff -urN benchmark.orig/scqd.h benchmark/scqd.h
--- benchmark.orig/scqd.h	1969-12-31 19:00:00.000000000 -0500
+++ benchmark/scqd.h	2019-10-22 10:31:31.128409708 -0400
@@ -0,0 +1,25 @@
+#ifndef SCQD_H
+#define SCQD_H
+
+#ifdef SCQD
+
+#include <stddef.h>
+#include "../lfring_cas1.h"
+#include "align.h"
+
+#define SCQD_ORDER 16
+#define EMPTY (void *) LFRING_EMPTY
+
+typedef struct _queue_t {
+  char aq[LFRING_SIZE(SCQD_ORDER)];
+  char fq[LFRING_SIZE(SCQD_ORDER)];
+  void  *val[(1U << SCQD_ORDER)];
+} queue_t DOUBLE_CACHE_ALIGNED;
+
+typedef struct _handle_t {
+  int pad;
+} handle_t DOUBLE_CACHE_ALIGNED;
+
+#endif
+
+#endif /* end of include guard: SCQD_H */
diff -urN benchmark.orig/scq.h benchmark/scq.h
--- benchmark.orig/scq.h	1969-12-31 19:00:00.000000000 -0500
+++ benchmark/scq.h	2019-10-22 10:31:31.124409717 -0400
@@ -0,0 +1,23 @@
+#ifndef SCQ_H
+#define SCQ_H
+
+#ifdef SCQ
+
+#include <stddef.h>
+#include "../lfring_cas1.h"
+#include "align.h"
+
+#define SCQ_ORDER 15
+#define EMPTY (void *) LFRING_EMPTY
+
+typedef struct _queue_t {
+  char ring[LFRING_SIZE(SCQ_ORDER)];
+} queue_t DOUBLE_CACHE_ALIGNED;
+
+typedef struct _handle_t {
+  int pad;
+} handle_t DOUBLE_CACHE_ALIGNED;
+
+#endif
+
+#endif /* end of include guard: SCQ_H */