uatomic/x86: Remove redundant memory barriers
[urcu.git] / tests / benchmark / test_urcu_gc.c
1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 //
3 // SPDX-License-Identifier: GPL-2.0-or-later
4
5 /*
6 * Userspace RCU library - test program (with batch reclamation)
7 */
8
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>
17 #include <errno.h>
18
19 #include <urcu/arch.h>
20 #include <urcu/assert.h>
21 #include <urcu/tls-compat.h>
22 #include <urcu/uatomic.h>
23 #include "thread-id.h"
24 #include "../common/debug-yield.h"
25
26 /* hardcoded number of CPUs */
27 #define NR_CPUS 16384
28
29 #ifndef DYNAMIC_LINK_TEST
30 #define _LGPL_SOURCE
31 #endif
32 #include <urcu.h>
33
34 struct test_array {
35 int a;
36 };
37
38 static unsigned long wdelay;
39
40 static struct test_array *test_rcu_pointer;
41
42 static long reclaim_batch = 1;
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
56 /* write-side C.S. duration, in loops */
57 static unsigned long wduration;
58
59 static inline void loop_sleep(unsigned long loops)
60 {
61 while (loops-- != 0)
62 caa_cpu_relax();
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
73 static unsigned int cpu_affinities[NR_CPUS];
74 static unsigned int next_aff = 0;
75 static int use_affinity = 0;
76
77 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
78
79 static void set_affinity(void)
80 {
81 #ifdef HAVE_SCHED_SETAFFINITY
82 cpu_set_t mask;
83 int cpu, ret;
84 #endif /* HAVE_SCHED_SETAFFINITY */
85
86 if (!use_affinity)
87 return;
88
89 #ifdef HAVE_SCHED_SETAFFINITY
90 ret = pthread_mutex_lock(&affinity_mutex);
91 if (ret) {
92 perror("Error in pthread mutex lock");
93 exit(-1);
94 }
95 cpu = cpu_affinities[next_aff++];
96 ret = pthread_mutex_unlock(&affinity_mutex);
97 if (ret) {
98 perror("Error in pthread mutex unlock");
99 exit(-1);
100 }
101
102 CPU_ZERO(&mask);
103 CPU_SET(cpu, &mask);
104 sched_setaffinity(0, sizeof(mask), &mask);
105 #endif /* HAVE_SCHED_SETAFFINITY */
106 }
107
108 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
109 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
110
111 static
112 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
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
119 static
120 void *thr_reader(void *_count)
121 {
122 unsigned long long *count = _count;
123 struct test_array *local_ptr;
124
125 printf_verbose("thread_begin %s, tid %lu\n",
126 "reader", urcu_get_thread_id());
127
128 set_affinity();
129
130 rcu_register_thread();
131
132 wait_until_go();
133
134 for (;;) {
135 rcu_read_lock();
136 local_ptr = rcu_dereference(test_rcu_pointer);
137 rcu_debug_yield_read();
138 if (local_ptr)
139 urcu_posix_assert(local_ptr->a == 8);
140 if (caa_unlikely(rduration))
141 loop_sleep(rduration);
142 rcu_read_unlock();
143 URCU_TLS(nr_reads)++;
144 if (caa_unlikely(!test_duration_read()))
145 break;
146 }
147
148 rcu_unregister_thread();
149
150 *count = URCU_TLS(nr_reads);
151 printf_verbose("thread_end %s, tid %lu\n",
152 "reader", urcu_get_thread_id());
153 return ((void*)1);
154
155 }
156
157 static void rcu_gc_clear_queue(unsigned long wtidx)
158 {
159 void **p;
160
161 /* Wait for Q.S and empty queue */
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
174 /* Using per-thread queue */
175 static void rcu_gc_reclaim(unsigned long wtidx, void *old)
176 {
177 /* Queue pointer */
178 *pending_reclaims[wtidx].head = old;
179 pending_reclaims[wtidx].head++;
180
181 if (caa_likely(pending_reclaims[wtidx].head - pending_reclaims[wtidx].queue
182 < reclaim_batch))
183 return;
184
185 rcu_gc_clear_queue(wtidx);
186 }
187
188 static
189 void *thr_writer(void *data)
190 {
191 unsigned long wtidx = (unsigned long)data;
192 #ifdef TEST_LOCAL_GC
193 struct test_array *old = NULL;
194 #else
195 struct test_array *new, *old;
196 #endif
197
198 printf_verbose("thread_begin %s, tid %lu\n",
199 "writer", urcu_get_thread_id());
200
201 set_affinity();
202
203 wait_until_go();
204
205 for (;;) {
206 #ifndef TEST_LOCAL_GC
207 new = malloc(sizeof(*new));
208 new->a = 8;
209 old = rcu_xchg_pointer(&test_rcu_pointer, new);
210 #endif
211 if (caa_unlikely(wduration))
212 loop_sleep(wduration);
213 rcu_gc_reclaim(wtidx, old);
214 URCU_TLS(nr_writes)++;
215 if (caa_unlikely(!test_duration_write()))
216 break;
217 if (caa_unlikely(wdelay))
218 loop_sleep(wdelay);
219 }
220
221 printf_verbose("thread_end %s, tid %lu\n",
222 "writer", urcu_get_thread_id());
223 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
224 return ((void*)2);
225 }
226
227 static
228 void show_usage(char **argv)
229 {
230 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
231 argv[0]);
232 printf("OPTIONS:\n");
233 printf(" [-r] [-w] (yield reader and/or writer)\n");
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");
239 printf("\n");
240 }
241
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;
250 unsigned int i_thr;
251
252 if (argc < 4) {
253 show_usage(argv);
254 return -1;
255 }
256
257 err = sscanf(argv[1], "%u", &nr_readers);
258 if (err != 1) {
259 show_usage(argv);
260 return -1;
261 }
262
263 err = sscanf(argv[2], "%u", &nr_writers);
264 if (err != 1) {
265 show_usage(argv);
266 return -1;
267 }
268
269 err = sscanf(argv[3], "%lu", &duration);
270 if (err != 1) {
271 show_usage(argv);
272 return -1;
273 }
274
275 for (i = 4; i < argc; i++) {
276 if (argv[i][0] != '-')
277 continue;
278 switch (argv[i][1]) {
279 case 'r':
280 rcu_debug_yield_enable(RCU_YIELD_READ);
281 break;
282 case 'w':
283 rcu_debug_yield_enable(RCU_YIELD_WRITE);
284 break;
285 case 'a':
286 if (argc < i + 2) {
287 show_usage(argv);
288 return -1;
289 }
290 a = atoi(argv[++i]);
291 cpu_affinities[next_aff++] = a;
292 use_affinity = 1;
293 printf_verbose("Adding CPU %d affinity\n", a);
294 break;
295 case 'b':
296 if (argc < i + 2) {
297 show_usage(argv);
298 return -1;
299 }
300 reclaim_batch = atol(argv[++i]);
301 break;
302 case 'c':
303 if (argc < i + 2) {
304 show_usage(argv);
305 return -1;
306 }
307 rduration = atol(argv[++i]);
308 break;
309 case 'd':
310 if (argc < i + 2) {
311 show_usage(argv);
312 return -1;
313 }
314 wdelay = atol(argv[++i]);
315 break;
316 case 'e':
317 if (argc < i + 2) {
318 show_usage(argv);
319 return -1;
320 }
321 wduration = atol(argv[++i]);
322 break;
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);
333 printf_verbose("thread %-6s, tid %lu\n",
334 "main", urcu_get_thread_id());
335
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));
341
342 if (reclaim_batch * sizeof(*pending_reclaims[0].queue)
343 < CAA_CACHE_LINE_SIZE)
344 for (i_thr = 0; i_thr < nr_writers; i_thr++)
345 pending_reclaims[i_thr].queue = calloc(1, CAA_CACHE_LINE_SIZE);
346 else
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;
352
353 next_aff = 0;
354
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]);
358 if (err != 0)
359 exit(1);
360 }
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);
364 if (err != 0)
365 exit(1);
366 }
367
368 test_for(duration);
369
370 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
371 err = pthread_join(tid_reader[i_thr], &tret);
372 if (err != 0)
373 exit(1);
374 tot_reads += count_reader[i_thr];
375 }
376 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
377 err = pthread_join(tid_writer[i_thr], &tret);
378 if (err != 0)
379 exit(1);
380 tot_writes += tot_nr_writes[i_thr];
381 rcu_gc_clear_queue(i_thr);
382 }
383
384 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
385 tot_writes);
386 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
387 "nr_writers %3u "
388 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
389 "batch %ld\n",
390 argv[0], duration, nr_readers, rduration, wduration,
391 nr_writers, wdelay, tot_reads, tot_writes,
392 tot_reads + tot_writes, reclaim_batch);
393
394 free(tid_reader);
395 free(tid_writer);
396 free(count_reader);
397 free(tot_nr_writes);
398
399 for (i_thr = 0; i_thr < nr_writers; i_thr++)
400 free(pending_reclaims[i_thr].queue);
401 free(pending_reclaims);
402
403 return 0;
404 }
This page took 0.036887 seconds and 5 git commands to generate.