1da668aa1SThomas Huth /*
2da668aa1SThomas Huth * AioContext multithreading tests
3da668aa1SThomas Huth *
4da668aa1SThomas Huth * Copyright Red Hat, Inc. 2016
5da668aa1SThomas Huth *
6da668aa1SThomas Huth * Authors:
7da668aa1SThomas Huth * Paolo Bonzini <pbonzini@redhat.com>
8da668aa1SThomas Huth *
9da668aa1SThomas Huth * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10da668aa1SThomas Huth * See the COPYING.LIB file in the top-level directory.
11da668aa1SThomas Huth */
12da668aa1SThomas Huth
13da668aa1SThomas Huth #include "qemu/osdep.h"
14da668aa1SThomas Huth #include "block/aio.h"
15da668aa1SThomas Huth #include "qemu/coroutine.h"
16da668aa1SThomas Huth #include "qemu/thread.h"
17da668aa1SThomas Huth #include "qemu/error-report.h"
18da668aa1SThomas Huth #include "iothread.h"
19da668aa1SThomas Huth
20da668aa1SThomas Huth /* AioContext management */
21da668aa1SThomas Huth
22da668aa1SThomas Huth #define NUM_CONTEXTS 5
23da668aa1SThomas Huth
24da668aa1SThomas Huth static IOThread *threads[NUM_CONTEXTS];
25da668aa1SThomas Huth static AioContext *ctx[NUM_CONTEXTS];
26da668aa1SThomas Huth static __thread int id = -1;
27da668aa1SThomas Huth
28da668aa1SThomas Huth static QemuEvent done_event;
29da668aa1SThomas Huth
30da668aa1SThomas Huth /* Run a function synchronously on a remote iothread. */
31da668aa1SThomas Huth
32da668aa1SThomas Huth typedef struct CtxRunData {
33da668aa1SThomas Huth QEMUBHFunc *cb;
34da668aa1SThomas Huth void *arg;
35da668aa1SThomas Huth } CtxRunData;
36da668aa1SThomas Huth
ctx_run_bh_cb(void * opaque)37da668aa1SThomas Huth static void ctx_run_bh_cb(void *opaque)
38da668aa1SThomas Huth {
39da668aa1SThomas Huth CtxRunData *data = opaque;
40da668aa1SThomas Huth
41da668aa1SThomas Huth data->cb(data->arg);
42da668aa1SThomas Huth qemu_event_set(&done_event);
43da668aa1SThomas Huth }
44da668aa1SThomas Huth
ctx_run(int i,QEMUBHFunc * cb,void * opaque)45da668aa1SThomas Huth static void ctx_run(int i, QEMUBHFunc *cb, void *opaque)
46da668aa1SThomas Huth {
47da668aa1SThomas Huth CtxRunData data = {
48da668aa1SThomas Huth .cb = cb,
49da668aa1SThomas Huth .arg = opaque
50da668aa1SThomas Huth };
51da668aa1SThomas Huth
52da668aa1SThomas Huth qemu_event_reset(&done_event);
53da668aa1SThomas Huth aio_bh_schedule_oneshot(ctx[i], ctx_run_bh_cb, &data);
54da668aa1SThomas Huth qemu_event_wait(&done_event);
55da668aa1SThomas Huth }
56da668aa1SThomas Huth
57da668aa1SThomas Huth /* Starting the iothreads. */
58da668aa1SThomas Huth
set_id_cb(void * opaque)59da668aa1SThomas Huth static void set_id_cb(void *opaque)
60da668aa1SThomas Huth {
61da668aa1SThomas Huth int *i = opaque;
62da668aa1SThomas Huth
63da668aa1SThomas Huth id = *i;
64da668aa1SThomas Huth }
65da668aa1SThomas Huth
create_aio_contexts(void)66da668aa1SThomas Huth static void create_aio_contexts(void)
67da668aa1SThomas Huth {
68da668aa1SThomas Huth int i;
69da668aa1SThomas Huth
70da668aa1SThomas Huth for (i = 0; i < NUM_CONTEXTS; i++) {
71da668aa1SThomas Huth threads[i] = iothread_new();
72da668aa1SThomas Huth ctx[i] = iothread_get_aio_context(threads[i]);
73da668aa1SThomas Huth }
74da668aa1SThomas Huth
75da668aa1SThomas Huth qemu_event_init(&done_event, false);
76da668aa1SThomas Huth for (i = 0; i < NUM_CONTEXTS; i++) {
77da668aa1SThomas Huth ctx_run(i, set_id_cb, &i);
78da668aa1SThomas Huth }
79da668aa1SThomas Huth }
80da668aa1SThomas Huth
81da668aa1SThomas Huth /* Stopping the iothreads. */
82da668aa1SThomas Huth
join_aio_contexts(void)83da668aa1SThomas Huth static void join_aio_contexts(void)
84da668aa1SThomas Huth {
85da668aa1SThomas Huth int i;
86da668aa1SThomas Huth
87da668aa1SThomas Huth for (i = 0; i < NUM_CONTEXTS; i++) {
88da668aa1SThomas Huth aio_context_ref(ctx[i]);
89da668aa1SThomas Huth }
90da668aa1SThomas Huth for (i = 0; i < NUM_CONTEXTS; i++) {
91da668aa1SThomas Huth iothread_join(threads[i]);
92da668aa1SThomas Huth }
93da668aa1SThomas Huth for (i = 0; i < NUM_CONTEXTS; i++) {
94da668aa1SThomas Huth aio_context_unref(ctx[i]);
95da668aa1SThomas Huth }
96da668aa1SThomas Huth qemu_event_destroy(&done_event);
97da668aa1SThomas Huth }
98da668aa1SThomas Huth
99da668aa1SThomas Huth /* Basic test for the stuff above. */
100da668aa1SThomas Huth
test_lifecycle(void)101da668aa1SThomas Huth static void test_lifecycle(void)
102da668aa1SThomas Huth {
103da668aa1SThomas Huth create_aio_contexts();
104da668aa1SThomas Huth join_aio_contexts();
105da668aa1SThomas Huth }
106da668aa1SThomas Huth
107da668aa1SThomas Huth /* aio_co_schedule test. */
108da668aa1SThomas Huth
109da668aa1SThomas Huth static Coroutine *to_schedule[NUM_CONTEXTS];
110355635c0SPaolo Bonzini static bool stop[NUM_CONTEXTS];
111da668aa1SThomas Huth
112da668aa1SThomas Huth static int count_retry;
113da668aa1SThomas Huth static int count_here;
114da668aa1SThomas Huth static int count_other;
115da668aa1SThomas Huth
schedule_next(int n)116da668aa1SThomas Huth static bool schedule_next(int n)
117da668aa1SThomas Huth {
118da668aa1SThomas Huth Coroutine *co;
119da668aa1SThomas Huth
120da668aa1SThomas Huth co = qatomic_xchg(&to_schedule[n], NULL);
121da668aa1SThomas Huth if (!co) {
122da668aa1SThomas Huth qatomic_inc(&count_retry);
123da668aa1SThomas Huth return false;
124da668aa1SThomas Huth }
125da668aa1SThomas Huth
126da668aa1SThomas Huth if (n == id) {
127da668aa1SThomas Huth qatomic_inc(&count_here);
128da668aa1SThomas Huth } else {
129da668aa1SThomas Huth qatomic_inc(&count_other);
130da668aa1SThomas Huth }
131da668aa1SThomas Huth
132da668aa1SThomas Huth aio_co_schedule(ctx[n], co);
133da668aa1SThomas Huth return true;
134da668aa1SThomas Huth }
135da668aa1SThomas Huth
finish_cb(void * opaque)136da668aa1SThomas Huth static void finish_cb(void *opaque)
137da668aa1SThomas Huth {
138355635c0SPaolo Bonzini stop[id] = true;
139da668aa1SThomas Huth schedule_next(id);
140da668aa1SThomas Huth }
141da668aa1SThomas Huth
test_multi_co_schedule_entry(void * opaque)142da668aa1SThomas Huth static coroutine_fn void test_multi_co_schedule_entry(void *opaque)
143da668aa1SThomas Huth {
144da668aa1SThomas Huth g_assert(to_schedule[id] == NULL);
145da668aa1SThomas Huth
146355635c0SPaolo Bonzini /*
147355635c0SPaolo Bonzini * The next iteration will set to_schedule[id] again, but once finish_cb
148355635c0SPaolo Bonzini * is scheduled there is no guarantee that it will actually be woken up,
149355635c0SPaolo Bonzini * so at that point it must not go to sleep.
150355635c0SPaolo Bonzini */
151355635c0SPaolo Bonzini while (!stop[id]) {
152da668aa1SThomas Huth int n;
153da668aa1SThomas Huth
154da668aa1SThomas Huth n = g_test_rand_int_range(0, NUM_CONTEXTS);
155da668aa1SThomas Huth schedule_next(n);
156da668aa1SThomas Huth
157*06831001SPaolo Bonzini qatomic_set_mb(&to_schedule[id], qemu_coroutine_self());
158355635c0SPaolo Bonzini /* finish_cb can run here. */
159da668aa1SThomas Huth qemu_coroutine_yield();
160da668aa1SThomas Huth g_assert(to_schedule[id] == NULL);
161da668aa1SThomas Huth }
162da668aa1SThomas Huth }
163da668aa1SThomas Huth
164da668aa1SThomas Huth
test_multi_co_schedule(int seconds)165da668aa1SThomas Huth static void test_multi_co_schedule(int seconds)
166da668aa1SThomas Huth {
167da668aa1SThomas Huth int i;
168da668aa1SThomas Huth
169da668aa1SThomas Huth count_here = count_other = count_retry = 0;
170da668aa1SThomas Huth
171da668aa1SThomas Huth create_aio_contexts();
172da668aa1SThomas Huth for (i = 0; i < NUM_CONTEXTS; i++) {
173da668aa1SThomas Huth Coroutine *co1 = qemu_coroutine_create(test_multi_co_schedule_entry, NULL);
174da668aa1SThomas Huth aio_co_schedule(ctx[i], co1);
175da668aa1SThomas Huth }
176da668aa1SThomas Huth
177da668aa1SThomas Huth g_usleep(seconds * 1000000);
178da668aa1SThomas Huth
179355635c0SPaolo Bonzini /* Guarantee that each AioContext is woken up from its last wait. */
180da668aa1SThomas Huth for (i = 0; i < NUM_CONTEXTS; i++) {
181da668aa1SThomas Huth ctx_run(i, finish_cb, NULL);
182355635c0SPaolo Bonzini g_assert(to_schedule[i] == NULL);
183da668aa1SThomas Huth }
184da668aa1SThomas Huth
185da668aa1SThomas Huth join_aio_contexts();
186da668aa1SThomas Huth g_test_message("scheduled %d, queued %d, retry %d, total %d",
187da668aa1SThomas Huth count_other, count_here, count_retry,
188da668aa1SThomas Huth count_here + count_other + count_retry);
189da668aa1SThomas Huth }
190da668aa1SThomas Huth
test_multi_co_schedule_1(void)191da668aa1SThomas Huth static void test_multi_co_schedule_1(void)
192da668aa1SThomas Huth {
193da668aa1SThomas Huth test_multi_co_schedule(1);
194da668aa1SThomas Huth }
195da668aa1SThomas Huth
test_multi_co_schedule_10(void)196da668aa1SThomas Huth static void test_multi_co_schedule_10(void)
197da668aa1SThomas Huth {
198da668aa1SThomas Huth test_multi_co_schedule(10);
199da668aa1SThomas Huth }
200da668aa1SThomas Huth
201da668aa1SThomas Huth /* CoMutex thread-safety. */
202da668aa1SThomas Huth
203da668aa1SThomas Huth static uint32_t atomic_counter;
204da668aa1SThomas Huth static uint32_t running;
205da668aa1SThomas Huth static uint32_t counter;
206da668aa1SThomas Huth static CoMutex comutex;
207355635c0SPaolo Bonzini static bool now_stopping;
208da668aa1SThomas Huth
test_multi_co_mutex_entry(void * opaque)209da668aa1SThomas Huth static void coroutine_fn test_multi_co_mutex_entry(void *opaque)
210da668aa1SThomas Huth {
2114f7335e2SPaolo Bonzini while (!qatomic_read(&now_stopping)) {
212da668aa1SThomas Huth qemu_co_mutex_lock(&comutex);
213da668aa1SThomas Huth counter++;
214da668aa1SThomas Huth qemu_co_mutex_unlock(&comutex);
215da668aa1SThomas Huth
216da668aa1SThomas Huth /* Increase atomic_counter *after* releasing the mutex. Otherwise
217da668aa1SThomas Huth * there is a chance (it happens about 1 in 3 runs) that the iothread
218da668aa1SThomas Huth * exits before the coroutine is woken up, causing a spurious
219da668aa1SThomas Huth * assertion failure.
220da668aa1SThomas Huth */
221da668aa1SThomas Huth qatomic_inc(&atomic_counter);
222da668aa1SThomas Huth }
223da668aa1SThomas Huth qatomic_dec(&running);
224da668aa1SThomas Huth }
225da668aa1SThomas Huth
test_multi_co_mutex(int threads,int seconds)226da668aa1SThomas Huth static void test_multi_co_mutex(int threads, int seconds)
227da668aa1SThomas Huth {
228da668aa1SThomas Huth int i;
229da668aa1SThomas Huth
230da668aa1SThomas Huth qemu_co_mutex_init(&comutex);
231da668aa1SThomas Huth counter = 0;
232da668aa1SThomas Huth atomic_counter = 0;
233da668aa1SThomas Huth now_stopping = false;
234da668aa1SThomas Huth
235da668aa1SThomas Huth create_aio_contexts();
236da668aa1SThomas Huth assert(threads <= NUM_CONTEXTS);
237da668aa1SThomas Huth running = threads;
238da668aa1SThomas Huth for (i = 0; i < threads; i++) {
239da668aa1SThomas Huth Coroutine *co1 = qemu_coroutine_create(test_multi_co_mutex_entry, NULL);
240da668aa1SThomas Huth aio_co_schedule(ctx[i], co1);
241da668aa1SThomas Huth }
242da668aa1SThomas Huth
243da668aa1SThomas Huth g_usleep(seconds * 1000000);
244da668aa1SThomas Huth
2454f7335e2SPaolo Bonzini qatomic_set(&now_stopping, true);
246da668aa1SThomas Huth while (running > 0) {
247da668aa1SThomas Huth g_usleep(100000);
248da668aa1SThomas Huth }
249da668aa1SThomas Huth
250da668aa1SThomas Huth join_aio_contexts();
251da668aa1SThomas Huth g_test_message("%d iterations/second", counter / seconds);
252da668aa1SThomas Huth g_assert_cmpint(counter, ==, atomic_counter);
253da668aa1SThomas Huth }
254da668aa1SThomas Huth
255da668aa1SThomas Huth /* Testing with NUM_CONTEXTS threads focuses on the queue. The mutex however
256da668aa1SThomas Huth * is too contended (and the threads spend too much time in aio_poll)
257da668aa1SThomas Huth * to actually stress the handoff protocol.
258da668aa1SThomas Huth */
test_multi_co_mutex_1(void)259da668aa1SThomas Huth static void test_multi_co_mutex_1(void)
260da668aa1SThomas Huth {
261da668aa1SThomas Huth test_multi_co_mutex(NUM_CONTEXTS, 1);
262da668aa1SThomas Huth }
263da668aa1SThomas Huth
test_multi_co_mutex_10(void)264da668aa1SThomas Huth static void test_multi_co_mutex_10(void)
265da668aa1SThomas Huth {
266da668aa1SThomas Huth test_multi_co_mutex(NUM_CONTEXTS, 10);
267da668aa1SThomas Huth }
268da668aa1SThomas Huth
269da668aa1SThomas Huth /* Testing with fewer threads stresses the handoff protocol too. Still, the
270da668aa1SThomas Huth * case where the locker _can_ pick up a handoff is very rare, happening
271da668aa1SThomas Huth * about 10 times in 1 million, so increase the runtime a bit compared to
272da668aa1SThomas Huth * other "quick" testcases that only run for 1 second.
273da668aa1SThomas Huth */
test_multi_co_mutex_2_3(void)274da668aa1SThomas Huth static void test_multi_co_mutex_2_3(void)
275da668aa1SThomas Huth {
276da668aa1SThomas Huth test_multi_co_mutex(2, 3);
277da668aa1SThomas Huth }
278da668aa1SThomas Huth
test_multi_co_mutex_2_30(void)279da668aa1SThomas Huth static void test_multi_co_mutex_2_30(void)
280da668aa1SThomas Huth {
281da668aa1SThomas Huth test_multi_co_mutex(2, 30);
282da668aa1SThomas Huth }
283da668aa1SThomas Huth
284da668aa1SThomas Huth /* Same test with fair mutexes, for performance comparison. */
285da668aa1SThomas Huth
286da668aa1SThomas Huth #ifdef CONFIG_LINUX
287da668aa1SThomas Huth #include "qemu/futex.h"
288da668aa1SThomas Huth
289da668aa1SThomas Huth /* The nodes for the mutex reside in this structure (on which we try to avoid
290da668aa1SThomas Huth * false sharing). The head of the mutex is in the "mutex_head" variable.
291da668aa1SThomas Huth */
292da668aa1SThomas Huth static struct {
293da668aa1SThomas Huth int next, locked;
294da668aa1SThomas Huth int padding[14];
295da668aa1SThomas Huth } nodes[NUM_CONTEXTS] __attribute__((__aligned__(64)));
296da668aa1SThomas Huth
297da668aa1SThomas Huth static int mutex_head = -1;
298da668aa1SThomas Huth
mcs_mutex_lock(void)299da668aa1SThomas Huth static void mcs_mutex_lock(void)
300da668aa1SThomas Huth {
301da668aa1SThomas Huth int prev;
302da668aa1SThomas Huth
303da668aa1SThomas Huth nodes[id].next = -1;
304da668aa1SThomas Huth nodes[id].locked = 1;
305da668aa1SThomas Huth prev = qatomic_xchg(&mutex_head, id);
306da668aa1SThomas Huth if (prev != -1) {
307da668aa1SThomas Huth qatomic_set(&nodes[prev].next, id);
308da668aa1SThomas Huth qemu_futex_wait(&nodes[id].locked, 1);
309da668aa1SThomas Huth }
310da668aa1SThomas Huth }
311da668aa1SThomas Huth
mcs_mutex_unlock(void)312da668aa1SThomas Huth static void mcs_mutex_unlock(void)
313da668aa1SThomas Huth {
314da668aa1SThomas Huth int next;
315da668aa1SThomas Huth if (qatomic_read(&nodes[id].next) == -1) {
316da668aa1SThomas Huth if (qatomic_read(&mutex_head) == id &&
317da668aa1SThomas Huth qatomic_cmpxchg(&mutex_head, id, -1) == id) {
318da668aa1SThomas Huth /* Last item in the list, exit. */
319da668aa1SThomas Huth return;
320da668aa1SThomas Huth }
321da668aa1SThomas Huth while (qatomic_read(&nodes[id].next) == -1) {
322da668aa1SThomas Huth /* mcs_mutex_lock did the xchg, but has not updated
323da668aa1SThomas Huth * nodes[prev].next yet.
324da668aa1SThomas Huth */
325da668aa1SThomas Huth }
326da668aa1SThomas Huth }
327da668aa1SThomas Huth
328da668aa1SThomas Huth /* Wake up the next in line. */
329da668aa1SThomas Huth next = qatomic_read(&nodes[id].next);
330da668aa1SThomas Huth nodes[next].locked = 0;
331da668aa1SThomas Huth qemu_futex_wake(&nodes[next].locked, 1);
332da668aa1SThomas Huth }
333da668aa1SThomas Huth
test_multi_fair_mutex_entry(void * opaque)334da668aa1SThomas Huth static void test_multi_fair_mutex_entry(void *opaque)
335da668aa1SThomas Huth {
3364f7335e2SPaolo Bonzini while (!qatomic_read(&now_stopping)) {
337da668aa1SThomas Huth mcs_mutex_lock();
338da668aa1SThomas Huth counter++;
339da668aa1SThomas Huth mcs_mutex_unlock();
340da668aa1SThomas Huth qatomic_inc(&atomic_counter);
341da668aa1SThomas Huth }
342da668aa1SThomas Huth qatomic_dec(&running);
343da668aa1SThomas Huth }
344da668aa1SThomas Huth
test_multi_fair_mutex(int threads,int seconds)345da668aa1SThomas Huth static void test_multi_fair_mutex(int threads, int seconds)
346da668aa1SThomas Huth {
347da668aa1SThomas Huth int i;
348da668aa1SThomas Huth
349da668aa1SThomas Huth assert(mutex_head == -1);
350da668aa1SThomas Huth counter = 0;
351da668aa1SThomas Huth atomic_counter = 0;
352da668aa1SThomas Huth now_stopping = false;
353da668aa1SThomas Huth
354da668aa1SThomas Huth create_aio_contexts();
355da668aa1SThomas Huth assert(threads <= NUM_CONTEXTS);
356da668aa1SThomas Huth running = threads;
357da668aa1SThomas Huth for (i = 0; i < threads; i++) {
358da668aa1SThomas Huth Coroutine *co1 = qemu_coroutine_create(test_multi_fair_mutex_entry, NULL);
359da668aa1SThomas Huth aio_co_schedule(ctx[i], co1);
360da668aa1SThomas Huth }
361da668aa1SThomas Huth
362da668aa1SThomas Huth g_usleep(seconds * 1000000);
363da668aa1SThomas Huth
3644f7335e2SPaolo Bonzini qatomic_set(&now_stopping, true);
365da668aa1SThomas Huth while (running > 0) {
366da668aa1SThomas Huth g_usleep(100000);
367da668aa1SThomas Huth }
368da668aa1SThomas Huth
369da668aa1SThomas Huth join_aio_contexts();
370da668aa1SThomas Huth g_test_message("%d iterations/second", counter / seconds);
371da668aa1SThomas Huth g_assert_cmpint(counter, ==, atomic_counter);
372da668aa1SThomas Huth }
373da668aa1SThomas Huth
test_multi_fair_mutex_1(void)374da668aa1SThomas Huth static void test_multi_fair_mutex_1(void)
375da668aa1SThomas Huth {
376da668aa1SThomas Huth test_multi_fair_mutex(NUM_CONTEXTS, 1);
377da668aa1SThomas Huth }
378da668aa1SThomas Huth
test_multi_fair_mutex_10(void)379da668aa1SThomas Huth static void test_multi_fair_mutex_10(void)
380da668aa1SThomas Huth {
381da668aa1SThomas Huth test_multi_fair_mutex(NUM_CONTEXTS, 10);
382da668aa1SThomas Huth }
383da668aa1SThomas Huth #endif
384da668aa1SThomas Huth
385da668aa1SThomas Huth /* Same test with pthread mutexes, for performance comparison and
386da668aa1SThomas Huth * portability. */
387da668aa1SThomas Huth
388da668aa1SThomas Huth static QemuMutex mutex;
389da668aa1SThomas Huth
test_multi_mutex_entry(void * opaque)390da668aa1SThomas Huth static void test_multi_mutex_entry(void *opaque)
391da668aa1SThomas Huth {
3924f7335e2SPaolo Bonzini while (!qatomic_read(&now_stopping)) {
393da668aa1SThomas Huth qemu_mutex_lock(&mutex);
394da668aa1SThomas Huth counter++;
395da668aa1SThomas Huth qemu_mutex_unlock(&mutex);
396da668aa1SThomas Huth qatomic_inc(&atomic_counter);
397da668aa1SThomas Huth }
398da668aa1SThomas Huth qatomic_dec(&running);
399da668aa1SThomas Huth }
400da668aa1SThomas Huth
test_multi_mutex(int threads,int seconds)401da668aa1SThomas Huth static void test_multi_mutex(int threads, int seconds)
402da668aa1SThomas Huth {
403da668aa1SThomas Huth int i;
404da668aa1SThomas Huth
405da668aa1SThomas Huth qemu_mutex_init(&mutex);
406da668aa1SThomas Huth counter = 0;
407da668aa1SThomas Huth atomic_counter = 0;
408da668aa1SThomas Huth now_stopping = false;
409da668aa1SThomas Huth
410da668aa1SThomas Huth create_aio_contexts();
411da668aa1SThomas Huth assert(threads <= NUM_CONTEXTS);
412da668aa1SThomas Huth running = threads;
413da668aa1SThomas Huth for (i = 0; i < threads; i++) {
414da668aa1SThomas Huth Coroutine *co1 = qemu_coroutine_create(test_multi_mutex_entry, NULL);
415da668aa1SThomas Huth aio_co_schedule(ctx[i], co1);
416da668aa1SThomas Huth }
417da668aa1SThomas Huth
418da668aa1SThomas Huth g_usleep(seconds * 1000000);
419da668aa1SThomas Huth
4204f7335e2SPaolo Bonzini qatomic_set(&now_stopping, true);
421da668aa1SThomas Huth while (running > 0) {
422da668aa1SThomas Huth g_usleep(100000);
423da668aa1SThomas Huth }
424da668aa1SThomas Huth
425da668aa1SThomas Huth join_aio_contexts();
426da668aa1SThomas Huth g_test_message("%d iterations/second", counter / seconds);
427da668aa1SThomas Huth g_assert_cmpint(counter, ==, atomic_counter);
428da668aa1SThomas Huth }
429da668aa1SThomas Huth
test_multi_mutex_1(void)430da668aa1SThomas Huth static void test_multi_mutex_1(void)
431da668aa1SThomas Huth {
432da668aa1SThomas Huth test_multi_mutex(NUM_CONTEXTS, 1);
433da668aa1SThomas Huth }
434da668aa1SThomas Huth
test_multi_mutex_10(void)435da668aa1SThomas Huth static void test_multi_mutex_10(void)
436da668aa1SThomas Huth {
437da668aa1SThomas Huth test_multi_mutex(NUM_CONTEXTS, 10);
438da668aa1SThomas Huth }
439da668aa1SThomas Huth
440da668aa1SThomas Huth /* End of tests. */
441da668aa1SThomas Huth
main(int argc,char ** argv)442da668aa1SThomas Huth int main(int argc, char **argv)
443da668aa1SThomas Huth {
444da668aa1SThomas Huth init_clocks(NULL);
445da668aa1SThomas Huth
446da668aa1SThomas Huth g_test_init(&argc, &argv, NULL);
447da668aa1SThomas Huth g_test_add_func("/aio/multi/lifecycle", test_lifecycle);
448da668aa1SThomas Huth if (g_test_quick()) {
449da668aa1SThomas Huth g_test_add_func("/aio/multi/schedule", test_multi_co_schedule_1);
450da668aa1SThomas Huth g_test_add_func("/aio/multi/mutex/contended", test_multi_co_mutex_1);
451da668aa1SThomas Huth g_test_add_func("/aio/multi/mutex/handoff", test_multi_co_mutex_2_3);
452da668aa1SThomas Huth #ifdef CONFIG_LINUX
453da668aa1SThomas Huth g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_1);
454da668aa1SThomas Huth #endif
455da668aa1SThomas Huth g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_1);
456da668aa1SThomas Huth } else {
457da668aa1SThomas Huth g_test_add_func("/aio/multi/schedule", test_multi_co_schedule_10);
458da668aa1SThomas Huth g_test_add_func("/aio/multi/mutex/contended", test_multi_co_mutex_10);
459da668aa1SThomas Huth g_test_add_func("/aio/multi/mutex/handoff", test_multi_co_mutex_2_30);
460da668aa1SThomas Huth #ifdef CONFIG_LINUX
461da668aa1SThomas Huth g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_10);
462da668aa1SThomas Huth #endif
463da668aa1SThomas Huth g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_10);
464da668aa1SThomas Huth }
465da668aa1SThomas Huth return g_test_run();
466da668aa1SThomas Huth }
467