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