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