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