Commit | Line | Data |
---|---|---|
fdee2e6d 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> |
fdee2e6d MD |
7 | * |
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. | |
21 | */ | |
22 | ||
23 | #define _GNU_SOURCE | |
d8540fc5 | 24 | #include "../config.h" |
fdee2e6d MD |
25 | #include <stdio.h> |
26 | #include <pthread.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/wait.h> | |
31 | #include <unistd.h> | |
32 | #include <stdio.h> | |
33 | #include <assert.h> | |
94b343fd | 34 | #include <errno.h> |
fdee2e6d MD |
35 | |
36 | #include <urcu/arch.h> | |
bd252a04 | 37 | #include <urcu/tls-compat.h> |
95d8822d | 38 | #include "cpuset.h" |
fdee2e6d | 39 | |
65fcc7e9 MD |
40 | #ifdef __linux__ |
41 | #include <syscall.h> | |
42 | #endif | |
43 | ||
fdee2e6d MD |
44 | /* hardcoded number of CPUs */ |
45 | #define NR_CPUS 16384 | |
46 | ||
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 | ||
62 | #ifndef DYNAMIC_LINK_TEST | |
63 | #define _LGPL_SOURCE | |
64 | #else | |
65 | #define debug_yield_read() | |
66 | #endif | |
67 | #include <urcu-bp.h> | |
68 | ||
69 | struct test_array { | |
70 | int a; | |
71 | }; | |
72 | ||
73 | static volatile int test_go, test_stop; | |
74 | ||
75 | static unsigned long wdelay; | |
76 | ||
77 | static struct test_array *test_rcu_pointer; | |
78 | ||
79 | static unsigned long duration; | |
80 | ||
81 | /* read-side C.S. duration, in loops */ | |
82 | static unsigned long rduration; | |
83 | ||
31b598e0 MD |
84 | /* write-side C.S. duration, in loops */ |
85 | static unsigned long wduration; | |
86 | ||
fdee2e6d MD |
87 | static inline void loop_sleep(unsigned long l) |
88 | { | |
89 | while(l-- != 0) | |
06f22bdb | 90 | caa_cpu_relax(); |
fdee2e6d MD |
91 | } |
92 | ||
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 | ||
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 |
fdee2e6d 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 | |
fdee2e6d 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 | |
fdee2e6d | 134 | sched_setaffinity(0, sizeof(mask), &mask); |
d8540fc5 PA |
135 | #endif |
136 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
fdee2e6d MD |
137 | } |
138 | ||
139 | /* | |
140 | * returns 0 if test should end. | |
141 | */ | |
142 | static int test_duration_write(void) | |
143 | { | |
144 | return !test_stop; | |
145 | } | |
146 | ||
147 | static int test_duration_read(void) | |
148 | { | |
149 | return !test_stop; | |
150 | } | |
151 | ||
bd252a04 MD |
152 | static DEFINE_URCU_TLS(unsigned long long, nr_writes); |
153 | static DEFINE_URCU_TLS(unsigned long long, nr_reads); | |
fdee2e6d MD |
154 | |
155 | static unsigned int nr_readers; | |
156 | static unsigned int nr_writers; | |
157 | ||
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 | } | |
180 | ||
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. | |
fdee2e6d MD |
186 | */ |
187 | #define ARRAY_SIZE (1048576 * nr_writers) | |
188 | #define ARRAY_POISON 0xDEADBEEF | |
189 | static int array_index; | |
190 | static struct test_array *test_array; | |
191 | ||
192 | static struct test_array *test_array_alloc(void) | |
193 | { | |
194 | struct test_array *ret; | |
195 | int index; | |
196 | ||
fdee2e6d 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; | |
fdee2e6d MD |
204 | return ret; |
205 | } | |
206 | ||
207 | static void test_array_free(struct test_array *ptr) | |
208 | { | |
209 | if (!ptr) | |
210 | return; | |
fdee2e6d | 211 | ptr->a = ARRAY_POISON; |
fdee2e6d MD |
212 | } |
213 | ||
214 | void *thr_reader(void *_count) | |
215 | { | |
216 | unsigned long long *count = _count; | |
217 | struct test_array *local_ptr; | |
218 | ||
219 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
2d72fae2 MD |
220 | "reader", (unsigned long) pthread_self(), |
221 | (unsigned long) gettid()); | |
fdee2e6d MD |
222 | |
223 | set_affinity(); | |
224 | ||
225 | rcu_register_thread(); | |
226 | ||
227 | while (!test_go) | |
228 | { | |
229 | } | |
5481ddb3 | 230 | cmm_smp_mb(); |
fdee2e6d MD |
231 | |
232 | for (;;) { | |
233 | rcu_read_lock(); | |
234 | local_ptr = rcu_dereference(test_rcu_pointer); | |
235 | debug_yield_read(); | |
236 | if (local_ptr) | |
237 | assert(local_ptr->a == 8); | |
a0b7f7ea | 238 | if (caa_unlikely(rduration)) |
fdee2e6d MD |
239 | loop_sleep(rduration); |
240 | rcu_read_unlock(); | |
bd252a04 | 241 | URCU_TLS(nr_reads)++; |
a0b7f7ea | 242 | if (caa_unlikely(!test_duration_read())) |
fdee2e6d MD |
243 | break; |
244 | } | |
245 | ||
246 | rcu_unregister_thread(); | |
247 | ||
bd252a04 | 248 | *count = URCU_TLS(nr_reads); |
fdee2e6d | 249 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
2d72fae2 MD |
250 | "reader", (unsigned long) pthread_self(), |
251 | (unsigned long) gettid()); | |
fdee2e6d MD |
252 | return ((void*)1); |
253 | ||
254 | } | |
255 | ||
256 | void *thr_writer(void *_count) | |
257 | { | |
258 | unsigned long long *count = _count; | |
259 | struct test_array *new, *old; | |
260 | ||
261 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
2d72fae2 MD |
262 | "writer", (unsigned long) pthread_self(), |
263 | (unsigned long) gettid()); | |
fdee2e6d MD |
264 | |
265 | set_affinity(); | |
266 | ||
267 | while (!test_go) | |
268 | { | |
269 | } | |
5481ddb3 | 270 | cmm_smp_mb(); |
fdee2e6d MD |
271 | |
272 | for (;;) { | |
d4267b0b | 273 | rcu_copy_mutex_lock(); |
fdee2e6d MD |
274 | new = test_array_alloc(); |
275 | new->a = 8; | |
d2835e6f | 276 | old = rcu_xchg_pointer(&test_rcu_pointer, new); |
a0b7f7ea | 277 | if (caa_unlikely(wduration)) |
31b598e0 | 278 | loop_sleep(wduration); |
d2835e6f | 279 | synchronize_rcu(); |
fdee2e6d MD |
280 | if (old) |
281 | old->a = 0; | |
282 | test_array_free(old); | |
d4267b0b | 283 | rcu_copy_mutex_unlock(); |
bd252a04 | 284 | URCU_TLS(nr_writes)++; |
a0b7f7ea | 285 | if (caa_unlikely(!test_duration_write())) |
fdee2e6d | 286 | break; |
a0b7f7ea | 287 | if (caa_unlikely(wdelay)) |
fdee2e6d MD |
288 | loop_sleep(wdelay); |
289 | } | |
290 | ||
291 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
2d72fae2 MD |
292 | "writer", (unsigned long) pthread_self(), |
293 | (unsigned long) gettid()); | |
bd252a04 | 294 | *count = URCU_TLS(nr_writes); |
fdee2e6d MD |
295 | return ((void*)2); |
296 | } | |
297 | ||
298 | void show_usage(int argc, char **argv) | |
299 | { | |
300 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); | |
301 | #ifdef DEBUG_YIELD | |
302 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
303 | #endif | |
304 | printf(" [-d delay] (writer period (us))"); | |
305 | printf(" [-c duration] (reader C.S. duration (in loops))"); | |
31b598e0 | 306 | printf(" [-e duration] (writer C.S. duration (in loops))"); |
fdee2e6d MD |
307 | printf(" [-v] (verbose output)"); |
308 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); | |
309 | printf("\n"); | |
310 | } | |
311 | ||
312 | int main(int argc, char **argv) | |
313 | { | |
314 | int err; | |
315 | pthread_t *tid_reader, *tid_writer; | |
316 | void *tret; | |
317 | unsigned long long *count_reader, *count_writer; | |
318 | unsigned long long tot_reads = 0, tot_writes = 0; | |
319 | int i, a; | |
320 | ||
321 | if (argc < 4) { | |
322 | show_usage(argc, argv); | |
323 | return -1; | |
324 | } | |
325 | ||
326 | err = sscanf(argv[1], "%u", &nr_readers); | |
327 | if (err != 1) { | |
328 | show_usage(argc, argv); | |
329 | return -1; | |
330 | } | |
331 | ||
332 | err = sscanf(argv[2], "%u", &nr_writers); | |
333 | if (err != 1) { | |
334 | show_usage(argc, argv); | |
335 | return -1; | |
336 | } | |
337 | ||
338 | err = sscanf(argv[3], "%lu", &duration); | |
339 | if (err != 1) { | |
340 | show_usage(argc, argv); | |
341 | return -1; | |
342 | } | |
343 | ||
344 | for (i = 4; i < argc; i++) { | |
345 | if (argv[i][0] != '-') | |
346 | continue; | |
347 | switch (argv[i][1]) { | |
348 | #ifdef DEBUG_YIELD | |
349 | case 'r': | |
350 | yield_active |= YIELD_READ; | |
351 | break; | |
352 | case 'w': | |
353 | yield_active |= YIELD_WRITE; | |
354 | break; | |
355 | #endif | |
356 | case 'a': | |
357 | if (argc < i + 2) { | |
358 | show_usage(argc, argv); | |
359 | return -1; | |
360 | } | |
361 | a = atoi(argv[++i]); | |
362 | cpu_affinities[next_aff++] = a; | |
363 | use_affinity = 1; | |
364 | printf_verbose("Adding CPU %d affinity\n", a); | |
365 | break; | |
366 | case 'c': | |
367 | if (argc < i + 2) { | |
368 | show_usage(argc, argv); | |
369 | return -1; | |
370 | } | |
371 | rduration = atol(argv[++i]); | |
372 | break; | |
373 | case 'd': | |
374 | if (argc < i + 2) { | |
375 | show_usage(argc, argv); | |
376 | return -1; | |
377 | } | |
378 | wdelay = atol(argv[++i]); | |
379 | break; | |
31b598e0 MD |
380 | case 'e': |
381 | if (argc < i + 2) { | |
382 | show_usage(argc, argv); | |
383 | return -1; | |
384 | } | |
385 | wduration = atol(argv[++i]); | |
386 | break; | |
fdee2e6d MD |
387 | case 'v': |
388 | verbose_mode = 1; | |
389 | break; | |
390 | } | |
391 | } | |
392 | ||
393 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", | |
394 | duration, nr_readers, nr_writers); | |
395 | printf_verbose("Writer delay : %lu loops.\n", wdelay); | |
396 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
397 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", | |
2d72fae2 MD |
398 | "main", (unsigned long) pthread_self(), |
399 | (unsigned long) gettid()); | |
fdee2e6d | 400 | |
92e8d9d6 | 401 | test_array = calloc(1, sizeof(*test_array) * ARRAY_SIZE); |
f90535ef MD |
402 | tid_reader = calloc(nr_readers, sizeof(*tid_reader)); |
403 | tid_writer = calloc(nr_writers, sizeof(*tid_writer)); | |
404 | count_reader = calloc(nr_readers, sizeof(*count_reader)); | |
405 | count_writer = calloc(nr_writers, sizeof(*count_writer)); | |
fdee2e6d MD |
406 | |
407 | next_aff = 0; | |
408 | ||
409 | for (i = 0; i < nr_readers; i++) { | |
410 | err = pthread_create(&tid_reader[i], NULL, thr_reader, | |
411 | &count_reader[i]); | |
412 | if (err != 0) | |
413 | exit(1); | |
414 | } | |
415 | for (i = 0; i < nr_writers; i++) { | |
416 | err = pthread_create(&tid_writer[i], NULL, thr_writer, | |
417 | &count_writer[i]); | |
418 | if (err != 0) | |
419 | exit(1); | |
420 | } | |
421 | ||
5481ddb3 | 422 | cmm_smp_mb(); |
fdee2e6d MD |
423 | |
424 | test_go = 1; | |
425 | ||
426 | sleep(duration); | |
427 | ||
428 | test_stop = 1; | |
429 | ||
430 | for (i = 0; i < nr_readers; i++) { | |
431 | err = pthread_join(tid_reader[i], &tret); | |
432 | if (err != 0) | |
433 | exit(1); | |
434 | tot_reads += count_reader[i]; | |
435 | } | |
436 | for (i = 0; i < nr_writers; i++) { | |
437 | err = pthread_join(tid_writer[i], &tret); | |
438 | if (err != 0) | |
439 | exit(1); | |
440 | tot_writes += count_writer[i]; | |
441 | } | |
442 | ||
443 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, | |
444 | tot_writes); | |
a50a7b43 | 445 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu " |
fdee2e6d MD |
446 | "nr_writers %3u " |
447 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", | |
a50a7b43 | 448 | argv[0], duration, nr_readers, rduration, wduration, |
fdee2e6d MD |
449 | nr_writers, wdelay, tot_reads, tot_writes, |
450 | tot_reads + tot_writes); | |
451 | test_array_free(test_rcu_pointer); | |
452 | free(test_array); | |
453 | free(tid_reader); | |
454 | free(tid_writer); | |
455 | free(count_reader); | |
456 | free(count_writer); | |
457 | return 0; | |
458 | } |