Re: [Qemu-block] [PATCH 02/14] test-bdrv-drain: Drain with block jobs in an I/O thread

2018-09-11 Thread Fam Zheng
On Fri, 09/07 18:15, Kevin Wolf wrote:
> This extends the existing drain test with a block job to include
> variants where the block job runs in a different AioContext.
> 
> Signed-off-by: Kevin Wolf 

Reviewed-by: Fam Zheng 




[Qemu-block] [PATCH 02/14] test-bdrv-drain: Drain with block jobs in an I/O thread

2018-09-07 Thread Kevin Wolf
This extends the existing drain test with a block job to include
variants where the block job runs in a different AioContext.

Signed-off-by: Kevin Wolf 
---
 tests/test-bdrv-drain.c | 92 +
 1 file changed, 86 insertions(+), 6 deletions(-)

diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
index 17bb8508ae..ab055e85f8 100644
--- a/tests/test-bdrv-drain.c
+++ b/tests/test-bdrv-drain.c
@@ -174,6 +174,28 @@ static void do_drain_end(enum drain_type drain_type, 
BlockDriverState *bs)
 }
 }
 
+static void do_drain_begin_unlocked(enum drain_type drain_type, 
BlockDriverState *bs)
+{
+if (drain_type != BDRV_DRAIN_ALL) {
+aio_context_acquire(bdrv_get_aio_context(bs));
+}
+do_drain_begin(drain_type, bs);
+if (drain_type != BDRV_DRAIN_ALL) {
+aio_context_release(bdrv_get_aio_context(bs));
+}
+}
+
+static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState 
*bs)
+{
+if (drain_type != BDRV_DRAIN_ALL) {
+aio_context_acquire(bdrv_get_aio_context(bs));
+}
+do_drain_end(drain_type, bs);
+if (drain_type != BDRV_DRAIN_ALL) {
+aio_context_release(bdrv_get_aio_context(bs));
+}
+}
+
 static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
 {
 BlockBackend *blk;
@@ -790,11 +812,13 @@ BlockJobDriver test_job_driver = {
 },
 };
 
-static void test_blockjob_common(enum drain_type drain_type)
+static void test_blockjob_common(enum drain_type drain_type, bool use_iothread)
 {
 BlockBackend *blk_src, *blk_target;
 BlockDriverState *src, *target;
 BlockJob *job;
+IOThread *iothread = NULL;
+AioContext *ctx;
 int ret;
 
 src = bdrv_new_open_driver(_test, "source", BDRV_O_RDWR,
@@ -802,21 +826,31 @@ static void test_blockjob_common(enum drain_type 
drain_type)
 blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
 blk_insert_bs(blk_src, src, _abort);
 
+if (use_iothread) {
+iothread = iothread_new();
+ctx = iothread_get_aio_context(iothread);
+blk_set_aio_context(blk_src, ctx);
+} else {
+ctx = qemu_get_aio_context();
+}
+
 target = bdrv_new_open_driver(_test, "target", BDRV_O_RDWR,
   _abort);
 blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
 blk_insert_bs(blk_target, target, _abort);
 
+aio_context_acquire(ctx);
 job = block_job_create("job0", _job_driver, NULL, src, 0, 
BLK_PERM_ALL,
0, 0, NULL, NULL, _abort);
 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, _abort);
 job_start(>job);
+aio_context_release(ctx);
 
 g_assert_cmpint(job->job.pause_count, ==, 0);
 g_assert_false(job->job.paused);
 g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
 
-do_drain_begin(drain_type, src);
+do_drain_begin_unlocked(drain_type, src);
 
 if (drain_type == BDRV_DRAIN_ALL) {
 /* bdrv_drain_all() drains both src and target */
@@ -827,7 +861,14 @@ static void test_blockjob_common(enum drain_type 
drain_type)
 g_assert_true(job->job.paused);
 g_assert_false(job->job.busy); /* The job is paused */
 
-do_drain_end(drain_type, src);
+do_drain_end_unlocked(drain_type, src);
+
+if (use_iothread) {
+/* paused is reset in the I/O thread, wait for it */
+while (job->job.paused) {
+aio_poll(qemu_get_aio_context(), false);
+}
+}
 
 g_assert_cmpint(job->job.pause_count, ==, 0);
 g_assert_false(job->job.paused);
@@ -846,32 +887,64 @@ static void test_blockjob_common(enum drain_type 
drain_type)
 
 do_drain_end(drain_type, target);
 
+if (use_iothread) {
+/* paused is reset in the I/O thread, wait for it */
+while (job->job.paused) {
+aio_poll(qemu_get_aio_context(), false);
+}
+}
+
 g_assert_cmpint(job->job.pause_count, ==, 0);
 g_assert_false(job->job.paused);
 g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
 
+aio_context_acquire(ctx);
 ret = job_complete_sync(>job, _abort);
 g_assert_cmpint(ret, ==, 0);
 
+if (use_iothread) {
+blk_set_aio_context(blk_src, qemu_get_aio_context());
+}
+aio_context_release(ctx);
+
 blk_unref(blk_src);
 blk_unref(blk_target);
 bdrv_unref(src);
 bdrv_unref(target);
+
+if (iothread) {
+iothread_join(iothread);
+}
 }
 
 static void test_blockjob_drain_all(void)
 {
-test_blockjob_common(BDRV_DRAIN_ALL);
+test_blockjob_common(BDRV_DRAIN_ALL, false);
 }
 
 static void test_blockjob_drain(void)
 {
-test_blockjob_common(BDRV_DRAIN);
+test_blockjob_common(BDRV_DRAIN, false);
 }
 
 static void test_blockjob_drain_subtree(void)
 {
-test_blockjob_common(BDRV_SUBTREE_DRAIN);
+test_blockjob_common(BDRV_SUBTREE_DRAIN, false);
+}
+
+static void test_blockjob_iothread_drain_all(void)
+{
+test_blockjob_common(BDRV_DRAIN_ALL,