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