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