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