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