Cleanup: missing sign compare fixes
[urcu.git] / tests / benchmark / test_urcu_gc.c
1 /*
2 * test_urcu_gc.c
3 *
4 * Userspace RCU library - test program (with batch reclamation)
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
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>
32 #include <errno.h>
33
34 #include <urcu/arch.h>
35 #include <urcu/tls-compat.h>
36 #include "cpuset.h"
37 #include "thread-id.h"
38 #include "../common/debug-yield.h"
39
40 /* hardcoded number of CPUs */
41 #define NR_CPUS 16384
42
43 #ifndef DYNAMIC_LINK_TEST
44 #define _LGPL_SOURCE
45 #endif
46 #include <urcu.h>
47
48 struct test_array {
49 int a;
50 };
51
52 static volatile int test_go, test_stop;
53
54 static unsigned long wdelay;
55
56 static struct test_array *test_rcu_pointer;
57
58 static long reclaim_batch = 1;
59
60 struct reclaim_queue {
61 void **queue; /* Beginning of queue */
62 void **head; /* Insert position */
63 };
64
65 static struct reclaim_queue *pending_reclaims;
66
67 static unsigned long duration;
68
69 /* read-side C.S. duration, in loops */
70 static unsigned long rduration;
71
72 /* write-side C.S. duration, in loops */
73 static unsigned long wduration;
74
75 static inline void loop_sleep(unsigned long loops)
76 {
77 while (loops-- != 0)
78 caa_cpu_relax();
79 }
80
81 static int verbose_mode;
82
83 #define printf_verbose(fmt, args...) \
84 do { \
85 if (verbose_mode) \
86 printf(fmt, args); \
87 } while (0)
88
89 static unsigned int cpu_affinities[NR_CPUS];
90 static unsigned int next_aff = 0;
91 static int use_affinity = 0;
92
93 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
94
95 static void set_affinity(void)
96 {
97 #if HAVE_SCHED_SETAFFINITY
98 cpu_set_t mask;
99 int cpu, ret;
100 #endif /* HAVE_SCHED_SETAFFINITY */
101
102 if (!use_affinity)
103 return;
104
105 #if HAVE_SCHED_SETAFFINITY
106 ret = pthread_mutex_lock(&affinity_mutex);
107 if (ret) {
108 perror("Error in pthread mutex lock");
109 exit(-1);
110 }
111 cpu = cpu_affinities[next_aff++];
112 ret = pthread_mutex_unlock(&affinity_mutex);
113 if (ret) {
114 perror("Error in pthread mutex unlock");
115 exit(-1);
116 }
117
118 CPU_ZERO(&mask);
119 CPU_SET(cpu, &mask);
120 #if SCHED_SETAFFINITY_ARGS == 2
121 sched_setaffinity(0, &mask);
122 #else
123 sched_setaffinity(0, sizeof(mask), &mask);
124 #endif
125 #endif /* HAVE_SCHED_SETAFFINITY */
126 }
127
128 /*
129 * returns 0 if test should end.
130 */
131 static int test_duration_write(void)
132 {
133 return !test_stop;
134 }
135
136 static int test_duration_read(void)
137 {
138 return !test_stop;
139 }
140
141 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
142 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
143
144 static
145 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
146
147 static unsigned int nr_readers;
148 static unsigned int nr_writers;
149
150 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
151
152 void 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
162 void 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
173 void *thr_reader(void *_count)
174 {
175 unsigned long long *count = _count;
176 struct test_array *local_ptr;
177
178 printf_verbose("thread_begin %s, tid %lu\n",
179 "reader", urcu_get_thread_id());
180
181 set_affinity();
182
183 rcu_register_thread();
184
185 while (!test_go)
186 {
187 }
188 cmm_smp_mb();
189
190 for (;;) {
191 rcu_read_lock();
192 local_ptr = rcu_dereference(test_rcu_pointer);
193 rcu_debug_yield_read();
194 if (local_ptr)
195 assert(local_ptr->a == 8);
196 if (caa_unlikely(rduration))
197 loop_sleep(rduration);
198 rcu_read_unlock();
199 URCU_TLS(nr_reads)++;
200 if (caa_unlikely(!test_duration_read()))
201 break;
202 }
203
204 rcu_unregister_thread();
205
206 *count = URCU_TLS(nr_reads);
207 printf_verbose("thread_end %s, tid %lu\n",
208 "reader", urcu_get_thread_id());
209 return ((void*)1);
210
211 }
212
213 static void rcu_gc_clear_queue(unsigned long wtidx)
214 {
215 void **p;
216
217 /* Wait for Q.S and empty queue */
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
230 /* Using per-thread queue */
231 static void rcu_gc_reclaim(unsigned long wtidx, void *old)
232 {
233 /* Queue pointer */
234 *pending_reclaims[wtidx].head = old;
235 pending_reclaims[wtidx].head++;
236
237 if (caa_likely(pending_reclaims[wtidx].head - pending_reclaims[wtidx].queue
238 < reclaim_batch))
239 return;
240
241 rcu_gc_clear_queue(wtidx);
242 }
243
244 void *thr_writer(void *data)
245 {
246 unsigned long wtidx = (unsigned long)data;
247 #ifdef TEST_LOCAL_GC
248 struct test_array *old = NULL;
249 #else
250 struct test_array *new, *old;
251 #endif
252
253 printf_verbose("thread_begin %s, tid %lu\n",
254 "writer", urcu_get_thread_id());
255
256 set_affinity();
257
258 while (!test_go)
259 {
260 }
261 cmm_smp_mb();
262
263 for (;;) {
264 #ifndef TEST_LOCAL_GC
265 new = malloc(sizeof(*new));
266 new->a = 8;
267 old = rcu_xchg_pointer(&test_rcu_pointer, new);
268 #endif
269 if (caa_unlikely(wduration))
270 loop_sleep(wduration);
271 rcu_gc_reclaim(wtidx, old);
272 URCU_TLS(nr_writes)++;
273 if (caa_unlikely(!test_duration_write()))
274 break;
275 if (caa_unlikely(wdelay))
276 loop_sleep(wdelay);
277 }
278
279 printf_verbose("thread_end %s, tid %lu\n",
280 "writer", urcu_get_thread_id());
281 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
282 return ((void*)2);
283 }
284
285 void show_usage(int argc, char **argv)
286 {
287 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
288 argv[0]);
289 printf("OPTIONS:\n");
290 printf(" [-r] [-w] (yield reader and/or writer)\n");
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");
296 printf("\n");
297 }
298
299 int 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;
307 unsigned int i_thr;
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 }
325
326 err = sscanf(argv[3], "%lu", &duration);
327 if (err != 1) {
328 show_usage(argc, argv);
329 return -1;
330 }
331
332 for (i = 4; i < argc; i++) {
333 if (argv[i][0] != '-')
334 continue;
335 switch (argv[i][1]) {
336 case 'r':
337 rcu_debug_yield_enable(RCU_YIELD_READ);
338 break;
339 case 'w':
340 rcu_debug_yield_enable(RCU_YIELD_WRITE);
341 break;
342 case 'a':
343 if (argc < i + 2) {
344 show_usage(argc, argv);
345 return -1;
346 }
347 a = atoi(argv[++i]);
348 cpu_affinities[next_aff++] = a;
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;
373 case 'e':
374 if (argc < i + 2) {
375 show_usage(argc, argv);
376 return -1;
377 }
378 wduration = atol(argv[++i]);
379 break;
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);
390 printf_verbose("thread %-6s, tid %lu\n",
391 "main", urcu_get_thread_id());
392
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));
398
399 if (reclaim_batch * sizeof(*pending_reclaims[0].queue)
400 < CAA_CACHE_LINE_SIZE)
401 for (i_thr = 0; i_thr < nr_writers; i_thr++)
402 pending_reclaims[i_thr].queue = calloc(1, CAA_CACHE_LINE_SIZE);
403 else
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;
409
410 next_aff = 0;
411
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]);
415 if (err != 0)
416 exit(1);
417 }
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);
421 if (err != 0)
422 exit(1);
423 }
424
425 cmm_smp_mb();
426
427 test_go = 1;
428
429 sleep(duration);
430
431 test_stop = 1;
432
433 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
434 err = pthread_join(tid_reader[i_thr], &tret);
435 if (err != 0)
436 exit(1);
437 tot_reads += count_reader[i_thr];
438 }
439 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
440 err = pthread_join(tid_writer[i_thr], &tret);
441 if (err != 0)
442 exit(1);
443 tot_writes += tot_nr_writes[i_thr];
444 rcu_gc_clear_queue(i_thr);
445 }
446
447 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
448 tot_writes);
449 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
450 "nr_writers %3u "
451 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
452 "batch %ld\n",
453 argv[0], duration, nr_readers, rduration, wduration,
454 nr_writers, wdelay, tot_reads, tot_writes,
455 tot_reads + tot_writes, reclaim_batch);
456
457 free(tid_reader);
458 free(tid_writer);
459 free(count_reader);
460 free(tot_nr_writes);
461
462 for (i_thr = 0; i_thr < nr_writers; i_thr++)
463 free(pending_reclaims[i_thr].queue);
464 free(pending_reclaims);
465
466 return 0;
467 }
This page took 0.049593 seconds and 4 git commands to generate.