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