26ba25
From f894047d028d1b80ab1aeb954464542f89d66f8f Mon Sep 17 00:00:00 2001
26ba25
From: Kevin Wolf <kwolf@redhat.com>
26ba25
Date: Wed, 10 Oct 2018 20:21:58 +0100
26ba25
Subject: [PATCH 32/49] test-bdrv-drain: Drain with block jobs in an I/O thread
26ba25
26ba25
RH-Author: Kevin Wolf <kwolf@redhat.com>
26ba25
Message-id: <20181010202213.7372-20-kwolf@redhat.com>
26ba25
Patchwork-id: 82608
26ba25
O-Subject: [RHEL-8 qemu-kvm PATCH 29/44] test-bdrv-drain: Drain with block jobs in an I/O thread
26ba25
Bugzilla: 1637976
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: John Snow <jsnow@redhat.com>
26ba25
RH-Acked-by: Thomas Huth <thuth@redhat.com>
26ba25
26ba25
This extends the existing drain test with a block job to include
26ba25
variants where the block job runs in a different AioContext.
26ba25
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Reviewed-by: Fam Zheng <famz@redhat.com>
26ba25
(cherry picked from commit f62c172959cd2b6de4dd8ba782e855d64d94764b)
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 tests/test-bdrv-drain.c | 92 +++++++++++++++++++++++++++++++++++++++++++++----
26ba25
 1 file changed, 86 insertions(+), 6 deletions(-)
26ba25
26ba25
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
26ba25
index 9bcb3c7..3cf3ba3 100644
26ba25
--- a/tests/test-bdrv-drain.c
26ba25
+++ b/tests/test-bdrv-drain.c
26ba25
@@ -174,6 +174,28 @@ static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
26ba25
     }
26ba25
 }
26ba25
 
26ba25
+static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs)
26ba25
+{
26ba25
+    if (drain_type != BDRV_DRAIN_ALL) {
26ba25
+        aio_context_acquire(bdrv_get_aio_context(bs));
26ba25
+    }
26ba25
+    do_drain_begin(drain_type, bs);
26ba25
+    if (drain_type != BDRV_DRAIN_ALL) {
26ba25
+        aio_context_release(bdrv_get_aio_context(bs));
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
26ba25
+{
26ba25
+    if (drain_type != BDRV_DRAIN_ALL) {
26ba25
+        aio_context_acquire(bdrv_get_aio_context(bs));
26ba25
+    }
26ba25
+    do_drain_end(drain_type, bs);
26ba25
+    if (drain_type != BDRV_DRAIN_ALL) {
26ba25
+        aio_context_release(bdrv_get_aio_context(bs));
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
 static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
26ba25
 {
26ba25
     BlockBackend *blk;
26ba25
@@ -785,11 +807,13 @@ BlockJobDriver test_job_driver = {
26ba25
     },
26ba25
 };
26ba25
 
26ba25
-static void test_blockjob_common(enum drain_type drain_type)
26ba25
+static void test_blockjob_common(enum drain_type drain_type, bool use_iothread)
26ba25
 {
26ba25
     BlockBackend *blk_src, *blk_target;
26ba25
     BlockDriverState *src, *target;
26ba25
     BlockJob *job;
26ba25
+    IOThread *iothread = NULL;
26ba25
+    AioContext *ctx;
26ba25
     int ret;
26ba25
 
26ba25
     src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
26ba25
@@ -797,21 +821,31 @@ static void test_blockjob_common(enum drain_type drain_type)
26ba25
     blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
26ba25
     blk_insert_bs(blk_src, src, &error_abort);
26ba25
 
26ba25
+    if (use_iothread) {
26ba25
+        iothread = iothread_new();
26ba25
+        ctx = iothread_get_aio_context(iothread);
26ba25
+        blk_set_aio_context(blk_src, ctx);
26ba25
+    } else {
26ba25
+        ctx = qemu_get_aio_context();
26ba25
+    }
26ba25
+
26ba25
     target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
26ba25
                                   &error_abort);
26ba25
     blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
26ba25
     blk_insert_bs(blk_target, target, &error_abort);
26ba25
 
26ba25
+    aio_context_acquire(ctx);
26ba25
     job = block_job_create("job0", &test_job_driver, NULL, src, 0, BLK_PERM_ALL,
26ba25
                            0, 0, NULL, NULL, &error_abort);
26ba25
     block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
26ba25
     job_start(&job->job);
26ba25
+    aio_context_release(ctx);
26ba25
 
26ba25
     g_assert_cmpint(job->job.pause_count, ==, 0);
26ba25
     g_assert_false(job->job.paused);
26ba25
     g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
26ba25
 
26ba25
-    do_drain_begin(drain_type, src);
26ba25
+    do_drain_begin_unlocked(drain_type, src);
26ba25
 
26ba25
     if (drain_type == BDRV_DRAIN_ALL) {
26ba25
         /* bdrv_drain_all() drains both src and target */
26ba25
@@ -822,7 +856,14 @@ static void test_blockjob_common(enum drain_type drain_type)
26ba25
     g_assert_true(job->job.paused);
26ba25
     g_assert_false(job->job.busy); /* The job is paused */
26ba25
 
26ba25
-    do_drain_end(drain_type, src);
26ba25
+    do_drain_end_unlocked(drain_type, src);
26ba25
+
26ba25
+    if (use_iothread) {
26ba25
+        /* paused is reset in the I/O thread, wait for it */
26ba25
+        while (job->job.paused) {
26ba25
+            aio_poll(qemu_get_aio_context(), false);
26ba25
+        }
26ba25
+    }
26ba25
 
26ba25
     g_assert_cmpint(job->job.pause_count, ==, 0);
26ba25
     g_assert_false(job->job.paused);
26ba25
@@ -841,32 +882,64 @@ static void test_blockjob_common(enum drain_type drain_type)
26ba25
 
26ba25
     do_drain_end(drain_type, target);
26ba25
 
26ba25
+    if (use_iothread) {
26ba25
+        /* paused is reset in the I/O thread, wait for it */
26ba25
+        while (job->job.paused) {
26ba25
+            aio_poll(qemu_get_aio_context(), false);
26ba25
+        }
26ba25
+    }
26ba25
+
26ba25
     g_assert_cmpint(job->job.pause_count, ==, 0);
26ba25
     g_assert_false(job->job.paused);
26ba25
     g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
26ba25
 
26ba25
+    aio_context_acquire(ctx);
26ba25
     ret = job_complete_sync(&job->job, &error_abort);
26ba25
     g_assert_cmpint(ret, ==, 0);
26ba25
 
26ba25
+    if (use_iothread) {
26ba25
+        blk_set_aio_context(blk_src, qemu_get_aio_context());
26ba25
+    }
26ba25
+    aio_context_release(ctx);
26ba25
+
26ba25
     blk_unref(blk_src);
26ba25
     blk_unref(blk_target);
26ba25
     bdrv_unref(src);
26ba25
     bdrv_unref(target);
26ba25
+
26ba25
+    if (iothread) {
26ba25
+        iothread_join(iothread);
26ba25
+    }
26ba25
 }
26ba25
 
26ba25
 static void test_blockjob_drain_all(void)
26ba25
 {
26ba25
-    test_blockjob_common(BDRV_DRAIN_ALL);
26ba25
+    test_blockjob_common(BDRV_DRAIN_ALL, false);
26ba25
 }
26ba25
 
26ba25
 static void test_blockjob_drain(void)
26ba25
 {
26ba25
-    test_blockjob_common(BDRV_DRAIN);
26ba25
+    test_blockjob_common(BDRV_DRAIN, false);
26ba25
 }
26ba25
 
26ba25
 static void test_blockjob_drain_subtree(void)
26ba25
 {
26ba25
-    test_blockjob_common(BDRV_SUBTREE_DRAIN);
26ba25
+    test_blockjob_common(BDRV_SUBTREE_DRAIN, false);
26ba25
+}
26ba25
+
26ba25
+static void test_blockjob_iothread_drain_all(void)
26ba25
+{
26ba25
+    test_blockjob_common(BDRV_DRAIN_ALL, true);
26ba25
+}
26ba25
+
26ba25
+static void test_blockjob_iothread_drain(void)
26ba25
+{
26ba25
+    test_blockjob_common(BDRV_DRAIN, true);
26ba25
+}
26ba25
+
26ba25
+static void test_blockjob_iothread_drain_subtree(void)
26ba25
+{
26ba25
+    test_blockjob_common(BDRV_SUBTREE_DRAIN, true);
26ba25
 }
26ba25
 
26ba25
 
26ba25
@@ -1337,6 +1410,13 @@ int main(int argc, char **argv)
26ba25
     g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
26ba25
                     test_blockjob_drain_subtree);
26ba25
 
26ba25
+    g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
26ba25
+                    test_blockjob_iothread_drain_all);
26ba25
+    g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
26ba25
+                    test_blockjob_iothread_drain);
26ba25
+    g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
26ba25
+                    test_blockjob_iothread_drain_subtree);
26ba25
+
26ba25
     g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
26ba25
     g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
26ba25
     g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
26ba25
-- 
26ba25
1.8.3.1
26ba25