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