Commit | Line | Data |
---|---|---|
b257a10b MD |
1 | /* |
2 | * test_urcu.c | |
3 | * | |
4 | * Userspace RCU library - test program | |
5 | * | |
6 | * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
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 |
ac260fd9 | 24 | #include <stdio.h> |
f69f195a MD |
25 | #include <pthread.h> |
26 | #include <stdlib.h> | |
41718ff9 | 27 | #include <string.h> |
f69f195a MD |
28 | #include <sys/types.h> |
29 | #include <sys/wait.h> | |
30 | #include <unistd.h> | |
31 | #include <stdio.h> | |
41718ff9 | 32 | #include <assert.h> |
87bd15cd | 33 | #include <sys/syscall.h> |
9e97e478 | 34 | #include <sched.h> |
87bd15cd | 35 | |
7685d963 MD |
36 | #include "arch.h" |
37 | ||
87bd15cd BW |
38 | #if defined(_syscall0) |
39 | _syscall0(pid_t, gettid) | |
40 | #elif defined(__NR_gettid) | |
41 | static inline pid_t gettid(void) | |
42 | { | |
43 | return syscall(__NR_gettid); | |
44 | } | |
45 | #else | |
46 | #warning "use pid as tid" | |
47 | static inline pid_t gettid(void) | |
48 | { | |
49 | return getpid(); | |
50 | } | |
51 | #endif | |
52 | ||
121a5d44 MD |
53 | #ifndef DYNAMIC_LINK_TEST |
54 | #define _LGPL_SOURCE | |
55 | #else | |
56 | #define debug_yield_read() | |
57 | #endif | |
ac260fd9 MD |
58 | #include "urcu.h" |
59 | ||
41718ff9 MD |
60 | struct test_array { |
61 | int a; | |
41718ff9 MD |
62 | }; |
63 | ||
78efb485 | 64 | static volatile int test_go, test_stop; |
8c86b9a1 MD |
65 | |
66 | static int wdelay; | |
bb488185 | 67 | |
41718ff9 MD |
68 | static struct test_array *test_rcu_pointer; |
69 | ||
cf380c2f | 70 | static unsigned long duration; |
cf380c2f | 71 | |
daddf5b0 | 72 | /* read-side C.S. duration, in loops */ |
8b632bab MD |
73 | static unsigned long rduration; |
74 | ||
daddf5b0 MD |
75 | static inline void loop_sleep(unsigned long l) |
76 | { | |
77 | while(l-- != 0) | |
78 | cpu_relax(); | |
79 | } | |
80 | ||
4bad7d45 MD |
81 | static int verbose_mode; |
82 | ||
83 | #define printf_verbose(fmt, args...) \ | |
84 | do { \ | |
85 | if (verbose_mode) \ | |
86 | printf(fmt, args); \ | |
87 | } while (0) | |
88 | ||
cf380c2f MD |
89 | /* |
90 | * returns 0 if test should end. | |
91 | */ | |
5873cd17 | 92 | static int test_duration_write(void) |
cf380c2f | 93 | { |
78efb485 | 94 | return !test_stop; |
5873cd17 MD |
95 | } |
96 | ||
97 | static int test_duration_read(void) | |
98 | { | |
78efb485 | 99 | return !test_stop; |
cf380c2f MD |
100 | } |
101 | ||
1eec319e MD |
102 | static unsigned long long __thread nr_writes; |
103 | static unsigned long long __thread nr_reads; | |
104 | ||
8c86b9a1 MD |
105 | static unsigned int nr_readers; |
106 | static unsigned int nr_writers; | |
107 | ||
c265818b MD |
108 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
109 | ||
110 | void rcu_copy_mutex_lock(void) | |
111 | { | |
112 | int ret; | |
113 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
114 | if (ret) { | |
115 | perror("Error in pthread mutex lock"); | |
116 | exit(-1); | |
117 | } | |
118 | } | |
119 | ||
120 | void rcu_copy_mutex_unlock(void) | |
121 | { | |
122 | int ret; | |
123 | ||
124 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
125 | if (ret) { | |
126 | perror("Error in pthread mutex unlock"); | |
127 | exit(-1); | |
128 | } | |
129 | } | |
f69f195a | 130 | |
bb488185 MD |
131 | /* |
132 | * malloc/free are reusing memory areas too quickly, which does not let us | |
133 | * test races appropriately. Use a large circular array for allocations. | |
8c86b9a1 | 134 | * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail. |
bb488185 | 135 | */ |
8c86b9a1 | 136 | #define ARRAY_SIZE (1048576 * nr_writers) |
bb488185 MD |
137 | #define ARRAY_POISON 0xDEADBEEF |
138 | static int array_index; | |
8c86b9a1 | 139 | static struct test_array *test_array; |
bb488185 MD |
140 | |
141 | static struct test_array *test_array_alloc(void) | |
142 | { | |
143 | struct test_array *ret; | |
144 | int index; | |
145 | ||
146 | rcu_copy_mutex_lock(); | |
147 | index = array_index % ARRAY_SIZE; | |
148 | assert(test_array[index].a == ARRAY_POISON || | |
149 | test_array[index].a == 0); | |
150 | ret = &test_array[index]; | |
151 | array_index++; | |
152 | if (array_index == ARRAY_SIZE) | |
153 | array_index = 0; | |
154 | rcu_copy_mutex_unlock(); | |
155 | return ret; | |
156 | } | |
157 | ||
158 | static void test_array_free(struct test_array *ptr) | |
159 | { | |
160 | if (!ptr) | |
161 | return; | |
162 | rcu_copy_mutex_lock(); | |
163 | ptr->a = ARRAY_POISON; | |
164 | rcu_copy_mutex_unlock(); | |
165 | } | |
166 | ||
1eec319e | 167 | void *thr_reader(void *_count) |
f69f195a | 168 | { |
1eec319e | 169 | unsigned long long *count = _count; |
41718ff9 MD |
170 | struct test_array *local_ptr; |
171 | ||
4bad7d45 | 172 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 173 | "reader", pthread_self(), (unsigned long)gettid()); |
f69f195a | 174 | |
121a5d44 | 175 | rcu_register_thread(); |
f69f195a | 176 | |
8c86b9a1 MD |
177 | while (!test_go) |
178 | { | |
179 | } | |
7685d963 | 180 | smp_mb(); |
8c86b9a1 | 181 | |
cf380c2f | 182 | for (;;) { |
1430ee0b | 183 | rcu_read_lock(); |
cf380c2f | 184 | local_ptr = rcu_dereference(test_rcu_pointer); |
bb488185 | 185 | debug_yield_read(); |
cf380c2f MD |
186 | if (local_ptr) |
187 | assert(local_ptr->a == 8); | |
8b632bab | 188 | if (unlikely(rduration)) |
daddf5b0 | 189 | loop_sleep(rduration); |
1430ee0b | 190 | rcu_read_unlock(); |
1eec319e | 191 | nr_reads++; |
59d5a406 | 192 | if (unlikely(!test_duration_read())) |
cf380c2f | 193 | break; |
41718ff9 | 194 | } |
f69f195a | 195 | |
121a5d44 | 196 | rcu_unregister_thread(); |
41718ff9 | 197 | |
1eec319e | 198 | *count = nr_reads; |
4bad7d45 | 199 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
cf380c2f | 200 | "reader", pthread_self(), (unsigned long)gettid()); |
f69f195a MD |
201 | return ((void*)1); |
202 | ||
203 | } | |
204 | ||
1eec319e | 205 | void *thr_writer(void *_count) |
f69f195a | 206 | { |
1eec319e | 207 | unsigned long long *count = _count; |
41718ff9 | 208 | struct test_array *new, *old; |
f69f195a | 209 | |
4bad7d45 | 210 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 211 | "writer", pthread_self(), (unsigned long)gettid()); |
f69f195a | 212 | |
8c86b9a1 MD |
213 | while (!test_go) |
214 | { | |
215 | } | |
7685d963 | 216 | smp_mb(); |
8c86b9a1 | 217 | |
cf380c2f | 218 | for (;;) { |
bb488185 | 219 | new = test_array_alloc(); |
c265818b | 220 | rcu_copy_mutex_lock(); |
41718ff9 | 221 | old = test_rcu_pointer; |
cf380c2f | 222 | if (old) |
41718ff9 | 223 | assert(old->a == 8); |
ad6ce6ae | 224 | new->a = 8; |
121a5d44 | 225 | old = rcu_publish_content(&test_rcu_pointer, new); |
c265818b | 226 | rcu_copy_mutex_unlock(); |
41718ff9 | 227 | /* can be done after unlock */ |
cf380c2f | 228 | if (old) |
8c8eed97 | 229 | old->a = 0; |
bb488185 | 230 | test_array_free(old); |
1eec319e | 231 | nr_writes++; |
59d5a406 | 232 | if (unlikely(!test_duration_write())) |
cf380c2f | 233 | break; |
59d5a406 | 234 | if (unlikely(wdelay)) |
8c86b9a1 | 235 | usleep(wdelay); |
f69f195a MD |
236 | } |
237 | ||
4bad7d45 | 238 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
cf380c2f | 239 | "writer", pthread_self(), (unsigned long)gettid()); |
1eec319e | 240 | *count = nr_writes; |
f69f195a MD |
241 | return ((void*)2); |
242 | } | |
ac260fd9 | 243 | |
1430ee0b MD |
244 | void show_usage(int argc, char **argv) |
245 | { | |
8c86b9a1 | 246 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); |
1430ee0b MD |
247 | #ifdef DEBUG_YIELD |
248 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
249 | #endif | |
7685d963 | 250 | printf(" [-d delay] (writer period (us))"); |
daddf5b0 | 251 | printf(" [-c duration] (reader C.S. duration (in loops))"); |
4bad7d45 | 252 | printf(" [-v] (verbose output)"); |
9e97e478 | 253 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); |
1430ee0b MD |
254 | printf("\n"); |
255 | } | |
256 | ||
9e97e478 MD |
257 | cpu_set_t affinity; |
258 | ||
cf380c2f | 259 | int main(int argc, char **argv) |
ac260fd9 | 260 | { |
f69f195a | 261 | int err; |
8c86b9a1 | 262 | pthread_t *tid_reader, *tid_writer; |
f69f195a | 263 | void *tret; |
8c86b9a1 | 264 | unsigned long long *count_reader, *count_writer; |
1eec319e | 265 | unsigned long long tot_reads = 0, tot_writes = 0; |
9e97e478 MD |
266 | int i, a; |
267 | int use_affinity = 0; | |
f69f195a | 268 | |
8c86b9a1 MD |
269 | if (argc < 4) { |
270 | show_usage(argc, argv); | |
271 | return -1; | |
272 | } | |
273 | ||
274 | err = sscanf(argv[1], "%u", &nr_readers); | |
275 | if (err != 1) { | |
1430ee0b | 276 | show_usage(argc, argv); |
cf380c2f MD |
277 | return -1; |
278 | } | |
279 | ||
8c86b9a1 MD |
280 | err = sscanf(argv[2], "%u", &nr_writers); |
281 | if (err != 1) { | |
282 | show_usage(argc, argv); | |
283 | return -1; | |
284 | } | |
285 | ||
286 | err = sscanf(argv[3], "%lu", &duration); | |
cf380c2f | 287 | if (err != 1) { |
1430ee0b | 288 | show_usage(argc, argv); |
cf380c2f MD |
289 | return -1; |
290 | } | |
291 | ||
9e97e478 MD |
292 | CPU_ZERO(&affinity); |
293 | ||
8c86b9a1 | 294 | for (i = 4; i < argc; i++) { |
cf380c2f MD |
295 | if (argv[i][0] != '-') |
296 | continue; | |
297 | switch (argv[i][1]) { | |
bb488185 | 298 | #ifdef DEBUG_YIELD |
cf380c2f MD |
299 | case 'r': |
300 | yield_active |= YIELD_READ; | |
301 | break; | |
302 | case 'w': | |
303 | yield_active |= YIELD_WRITE; | |
304 | break; | |
bb488185 | 305 | #endif |
9e97e478 MD |
306 | case 'a': |
307 | if (argc < i + 2) { | |
308 | show_usage(argc, argv); | |
309 | return -1; | |
310 | } | |
311 | a = atoi(argv[++i]); | |
312 | CPU_SET(a, &affinity); | |
313 | use_affinity = 1; | |
4bad7d45 | 314 | printf_verbose("Adding CPU %d affinity\n", a); |
9e97e478 | 315 | break; |
8b632bab MD |
316 | case 'c': |
317 | if (argc < i + 2) { | |
318 | show_usage(argc, argv); | |
319 | return -1; | |
320 | } | |
321 | rduration = atoi(argv[++i]); | |
322 | break; | |
8c86b9a1 | 323 | case 'd': |
7685d963 | 324 | if (argc < i + 2) { |
8c86b9a1 MD |
325 | show_usage(argc, argv); |
326 | return -1; | |
327 | } | |
328 | wdelay = atoi(argv[++i]); | |
bb488185 | 329 | break; |
4bad7d45 MD |
330 | case 'v': |
331 | verbose_mode = 1; | |
332 | break; | |
cf380c2f MD |
333 | } |
334 | } | |
cf380c2f | 335 | |
4bad7d45 | 336 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", |
8c86b9a1 | 337 | duration, nr_readers, nr_writers); |
4bad7d45 MD |
338 | printf_verbose("Writer delay : %u us.\n", wdelay); |
339 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
340 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", | |
87bd15cd BW |
341 | "main", pthread_self(), (unsigned long)gettid()); |
342 | ||
9e97e478 MD |
343 | if (use_affinity |
344 | && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) { | |
345 | perror("sched_setaffinity"); | |
346 | exit(-1); | |
347 | } | |
348 | ||
8c86b9a1 MD |
349 | test_array = malloc(sizeof(*test_array) * ARRAY_SIZE); |
350 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); | |
351 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
352 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
353 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
354 | ||
355 | for (i = 0; i < nr_readers; i++) { | |
1eec319e MD |
356 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
357 | &count_reader[i]); | |
f69f195a MD |
358 | if (err != 0) |
359 | exit(1); | |
360 | } | |
8c86b9a1 | 361 | for (i = 0; i < nr_writers; i++) { |
1eec319e MD |
362 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
363 | &count_writer[i]); | |
f69f195a MD |
364 | if (err != 0) |
365 | exit(1); | |
366 | } | |
367 | ||
7685d963 | 368 | smp_mb(); |
8c86b9a1 | 369 | |
78efb485 MD |
370 | test_go = 1; |
371 | ||
372 | sleep(duration); | |
373 | ||
374 | test_stop = 1; | |
375 | ||
8c86b9a1 | 376 | for (i = 0; i < nr_readers; i++) { |
f69f195a MD |
377 | err = pthread_join(tid_reader[i], &tret); |
378 | if (err != 0) | |
379 | exit(1); | |
1eec319e | 380 | tot_reads += count_reader[i]; |
f69f195a | 381 | } |
8c86b9a1 | 382 | for (i = 0; i < nr_writers; i++) { |
f69f195a MD |
383 | err = pthread_join(tid_writer[i], &tret); |
384 | if (err != 0) | |
385 | exit(1); | |
1eec319e | 386 | tot_writes += count_writer[i]; |
f69f195a | 387 | } |
5873cd17 | 388 | |
4bad7d45 | 389 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, |
1eec319e | 390 | tot_writes); |
6a190115 | 391 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " |
cda3c71d MD |
392 | "nr_writers %3u " |
393 | "wdelay %4u nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", | |
4bad7d45 MD |
394 | argv[0], duration, nr_readers, rduration, |
395 | nr_writers, wdelay, tot_reads, tot_writes, | |
396 | tot_reads + tot_writes); | |
bb488185 | 397 | test_array_free(test_rcu_pointer); |
8c86b9a1 MD |
398 | free(test_array); |
399 | free(tid_reader); | |
400 | free(tid_writer); | |
401 | free(count_reader); | |
402 | free(count_writer); | |
f69f195a | 403 | return 0; |
ac260fd9 | 404 | } |