tests/qtest/libqmp.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
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
On Thu, Nov 26, 2026 at 05:07:43PM +0100, Thomas Huth wrote: > 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(-) Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> That's a nicer approach than my 5 minute PoC from a few months back that tried to keep around a buffer statically. With regards, Daniel -- |: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
© 2016 - 2026 Red Hat, Inc.