Debian Codegraph @

regressions/ck_ring/validate/ck_ring_spmc.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_spmc CK_CC_CACHELINE;
60 static struct affinity a;
61 static int size;
62 static int eb;
63 static ck_barrier_centralized_t barrier = CK_BARRIER_CENTRALIZED_INITIALIZER;
64 static struct context *_context;
65
66 static void *
67 test_spmc(void *c)
68 {
69 unsigned int observed = 0;
70 unsigned long previous = 0;
71 unsigned int seed;
72 int i, k, j, tid;
73 struct context *context = c;
74 ck_ring_buffer_t *buffer;
75
76 buffer = context->buffer;
77 if (aff_iterate(&a)) {
78 perror("ERROR: Could not affine thread");
79 exit(EXIT_FAILURE);
80 }
81
82 tid = ck_pr_faa_int(&eb, 1);
83 ck_pr_fence_memory();
84 while (ck_pr_load_int(&eb) != nthr - 1);
85
86 for (i = 0; i < ITERATIONS; i++) {
87 for (j = 0; j < size; j++) {
88 struct entry *o;
89 int spin;
90
91 /* Keep trying until we encounter at least one node. */
92 if (j & 1) {
93 while (ck_ring_dequeue_spmc(&ring_spmc, buffer,
94 &o) == false);
95 } else {
96 while (ck_ring_trydequeue_spmc(&ring_spmc, buffer,
97 &o) == false);
98 }
99
100 observed++;
101 if (o->value < 0
102 || o->value != o->tid
103 || o->magic != 0xdead
104 || (previous != 0 && previous >= o->value_long)) {
105 ck_error("[0x%p] (%x) (%d, %d) >< (0, %d)\n",
106 (void *)o, o->magic, o->tid, o->value, size);
107 }
108
109 o->magic = 0xbeef;
110 o->value = -31337;
111 o->tid = -31338;
112 previous = o->value_long;
113
114 if (ck_pr_faa_uint(&o->ref, 1) != 0) {
115 ck_error("[%p] We dequeued twice.\n", (void *)o);
116 }
117
118 if ((i % 4) == 0) {
119 spin = common_rand_r(&seed) % 16384;
120 for (k = 0; k < spin; k++) {
121 ck_pr_stall();
122 }
123 }
124
125 free(o);
126 }
127 }
128
129 fprintf(stderr, "[%d] Observed %u\n", tid, observed);
130 return NULL;
131 }
132
133 static void *
134 test(void *c)
135 {
136 struct context *context = c;
137 struct entry *entry;
138 unsigned int s;
139 int i, j;
140 bool r;
141 ck_ring_buffer_t *buffer = context->buffer;
142 ck_barrier_centralized_state_t sense =
143 CK_BARRIER_CENTRALIZED_STATE_INITIALIZER;
144
145 if (aff_iterate(&a)) {
146 perror("ERROR: Could not affine thread");
147 exit(EXIT_FAILURE);
148 }
149
150 if (context->tid == 0) {
151 struct entry *entries;
152
153 entries = malloc(sizeof(struct entry) * size);
154 assert(entries != NULL);
155
156 if (ck_ring_size(ring) != 0) {
157 ck_error("More entries than expected: %u > 0\n",
158 ck_ring_size(ring));
159 }
160
161 for (i = 0; i < size; i++) {
162 entries[i].value = i;
163 entries[i].tid = 0;
164
165 if (i & 1) {
166 r = ck_ring_enqueue_spmc(ring, buffer,
167 entries + i);
168 } else {
169 r = ck_ring_enqueue_spmc_size(ring, buffer,
170 entries + i, &s);
171
172 if ((int)s != i) {
173 ck_error("Size is %u, expected %d.\n",
174 s, size);
175 }
176 }
177
178 assert(r != false);
179 }
180
181 if (ck_ring_size(ring) != (unsigned int)size) {
182 ck_error("Less entries than expected: %u < %d\n",
183 ck_ring_size(ring), size);
184 }
185
186 if (ck_ring_capacity(ring) != ck_ring_size(ring) + 1) {
187 ck_error("Capacity less than expected: %u < %u\n",
188 ck_ring_size(ring), ck_ring_capacity(ring));
189 }
190 }
191
192 /*
193 * Wait for all threads. The idea here is to maximize the contention.
194 */
195 ck_barrier_centralized(&barrier, &sense, nthr);
196
197 for (i = 0; i < ITERATIONS; i++) {
198 for (j = 0; j < size; j++) {
199 buffer = _context[context->previous].buffer;
200 while (ck_ring_dequeue_spmc(ring + context->previous,
201 buffer, &entry) == false);
202
203 if (context->previous != (unsigned int)entry->tid) {
204 ck_error("[%u:%p] %u != %u\n",
205 context->tid, (void *)entry, entry->tid, context->previous);
206 }
207
208 if (entry->value < 0 || entry->value >= size) {
209 ck_error("[%u:%p] %u </> %u\n",
210 context->tid, (void *)entry, entry->tid, context->previous);
211 }
212
213 entry->tid = context->tid;
214 buffer = context->buffer;
215
216 if (i & 1) {
217 r = ck_ring_enqueue_spmc(ring + context->tid,
218 buffer, entry);
219 } else {
220 r = ck_ring_enqueue_spmc_size(ring + context->tid,
221 buffer, entry, &s);
222
223 if ((int)s >= size) {
224 ck_error("Size %u out of range of %d\n",
225 s, size);
226 }
227 }
228 assert(r == true);
229 }
230 }
231
232 return NULL;
233 }
234
235 int
236 main(int argc, char *argv[])
237 {
238 int i, r;
239 unsigned long l;
240 pthread_t *thread;
241 ck_ring_buffer_t *buffer;
242
243 if (argc != 4) {
244 ck_error("Usage: validate <threads> <affinity delta> <size>\n");
245 }
246
247 a.request = 0;
248 a.delta = atoi(argv[2]);
249
250 nthr = atoi(argv[1]);
251 assert(nthr >= 1);
252
253 size = atoi(argv[3]);
254 assert(size >= 4 && (size & size - 1) == 0);
255 size -= 1;
256
257 ring = malloc(sizeof(ck_ring_t) * nthr);
258 assert(ring);
259
260 _context = malloc(sizeof(*_context) * nthr);
261 assert(_context);
262
263 thread = malloc(sizeof(pthread_t) * nthr);
264 assert(thread);
265
266 fprintf(stderr, "SPSC test:");
267 for (i = 0; i < nthr; i++) {
268 _context[i].tid = i;
269 if (i == 0) {
270 _context[i].previous = nthr - 1;
271 _context[i].next = i + 1;
272 } else if (i == nthr - 1) {
273 _context[i].next = 0;
274 _context[i].previous = i - 1;
275 } else {
276 _context[i].next = i + 1;
277 _context[i].previous = i - 1;
278 }
279
280 buffer = malloc(sizeof(ck_ring_buffer_t) * (size + 1));
281 assert(buffer);
282 memset(buffer, 0, sizeof(ck_ring_buffer_t) * (size + 1));
283 _context[i].buffer = buffer;
284 ck_ring_init(ring + i, size + 1);
285 r = pthread_create(thread + i, NULL, test, _context + i);
286 assert(r == 0);
287 }
288
289 for (i = 0; i < nthr; i++)
290 pthread_join(thread[i], NULL);
291
292 fprintf(stderr, " done\n");
293
294 fprintf(stderr, "SPMC test:\n");
295 buffer = malloc(sizeof(ck_ring_buffer_t) * (size + 1));
296 assert(buffer);
297 memset(buffer, 0, sizeof(void *) * (size + 1));
298 ck_ring_init(&ring_spmc, size + 1);
299 for (i = 0; i < nthr - 1; i++) {
300 _context[i].buffer = buffer;
301 r = pthread_create(thread + i, NULL, test_spmc, _context + i);
302 assert(r == 0);
303 }
304
305 for (l = 0; l < (unsigned long)size * ITERATIONS * (nthr - 1) ; l++) {
306 struct entry *entry = malloc(sizeof *entry);
307
308 assert(entry != NULL);
309 entry->value_long = l;
310 entry->value = (int)l;
311 entry->tid = (int)l;
312 entry->magic = 0xdead;
313 entry->ref = 0;
314
315 /* Wait until queue is not full. */
316 if (l & 1) {
317 while (ck_ring_enqueue_spmc(&ring_spmc,
318 buffer,
319 entry) == false)
320 ck_pr_stall();
321 } else {
322 unsigned int s;
323
324 while (ck_ring_enqueue_spmc_size(&ring_spmc,
325 buffer, entry, &s) == false) {
326 ck_pr_stall();
327 }
328
329 if ((int)s >= (size * ITERATIONS * (nthr - 1))) {
330 ck_error("MPMC: Unexpected size of %u\n", s);
331 }
332 }
333 }
334
335 for (i = 0; i < nthr - 1; i++)
336 pthread_join(thread[i], NULL);
337
338 return (0);
339 }
340