Skip to contentSkip to conversation

Some win32 fixes

PATCH Applied Marc-André Lureau 8 participants 13 messages Oct 1, 2019

lore
Share & tools
lore
#0 lore raw

cover Some win32 fixes

Hi,

Here are a few patches that should fix some busy looping issues already reported >2y ago
(https://lists.gnu.org/archive/html/qemu-devel/2017-01/msg00420.html),
and fixing test-char on win32.

hmm, do we have any automated testing/CI on Windows (beside just cross-compilation)?

thanks

Marc-André Lureau (3):
util: WSAEWOULDBLOCK on connect should map to EINPROGRESS
tests: skip serial test on windows
win32: fix main-loop busy loop on socket/fd event

 tests/test-char.c  | 4 ++--
 util/async.c       | 6 +++++-
 util/oslib-win32.c | 6 +++++-
 3 files changed, 12 insertions(+), 4 deletions(-)
Signature · 1 line
2.23.0
#1 lore raw

PATCH 1/3 util: WSAEWOULDBLOCK on connect should map to EINPROGRESS

In general, WSAEWOULDBLOCK can be mapped to EAGAIN as done by socket_error() (or EWOULDBLOCK). But for connect() with non-blocking sockets, it actually means the operation is in progress:

https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect
"The socket is marked as nonblocking and the connection cannot be completed immediately."

(this is also the behaviour implemented by GLib GSocket)

This fixes socket_can_bind_connect() test on win32.

---
 util/oslib-win32.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
util/oslib-win32.c +5 -1
index c62cd4328c..886e400d6a 100644
@@ -585,7 +585,11 @@ int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
     int ret;
     ret = connect(sockfd, addr, addrlen);
     if (ret < 0) {
-        errno = socket_error();
+        if (WSAGetLastError() == WSAEWOULDBLOCK) {
+            errno = EINPROGRESS;
+        } else {
+            errno = socket_error();
+        }
     }
     return ret;
 }
Signature · 1 line
2.23.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] util: WSAEWOULDBLOCK on connect should map to EINPROGRESS

Quoted 24 lines · Marc-André Lureau

In general, WSAEWOULDBLOCK can be mapped to EAGAIN as done by socket_error() (or EWOULDBLOCK). But for connect() with non-blocking sockets, it actually means the operation is in progress:

https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect
"The socket is marked as nonblocking and the connection cannot be completed immediately."

(this is also the behaviour implemented by GLib GSocket)

This fixes socket_can_bind_connect() test on win32.

---
  util/oslib-win32.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
util/oslib-win32.c +0 -0
index c62cd4328c..886e400d6a 100644
@@ -585,7 +585,11 @@ int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
      int ret;
      ret = connect(sockfd, addr, addrlen);

util/oslib-win32.c int qemu_connect_wrap(int sockfd, const struct sockaddr *addr, · line 595

Quoted 10 lines
util/oslib-win32.c +5 -1
      if (ret < 0) {
-        errno = socket_error();
+        if (WSAGetLastError() == WSAEWOULDBLOCK) {
+            errno = EINPROGRESS;
+        } else {
+            errno = socket_error();
+        }
      }
      return ret;
  }
#3 lore raw

PATCH 2/3 tests: skip serial test on windows

In reply to Marc-André Lureau

Serial test is currently hard-coded to /dev/null.

On Windows, serial chardev expect a COM: device, which may not be availble.

---
 tests/test-char.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/test-char.c +2 -2
index d62de1b088..45e42af290 100644
@@ -1103,7 +1103,7 @@ static void char_socket_server_two_clients_test(gconstpointer opaque)
 }
 
 
-#ifdef HAVE_CHARDEV_SERIAL
+#if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
 static void char_serial_test(void)
 {
     QemuOpts *opts;
@@ -1460,7 +1460,7 @@ int main(int argc, char **argv)
 #endif
 
     g_test_add_func("/char/udp", char_udp_test);
-#ifdef HAVE_CHARDEV_SERIAL
+#if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
     g_test_add_func("/char/serial", char_serial_test);
 #endif
     g_test_add_func("/char/hotswap", char_hotswap_test);
Signature · 1 line
2.23.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] tests: skip serial test on windows

Quoted 4 lines · Marc-André Lureau

Serial test is currently hard-coded to /dev/null.

On Windows, serial chardev expect a COM: device, which may not be availble.

"available"

Quoted 2 lines
Quoted 27 lines
---
  tests/test-char.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tests/test-char.c +2 -2
index d62de1b088..45e42af290 100644
@@ -1103,7 +1103,7 @@ static void char_socket_server_two_clients_test(gconstpointer opaque)
  }
 
 
-#ifdef HAVE_CHARDEV_SERIAL
+#if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
  static void char_serial_test(void)
  {
      QemuOpts *opts;
@@ -1460,7 +1460,7 @@ int main(int argc, char **argv)
  #endif
 
      g_test_add_func("/char/udp", char_udp_test);
-#ifdef HAVE_CHARDEV_SERIAL
+#if defined(HAVE_CHARDEV_SERIAL) && !defined(WIN32)
      g_test_add_func("/char/serial", char_serial_test);
  #endif
      g_test_add_func("/char/hotswap", char_hotswap_test);
#5 lore raw

PATCH 3/3 win32: fix main-loop busy loop on socket/fd event

In reply to Marc-André Lureau

Commit 05e514b1d4d5bd4209e2c8bbc76ff05c85a235f3 introduced an AIO context optimization to avoid calling event_notifier_test_and_clear() on ctx->notifier. On Windows, the same notifier is being used to wakeup the wait on socket events (see commit d3385eb448e38f828c78f8f68ec5d79c66a58b5d).

The ctx->notifier event is added to the gpoll sources in aio_set_event_notifier(), aio_ctx_check() should clear the event regardless of ctx->notified, since Windows sets the event by itself, bypassing the aio->notified. This fixes qemu not clearing the event resulting in a busy loop.

Paolo suggested to me on irc to call event_notifier_test_and_clear() after select() >0 from aio-win32.c's aio_prepare. Unfortunately, not all fds associated with ctx->notifiers are in AIO fd handlers set. (qemu_set_nonblock() in util/oslib-win32.c calls qemu_fd_register()).

This is essentially a v2 of a patch that was sent earlier:
https://lists.gnu.org/archive/html/qemu-devel/2017-01/msg00420.html

that resurfaced when James investigated Spice performance issues on Windows:
https://gitlab.freedesktop.org/spice/spice/issues/36

In order to test that patch, I simply tried running test-char on win32, and it hangs. Applying that patch solves it. QIO idle sources are not dispatched. I haven't investigated much further, I suspect source priorities and busy looping still come into play.

This version keeps the "notified" field, so event_notifier_poll() should still work as expected.

---
 util/async.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
util/async.c +5 -1
index 4e4c7af51e..ca83e32c7f 100644
@@ -354,7 +354,11 @@ void aio_notify(AioContext *ctx)
 
 void aio_notify_accept(AioContext *ctx)
 {
-    if (atomic_xchg(&ctx->notified, false)) {
+    if (atomic_xchg(&ctx->notified, false)
+#ifdef WIN32
+        || true
+#endif
+    ) {
         event_notifier_test_and_clear(&ctx->notifier);
     }
 }
Signature · 1 line
2.23.0
#6 lore raw

Re: [PATCH 3/3] win32: fix main-loop busy loop on socket/fd event

Quoted 44 lines · On Tue, 1 Oct 2019 17:26:09 +0400 Marc-André Lureau…

Commit 05e514b1d4d5bd4209e2c8bbc76ff05c85a235f3 introduced an AIO context optimization to avoid calling event_notifier_test_and_clear() on ctx->notifier. On Windows, the same notifier is being used to wakeup the wait on socket events (see commit d3385eb448e38f828c78f8f68ec5d79c66a58b5d).

The ctx->notifier event is added to the gpoll sources in aio_set_event_notifier(), aio_ctx_check() should clear the event regardless of ctx->notified, since Windows sets the event by itself, bypassing the aio->notified. This fixes qemu not clearing the event resulting in a busy loop.

Paolo suggested to me on irc to call event_notifier_test_and_clear() after select() >0 from aio-win32.c's aio_prepare. Unfortunately, not all fds associated with ctx->notifiers are in AIO fd handlers set. (qemu_set_nonblock() in util/oslib-win32.c calls qemu_fd_register()).

This is essentially a v2 of a patch that was sent earlier:
https://lists.gnu.org/archive/html/qemu-devel/2017-01/msg00420.html

that resurfaced when James investigated Spice performance issues on Windows:
https://gitlab.freedesktop.org/spice/spice/issues/36

In order to test that patch, I simply tried running test-char on win32, and it hangs. Applying that patch solves it. QIO idle sources are not dispatched. I haven't investigated much further, I suspect source priorities and busy looping still come into play.

This version keeps the "notified" field, so event_notifier_poll() should still work as expected.

---
 util/async.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
util/async.c +0 -0
index 4e4c7af51e..ca83e32c7f 100644
@@ -354,7 +354,11 @@ void aio_notify(AioContext *ctx)
 
 void aio_notify_accept(AioContext *ctx)

util/async.c void aio_notify(AioContext *ctx) · line 364

Quoted 10 lines
util/async.c +5 -1
 {
-    if (atomic_xchg(&ctx->notified, false)) {
+    if (atomic_xchg(&ctx->notified, false)
+#ifdef WIN32
+        || true
+#endif
+    ) {
         event_notifier_test_and_clear(&ctx->notifier);
     }
 }

I can confirm that this updated patch fixes my performance issue. The idle CPU usage drops from around 35% to around 2%. Moving the mouse now makes the usage go up, not down. :) Many thanks!

Regards,

Signature · 2 lines
James Le Cuirot (chewi)
Gentoo Linux Developer
#8 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 2 lines · Marc-André Lureau

hmm, do we have any automated testing/CI on Windows (beside just cross-compilation)?

Nope. :(

I did wonder whether it would be possible to get 'make check' passing under Wine, but never investigated...

thanks
-- PMM

#9 lore raw
Quoted 10 lines · Peter Maydell
Quoted 6 lines · Paolo Bonzini
Quoted 2 lines · Marc-André Lureau

hmm, do we have any automated testing/CI on Windows (beside just cross-compilation)?

Nope. :(

I did wonder whether it would be possible to get 'make check' passing under Wine, but never investigated...

Nelson Chen started to use Azure CI:

https://github.com/nelsonjchen/nc-qemu/blob/master/.azure/msys2-build.sh

He posted some notes here:

https://mindflakes.com/projects/nc-qemu/

I have this link in my CI TODO to try to upstream it and run 'make check' but it is not a high priority.

#10 lore raw
In reply to Peter Maydell

Am 01.10.2019 um 15:44 schrieb Peter Maydell:

Quoted 10 lines · Paolo Bonzini
Quoted 4 lines · Marc-André Lureau
Quoted 2 lines

hmm, do we have any automated testing/CI on Windows (beside just cross-compilation)?

Nope. :(

I did wonder whether it would be possible to get 'make check' passing under Wine, but never investigated...

thanks
-- PMM

Running "make check" under Wine had worked for me (well, at least partially) in the past. Of course it is not a full replacement for tests under Windows, but it should be sufficient to cover most of the functionality. Usually I also use Wine to examine bug reports for Windows.

Are there test servers with Wine available?

Regards,
Stefan Weil

#11 lore raw
In reply to Marc-André Lureau
Quoted 9 lines · Marc-André Lureau

Hi,

Here are a few patches that should fix some busy looping issues already reported >2y ago
(https://lists.gnu.org/archive/html/qemu-devel/2017-01/msg00420.html),
and fixing test-char on win32.

hmm, do we have any automated testing/CI on Windows (beside just cross-compilation)?

Not that I'm aware of. I did briefly look at CircleCI but never beyond playing with my own experiments. Ultimately I find it hard to care about Windows as I don't have a copy myself. Maybe someone else does care and would like to explore further?

FWIW I believe Travis has Windows support now and there is also AppVeyor. It's probably worth spreading the love around given how loaded our Travis jobs are.

Quoted 8 lines

thanks

Marc-André Lureau (3):
util: WSAEWOULDBLOCK on connect should map to EINPROGRESS
tests: skip serial test on windows
win32: fix main-loop busy loop on socket/fd event

Quoted 4 lines
 tests/test-char.c  | 4 ++--
 util/async.c       | 6 +++++-
 util/oslib-win32.c | 6 +++++-
 3 files changed, 12 insertions(+), 4 deletions(-)

--
Alex Bennée

#12 lore raw
In reply to Marc-André Lureau
Quoted 9 lines · Marc-André Lureau

Hi,

Here are a few patches that should fix some busy looping issues already reported >2y ago
(https://lists.gnu.org/archive/html/qemu-devel/2017-01/msg00420.html),
and fixing test-char on win32.

hmm, do we have any automated testing/CI on Windows (beside just cross-compilation)?

AFAIK we do not have any automated CI on Windows yet. But seems like both, Travis and Cirrus, have Windows support, so you could add a test to our .travis.yml or .cirrus.yml file, I think.

Thomas