Debian Codegraph @

regressions/ck_ring/validate/ck_ring_spsc.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 <pthread.h>
31
32 #include <ck_barrier.h>
33 #include <ck_ring.h>
34 #include "../../common.h"
35
36 #ifndef ITERATIONS
37 #define ITERATIONS 128
38 #endif
39
40 struct context {
41 unsigned int tid;
42 unsigned int previous;
43 unsigned int next;
44 void *buffer;
45 };
46
47 struct entry {
48 int tid;
49 int value;
50 };
51
52 static int nthr;
53 static ck_ring_t *ring;
54 static struct affinity a;
55 static int size;
56 static ck_barrier_centralized_t barrier = CK_BARRIER_CENTRALIZED_INITIALIZER;
57 static struct context *_context;
58
59 static void *
60 test(void *c)
61 {
62 struct context *context = c;
63 struct entry *entry;
64 unsigned int s;
65 int i, j;
66 bool r;
67 ck_barrier_centralized_state_t sense =
68 CK_BARRIER_CENTRALIZED_STATE_INITIALIZER;
69 ck_ring_buffer_t *buffer;
70
71 if (aff_iterate(&a)) {
72 perror("ERROR: Could not affine thread");
73 exit(EXIT_FAILURE);
74 }
75
76 buffer = context->buffer;
77 if (context->tid == 0) {
78 struct entry *entries;
79
80 entries = malloc(sizeof(struct entry) * size);
81 assert(entries != NULL);
82
83 if (ck_ring_size(ring) != 0) {
84 ck_error("More entries than expected: %u > 0\n",
85 ck_ring_size(ring));
86 }
87
88 for (i = 0; i < size; i++) {
89 entries[i].value = i;
90 entries[i].tid = 0;
91
92 if (i & 1) {
93 r = ck_ring_enqueue_spsc(ring, buffer,
94 entries + i);
95 } else {
96 r = ck_ring_enqueue_spsc_size(ring,
97 buffer, entries + i, &s);
98
99 if ((int)s != i) {
100 ck_error("Size is %u, expected %d\n",
101 s, i + 1);
102 }
103 }
104
105 assert(r != false);
106 }
107
108 if (ck_ring_size(ring) != (unsigned int)size) {
109 ck_error("Less entries than expected: %u < %d\n",
110 ck_ring_size(ring), size);
111 }
112
113 if (ck_ring_capacity(ring) != ck_ring_size(ring) + 1) {
114 ck_error("Capacity less than expected: %u < %u\n",
115 ck_ring_size(ring), ck_ring_capacity(ring));
116 }
117 }
118
119 ck_barrier_centralized(&barrier, &sense, nthr);
120
121 for (i = 0; i < ITERATIONS; i++) {
122 for (j = 0; j < size; j++) {
123 buffer = _context[context->previous].buffer;
124 while (ck_ring_dequeue_spsc(ring + context->previous,
125 buffer, &entry) == false);
126
127 if (context->previous != (unsigned int)entry->tid) {
128 ck_error("[%u:%p] %u != %u\n",
129 context->tid, (void *)entry, entry->tid, context->previous);
130 }
131
132 if (entry->value != j) {
133 ck_error("[%u:%p] %u != %u\n",
134 context->tid, (void *)entry, entry->tid, context->previous);
135 }
136
137 entry->tid = context->tid;
138 buffer = context->buffer;
139 if (i & 1) {
140 r = ck_ring_enqueue_spsc(ring + context->tid,
141 buffer, entry);
142 } else {
143 r = ck_ring_enqueue_spsc_size(ring +
144 context->tid, buffer, entry, &s);
145
146 if ((int)s >= size) {
147 ck_error("Size %u is out of range %d\n",
148 s, size);
149 }
150 }
151 assert(r == true);
152 }
153 }
154
155 return NULL;
156 }
157
158 int
159 main(int argc, char *argv[])
160 {
161 int i, r;
162 ck_ring_buffer_t *buffer;
163 pthread_t *thread;
164
165 if (argc != 4) {
166 ck_error("Usage: validate <threads> <affinity delta> <size>\n");
167 }
168
169 a.request = 0;
170 a.delta = atoi(argv[2]);
171
172 nthr = atoi(argv[1]);
173 assert(nthr >= 1);
174
175 size = atoi(argv[3]);
176 assert(size >= 4 && (size & size - 1) == 0);
177 size -= 1;
178
179 ring = malloc(sizeof(ck_ring_t) * nthr);
180 assert(ring);
181
182 _context = malloc(sizeof(*_context) * nthr);
183 assert(_context);
184
185 thread = malloc(sizeof(pthread_t) * nthr);
186 assert(thread);
187
188 for (i = 0; i < nthr; i++) {
189 _context[i].tid = i;
190 if (i == 0) {
191 _context[i].previous = nthr - 1;
192 _context[i].next = i + 1;
193 } else if (i == nthr - 1) {
194 _context[i].next = 0;
195 _context[i].previous = i - 1;
196 } else {
197 _context[i].next = i + 1;
198 _context[i].previous = i - 1;
199 }
200
201 buffer = malloc(sizeof(ck_ring_buffer_t) * (size + 1));
202 assert(buffer);
203 _context[i].buffer = buffer;
204 ck_ring_init(ring + i, size + 1);
205 r = pthread_create(thread + i, NULL, test, _context + i);
206 assert(r == 0);
207 }
208
209 for (i = 0; i < nthr; i++)
210 pthread_join(thread[i], NULL);
211
212 return (0);
213 }