2 * test_urcu_workqueue.c
4 * Userspace RCU library - workqueue test
6 * Copyright February 2010-2014 - 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>
41 #include <urcu/uatomic.h>
43 #include "thread-id.h"
45 /* hardcoded number of CPUs */
48 #ifndef DYNAMIC_LINK_TEST
52 #include <urcu/wfstack.h>
53 #include <urcu/workqueue-fifo.h>
55 static volatile int test_go
, test_stop_enqueue
;
57 static unsigned long work_loops
;
59 static unsigned long duration
;
61 static unsigned long dispatch_delay_loops
;
63 static inline void loop_sleep(unsigned long loops
)
69 static int verbose_mode
;
71 static int test_wait_empty
;
72 static int test_enqueue_stopped
;
74 #define printf_verbose(fmt, args...) \
77 fprintf(stderr, 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_enqueue(void)
124 return !test_stop_enqueue
;
127 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
128 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
130 static unsigned int nr_dispatchers
;
131 static unsigned int nr_workers
;
133 static struct urcu_workqueue workqueue
;
139 static void *thr_dispatcher(void *_count
)
141 unsigned long long *count
= _count
;
144 printf_verbose("thread_begin %s, tid %lu\n",
145 "dispatcher", urcu_get_thread_id());
155 struct test_work
*work
= malloc(sizeof(*work
));
158 printf_verbose("queue work %p\n", work
);
159 urcu_queue_work(&workqueue
, &work
->w
);
160 URCU_TLS(nr_enqueues
)++;
162 if (caa_unlikely(dispatch_delay_loops
))
163 loop_sleep(dispatch_delay_loops
);
165 if (caa_unlikely(!test_duration_enqueue()))
169 uatomic_inc(&test_enqueue_stopped
);
170 count
[0] = URCU_TLS(nr_enqueues
);
171 printf_verbose("dispatcher thread_end, tid %lu, "
173 urcu_get_thread_id(),
174 URCU_TLS(nr_enqueues
));
178 static void *thr_worker(void *_count
)
180 unsigned long long *count
= _count
;
181 unsigned int counter
= 0;
182 struct urcu_worker worker
;
184 printf_verbose("thread_begin %s, tid %lu\n",
185 "worker", urcu_get_thread_id());
189 rcu_register_thread();
190 urcu_worker_init(&worker
, URCU_WORKER_STEAL
);
191 //urcu_worker_init(&worker, 0);
192 urcu_worker_register(&workqueue
, &worker
);
200 enum urcu_accept_ret ret
;
202 ret
= urcu_accept_work(&workqueue
, &worker
);
203 if (ret
== URCU_ACCEPT_SHUTDOWN
)
206 struct urcu_work
*work
;
209 work
= urcu_dequeue_work(&worker
);
212 t
= caa_container_of(work
, struct test_work
, w
);
213 printf_verbose("dequeue work %p\n", t
);
214 URCU_TLS(nr_dequeues
)++;
215 if (caa_unlikely(work_loops
))
216 loop_sleep(work_loops
);
221 urcu_worker_unregister(&workqueue
, &worker
);
222 rcu_unregister_thread();
224 printf_verbose("worker thread_end, tid %lu, "
226 urcu_get_thread_id(),
227 URCU_TLS(nr_dequeues
));
228 count
[0] = URCU_TLS(nr_dequeues
);
232 static void show_usage(int argc
, char **argv
)
234 printf("Usage : %s nr_workers nr_dispatchers duration (s) <OPTIONS>\n",
236 printf("OPTIONS:\n");
237 printf(" [-d delay] (dispatcher period (in loops))\n");
238 printf(" [-c duration] (worker period (in loops))\n");
239 printf(" [-v] (verbose output)\n");
240 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
241 printf(" [-w] Wait for worker to empty stack\n");
245 int main(int argc
, char **argv
)
248 pthread_t
*tid_dispatcher
, *tid_worker
;
250 unsigned long long *count_dispatcher
, *count_worker
;
251 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
252 unsigned long long end_dequeues
= 0;
253 int i
, a
, retval
= 0;
256 show_usage(argc
, argv
);
260 err
= sscanf(argv
[1], "%u", &nr_workers
);
262 show_usage(argc
, argv
);
266 err
= sscanf(argv
[2], "%u", &nr_dispatchers
);
268 show_usage(argc
, argv
);
272 err
= sscanf(argv
[3], "%lu", &duration
);
274 show_usage(argc
, argv
);
278 for (i
= 4; i
< argc
; i
++) {
279 if (argv
[i
][0] != '-')
281 switch (argv
[i
][1]) {
284 show_usage(argc
, argv
);
288 cpu_affinities
[next_aff
++] = a
;
290 printf_verbose("Adding CPU %d affinity\n", a
);
294 show_usage(argc
, argv
);
297 work_loops
= atol(argv
[++i
]);
301 show_usage(argc
, argv
);
304 dispatch_delay_loops
= atol(argv
[++i
]);
315 printf_verbose("running test for %lu seconds, %u dispatchers, "
317 duration
, nr_dispatchers
, nr_workers
);
319 printf_verbose("Wait for workers to empty workqueue.\n");
320 printf_verbose("Work duration: %lu loops.\n", work_loops
);
321 printf_verbose("Dispatcher arrival delay: %lu loops.\n", dispatch_delay_loops
);
322 printf_verbose("thread %-6s, tid %lu\n",
323 "main", urcu_get_thread_id());
325 tid_dispatcher
= calloc(nr_dispatchers
, sizeof(*tid_dispatcher
));
326 tid_worker
= calloc(nr_workers
, sizeof(*tid_worker
));
327 count_dispatcher
= calloc(nr_dispatchers
, sizeof(*count_dispatcher
));
328 count_worker
= calloc(nr_workers
, sizeof(*count_worker
));
329 urcu_workqueue_init(&workqueue
);
333 for (i
= 0; i
< nr_dispatchers
; i
++) {
334 err
= pthread_create(&tid_dispatcher
[i
], NULL
, thr_dispatcher
,
335 &count_dispatcher
[i
]);
339 for (i
= 0; i
< nr_workers
; i
++) {
340 err
= pthread_create(&tid_worker
[i
], NULL
, thr_worker
,
350 for (i
= 0; i
< duration
; i
++) {
353 (void) write(1, ".", 1);
356 test_stop_enqueue
= 1;
357 while (nr_dispatchers
!= uatomic_read(&test_enqueue_stopped
)) {
361 if (test_wait_empty
) {
362 while (!cds_wfcq_empty(&workqueue
.head
, &workqueue
.tail
)) {
366 urcu_workqueue_shutdown(&workqueue
);
368 for (i
= 0; i
< nr_dispatchers
; i
++) {
369 err
= pthread_join(tid_dispatcher
[i
], &tret
);
372 tot_enqueues
+= count_dispatcher
[i
];
374 for (i
= 0; i
< nr_workers
; i
++) {
375 err
= pthread_join(tid_worker
[i
], &tret
);
378 tot_dequeues
+= count_worker
[i
];
381 printf("SUMMARY %-25s testdur %4lu nr_dispatchers %3u dispatch_delay_loops %6lu "
382 "work_loops %lu nr_workers %3u "
383 "nr_enqueues %12llu nr_dequeues %12llu\n",
384 argv
[0], duration
, nr_dispatchers
, dispatch_delay_loops
, work_loops
,
385 nr_workers
, tot_enqueues
, tot_dequeues
);
386 free(count_dispatcher
);
388 free(tid_dispatcher
);