ui: add trace events related to VNC client throttling
[qemu/ar7.git] / tests / test-aio-multithread.c
blobc8bec81520e0dcb91e56838f025cd13312b2ca3b
1 /*
2 * AioContext multithreading tests
4 * Copyright Red Hat, Inc. 2016
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "block/aio.h"
15 #include "qapi/error.h"
16 #include "qemu/coroutine.h"
17 #include "qemu/thread.h"
18 #include "qemu/error-report.h"
19 #include "iothread.h"
21 /* AioContext management */
23 #define NUM_CONTEXTS 5
25 static IOThread *threads[NUM_CONTEXTS];
26 static AioContext *ctx[NUM_CONTEXTS];
27 static __thread int id = -1;
29 static QemuEvent done_event;
31 /* Run a function synchronously on a remote iothread. */
33 typedef struct CtxRunData {
34 QEMUBHFunc *cb;
35 void *arg;
36 } CtxRunData;
38 static void ctx_run_bh_cb(void *opaque)
40 CtxRunData *data = opaque;
42 data->cb(data->arg);
43 qemu_event_set(&done_event);
46 static void ctx_run(int i, QEMUBHFunc *cb, void *opaque)
48 CtxRunData data = {
49 .cb = cb,
50 .arg = opaque
53 qemu_event_reset(&done_event);
54 aio_bh_schedule_oneshot(ctx[i], ctx_run_bh_cb, &data);
55 qemu_event_wait(&done_event);
58 /* Starting the iothreads. */
60 static void set_id_cb(void *opaque)
62 int *i = opaque;
64 id = *i;
67 static void create_aio_contexts(void)
69 int i;
71 for (i = 0; i < NUM_CONTEXTS; i++) {
72 threads[i] = iothread_new();
73 ctx[i] = iothread_get_aio_context(threads[i]);
76 qemu_event_init(&done_event, false);
77 for (i = 0; i < NUM_CONTEXTS; i++) {
78 ctx_run(i, set_id_cb, &i);
82 /* Stopping the iothreads. */
84 static void join_aio_contexts(void)
86 int i;
88 for (i = 0; i < NUM_CONTEXTS; i++) {
89 aio_context_ref(ctx[i]);
91 for (i = 0; i < NUM_CONTEXTS; i++) {
92 iothread_join(threads[i]);
94 for (i = 0; i < NUM_CONTEXTS; i++) {
95 aio_context_unref(ctx[i]);
97 qemu_event_destroy(&done_event);
100 /* Basic test for the stuff above. */
102 static void test_lifecycle(void)
104 create_aio_contexts();
105 join_aio_contexts();
108 /* aio_co_schedule test. */
110 static Coroutine *to_schedule[NUM_CONTEXTS];
112 static bool now_stopping;
114 static int count_retry;
115 static int count_here;
116 static int count_other;
118 static bool schedule_next(int n)
120 Coroutine *co;
122 co = atomic_xchg(&to_schedule[n], NULL);
123 if (!co) {
124 atomic_inc(&count_retry);
125 return false;
128 if (n == id) {
129 atomic_inc(&count_here);
130 } else {
131 atomic_inc(&count_other);
134 aio_co_schedule(ctx[n], co);
135 return true;
138 static void finish_cb(void *opaque)
140 schedule_next(id);
143 static coroutine_fn void test_multi_co_schedule_entry(void *opaque)
145 g_assert(to_schedule[id] == NULL);
147 while (!atomic_mb_read(&now_stopping)) {
148 int n;
150 n = g_test_rand_int_range(0, NUM_CONTEXTS);
151 schedule_next(n);
153 atomic_mb_set(&to_schedule[id], qemu_coroutine_self());
154 qemu_coroutine_yield();
155 g_assert(to_schedule[id] == NULL);
160 static void test_multi_co_schedule(int seconds)
162 int i;
164 count_here = count_other = count_retry = 0;
165 now_stopping = false;
167 create_aio_contexts();
168 for (i = 0; i < NUM_CONTEXTS; i++) {
169 Coroutine *co1 = qemu_coroutine_create(test_multi_co_schedule_entry, NULL);
170 aio_co_schedule(ctx[i], co1);
173 g_usleep(seconds * 1000000);
175 atomic_mb_set(&now_stopping, true);
176 for (i = 0; i < NUM_CONTEXTS; i++) {
177 ctx_run(i, finish_cb, NULL);
178 to_schedule[i] = NULL;
181 join_aio_contexts();
182 g_test_message("scheduled %d, queued %d, retry %d, total %d\n",
183 count_other, count_here, count_retry,
184 count_here + count_other + count_retry);
187 static void test_multi_co_schedule_1(void)
189 test_multi_co_schedule(1);
192 static void test_multi_co_schedule_10(void)
194 test_multi_co_schedule(10);
197 /* CoMutex thread-safety. */
199 static uint32_t atomic_counter;
200 static uint32_t running;
201 static uint32_t counter;
202 static CoMutex comutex;
204 static void coroutine_fn test_multi_co_mutex_entry(void *opaque)
206 while (!atomic_mb_read(&now_stopping)) {
207 qemu_co_mutex_lock(&comutex);
208 counter++;
209 qemu_co_mutex_unlock(&comutex);
211 /* Increase atomic_counter *after* releasing the mutex. Otherwise
212 * there is a chance (it happens about 1 in 3 runs) that the iothread
213 * exits before the coroutine is woken up, causing a spurious
214 * assertion failure.
216 atomic_inc(&atomic_counter);
218 atomic_dec(&running);
221 static void test_multi_co_mutex(int threads, int seconds)
223 int i;
225 qemu_co_mutex_init(&comutex);
226 counter = 0;
227 atomic_counter = 0;
228 now_stopping = false;
230 create_aio_contexts();
231 assert(threads <= NUM_CONTEXTS);
232 running = threads;
233 for (i = 0; i < threads; i++) {
234 Coroutine *co1 = qemu_coroutine_create(test_multi_co_mutex_entry, NULL);
235 aio_co_schedule(ctx[i], co1);
238 g_usleep(seconds * 1000000);
240 atomic_mb_set(&now_stopping, true);
241 while (running > 0) {
242 g_usleep(100000);
245 join_aio_contexts();
246 g_test_message("%d iterations/second\n", counter / seconds);
247 g_assert_cmpint(counter, ==, atomic_counter);
250 /* Testing with NUM_CONTEXTS threads focuses on the queue. The mutex however
251 * is too contended (and the threads spend too much time in aio_poll)
252 * to actually stress the handoff protocol.
254 static void test_multi_co_mutex_1(void)
256 test_multi_co_mutex(NUM_CONTEXTS, 1);
259 static void test_multi_co_mutex_10(void)
261 test_multi_co_mutex(NUM_CONTEXTS, 10);
264 /* Testing with fewer threads stresses the handoff protocol too. Still, the
265 * case where the locker _can_ pick up a handoff is very rare, happening
266 * about 10 times in 1 million, so increase the runtime a bit compared to
267 * other "quick" testcases that only run for 1 second.
269 static void test_multi_co_mutex_2_3(void)
271 test_multi_co_mutex(2, 3);
274 static void test_multi_co_mutex_2_30(void)
276 test_multi_co_mutex(2, 30);
279 /* Same test with fair mutexes, for performance comparison. */
281 #ifdef CONFIG_LINUX
282 #include "qemu/futex.h"
284 /* The nodes for the mutex reside in this structure (on which we try to avoid
285 * false sharing). The head of the mutex is in the "mutex_head" variable.
287 static struct {
288 int next, locked;
289 int padding[14];
290 } nodes[NUM_CONTEXTS] __attribute__((__aligned__(64)));
292 static int mutex_head = -1;
294 static void mcs_mutex_lock(void)
296 int prev;
298 nodes[id].next = -1;
299 nodes[id].locked = 1;
300 prev = atomic_xchg(&mutex_head, id);
301 if (prev != -1) {
302 atomic_set(&nodes[prev].next, id);
303 qemu_futex_wait(&nodes[id].locked, 1);
307 static void mcs_mutex_unlock(void)
309 int next;
310 if (atomic_read(&nodes[id].next) == -1) {
311 if (atomic_read(&mutex_head) == id &&
312 atomic_cmpxchg(&mutex_head, id, -1) == id) {
313 /* Last item in the list, exit. */
314 return;
316 while (atomic_read(&nodes[id].next) == -1) {
317 /* mcs_mutex_lock did the xchg, but has not updated
318 * nodes[prev].next yet.
323 /* Wake up the next in line. */
324 next = atomic_read(&nodes[id].next);
325 nodes[next].locked = 0;
326 qemu_futex_wake(&nodes[next].locked, 1);
329 static void test_multi_fair_mutex_entry(void *opaque)
331 while (!atomic_mb_read(&now_stopping)) {
332 mcs_mutex_lock();
333 counter++;
334 mcs_mutex_unlock();
335 atomic_inc(&atomic_counter);
337 atomic_dec(&running);
340 static void test_multi_fair_mutex(int threads, int seconds)
342 int i;
344 assert(mutex_head == -1);
345 counter = 0;
346 atomic_counter = 0;
347 now_stopping = false;
349 create_aio_contexts();
350 assert(threads <= NUM_CONTEXTS);
351 running = threads;
352 for (i = 0; i < threads; i++) {
353 Coroutine *co1 = qemu_coroutine_create(test_multi_fair_mutex_entry, NULL);
354 aio_co_schedule(ctx[i], co1);
357 g_usleep(seconds * 1000000);
359 atomic_mb_set(&now_stopping, true);
360 while (running > 0) {
361 g_usleep(100000);
364 join_aio_contexts();
365 g_test_message("%d iterations/second\n", counter / seconds);
366 g_assert_cmpint(counter, ==, atomic_counter);
369 static void test_multi_fair_mutex_1(void)
371 test_multi_fair_mutex(NUM_CONTEXTS, 1);
374 static void test_multi_fair_mutex_10(void)
376 test_multi_fair_mutex(NUM_CONTEXTS, 10);
378 #endif
380 /* Same test with pthread mutexes, for performance comparison and
381 * portability. */
383 static QemuMutex mutex;
385 static void test_multi_mutex_entry(void *opaque)
387 while (!atomic_mb_read(&now_stopping)) {
388 qemu_mutex_lock(&mutex);
389 counter++;
390 qemu_mutex_unlock(&mutex);
391 atomic_inc(&atomic_counter);
393 atomic_dec(&running);
396 static void test_multi_mutex(int threads, int seconds)
398 int i;
400 qemu_mutex_init(&mutex);
401 counter = 0;
402 atomic_counter = 0;
403 now_stopping = false;
405 create_aio_contexts();
406 assert(threads <= NUM_CONTEXTS);
407 running = threads;
408 for (i = 0; i < threads; i++) {
409 Coroutine *co1 = qemu_coroutine_create(test_multi_mutex_entry, NULL);
410 aio_co_schedule(ctx[i], co1);
413 g_usleep(seconds * 1000000);
415 atomic_mb_set(&now_stopping, true);
416 while (running > 0) {
417 g_usleep(100000);
420 join_aio_contexts();
421 g_test_message("%d iterations/second\n", counter / seconds);
422 g_assert_cmpint(counter, ==, atomic_counter);
425 static void test_multi_mutex_1(void)
427 test_multi_mutex(NUM_CONTEXTS, 1);
430 static void test_multi_mutex_10(void)
432 test_multi_mutex(NUM_CONTEXTS, 10);
435 /* End of tests. */
437 int main(int argc, char **argv)
439 init_clocks(NULL);
441 g_test_init(&argc, &argv, NULL);
442 g_test_add_func("/aio/multi/lifecycle", test_lifecycle);
443 if (g_test_quick()) {
444 g_test_add_func("/aio/multi/schedule", test_multi_co_schedule_1);
445 g_test_add_func("/aio/multi/mutex/contended", test_multi_co_mutex_1);
446 g_test_add_func("/aio/multi/mutex/handoff", test_multi_co_mutex_2_3);
447 #ifdef CONFIG_LINUX
448 g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_1);
449 #endif
450 g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_1);
451 } else {
452 g_test_add_func("/aio/multi/schedule", test_multi_co_schedule_10);
453 g_test_add_func("/aio/multi/mutex/contended", test_multi_co_mutex_10);
454 g_test_add_func("/aio/multi/mutex/handoff", test_multi_co_mutex_2_30);
455 #ifdef CONFIG_LINUX
456 g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_10);
457 #endif
458 g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_10);
460 return g_test_run();