Skip to contentSkip to conversation

block: three optimizations

PATCH Applied Paolo Bonzini 2 participants 5 messages Dec 17, 2014 – Dec 18, 2014

lore
Share & tools
lore
#0 lore raw

cover block: three optimizations

These are three unrelated micro-optimizations of the block layer. The first is only visible on non-dataplane operation, the others are generic.

Paolo Bonzini (3):
block: mark AioContext as recursive
block: do not allocate an iovec per read of a growable/zero_after_eof BDS
block: replace g_new0 with g_new for bottom half allocation.

 async.c | 11 +++++++----
 block.c | 12 +++++-------
 2 files changed, 12 insertions(+), 11 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH 1/3 block: mark AioContext as recursive

AioContext can be accessed recursively, in fact that's what we do with aio_poll. Marking the GSource as recursive avoids that GLib blocks it and unblocks it around every call to aio_dispatch, which is a pretty expensive operation.

---
 async.c | 1 +
 1 file changed, 1 insertion(+)
async.c +1 -0
index 3939b79..572f239 100644
@@ -300,6 +300,7 @@ AioContext *aio_context_new(Error **errp)
         error_setg_errno(errp, -ret, "Failed to initialize event notifier");
         return NULL;
     }
+    g_source_set_can_recurse(&ctx->source, true);
     aio_set_event_notifier(ctx, &ctx->notifier,
                            (EventNotifierHandler *)
                            event_notifier_test_and_clear);
Signature · 1 line
2.1.0
#2 lore raw

PATCH 2/3 block: do not allocate an iovec per read of a growable/zero_after_eof BDS

In reply to Paolo Bonzini

Most reads do not go past the end of the file, and they can use the input QEMUIOVector instead of creating one. This removes the qemu_iovec_* functions from the profile.

---
 block.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
block.c +5 -7
index 4165d42..58f8042 100644
@@ -3034,18 +3034,16 @@ static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs,
 
         max_nb_sectors = ROUND_UP(MAX(0, total_sectors - sector_num),
                                   align >> BDRV_SECTOR_BITS);
-        if (max_nb_sectors > 0) {
+        if (nb_sectors < max_nb_sectors) {
+            ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
+        } else if (max_nb_sectors > 0) {
             QEMUIOVector local_qiov;
-            size_t local_sectors;
-
-            max_nb_sectors = MIN(max_nb_sectors, SIZE_MAX / BDRV_SECTOR_BITS);
-            local_sectors = MIN(max_nb_sectors, nb_sectors);
 
             qemu_iovec_init(&local_qiov, qiov->niov);
             qemu_iovec_concat(&local_qiov, qiov, 0,
-                              local_sectors * BDRV_SECTOR_SIZE);
+                              max_nb_sectors * BDRV_SECTOR_SIZE);
 
-            ret = drv->bdrv_co_readv(bs, sector_num, local_sectors,
+            ret = drv->bdrv_co_readv(bs, sector_num, max_nb_sectors,
                                      &local_qiov);
 
             qemu_iovec_destroy(&local_qiov);
Signature · 1 line
2.1.0
#3 lore raw

PATCH 3/3 block: replace g_new0 with g_new for bottom half allocation.

In reply to Paolo Bonzini

This saves about 15% of the clock cycles spent on allocation. Using the slice allocator does not add a visible improvement; allocation is faster than malloc, while freeing seems to be slower.

---
 async.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
async.c +6 -4
index 572f239..2be88cc 100644
@@ -44,10 +44,12 @@ struct QEMUBH {
 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
 {
     QEMUBH *bh;
-    bh = g_new0(QEMUBH, 1);
-    bh->ctx = ctx;
-    bh->cb = cb;
-    bh->opaque = opaque;
+    bh = g_new(QEMUBH, 1);
+    *bh = (QEMUBH){
+        .ctx = ctx,
+        .cb = cb,
+        .opaque = opaque,
+    };
     qemu_mutex_lock(&ctx->bh_lock);
     bh->next = ctx->first_bh;
     /* Make sure that the members are ready before putting bh into list */
Signature · 1 line
2.1.0
Applied
#4 lore raw
In reply to Paolo Bonzini

Am 17.12.2014 um 16:09 hat Paolo Bonzini geschrieben:

Quoted 3 lines

These are three unrelated micro-optimizations of the block layer. The first is only visible on non-dataplane operation, the others are generic.

Thanks, applied all to the block branch.

Kevin