All of lore.kernel.org
 help / color / mirror / Atom feed
From: Fabiano Rosas <farosas@suse.de>
To: Thomas Huth <thuth@redhat.com>, qemu-devel@nongnu.org
Cc: "Laurent Vivier" <lvivier@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	berrange@redhat.com
Subject: Re: [PATCH for-12.0] tests/qtest: Speed up qmp_fd_receive() by doing fewer systemcalls
Date: Mon, 30 Nov 2026 07:10:18 -0300	[thread overview]
Message-ID: <87pkvmfx4l.fsf@suse.de> (raw)
In-Reply-To: <20261126160743.276957-1-thuth@redhat.com>

Thomas Huth <thuth@redhat.com> writes:

> qmp_fd_receive() currently reads each byte individually via recv()
> from the socket. For tests that do a lot of traffic via QMP, this
> can be a significant overhead. Unfortunately, we cannot easily use
> a buffer for storing characters here, since we don't know the amount
> of characters in the current QMP reply yet, and thus we might read
> into the next reply already, i.e. the data that is required for the
> next call to qmp_fd_receive() instead. A buffer would be required to
> be stored on the caller side, and implementing this would be a major
> rework in this library and the code using it.
>
> So this patch follows a slightly different approach instead: Read the
> bytes into a buffer with MSG_PEEK first - this does not remove the
> bytes from the socket queue yet. Then we try to consume as many bytes
> from the buffer as possible until a full response has been recognized
> or the buffer has been fully consumed. At the end, we then can
> finally remove the amount of consumed bytes from the socket queue.
>
> This improvement especially speeds up the device-introspect-test in
> thorough mode.
>
> Before the change:
>
>  $ export QTEST_QEMU_BINARY=./qemu-system-aarch64
>  $ time tests/qtest/device-introspect-test -m thorough >/dev/null
>
>  real	5m8,861s
>  user	2m37,489s
>  sys	2m33,150s
>
> With this change included:
>
>  $ export QTEST_QEMU_BINARY=./qemu-system-aarch64
>  $ time tests/qtest/device-introspect-test -m thorough >/dev/null
>
>  real	3m21,598s
>  user	2m11,187s
>  sys	1m11,032s
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  tests/qtest/libqmp.c | 24 ++++++++++++++----------
>  1 file changed, 14 insertions(+), 10 deletions(-)
>
> diff --git a/tests/qtest/libqmp.c b/tests/qtest/libqmp.c
> index cd12bd26782..9dddeb1eed5 100644
> --- a/tests/qtest/libqmp.c
> +++ b/tests/qtest/libqmp.c
> @@ -68,23 +68,27 @@ QDict *qmp_fd_receive(int fd)
>      qmp.response = NULL;
>      json_message_parser_init(&qmp.parser, qmp_response, &qmp, NULL);
>      while (!qmp.response) {
> -        ssize_t len;
> -        char c;
> +        ssize_t len, consumed;
> +        char buf[512];
>  
> -        len = recv(fd, &c, 1, 0);
> -        if (len == -1 && errno == EINTR) {
> -            continue;
> -        }
> -
> -        if (len == -1 || len == 0) {
> +        /* Get copy of the available data without removing it from the queue */
> +        len = RETRY_ON_EINTR(recv(fd, buf, sizeof(buf), MSG_PEEK));
> +        if (len <= 0) {
>              fprintf(stderr, "Broken pipe\n");
>              abort();
>          }
>  
> +        for (consumed = 0; consumed < len && !qmp.response; consumed++) {
> +            json_message_parser_feed(&qmp.parser, &buf[consumed], 1);
> +        }
> +
>          if (log) {
> -            g_assert(write(2, &c, 1) == 1);
> +            g_assert(write(2, buf, consumed) == consumed);
>          }
> -        json_message_parser_feed(&qmp.parser, &c, 1);
> +
> +        /* Remove the handled characters from the queue */
> +        len = RETRY_ON_EINTR(recv(fd, buf, consumed, 0));
> +        g_assert_cmpint(len, ==, consumed);
>      }
>      if (log) {
>          g_assert(write(2, "\n", 1) == 1);

Tested-by: Fabiano Rosas <farosas@suse.de>

Queued for qtest-next.


      parent reply	other threads:[~2026-11-30 10:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 16:07 [PATCH for-12.0] tests/qtest: Speed up qmp_fd_receive() by doing fewer systemcalls Thomas Huth
2026-11-26 16:15 ` Daniel P. Berrangé
2026-11-30 10:10 ` Fabiano Rosas [this message]

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=87pkvmfx4l.fsf@suse.de \
    --to=farosas@suse.de \
    --cc=alex.bennee@linaro.org \
    --cc=berrange@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /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 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.