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