Commit | Line | Data |
---|---|---|
b257a10b MD |
1 | /* |
2 | * test_urcu.c | |
3 | * | |
4 | * Userspace RCU library - test program | |
5 | * | |
6982d6d7 | 6 | * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
b257a10b | 7 | * |
af02d47e MD |
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. | |
b257a10b MD |
21 | */ |
22 | ||
ac260fd9 | 23 | #include <stdio.h> |
f69f195a MD |
24 | #include <pthread.h> |
25 | #include <stdlib.h> | |
41718ff9 | 26 | #include <string.h> |
f69f195a MD |
27 | #include <sys/types.h> |
28 | #include <sys/wait.h> | |
29 | #include <unistd.h> | |
30 | #include <stdio.h> | |
41718ff9 | 31 | #include <assert.h> |
94b343fd | 32 | #include <errno.h> |
87bd15cd | 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 | |
2a7ac59d MD |
40 | /* hardcoded number of CPUs */ |
41 | #define NR_CPUS 16384 | |
42 | ||
121a5d44 MD |
43 | #ifndef DYNAMIC_LINK_TEST |
44 | #define _LGPL_SOURCE | |
121a5d44 | 45 | #endif |
ec4e58a3 | 46 | #include <urcu.h> |
ac260fd9 | 47 | |
78efb485 | 48 | static volatile int test_go, test_stop; |
8c86b9a1 | 49 | |
9e31d0f0 | 50 | static unsigned long wdelay; |
bb488185 | 51 | |
bcc74df3 | 52 | static int *test_rcu_pointer; |
41718ff9 | 53 | |
cf380c2f | 54 | static unsigned long duration; |
cf380c2f | 55 | |
daddf5b0 | 56 | /* read-side C.S. duration, in loops */ |
8b632bab MD |
57 | static unsigned long rduration; |
58 | ||
31b598e0 MD |
59 | /* write-side C.S. duration, in loops */ |
60 | static unsigned long wduration; | |
61 | ||
ab0aacbe | 62 | static inline void loop_sleep(unsigned long loops) |
daddf5b0 | 63 | { |
ab0aacbe | 64 | while (loops-- != 0) |
06f22bdb | 65 | caa_cpu_relax(); |
daddf5b0 MD |
66 | } |
67 | ||
4bad7d45 MD |
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 | ||
2a7ac59d MD |
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 | ||
82 | static void set_affinity(void) | |
83 | { | |
95bc7fb9 | 84 | #if HAVE_SCHED_SETAFFINITY |
2a7ac59d | 85 | cpu_set_t mask; |
95bc7fb9 MD |
86 | int cpu, ret; |
87 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
2a7ac59d MD |
88 | |
89 | if (!use_affinity) | |
90 | return; | |
91 | ||
d8540fc5 | 92 | #if HAVE_SCHED_SETAFFINITY |
2a7ac59d 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 | } | |
d8540fc5 | 104 | |
2a7ac59d MD |
105 | CPU_ZERO(&mask); |
106 | CPU_SET(cpu, &mask); | |
d8540fc5 PA |
107 | #if SCHED_SETAFFINITY_ARGS == 2 |
108 | sched_setaffinity(0, &mask); | |
109 | #else | |
2a7ac59d | 110 | sched_setaffinity(0, sizeof(mask), &mask); |
d8540fc5 PA |
111 | #endif |
112 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
2a7ac59d MD |
113 | } |
114 | ||
cf380c2f MD |
115 | /* |
116 | * returns 0 if test should end. | |
117 | */ | |
5873cd17 | 118 | static int test_duration_write(void) |
cf380c2f | 119 | { |
78efb485 | 120 | return !test_stop; |
5873cd17 MD |
121 | } |
122 | ||
123 | static int test_duration_read(void) | |
124 | { | |
78efb485 | 125 | return !test_stop; |
cf380c2f MD |
126 | } |
127 | ||
bd252a04 MD |
128 | static DEFINE_URCU_TLS(unsigned long long, nr_writes); |
129 | static DEFINE_URCU_TLS(unsigned long long, nr_reads); | |
1eec319e | 130 | |
8c86b9a1 MD |
131 | static unsigned int nr_readers; |
132 | static unsigned int nr_writers; | |
133 | ||
c265818b MD |
134 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
135 | ||
3b4d3084 | 136 | static |
1eec319e | 137 | void *thr_reader(void *_count) |
f69f195a | 138 | { |
1eec319e | 139 | unsigned long long *count = _count; |
bcc74df3 | 140 | int *local_ptr; |
41718ff9 | 141 | |
94df6318 MD |
142 | printf_verbose("thread_begin %s, tid %lu\n", |
143 | "reader", urcu_get_thread_id()); | |
f69f195a | 144 | |
2a7ac59d MD |
145 | set_affinity(); |
146 | ||
121a5d44 | 147 | rcu_register_thread(); |
882f3357 | 148 | assert(!rcu_read_ongoing()); |
f69f195a | 149 | |
8c86b9a1 MD |
150 | while (!test_go) |
151 | { | |
152 | } | |
5481ddb3 | 153 | cmm_smp_mb(); |
8c86b9a1 | 154 | |
cf380c2f | 155 | for (;;) { |
1430ee0b | 156 | rcu_read_lock(); |
882f3357 | 157 | assert(rcu_read_ongoing()); |
cf380c2f | 158 | local_ptr = rcu_dereference(test_rcu_pointer); |
1de4df4b | 159 | rcu_debug_yield_read(); |
cf380c2f | 160 | if (local_ptr) |
bcc74df3 | 161 | assert(*local_ptr == 8); |
a0b7f7ea | 162 | if (caa_unlikely(rduration)) |
daddf5b0 | 163 | loop_sleep(rduration); |
1430ee0b | 164 | rcu_read_unlock(); |
bd252a04 | 165 | URCU_TLS(nr_reads)++; |
a0b7f7ea | 166 | if (caa_unlikely(!test_duration_read())) |
cf380c2f | 167 | break; |
41718ff9 | 168 | } |
f69f195a | 169 | |
121a5d44 | 170 | rcu_unregister_thread(); |
41718ff9 | 171 | |
2b514ae2 MD |
172 | /* test extra thread registration */ |
173 | rcu_register_thread(); | |
174 | rcu_unregister_thread(); | |
175 | ||
bd252a04 | 176 | *count = URCU_TLS(nr_reads); |
94df6318 MD |
177 | printf_verbose("thread_end %s, tid %lu\n", |
178 | "reader", urcu_get_thread_id()); | |
f69f195a MD |
179 | return ((void*)1); |
180 | ||
181 | } | |
182 | ||
3b4d3084 | 183 | static |
1eec319e | 184 | void *thr_writer(void *_count) |
f69f195a | 185 | { |
1eec319e | 186 | unsigned long long *count = _count; |
bcc74df3 | 187 | int *new, *old; |
f69f195a | 188 | |
94df6318 MD |
189 | printf_verbose("thread_begin %s, tid %lu\n", |
190 | "writer", urcu_get_thread_id()); | |
f69f195a | 191 | |
2a7ac59d MD |
192 | set_affinity(); |
193 | ||
8c86b9a1 MD |
194 | while (!test_go) |
195 | { | |
196 | } | |
5481ddb3 | 197 | cmm_smp_mb(); |
8c86b9a1 | 198 | |
cf380c2f | 199 | for (;;) { |
bcc74df3 MD |
200 | new = malloc(sizeof(int)); |
201 | assert(new); | |
202 | *new = 8; | |
d2835e6f | 203 | old = rcu_xchg_pointer(&test_rcu_pointer, new); |
a0b7f7ea | 204 | if (caa_unlikely(wduration)) |
31b598e0 | 205 | loop_sleep(wduration); |
d2835e6f | 206 | synchronize_rcu(); |
cf380c2f | 207 | if (old) |
bcc74df3 MD |
208 | *old = 0; |
209 | free(old); | |
bd252a04 | 210 | URCU_TLS(nr_writes)++; |
a0b7f7ea | 211 | if (caa_unlikely(!test_duration_write())) |
cf380c2f | 212 | break; |
a0b7f7ea | 213 | if (caa_unlikely(wdelay)) |
9e31d0f0 | 214 | loop_sleep(wdelay); |
f69f195a MD |
215 | } |
216 | ||
94df6318 MD |
217 | printf_verbose("thread_end %s, tid %lu\n", |
218 | "writer", urcu_get_thread_id()); | |
bd252a04 | 219 | *count = URCU_TLS(nr_writes); |
f69f195a MD |
220 | return ((void*)2); |
221 | } | |
ac260fd9 | 222 | |
3b4d3084 | 223 | static |
5eedd745 | 224 | void show_usage(char **argv) |
1430ee0b | 225 | { |
06637138 MD |
226 | printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n", |
227 | argv[0]); | |
228 | printf("OPTIONS:\n"); | |
06637138 | 229 | printf(" [-r] [-w] (yield reader and/or writer)\n"); |
06637138 MD |
230 | printf(" [-d delay] (writer period (us))\n"); |
231 | printf(" [-c duration] (reader C.S. duration (in loops))\n"); | |
232 | printf(" [-e duration] (writer C.S. duration (in loops))\n"); | |
233 | printf(" [-v] (verbose output)\n"); | |
234 | printf(" [-a cpu#] [-a cpu#]... (affinity)\n"); | |
1430ee0b MD |
235 | printf("\n"); |
236 | } | |
237 | ||
cf380c2f | 238 | int main(int argc, char **argv) |
ac260fd9 | 239 | { |
f69f195a | 240 | int err; |
8c86b9a1 | 241 | pthread_t *tid_reader, *tid_writer; |
f69f195a | 242 | void *tret; |
8c86b9a1 | 243 | unsigned long long *count_reader, *count_writer; |
1eec319e | 244 | unsigned long long tot_reads = 0, tot_writes = 0; |
9e97e478 | 245 | int i, a; |
83e334d0 | 246 | unsigned int i_thr; |
f69f195a | 247 | |
8c86b9a1 | 248 | if (argc < 4) { |
5eedd745 | 249 | show_usage(argv); |
8c86b9a1 MD |
250 | return -1; |
251 | } | |
252 | ||
253 | err = sscanf(argv[1], "%u", &nr_readers); | |
254 | if (err != 1) { | |
5eedd745 | 255 | show_usage(argv); |
cf380c2f MD |
256 | return -1; |
257 | } | |
258 | ||
8c86b9a1 MD |
259 | err = sscanf(argv[2], "%u", &nr_writers); |
260 | if (err != 1) { | |
5eedd745 | 261 | show_usage(argv); |
8c86b9a1 MD |
262 | return -1; |
263 | } | |
83e334d0 | 264 | |
8c86b9a1 | 265 | err = sscanf(argv[3], "%lu", &duration); |
cf380c2f | 266 | if (err != 1) { |
5eedd745 | 267 | show_usage(argv); |
cf380c2f MD |
268 | return -1; |
269 | } | |
270 | ||
8c86b9a1 | 271 | for (i = 4; i < argc; i++) { |
cf380c2f MD |
272 | if (argv[i][0] != '-') |
273 | continue; | |
274 | switch (argv[i][1]) { | |
275 | case 'r': | |
2650042a | 276 | rcu_debug_yield_enable(RCU_YIELD_READ); |
cf380c2f MD |
277 | break; |
278 | case 'w': | |
2650042a | 279 | rcu_debug_yield_enable(RCU_YIELD_WRITE); |
cf380c2f | 280 | break; |
9e97e478 MD |
281 | case 'a': |
282 | if (argc < i + 2) { | |
5eedd745 | 283 | show_usage(argv); |
9e97e478 MD |
284 | return -1; |
285 | } | |
286 | a = atoi(argv[++i]); | |
2a7ac59d | 287 | cpu_affinities[next_aff++] = a; |
9e97e478 | 288 | use_affinity = 1; |
4bad7d45 | 289 | printf_verbose("Adding CPU %d affinity\n", a); |
9e97e478 | 290 | break; |
8b632bab MD |
291 | case 'c': |
292 | if (argc < i + 2) { | |
5eedd745 | 293 | show_usage(argv); |
8b632bab MD |
294 | return -1; |
295 | } | |
9e31d0f0 | 296 | rduration = atol(argv[++i]); |
8b632bab | 297 | break; |
8c86b9a1 | 298 | case 'd': |
7685d963 | 299 | if (argc < i + 2) { |
5eedd745 | 300 | show_usage(argv); |
8c86b9a1 MD |
301 | return -1; |
302 | } | |
9e31d0f0 | 303 | wdelay = atol(argv[++i]); |
bb488185 | 304 | break; |
31b598e0 MD |
305 | case 'e': |
306 | if (argc < i + 2) { | |
5eedd745 | 307 | show_usage(argv); |
31b598e0 MD |
308 | return -1; |
309 | } | |
310 | wduration = atol(argv[++i]); | |
311 | break; | |
4bad7d45 MD |
312 | case 'v': |
313 | verbose_mode = 1; | |
314 | break; | |
cf380c2f MD |
315 | } |
316 | } | |
cf380c2f | 317 | |
4bad7d45 | 318 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", |
8c86b9a1 | 319 | duration, nr_readers, nr_writers); |
9e31d0f0 | 320 | printf_verbose("Writer delay : %lu loops.\n", wdelay); |
4bad7d45 | 321 | printf_verbose("Reader duration : %lu loops.\n", rduration); |
94df6318 MD |
322 | printf_verbose("thread %-6s, tid %lu\n", |
323 | "main", urcu_get_thread_id()); | |
87bd15cd | 324 | |
9aa14175 MD |
325 | tid_reader = calloc(nr_readers, sizeof(*tid_reader)); |
326 | tid_writer = calloc(nr_writers, sizeof(*tid_writer)); | |
327 | count_reader = calloc(nr_readers, sizeof(*count_reader)); | |
328 | count_writer = calloc(nr_writers, sizeof(*count_writer)); | |
8c86b9a1 | 329 | |
2a7ac59d MD |
330 | next_aff = 0; |
331 | ||
83e334d0 MJ |
332 | for (i_thr = 0; i_thr < nr_readers; i_thr++) { |
333 | err = pthread_create(&tid_reader[i_thr], NULL, thr_reader, | |
334 | &count_reader[i_thr]); | |
f69f195a MD |
335 | if (err != 0) |
336 | exit(1); | |
337 | } | |
83e334d0 MJ |
338 | for (i_thr = 0; i_thr < nr_writers; i_thr++) { |
339 | err = pthread_create(&tid_writer[i_thr], NULL, thr_writer, | |
340 | &count_writer[i_thr]); | |
f69f195a MD |
341 | if (err != 0) |
342 | exit(1); | |
343 | } | |
344 | ||
5481ddb3 | 345 | cmm_smp_mb(); |
8c86b9a1 | 346 | |
78efb485 MD |
347 | test_go = 1; |
348 | ||
349 | sleep(duration); | |
350 | ||
351 | test_stop = 1; | |
352 | ||
83e334d0 MJ |
353 | for (i_thr = 0; i_thr < nr_readers; i_thr++) { |
354 | err = pthread_join(tid_reader[i_thr], &tret); | |
f69f195a MD |
355 | if (err != 0) |
356 | exit(1); | |
83e334d0 | 357 | tot_reads += count_reader[i_thr]; |
f69f195a | 358 | } |
83e334d0 MJ |
359 | for (i_thr = 0; i_thr < nr_writers; i_thr++) { |
360 | err = pthread_join(tid_writer[i_thr], &tret); | |
f69f195a MD |
361 | if (err != 0) |
362 | exit(1); | |
83e334d0 | 363 | tot_writes += count_writer[i_thr]; |
f69f195a | 364 | } |
83e334d0 | 365 | |
4bad7d45 | 366 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, |
1eec319e | 367 | tot_writes); |
a50a7b43 | 368 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu " |
cda3c71d | 369 | "nr_writers %3u " |
9e31d0f0 | 370 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", |
a50a7b43 | 371 | argv[0], duration, nr_readers, rduration, wduration, |
4bad7d45 MD |
372 | nr_writers, wdelay, tot_reads, tot_writes, |
373 | tot_reads + tot_writes); | |
bcc74df3 | 374 | free(test_rcu_pointer); |
8c86b9a1 MD |
375 | free(tid_reader); |
376 | free(tid_writer); | |
377 | free(count_reader); | |
378 | free(count_writer); | |
f69f195a | 379 | return 0; |
ac260fd9 | 380 | } |