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