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> |
94b343fd | 34 | #include <errno.h> |
87bd15cd | 35 | |
ec4e58a3 | 36 | #include <urcu/arch.h> |
bd252a04 | 37 | #include <urcu/tls-compat.h> |
95d8822d | 38 | #include "cpuset.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 | |
65 | #define debug_yield_read() | |
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 | ||
daddf5b0 MD |
87 | static inline void loop_sleep(unsigned long l) |
88 | { | |
89 | while(l-- != 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 | ||
107 | static void set_affinity(void) | |
108 | { | |
109 | cpu_set_t mask; | |
110 | int cpu; | |
111 | int ret; | |
112 | ||
113 | if (!use_affinity) | |
114 | return; | |
115 | ||
d8540fc5 | 116 | #if HAVE_SCHED_SETAFFINITY |
2a7ac59d MD |
117 | ret = pthread_mutex_lock(&affinity_mutex); |
118 | if (ret) { | |
119 | perror("Error in pthread mutex lock"); | |
120 | exit(-1); | |
121 | } | |
122 | cpu = cpu_affinities[next_aff++]; | |
123 | ret = pthread_mutex_unlock(&affinity_mutex); | |
124 | if (ret) { | |
125 | perror("Error in pthread mutex unlock"); | |
126 | exit(-1); | |
127 | } | |
d8540fc5 | 128 | |
2a7ac59d MD |
129 | CPU_ZERO(&mask); |
130 | CPU_SET(cpu, &mask); | |
d8540fc5 PA |
131 | #if SCHED_SETAFFINITY_ARGS == 2 |
132 | sched_setaffinity(0, &mask); | |
133 | #else | |
2a7ac59d | 134 | sched_setaffinity(0, sizeof(mask), &mask); |
d8540fc5 PA |
135 | #endif |
136 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
2a7ac59d MD |
137 | } |
138 | ||
cf380c2f MD |
139 | /* |
140 | * returns 0 if test should end. | |
141 | */ | |
5873cd17 | 142 | static int test_duration_write(void) |
cf380c2f | 143 | { |
78efb485 | 144 | return !test_stop; |
5873cd17 MD |
145 | } |
146 | ||
147 | static int test_duration_read(void) | |
148 | { | |
78efb485 | 149 | return !test_stop; |
cf380c2f MD |
150 | } |
151 | ||
bd252a04 MD |
152 | static DEFINE_URCU_TLS(unsigned long long, nr_writes); |
153 | static DEFINE_URCU_TLS(unsigned long long, nr_reads); | |
1eec319e | 154 | |
8c86b9a1 MD |
155 | static unsigned int nr_readers; |
156 | static unsigned int nr_writers; | |
157 | ||
c265818b MD |
158 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
159 | ||
160 | void rcu_copy_mutex_lock(void) | |
161 | { | |
162 | int ret; | |
163 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
164 | if (ret) { | |
165 | perror("Error in pthread mutex lock"); | |
166 | exit(-1); | |
167 | } | |
168 | } | |
169 | ||
170 | void rcu_copy_mutex_unlock(void) | |
171 | { | |
172 | int ret; | |
173 | ||
174 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
175 | if (ret) { | |
176 | perror("Error in pthread mutex unlock"); | |
177 | exit(-1); | |
178 | } | |
179 | } | |
f69f195a | 180 | |
bb488185 MD |
181 | /* |
182 | * malloc/free are reusing memory areas too quickly, which does not let us | |
183 | * test races appropriately. Use a large circular array for allocations. | |
d4267b0b MD |
184 | * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across |
185 | * both alloc and free, which insures we never run over our tail. | |
bb488185 | 186 | */ |
8c86b9a1 | 187 | #define ARRAY_SIZE (1048576 * nr_writers) |
bb488185 MD |
188 | #define ARRAY_POISON 0xDEADBEEF |
189 | static int array_index; | |
8c86b9a1 | 190 | static struct test_array *test_array; |
bb488185 MD |
191 | |
192 | static struct test_array *test_array_alloc(void) | |
193 | { | |
194 | struct test_array *ret; | |
195 | int index; | |
196 | ||
bb488185 MD |
197 | index = array_index % ARRAY_SIZE; |
198 | assert(test_array[index].a == ARRAY_POISON || | |
199 | test_array[index].a == 0); | |
200 | ret = &test_array[index]; | |
201 | array_index++; | |
202 | if (array_index == ARRAY_SIZE) | |
203 | array_index = 0; | |
bb488185 MD |
204 | return ret; |
205 | } | |
206 | ||
207 | static void test_array_free(struct test_array *ptr) | |
208 | { | |
209 | if (!ptr) | |
210 | return; | |
bb488185 | 211 | ptr->a = ARRAY_POISON; |
bb488185 MD |
212 | } |
213 | ||
1eec319e | 214 | void *thr_reader(void *_count) |
f69f195a | 215 | { |
1eec319e | 216 | unsigned long long *count = _count; |
41718ff9 MD |
217 | struct test_array *local_ptr; |
218 | ||
4bad7d45 | 219 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
2d72fae2 MD |
220 | "reader", (unsigned long) pthread_self(), |
221 | (unsigned long) gettid()); | |
f69f195a | 222 | |
2a7ac59d MD |
223 | set_affinity(); |
224 | ||
121a5d44 | 225 | rcu_register_thread(); |
f69f195a | 226 | |
8c86b9a1 MD |
227 | while (!test_go) |
228 | { | |
229 | } | |
5481ddb3 | 230 | cmm_smp_mb(); |
8c86b9a1 | 231 | |
cf380c2f | 232 | for (;;) { |
1430ee0b | 233 | rcu_read_lock(); |
cf380c2f | 234 | local_ptr = rcu_dereference(test_rcu_pointer); |
bb488185 | 235 | debug_yield_read(); |
cf380c2f MD |
236 | if (local_ptr) |
237 | assert(local_ptr->a == 8); | |
a0b7f7ea | 238 | if (caa_unlikely(rduration)) |
daddf5b0 | 239 | loop_sleep(rduration); |
1430ee0b | 240 | rcu_read_unlock(); |
bd252a04 | 241 | URCU_TLS(nr_reads)++; |
a0b7f7ea | 242 | if (caa_unlikely(!test_duration_read())) |
cf380c2f | 243 | break; |
41718ff9 | 244 | } |
f69f195a | 245 | |
121a5d44 | 246 | rcu_unregister_thread(); |
41718ff9 | 247 | |
2b514ae2 MD |
248 | /* test extra thread registration */ |
249 | rcu_register_thread(); | |
250 | rcu_unregister_thread(); | |
251 | ||
bd252a04 | 252 | *count = URCU_TLS(nr_reads); |
4bad7d45 | 253 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
2d72fae2 MD |
254 | "reader", (unsigned long) pthread_self(), |
255 | (unsigned long) gettid()); | |
f69f195a MD |
256 | return ((void*)1); |
257 | ||
258 | } | |
259 | ||
1eec319e | 260 | void *thr_writer(void *_count) |
f69f195a | 261 | { |
1eec319e | 262 | unsigned long long *count = _count; |
41718ff9 | 263 | struct test_array *new, *old; |
f69f195a | 264 | |
4bad7d45 | 265 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
2d72fae2 MD |
266 | "writer", (unsigned long) pthread_self(), |
267 | (unsigned long) gettid()); | |
f69f195a | 268 | |
2a7ac59d MD |
269 | set_affinity(); |
270 | ||
8c86b9a1 MD |
271 | while (!test_go) |
272 | { | |
273 | } | |
5481ddb3 | 274 | cmm_smp_mb(); |
8c86b9a1 | 275 | |
cf380c2f | 276 | for (;;) { |
d4267b0b | 277 | rcu_copy_mutex_lock(); |
bb488185 | 278 | new = test_array_alloc(); |
ad6ce6ae | 279 | new->a = 8; |
d2835e6f | 280 | old = rcu_xchg_pointer(&test_rcu_pointer, new); |
a0b7f7ea | 281 | if (caa_unlikely(wduration)) |
31b598e0 | 282 | loop_sleep(wduration); |
d2835e6f | 283 | synchronize_rcu(); |
cf380c2f | 284 | if (old) |
8c8eed97 | 285 | old->a = 0; |
bb488185 | 286 | test_array_free(old); |
d4267b0b | 287 | rcu_copy_mutex_unlock(); |
bd252a04 | 288 | URCU_TLS(nr_writes)++; |
a0b7f7ea | 289 | if (caa_unlikely(!test_duration_write())) |
cf380c2f | 290 | break; |
a0b7f7ea | 291 | if (caa_unlikely(wdelay)) |
9e31d0f0 | 292 | loop_sleep(wdelay); |
f69f195a MD |
293 | } |
294 | ||
4bad7d45 | 295 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
2d72fae2 MD |
296 | "writer", (unsigned long) pthread_self(), |
297 | (unsigned long) gettid()); | |
bd252a04 | 298 | *count = URCU_TLS(nr_writes); |
f69f195a MD |
299 | return ((void*)2); |
300 | } | |
ac260fd9 | 301 | |
1430ee0b MD |
302 | void show_usage(int argc, char **argv) |
303 | { | |
8c86b9a1 | 304 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); |
1430ee0b MD |
305 | #ifdef DEBUG_YIELD |
306 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
307 | #endif | |
7685d963 | 308 | printf(" [-d delay] (writer period (us))"); |
daddf5b0 | 309 | printf(" [-c duration] (reader C.S. duration (in loops))"); |
31b598e0 | 310 | printf(" [-e duration] (writer C.S. duration (in loops))"); |
4bad7d45 | 311 | printf(" [-v] (verbose output)"); |
9e97e478 | 312 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); |
1430ee0b MD |
313 | printf("\n"); |
314 | } | |
315 | ||
cf380c2f | 316 | int main(int argc, char **argv) |
ac260fd9 | 317 | { |
f69f195a | 318 | int err; |
8c86b9a1 | 319 | pthread_t *tid_reader, *tid_writer; |
f69f195a | 320 | void *tret; |
8c86b9a1 | 321 | unsigned long long *count_reader, *count_writer; |
1eec319e | 322 | unsigned long long tot_reads = 0, tot_writes = 0; |
9e97e478 | 323 | int i, a; |
f69f195a | 324 | |
8c86b9a1 MD |
325 | if (argc < 4) { |
326 | show_usage(argc, argv); | |
327 | return -1; | |
328 | } | |
329 | ||
330 | err = sscanf(argv[1], "%u", &nr_readers); | |
331 | if (err != 1) { | |
1430ee0b | 332 | show_usage(argc, argv); |
cf380c2f MD |
333 | return -1; |
334 | } | |
335 | ||
8c86b9a1 MD |
336 | err = sscanf(argv[2], "%u", &nr_writers); |
337 | if (err != 1) { | |
338 | show_usage(argc, argv); | |
339 | return -1; | |
340 | } | |
341 | ||
342 | err = sscanf(argv[3], "%lu", &duration); | |
cf380c2f | 343 | if (err != 1) { |
1430ee0b | 344 | show_usage(argc, argv); |
cf380c2f MD |
345 | return -1; |
346 | } | |
347 | ||
8c86b9a1 | 348 | for (i = 4; i < argc; i++) { |
cf380c2f MD |
349 | if (argv[i][0] != '-') |
350 | continue; | |
351 | switch (argv[i][1]) { | |
bb488185 | 352 | #ifdef DEBUG_YIELD |
cf380c2f MD |
353 | case 'r': |
354 | yield_active |= YIELD_READ; | |
355 | break; | |
356 | case 'w': | |
357 | yield_active |= YIELD_WRITE; | |
358 | break; | |
bb488185 | 359 | #endif |
9e97e478 MD |
360 | case 'a': |
361 | if (argc < i + 2) { | |
362 | show_usage(argc, argv); | |
363 | return -1; | |
364 | } | |
365 | a = atoi(argv[++i]); | |
2a7ac59d | 366 | cpu_affinities[next_aff++] = a; |
9e97e478 | 367 | use_affinity = 1; |
4bad7d45 | 368 | printf_verbose("Adding CPU %d affinity\n", a); |
9e97e478 | 369 | break; |
8b632bab MD |
370 | case 'c': |
371 | if (argc < i + 2) { | |
372 | show_usage(argc, argv); | |
373 | return -1; | |
374 | } | |
9e31d0f0 | 375 | rduration = atol(argv[++i]); |
8b632bab | 376 | break; |
8c86b9a1 | 377 | case 'd': |
7685d963 | 378 | if (argc < i + 2) { |
8c86b9a1 MD |
379 | show_usage(argc, argv); |
380 | return -1; | |
381 | } | |
9e31d0f0 | 382 | wdelay = atol(argv[++i]); |
bb488185 | 383 | break; |
31b598e0 MD |
384 | case 'e': |
385 | if (argc < i + 2) { | |
386 | show_usage(argc, argv); | |
387 | return -1; | |
388 | } | |
389 | wduration = atol(argv[++i]); | |
390 | break; | |
4bad7d45 MD |
391 | case 'v': |
392 | verbose_mode = 1; | |
393 | break; | |
cf380c2f MD |
394 | } |
395 | } | |
cf380c2f | 396 | |
4bad7d45 | 397 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", |
8c86b9a1 | 398 | duration, nr_readers, nr_writers); |
9e31d0f0 | 399 | printf_verbose("Writer delay : %lu loops.\n", wdelay); |
4bad7d45 MD |
400 | printf_verbose("Reader duration : %lu loops.\n", rduration); |
401 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", | |
2d72fae2 MD |
402 | "main", (unsigned long) pthread_self(), |
403 | (unsigned long)gettid()); | |
87bd15cd | 404 | |
92e8d9d6 | 405 | test_array = calloc(1, sizeof(*test_array) * ARRAY_SIZE); |
8c86b9a1 MD |
406 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); |
407 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
408 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
409 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
410 | ||
2a7ac59d MD |
411 | next_aff = 0; |
412 | ||
8c86b9a1 | 413 | for (i = 0; i < nr_readers; i++) { |
1eec319e MD |
414 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
415 | &count_reader[i]); | |
f69f195a MD |
416 | if (err != 0) |
417 | exit(1); | |
418 | } | |
8c86b9a1 | 419 | for (i = 0; i < nr_writers; i++) { |
1eec319e MD |
420 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
421 | &count_writer[i]); | |
f69f195a MD |
422 | if (err != 0) |
423 | exit(1); | |
424 | } | |
425 | ||
5481ddb3 | 426 | cmm_smp_mb(); |
8c86b9a1 | 427 | |
78efb485 MD |
428 | test_go = 1; |
429 | ||
430 | sleep(duration); | |
431 | ||
432 | test_stop = 1; | |
433 | ||
8c86b9a1 | 434 | for (i = 0; i < nr_readers; i++) { |
f69f195a MD |
435 | err = pthread_join(tid_reader[i], &tret); |
436 | if (err != 0) | |
437 | exit(1); | |
1eec319e | 438 | tot_reads += count_reader[i]; |
f69f195a | 439 | } |
8c86b9a1 | 440 | for (i = 0; i < nr_writers; i++) { |
f69f195a MD |
441 | err = pthread_join(tid_writer[i], &tret); |
442 | if (err != 0) | |
443 | exit(1); | |
1eec319e | 444 | tot_writes += count_writer[i]; |
f69f195a | 445 | } |
5873cd17 | 446 | |
4bad7d45 | 447 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, |
1eec319e | 448 | tot_writes); |
a50a7b43 | 449 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu " |
cda3c71d | 450 | "nr_writers %3u " |
9e31d0f0 | 451 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", |
a50a7b43 | 452 | argv[0], duration, nr_readers, rduration, wduration, |
4bad7d45 MD |
453 | nr_writers, wdelay, tot_reads, tot_writes, |
454 | tot_reads + tot_writes); | |
bb488185 | 455 | test_array_free(test_rcu_pointer); |
8c86b9a1 MD |
456 | free(test_array); |
457 | free(tid_reader); | |
458 | free(tid_writer); | |
459 | free(count_reader); | |
460 | free(count_writer); | |
f69f195a | 461 | return 0; |
ac260fd9 | 462 | } |