* [PATCH v2] net/filter: Optimize filter_send to coroutine
@ 2021-12-27 1:20 Rao Lei
2021-12-27 2:14 ` Zhang, Chen
0 siblings, 1 reply; 3+ messages in thread
From: Rao Lei @ 2021-12-27 1:20 UTC (permalink / raw)
To: chen.zhang, lizhijian, jasowang; +Cc: Rao Lei, qemu-devel, Li Zhijian
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>
---
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 f20240cc9f..34a63b5dbb 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.32.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* RE: [PATCH v2] net/filter: Optimize filter_send to coroutine
2021-12-27 1:20 [PATCH v2] net/filter: Optimize filter_send to coroutine Rao Lei
@ 2021-12-27 2:14 ` Zhang, Chen
2022-01-07 4:38 ` Jason Wang
0 siblings, 1 reply; 3+ messages in thread
From: Zhang, Chen @ 2021-12-27 2:14 UTC (permalink / raw)
To: Rao, Lei, lizhijian, jasowang; +Cc: qemu-devel, Li Zhijian
> -----Original Message-----
> From: Rao, Lei <lei.rao@intel.com>
> Sent: Monday, December 27, 2021 9:20 AM
> To: Zhang, Chen <chen.zhang@intel.com>; lizhijian@cn.fujitsu.com;
> jasowang@redhat.com
> Cc: qemu-devel@nongnu.org; Rao, Lei <lei.rao@intel.com>; Li Zhijian
> <lizhijian@fujitsu.com>
> Subject: [PATCH v2] net/filter: Optimize filter_send to coroutine
>
> 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>
Looks good to me.
Reviewed-by: Zhang Chen <chen.zhang@intel.com>
Thanks
Chen
> ---
> 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
> f20240cc9f..34a63b5dbb 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.32.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] net/filter: Optimize filter_send to coroutine
2021-12-27 2:14 ` Zhang, Chen
@ 2022-01-07 4:38 ` Jason Wang
0 siblings, 0 replies; 3+ messages in thread
From: Jason Wang @ 2022-01-07 4:38 UTC (permalink / raw)
To: Zhang, Chen; +Cc: Rao, Lei, qemu-devel, lizhijian, Li Zhijian
On Mon, Dec 27, 2021 at 10:14 AM Zhang, Chen <chen.zhang@intel.com> wrote:
>
>
>
> > -----Original Message-----
> > From: Rao, Lei <lei.rao@intel.com>
> > Sent: Monday, December 27, 2021 9:20 AM
> > To: Zhang, Chen <chen.zhang@intel.com>; lizhijian@cn.fujitsu.com;
> > jasowang@redhat.com
> > Cc: qemu-devel@nongnu.org; Rao, Lei <lei.rao@intel.com>; Li Zhijian
> > <lizhijian@fujitsu.com>
> > Subject: [PATCH v2] net/filter: Optimize filter_send to coroutine
> >
> > 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>
>
> Looks good to me.
> Reviewed-by: Zhang Chen <chen.zhang@intel.com>
Applied.
Thanks
>
> Thanks
> Chen
>
> > ---
> > 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
> > f20240cc9f..34a63b5dbb 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.32.0
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-01-07 4:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-27 1:20 [PATCH v2] net/filter: Optimize filter_send to coroutine Rao Lei
2021-12-27 2:14 ` Zhang, Chen
2022-01-07 4:38 ` Jason Wang
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.