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