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 | |
8b632bab MD |
72 | /* read-side C.S. duration, in us */ |
73 | static unsigned long rduration; | |
74 | ||
cf380c2f MD |
75 | /* |
76 | * returns 0 if test should end. | |
77 | */ | |
5873cd17 | 78 | static int test_duration_write(void) |
cf380c2f | 79 | { |
78efb485 | 80 | return !test_stop; |
5873cd17 MD |
81 | } |
82 | ||
83 | static int test_duration_read(void) | |
84 | { | |
78efb485 | 85 | return !test_stop; |
cf380c2f MD |
86 | } |
87 | ||
1eec319e MD |
88 | static unsigned long long __thread nr_writes; |
89 | static unsigned long long __thread nr_reads; | |
90 | ||
8c86b9a1 MD |
91 | static unsigned int nr_readers; |
92 | static unsigned int nr_writers; | |
93 | ||
c265818b MD |
94 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
95 | ||
96 | void rcu_copy_mutex_lock(void) | |
97 | { | |
98 | int ret; | |
99 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
100 | if (ret) { | |
101 | perror("Error in pthread mutex lock"); | |
102 | exit(-1); | |
103 | } | |
104 | } | |
105 | ||
106 | void rcu_copy_mutex_unlock(void) | |
107 | { | |
108 | int ret; | |
109 | ||
110 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
111 | if (ret) { | |
112 | perror("Error in pthread mutex unlock"); | |
113 | exit(-1); | |
114 | } | |
115 | } | |
f69f195a | 116 | |
bb488185 MD |
117 | /* |
118 | * malloc/free are reusing memory areas too quickly, which does not let us | |
119 | * test races appropriately. Use a large circular array for allocations. | |
8c86b9a1 | 120 | * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail. |
bb488185 | 121 | */ |
8c86b9a1 | 122 | #define ARRAY_SIZE (1048576 * nr_writers) |
bb488185 MD |
123 | #define ARRAY_POISON 0xDEADBEEF |
124 | static int array_index; | |
8c86b9a1 | 125 | static struct test_array *test_array; |
bb488185 MD |
126 | |
127 | static struct test_array *test_array_alloc(void) | |
128 | { | |
129 | struct test_array *ret; | |
130 | int index; | |
131 | ||
132 | rcu_copy_mutex_lock(); | |
133 | index = array_index % ARRAY_SIZE; | |
134 | assert(test_array[index].a == ARRAY_POISON || | |
135 | test_array[index].a == 0); | |
136 | ret = &test_array[index]; | |
137 | array_index++; | |
138 | if (array_index == ARRAY_SIZE) | |
139 | array_index = 0; | |
140 | rcu_copy_mutex_unlock(); | |
141 | return ret; | |
142 | } | |
143 | ||
144 | static void test_array_free(struct test_array *ptr) | |
145 | { | |
146 | if (!ptr) | |
147 | return; | |
148 | rcu_copy_mutex_lock(); | |
149 | ptr->a = ARRAY_POISON; | |
150 | rcu_copy_mutex_unlock(); | |
151 | } | |
152 | ||
1eec319e | 153 | void *thr_reader(void *_count) |
f69f195a | 154 | { |
1eec319e | 155 | unsigned long long *count = _count; |
41718ff9 MD |
156 | struct test_array *local_ptr; |
157 | ||
cf380c2f | 158 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 159 | "reader", pthread_self(), (unsigned long)gettid()); |
f69f195a | 160 | |
121a5d44 | 161 | rcu_register_thread(); |
f69f195a | 162 | |
8c86b9a1 MD |
163 | while (!test_go) |
164 | { | |
165 | } | |
7685d963 | 166 | smp_mb(); |
8c86b9a1 | 167 | |
cf380c2f | 168 | for (;;) { |
1430ee0b | 169 | rcu_read_lock(); |
cf380c2f | 170 | local_ptr = rcu_dereference(test_rcu_pointer); |
bb488185 | 171 | debug_yield_read(); |
cf380c2f MD |
172 | if (local_ptr) |
173 | assert(local_ptr->a == 8); | |
8b632bab MD |
174 | if (unlikely(rduration)) |
175 | usleep(rduration); | |
1430ee0b | 176 | rcu_read_unlock(); |
1eec319e | 177 | nr_reads++; |
59d5a406 | 178 | if (unlikely(!test_duration_read())) |
cf380c2f | 179 | break; |
41718ff9 | 180 | } |
f69f195a | 181 | |
121a5d44 | 182 | rcu_unregister_thread(); |
41718ff9 | 183 | |
1eec319e | 184 | *count = nr_reads; |
cf380c2f MD |
185 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
186 | "reader", pthread_self(), (unsigned long)gettid()); | |
f69f195a MD |
187 | return ((void*)1); |
188 | ||
189 | } | |
190 | ||
1eec319e | 191 | void *thr_writer(void *_count) |
f69f195a | 192 | { |
1eec319e | 193 | unsigned long long *count = _count; |
41718ff9 | 194 | struct test_array *new, *old; |
f69f195a | 195 | |
cf380c2f | 196 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 197 | "writer", pthread_self(), (unsigned long)gettid()); |
f69f195a | 198 | |
8c86b9a1 MD |
199 | while (!test_go) |
200 | { | |
201 | } | |
7685d963 | 202 | smp_mb(); |
8c86b9a1 | 203 | |
cf380c2f | 204 | for (;;) { |
bb488185 | 205 | new = test_array_alloc(); |
c265818b | 206 | rcu_copy_mutex_lock(); |
41718ff9 | 207 | old = test_rcu_pointer; |
cf380c2f | 208 | if (old) |
41718ff9 | 209 | assert(old->a == 8); |
ad6ce6ae | 210 | new->a = 8; |
121a5d44 | 211 | old = rcu_publish_content(&test_rcu_pointer, new); |
c265818b | 212 | rcu_copy_mutex_unlock(); |
41718ff9 | 213 | /* can be done after unlock */ |
cf380c2f | 214 | if (old) |
8c8eed97 | 215 | old->a = 0; |
bb488185 | 216 | test_array_free(old); |
1eec319e | 217 | nr_writes++; |
59d5a406 | 218 | if (unlikely(!test_duration_write())) |
cf380c2f | 219 | break; |
59d5a406 | 220 | if (unlikely(wdelay)) |
8c86b9a1 | 221 | usleep(wdelay); |
f69f195a MD |
222 | } |
223 | ||
cf380c2f MD |
224 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
225 | "writer", pthread_self(), (unsigned long)gettid()); | |
1eec319e | 226 | *count = nr_writes; |
f69f195a MD |
227 | return ((void*)2); |
228 | } | |
ac260fd9 | 229 | |
1430ee0b MD |
230 | void show_usage(int argc, char **argv) |
231 | { | |
8c86b9a1 | 232 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); |
1430ee0b MD |
233 | #ifdef DEBUG_YIELD |
234 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
235 | #endif | |
7685d963 | 236 | printf(" [-d delay] (writer period (us))"); |
8b632bab | 237 | printf(" [-c duration] (reader C.S. duration (us))"); |
9e97e478 | 238 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); |
1430ee0b MD |
239 | printf("\n"); |
240 | } | |
241 | ||
9e97e478 MD |
242 | cpu_set_t affinity; |
243 | ||
cf380c2f | 244 | int main(int argc, char **argv) |
ac260fd9 | 245 | { |
f69f195a | 246 | int err; |
8c86b9a1 | 247 | pthread_t *tid_reader, *tid_writer; |
f69f195a | 248 | void *tret; |
8c86b9a1 | 249 | unsigned long long *count_reader, *count_writer; |
1eec319e | 250 | unsigned long long tot_reads = 0, tot_writes = 0; |
9e97e478 MD |
251 | int i, a; |
252 | int use_affinity = 0; | |
f69f195a | 253 | |
8c86b9a1 MD |
254 | if (argc < 4) { |
255 | show_usage(argc, argv); | |
256 | return -1; | |
257 | } | |
258 | ||
259 | err = sscanf(argv[1], "%u", &nr_readers); | |
260 | if (err != 1) { | |
1430ee0b | 261 | show_usage(argc, argv); |
cf380c2f MD |
262 | return -1; |
263 | } | |
264 | ||
8c86b9a1 MD |
265 | err = sscanf(argv[2], "%u", &nr_writers); |
266 | if (err != 1) { | |
267 | show_usage(argc, argv); | |
268 | return -1; | |
269 | } | |
270 | ||
271 | err = sscanf(argv[3], "%lu", &duration); | |
cf380c2f | 272 | if (err != 1) { |
1430ee0b | 273 | show_usage(argc, argv); |
cf380c2f MD |
274 | return -1; |
275 | } | |
276 | ||
9e97e478 MD |
277 | CPU_ZERO(&affinity); |
278 | ||
8c86b9a1 | 279 | for (i = 4; i < argc; i++) { |
cf380c2f MD |
280 | if (argv[i][0] != '-') |
281 | continue; | |
282 | switch (argv[i][1]) { | |
bb488185 | 283 | #ifdef DEBUG_YIELD |
cf380c2f MD |
284 | case 'r': |
285 | yield_active |= YIELD_READ; | |
286 | break; | |
287 | case 'w': | |
288 | yield_active |= YIELD_WRITE; | |
289 | break; | |
bb488185 | 290 | #endif |
9e97e478 MD |
291 | case 'a': |
292 | if (argc < i + 2) { | |
293 | show_usage(argc, argv); | |
294 | return -1; | |
295 | } | |
296 | a = atoi(argv[++i]); | |
297 | CPU_SET(a, &affinity); | |
298 | use_affinity = 1; | |
299 | printf("Adding CPU %d affinity\n", a); | |
300 | break; | |
8b632bab MD |
301 | case 'c': |
302 | if (argc < i + 2) { | |
303 | show_usage(argc, argv); | |
304 | return -1; | |
305 | } | |
306 | rduration = atoi(argv[++i]); | |
307 | break; | |
8c86b9a1 | 308 | case 'd': |
7685d963 | 309 | if (argc < i + 2) { |
8c86b9a1 MD |
310 | show_usage(argc, argv); |
311 | return -1; | |
312 | } | |
313 | wdelay = atoi(argv[++i]); | |
bb488185 | 314 | break; |
cf380c2f MD |
315 | } |
316 | } | |
cf380c2f | 317 | |
8c86b9a1 MD |
318 | printf("running test for %lu seconds, %u readers, %u writers.\n", |
319 | duration, nr_readers, nr_writers); | |
320 | printf("Writer delay : %u us.\n", wdelay); | |
87bd15cd BW |
321 | printf("thread %-6s, thread id : %lx, tid %lu\n", |
322 | "main", pthread_self(), (unsigned long)gettid()); | |
323 | ||
9e97e478 MD |
324 | if (use_affinity |
325 | && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) { | |
326 | perror("sched_setaffinity"); | |
327 | exit(-1); | |
328 | } | |
329 | ||
8c86b9a1 MD |
330 | test_array = malloc(sizeof(*test_array) * ARRAY_SIZE); |
331 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); | |
332 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
333 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
334 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
335 | ||
336 | for (i = 0; i < nr_readers; i++) { | |
1eec319e MD |
337 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
338 | &count_reader[i]); | |
f69f195a MD |
339 | if (err != 0) |
340 | exit(1); | |
341 | } | |
8c86b9a1 | 342 | for (i = 0; i < nr_writers; i++) { |
1eec319e MD |
343 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
344 | &count_writer[i]); | |
f69f195a MD |
345 | if (err != 0) |
346 | exit(1); | |
347 | } | |
348 | ||
7685d963 | 349 | smp_mb(); |
8c86b9a1 | 350 | |
78efb485 MD |
351 | test_go = 1; |
352 | ||
353 | sleep(duration); | |
354 | ||
355 | test_stop = 1; | |
356 | ||
8c86b9a1 | 357 | for (i = 0; i < nr_readers; i++) { |
f69f195a MD |
358 | err = pthread_join(tid_reader[i], &tret); |
359 | if (err != 0) | |
360 | exit(1); | |
1eec319e | 361 | tot_reads += count_reader[i]; |
f69f195a | 362 | } |
8c86b9a1 | 363 | for (i = 0; i < nr_writers; i++) { |
f69f195a MD |
364 | err = pthread_join(tid_writer[i], &tret); |
365 | if (err != 0) | |
366 | exit(1); | |
1eec319e | 367 | tot_writes += count_writer[i]; |
f69f195a | 368 | } |
5873cd17 | 369 | |
1eec319e MD |
370 | printf("total number of reads : %llu, writes %llu\n", tot_reads, |
371 | tot_writes); | |
bb488185 | 372 | test_array_free(test_rcu_pointer); |
8c86b9a1 MD |
373 | free(test_array); |
374 | free(tid_reader); | |
375 | free(tid_writer); | |
376 | free(count_reader); | |
377 | free(count_writer); | |
f69f195a | 378 | return 0; |
ac260fd9 | 379 | } |