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.
25 #include "../config.h"
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
53 static volatile int test_go
, test_stop
;
55 static unsigned long rduration
;
57 static unsigned long duration
;
59 /* read-side C.S. duration, in loops */
60 static unsigned long wdelay
;
62 static inline void loop_sleep(unsigned long loops
)
68 static int verbose_mode
;
70 #define printf_verbose(fmt, args...) \
76 static unsigned int cpu_affinities
[NR_CPUS
];
77 static unsigned int next_aff
= 0;
78 static int use_affinity
= 0;
80 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
82 static void set_affinity(void)
84 #if HAVE_SCHED_SETAFFINITY
87 #endif /* HAVE_SCHED_SETAFFINITY */
92 #if HAVE_SCHED_SETAFFINITY
93 ret
= pthread_mutex_lock(&affinity_mutex
);
95 perror("Error in pthread mutex lock");
98 cpu
= cpu_affinities
[next_aff
++];
99 ret
= pthread_mutex_unlock(&affinity_mutex
);
101 perror("Error in pthread mutex unlock");
107 #if SCHED_SETAFFINITY_ARGS == 2
108 sched_setaffinity(0, &mask
);
110 sched_setaffinity(0, sizeof(mask
), &mask
);
112 #endif /* HAVE_SCHED_SETAFFINITY */
116 * returns 0 if test should end.
118 static int test_duration_dequeue(void)
123 static int test_duration_enqueue(void)
128 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
129 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
131 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
132 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
134 static unsigned int nr_enqueuers
;
135 static unsigned int nr_dequeuers
;
138 struct cds_lfq_node_rcu list
;
142 static struct cds_lfq_queue_rcu q
;
144 void *thr_enqueuer(void *_count
)
146 unsigned long long *count
= _count
;
148 printf_verbose("thread_begin %s, tid %lu\n",
149 "enqueuer", urcu_get_thread_id());
153 rcu_register_thread();
161 struct test
*node
= malloc(sizeof(*node
));
164 cds_lfq_node_init_rcu(&node
->list
);
166 cds_lfq_enqueue_rcu(&q
, &node
->list
);
168 URCU_TLS(nr_successful_enqueues
)++;
170 if (caa_unlikely(wdelay
))
173 URCU_TLS(nr_enqueues
)++;
174 if (caa_unlikely(!test_duration_enqueue()))
178 rcu_unregister_thread();
180 count
[0] = URCU_TLS(nr_enqueues
);
181 count
[1] = URCU_TLS(nr_successful_enqueues
);
182 printf_verbose("enqueuer thread_end, tid %lu, "
183 "enqueues %llu successful_enqueues %llu\n",
184 urcu_get_thread_id(),
185 URCU_TLS(nr_enqueues
),
186 URCU_TLS(nr_successful_enqueues
));
192 void free_node_cb(struct rcu_head
*head
)
195 caa_container_of(head
, struct test
, rcu
);
199 void *thr_dequeuer(void *_count
)
201 unsigned long long *count
= _count
;
203 printf_verbose("thread_begin %s, tid %lu\n",
204 "dequeuer", urcu_get_thread_id());
208 rcu_register_thread();
216 struct cds_lfq_node_rcu
*qnode
;
219 qnode
= cds_lfq_dequeue_rcu(&q
);
225 node
= caa_container_of(qnode
, struct test
, list
);
226 call_rcu(&node
->rcu
, free_node_cb
);
227 URCU_TLS(nr_successful_dequeues
)++;
230 URCU_TLS(nr_dequeues
)++;
231 if (caa_unlikely(!test_duration_dequeue()))
233 if (caa_unlikely(rduration
))
234 loop_sleep(rduration
);
237 rcu_unregister_thread();
238 printf_verbose("dequeuer thread_end, tid %lu, "
239 "dequeues %llu, successful_dequeues %llu\n",
240 urcu_get_thread_id(),
241 URCU_TLS(nr_dequeues
),
242 URCU_TLS(nr_successful_dequeues
));
243 count
[0] = URCU_TLS(nr_dequeues
);
244 count
[1] = URCU_TLS(nr_successful_dequeues
);
248 void test_end(struct cds_lfq_queue_rcu
*q
, unsigned long long *nr_dequeues
)
250 struct cds_lfq_node_rcu
*snode
;
253 snode
= cds_lfq_dequeue_rcu(q
);
257 node
= caa_container_of(snode
, struct test
, list
);
258 free(node
); /* no more concurrent access */
264 void show_usage(int argc
, char **argv
)
266 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
268 printf("OPTIONS:\n");
269 printf(" [-d delay] (enqueuer period (in loops))\n");
270 printf(" [-c duration] (dequeuer period (in loops))\n");
271 printf(" [-v] (verbose output)\n");
272 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
276 int main(int argc
, char **argv
)
279 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
281 unsigned long long *count_enqueuer
, *count_dequeuer
;
282 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
283 unsigned long long tot_successful_enqueues
= 0,
284 tot_successful_dequeues
= 0;
285 unsigned long long end_dequeues
= 0;
289 show_usage(argc
, argv
);
293 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
295 show_usage(argc
, argv
);
299 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
301 show_usage(argc
, argv
);
305 err
= sscanf(argv
[3], "%lu", &duration
);
307 show_usage(argc
, argv
);
311 for (i
= 4; i
< argc
; i
++) {
312 if (argv
[i
][0] != '-')
314 switch (argv
[i
][1]) {
317 show_usage(argc
, argv
);
321 cpu_affinities
[next_aff
++] = a
;
323 printf_verbose("Adding CPU %d affinity\n", a
);
327 show_usage(argc
, argv
);
330 rduration
= atol(argv
[++i
]);
334 show_usage(argc
, argv
);
337 wdelay
= atol(argv
[++i
]);
345 printf_verbose("running test for %lu seconds, %u enqueuers, "
347 duration
, nr_enqueuers
, nr_dequeuers
);
348 printf_verbose("Writer delay : %lu loops.\n", rduration
);
349 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
350 printf_verbose("thread %-6s, tid %lu\n",
351 "main", urcu_get_thread_id());
353 tid_enqueuer
= malloc(sizeof(*tid_enqueuer
) * nr_enqueuers
);
354 tid_dequeuer
= malloc(sizeof(*tid_dequeuer
) * nr_dequeuers
);
355 count_enqueuer
= malloc(2 * sizeof(*count_enqueuer
) * nr_enqueuers
);
356 count_dequeuer
= malloc(2 * sizeof(*count_dequeuer
) * nr_dequeuers
);
357 cds_lfq_init_rcu(&q
, call_rcu
);
358 err
= create_all_cpu_call_rcu_data(0);
360 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
365 for (i
= 0; i
< nr_enqueuers
; i
++) {
366 err
= pthread_create(&tid_enqueuer
[i
], NULL
, thr_enqueuer
,
367 &count_enqueuer
[2 * i
]);
371 for (i
= 0; i
< nr_dequeuers
; i
++) {
372 err
= pthread_create(&tid_dequeuer
[i
], NULL
, thr_dequeuer
,
373 &count_dequeuer
[2 * i
]);
382 for (i
= 0; i
< duration
; i
++) {
390 for (i
= 0; i
< nr_enqueuers
; i
++) {
391 err
= pthread_join(tid_enqueuer
[i
], &tret
);
394 tot_enqueues
+= count_enqueuer
[2 * i
];
395 tot_successful_enqueues
+= count_enqueuer
[2 * i
+ 1];
397 for (i
= 0; i
< nr_dequeuers
; i
++) {
398 err
= pthread_join(tid_dequeuer
[i
], &tret
);
401 tot_dequeues
+= count_dequeuer
[2 * i
];
402 tot_successful_dequeues
+= count_dequeuer
[2 * i
+ 1];
405 test_end(&q
, &end_dequeues
);
406 err
= cds_lfq_destroy_rcu(&q
);
409 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
410 tot_enqueues
, tot_dequeues
);
411 printf_verbose("total number of successful enqueues : %llu, "
412 "successful dequeues %llu\n",
413 tot_successful_enqueues
, tot_successful_dequeues
);
414 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
416 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
417 "successful enqueues %12llu successful dequeues %12llu "
418 "end_dequeues %llu nr_ops %12llu\n",
419 argv
[0], duration
, nr_enqueuers
, wdelay
,
420 nr_dequeuers
, rduration
, tot_enqueues
, tot_dequeues
,
421 tot_successful_enqueues
,
422 tot_successful_dequeues
, end_dequeues
,
423 tot_enqueues
+ tot_dequeues
);
424 if (tot_successful_enqueues
!= tot_successful_dequeues
+ end_dequeues
)
425 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
426 "succ. dequeues + end dequeues %llu.\n",
427 tot_successful_enqueues
,
428 tot_successful_dequeues
+ end_dequeues
);
430 free_all_cpu_call_rcu_data();
431 free(count_enqueuer
);
432 free(count_dequeuer
);