2016-02-08 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-11-23 19:13:24 +04:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/aio.h"
|
|
|
|
#include "block/thread-pool.h"
|
|
|
|
#include "block/block.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2013-08-21 19:02:49 +04:00
|
|
|
#include "qemu/timer.h"
|
2014-09-18 15:30:49 +04:00
|
|
|
#include "qemu/error-report.h"
|
2017-02-13 16:52:21 +03:00
|
|
|
#include "qemu/main-loop.h"
|
2012-11-23 19:13:24 +04:00
|
|
|
|
2013-03-07 16:41:49 +04:00
|
|
|
static AioContext *ctx;
|
|
|
|
static ThreadPool *pool;
|
2012-11-23 19:13:24 +04:00
|
|
|
static int active;
|
|
|
|
|
|
|
|
typedef struct {
|
2014-10-07 15:59:14 +04:00
|
|
|
BlockAIOCB *aiocb;
|
2012-11-23 19:13:24 +04:00
|
|
|
int n;
|
|
|
|
int ret;
|
|
|
|
} WorkerTestData;
|
|
|
|
|
|
|
|
static int worker_cb(void *opaque)
|
|
|
|
{
|
|
|
|
WorkerTestData *data = opaque;
|
2013-05-13 15:29:47 +04:00
|
|
|
return atomic_fetch_inc(&data->n);
|
2012-11-23 19:13:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int long_cb(void *opaque)
|
|
|
|
{
|
|
|
|
WorkerTestData *data = opaque;
|
2019-03-14 21:20:07 +03:00
|
|
|
if (atomic_cmpxchg(&data->n, 0, 1) == 0) {
|
|
|
|
g_usleep(2000000);
|
|
|
|
atomic_or(&data->n, 2);
|
|
|
|
}
|
2012-11-23 19:13:24 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void done_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
WorkerTestData *data = opaque;
|
2014-09-11 09:41:12 +04:00
|
|
|
g_assert(data->ret == -EINPROGRESS || data->ret == -ECANCELED);
|
2012-11-23 19:13:24 +04:00
|
|
|
data->ret = ret;
|
|
|
|
data->aiocb = NULL;
|
|
|
|
|
|
|
|
/* Callbacks are serialized, so no need to use atomic ops. */
|
|
|
|
active--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_submit(void)
|
|
|
|
{
|
|
|
|
WorkerTestData data = { .n = 0 };
|
2013-03-07 16:41:49 +04:00
|
|
|
thread_pool_submit(pool, worker_cb, &data);
|
2013-04-16 19:49:42 +04:00
|
|
|
while (data.n == 0) {
|
|
|
|
aio_poll(ctx, true);
|
|
|
|
}
|
2012-11-23 19:13:24 +04:00
|
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_submit_aio(void)
|
|
|
|
{
|
|
|
|
WorkerTestData data = { .n = 0, .ret = -EINPROGRESS };
|
2013-03-07 16:41:49 +04:00
|
|
|
data.aiocb = thread_pool_submit_aio(pool, worker_cb, &data,
|
|
|
|
done_cb, &data);
|
2012-11-23 19:13:24 +04:00
|
|
|
|
|
|
|
/* The callbacks are not called until after the first wait. */
|
|
|
|
active = 1;
|
|
|
|
g_assert_cmpint(data.ret, ==, -EINPROGRESS);
|
2013-04-16 19:49:42 +04:00
|
|
|
while (data.ret == -EINPROGRESS) {
|
|
|
|
aio_poll(ctx, true);
|
|
|
|
}
|
2012-11-23 19:13:24 +04:00
|
|
|
g_assert_cmpint(active, ==, 0);
|
|
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
g_assert_cmpint(data.ret, ==, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void co_test_cb(void *opaque)
|
|
|
|
{
|
|
|
|
WorkerTestData *data = opaque;
|
|
|
|
|
|
|
|
active = 1;
|
|
|
|
data->n = 0;
|
|
|
|
data->ret = -EINPROGRESS;
|
2013-03-07 16:41:49 +04:00
|
|
|
thread_pool_submit_co(pool, worker_cb, data);
|
2012-11-23 19:13:24 +04:00
|
|
|
|
|
|
|
/* The test continues in test_submit_co, after qemu_coroutine_enter... */
|
|
|
|
|
|
|
|
g_assert_cmpint(data->n, ==, 1);
|
|
|
|
data->ret = 0;
|
|
|
|
active--;
|
|
|
|
|
2014-07-07 17:18:02 +04:00
|
|
|
/* The test continues in test_submit_co, after aio_poll... */
|
2012-11-23 19:13:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_submit_co(void)
|
|
|
|
{
|
|
|
|
WorkerTestData data;
|
coroutine: move entry argument to qemu_coroutine_create
In practice the entry argument is always known at creation time, and
it is confusing that sometimes qemu_coroutine_enter is used with a
non-NULL argument to re-enter a coroutine (this happens in
block/sheepdog.c and tests/test-coroutine.c). So pass the opaque value
at creation time, for consistency with e.g. aio_bh_new.
Mostly done with the following semantic patch:
@ entry1 @
expression entry, arg, co;
@@
- co = qemu_coroutine_create(entry);
+ co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry2 @
expression entry, arg;
identifier co;
@@
- Coroutine *co = qemu_coroutine_create(entry);
+ Coroutine *co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry3 @
expression entry, arg;
@@
- qemu_coroutine_enter(qemu_coroutine_create(entry), arg);
+ qemu_coroutine_enter(qemu_coroutine_create(entry, arg));
@ reentry @
expression co;
@@
- qemu_coroutine_enter(co, NULL);
+ qemu_coroutine_enter(co);
except for the aforementioned few places where the semantic patch
stumbled (as expected) and for test_co_queue, which would otherwise
produce an uninitialized variable warning.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2016-07-04 20:10:01 +03:00
|
|
|
Coroutine *co = qemu_coroutine_create(co_test_cb, &data);
|
2012-11-23 19:13:24 +04:00
|
|
|
|
coroutine: move entry argument to qemu_coroutine_create
In practice the entry argument is always known at creation time, and
it is confusing that sometimes qemu_coroutine_enter is used with a
non-NULL argument to re-enter a coroutine (this happens in
block/sheepdog.c and tests/test-coroutine.c). So pass the opaque value
at creation time, for consistency with e.g. aio_bh_new.
Mostly done with the following semantic patch:
@ entry1 @
expression entry, arg, co;
@@
- co = qemu_coroutine_create(entry);
+ co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry2 @
expression entry, arg;
identifier co;
@@
- Coroutine *co = qemu_coroutine_create(entry);
+ Coroutine *co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry3 @
expression entry, arg;
@@
- qemu_coroutine_enter(qemu_coroutine_create(entry), arg);
+ qemu_coroutine_enter(qemu_coroutine_create(entry, arg));
@ reentry @
expression co;
@@
- qemu_coroutine_enter(co, NULL);
+ qemu_coroutine_enter(co);
except for the aforementioned few places where the semantic patch
stumbled (as expected) and for test_co_queue, which would otherwise
produce an uninitialized variable warning.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2016-07-04 20:10:01 +03:00
|
|
|
qemu_coroutine_enter(co);
|
2012-11-23 19:13:24 +04:00
|
|
|
|
|
|
|
/* Back here once the worker has started. */
|
|
|
|
|
|
|
|
g_assert_cmpint(active, ==, 1);
|
|
|
|
g_assert_cmpint(data.ret, ==, -EINPROGRESS);
|
|
|
|
|
2014-07-07 17:18:02 +04:00
|
|
|
/* aio_poll will execute the rest of the coroutine. */
|
2012-11-23 19:13:24 +04:00
|
|
|
|
2013-04-16 19:49:42 +04:00
|
|
|
while (data.ret == -EINPROGRESS) {
|
|
|
|
aio_poll(ctx, true);
|
|
|
|
}
|
2012-11-23 19:13:24 +04:00
|
|
|
|
|
|
|
/* Back here after the coroutine has finished. */
|
|
|
|
|
|
|
|
g_assert_cmpint(active, ==, 0);
|
|
|
|
g_assert_cmpint(data.ret, ==, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_submit_many(void)
|
|
|
|
{
|
|
|
|
WorkerTestData data[100];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Start more work items than there will be threads. */
|
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
data[i].n = 0;
|
|
|
|
data[i].ret = -EINPROGRESS;
|
2013-03-07 16:41:49 +04:00
|
|
|
thread_pool_submit_aio(pool, worker_cb, &data[i], done_cb, &data[i]);
|
2012-11-23 19:13:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
active = 100;
|
|
|
|
while (active > 0) {
|
2013-03-07 16:41:49 +04:00
|
|
|
aio_poll(ctx, true);
|
2012-11-23 19:13:24 +04:00
|
|
|
}
|
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
g_assert_cmpint(data[i].n, ==, 1);
|
|
|
|
g_assert_cmpint(data[i].ret, ==, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:41:12 +04:00
|
|
|
static void do_test_cancel(bool sync)
|
2012-11-23 19:13:24 +04:00
|
|
|
{
|
|
|
|
WorkerTestData data[100];
|
2012-11-27 12:51:48 +04:00
|
|
|
int num_canceled;
|
2012-11-23 19:13:24 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Start more work items than there will be threads, to ensure
|
|
|
|
* the pool is full.
|
|
|
|
*/
|
|
|
|
test_submit_many();
|
|
|
|
|
|
|
|
/* Start long running jobs, to ensure we can cancel some. */
|
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
data[i].n = 0;
|
|
|
|
data[i].ret = -EINPROGRESS;
|
2013-03-07 16:41:49 +04:00
|
|
|
data[i].aiocb = thread_pool_submit_aio(pool, long_cb, &data[i],
|
2012-11-23 19:13:24 +04:00
|
|
|
done_cb, &data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Starting the threads may be left to a bottom half. Let it
|
|
|
|
* run, but do not waste too much time...
|
|
|
|
*/
|
|
|
|
active = 100;
|
2013-03-07 16:41:49 +04:00
|
|
|
aio_notify(ctx);
|
|
|
|
aio_poll(ctx, false);
|
2012-11-23 19:13:24 +04:00
|
|
|
|
|
|
|
/* Wait some time for the threads to start, with some sanity
|
|
|
|
* testing on the behavior of the scheduler...
|
|
|
|
*/
|
|
|
|
g_assert_cmpint(active, ==, 100);
|
|
|
|
g_usleep(1000000);
|
|
|
|
g_assert_cmpint(active, >, 50);
|
|
|
|
|
|
|
|
/* Cancel the jobs that haven't been started yet. */
|
2012-11-27 12:51:48 +04:00
|
|
|
num_canceled = 0;
|
2012-11-23 19:13:24 +04:00
|
|
|
for (i = 0; i < 100; i++) {
|
2019-03-14 21:20:07 +03:00
|
|
|
if (atomic_cmpxchg(&data[i].n, 0, 4) == 0) {
|
2012-11-23 19:13:24 +04:00
|
|
|
data[i].ret = -ECANCELED;
|
2014-09-11 09:41:12 +04:00
|
|
|
if (sync) {
|
|
|
|
bdrv_aio_cancel(data[i].aiocb);
|
|
|
|
} else {
|
|
|
|
bdrv_aio_cancel_async(data[i].aiocb);
|
|
|
|
}
|
2012-11-27 12:51:48 +04:00
|
|
|
num_canceled++;
|
2012-11-23 19:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
2012-11-27 12:51:48 +04:00
|
|
|
g_assert_cmpint(active, >, 0);
|
|
|
|
g_assert_cmpint(num_canceled, <, 100);
|
2012-11-23 19:13:24 +04:00
|
|
|
|
|
|
|
for (i = 0; i < 100; i++) {
|
2019-03-14 21:20:07 +03:00
|
|
|
if (data[i].aiocb && atomic_read(&data[i].n) < 4) {
|
2014-09-11 09:41:12 +04:00
|
|
|
if (sync) {
|
|
|
|
/* Canceling the others will be a blocking operation. */
|
|
|
|
bdrv_aio_cancel(data[i].aiocb);
|
|
|
|
} else {
|
|
|
|
bdrv_aio_cancel_async(data[i].aiocb);
|
|
|
|
}
|
2012-11-23 19:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish execution and execute any remaining callbacks. */
|
2013-04-16 19:49:42 +04:00
|
|
|
while (active > 0) {
|
|
|
|
aio_poll(ctx, true);
|
|
|
|
}
|
2012-11-23 19:13:24 +04:00
|
|
|
g_assert_cmpint(active, ==, 0);
|
|
|
|
for (i = 0; i < 100; i++) {
|
2019-03-14 21:20:07 +03:00
|
|
|
g_assert(data[i].aiocb == NULL);
|
|
|
|
switch (data[i].n) {
|
|
|
|
case 0:
|
|
|
|
fprintf(stderr, "Callback not canceled but never started?\n");
|
|
|
|
abort();
|
|
|
|
case 3:
|
|
|
|
/* Couldn't be canceled asynchronously, must have completed. */
|
|
|
|
g_assert_cmpint(data[i].ret, ==, 0);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
/* Could be canceled asynchronously, never started. */
|
2012-11-23 19:13:24 +04:00
|
|
|
g_assert_cmpint(data[i].ret, ==, -ECANCELED);
|
2019-03-14 21:20:07 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Callback aborted while running?\n");
|
|
|
|
abort();
|
2012-11-23 19:13:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:41:12 +04:00
|
|
|
static void test_cancel(void)
|
|
|
|
{
|
|
|
|
do_test_cancel(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cancel_async(void)
|
|
|
|
{
|
|
|
|
do_test_cancel(false);
|
|
|
|
}
|
|
|
|
|
2012-11-23 19:13:24 +04:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-02-13 16:52:21 +03:00
|
|
|
qemu_init_main_loop(&error_abort);
|
|
|
|
ctx = qemu_get_current_aio_context();
|
2013-03-07 16:41:49 +04:00
|
|
|
pool = aio_get_thread_pool(ctx);
|
2012-11-23 19:13:24 +04:00
|
|
|
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
g_test_add_func("/thread-pool/submit", test_submit);
|
|
|
|
g_test_add_func("/thread-pool/submit-aio", test_submit_aio);
|
|
|
|
g_test_add_func("/thread-pool/submit-co", test_submit_co);
|
|
|
|
g_test_add_func("/thread-pool/submit-many", test_submit_many);
|
|
|
|
g_test_add_func("/thread-pool/cancel", test_cancel);
|
2014-09-11 09:41:12 +04:00
|
|
|
g_test_add_func("/thread-pool/cancel-async", test_cancel_async);
|
2013-03-07 16:41:49 +04:00
|
|
|
|
2018-03-23 17:32:02 +03:00
|
|
|
return g_test_run();
|
2012-11-23 19:13:24 +04:00
|
|
|
}
|