Debian Codegraph @

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