All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: Fabiano Rosas <farosas@suse.de>, 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: [PATCH for-12.0] tests/qtest: Speed up qmp_fd_receive() by doing fewer systemcalls
Date: Thu, 26 Nov 2026 17:07:43 +0100	[thread overview]
Message-ID: <20261126160743.276957-1-thuth@redhat.com> (raw)

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);
-- 
2.56.1



             reply	other threads:[~2026-11-26 16:08 UTC|newest]

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

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=20261126160743.276957-1-thuth@redhat.com \
    --to=thuth@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=berrange@redhat.com \
    --cc=farosas@suse.de \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --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 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.