Fix tests: use of uninitialized variables
[userspace-rcu.git] / tests / test_urcu_wfq.c
1 /*
2 * test_urcu_wfq.c
3 *
4 * Userspace RCU library - example RCU-based lock-free queue
5 *
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #define _GNU_SOURCE
25 #include "../config.h"
26 #include <stdio.h>
27 #include <pthread.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <assert.h>
37 #include <errno.h>
38
39 #include <urcu/arch.h>
40 #include <urcu/tls-compat.h>
41 #include "cpuset.h"
42
43 #ifdef __linux__
44 #include <syscall.h>
45 #endif
46
47 /* hardcoded number of CPUs */
48 #define NR_CPUS 16384
49
50 #if defined(_syscall0)
51 _syscall0(pid_t, gettid)
52 #elif defined(__NR_gettid)
53 static inline pid_t gettid(void)
54 {
55 return syscall(__NR_gettid);
56 }
57 #else
58 #warning "use pid as tid"
59 static inline pid_t gettid(void)
60 {
61 return getpid();
62 }
63 #endif
64
65 #ifndef DYNAMIC_LINK_TEST
66 #define _LGPL_SOURCE
67 #endif
68 #include <urcu.h>
69 #include <urcu/wfqueue.h>
70
71 static volatile int test_go, test_stop;
72
73 static unsigned long rduration;
74
75 static unsigned long duration;
76
77 /* read-side C.S. duration, in loops */
78 static unsigned long wdelay;
79
80 static inline void loop_sleep(unsigned long l)
81 {
82 while(l-- != 0)
83 caa_cpu_relax();
84 }
85
86 static int verbose_mode;
87
88 #define printf_verbose(fmt, args...) \
89 do { \
90 if (verbose_mode) \
91 printf(fmt, args); \
92 } while (0)
93
94 static unsigned int cpu_affinities[NR_CPUS];
95 static unsigned int next_aff = 0;
96 static int use_affinity = 0;
97
98 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
99
100 static 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 #if HAVE_SCHED_SETAFFINITY
110 ret = pthread_mutex_lock(&affinity_mutex);
111 if (ret) {
112 perror("Error in pthread mutex lock");
113 exit(-1);
114 }
115 cpu = cpu_affinities[next_aff++];
116 ret = pthread_mutex_unlock(&affinity_mutex);
117 if (ret) {
118 perror("Error in pthread mutex unlock");
119 exit(-1);
120 }
121
122 CPU_ZERO(&mask);
123 CPU_SET(cpu, &mask);
124 #if SCHED_SETAFFINITY_ARGS == 2
125 sched_setaffinity(0, &mask);
126 #else
127 sched_setaffinity(0, sizeof(mask), &mask);
128 #endif
129 #endif /* HAVE_SCHED_SETAFFINITY */
130 }
131
132 /*
133 * returns 0 if test should end.
134 */
135 static int test_duration_dequeue(void)
136 {
137 return !test_stop;
138 }
139
140 static int test_duration_enqueue(void)
141 {
142 return !test_stop;
143 }
144
145 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
146 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
147
148 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
149 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
150
151 static unsigned int nr_enqueuers;
152 static unsigned int nr_dequeuers;
153
154 static struct cds_wfq_queue q;
155
156 void *thr_enqueuer(void *_count)
157 {
158 unsigned long long *count = _count;
159
160 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
161 "enqueuer", (unsigned long) pthread_self(),
162 (unsigned long) gettid());
163
164 set_affinity();
165
166 while (!test_go)
167 {
168 }
169 cmm_smp_mb();
170
171 for (;;) {
172 struct cds_wfq_node *node = malloc(sizeof(*node));
173 if (!node)
174 goto fail;
175 cds_wfq_node_init(node);
176 cds_wfq_enqueue(&q, node);
177 URCU_TLS(nr_successful_enqueues)++;
178
179 if (caa_unlikely(wdelay))
180 loop_sleep(wdelay);
181 fail:
182 URCU_TLS(nr_enqueues)++;
183 if (caa_unlikely(!test_duration_enqueue()))
184 break;
185 }
186
187 count[0] = URCU_TLS(nr_enqueues);
188 count[1] = URCU_TLS(nr_successful_enqueues);
189 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
190 "enqueues %llu successful_enqueues %llu\n",
191 (unsigned long) pthread_self(),
192 (unsigned long) gettid(),
193 URCU_TLS(nr_enqueues), URCU_TLS(nr_successful_enqueues));
194 return ((void*)1);
195
196 }
197
198 void *thr_dequeuer(void *_count)
199 {
200 unsigned long long *count = _count;
201
202 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
203 "dequeuer", (unsigned long) pthread_self(),
204 (unsigned long) gettid());
205
206 set_affinity();
207
208 while (!test_go)
209 {
210 }
211 cmm_smp_mb();
212
213 for (;;) {
214 struct cds_wfq_node *node = cds_wfq_dequeue_blocking(&q);
215
216 if (node) {
217 free(node);
218 URCU_TLS(nr_successful_dequeues)++;
219 }
220
221 URCU_TLS(nr_dequeues)++;
222 if (caa_unlikely(!test_duration_dequeue()))
223 break;
224 if (caa_unlikely(rduration))
225 loop_sleep(rduration);
226 }
227
228 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
229 "dequeues %llu, successful_dequeues %llu\n",
230 (unsigned long) pthread_self(),
231 (unsigned long) gettid(),
232 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues));
233 count[0] = URCU_TLS(nr_dequeues);
234 count[1] = URCU_TLS(nr_successful_dequeues);
235 return ((void*)2);
236 }
237
238 void test_end(struct cds_wfq_queue *q, unsigned long long *nr_dequeues)
239 {
240 struct cds_wfq_node *node;
241
242 do {
243 node = cds_wfq_dequeue_blocking(q);
244 if (node) {
245 free(node);
246 (*nr_dequeues)++;
247 }
248 } while (node);
249 }
250
251 void show_usage(int argc, char **argv)
252 {
253 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
254 printf(" [-d delay] (enqueuer period (in loops))");
255 printf(" [-c duration] (dequeuer period (in loops))");
256 printf(" [-v] (verbose output)");
257 printf(" [-a cpu#] [-a cpu#]... (affinity)");
258 printf("\n");
259 }
260
261 int main(int argc, char **argv)
262 {
263 int err;
264 pthread_t *tid_enqueuer, *tid_dequeuer;
265 void *tret;
266 unsigned long long *count_enqueuer, *count_dequeuer;
267 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
268 unsigned long long tot_successful_enqueues = 0,
269 tot_successful_dequeues = 0;
270 unsigned long long end_dequeues = 0;
271 int i, a;
272
273 if (argc < 4) {
274 show_usage(argc, argv);
275 return -1;
276 }
277
278 err = sscanf(argv[1], "%u", &nr_dequeuers);
279 if (err != 1) {
280 show_usage(argc, argv);
281 return -1;
282 }
283
284 err = sscanf(argv[2], "%u", &nr_enqueuers);
285 if (err != 1) {
286 show_usage(argc, argv);
287 return -1;
288 }
289
290 err = sscanf(argv[3], "%lu", &duration);
291 if (err != 1) {
292 show_usage(argc, argv);
293 return -1;
294 }
295
296 for (i = 4; i < argc; i++) {
297 if (argv[i][0] != '-')
298 continue;
299 switch (argv[i][1]) {
300 case 'a':
301 if (argc < i + 2) {
302 show_usage(argc, argv);
303 return -1;
304 }
305 a = atoi(argv[++i]);
306 cpu_affinities[next_aff++] = a;
307 use_affinity = 1;
308 printf_verbose("Adding CPU %d affinity\n", a);
309 break;
310 case 'c':
311 if (argc < i + 2) {
312 show_usage(argc, argv);
313 return -1;
314 }
315 rduration = atol(argv[++i]);
316 break;
317 case 'd':
318 if (argc < i + 2) {
319 show_usage(argc, argv);
320 return -1;
321 }
322 wdelay = atol(argv[++i]);
323 break;
324 case 'v':
325 verbose_mode = 1;
326 break;
327 }
328 }
329
330 printf_verbose("running test for %lu seconds, %u enqueuers, "
331 "%u dequeuers.\n",
332 duration, nr_enqueuers, nr_dequeuers);
333 printf_verbose("Writer delay : %lu loops.\n", rduration);
334 printf_verbose("Reader duration : %lu loops.\n", wdelay);
335 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
336 "main", (unsigned long) pthread_self(),
337 (unsigned long) gettid());
338
339 tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer));
340 tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer));
341 count_enqueuer = calloc(nr_enqueuers, 2 * sizeof(*count_enqueuer));
342 count_dequeuer = calloc(nr_dequeuers, 2 * sizeof(*count_dequeuer));
343 cds_wfq_init(&q);
344
345 next_aff = 0;
346
347 for (i = 0; i < nr_enqueuers; i++) {
348 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
349 &count_enqueuer[2 * i]);
350 if (err != 0)
351 exit(1);
352 }
353 for (i = 0; i < nr_dequeuers; i++) {
354 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
355 &count_dequeuer[2 * i]);
356 if (err != 0)
357 exit(1);
358 }
359
360 cmm_smp_mb();
361
362 test_go = 1;
363
364 for (i = 0; i < duration; i++) {
365 sleep(1);
366 if (verbose_mode)
367 write (1, ".", 1);
368 }
369
370 test_stop = 1;
371
372 for (i = 0; i < nr_enqueuers; i++) {
373 err = pthread_join(tid_enqueuer[i], &tret);
374 if (err != 0)
375 exit(1);
376 tot_enqueues += count_enqueuer[2 * i];
377 tot_successful_enqueues += count_enqueuer[2 * i + 1];
378 }
379 for (i = 0; i < nr_dequeuers; i++) {
380 err = pthread_join(tid_dequeuer[i], &tret);
381 if (err != 0)
382 exit(1);
383 tot_dequeues += count_dequeuer[2 * i];
384 tot_successful_dequeues += count_dequeuer[2 * i + 1];
385 }
386
387 test_end(&q, &end_dequeues);
388
389 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
390 tot_enqueues, tot_dequeues);
391 printf_verbose("total number of successful enqueues : %llu, "
392 "successful dequeues %llu\n",
393 tot_successful_enqueues, tot_successful_dequeues);
394 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
395 "nr_dequeuers %3u "
396 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
397 "successful enqueues %12llu successful dequeues %12llu "
398 "end_dequeues %llu nr_ops %12llu\n",
399 argv[0], duration, nr_enqueuers, wdelay,
400 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
401 tot_successful_enqueues,
402 tot_successful_dequeues, end_dequeues,
403 tot_enqueues + tot_dequeues);
404 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
405 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
406 "succ. dequeues + end dequeues %llu.\n",
407 tot_successful_enqueues,
408 tot_successful_dequeues + end_dequeues);
409
410 free(count_enqueuer);
411 free(count_dequeuer);
412 free(tid_enqueuer);
413 free(tid_dequeuer);
414 return 0;
415 }
This page took 0.039039 seconds and 4 git commands to generate.