Skip to contentSkip to conversation

coroutine: dynamically scale pool size

PATCH Superseded Stefan Hajnoczi 5 participants 11 messages Jul 3, 2014 – Jul 4, 2014

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover coroutine: dynamically scale pool size

The coroutine pool reuses exited coroutines to make qemu_coroutine_create() cheap. The size of the pool is capped to prevent it from hogging memory after a period of high coroutine activity. Previously the max size was hardcoded to 64 but this doesn't scale with guest size.

A guest with lots of disks can do more parallel I/O and therefore requires a larger coroutine pool size. This series tries to solve the problem by scaling pool size according to the number of drives.

Ming: Please let me know if this eliminates the rt_sigprocmask system calls you are seeing. It should solve part of the performance regression you have seen in qemu.git/master virtio-blk dataplane.

Stefan Hajnoczi (2):
coroutine: make pool size dynamic
block: bump coroutine pool size for drives

 block.c                   |  4 ++++
 include/block/coroutine.h | 11 +++++++++++
 qemu-coroutine.c          | 25 +++++++++++++++++++------
 3 files changed, 34 insertions(+), 6 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/2 coroutine: make pool size dynamic

Allow coroutine users to adjust the pool size. For example, if the guest has multiple emulated disk drives we should keep around more coroutines.

---
 include/block/coroutine.h | 11 +++++++++++
 qemu-coroutine.c          | 25 +++++++++++++++++++------
 2 files changed, 30 insertions(+), 6 deletions(-)
include/block/coroutine.h +11 -0
index a1797ae..07eeb3c 100644
@@ -223,4 +223,15 @@ void coroutine_fn co_aio_sleep_ns(AioContext *ctx, QEMUClockType type,
  * Note that this function clobbers the handlers for the file descriptor.
  */
 void coroutine_fn yield_until_fd_readable(int fd);
+
+/**
+ * Add or subtract from the coroutine pool size
+ *
+ * The coroutine implementation keeps a pool of coroutines to be reused by
+ * qemu_coroutine_create().  This makes coroutine creation cheap.  Heavy
+ * coroutine users should call this to reserve pool space.  Call it again with
+ * a negative number to release pool space.
+ */
+void qemu_coroutine_adjust_pool_size(int n);
+
 #endif /* QEMU_COROUTINE_H */
qemu-coroutine.c +19 -6
index 4708521..d94234a 100644
@@ -18,15 +18,11 @@
 #include "block/coroutine.h"
 #include "block/coroutine_int.h"
 
-enum {
-    /* Maximum free pool size prevents holding too many freed coroutines */
-    POOL_MAX_SIZE = 64,
-};
-
 /** Free list to speed up creation */
 static QemuMutex pool_lock;
 static QSLIST_HEAD(, Coroutine) pool = QSLIST_HEAD_INITIALIZER(pool);
 static unsigned int pool_size;
+static unsigned int pool_max_size = 64;
 
 Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
 {
@@ -55,7 +51,7 @@ static void coroutine_delete(Coroutine *co)
 {
     if (CONFIG_COROUTINE_POOL) {
         qemu_mutex_lock(&pool_lock);
-        if (pool_size < POOL_MAX_SIZE) {
+        if (pool_size < pool_max_size) {
             QSLIST_INSERT_HEAD(&pool, co, pool_next);
             co->caller = NULL;
             pool_size++;
@@ -137,3 +133,20 @@ void coroutine_fn qemu_coroutine_yield(void)
     self->caller = NULL;
     coroutine_swap(self, to);
 }
+
+void qemu_coroutine_adjust_pool_size(int n)
+{
+    qemu_mutex_lock(&pool_lock);
+
+    pool_max_size += n;
+
+    /* Trim oversized pool down to new max */
+    while (pool_size > pool_max_size) {
+        Coroutine *co = QSLIST_FIRST(&pool);
+        QSLIST_REMOVE_HEAD(&pool, pool_next);
+        pool_size--;
+        qemu_coroutine_delete(co);
+    }
+
+    qemu_mutex_unlock(&pool_lock);
+}
Signature · 1 line
1.9.3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] coroutine: make pool size dynamic

Quoted 11 lines · Stefan Hajnoczi

Allow coroutine users to adjust the pool size. For example, if the guest has multiple emulated disk drives we should keep around more coroutines.

---
 include/block/coroutine.h | 11 +++++++++++
 qemu-coroutine.c          | 25 +++++++++++++++++++------
 2 files changed, 30 insertions(+), 6 deletions(-)

qemu-coroutine.c void coroutine_fn qemu_coroutine_yield(void) · line 141

Quoted 6 lines
qemu-coroutine.c +6 -0
+
+void qemu_coroutine_adjust_pool_size(int n)
+{
+    qemu_mutex_lock(&pool_lock);
+
+    pool_max_size += n;

Where do you check that this doesn't fall below a bare-minimum acceptable level? Is it worth assert(pool_max_size >= 64)?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] coroutine: make pool size dynamic

Quoted 12 lines · Eric Blake
Quoted 10 lines · Stefan Hajnoczi

Allow coroutine users to adjust the pool size. For example, if the guest has multiple emulated disk drives we should keep around more coroutines.

---
 include/block/coroutine.h | 11 +++++++++++
 qemu-coroutine.c          | 25 +++++++++++++++++++------
 2 files changed, 30 insertions(+), 6 deletions(-)
Quoted 9 lines

qemu-coroutine.c void coroutine_fn qemu_coroutine_yield(void) · line 141

Quoted 6 lines
qemu-coroutine.c +6 -0
+
+void qemu_coroutine_adjust_pool_size(int n)
+{
+    qemu_mutex_lock(&pool_lock);
+
+    pool_max_size += n;

Where do you check that this doesn't fall below a bare-minimum acceptable level? Is it worth assert(pool_max_size >= 64)?

I don't. I think you are right; it's worth checking.

Stefan

#4 lore raw

PATCH 2/2 block: bump coroutine pool size for drives

In reply to Stefan Hajnoczi

When a BlockDriverState is associated with a storage controller DeviceState we expect guest I/O. Use this opportunity to bump the coroutine pool size by 64.

This patch ensures that the coroutine pool size scales with the number of drives attached to the guest. It should increase coroutine pool usage (which makes qemu_coroutine_create() fast) without hogging too much memory when fewer drives are attached.

---
 block.c | 4 ++++
 1 file changed, 4 insertions(+)
block.c +4 -0
index f80e2b2..c8379ca 100644
@@ -2093,6 +2093,9 @@ int bdrv_attach_dev(BlockDriverState *bs, void *dev)
     }
     bs->dev = dev;
     bdrv_iostatus_reset(bs);
+
+    /* We're expecting I/O from the device so bump up coroutine pool size */
+    qemu_coroutine_adjust_pool_size(64);
     return 0;
 }
 
@@ -2112,6 +2115,7 @@ void bdrv_detach_dev(BlockDriverState *bs, void *dev)
     bs->dev_ops = NULL;
     bs->dev_opaque = NULL;
     bs->guest_block_size = 512;
+    qemu_coroutine_adjust_pool_size(-64);
 }
 
 /* TODO change to return DeviceState * when all users are qdevified */
Signature · 1 line
1.9.3
Reviewed-by on v1 2/2
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] block: bump coroutine pool size for drives

Quoted 9 lines · Stefan Hajnoczi

When a BlockDriverState is associated with a storage controller DeviceState we expect guest I/O. Use this opportunity to bump the coroutine pool size by 64.

This patch ensures that the coroutine pool size scales with the number of drives attached to the guest. It should increase coroutine pool usage (which makes qemu_coroutine_create() fast) without hogging too much memory when fewer drives are attached.

Quoted 4 lines
---
 block.c | 4 ++++
 1 file changed, 4 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw
In reply to Stefan Hajnoczi
Quoted 5 lines · Stefan Hajnoczi

The coroutine pool reuses exited coroutines to make qemu_coroutine_create() cheap. The size of the pool is capped to prevent it from hogging memory after a period of high coroutine activity. Previously the max size was hardcoded to 64 but this doesn't scale with guest size.

Quoted 7 lines

A guest with lots of disks can do more parallel I/O and therefore requires a larger coroutine pool size. This series tries to solve the problem by scaling pool size according to the number of drives.

Ming: Please let me know if this eliminates the rt_sigprocmask system calls you are seeing. It should solve part of the performance regression you have seen in qemu.git/master virtio-blk dataplane.

With both the two coroutine patches and the block plug&unplug patches, performance of qemu.git/master virtio-blk dataplane can recover to level of QEMU 2.0.

Quoted 14 lines

Stefan Hajnoczi (2):
coroutine: make pool size dynamic
block: bump coroutine pool size for drives

 block.c                   |  4 ++++
 include/block/coroutine.h | 11 +++++++++++
 qemu-coroutine.c          | 25 +++++++++++++++++++------
 3 files changed, 34 insertions(+), 6 deletions(-)

--
1.9.3

#7 lore raw

Il 04/07/2014 07:10, Ming Lei ha scritto:

Quoted 3 lines

With both the two coroutine patches and the block plug&unplug patches, performance of qemu.git/master virtio-blk dataplane can recover to level of QEMU 2.0.

So the excessive writes (to eventfd and irqfd) are not a problem? That'd be a relief. :)

Paolo

#8 lore raw
Quoted 10 lines · Paolo Bonzini

Il 04/07/2014 07:10, Ming Lei ha scritto:

Quoted 4 lines

With both the two coroutine patches and the block plug&unplug patches, performance of qemu.git/master virtio-blk dataplane can recover to level of
QEMU 2.0.

So the excessive writes (to eventfd and irqfd) are not a problem? That'd be a relief. :)

I mean it is in a level, but your aio_notify() patch still can improve virtioblk dataplane performance some, in my test, with 5~10K IOPS improvement, which should belong to same level, because my test is a bit coarse, :-)

IMO not only virtioblk can benefit from your patch, others can benefit too.

Thanks,
--
Ming Lei

#9 lore raw

Il 04/07/2014 09:02, Ming Lei ha scritto:

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

So the excessive writes (to eventfd and irqfd) are not a problem? That'd be a relief. :)

I mean it is in a level, but your aio_notify() patch still can improve virtioblk dataplane performance some, in my test, with 5~10K IOPS improvement, which should belong to same level, because my test is a bit coarse, :-)

IMO not only virtioblk can benefit from your patch, others can benefit too.

Definitely, but it's not the kind of patch I enjoy including 3 weeks from release. I have already broken enough eggs during the 2.1 development period. :)

Paolo

#10 lore raw
In reply to Ming Lei
Quoted 10 lines · Ming Lei
Quoted 9 lines · Stefan Hajnoczi

The coroutine pool reuses exited coroutines to make qemu_coroutine_create() cheap. The size of the pool is capped to prevent it from hogging memory after a period of high coroutine activity. Previously the max size was hardcoded to 64 but this doesn't scale with guest size.

A guest with lots of disks can do more parallel I/O and therefore requires a larger coroutine pool size. This series tries to solve the problem by scaling pool size according to the number of drives.

Quoted 7 lines
Quoted 3 lines

Ming: Please let me know if this eliminates the rt_sigprocmask system calls you are seeing. It should solve part of the performance regression you have seen in qemu.git/master virtio-blk dataplane.

With both the two coroutine patches and the block plug&unplug patches, performance of qemu.git/master virtio-blk dataplane can recover to level of QEMU 2.0.

/me does the happy dance

Thanks for your great efforts in investigating and fixing the regression!

Stefan