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