Skip to contentSkip to conversation

Patch to adjust coroutine pool size adaptively

PATCH Superseded Hiroki Narukawa 2 participants 6 messages Jan 11, 2022 – Jan 28, 2022

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Patch to adjust coroutine pool size adaptively

Resending patch with fixing atomic access method to use qatomic_read().

We encountered random disk IO performance drop since qemu-5.0.0, and this patch fixes it.

Commit message in c740ad92 implied to adjust coroutine pool size adaptively, so I tried to implement this.

Could you review this patch?

Hiroki Narukawa (1):
util: adjust coroutine pool size to virtio block queue

 hw/block/virtio-blk.c    |  3 +++
 include/qemu/coroutine.h |  5 +++++
 util/qemu-coroutine.c    | 15 +++++++++++----
 3 files changed, 19 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/1 util: adjust coroutine pool size to virtio block queue

Coroutine pool size was 64 from long ago, and the basis was organized in the commit message in c740ad92.

At that time, virtio-blk queue-size and num-queue were not configuable, and equivalent values were 128 and 1.

Coroutine pool size 64 was fine then.

Later queue-size and num-queue got configuable, and default values were increased.

Coroutine pool with size 64 exhausts frequently with random disk IO in new size, and slows down.

This commit adjusts coroutine pool size adaptively with new values.

This commit adds 64 by default, but now coroutine is not only for block devices,

and is not too much burdon comparing with new default.

pool size of 128 * vCPUs.

---
 hw/block/virtio-blk.c    |  3 +++
 include/qemu/coroutine.h |  5 +++++
 util/qemu-coroutine.c    | 15 +++++++++++----
 3 files changed, 19 insertions(+), 4 deletions(-)
hw/block/virtio-blk.c +3 -0
index f139cd7cc9..726dbe14de 100644
@@ -32,6 +32,7 @@
 #include "hw/virtio/virtio-bus.h"
 #include "migration/qemu-file-types.h"
 #include "hw/virtio/virtio-access.h"
+#include "qemu/coroutine.h"
 
 /* Config size before the discard support (hide associated config fields) */
 #define VIRTIO_BLK_CFG_SIZE offsetof(struct virtio_blk_config, \
@@ -1222,6 +1223,8 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
     for (i = 0; i < conf->num_queues; i++) {
         virtio_add_queue(vdev, conf->queue_size, virtio_blk_handle_output);
     }
+    qemu_coroutine_increase_pool_batch_size(conf->num_queues * conf->queue_size
+                                            / 2);
     virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
     if (err != NULL) {
         error_propagate(errp, err);
include/qemu/coroutine.h +5 -0
index 4829ff373d..e52ed76ab2 100644
@@ -331,6 +331,11 @@ void qemu_co_sleep_wake(QemuCoSleep *w);
  */
 void coroutine_fn yield_until_fd_readable(int fd);
 
+/**
+ * Increase coroutine pool size
+ */
+void qemu_coroutine_increase_pool_batch_size(unsigned int additional_pool_size);
+
 #include "qemu/lockable.h"
 
 #endif /* QEMU_COROUTINE_H */
util/qemu-coroutine.c +11 -4
index 38fb6d3084..d5bd9d468f 100644
@@ -20,12 +20,14 @@
 #include "qemu/coroutine_int.h"
 #include "block/aio.h"
 
+/** Initial batch size is 64, and is increased on demand */
 enum {
-    POOL_BATCH_SIZE = 64,
+    POOL_INITIAL_BATCH_SIZE = 64,
 };
 
 /** Free list to speed up creation */
 static QSLIST_HEAD(, Coroutine) release_pool = QSLIST_HEAD_INITIALIZER(pool);
+static unsigned int pool_batch_size = POOL_INITIAL_BATCH_SIZE;
 static unsigned int release_pool_size;
 static __thread QSLIST_HEAD(, Coroutine) alloc_pool = QSLIST_HEAD_INITIALIZER(pool);
 static __thread unsigned int alloc_pool_size;
@@ -49,7 +51,7 @@ Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque)
     if (CONFIG_COROUTINE_POOL) {
         co = QSLIST_FIRST(&alloc_pool);
         if (!co) {
-            if (release_pool_size > POOL_BATCH_SIZE) {
+            if (release_pool_size > qatomic_read(&pool_batch_size)) {
                 /* Slow path; a good place to register the destructor, too.  */
                 if (!coroutine_pool_cleanup_notifier.notify) {
                     coroutine_pool_cleanup_notifier.notify = coroutine_pool_cleanup;
@@ -86,12 +88,12 @@ static void coroutine_delete(Coroutine *co)
     co->caller = NULL;
 
     if (CONFIG_COROUTINE_POOL) {
-        if (release_pool_size < POOL_BATCH_SIZE * 2) {
+        if (release_pool_size < qatomic_read(&pool_batch_size) * 2) {
             QSLIST_INSERT_HEAD_ATOMIC(&release_pool, co, pool_next);
             qatomic_inc(&release_pool_size);
             return;
         }
-        if (alloc_pool_size < POOL_BATCH_SIZE) {
+        if (alloc_pool_size < qatomic_read(&pool_batch_size)) {
             QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next);
             alloc_pool_size++;
             return;
@@ -202,3 +204,8 @@ AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co)
 {
     return co->ctx;
 }
+
+void qemu_coroutine_increase_pool_batch_size(unsigned int additional_pool_size)
+{
+    qatomic_add(&pool_batch_size, additional_pool_size);
+}
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/1] util: adjust coroutine pool size to virtio block queue

Quoted 38 lines · Hiroki Narukawa

Coroutine pool size was 64 from long ago, and the basis was organized in the commit message in c740ad92.

At that time, virtio-blk queue-size and num-queue were not configuable, and equivalent values were 128 and 1.

Coroutine pool size 64 was fine then.

Later queue-size and num-queue got configuable, and default values were increased.

Coroutine pool with size 64 exhausts frequently with random disk IO in new size, and slows down.

This commit adjusts coroutine pool size adaptively with new values.

This commit adds 64 by default, but now coroutine is not only for block devices,

and is not too much burdon comparing with new default.

pool size of 128 * vCPUs.

---
 hw/block/virtio-blk.c    |  3 +++
 include/qemu/coroutine.h |  5 +++++
 util/qemu-coroutine.c    | 15 +++++++++++----
 3 files changed, 19 insertions(+), 4 deletions(-)
hw/block/virtio-blk.c +1 -0
index f139cd7cc9..726dbe14de 100644
@@ -32,6 +32,7 @@
 #include "hw/virtio/virtio-bus.h"
 #include "migration/qemu-file-types.h"
 #include "hw/virtio/virtio-access.h"
+#include "qemu/coroutine.h"
 
 /* Config size before the discard support (hide associated config fields) */
 #define VIRTIO_BLK_CFG_SIZE offsetof(struct virtio_blk_config, \

hw/block/virtio-blk.c static void virtio_blk_device_realize(DeviceState *dev, Error **errp) · line 1227

Quoted 5 lines
hw/block/virtio-blk.c +2 -0
@@ -1222,6 +1223,8 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
     for (i = 0; i < conf->num_queues; i++) {
         virtio_add_queue(vdev, conf->queue_size, virtio_blk_handle_output);
     }
+    qemu_coroutine_increase_pool_batch_size(conf->num_queues * conf->queue_size
+                                            / 2);

Why "/ 2"?

hw/block/virtio-blk.c static void virtio_blk_device_realize(DeviceState *dev, Error **errp) · line 1230

Quoted 3 lines
hw/block/virtio-blk.c +0 -0
     virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
     if (err != NULL) {
         error_propagate(errp, err);

Please handle hot unplug (->unrealize()) so the coroutine pool shrinks down again when virtio-blk devices are removed.

My main concern is memory footprint. A burst of I/O can create many coroutines and they might never be used again. But I think we can deal with that using a timer in a separate future patch (if necessary).

Thanks,
Stefan

#3 lore raw

RE: [PATCH 1/1] util: adjust coroutine pool size to virtio block queue

Quoted 6 lines
Quoted 3 lines

/* Config size before the discard support (hide associated config
fields) */ #define VIRTIO_BLK_CFG_SIZE offsetof(struct virtio_blk_config, \ @@ -1222,6 +1223,8 @@ static void

virtio_blk_device_realize(DeviceState *dev, Error **errp)

Quoted 2 lines
     for (i = 0; i < conf->num_queues; i++) {
         virtio_add_queue(vdev, conf->queue_size,
Quoted 7 lines

virtio_blk_handle_output);

Quoted 2 lines
     }

+ qemu_coroutine_increase_pool_batch_size(conf->num_queues *

conf->queue_size

Quoted 1 line

+ / 2);

Why "/ 2"?

In my understanding, a request on virtio-blk consumes 2 entries each for rx and tx, so there can be num_queues * queue_size / 2 requests running at the same time.

Start point of this was that coroutine pool size was 64, queue_size equivalent size was 128, and num_queue equivalent size was 1 from long ago and seems to work well.
New value num_queues * queue_size / 2 also seems to work well as more overprovisioned value.

Quoted 8 lines
Quoted 4 lines
     virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
     if (err != NULL) {
         error_propagate(errp, err);

Please handle hot unplug (->unrealize()) so the coroutine pool shrinks down again when virtio-blk devices are removed.

Added it in v3 and resent it.

Quoted 3 lines

My main concern is memory footprint. A burst of I/O can create many coroutines and they might never be used again. But I think we can deal with that using a timer in a separate future patch (if necessary).

In my understanding coroutine pool size does not limit the peak memory consumption,
so I think even when coroutines are temporarily released, it is a room required for
qemu to keep running with accessing disk IO, which I think users does not imagine to be memory-consuming task.

Timer to release unused memory would be nice, but how serious is it?

#4 lore raw
In reply to Hiroki Narukawa

ping, how is the status of this patch?

Link for this patch v2 on patchew is this one: https://patchew.org/QEMU/[email protected]/
The last message on patch v1 is this one: https://lore.kernel.org/qemu-devel/TYCPR01MB8357E8D13D661265CDBB442C80519@TYCPR01MB8357.jpnprd01.prod.outlook.com/T/#u

The difference from v1 is use of qatomic_read().

Quoted 21 lines

Resending patch with fixing atomic access method to use qatomic_read().

We encountered random disk IO performance drop since qemu-5.0.0, and this patch fixes it.

Commit message in c740ad92 implied to adjust coroutine pool size adaptively, so I tried to implement this.

Could you review this patch?

Hiroki Narukawa (1):
util: adjust coroutine pool size to virtio block queue

 hw/block/virtio-blk.c    |  3 +++
 include/qemu/coroutine.h |  5 +++++
 util/qemu-coroutine.c    | 15 +++++++++++----
 3 files changed, 19 insertions(+), 4 deletions(-)

--
2.17.1