Debian Codegraph @

regressions/ck_ring/validate/ck_ring_mpmc.c

raw
1 /*
2 * Copyright 2011-2015 Samy Al Bahra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <pthread.h>
32
33 #include <ck_barrier.h>
34 #include <ck_ring.h>
35 #include <ck_spinlock.h>
36 #include "../../common.h"
37
38 #ifndef ITERATIONS
39 #define ITERATIONS 128
40 #endif
41
42 struct context {
43 unsigned int tid;
44 unsigned int previous;
45 unsigned int next;
46 ck_ring_buffer_t *buffer;
47 };
48
49 struct entry {
50 unsigned long value_long;
51 unsigned int magic;
52 unsigned int ref;
53 int tid;
54 int value;
55 };
56
57 static int nthr;
58 static ck_ring_t *ring;
59 static ck_ring_t ring_mpmc CK_CC_CACHELINE;
60 static ck_ring_t ring_mw CK_CC_CACHELINE;
61 static struct affinity a;
62 static int size;
63 static int eb;
64 static ck_barrier_centralized_t barrier = CK_BARRIER_CENTRALIZED_INITIALIZER;
65 static struct context *_context;
66
67 static unsigned int global_counter;
68
69 static void *
70 test_mpmc(void *c)
71 {
72 unsigned int observed = 0;
73 unsigned int enqueue = 0;
74 unsigned int seed;
75 int i, k, j, tid;
76 struct context *context = c;
77 ck_ring_buffer_t *buffer;
78 unsigned int *csp;
79
80 csp = malloc(sizeof(*csp) * nthr);
81 assert(csp != NULL);
82
83 memset(csp, 0, sizeof(*csp) * nthr);
84
85 buffer = context->buffer;
86 if (aff_iterate(&a)) {
87 perror("ERROR: Could not affine thread");
88 exit(EXIT_FAILURE);
89 }
90
91 tid = ck_pr_faa_int(&eb, 1);
92 ck_pr_fence_memory();
93 while (ck_pr_load_int(&eb) != nthr - 1);
94
95 for (i = 0; i < ITERATIONS; i++) {
96 for (j = 0; j < size; j++) {
97 struct entry *o = NULL;
98 int spin;
99
100 /* Keep trying until we encounter at least one node. */
101 if (j & 1) {
102 if (ck_ring_dequeue_mpmc(&ring_mw, buffer, &o) == false)
103 o = NULL;
104 } else {
105 if (ck_ring_trydequeue_mpmc(&ring_mw, buffer, &o) == false)
106 o = NULL;
107 }
108
109 if (o == NULL) {
110 o = malloc(sizeof(*o));
111 if (o == NULL)
112 continue;
113
114 o->value_long = (unsigned long)ck_pr_faa_uint(&global_counter, 1) + 1;
115
116 o->magic = 0xdead;
117 o->ref = 0;
118 o->tid = tid;
119
120 if (ck_ring_enqueue_mpmc(&ring_mw, buffer, o) == false) {
121 free(o);
122 } else {
123 enqueue++;
124 }
125
126 continue;
127 }
128
129 observed++;
130
131 if (o->magic != 0xdead) {
132 ck_error("[%p] (%x)\n",
133 (void *)o, o->magic);
134 }
135
136 o->magic = 0xbeef;
137
138 if (csp[o->tid] >= o->value_long)
139 ck_error("queue semantics violated: %lu <= %lu\n", o->value_long, csp[o->tid]);
140
141 csp[o->tid] = o->value_long;
142
143 if (ck_pr_faa_uint(&o->ref, 1) != 0) {
144 ck_error("[%p] We dequeued twice.\n", (void *)o);
145 }
146
147 if ((i % 4) == 0) {
148 spin = common_rand_r(&seed) % 16384;
149 for (k = 0; k < spin; k++) {
150 ck_pr_stall();
151 }
152 }
153
154 free(o);
155 }
156 }
157
158 fprintf(stderr, "[%d] dequeue=%u enqueue=%u\n", tid, observed, enqueue);
159 return NULL;
160 }
161
162 static void *
163 test_spmc(void *c)
164 {
165 unsigned int observed = 0;
166 unsigned long previous = 0;
167 unsigned int seed;
168 int i, k, j, tid;
169 struct context *context = c;
170 ck_ring_buffer_t *buffer;
171
172 buffer = context->buffer;
173 if (aff_iterate(&a)) {
174 perror("ERROR: Could not affine thread");
175 exit(EXIT_FAILURE);
176 }
177
178 tid = ck_pr_faa_int(&eb, 1);
179 ck_pr_fence_memory();
180 while (ck_pr_load_int(&eb) != nthr - 1);
181
182 for (i = 0; i < ITERATIONS; i++) {
183 for (j = 0; j < size; j++) {
184 struct entry *o;
185 int spin;
186
187 /* Keep trying until we encounter at least one node. */
188 if (j & 1) {
189 while (ck_ring_dequeue_mpmc(&ring_mpmc, buffer,
190 &o) == false);
191 } else {
192 while (ck_ring_trydequeue_mpmc(&ring_mpmc, buffer,
193 &o) == false);
194 }
195
196 observed++;
197 if (o->value < 0
198 || o->value != o->tid
199 || o->magic != 0xdead
200 || (previous != 0 && previous >= o->value_long)) {
201 ck_error("[0x%p] (%x) (%d, %d) >< (0, %d)\n",
202 (void *)o, o->magic, o->tid, o->value, size);
203 }
204
205 o->magic = 0xbeef;
206 o->value = -31337;
207 o->tid = -31338;
208 previous = o->value_long;
209
210 if (ck_pr_faa_uint(&o->ref, 1) != 0) {
211 ck_error("[%p] We dequeued twice.\n", (void *)o);
212 }
213
214 if ((i % 4) == 0) {
215 spin = common_rand_r(&seed) % 16384;
216 for (k = 0; k < spin; k++) {
217 ck_pr_stall();
218 }
219 }
220
221 free(o);
222 }
223 }
224
225 fprintf(stderr, "[%d] Observed %u\n", tid, observed);
226 return NULL;
227 }
228
229 static void *
230 test(void *c)
231 {
232 struct context *context = c;
233 struct entry *entry;
234 unsigned int s;
235 int i, j;
236 bool r;
237 ck_ring_buffer_t *buffer = context->buffer;
238 ck_barrier_centralized_state_t sense =
239 CK_BARRIER_CENTRALIZED_STATE_INITIALIZER;
240
241 if (aff_iterate(&a)) {
242 perror("ERROR: Could not affine thread");
243 exit(EXIT_FAILURE);
244 }
245
246 if (context->tid == 0) {
247 struct entry *entries;
248
249 entries = malloc(sizeof(struct entry) * size);
250 assert(entries != NULL);
251
252 if (ck_ring_size(ring) != 0) {
253 ck_error("More entries than expected: %u > 0\n",
254 ck_ring_size(ring));
255 }
256
257 for (i = 0; i < size; i++) {
258 entries[i].value = i;
259 entries[i].tid = 0;
260
261 if (true) {
262 r = ck_ring_enqueue_mpmc(ring, buffer,
263 entries + i);
264 } else {
265 r = ck_ring_enqueue_mpmc_size(ring, buffer,
266 entries + i, &s);
267
268 if ((int)s != i) {
269 ck_error("Size is %u, expected %d.\n",
270 s, size);
271 }
272 }
273
274 assert(r != false);
275 }
276
277 if (ck_ring_size(ring) != (unsigned int)size) {
278 ck_error("Less entries than expected: %u < %d\n",
279 ck_ring_size(ring), size);
280 }
281
282 if (ck_ring_capacity(ring) != ck_ring_size(ring) + 1) {
283 ck_error("Capacity less than expected: %u < %u\n",
284 ck_ring_size(ring), ck_ring_capacity(ring));
285 }
286 }
287
288 /*
289 * Wait for all threads. The idea here is to maximize the contention.
290 */
291 ck_barrier_centralized(&barrier, &sense, nthr);
292
293 for (i = 0; i < ITERATIONS; i++) {
294 for (j = 0; j < size; j++) {
295 buffer = _context[context->previous].buffer;
296 while (ck_ring_dequeue_mpmc(ring + context->previous,
297 buffer, &entry) == false);
298
299 if (context->previous != (unsigned int)entry->tid) {
300 ck_error("[%u:%p] %u != %u\n",
301 context->tid, (void *)entry, entry->tid, context->previous);
302 }
303
304 if (entry->value < 0 || entry->value >= size) {
305 ck_error("[%u:%p] %u </> %u\n",
306 context->tid, (void *)entry, entry->tid, context->previous);
307 }
308
309 entry->tid = context->tid;
310 buffer = context->buffer;
311
312 if (true) {
313 r = ck_ring_enqueue_mpmc(ring + context->tid,
314 buffer, entry);
315 } else {
316 r = ck_ring_enqueue_mpmc_size(ring + context->tid,
317 buffer, entry, &s);
318
319 if ((int)s >= size) {
320 ck_error("Size %u out of range of %d\n",
321 s, size);
322 }
323 }
324 assert(r == true);
325 }
326 }
327
328 return NULL;
329 }
330
331 int
332 main(int argc, char *argv[])
333 {
334 int i, r;
335 unsigned long l;
336 pthread_t *thread;
337 ck_ring_buffer_t *buffer;
338
339 if (argc != 4) {
340 ck_error("Usage: validate <threads> <affinity delta> <size>\n");
341 }
342
343 a.request = 0;
344 a.delta = atoi(argv[2]);
345
346 nthr = atoi(argv[1]);
347 assert(nthr >= 1);
348
349 size = atoi(argv[3]);
350 assert(size >= 4 && (size & size - 1) == 0);
351 size -= 1;
352
353 ring = malloc(sizeof(ck_ring_t) * nthr);
354 assert(ring);
355
356 _context = malloc(sizeof(*_context) * nthr);
357 assert(_context);
358
359 thread = malloc(sizeof(pthread_t) * nthr);
360 assert(thread);
361 fprintf(stderr, "SPSC test:");
362 for (i = 0; i < nthr; i++) {
363 _context[i].tid = i;
364 if (i == 0) {
365 _context[i].previous = nthr - 1;
366 _context[i].next = i + 1;
367 } else if (i == nthr - 1) {
368 _context[i].next = 0;
369 _context[i].previous = i - 1;
370 } else {
371 _context[i].next = i + 1;
372 _context[i].previous = i - 1;
373 }
374
375 buffer = malloc(sizeof(ck_ring_buffer_t) * (size + 1));
376 assert(buffer);
377 memset(buffer, 0, sizeof(ck_ring_buffer_t) * (size + 1));
378 _context[i].buffer = buffer;
379 ck_ring_init(ring + i, size + 1);
380 r = pthread_create(thread + i, NULL, test, _context + i);
381 assert(r == 0);
382 }
383
384 for (i = 0; i < nthr; i++)
385 pthread_join(thread[i], NULL);
386
387 fprintf(stderr, " done\n");
388
389 fprintf(stderr, "SPMC test:\n");
390 buffer = malloc(sizeof(ck_ring_buffer_t) * (size + 1));
391 assert(buffer);
392 memset(buffer, 0, sizeof(void *) * (size + 1));
393 ck_ring_init(&ring_mpmc, size + 1);
394 for (i = 0; i < nthr - 1; i++) {
395 _context[i].buffer = buffer;
396 r = pthread_create(thread + i, NULL, test_spmc, _context + i);
397 assert(r == 0);
398 }
399
400 for (l = 0; l < (unsigned long)size * ITERATIONS * (nthr - 1) ; l++) {
401 struct entry *entry = malloc(sizeof *entry);
402
403 assert(entry != NULL);
404 entry->value_long = l;
405 entry->value = (int)l;
406 entry->tid = (int)l;
407 entry->magic = 0xdead;
408 entry->ref = 0;
409
410 /* Wait until queue is not full. */
411 if (l & 1) {
412 while (ck_ring_enqueue_mpmc(&ring_mpmc,
413 buffer,
414 entry) == false)
415 ck_pr_stall();
416 } else {
417 unsigned int s;
418
419 while (ck_ring_enqueue_mpmc_size(&ring_mpmc,
420 buffer, entry, &s) == false) {
421 ck_pr_stall();
422 }
423
424 if ((int)s >= (size * ITERATIONS * (nthr - 1))) {
425 ck_error("MPMC: Unexpected size of %u\n", s);
426 }
427 }
428 }
429
430 for (i = 0; i < nthr - 1; i++)
431 pthread_join(thread[i], NULL);
432 ck_pr_store_int(&eb, 0);
433 fprintf(stderr, "MPMC test:\n");
434 buffer = malloc(sizeof(ck_ring_buffer_t) * (size + 1));
435 assert(buffer);
436 memset(buffer, 0, sizeof(void *) * (size + 1));
437 ck_ring_init(&ring_mw, size + 1);
438 for (i = 0; i < nthr - 1; i++) {
439 _context[i].buffer = buffer;
440 r = pthread_create(thread + i, NULL, test_mpmc, _context + i);
441 assert(r == 0);
442 }
443
444 for (i = 0; i < nthr - 1; i++)
445 pthread_join(thread[i], NULL);
446
447 return (0);
448 }