79ff716c34f153ae66b9e1fe0cd5bc9fa6b35ad7
[urcu.git] / tests / benchmark / 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 #include <stdio.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include <errno.h>
36
37 #include <urcu/arch.h>
38 #include <urcu/tls-compat.h>
39 #include "thread-id.h"
40
41 /* hardcoded number of CPUs */
42 #define NR_CPUS 16384
43
44 #ifndef DYNAMIC_LINK_TEST
45 #define _LGPL_SOURCE
46 #endif
47
48 /* Remove deprecation warnings from test build. */
49 #define CDS_WFQ_DEPRECATED
50
51 #include <urcu.h>
52 #include <urcu/wfqueue.h>
53
54 static volatile int test_go, test_stop;
55
56 static unsigned long rduration;
57
58 static unsigned long duration;
59
60 /* read-side C.S. duration, in loops */
61 static unsigned long wdelay;
62
63 static inline void loop_sleep(unsigned long loops)
64 {
65 while (loops-- != 0)
66 caa_cpu_relax();
67 }
68
69 static int verbose_mode;
70
71 #define printf_verbose(fmt, args...) \
72 do { \
73 if (verbose_mode) \
74 printf(fmt, args); \
75 } while (0)
76
77 static unsigned int cpu_affinities[NR_CPUS];
78 static unsigned int next_aff = 0;
79 static int use_affinity = 0;
80
81 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
82
83 static void set_affinity(void)
84 {
85 #if HAVE_SCHED_SETAFFINITY
86 cpu_set_t mask;
87 int cpu, ret;
88 #endif /* HAVE_SCHED_SETAFFINITY */
89
90 if (!use_affinity)
91 return;
92
93 #if HAVE_SCHED_SETAFFINITY
94 ret = pthread_mutex_lock(&affinity_mutex);
95 if (ret) {
96 perror("Error in pthread mutex lock");
97 exit(-1);
98 }
99 cpu = cpu_affinities[next_aff++];
100 ret = pthread_mutex_unlock(&affinity_mutex);
101 if (ret) {
102 perror("Error in pthread mutex unlock");
103 exit(-1);
104 }
105
106 CPU_ZERO(&mask);
107 CPU_SET(cpu, &mask);
108 sched_setaffinity(0, sizeof(mask), &mask);
109 #endif /* HAVE_SCHED_SETAFFINITY */
110 }
111
112 /*
113 * returns 0 if test should end.
114 */
115 static int test_duration_dequeue(void)
116 {
117 return !test_stop;
118 }
119
120 static int test_duration_enqueue(void)
121 {
122 return !test_stop;
123 }
124
125 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
126 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
127
128 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
129 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
130
131 static unsigned int nr_enqueuers;
132 static unsigned int nr_dequeuers;
133
134 static struct cds_wfq_queue q;
135
136 void *thr_enqueuer(void *_count)
137 {
138 unsigned long long *count = _count;
139
140 printf_verbose("thread_begin %s, tid %lu\n",
141 "enqueuer", urcu_get_thread_id());
142
143 set_affinity();
144
145 while (!test_go)
146 {
147 }
148 cmm_smp_mb();
149
150 for (;;) {
151 struct cds_wfq_node *node = malloc(sizeof(*node));
152 if (!node)
153 goto fail;
154 cds_wfq_node_init(node);
155 cds_wfq_enqueue(&q, node);
156 URCU_TLS(nr_successful_enqueues)++;
157
158 if (caa_unlikely(wdelay))
159 loop_sleep(wdelay);
160 fail:
161 URCU_TLS(nr_enqueues)++;
162 if (caa_unlikely(!test_duration_enqueue()))
163 break;
164 }
165
166 count[0] = URCU_TLS(nr_enqueues);
167 count[1] = URCU_TLS(nr_successful_enqueues);
168 printf_verbose("enqueuer thread_end, tid %lu, "
169 "enqueues %llu successful_enqueues %llu\n",
170 urcu_get_thread_id(),
171 URCU_TLS(nr_enqueues),
172 URCU_TLS(nr_successful_enqueues));
173 return ((void*)1);
174
175 }
176
177 void *thr_dequeuer(void *_count)
178 {
179 unsigned long long *count = _count;
180
181 printf_verbose("thread_begin %s, tid %lu\n",
182 "dequeuer", urcu_get_thread_id());
183
184 set_affinity();
185
186 while (!test_go)
187 {
188 }
189 cmm_smp_mb();
190
191 for (;;) {
192 struct cds_wfq_node *node = cds_wfq_dequeue_blocking(&q);
193
194 if (node) {
195 free(node);
196 URCU_TLS(nr_successful_dequeues)++;
197 }
198
199 URCU_TLS(nr_dequeues)++;
200 if (caa_unlikely(!test_duration_dequeue()))
201 break;
202 if (caa_unlikely(rduration))
203 loop_sleep(rduration);
204 }
205
206 printf_verbose("dequeuer thread_end, tid %lu, "
207 "dequeues %llu, successful_dequeues %llu\n",
208 urcu_get_thread_id(),
209 URCU_TLS(nr_dequeues),
210 URCU_TLS(nr_successful_dequeues));
211 count[0] = URCU_TLS(nr_dequeues);
212 count[1] = URCU_TLS(nr_successful_dequeues);
213 return ((void*)2);
214 }
215
216 void test_end(struct cds_wfq_queue *q, unsigned long long *nr_dequeues)
217 {
218 struct cds_wfq_node *node;
219
220 do {
221 node = cds_wfq_dequeue_blocking(q);
222 if (node) {
223 free(node);
224 (*nr_dequeues)++;
225 }
226 } while (node);
227 }
228
229 void show_usage(int argc, char **argv)
230 {
231 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
232 argv[0]);
233 printf("OPTIONS:\n");
234 printf(" [-d delay] (enqueuer period (in loops))\n");
235 printf(" [-c duration] (dequeuer period (in loops))\n");
236 printf(" [-v] (verbose output)\n");
237 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
238 printf("\n");
239 }
240
241 int main(int argc, char **argv)
242 {
243 int err;
244 pthread_t *tid_enqueuer, *tid_dequeuer;
245 void *tret;
246 unsigned long long *count_enqueuer, *count_dequeuer;
247 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
248 unsigned long long tot_successful_enqueues = 0,
249 tot_successful_dequeues = 0;
250 unsigned long long end_dequeues = 0;
251 int i, a;
252 unsigned int i_thr;
253
254 if (argc < 4) {
255 show_usage(argc, argv);
256 return -1;
257 }
258
259 err = sscanf(argv[1], "%u", &nr_dequeuers);
260 if (err != 1) {
261 show_usage(argc, argv);
262 return -1;
263 }
264
265 err = sscanf(argv[2], "%u", &nr_enqueuers);
266 if (err != 1) {
267 show_usage(argc, argv);
268 return -1;
269 }
270
271 err = sscanf(argv[3], "%lu", &duration);
272 if (err != 1) {
273 show_usage(argc, argv);
274 return -1;
275 }
276
277 for (i = 4; i < argc; i++) {
278 if (argv[i][0] != '-')
279 continue;
280 switch (argv[i][1]) {
281 case 'a':
282 if (argc < i + 2) {
283 show_usage(argc, argv);
284 return -1;
285 }
286 a = atoi(argv[++i]);
287 cpu_affinities[next_aff++] = a;
288 use_affinity = 1;
289 printf_verbose("Adding CPU %d affinity\n", a);
290 break;
291 case 'c':
292 if (argc < i + 2) {
293 show_usage(argc, argv);
294 return -1;
295 }
296 rduration = atol(argv[++i]);
297 break;
298 case 'd':
299 if (argc < i + 2) {
300 show_usage(argc, argv);
301 return -1;
302 }
303 wdelay = atol(argv[++i]);
304 break;
305 case 'v':
306 verbose_mode = 1;
307 break;
308 }
309 }
310
311 printf_verbose("running test for %lu seconds, %u enqueuers, "
312 "%u dequeuers.\n",
313 duration, nr_enqueuers, nr_dequeuers);
314 printf_verbose("Writer delay : %lu loops.\n", rduration);
315 printf_verbose("Reader duration : %lu loops.\n", wdelay);
316 printf_verbose("thread %-6s, tid %lu\n",
317 "main", urcu_get_thread_id());
318
319 tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer));
320 tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer));
321 count_enqueuer = calloc(nr_enqueuers, 2 * sizeof(*count_enqueuer));
322 count_dequeuer = calloc(nr_dequeuers, 2 * sizeof(*count_dequeuer));
323 cds_wfq_init(&q);
324
325 next_aff = 0;
326
327 for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) {
328 err = pthread_create(&tid_enqueuer[i_thr], NULL, thr_enqueuer,
329 &count_enqueuer[2 * i_thr]);
330 if (err != 0)
331 exit(1);
332 }
333 for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) {
334 err = pthread_create(&tid_dequeuer[i_thr], NULL, thr_dequeuer,
335 &count_dequeuer[2 * i_thr]);
336 if (err != 0)
337 exit(1);
338 }
339
340 cmm_smp_mb();
341
342 test_go = 1;
343
344 for (i_thr = 0; i_thr < duration; i_thr++) {
345 sleep(1);
346 if (verbose_mode) {
347 fwrite(".", sizeof(char), 1, stdout);
348 fflush(stdout);
349 }
350 }
351
352 test_stop = 1;
353
354 for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) {
355 err = pthread_join(tid_enqueuer[i_thr], &tret);
356 if (err != 0)
357 exit(1);
358 tot_enqueues += count_enqueuer[2 * i_thr];
359 tot_successful_enqueues += count_enqueuer[2 * i_thr + 1];
360 }
361 for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) {
362 err = pthread_join(tid_dequeuer[i_thr], &tret);
363 if (err != 0)
364 exit(1);
365 tot_dequeues += count_dequeuer[2 * i_thr];
366 tot_successful_dequeues += count_dequeuer[2 * i_thr + 1];
367 }
368
369 test_end(&q, &end_dequeues);
370
371 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
372 tot_enqueues, tot_dequeues);
373 printf_verbose("total number of successful enqueues : %llu, "
374 "successful dequeues %llu\n",
375 tot_successful_enqueues, tot_successful_dequeues);
376 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
377 "nr_dequeuers %3u "
378 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
379 "successful enqueues %12llu successful dequeues %12llu "
380 "end_dequeues %llu nr_ops %12llu\n",
381 argv[0], duration, nr_enqueuers, wdelay,
382 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
383 tot_successful_enqueues,
384 tot_successful_dequeues, end_dequeues,
385 tot_enqueues + tot_dequeues);
386 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
387 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
388 "succ. dequeues + end dequeues %llu.\n",
389 tot_successful_enqueues,
390 tot_successful_dequeues + end_dequeues);
391
392 cds_wfq_destroy(&q);
393 free(count_enqueuer);
394 free(count_dequeuer);
395 free(tid_enqueuer);
396 free(tid_dequeuer);
397
398 return 0;
399 }
This page took 0.035404 seconds and 3 git commands to generate.