qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Jason Wang <jasowang@redhat.com>
To: peter.maydell@linaro.org
Cc: Jason Wang <jasowang@redhat.com>,
	Zhang Chen <chen.zhang@intel.com>, Rao Lei <lei.rao@intel.com>,
	qemu-devel@nongnu.org, Li Zhijian <lizhijian@fujitsu.com>
Subject: [PULL 06/13] net/filter: Optimize filter_send to coroutine
Date: Mon, 10 Jan 2022 11:39:53 +0800	[thread overview]
Message-ID: <20220110034000.20221-7-jasowang@redhat.com> (raw)
In-Reply-To: <20220110034000.20221-1-jasowang@redhat.com>

From: Rao Lei <lei.rao@intel.com>

This patch is to improve the logic of QEMU main thread sleep code in
qemu_chr_write_buffer() where it can be blocked and can't run other
coroutines during COLO IO stress test.

Our approach is to put filter_send() in a coroutine. In this way,
filter_send() will call qemu_coroutine_yield() in qemu_co_sleep_ns(),
so that it can be scheduled out and QEMU main thread has opportunity to
run other tasks.

Signed-off-by: Lei Rao <lei.rao@intel.com>
Signed-off-by: Zhang Chen <chen.zhang@intel.com>
Reviewed-by: Li Zhijian <lizhijian@fujitsu.com>
Reviewed-by: Zhang Chen <chen.zhang@intel.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 net/filter-mirror.c | 66 ++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 53 insertions(+), 13 deletions(-)

diff --git a/net/filter-mirror.c b/net/filter-mirror.c
index f20240c..34a63b5 100644
--- a/net/filter-mirror.c
+++ b/net/filter-mirror.c
@@ -20,6 +20,7 @@
 #include "chardev/char-fe.h"
 #include "qemu/iov.h"
 #include "qemu/sockets.h"
+#include "block/aio-wait.h"
 
 #define TYPE_FILTER_MIRROR "filter-mirror"
 typedef struct MirrorState MirrorState;
@@ -42,20 +43,21 @@ struct MirrorState {
     bool vnet_hdr;
 };
 
-static int filter_send(MirrorState *s,
-                       const struct iovec *iov,
-                       int iovcnt)
+typedef struct FilterSendCo {
+    MirrorState *s;
+    char *buf;
+    ssize_t size;
+    bool done;
+    int ret;
+} FilterSendCo;
+
+static int _filter_send(MirrorState *s,
+                       char *buf,
+                       ssize_t size)
 {
     NetFilterState *nf = NETFILTER(s);
     int ret = 0;
-    ssize_t size = 0;
     uint32_t len = 0;
-    char *buf;
-
-    size = iov_size(iov, iovcnt);
-    if (!size) {
-        return 0;
-    }
 
     len = htonl(size);
     ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
@@ -80,10 +82,7 @@ static int filter_send(MirrorState *s,
         }
     }
 
-    buf = g_malloc(size);
-    iov_to_buf(iov, iovcnt, 0, buf, size);
     ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size);
-    g_free(buf);
     if (ret != size) {
         goto err;
     }
@@ -94,6 +93,47 @@ err:
     return ret < 0 ? ret : -EIO;
 }
 
+static void coroutine_fn filter_send_co(void *opaque)
+{
+    FilterSendCo *data = opaque;
+
+    data->ret = _filter_send(data->s, data->buf, data->size);
+    data->done = true;
+    g_free(data->buf);
+    aio_wait_kick();
+}
+
+static int filter_send(MirrorState *s,
+                       const struct iovec *iov,
+                       int iovcnt)
+{
+    ssize_t size = iov_size(iov, iovcnt);
+    char *buf = NULL;
+
+    if (!size) {
+        return 0;
+    }
+
+    buf = g_malloc(size);
+    iov_to_buf(iov, iovcnt, 0, buf, size);
+
+    FilterSendCo data = {
+        .s = s,
+        .size = size,
+        .buf = buf,
+        .ret = 0,
+    };
+
+    Coroutine *co = qemu_coroutine_create(filter_send_co, &data);
+    qemu_coroutine_enter(co);
+
+    while (!data.done) {
+        aio_poll(qemu_get_aio_context(), true);
+    }
+
+    return data.ret;
+}
+
 static void redirector_to_filter(NetFilterState *nf,
                                  const uint8_t *buf,
                                  int len)
-- 
2.7.4



  parent reply	other threads:[~2022-01-10  3:47 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-10  3:39 [PULL 00/13] Net patches Jason Wang
2022-01-10  3:39 ` [PULL 01/13] hw/net/vmxnet3: Log guest-triggerable errors using LOG_GUEST_ERROR Jason Wang
2022-01-10  3:39 ` [PULL 02/13] net/tap: Set return code on failure Jason Wang
2022-01-10  3:39 ` [PULL 03/13] net: Fix uninitialized data usage Jason Wang
2022-01-10  3:39 ` [PULL 04/13] net/colo-compare.c: Optimize compare order for performance Jason Wang
2022-01-10  3:39 ` [PULL 05/13] net/colo-compare.c: Update the default value comments Jason Wang
2022-01-10  3:39 ` Jason Wang [this message]
2022-01-10  3:39 ` [PULL 07/13] net/vmnet: add vmnet dependency and customizable option Jason Wang
2022-01-10  3:39 ` [PULL 08/13] net/vmnet: add vmnet backends to qapi/net Jason Wang
2022-01-10  3:39 ` [PULL 09/13] net/vmnet: implement shared mode (vmnet-shared) Jason Wang
2022-01-10  3:39 ` [PULL 10/13] net/vmnet: implement host mode (vmnet-host) Jason Wang
2022-01-10  3:39 ` [PULL 11/13] net/vmnet: implement bridged mode (vmnet-bridged) Jason Wang
2022-01-10  3:39 ` [PULL 12/13] net/vmnet: update qemu-options.hx Jason Wang
2022-01-10  3:40 ` [PULL 13/13] net/vmnet: update MAINTAINERS list Jason Wang
2022-01-10 16:49 ` [PULL 00/13] Net patches Peter Maydell
2022-01-11  2:09   ` Jason Wang
2022-01-11 22:02     ` Vladislav Yaroshchuk
2022-01-12  5:39       ` Jason Wang
2022-01-12  6:19         ` Vladislav Yaroshchuk
2022-01-12  7:49           ` Jason Wang
2022-01-12  7:10         ` Roman Bolshakov
2022-01-12  7:51           ` Jason Wang
2022-01-12 13:16             ` Vladislav Yaroshchuk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220110034000.20221-7-jasowang@redhat.com \
    --to=jasowang@redhat.com \
    --cc=chen.zhang@intel.com \
    --cc=lei.rao@intel.com \
    --cc=lizhijian@fujitsu.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).