Skip to contentSkip to conversation

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

PATCH Superseded Stefan Hajnoczi 3 participants 5 messages Feb 19, 2020 – Feb 21, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 include/block/aio.h |  17 +++-
 tests/test-aio.c    |   3 +-
 util/async.c        | 242 ++++++++++++++++++++++++++------------------
 3 files changed, 160 insertions(+), 102 deletions(-)
include/block/aio.h +15 -2
index 7ba9bd7874..51f4aa09cb 100644
@@ -51,6 +51,18 @@ struct ThreadPool;
 struct LinuxAioState;
 struct LuringState;
 
+/*
+ * Each aio_bh_poll() call carves off a slice of the BH list.  This way newly
+ * scheduled BHs are not processed until the next aio_bh_poll() call.  This
+ * concept extends to nested aio_bh_poll() calls because slices are chained
+ * together.
+ */
+typedef struct BHListSlice BHListSlice;
+struct BHListSlice {
+    QEMUBH *first_bh;
+    BHListSlice *next;
+};
+
 struct AioContext {
     GSource source;
 
@@ -91,8 +103,9 @@ 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 */
+    BHListSlice bh_list;
+    BHListSlice **bh_list_tail;
 
     /* 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 +143 -99

346 lines not on this page · raw message

Signature · 1 line
2.24.1
#1 lore raw

Really a great idea, though I have some remarks on the implementation below.

Quoted 4 lines · Stefan Hajnoczi

+ * Each aio_bh_poll() call carves off a slice of the BH list. This way newly
+ * scheduled BHs are not processed until the next aio_bh_poll() call. This
+ * concept extends to nested aio_bh_poll() calls because slices are chained
+ * together.

This is the tricky part so I would expand a bit on why it's needed:

/*
* 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 chained their slices together
* in a list, so that nested aio_bh_poll() calls process all scheduled
* bottom halves.
*/

include/block/aio.h struct ThreadPool; · line 65

Quoted 5 lines
include/block/aio.h +5 -0
+struct BHListSlice {
+    QEMUBH *first_bh;
+    BHListSlice *next;
+};
+

Using QLIST and QSLIST removes the need to create your own lists, since you can use QSLIST_MOVE_ATOMIC and QSLIST_INSERT_HEAD_ATOMIC. For example:

struct BHListSlice {
    QSLIST_HEAD(, QEMUBH) first_bh;
    QLIST_ENTRY(BHListSlice) next;
};

...

    QSLIST_HEAD(, QEMUBH) active_bh;
    QLIST_HEAD(, BHListSlice) bh_list;

Related to this, in the aio_bh_poll() loop:

    for (s = ctx->bh_list.next; s; s = s->next) {
    }

You can actually do the removal inside the loop. This is slightly more efficient since you can remove slices early from the nested aio_bh_poll(). Not that it's relevant for performance, but I think the FIFO order for slices is also more intuitive than LIFO.

Putting this idea together with the QLIST one, you would get:

    /*
     * If a bottom half causes a recursive call, this slice will be
     * removed by the nested aio_bh_poll().
     */
    QSLIST_MOVE_ATOMIC(&slice.first_bh, ctx->active_bh);
    QLIST_INSERT_TAIL(&ctx->bh_list, slice);
    while ((s = QLIST_FIRST(&ctx->bh_list)) {
        while ((bh = aio_bh_dequeue(&s, &flags))) {
        }
        QLIST_REMOVE_HEAD(s, next);
    }
Quoted 3 lines

/* Multiple occurrences of aio_bh_poll cannot be called concurrently.
* The count in ctx->list_lock is incremented before the call, and is
* not affected by the call.

The second sentence is now stale.

Paolo

Quoted 14 lines

*/
int aio_bh_poll(AioContext *ctx)
{

util/async.c +7 -2
-    QEMUBH *bh, **bhp, *next;
+    BHListSlice slice;
+    BHListSlice **old_tail;
+    BHListSlice *s;
+    QEMUBH *bh;
+    unsigned flags;
     int ret;
-    bool deleted = false;
+
+    old_tail = push_bh_list_slice(ctx, &slice);
#2 lore raw
Quoted 10 lines · Paolo Bonzini

Really a great idea, though I have some remarks on the implementation below.

Quoted 4 lines · Stefan Hajnoczi

+ * Each aio_bh_poll() call carves off a slice of the BH list. This way newly
+ * scheduled BHs are not processed until the next aio_bh_poll() call. This
+ * concept extends to nested aio_bh_poll() calls because slices are chained
+ * together.

This is the tricky part so I would expand a bit on why it's needed:

Quoted 7 lines

/*
* 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 chained their slices together
* in a list, so that nested aio_bh_poll() calls process all scheduled
* bottom halves.
*/

Thanks, will fix in v2.

Quoted 14 lines

include/block/aio.h struct ThreadPool; · line 65

Quoted 5 lines
include/block/aio.h +5 -0
+struct BHListSlice {
+    QEMUBH *first_bh;
+    BHListSlice *next;
+};
+

Using QLIST and QSLIST removes the need to create your own lists, since you can use QSLIST_MOVE_ATOMIC and QSLIST_INSERT_HEAD_ATOMIC. For example:

struct BHListSlice {
    QSLIST_HEAD(, QEMUBH) first_bh;
    QLIST_ENTRY(BHListSlice) next;
};
Quoted 4 lines

...

    QSLIST_HEAD(, QEMUBH) active_bh;
    QLIST_HEAD(, BHListSlice) bh_list;

I thought about this but chose the explicit tail pointer approach because it lets aio_compute_timeout() and aio_ctx_check() iterate over both the active BH list and slices in a single for loop :). But thinking about it more, maybe it can still be done by replacing active_bh with a permanently present first BHListSlice element.

Quoted 13 lines

Related to this, in the aio_bh_poll() loop:

    for (s = ctx->bh_list.next; s; s = s->next) {
    }

You can actually do the removal inside the loop. This is slightly more efficient since you can remove slices early from the nested aio_bh_poll(). Not that it's relevant for performance, but I think the FIFO order for slices is also more intuitive than LIFO.

Putting this idea together with the QLIST one, you would get:

Quoted 11 lines
    /*
     * If a bottom half causes a recursive call, this slice will be
     * removed by the nested aio_bh_poll().
     */
    QSLIST_MOVE_ATOMIC(&slice.first_bh, ctx->active_bh);
    QLIST_INSERT_TAIL(&ctx->bh_list, slice);
    while ((s = QLIST_FIRST(&ctx->bh_list)) {
        while ((bh = aio_bh_dequeue(&s, &flags))) {
        }
        QLIST_REMOVE_HEAD(s, next);
    }

Cool, reusing "queue.h" is nice.

Quoted 6 lines
Quoted 4 lines

/* Multiple occurrences of aio_bh_poll cannot be called concurrently.
* The count in ctx->list_lock is incremented before the call, and is
* not affected by the call.

The second sentence is now stale.

Thanks, will fix in v2.

Stefan

#3 lore raw

Il mer 19 feb 2020, 18:58 Stefan Hajnoczi <[email protected]> ha scritto:

Quoted 42 lines · Paolo Bonzini
Quoted 1 line

Really a great idea, though I have some remarks on the implementation

below.

Quoted 3 lines · Stefan Hajnoczi
Quoted 1 line

+ * Each aio_bh_poll() call carves off a slice of the BH list. This

way newly

Quoted 1 line
Quoted 1 line

+ * scheduled BHs are not processed until the next aio_bh_poll()

call. This

Quoted 1 line
Quoted 1 line

+ * concept extends to nested aio_bh_poll() calls because slices are

chained

Quoted 11 lines
Quoted 1 line

+ * together.

This is the tricky part so I would expand a bit on why it's needed:

/*
* 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 chained their slices together
* in a list, so that nested aio_bh_poll() calls process all scheduled
* bottom halves.
*/

Thanks, will fix in v2.

Quoted 8 lines

include/block/aio.h struct ThreadPool; · line 65

Quoted 5 lines
include/block/aio.h +5 -0
+struct BHListSlice {
+    QEMUBH *first_bh;
+    BHListSlice *next;
+};
+

Using QLIST and QSLIST removes the need to create your own lists, since you can use QSLIST_MOVE_ATOMIC and QSLIST_INSERT_HEAD_ATOMIC. For

example:

Quoted 8 lines
struct BHListSlice {
    QSLIST_HEAD(, QEMUBH) first_bh;
    QLIST_ENTRY(BHListSlice) next;
};

...

Quoted 9 lines
Quoted 2 lines
    QSLIST_HEAD(, QEMUBH) active_bh;
    QLIST_HEAD(, BHListSlice) bh_list;

I thought about this but chose the explicit tail pointer approach because it lets aio_compute_timeout() and aio_ctx_check() iterate over both the active BH list and slices in a single for loop :). But thinking about it more, maybe it can still be done by replacing active_bh with a permanently present first BHListSlice element.

Probably not so easy since the idea was to empty the slices list entirely via the FIFO order.

But since you are rewriting everything anyway, can you add a flag for whether there are any non-idle bottom halves anywhere in the list? It need not be computed perfectly, because any non-idle bh will cause the idle bottom halves to be triggered as well; you can just set in qemu_bh_schedule and clear it at the end of aio_bh_poll.

Then if there is any active bh or slice you consult the flag and use it to return the timeout, which will be either 0 or 10ms depending on the flag. That's truly O(1). (More precisely, this patch goes from O(#created-bh) to O(#scheduled-bh), which of course is optimal for aio_bh_poll but not for aio_compute_timeout; making timeout computation O(1) can lower latency a bit by decreasing the constant factor).

Paolo

Quoted 38 lines
Quoted 13 lines

Related to this, in the aio_bh_poll() loop:

    for (s = ctx->bh_list.next; s; s = s->next) {
    }

You can actually do the removal inside the loop. This is slightly more efficient since you can remove slices early from the nested aio_bh_poll(). Not that it's relevant for performance, but I think the FIFO order for slices is also more intuitive than LIFO.

Putting this idea together with the QLIST one, you would get:

Quoted 11 lines
    /*
     * If a bottom half causes a recursive call, this slice will be
     * removed by the nested aio_bh_poll().
     */
    QSLIST_MOVE_ATOMIC(&slice.first_bh, ctx->active_bh);
    QLIST_INSERT_TAIL(&ctx->bh_list, slice);
    while ((s = QLIST_FIRST(&ctx->bh_list)) {
        while ((bh = aio_bh_dequeue(&s, &flags))) {
        }
        QLIST_REMOVE_HEAD(s, next);
    }

Cool, reusing "queue.h" is nice.

Quoted 6 lines
Quoted 4 lines

/* Multiple occurrences of aio_bh_poll cannot be called concurrently.
* The count in ctx->list_lock is incremented before the call, and is
* not affected by the call.

The second sentence is now stale.

Thanks, will fix in v2.

Stefan

#4 lore raw
Quoted 63 lines · Paolo Bonzini

Il mer 19 feb 2020, 18:58 Stefan Hajnoczi <[email protected]> ha scritto:

Quoted 42 lines · Paolo Bonzini
Quoted 1 line

Really a great idea, though I have some remarks on the implementation

below.

Quoted 3 lines · Stefan Hajnoczi
Quoted 1 line

+ * Each aio_bh_poll() call carves off a slice of the BH list. This

way newly

Quoted 1 line
Quoted 1 line

+ * scheduled BHs are not processed until the next aio_bh_poll()

call. This

Quoted 1 line
Quoted 1 line

+ * concept extends to nested aio_bh_poll() calls because slices are

chained

Quoted 11 lines
Quoted 1 line

+ * together.

This is the tricky part so I would expand a bit on why it's needed:

/*
* 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 chained their slices together
* in a list, so that nested aio_bh_poll() calls process all scheduled
* bottom halves.
*/

Thanks, will fix in v2.

Quoted 8 lines

include/block/aio.h struct ThreadPool; · line 65

Quoted 5 lines
include/block/aio.h +5 -0
+struct BHListSlice {
+    QEMUBH *first_bh;
+    BHListSlice *next;
+};
+

Using QLIST and QSLIST removes the need to create your own lists, since you can use QSLIST_MOVE_ATOMIC and QSLIST_INSERT_HEAD_ATOMIC. For

example:

Quoted 8 lines
struct BHListSlice {
    QSLIST_HEAD(, QEMUBH) first_bh;
    QLIST_ENTRY(BHListSlice) next;
};

...

Quoted 9 lines
Quoted 2 lines
    QSLIST_HEAD(, QEMUBH) active_bh;
    QLIST_HEAD(, BHListSlice) bh_list;

I thought about this but chose the explicit tail pointer approach because it lets aio_compute_timeout() and aio_ctx_check() iterate over both the active BH list and slices in a single for loop :). But thinking about it more, maybe it can still be done by replacing active_bh with a permanently present first BHListSlice element.

Probably not so easy since the idea was to empty the slices list entirely via the FIFO order.

But since you are rewriting everything anyway, can you add a flag for whether there are any non-idle bottom halves anywhere in the list? It need not be computed perfectly, because any non-idle bh will cause the idle bottom halves to be triggered as well; you can just set in qemu_bh_schedule and clear it at the end of aio_bh_poll.

Quoted 6 lines

Then if there is any active bh or slice you consult the flag and use it to return the timeout, which will be either 0 or 10ms depending on the flag. That's truly O(1). (More precisely, this patch goes from O(#created-bh) to O(#scheduled-bh), which of course is optimal for aio_bh_poll but not for aio_compute_timeout; making timeout computation O(1) can lower latency a bit by decreasing the constant factor).

Yes, good idea. I'll send a follow-up patch.

Stefan