Commit | Line | Data |
---|---|---|
ee566d46 MD |
1 | /* |
2 | * test_urcu_wfq.c | |
3 | * | |
4 | * Userspace RCU library - example RCU-based lock-free queue | |
5 | * | |
6 | * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License along | |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
22 | */ | |
23 | ||
24 | #define _GNU_SOURCE | |
f5ab766e | 25 | #include "config.h" |
ee566d46 MD |
26 | #include <stdio.h> |
27 | #include <pthread.h> | |
28 | #include <stdlib.h> | |
29 | #include <stdint.h> | |
30 | #include <stdbool.h> | |
31 | #include <string.h> | |
32 | #include <sys/types.h> | |
33 | #include <sys/wait.h> | |
34 | #include <unistd.h> | |
35 | #include <stdio.h> | |
36 | #include <assert.h> | |
ee566d46 MD |
37 | #include <errno.h> |
38 | ||
39 | #include <urcu/arch.h> | |
bd252a04 | 40 | #include <urcu/tls-compat.h> |
2953b501 | 41 | #include "cpuset.h" |
94df6318 | 42 | #include "thread-id.h" |
65fcc7e9 | 43 | |
ee566d46 MD |
44 | /* hardcoded number of CPUs */ |
45 | #define NR_CPUS 16384 | |
46 | ||
ee566d46 MD |
47 | #ifndef DYNAMIC_LINK_TEST |
48 | #define _LGPL_SOURCE | |
49 | #endif | |
14748510 MD |
50 | |
51 | /* Remove deprecation warnings from test build. */ | |
52 | #define CDS_WFQ_DEPRECATED | |
53 | ||
ee566d46 | 54 | #include <urcu.h> |
4afee0a7 | 55 | #include <urcu/wfqueue.h> |
ee566d46 MD |
56 | |
57 | static volatile int test_go, test_stop; | |
58 | ||
59 | static unsigned long rduration; | |
60 | ||
61 | static unsigned long duration; | |
62 | ||
63 | /* read-side C.S. duration, in loops */ | |
64 | static unsigned long wdelay; | |
65 | ||
ab0aacbe | 66 | static inline void loop_sleep(unsigned long loops) |
ee566d46 | 67 | { |
ab0aacbe | 68 | while (loops-- != 0) |
06f22bdb | 69 | caa_cpu_relax(); |
ee566d46 MD |
70 | } |
71 | ||
72 | static int verbose_mode; | |
73 | ||
74 | #define printf_verbose(fmt, args...) \ | |
75 | do { \ | |
76 | if (verbose_mode) \ | |
77 | printf(fmt, args); \ | |
78 | } while (0) | |
79 | ||
80 | static unsigned int cpu_affinities[NR_CPUS]; | |
81 | static unsigned int next_aff = 0; | |
82 | static int use_affinity = 0; | |
83 | ||
84 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
85 | ||
ee566d46 MD |
86 | static void set_affinity(void) |
87 | { | |
95bc7fb9 | 88 | #if HAVE_SCHED_SETAFFINITY |
ee566d46 | 89 | cpu_set_t mask; |
95bc7fb9 MD |
90 | int cpu, ret; |
91 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
ee566d46 MD |
92 | |
93 | if (!use_affinity) | |
94 | return; | |
95 | ||
96 | #if HAVE_SCHED_SETAFFINITY | |
97 | ret = pthread_mutex_lock(&affinity_mutex); | |
98 | if (ret) { | |
99 | perror("Error in pthread mutex lock"); | |
100 | exit(-1); | |
101 | } | |
102 | cpu = cpu_affinities[next_aff++]; | |
103 | ret = pthread_mutex_unlock(&affinity_mutex); | |
104 | if (ret) { | |
105 | perror("Error in pthread mutex unlock"); | |
106 | exit(-1); | |
107 | } | |
108 | ||
109 | CPU_ZERO(&mask); | |
110 | CPU_SET(cpu, &mask); | |
111 | #if SCHED_SETAFFINITY_ARGS == 2 | |
112 | sched_setaffinity(0, &mask); | |
113 | #else | |
114 | sched_setaffinity(0, sizeof(mask), &mask); | |
115 | #endif | |
116 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
117 | } | |
118 | ||
119 | /* | |
120 | * returns 0 if test should end. | |
121 | */ | |
122 | static int test_duration_dequeue(void) | |
123 | { | |
124 | return !test_stop; | |
125 | } | |
126 | ||
127 | static int test_duration_enqueue(void) | |
128 | { | |
129 | return !test_stop; | |
130 | } | |
131 | ||
bd252a04 MD |
132 | static DEFINE_URCU_TLS(unsigned long long, nr_dequeues); |
133 | static DEFINE_URCU_TLS(unsigned long long, nr_enqueues); | |
ee566d46 | 134 | |
bd252a04 MD |
135 | static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues); |
136 | static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues); | |
ee566d46 MD |
137 | |
138 | static unsigned int nr_enqueuers; | |
139 | static unsigned int nr_dequeuers; | |
140 | ||
16aa9ee8 | 141 | static struct cds_wfq_queue q; |
ee566d46 MD |
142 | |
143 | void *thr_enqueuer(void *_count) | |
144 | { | |
145 | unsigned long long *count = _count; | |
146 | ||
94df6318 MD |
147 | printf_verbose("thread_begin %s, tid %lu\n", |
148 | "enqueuer", urcu_get_thread_id()); | |
ee566d46 MD |
149 | |
150 | set_affinity(); | |
151 | ||
ee566d46 MD |
152 | while (!test_go) |
153 | { | |
154 | } | |
5481ddb3 | 155 | cmm_smp_mb(); |
ee566d46 MD |
156 | |
157 | for (;;) { | |
16aa9ee8 | 158 | struct cds_wfq_node *node = malloc(sizeof(*node)); |
ee566d46 MD |
159 | if (!node) |
160 | goto fail; | |
16aa9ee8 DG |
161 | cds_wfq_node_init(node); |
162 | cds_wfq_enqueue(&q, node); | |
bd252a04 | 163 | URCU_TLS(nr_successful_enqueues)++; |
ee566d46 | 164 | |
a0b7f7ea | 165 | if (caa_unlikely(wdelay)) |
ee566d46 MD |
166 | loop_sleep(wdelay); |
167 | fail: | |
bd252a04 | 168 | URCU_TLS(nr_enqueues)++; |
a0b7f7ea | 169 | if (caa_unlikely(!test_duration_enqueue())) |
ee566d46 MD |
170 | break; |
171 | } | |
172 | ||
bd252a04 MD |
173 | count[0] = URCU_TLS(nr_enqueues); |
174 | count[1] = URCU_TLS(nr_successful_enqueues); | |
94df6318 MD |
175 | printf_verbose("enqueuer thread_end, tid %lu, " |
176 | "enqueues %llu successful_enqueues %llu\n", | |
177 | urcu_get_thread_id(), | |
178 | URCU_TLS(nr_enqueues), | |
179 | URCU_TLS(nr_successful_enqueues)); | |
ee566d46 MD |
180 | return ((void*)1); |
181 | ||
182 | } | |
183 | ||
ee566d46 MD |
184 | void *thr_dequeuer(void *_count) |
185 | { | |
186 | unsigned long long *count = _count; | |
187 | ||
94df6318 MD |
188 | printf_verbose("thread_begin %s, tid %lu\n", |
189 | "dequeuer", urcu_get_thread_id()); | |
ee566d46 MD |
190 | |
191 | set_affinity(); | |
192 | ||
ee566d46 MD |
193 | while (!test_go) |
194 | { | |
195 | } | |
5481ddb3 | 196 | cmm_smp_mb(); |
ee566d46 MD |
197 | |
198 | for (;;) { | |
16aa9ee8 | 199 | struct cds_wfq_node *node = cds_wfq_dequeue_blocking(&q); |
ee566d46 MD |
200 | |
201 | if (node) { | |
4afee0a7 | 202 | free(node); |
bd252a04 | 203 | URCU_TLS(nr_successful_dequeues)++; |
ee566d46 MD |
204 | } |
205 | ||
bd252a04 | 206 | URCU_TLS(nr_dequeues)++; |
a0b7f7ea | 207 | if (caa_unlikely(!test_duration_dequeue())) |
ee566d46 | 208 | break; |
a0b7f7ea | 209 | if (caa_unlikely(rduration)) |
ee566d46 MD |
210 | loop_sleep(rduration); |
211 | } | |
212 | ||
94df6318 MD |
213 | printf_verbose("dequeuer thread_end, tid %lu, " |
214 | "dequeues %llu, successful_dequeues %llu\n", | |
215 | urcu_get_thread_id(), | |
216 | URCU_TLS(nr_dequeues), | |
217 | URCU_TLS(nr_successful_dequeues)); | |
bd252a04 MD |
218 | count[0] = URCU_TLS(nr_dequeues); |
219 | count[1] = URCU_TLS(nr_successful_dequeues); | |
ee566d46 MD |
220 | return ((void*)2); |
221 | } | |
222 | ||
16aa9ee8 | 223 | void test_end(struct cds_wfq_queue *q, unsigned long long *nr_dequeues) |
ee566d46 | 224 | { |
16aa9ee8 | 225 | struct cds_wfq_node *node; |
ee566d46 MD |
226 | |
227 | do { | |
16aa9ee8 | 228 | node = cds_wfq_dequeue_blocking(q); |
ee566d46 | 229 | if (node) { |
4afee0a7 | 230 | free(node); |
ee566d46 MD |
231 | (*nr_dequeues)++; |
232 | } | |
233 | } while (node); | |
234 | } | |
235 | ||
236 | void show_usage(int argc, char **argv) | |
237 | { | |
06637138 MD |
238 | printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n", |
239 | argv[0]); | |
240 | printf("OPTIONS:\n"); | |
241 | printf(" [-d delay] (enqueuer period (in loops))\n"); | |
242 | printf(" [-c duration] (dequeuer period (in loops))\n"); | |
243 | printf(" [-v] (verbose output)\n"); | |
244 | printf(" [-a cpu#] [-a cpu#]... (affinity)\n"); | |
ee566d46 MD |
245 | printf("\n"); |
246 | } | |
247 | ||
248 | int main(int argc, char **argv) | |
249 | { | |
250 | int err; | |
251 | pthread_t *tid_enqueuer, *tid_dequeuer; | |
252 | void *tret; | |
253 | unsigned long long *count_enqueuer, *count_dequeuer; | |
254 | unsigned long long tot_enqueues = 0, tot_dequeues = 0; | |
255 | unsigned long long tot_successful_enqueues = 0, | |
256 | tot_successful_dequeues = 0; | |
257 | unsigned long long end_dequeues = 0; | |
258 | int i, a; | |
259 | ||
260 | if (argc < 4) { | |
261 | show_usage(argc, argv); | |
262 | return -1; | |
263 | } | |
264 | ||
265 | err = sscanf(argv[1], "%u", &nr_dequeuers); | |
266 | if (err != 1) { | |
267 | show_usage(argc, argv); | |
268 | return -1; | |
269 | } | |
270 | ||
271 | err = sscanf(argv[2], "%u", &nr_enqueuers); | |
272 | if (err != 1) { | |
273 | show_usage(argc, argv); | |
274 | return -1; | |
275 | } | |
276 | ||
277 | err = sscanf(argv[3], "%lu", &duration); | |
278 | if (err != 1) { | |
279 | show_usage(argc, argv); | |
280 | return -1; | |
281 | } | |
282 | ||
283 | for (i = 4; i < argc; i++) { | |
284 | if (argv[i][0] != '-') | |
285 | continue; | |
286 | switch (argv[i][1]) { | |
287 | case 'a': | |
288 | if (argc < i + 2) { | |
289 | show_usage(argc, argv); | |
290 | return -1; | |
291 | } | |
292 | a = atoi(argv[++i]); | |
293 | cpu_affinities[next_aff++] = a; | |
294 | use_affinity = 1; | |
295 | printf_verbose("Adding CPU %d affinity\n", a); | |
296 | break; | |
297 | case 'c': | |
298 | if (argc < i + 2) { | |
299 | show_usage(argc, argv); | |
300 | return -1; | |
301 | } | |
302 | rduration = atol(argv[++i]); | |
303 | break; | |
304 | case 'd': | |
305 | if (argc < i + 2) { | |
306 | show_usage(argc, argv); | |
307 | return -1; | |
308 | } | |
309 | wdelay = atol(argv[++i]); | |
310 | break; | |
311 | case 'v': | |
312 | verbose_mode = 1; | |
313 | break; | |
314 | } | |
315 | } | |
316 | ||
317 | printf_verbose("running test for %lu seconds, %u enqueuers, " | |
318 | "%u dequeuers.\n", | |
319 | duration, nr_enqueuers, nr_dequeuers); | |
320 | printf_verbose("Writer delay : %lu loops.\n", rduration); | |
321 | printf_verbose("Reader duration : %lu loops.\n", wdelay); | |
94df6318 MD |
322 | printf_verbose("thread %-6s, tid %lu\n", |
323 | "main", urcu_get_thread_id()); | |
ee566d46 | 324 | |
9aa14175 MD |
325 | tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer)); |
326 | tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer)); | |
327 | count_enqueuer = calloc(nr_enqueuers, 2 * sizeof(*count_enqueuer)); | |
328 | count_dequeuer = calloc(nr_dequeuers, 2 * sizeof(*count_dequeuer)); | |
16aa9ee8 | 329 | cds_wfq_init(&q); |
ee566d46 MD |
330 | |
331 | next_aff = 0; | |
332 | ||
333 | for (i = 0; i < nr_enqueuers; i++) { | |
334 | err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer, | |
335 | &count_enqueuer[2 * i]); | |
336 | if (err != 0) | |
337 | exit(1); | |
338 | } | |
339 | for (i = 0; i < nr_dequeuers; i++) { | |
340 | err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer, | |
341 | &count_dequeuer[2 * i]); | |
342 | if (err != 0) | |
343 | exit(1); | |
344 | } | |
345 | ||
5481ddb3 | 346 | cmm_smp_mb(); |
ee566d46 MD |
347 | |
348 | test_go = 1; | |
349 | ||
350 | for (i = 0; i < duration; i++) { | |
351 | sleep(1); | |
352 | if (verbose_mode) | |
d863e153 | 353 | (void) write(1, ".", 1); |
ee566d46 MD |
354 | } |
355 | ||
356 | test_stop = 1; | |
357 | ||
358 | for (i = 0; i < nr_enqueuers; i++) { | |
359 | err = pthread_join(tid_enqueuer[i], &tret); | |
360 | if (err != 0) | |
361 | exit(1); | |
362 | tot_enqueues += count_enqueuer[2 * i]; | |
363 | tot_successful_enqueues += count_enqueuer[2 * i + 1]; | |
364 | } | |
365 | for (i = 0; i < nr_dequeuers; i++) { | |
366 | err = pthread_join(tid_dequeuer[i], &tret); | |
367 | if (err != 0) | |
368 | exit(1); | |
369 | tot_dequeues += count_dequeuer[2 * i]; | |
370 | tot_successful_dequeues += count_dequeuer[2 * i + 1]; | |
371 | } | |
372 | ||
373 | test_end(&q, &end_dequeues); | |
374 | ||
375 | printf_verbose("total number of enqueues : %llu, dequeues %llu\n", | |
376 | tot_enqueues, tot_dequeues); | |
377 | printf_verbose("total number of successful enqueues : %llu, " | |
378 | "successful dequeues %llu\n", | |
379 | tot_successful_enqueues, tot_successful_dequeues); | |
380 | printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu " | |
381 | "nr_dequeuers %3u " | |
382 | "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu " | |
383 | "successful enqueues %12llu successful dequeues %12llu " | |
384 | "end_dequeues %llu nr_ops %12llu\n", | |
385 | argv[0], duration, nr_enqueuers, wdelay, | |
386 | nr_dequeuers, rduration, tot_enqueues, tot_dequeues, | |
387 | tot_successful_enqueues, | |
388 | tot_successful_dequeues, end_dequeues, | |
389 | tot_enqueues + tot_dequeues); | |
390 | if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) | |
391 | printf("WARNING! Discrepancy between nr succ. enqueues %llu vs " | |
392 | "succ. dequeues + end dequeues %llu.\n", | |
393 | tot_successful_enqueues, | |
394 | tot_successful_dequeues + end_dequeues); | |
395 | ||
396 | free(count_enqueuer); | |
397 | free(count_dequeuer); | |
398 | free(tid_enqueuer); | |
399 | free(tid_dequeuer); | |
400 | return 0; | |
401 | } |