Commit | Line | Data |
---|---|---|
b257a10b MD |
1 | /* |
2 | * test_urcu.c | |
3 | * | |
4 | * Userspace RCU library - test program | |
5 | * | |
6982d6d7 | 6 | * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
b257a10b | 7 | * |
af02d47e MD |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
b257a10b MD |
21 | */ |
22 | ||
9e97e478 | 23 | #define _GNU_SOURCE |
d8540fc5 | 24 | #include "../config.h" |
ac260fd9 | 25 | #include <stdio.h> |
f69f195a MD |
26 | #include <pthread.h> |
27 | #include <stdlib.h> | |
41718ff9 | 28 | #include <string.h> |
f69f195a MD |
29 | #include <sys/types.h> |
30 | #include <sys/wait.h> | |
31 | #include <unistd.h> | |
32 | #include <stdio.h> | |
41718ff9 | 33 | #include <assert.h> |
9e97e478 | 34 | #include <sched.h> |
94b343fd | 35 | #include <errno.h> |
87bd15cd | 36 | |
ec4e58a3 | 37 | #include <urcu/arch.h> |
bd252a04 | 38 | #include <urcu/tls-compat.h> |
7685d963 | 39 | |
65fcc7e9 MD |
40 | #ifdef __linux__ |
41 | #include <syscall.h> | |
42 | #endif | |
43 | ||
2a7ac59d MD |
44 | /* hardcoded number of CPUs */ |
45 | #define NR_CPUS 16384 | |
46 | ||
87bd15cd BW |
47 | #if defined(_syscall0) |
48 | _syscall0(pid_t, gettid) | |
49 | #elif defined(__NR_gettid) | |
50 | static inline pid_t gettid(void) | |
51 | { | |
52 | return syscall(__NR_gettid); | |
53 | } | |
54 | #else | |
55 | #warning "use pid as tid" | |
56 | static inline pid_t gettid(void) | |
57 | { | |
58 | return getpid(); | |
59 | } | |
60 | #endif | |
61 | ||
121a5d44 MD |
62 | #ifndef DYNAMIC_LINK_TEST |
63 | #define _LGPL_SOURCE | |
64 | #else | |
1de4df4b | 65 | #define rcu_debug_yield_read() |
121a5d44 | 66 | #endif |
ec4e58a3 | 67 | #include <urcu.h> |
ac260fd9 | 68 | |
41718ff9 MD |
69 | struct test_array { |
70 | int a; | |
41718ff9 MD |
71 | }; |
72 | ||
78efb485 | 73 | static volatile int test_go, test_stop; |
8c86b9a1 | 74 | |
9e31d0f0 | 75 | static unsigned long wdelay; |
bb488185 | 76 | |
41718ff9 MD |
77 | static struct test_array *test_rcu_pointer; |
78 | ||
cf380c2f | 79 | static unsigned long duration; |
cf380c2f | 80 | |
daddf5b0 | 81 | /* read-side C.S. duration, in loops */ |
8b632bab MD |
82 | static unsigned long rduration; |
83 | ||
31b598e0 MD |
84 | /* write-side C.S. duration, in loops */ |
85 | static unsigned long wduration; | |
86 | ||
ab0aacbe | 87 | static inline void loop_sleep(unsigned long loops) |
daddf5b0 | 88 | { |
ab0aacbe | 89 | while (loops-- != 0) |
06f22bdb | 90 | caa_cpu_relax(); |
daddf5b0 MD |
91 | } |
92 | ||
4bad7d45 MD |
93 | static int verbose_mode; |
94 | ||
95 | #define printf_verbose(fmt, args...) \ | |
96 | do { \ | |
97 | if (verbose_mode) \ | |
98 | printf(fmt, args); \ | |
99 | } while (0) | |
100 | ||
2a7ac59d MD |
101 | static unsigned int cpu_affinities[NR_CPUS]; |
102 | static unsigned int next_aff = 0; | |
103 | static int use_affinity = 0; | |
104 | ||
105 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
106 | ||
d8540fc5 PA |
107 | #ifndef HAVE_CPU_SET_T |
108 | typedef unsigned long cpu_set_t; | |
109 | # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) | |
110 | # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) | |
111 | #endif | |
112 | ||
2a7ac59d MD |
113 | static void set_affinity(void) |
114 | { | |
95bc7fb9 | 115 | #if HAVE_SCHED_SETAFFINITY |
2a7ac59d | 116 | cpu_set_t mask; |
95bc7fb9 MD |
117 | int cpu, ret; |
118 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
2a7ac59d MD |
119 | |
120 | if (!use_affinity) | |
121 | return; | |
122 | ||
d8540fc5 | 123 | #if HAVE_SCHED_SETAFFINITY |
2a7ac59d MD |
124 | ret = pthread_mutex_lock(&affinity_mutex); |
125 | if (ret) { | |
126 | perror("Error in pthread mutex lock"); | |
127 | exit(-1); | |
128 | } | |
129 | cpu = cpu_affinities[next_aff++]; | |
130 | ret = pthread_mutex_unlock(&affinity_mutex); | |
131 | if (ret) { | |
132 | perror("Error in pthread mutex unlock"); | |
133 | exit(-1); | |
134 | } | |
d8540fc5 | 135 | |
2a7ac59d MD |
136 | CPU_ZERO(&mask); |
137 | CPU_SET(cpu, &mask); | |
d8540fc5 PA |
138 | #if SCHED_SETAFFINITY_ARGS == 2 |
139 | sched_setaffinity(0, &mask); | |
140 | #else | |
2a7ac59d | 141 | sched_setaffinity(0, sizeof(mask), &mask); |
d8540fc5 PA |
142 | #endif |
143 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
2a7ac59d MD |
144 | } |
145 | ||
cf380c2f MD |
146 | /* |
147 | * returns 0 if test should end. | |
148 | */ | |
5873cd17 | 149 | static int test_duration_write(void) |
cf380c2f | 150 | { |
78efb485 | 151 | return !test_stop; |
5873cd17 MD |
152 | } |
153 | ||
154 | static int test_duration_read(void) | |
155 | { | |
78efb485 | 156 | return !test_stop; |
cf380c2f MD |
157 | } |
158 | ||
bd252a04 MD |
159 | static DEFINE_URCU_TLS(unsigned long long, nr_writes); |
160 | static DEFINE_URCU_TLS(unsigned long long, nr_reads); | |
1eec319e | 161 | |
8c86b9a1 MD |
162 | static unsigned int nr_readers; |
163 | static unsigned int nr_writers; | |
164 | ||
c265818b MD |
165 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
166 | ||
167 | void rcu_copy_mutex_lock(void) | |
168 | { | |
169 | int ret; | |
170 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
171 | if (ret) { | |
172 | perror("Error in pthread mutex lock"); | |
173 | exit(-1); | |
174 | } | |
175 | } | |
176 | ||
177 | void rcu_copy_mutex_unlock(void) | |
178 | { | |
179 | int ret; | |
180 | ||
181 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
182 | if (ret) { | |
183 | perror("Error in pthread mutex unlock"); | |
184 | exit(-1); | |
185 | } | |
186 | } | |
f69f195a | 187 | |
bb488185 MD |
188 | /* |
189 | * malloc/free are reusing memory areas too quickly, which does not let us | |
190 | * test races appropriately. Use a large circular array for allocations. | |
d4267b0b MD |
191 | * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across |
192 | * both alloc and free, which insures we never run over our tail. | |
bb488185 | 193 | */ |
8c86b9a1 | 194 | #define ARRAY_SIZE (1048576 * nr_writers) |
bb488185 MD |
195 | #define ARRAY_POISON 0xDEADBEEF |
196 | static int array_index; | |
8c86b9a1 | 197 | static struct test_array *test_array; |
bb488185 MD |
198 | |
199 | static struct test_array *test_array_alloc(void) | |
200 | { | |
201 | struct test_array *ret; | |
202 | int index; | |
203 | ||
bb488185 MD |
204 | index = array_index % ARRAY_SIZE; |
205 | assert(test_array[index].a == ARRAY_POISON || | |
206 | test_array[index].a == 0); | |
207 | ret = &test_array[index]; | |
208 | array_index++; | |
209 | if (array_index == ARRAY_SIZE) | |
210 | array_index = 0; | |
bb488185 MD |
211 | return ret; |
212 | } | |
213 | ||
214 | static void test_array_free(struct test_array *ptr) | |
215 | { | |
216 | if (!ptr) | |
217 | return; | |
bb488185 | 218 | ptr->a = ARRAY_POISON; |
bb488185 MD |
219 | } |
220 | ||
1eec319e | 221 | void *thr_reader(void *_count) |
f69f195a | 222 | { |
1eec319e | 223 | unsigned long long *count = _count; |
41718ff9 MD |
224 | struct test_array *local_ptr; |
225 | ||
4bad7d45 | 226 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
6ff854ac MD |
227 | "reader", (unsigned long) pthread_self(), |
228 | (unsigned long) gettid()); | |
f69f195a | 229 | |
2a7ac59d MD |
230 | set_affinity(); |
231 | ||
121a5d44 | 232 | rcu_register_thread(); |
f69f195a | 233 | |
8c86b9a1 MD |
234 | while (!test_go) |
235 | { | |
236 | } | |
5481ddb3 | 237 | cmm_smp_mb(); |
8c86b9a1 | 238 | |
cf380c2f | 239 | for (;;) { |
1430ee0b | 240 | rcu_read_lock(); |
cf380c2f | 241 | local_ptr = rcu_dereference(test_rcu_pointer); |
1de4df4b | 242 | rcu_debug_yield_read(); |
cf380c2f MD |
243 | if (local_ptr) |
244 | assert(local_ptr->a == 8); | |
a0b7f7ea | 245 | if (caa_unlikely(rduration)) |
daddf5b0 | 246 | loop_sleep(rduration); |
1430ee0b | 247 | rcu_read_unlock(); |
bd252a04 | 248 | URCU_TLS(nr_reads)++; |
a0b7f7ea | 249 | if (caa_unlikely(!test_duration_read())) |
cf380c2f | 250 | break; |
41718ff9 | 251 | } |
f69f195a | 252 | |
121a5d44 | 253 | rcu_unregister_thread(); |
41718ff9 | 254 | |
2b514ae2 MD |
255 | /* test extra thread registration */ |
256 | rcu_register_thread(); | |
257 | rcu_unregister_thread(); | |
258 | ||
bd252a04 | 259 | *count = URCU_TLS(nr_reads); |
4bad7d45 | 260 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
6ff854ac MD |
261 | "reader", (unsigned long) pthread_self(), |
262 | (unsigned long) gettid()); | |
f69f195a MD |
263 | return ((void*)1); |
264 | ||
265 | } | |
266 | ||
1eec319e | 267 | void *thr_writer(void *_count) |
f69f195a | 268 | { |
1eec319e | 269 | unsigned long long *count = _count; |
41718ff9 | 270 | struct test_array *new, *old; |
f69f195a | 271 | |
4bad7d45 | 272 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
6ff854ac MD |
273 | "writer", (unsigned long) pthread_self(), |
274 | (unsigned long) gettid()); | |
f69f195a | 275 | |
2a7ac59d MD |
276 | set_affinity(); |
277 | ||
8c86b9a1 MD |
278 | while (!test_go) |
279 | { | |
280 | } | |
5481ddb3 | 281 | cmm_smp_mb(); |
8c86b9a1 | 282 | |
cf380c2f | 283 | for (;;) { |
d4267b0b | 284 | rcu_copy_mutex_lock(); |
bb488185 | 285 | new = test_array_alloc(); |
ad6ce6ae | 286 | new->a = 8; |
d2835e6f | 287 | old = rcu_xchg_pointer(&test_rcu_pointer, new); |
a0b7f7ea | 288 | if (caa_unlikely(wduration)) |
31b598e0 | 289 | loop_sleep(wduration); |
d2835e6f | 290 | synchronize_rcu(); |
cf380c2f | 291 | if (old) |
8c8eed97 | 292 | old->a = 0; |
bb488185 | 293 | test_array_free(old); |
d4267b0b | 294 | rcu_copy_mutex_unlock(); |
bd252a04 | 295 | URCU_TLS(nr_writes)++; |
a0b7f7ea | 296 | if (caa_unlikely(!test_duration_write())) |
cf380c2f | 297 | break; |
a0b7f7ea | 298 | if (caa_unlikely(wdelay)) |
9e31d0f0 | 299 | loop_sleep(wdelay); |
f69f195a MD |
300 | } |
301 | ||
4bad7d45 | 302 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
6ff854ac MD |
303 | "writer", (unsigned long) pthread_self(), |
304 | (unsigned long) gettid()); | |
bd252a04 | 305 | *count = URCU_TLS(nr_writes); |
f69f195a MD |
306 | return ((void*)2); |
307 | } | |
ac260fd9 | 308 | |
1430ee0b MD |
309 | void show_usage(int argc, char **argv) |
310 | { | |
8c86b9a1 | 311 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); |
1430ee0b MD |
312 | #ifdef DEBUG_YIELD |
313 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
314 | #endif | |
7685d963 | 315 | printf(" [-d delay] (writer period (us))"); |
daddf5b0 | 316 | printf(" [-c duration] (reader C.S. duration (in loops))"); |
31b598e0 | 317 | printf(" [-e duration] (writer C.S. duration (in loops))"); |
4bad7d45 | 318 | printf(" [-v] (verbose output)"); |
9e97e478 | 319 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); |
1430ee0b MD |
320 | printf("\n"); |
321 | } | |
322 | ||
cf380c2f | 323 | int main(int argc, char **argv) |
ac260fd9 | 324 | { |
f69f195a | 325 | int err; |
8c86b9a1 | 326 | pthread_t *tid_reader, *tid_writer; |
f69f195a | 327 | void *tret; |
8c86b9a1 | 328 | unsigned long long *count_reader, *count_writer; |
1eec319e | 329 | unsigned long long tot_reads = 0, tot_writes = 0; |
9e97e478 | 330 | int i, a; |
f69f195a | 331 | |
8c86b9a1 MD |
332 | if (argc < 4) { |
333 | show_usage(argc, argv); | |
334 | return -1; | |
335 | } | |
336 | ||
337 | err = sscanf(argv[1], "%u", &nr_readers); | |
338 | if (err != 1) { | |
1430ee0b | 339 | show_usage(argc, argv); |
cf380c2f MD |
340 | return -1; |
341 | } | |
342 | ||
8c86b9a1 MD |
343 | err = sscanf(argv[2], "%u", &nr_writers); |
344 | if (err != 1) { | |
345 | show_usage(argc, argv); | |
346 | return -1; | |
347 | } | |
348 | ||
349 | err = sscanf(argv[3], "%lu", &duration); | |
cf380c2f | 350 | if (err != 1) { |
1430ee0b | 351 | show_usage(argc, argv); |
cf380c2f MD |
352 | return -1; |
353 | } | |
354 | ||
8c86b9a1 | 355 | for (i = 4; i < argc; i++) { |
cf380c2f MD |
356 | if (argv[i][0] != '-') |
357 | continue; | |
358 | switch (argv[i][1]) { | |
bb488185 | 359 | #ifdef DEBUG_YIELD |
cf380c2f | 360 | case 'r': |
1de4df4b | 361 | rcu_yield_active |= RCU_YIELD_READ; |
cf380c2f MD |
362 | break; |
363 | case 'w': | |
1de4df4b | 364 | rcu_yield_active |= RCU_YIELD_WRITE; |
cf380c2f | 365 | break; |
bb488185 | 366 | #endif |
9e97e478 MD |
367 | case 'a': |
368 | if (argc < i + 2) { | |
369 | show_usage(argc, argv); | |
370 | return -1; | |
371 | } | |
372 | a = atoi(argv[++i]); | |
2a7ac59d | 373 | cpu_affinities[next_aff++] = a; |
9e97e478 | 374 | use_affinity = 1; |
4bad7d45 | 375 | printf_verbose("Adding CPU %d affinity\n", a); |
9e97e478 | 376 | break; |
8b632bab MD |
377 | case 'c': |
378 | if (argc < i + 2) { | |
379 | show_usage(argc, argv); | |
380 | return -1; | |
381 | } | |
9e31d0f0 | 382 | rduration = atol(argv[++i]); |
8b632bab | 383 | break; |
8c86b9a1 | 384 | case 'd': |
7685d963 | 385 | if (argc < i + 2) { |
8c86b9a1 MD |
386 | show_usage(argc, argv); |
387 | return -1; | |
388 | } | |
9e31d0f0 | 389 | wdelay = atol(argv[++i]); |
bb488185 | 390 | break; |
31b598e0 MD |
391 | case 'e': |
392 | if (argc < i + 2) { | |
393 | show_usage(argc, argv); | |
394 | return -1; | |
395 | } | |
396 | wduration = atol(argv[++i]); | |
397 | break; | |
4bad7d45 MD |
398 | case 'v': |
399 | verbose_mode = 1; | |
400 | break; | |
cf380c2f MD |
401 | } |
402 | } | |
cf380c2f | 403 | |
4bad7d45 | 404 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", |
8c86b9a1 | 405 | duration, nr_readers, nr_writers); |
9e31d0f0 | 406 | printf_verbose("Writer delay : %lu loops.\n", wdelay); |
4bad7d45 MD |
407 | printf_verbose("Reader duration : %lu loops.\n", rduration); |
408 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", | |
6ff854ac MD |
409 | "main", (unsigned long) pthread_self(), |
410 | (unsigned long)gettid()); | |
87bd15cd | 411 | |
92e8d9d6 | 412 | test_array = calloc(1, sizeof(*test_array) * ARRAY_SIZE); |
8c86b9a1 MD |
413 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); |
414 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
415 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
416 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
417 | ||
2a7ac59d MD |
418 | next_aff = 0; |
419 | ||
8c86b9a1 | 420 | for (i = 0; i < nr_readers; i++) { |
1eec319e MD |
421 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
422 | &count_reader[i]); | |
f69f195a MD |
423 | if (err != 0) |
424 | exit(1); | |
425 | } | |
8c86b9a1 | 426 | for (i = 0; i < nr_writers; i++) { |
1eec319e MD |
427 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
428 | &count_writer[i]); | |
f69f195a MD |
429 | if (err != 0) |
430 | exit(1); | |
431 | } | |
432 | ||
5481ddb3 | 433 | cmm_smp_mb(); |
8c86b9a1 | 434 | |
78efb485 MD |
435 | test_go = 1; |
436 | ||
437 | sleep(duration); | |
438 | ||
439 | test_stop = 1; | |
440 | ||
8c86b9a1 | 441 | for (i = 0; i < nr_readers; i++) { |
f69f195a MD |
442 | err = pthread_join(tid_reader[i], &tret); |
443 | if (err != 0) | |
444 | exit(1); | |
1eec319e | 445 | tot_reads += count_reader[i]; |
f69f195a | 446 | } |
8c86b9a1 | 447 | for (i = 0; i < nr_writers; i++) { |
f69f195a MD |
448 | err = pthread_join(tid_writer[i], &tret); |
449 | if (err != 0) | |
450 | exit(1); | |
1eec319e | 451 | tot_writes += count_writer[i]; |
f69f195a | 452 | } |
5873cd17 | 453 | |
4bad7d45 | 454 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, |
1eec319e | 455 | tot_writes); |
a50a7b43 | 456 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu " |
cda3c71d | 457 | "nr_writers %3u " |
9e31d0f0 | 458 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", |
a50a7b43 | 459 | argv[0], duration, nr_readers, rduration, wduration, |
4bad7d45 MD |
460 | nr_writers, wdelay, tot_reads, tot_writes, |
461 | tot_reads + tot_writes); | |
bb488185 | 462 | test_array_free(test_rcu_pointer); |
8c86b9a1 MD |
463 | free(test_array); |
464 | free(tid_reader); | |
465 | free(tid_writer); | |
466 | free(count_reader); | |
467 | free(count_writer); | |
f69f195a | 468 | return 0; |
ac260fd9 | 469 | } |