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