Skip to contentSkip to conversation

util/async: make bh_aio_poll() O(1)

PATCH Applied Stefan Hajnoczi 2 participants 3 messages Feb 21, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 util/async: make bh_aio_poll() O(1)

The ctx->first_bh list contains all created BHs, including those that are not scheduled. The list is iterated by the event loop and therefore has O(n) time complexity with respected to the number of created BHs.

Rewrite BHs so that only scheduled or deleted BHs are enqueued. Only BHs that actually require action will be iterated.

One semantic change is required: qemu_bh_delete() enqueues the BH and therefore invokes aio_notify(). The tests/test-aio.c:test_source_bh_delete_from_cb() test case assumed that g_main_context_iteration(NULL, false) returns false after qemu_bh_delete() but it now returns true for one iteration. Fix up the test case.

This patch makes aio_compute_timeout() and aio_bh_poll() drop from a CPU profile reported by perf-top(1). Previously they combined to 9% CPU utilization when AioContext polling is commented out and the guest has 2 virtio-blk,num-queues=1 and 99 virtio-blk,num-queues=32 devices.

---
v3:
* Use QSLIST_FOREACH_RCU() and QSLIST_FIRST_RCU() [Paolo]
v2:
* Use QSLIST for BHs and QSIMPLEQ for BHListSlices [Paolo] (Note that I replaced bh = atomic_rcu_read(&first_bh) with QSLIST_FOREACH(&bh_list) so there is no memory ordering but I think this is safe.)
* Comment clarifications [Paolo]

Based-on: [email protected]
("[PATCH] rcu_queue: add QSLIST functions")

---
 include/block/aio.h |  20 +++-
 tests/test-aio.c    |   3 +-
 util/async.c        | 237 ++++++++++++++++++++++++++------------------
 3 files changed, 158 insertions(+), 102 deletions(-)
include/block/aio.h +18 -2
index 7ba9bd7874..1a2ce9ca26 100644
@@ -51,6 +51,19 @@ struct ThreadPool;
 struct LinuxAioState;
 struct LuringState;
 
+/*
+ * Each aio_bh_poll() call carves off a slice of the BH list, so that newly
+ * scheduled BHs are not processed until the next aio_bh_poll() call.  All
+ * active aio_bh_poll() calls chain their slices together in a list, so that
+ * nested aio_bh_poll() calls process all scheduled bottom halves.
+ */
+typedef QSLIST_HEAD(, QEMUBH) BHList;
+typedef struct BHListSlice BHListSlice;
+struct BHListSlice {
+    BHList bh_list;
+    QSIMPLEQ_ENTRY(BHListSlice) next;
+};
+
 struct AioContext {
     GSource source;
 
@@ -91,8 +104,11 @@ struct AioContext {
      */
     QemuLockCnt list_lock;
 
-    /* Anchor of the list of Bottom Halves belonging to the context */
-    struct QEMUBH *first_bh;
+    /* Bottom Halves pending aio_bh_poll() processing */
+    BHList bh_list;
+
+    /* Chained BH list slices for each nested aio_bh_poll() call */
+    QSIMPLEQ_HEAD(, BHListSlice) bh_slice_list;
 
     /* Used by aio_notify.
      *
tests/test-aio.c +2 -1
index 86fb73b3d5..8a46078463 100644
@@ -615,7 +615,8 @@ static void test_source_bh_delete_from_cb(void)
     g_assert_cmpint(data1.n, ==, data1.max);
     g_assert(data1.bh == NULL);
 
-    g_assert(!g_main_context_iteration(NULL, false));
+    assert(g_main_context_iteration(NULL, false));
+    assert(!g_main_context_iteration(NULL, false));
 }
 
 static void test_source_bh_delete_from_cb_many(void)
util/async.c +138 -99

350 lines not on this page · raw message

Signature · 1 line
2.24.1
Reviewed-by Paolo Bonzini on v3 1/1
#1 lore raw
Quoted 20 lines · Stefan Hajnoczi

The ctx->first_bh list contains all created BHs, including those that are not scheduled. The list is iterated by the event loop and therefore has O(n) time complexity with respected to the number of created BHs.

Rewrite BHs so that only scheduled or deleted BHs are enqueued. Only BHs that actually require action will be iterated.

One semantic change is required: qemu_bh_delete() enqueues the BH and therefore invokes aio_notify(). The tests/test-aio.c:test_source_bh_delete_from_cb() test case assumed that g_main_context_iteration(NULL, false) returns false after qemu_bh_delete() but it now returns true for one iteration. Fix up the test case.

This patch makes aio_compute_timeout() and aio_bh_poll() drop from a CPU profile reported by perf-top(1). Previously they combined to 9% CPU utilization when AioContext polling is commented out and the guest has 2 virtio-blk,num-queues=1 and 99 virtio-blk,num-queues=32 devices.

Quoted 4 lines

---
v3:
* Use QSLIST_FOREACH_RCU() and QSLIST_FIRST_RCU() [Paolo]

I forgot to include Paolo's R-b that he gave conditional on making this change:

#2 lore raw
In reply to Stefan Hajnoczi
Quoted 30 lines · Stefan Hajnoczi

The ctx->first_bh list contains all created BHs, including those that are not scheduled. The list is iterated by the event loop and therefore has O(n) time complexity with respected to the number of created BHs.

Rewrite BHs so that only scheduled or deleted BHs are enqueued. Only BHs that actually require action will be iterated.

One semantic change is required: qemu_bh_delete() enqueues the BH and therefore invokes aio_notify(). The tests/test-aio.c:test_source_bh_delete_from_cb() test case assumed that g_main_context_iteration(NULL, false) returns false after qemu_bh_delete() but it now returns true for one iteration. Fix up the test case.

This patch makes aio_compute_timeout() and aio_bh_poll() drop from a CPU profile reported by perf-top(1). Previously they combined to 9% CPU utilization when AioContext polling is commented out and the guest has 2 virtio-blk,num-queues=1 and 99 virtio-blk,num-queues=32 devices.

---
v3:
* Use QSLIST_FOREACH_RCU() and QSLIST_FIRST_RCU() [Paolo]
v2:
* Use QSLIST for BHs and QSIMPLEQ for BHListSlices [Paolo] (Note that I replaced bh = atomic_rcu_read(&first_bh) with QSLIST_FOREACH(&bh_list) so there is no memory ordering but I think this is safe.)
* Comment clarifications [Paolo]

Quoted 7 lines

Based-on: [email protected]
("[PATCH] rcu_queue: add QSLIST functions")

---
 include/block/aio.h |  20 +++-
 tests/test-aio.c    |   3 +-
 util/async.c        | 237 ++++++++++++++++++++++++++------------------
 3 files changed, 158 insertions(+), 102 deletions(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan