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