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