Commit | Line | Data |
---|---|---|
453629a9 MD |
1 | /* |
2 | * test_urcu_lfq.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 | |
25 | #include "../config.h" | |
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> | |
453629a9 MD |
37 | #include <sched.h> |
38 | #include <errno.h> | |
39 | ||
40 | #include <urcu/arch.h> | |
41 | ||
65fcc7e9 MD |
42 | #ifdef __linux__ |
43 | #include <syscall.h> | |
44 | #endif | |
45 | ||
453629a9 MD |
46 | /* hardcoded number of CPUs */ |
47 | #define NR_CPUS 16384 | |
48 | ||
49 | #if defined(_syscall0) | |
50 | _syscall0(pid_t, gettid) | |
51 | #elif defined(__NR_gettid) | |
52 | static inline pid_t gettid(void) | |
53 | { | |
54 | return syscall(__NR_gettid); | |
55 | } | |
56 | #else | |
57 | #warning "use pid as tid" | |
58 | static inline pid_t gettid(void) | |
59 | { | |
60 | return getpid(); | |
61 | } | |
62 | #endif | |
63 | ||
64 | #ifndef DYNAMIC_LINK_TEST | |
65 | #define _LGPL_SOURCE | |
66 | #endif | |
67 | #include <urcu.h> | |
e5a7d709 | 68 | #include <urcu/cds.h> |
453629a9 MD |
69 | |
70 | static volatile int test_go, test_stop; | |
71 | ||
72 | static unsigned long rduration; | |
73 | ||
74 | static unsigned long duration; | |
75 | ||
76 | /* read-side C.S. duration, in loops */ | |
77 | static unsigned long wdelay; | |
78 | ||
79 | static inline void loop_sleep(unsigned long l) | |
80 | { | |
81 | while(l-- != 0) | |
06f22bdb | 82 | caa_cpu_relax(); |
453629a9 MD |
83 | } |
84 | ||
85 | static int verbose_mode; | |
86 | ||
87 | #define printf_verbose(fmt, args...) \ | |
88 | do { \ | |
89 | if (verbose_mode) \ | |
90 | printf(fmt, args); \ | |
91 | } while (0) | |
92 | ||
93 | static unsigned int cpu_affinities[NR_CPUS]; | |
94 | static unsigned int next_aff = 0; | |
95 | static int use_affinity = 0; | |
96 | ||
97 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
98 | ||
99 | #ifndef HAVE_CPU_SET_T | |
100 | typedef unsigned long cpu_set_t; | |
101 | # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) | |
102 | # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) | |
103 | #endif | |
104 | ||
105 | static void set_affinity(void) | |
106 | { | |
107 | cpu_set_t mask; | |
108 | int cpu; | |
109 | int ret; | |
110 | ||
111 | if (!use_affinity) | |
112 | return; | |
113 | ||
114 | #if HAVE_SCHED_SETAFFINITY | |
115 | ret = pthread_mutex_lock(&affinity_mutex); | |
116 | if (ret) { | |
117 | perror("Error in pthread mutex lock"); | |
118 | exit(-1); | |
119 | } | |
120 | cpu = cpu_affinities[next_aff++]; | |
121 | ret = pthread_mutex_unlock(&affinity_mutex); | |
122 | if (ret) { | |
123 | perror("Error in pthread mutex unlock"); | |
124 | exit(-1); | |
125 | } | |
126 | ||
127 | CPU_ZERO(&mask); | |
128 | CPU_SET(cpu, &mask); | |
129 | #if SCHED_SETAFFINITY_ARGS == 2 | |
130 | sched_setaffinity(0, &mask); | |
131 | #else | |
132 | sched_setaffinity(0, sizeof(mask), &mask); | |
133 | #endif | |
134 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
135 | } | |
136 | ||
137 | /* | |
138 | * returns 0 if test should end. | |
139 | */ | |
140 | static int test_duration_dequeue(void) | |
141 | { | |
142 | return !test_stop; | |
143 | } | |
144 | ||
145 | static int test_duration_enqueue(void) | |
146 | { | |
147 | return !test_stop; | |
148 | } | |
149 | ||
150 | static unsigned long long __thread nr_dequeues; | |
151 | static unsigned long long __thread nr_enqueues; | |
152 | ||
153 | static unsigned long long __thread nr_successful_dequeues; | |
154 | static unsigned long long __thread nr_successful_enqueues; | |
155 | ||
156 | static unsigned int nr_enqueuers; | |
157 | static unsigned int nr_dequeuers; | |
158 | ||
16aa9ee8 | 159 | static struct cds_lfq_queue_rcu q; |
453629a9 MD |
160 | |
161 | void *thr_enqueuer(void *_count) | |
162 | { | |
163 | unsigned long long *count = _count; | |
164 | ||
165 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
166 | "enqueuer", pthread_self(), (unsigned long)gettid()); | |
167 | ||
168 | set_affinity(); | |
169 | ||
170 | rcu_register_thread(); | |
171 | ||
172 | while (!test_go) | |
173 | { | |
174 | } | |
5481ddb3 | 175 | cmm_smp_mb(); |
453629a9 MD |
176 | |
177 | for (;;) { | |
16aa9ee8 | 178 | struct cds_lfq_node_rcu *node = malloc(sizeof(*node)); |
453629a9 MD |
179 | if (!node) |
180 | goto fail; | |
16aa9ee8 | 181 | cds_lfq_node_init_rcu(node); |
d9b52143 | 182 | rcu_read_lock(); |
16aa9ee8 | 183 | cds_lfq_enqueue_rcu(&q, node); |
d9b52143 | 184 | rcu_read_unlock(); |
453629a9 MD |
185 | nr_successful_enqueues++; |
186 | ||
187 | if (unlikely(wdelay)) | |
188 | loop_sleep(wdelay); | |
189 | fail: | |
190 | nr_enqueues++; | |
191 | if (unlikely(!test_duration_enqueue())) | |
192 | break; | |
193 | } | |
194 | ||
195 | rcu_unregister_thread(); | |
196 | ||
197 | count[0] = nr_enqueues; | |
198 | count[1] = nr_successful_enqueues; | |
199 | printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, " | |
200 | "enqueues %llu successful_enqueues %llu\n", | |
201 | pthread_self(), (unsigned long)gettid(), nr_enqueues, | |
202 | nr_successful_enqueues); | |
203 | return ((void*)1); | |
204 | ||
205 | } | |
206 | ||
d9b52143 | 207 | static void rcu_free_node(struct rcu_head *head) |
453629a9 | 208 | { |
d9b52143 MD |
209 | struct cds_lfq_node_rcu *node = |
210 | caa_container_of(head, struct cds_lfq_node_rcu, rcu_head); | |
211 | free(node); | |
212 | } | |
213 | ||
214 | static void ref_release_node(struct urcu_ref *ref) | |
215 | { | |
216 | struct cds_lfq_node_rcu *node = | |
217 | caa_container_of(ref, struct cds_lfq_node_rcu, ref); | |
218 | call_rcu(&node->rcu_head, rcu_free_node); | |
453629a9 MD |
219 | } |
220 | ||
221 | void *thr_dequeuer(void *_count) | |
222 | { | |
223 | unsigned long long *count = _count; | |
9bfaf0ca | 224 | int ret; |
453629a9 MD |
225 | |
226 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
227 | "dequeuer", pthread_self(), (unsigned long)gettid()); | |
228 | ||
229 | set_affinity(); | |
230 | ||
453629a9 MD |
231 | rcu_register_thread(); |
232 | ||
233 | while (!test_go) | |
234 | { | |
235 | } | |
5481ddb3 | 236 | cmm_smp_mb(); |
453629a9 MD |
237 | |
238 | for (;;) { | |
d9b52143 MD |
239 | struct cds_lfq_node_rcu *node; |
240 | ||
241 | rcu_read_lock(); | |
242 | node = cds_lfq_dequeue_rcu(&q); | |
243 | rcu_read_unlock(); | |
453629a9 MD |
244 | |
245 | if (node) { | |
d9b52143 | 246 | urcu_ref_put(&node->ref, ref_release_node); |
453629a9 MD |
247 | nr_successful_dequeues++; |
248 | } | |
249 | ||
250 | nr_dequeues++; | |
251 | if (unlikely(!test_duration_dequeue())) | |
252 | break; | |
253 | if (unlikely(rduration)) | |
254 | loop_sleep(rduration); | |
255 | } | |
256 | ||
257 | rcu_unregister_thread(); | |
453629a9 MD |
258 | printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, " |
259 | "dequeues %llu, successful_dequeues %llu\n", | |
260 | pthread_self(), (unsigned long)gettid(), nr_dequeues, | |
261 | nr_successful_dequeues); | |
262 | count[0] = nr_dequeues; | |
263 | count[1] = nr_successful_dequeues; | |
264 | return ((void*)2); | |
265 | } | |
266 | ||
267 | static void release_node(struct urcu_ref *ref) | |
268 | { | |
16aa9ee8 | 269 | struct cds_lfq_node_rcu *node = caa_container_of(ref, struct cds_lfq_node_rcu, ref); |
453629a9 MD |
270 | free(node); |
271 | } | |
272 | ||
16aa9ee8 | 273 | void test_end(struct cds_lfq_queue_rcu *q, unsigned long long *nr_dequeues) |
453629a9 | 274 | { |
16aa9ee8 | 275 | struct cds_lfq_node_rcu *node; |
453629a9 MD |
276 | |
277 | do { | |
d9b52143 MD |
278 | rcu_read_lock(); |
279 | node = cds_lfq_dequeue_rcu(q); | |
280 | rcu_read_unlock(); | |
453629a9 MD |
281 | if (node) { |
282 | urcu_ref_put(&node->ref, release_node); | |
283 | (*nr_dequeues)++; | |
284 | } | |
285 | } while (node); | |
286 | } | |
287 | ||
288 | void show_usage(int argc, char **argv) | |
289 | { | |
290 | printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]); | |
291 | printf(" [-d delay] (enqueuer period (in loops))"); | |
292 | printf(" [-c duration] (dequeuer period (in loops))"); | |
293 | printf(" [-v] (verbose output)"); | |
294 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); | |
295 | printf("\n"); | |
296 | } | |
297 | ||
298 | int main(int argc, char **argv) | |
299 | { | |
300 | int err; | |
301 | pthread_t *tid_enqueuer, *tid_dequeuer; | |
302 | void *tret; | |
303 | unsigned long long *count_enqueuer, *count_dequeuer; | |
304 | unsigned long long tot_enqueues = 0, tot_dequeues = 0; | |
305 | unsigned long long tot_successful_enqueues = 0, | |
306 | tot_successful_dequeues = 0; | |
307 | unsigned long long end_dequeues = 0; | |
308 | int i, a; | |
309 | ||
310 | if (argc < 4) { | |
311 | show_usage(argc, argv); | |
312 | return -1; | |
313 | } | |
314 | ||
315 | err = sscanf(argv[1], "%u", &nr_dequeuers); | |
316 | if (err != 1) { | |
317 | show_usage(argc, argv); | |
318 | return -1; | |
319 | } | |
320 | ||
321 | err = sscanf(argv[2], "%u", &nr_enqueuers); | |
322 | if (err != 1) { | |
323 | show_usage(argc, argv); | |
324 | return -1; | |
325 | } | |
326 | ||
327 | err = sscanf(argv[3], "%lu", &duration); | |
328 | if (err != 1) { | |
329 | show_usage(argc, argv); | |
330 | return -1; | |
331 | } | |
332 | ||
333 | for (i = 4; i < argc; i++) { | |
334 | if (argv[i][0] != '-') | |
335 | continue; | |
336 | switch (argv[i][1]) { | |
337 | case 'a': | |
338 | if (argc < i + 2) { | |
339 | show_usage(argc, argv); | |
340 | return -1; | |
341 | } | |
342 | a = atoi(argv[++i]); | |
343 | cpu_affinities[next_aff++] = a; | |
344 | use_affinity = 1; | |
345 | printf_verbose("Adding CPU %d affinity\n", a); | |
346 | break; | |
347 | case 'c': | |
348 | if (argc < i + 2) { | |
349 | show_usage(argc, argv); | |
350 | return -1; | |
351 | } | |
352 | rduration = atol(argv[++i]); | |
353 | break; | |
354 | case 'd': | |
355 | if (argc < i + 2) { | |
356 | show_usage(argc, argv); | |
357 | return -1; | |
358 | } | |
359 | wdelay = atol(argv[++i]); | |
360 | break; | |
361 | case 'v': | |
362 | verbose_mode = 1; | |
363 | break; | |
364 | } | |
365 | } | |
366 | ||
367 | printf_verbose("running test for %lu seconds, %u enqueuers, " | |
368 | "%u dequeuers.\n", | |
369 | duration, nr_enqueuers, nr_dequeuers); | |
370 | printf_verbose("Writer delay : %lu loops.\n", rduration); | |
371 | printf_verbose("Reader duration : %lu loops.\n", wdelay); | |
372 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", | |
373 | "main", pthread_self(), (unsigned long)gettid()); | |
374 | ||
375 | tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers); | |
376 | tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers); | |
377 | count_enqueuer = malloc(2 * sizeof(*count_enqueuer) * nr_enqueuers); | |
378 | count_dequeuer = malloc(2 * sizeof(*count_dequeuer) * nr_dequeuers); | |
d9b52143 | 379 | cds_lfq_init_rcu(&q, ref_release_node); |
453629a9 MD |
380 | |
381 | next_aff = 0; | |
382 | ||
383 | for (i = 0; i < nr_enqueuers; i++) { | |
384 | err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer, | |
385 | &count_enqueuer[2 * i]); | |
386 | if (err != 0) | |
387 | exit(1); | |
388 | } | |
389 | for (i = 0; i < nr_dequeuers; i++) { | |
390 | err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer, | |
391 | &count_dequeuer[2 * i]); | |
392 | if (err != 0) | |
393 | exit(1); | |
394 | } | |
395 | ||
5481ddb3 | 396 | cmm_smp_mb(); |
453629a9 MD |
397 | |
398 | test_go = 1; | |
399 | ||
400 | for (i = 0; i < duration; i++) { | |
401 | sleep(1); | |
402 | if (verbose_mode) | |
403 | write (1, ".", 1); | |
404 | } | |
405 | ||
406 | test_stop = 1; | |
407 | ||
408 | for (i = 0; i < nr_enqueuers; i++) { | |
409 | err = pthread_join(tid_enqueuer[i], &tret); | |
410 | if (err != 0) | |
411 | exit(1); | |
412 | tot_enqueues += count_enqueuer[2 * i]; | |
413 | tot_successful_enqueues += count_enqueuer[2 * i + 1]; | |
414 | } | |
415 | for (i = 0; i < nr_dequeuers; i++) { | |
416 | err = pthread_join(tid_dequeuer[i], &tret); | |
417 | if (err != 0) | |
418 | exit(1); | |
419 | tot_dequeues += count_dequeuer[2 * i]; | |
420 | tot_successful_dequeues += count_dequeuer[2 * i + 1]; | |
421 | } | |
422 | ||
423 | test_end(&q, &end_dequeues); | |
424 | ||
425 | printf_verbose("total number of enqueues : %llu, dequeues %llu\n", | |
426 | tot_enqueues, tot_dequeues); | |
427 | printf_verbose("total number of successful enqueues : %llu, " | |
428 | "successful dequeues %llu\n", | |
429 | tot_successful_enqueues, tot_successful_dequeues); | |
430 | printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu " | |
431 | "nr_dequeuers %3u " | |
432 | "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu " | |
433 | "successful enqueues %12llu successful dequeues %12llu " | |
434 | "end_dequeues %llu nr_ops %12llu\n", | |
435 | argv[0], duration, nr_enqueuers, wdelay, | |
436 | nr_dequeuers, rduration, tot_enqueues, tot_dequeues, | |
437 | tot_successful_enqueues, | |
438 | tot_successful_dequeues, end_dequeues, | |
439 | tot_enqueues + tot_dequeues); | |
440 | if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) | |
441 | printf("WARNING! Discrepancy between nr succ. enqueues %llu vs " | |
442 | "succ. dequeues + end dequeues %llu.\n", | |
443 | tot_successful_enqueues, | |
444 | tot_successful_dequeues + end_dequeues); | |
445 | ||
446 | free(count_enqueuer); | |
447 | free(count_dequeuer); | |
448 | free(tid_enqueuer); | |
449 | free(tid_dequeuer); | |
450 | return 0; | |
451 | } |