0d8e9a4a537ddca9bbf266691f2a01b2ad477fba
[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 unsigned int 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
308 if (argc < 4) {
309 show_usage(argc, argv);
310 return -1;
311 }
312
313 err = sscanf(argv[1], "%u", &nr_readers);
314 if (err != 1) {
315 show_usage(argc, argv);
316 return -1;
317 }
318
319 err = sscanf(argv[2], "%u", &nr_writers);
320 if (err != 1) {
321 show_usage(argc, argv);
322 return -1;
323 }
324
325 err = sscanf(argv[3], "%lu", &duration);
326 if (err != 1) {
327 show_usage(argc, argv);
328 return -1;
329 }
330
331 for (i = 4; i < argc; i++) {
332 if (argv[i][0] != '-')
333 continue;
334 switch (argv[i][1]) {
335 case 'r':
336 rcu_debug_yield_enable(RCU_YIELD_READ);
337 break;
338 case 'w':
339 rcu_debug_yield_enable(RCU_YIELD_WRITE);
340 break;
341 case 'a':
342 if (argc < i + 2) {
343 show_usage(argc, argv);
344 return -1;
345 }
346 a = atoi(argv[++i]);
347 cpu_affinities[next_aff++] = a;
348 use_affinity = 1;
349 printf_verbose("Adding CPU %d affinity\n", a);
350 break;
351 case 'b':
352 if (argc < i + 2) {
353 show_usage(argc, argv);
354 return -1;
355 }
356 reclaim_batch = atol(argv[++i]);
357 break;
358 case 'c':
359 if (argc < i + 2) {
360 show_usage(argc, argv);
361 return -1;
362 }
363 rduration = atol(argv[++i]);
364 break;
365 case 'd':
366 if (argc < i + 2) {
367 show_usage(argc, argv);
368 return -1;
369 }
370 wdelay = atol(argv[++i]);
371 break;
372 case 'e':
373 if (argc < i + 2) {
374 show_usage(argc, argv);
375 return -1;
376 }
377 wduration = atol(argv[++i]);
378 break;
379 case 'v':
380 verbose_mode = 1;
381 break;
382 }
383 }
384
385 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
386 duration, nr_readers, nr_writers);
387 printf_verbose("Writer delay : %lu loops.\n", wdelay);
388 printf_verbose("Reader duration : %lu loops.\n", rduration);
389 printf_verbose("thread %-6s, tid %lu\n",
390 "main", urcu_get_thread_id());
391
392 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
393 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
394 count_reader = calloc(nr_readers, sizeof(*count_reader));
395 tot_nr_writes = calloc(nr_writers, sizeof(*tot_nr_writes));
396 pending_reclaims = calloc(nr_writers, sizeof(*pending_reclaims));
397 if (reclaim_batch * sizeof(*pending_reclaims[i].queue)
398 < CAA_CACHE_LINE_SIZE)
399 for (i = 0; i < nr_writers; i++)
400 pending_reclaims[i].queue = calloc(1, CAA_CACHE_LINE_SIZE);
401 else
402 for (i = 0; i < nr_writers; i++)
403 pending_reclaims[i].queue = calloc(reclaim_batch,
404 sizeof(*pending_reclaims[i].queue));
405 for (i = 0; i < nr_writers; i++)
406 pending_reclaims[i].head = pending_reclaims[i].queue;
407
408 next_aff = 0;
409
410 for (i = 0; i < nr_readers; i++) {
411 err = pthread_create(&tid_reader[i], NULL, thr_reader,
412 &count_reader[i]);
413 if (err != 0)
414 exit(1);
415 }
416 for (i = 0; i < nr_writers; i++) {
417 err = pthread_create(&tid_writer[i], NULL, thr_writer,
418 (void *)(long)i);
419 if (err != 0)
420 exit(1);
421 }
422
423 cmm_smp_mb();
424
425 test_go = 1;
426
427 sleep(duration);
428
429 test_stop = 1;
430
431 for (i = 0; i < nr_readers; i++) {
432 err = pthread_join(tid_reader[i], &tret);
433 if (err != 0)
434 exit(1);
435 tot_reads += count_reader[i];
436 }
437 for (i = 0; i < nr_writers; i++) {
438 err = pthread_join(tid_writer[i], &tret);
439 if (err != 0)
440 exit(1);
441 tot_writes += tot_nr_writes[i];
442 rcu_gc_clear_queue(i);
443 }
444
445 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
446 tot_writes);
447 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
448 "nr_writers %3u "
449 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
450 "batch %u\n",
451 argv[0], duration, nr_readers, rduration, wduration,
452 nr_writers, wdelay, tot_reads, tot_writes,
453 tot_reads + tot_writes, reclaim_batch);
454 free(tid_reader);
455 free(tid_writer);
456 free(count_reader);
457 free(tot_nr_writes);
458 for (i = 0; i < nr_writers; i++)
459 free(pending_reclaims[i].queue);
460 free(pending_reclaims);
461
462 return 0;
463 }
This page took 0.055246 seconds and 3 git commands to generate.