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
, test_stop_dequeue
;
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_dequeue(void)
124 return !test_stop_dequeue
;
127 static int test_duration_enqueue(void)
129 return !test_stop_enqueue
;
132 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
133 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
135 static unsigned int nr_dispatchers
;
136 static unsigned int nr_workers
;
138 static struct urcu_workqueue workqueue
;
144 static void *thr_dispatcher(void *_count
)
146 unsigned long long *count
= _count
;
149 printf_verbose("thread_begin %s, tid %lu\n",
150 "dispatcher", urcu_get_thread_id());
160 struct test_work
*work
= malloc(sizeof(*work
));
163 printf_verbose("queue work %p\n", work
);
164 urcu_queue_work(&workqueue
, &work
->w
);
165 URCU_TLS(nr_enqueues
)++;
167 if (caa_unlikely(dispatch_delay_loops
))
168 loop_sleep(dispatch_delay_loops
);
170 if (caa_unlikely(!test_duration_enqueue()))
174 uatomic_inc(&test_enqueue_stopped
);
175 count
[0] = URCU_TLS(nr_enqueues
);
176 printf_verbose("dispatcher thread_end, tid %lu, "
178 urcu_get_thread_id(),
179 URCU_TLS(nr_enqueues
));
183 static void *thr_worker(void *_count
)
185 unsigned long long *count
= _count
;
186 unsigned int counter
= 0;
187 struct urcu_worker worker
;
190 printf_verbose("thread_begin %s, tid %lu\n",
191 "worker", urcu_get_thread_id());
195 rcu_register_thread();
196 urcu_worker_init(&worker
, URCU_WORKER_STEAL
);
197 //urcu_worker_init(&worker, 0);
198 urcu_worker_register(&workqueue
, &worker
);
206 int batch_work_count
= 0;
208 urcu_accept_work(&workqueue
, &worker
, blocking
);
210 struct urcu_work
*work
;
213 work
= urcu_dequeue_work(&worker
);
216 t
= caa_container_of(work
, struct test_work
, w
);
217 printf_verbose("dequeue work %p\n", t
);
219 URCU_TLS(nr_dequeues
)++;
220 if (caa_unlikely(work_loops
))
221 loop_sleep(work_loops
);
224 if (!test_duration_dequeue())
226 if (caa_unlikely(!test_duration_dequeue()
227 && !batch_work_count
))
231 urcu_worker_unregister(&workqueue
, &worker
);
232 rcu_unregister_thread();
234 printf_verbose("worker thread_end, tid %lu, "
236 urcu_get_thread_id(),
237 URCU_TLS(nr_dequeues
));
238 count
[0] = URCU_TLS(nr_dequeues
);
242 static void show_usage(int argc
, char **argv
)
244 printf("Usage : %s nr_workers nr_dispatchers duration (s) <OPTIONS>\n",
246 printf("OPTIONS:\n");
247 printf(" [-d delay] (dispatcher period (in loops))\n");
248 printf(" [-c duration] (worker period (in loops))\n");
249 printf(" [-v] (verbose output)\n");
250 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
251 printf(" [-w] Wait for worker to empty stack\n");
255 int main(int argc
, char **argv
)
258 pthread_t
*tid_dispatcher
, *tid_worker
;
260 unsigned long long *count_dispatcher
, *count_worker
;
261 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
262 unsigned long long end_dequeues
= 0;
263 int i
, a
, retval
= 0;
266 show_usage(argc
, argv
);
270 err
= sscanf(argv
[1], "%u", &nr_workers
);
272 show_usage(argc
, argv
);
276 err
= sscanf(argv
[2], "%u", &nr_dispatchers
);
278 show_usage(argc
, argv
);
282 err
= sscanf(argv
[3], "%lu", &duration
);
284 show_usage(argc
, argv
);
288 for (i
= 4; i
< argc
; i
++) {
289 if (argv
[i
][0] != '-')
291 switch (argv
[i
][1]) {
294 show_usage(argc
, argv
);
298 cpu_affinities
[next_aff
++] = a
;
300 printf_verbose("Adding CPU %d affinity\n", a
);
304 show_usage(argc
, argv
);
307 work_loops
= atol(argv
[++i
]);
311 show_usage(argc
, argv
);
314 dispatch_delay_loops
= atol(argv
[++i
]);
325 printf_verbose("running test for %lu seconds, %u dispatchers, "
327 duration
, nr_dispatchers
, nr_workers
);
329 printf_verbose("Wait for workers to empty workqueue.\n");
330 printf_verbose("Work duration: %lu loops.\n", work_loops
);
331 printf_verbose("Dispatcher arrival delay: %lu loops.\n", dispatch_delay_loops
);
332 printf_verbose("thread %-6s, tid %lu\n",
333 "main", urcu_get_thread_id());
335 tid_dispatcher
= calloc(nr_dispatchers
, sizeof(*tid_dispatcher
));
336 tid_worker
= calloc(nr_workers
, sizeof(*tid_worker
));
337 count_dispatcher
= calloc(nr_dispatchers
, sizeof(*count_dispatcher
));
338 count_worker
= calloc(nr_workers
, sizeof(*count_worker
));
339 urcu_workqueue_init(&workqueue
);
343 for (i
= 0; i
< nr_dispatchers
; i
++) {
344 err
= pthread_create(&tid_dispatcher
[i
], NULL
, thr_dispatcher
,
345 &count_dispatcher
[i
]);
349 for (i
= 0; i
< nr_workers
; i
++) {
350 err
= pthread_create(&tid_worker
[i
], NULL
, thr_worker
,
360 for (i
= 0; i
< duration
; i
++) {
363 (void) write(1, ".", 1);
366 test_stop_enqueue
= 1;
367 while (nr_dispatchers
!= uatomic_read(&test_enqueue_stopped
)) {
371 if (test_wait_empty
) {
372 while (!cds_wfcq_empty(&workqueue
.head
, &workqueue
.tail
)) {
376 test_stop_dequeue
= 1;
378 /* Send finish to all workers */
379 urcu_workqueue_wakeup_all(&workqueue
);
381 for (i
= 0; i
< nr_dispatchers
; i
++) {
382 err
= pthread_join(tid_dispatcher
[i
], &tret
);
385 tot_enqueues
+= count_dispatcher
[i
];
387 for (i
= 0; i
< nr_workers
; i
++) {
388 err
= pthread_join(tid_worker
[i
], &tret
);
391 tot_dequeues
+= count_worker
[i
];
394 printf("SUMMARY %-25s testdur %4lu nr_dispatchers %3u dispatch_delay_loops %6lu "
395 "work_loops %lu nr_workers %3u "
396 "nr_enqueues %12llu nr_dequeues %12llu\n",
397 argv
[0], duration
, nr_dispatchers
, dispatch_delay_loops
, work_loops
,
398 nr_workers
, tot_enqueues
, tot_dequeues
);
399 free(count_dispatcher
);
401 free(tid_dispatcher
);