4 * Userspace RCU library - example RCU-based lock-free queue
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com>
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.
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.
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.
32 #include <sys/types.h>
39 #include <urcu/arch.h>
40 #include <urcu/tls-compat.h>
42 #include "thread-id.h"
44 /* hardcoded number of CPUs */
47 #ifndef DYNAMIC_LINK_TEST
51 /* Remove deprecation warnings from test build. */
52 #define CDS_WFQ_DEPRECATED
55 #include <urcu/wfqueue.h>
57 static volatile int test_go
, test_stop
;
59 static unsigned long rduration
;
61 static unsigned long duration
;
63 /* read-side C.S. duration, in loops */
64 static unsigned long wdelay
;
66 static inline void loop_sleep(unsigned long loops
)
72 static int verbose_mode
;
74 #define printf_verbose(fmt, args...) \
80 static unsigned int cpu_affinities
[NR_CPUS
];
81 static unsigned int next_aff
= 0;
82 static int use_affinity
= 0;
84 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
86 static void set_affinity(void)
88 #if HAVE_SCHED_SETAFFINITY
91 #endif /* HAVE_SCHED_SETAFFINITY */
96 #if HAVE_SCHED_SETAFFINITY
97 ret
= pthread_mutex_lock(&affinity_mutex
);
99 perror("Error in pthread mutex lock");
102 cpu
= cpu_affinities
[next_aff
++];
103 ret
= pthread_mutex_unlock(&affinity_mutex
);
105 perror("Error in pthread mutex unlock");
111 #if SCHED_SETAFFINITY_ARGS == 2
112 sched_setaffinity(0, &mask
);
114 sched_setaffinity(0, sizeof(mask
), &mask
);
116 #endif /* HAVE_SCHED_SETAFFINITY */
120 * returns 0 if test should end.
122 static int test_duration_dequeue(void)
127 static int test_duration_enqueue(void)
132 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
133 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
135 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
136 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
138 static unsigned int nr_enqueuers
;
139 static unsigned int nr_dequeuers
;
141 static struct cds_wfq_queue q
;
143 void *thr_enqueuer(void *_count
)
145 unsigned long long *count
= _count
;
147 printf_verbose("thread_begin %s, tid %lu\n",
148 "enqueuer", urcu_get_thread_id());
158 struct cds_wfq_node
*node
= malloc(sizeof(*node
));
161 cds_wfq_node_init(node
);
162 cds_wfq_enqueue(&q
, node
);
163 URCU_TLS(nr_successful_enqueues
)++;
165 if (caa_unlikely(wdelay
))
168 URCU_TLS(nr_enqueues
)++;
169 if (caa_unlikely(!test_duration_enqueue()))
173 count
[0] = URCU_TLS(nr_enqueues
);
174 count
[1] = URCU_TLS(nr_successful_enqueues
);
175 printf_verbose("enqueuer thread_end, tid %lu, "
176 "enqueues %llu successful_enqueues %llu\n",
177 urcu_get_thread_id(),
178 URCU_TLS(nr_enqueues
),
179 URCU_TLS(nr_successful_enqueues
));
184 void *thr_dequeuer(void *_count
)
186 unsigned long long *count
= _count
;
188 printf_verbose("thread_begin %s, tid %lu\n",
189 "dequeuer", urcu_get_thread_id());
199 struct cds_wfq_node
*node
= cds_wfq_dequeue_blocking(&q
);
203 URCU_TLS(nr_successful_dequeues
)++;
206 URCU_TLS(nr_dequeues
)++;
207 if (caa_unlikely(!test_duration_dequeue()))
209 if (caa_unlikely(rduration
))
210 loop_sleep(rduration
);
213 printf_verbose("dequeuer thread_end, tid %lu, "
214 "dequeues %llu, successful_dequeues %llu\n",
215 urcu_get_thread_id(),
216 URCU_TLS(nr_dequeues
),
217 URCU_TLS(nr_successful_dequeues
));
218 count
[0] = URCU_TLS(nr_dequeues
);
219 count
[1] = URCU_TLS(nr_successful_dequeues
);
223 void test_end(struct cds_wfq_queue
*q
, unsigned long long *nr_dequeues
)
225 struct cds_wfq_node
*node
;
228 node
= cds_wfq_dequeue_blocking(q
);
236 void show_usage(int argc
, char **argv
)
238 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
240 printf("OPTIONS:\n");
241 printf(" [-d delay] (enqueuer period (in loops))\n");
242 printf(" [-c duration] (dequeuer period (in loops))\n");
243 printf(" [-v] (verbose output)\n");
244 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
248 int main(int argc
, char **argv
)
251 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
253 unsigned long long *count_enqueuer
, *count_dequeuer
;
254 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
255 unsigned long long tot_successful_enqueues
= 0,
256 tot_successful_dequeues
= 0;
257 unsigned long long end_dequeues
= 0;
261 show_usage(argc
, argv
);
265 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
267 show_usage(argc
, argv
);
271 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
273 show_usage(argc
, argv
);
277 err
= sscanf(argv
[3], "%lu", &duration
);
279 show_usage(argc
, argv
);
283 for (i
= 4; i
< argc
; i
++) {
284 if (argv
[i
][0] != '-')
286 switch (argv
[i
][1]) {
289 show_usage(argc
, argv
);
293 cpu_affinities
[next_aff
++] = a
;
295 printf_verbose("Adding CPU %d affinity\n", a
);
299 show_usage(argc
, argv
);
302 rduration
= atol(argv
[++i
]);
306 show_usage(argc
, argv
);
309 wdelay
= atol(argv
[++i
]);
317 printf_verbose("running test for %lu seconds, %u enqueuers, "
319 duration
, nr_enqueuers
, nr_dequeuers
);
320 printf_verbose("Writer delay : %lu loops.\n", rduration
);
321 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
322 printf_verbose("thread %-6s, tid %lu\n",
323 "main", urcu_get_thread_id());
325 tid_enqueuer
= calloc(nr_enqueuers
, sizeof(*tid_enqueuer
));
326 tid_dequeuer
= calloc(nr_dequeuers
, sizeof(*tid_dequeuer
));
327 count_enqueuer
= calloc(nr_enqueuers
, 2 * sizeof(*count_enqueuer
));
328 count_dequeuer
= calloc(nr_dequeuers
, 2 * sizeof(*count_dequeuer
));
333 for (i
= 0; i
< nr_enqueuers
; i
++) {
334 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
335 &count_enqueuer
[2 * i
]);
339 for (i
= 0; i
< nr_dequeuers
; i
++) {
340 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
341 &count_dequeuer
[2 * i
]);
350 for (i
= 0; i
< duration
; i
++) {
353 fwrite(".", sizeof(char), 1, stdout
);
360 for (i
= 0; i
< nr_enqueuers
; i
++) {
361 err
= pthread_join(tid_enqueuer
[i
], &tret
);
364 tot_enqueues
+= count_enqueuer
[2 * i
];
365 tot_successful_enqueues
+= count_enqueuer
[2 * i
+ 1];
367 for (i
= 0; i
< nr_dequeuers
; i
++) {
368 err
= pthread_join(tid_dequeuer
[i
], &tret
);
371 tot_dequeues
+= count_dequeuer
[2 * i
];
372 tot_successful_dequeues
+= count_dequeuer
[2 * i
+ 1];
375 test_end(&q
, &end_dequeues
);
377 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
378 tot_enqueues
, tot_dequeues
);
379 printf_verbose("total number of successful enqueues : %llu, "
380 "successful dequeues %llu\n",
381 tot_successful_enqueues
, tot_successful_dequeues
);
382 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
384 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
385 "successful enqueues %12llu successful dequeues %12llu "
386 "end_dequeues %llu nr_ops %12llu\n",
387 argv
[0], duration
, nr_enqueuers
, wdelay
,
388 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
389 tot_successful_enqueues
,
390 tot_successful_dequeues
, end_dequeues
,
391 tot_enqueues
+ tot_dequeues
);
392 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
393 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
394 "succ. dequeues + end dequeues %llu.\n",
395 tot_successful_enqueues
,
396 tot_successful_dequeues
+ end_dequeues
);
399 free(count_enqueuer
);
400 free(count_dequeuer
);