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 unsigned long max_queue_len
;
65 static inline void loop_sleep(unsigned long loops
)
71 static int verbose_mode
;
73 static int test_wait_empty
;
74 static int test_enqueue_stopped
;
76 #define printf_verbose(fmt, args...) \
79 fprintf(stderr, fmt, ## args); \
82 static unsigned int cpu_affinities
[NR_CPUS
];
83 static unsigned int next_aff
= 0;
84 static int use_affinity
= 0;
86 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
88 static void set_affinity(void)
90 #if HAVE_SCHED_SETAFFINITY
93 #endif /* HAVE_SCHED_SETAFFINITY */
98 #if HAVE_SCHED_SETAFFINITY
99 ret
= pthread_mutex_lock(&affinity_mutex
);
101 perror("Error in pthread mutex lock");
104 cpu
= cpu_affinities
[next_aff
++];
105 ret
= pthread_mutex_unlock(&affinity_mutex
);
107 perror("Error in pthread mutex unlock");
113 #if SCHED_SETAFFINITY_ARGS == 2
114 sched_setaffinity(0, &mask
);
116 sched_setaffinity(0, sizeof(mask
), &mask
);
118 #endif /* HAVE_SCHED_SETAFFINITY */
122 * returns 0 if test should end.
124 static int test_duration_enqueue(void)
126 return !test_stop_enqueue
;
129 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
130 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
132 static unsigned int nr_dispatchers
;
133 static unsigned int nr_workers
;
135 static struct urcu_workqueue workqueue
;
141 static void *thr_dispatcher(void *_count
)
143 unsigned long long *count
= _count
;
146 printf_verbose("thread_begin %s, tid %lu\n",
147 "dispatcher", urcu_get_thread_id());
157 struct test_work
*work
= malloc(sizeof(*work
));
158 enum urcu_enqueue_ret ret
;
163 printf_verbose("attempt queue work %p\n", work
);
164 ret
= urcu_queue_work(&workqueue
, &work
->w
);
165 if (ret
== URCU_ENQUEUE_FULL
) {
166 printf_verbose("queue work %p (queue full)\n", work
);
167 (void) poll(NULL
, 0, 10);
170 printf_verbose("queue work %p (ok)\n", work
);
171 URCU_TLS(nr_enqueues
)++;
173 if (caa_unlikely(dispatch_delay_loops
))
174 loop_sleep(dispatch_delay_loops
);
176 if (caa_unlikely(!test_duration_enqueue()))
180 uatomic_inc(&test_enqueue_stopped
);
181 count
[0] = URCU_TLS(nr_enqueues
);
182 printf_verbose("dispatcher thread_end, tid %lu, "
184 urcu_get_thread_id(),
185 URCU_TLS(nr_enqueues
));
189 static void *thr_worker(void *_count
)
191 unsigned long long *count
= _count
;
192 unsigned int counter
= 0;
193 struct urcu_worker worker
;
195 printf_verbose("thread_begin %s, tid %lu\n",
196 "worker", urcu_get_thread_id());
200 rcu_register_thread();
201 urcu_worker_init(&workqueue
, &worker
, URCU_WORKER_STEAL
);
202 //urcu_worker_init(&workqueue, &worker, 0);
203 urcu_worker_register(&workqueue
, &worker
);
211 enum urcu_accept_ret ret
;
213 ret
= urcu_accept_work(&worker
);
214 if (ret
== URCU_ACCEPT_SHUTDOWN
)
217 struct urcu_work
*work
;
220 work
= urcu_dequeue_work(&worker
);
223 t
= caa_container_of(work
, struct test_work
, w
);
224 printf_verbose("dequeue work %p\n", t
);
225 URCU_TLS(nr_dequeues
)++;
226 if (caa_unlikely(work_loops
))
227 loop_sleep(work_loops
);
232 urcu_worker_unregister(&workqueue
, &worker
);
233 rcu_unregister_thread();
235 printf_verbose("worker thread_end, tid %lu, "
237 urcu_get_thread_id(),
238 URCU_TLS(nr_dequeues
));
239 count
[0] = URCU_TLS(nr_dequeues
);
243 static void show_usage(int argc
, char **argv
)
245 printf("Usage : %s nr_workers nr_dispatchers duration (s) <OPTIONS>\n",
247 printf("OPTIONS:\n");
248 printf(" [-d delay] (dispatcher period (in loops))\n");
249 printf(" [-c duration] (worker period (in loops))\n");
250 printf(" [-v] (verbose output)\n");
251 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
252 printf(" [-w] Wait for worker to empty stack\n");
253 printf(" [-m len] (Max queue length. 0 means infinite.))\n");
257 int main(int argc
, char **argv
)
260 pthread_t
*tid_dispatcher
, *tid_worker
;
262 unsigned long long *count_dispatcher
, *count_worker
;
263 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
264 unsigned long long end_dequeues
= 0;
265 int i
, a
, retval
= 0;
268 show_usage(argc
, argv
);
272 err
= sscanf(argv
[1], "%u", &nr_workers
);
274 show_usage(argc
, argv
);
278 err
= sscanf(argv
[2], "%u", &nr_dispatchers
);
280 show_usage(argc
, argv
);
284 err
= sscanf(argv
[3], "%lu", &duration
);
286 show_usage(argc
, argv
);
290 for (i
= 4; i
< argc
; i
++) {
291 if (argv
[i
][0] != '-')
293 switch (argv
[i
][1]) {
296 show_usage(argc
, argv
);
300 cpu_affinities
[next_aff
++] = a
;
302 printf_verbose("Adding CPU %d affinity\n", a
);
306 show_usage(argc
, argv
);
309 max_queue_len
= atol(argv
[++i
]);
313 show_usage(argc
, argv
);
316 work_loops
= atol(argv
[++i
]);
320 show_usage(argc
, argv
);
323 dispatch_delay_loops
= atol(argv
[++i
]);
334 printf_verbose("running test for %lu seconds, %u dispatchers, "
336 duration
, nr_dispatchers
, nr_workers
);
338 printf_verbose("Wait for workers to empty workqueue.\n");
339 printf_verbose("Work duration: %lu loops.\n", work_loops
);
340 printf_verbose("Dispatcher arrival delay: %lu loops.\n", dispatch_delay_loops
);
341 printf_verbose("thread %-6s, tid %lu\n",
342 "main", urcu_get_thread_id());
344 tid_dispatcher
= calloc(nr_dispatchers
, sizeof(*tid_dispatcher
));
345 tid_worker
= calloc(nr_workers
, sizeof(*tid_worker
));
346 count_dispatcher
= calloc(nr_dispatchers
, sizeof(*count_dispatcher
));
347 count_worker
= calloc(nr_workers
, sizeof(*count_worker
));
348 urcu_workqueue_init(&workqueue
, max_queue_len
);
352 for (i
= 0; i
< nr_dispatchers
; i
++) {
353 err
= pthread_create(&tid_dispatcher
[i
], NULL
, thr_dispatcher
,
354 &count_dispatcher
[i
]);
358 for (i
= 0; i
< nr_workers
; i
++) {
359 err
= pthread_create(&tid_worker
[i
], NULL
, thr_worker
,
369 for (i
= 0; i
< duration
; i
++) {
372 (void) write(1, ".", 1);
375 test_stop_enqueue
= 1;
376 while (nr_dispatchers
!= uatomic_read(&test_enqueue_stopped
)) {
380 if (test_wait_empty
) {
381 while (!cds_wfcq_empty(&workqueue
.head
, &workqueue
.tail
)) {
385 urcu_workqueue_shutdown(&workqueue
);
387 for (i
= 0; i
< nr_dispatchers
; i
++) {
388 err
= pthread_join(tid_dispatcher
[i
], &tret
);
391 tot_enqueues
+= count_dispatcher
[i
];
393 for (i
= 0; i
< nr_workers
; i
++) {
394 err
= pthread_join(tid_worker
[i
], &tret
);
397 tot_dequeues
+= count_worker
[i
];
400 printf("SUMMARY %-25s testdur %4lu nr_dispatchers %3u dispatch_delay_loops %6lu "
401 "work_loops %lu nr_workers %3u "
402 "nr_enqueues %12llu nr_dequeues %12llu max_queue_len %lu\n",
403 argv
[0], duration
, nr_dispatchers
, dispatch_delay_loops
, work_loops
,
404 nr_workers
, tot_enqueues
, tot_dequeues
, max_queue_len
);
405 free(count_dispatcher
);
407 free(tid_dispatcher
);