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