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