Blame 0112-glib-fix-g_poll-early-timeout-on-windows.patch

0410ae
From 488f948b9f89a0dd90ed465f5d692230af2ecb05 Mon Sep 17 00:00:00 2001
0410ae
From: Sangho Park <sangho1206.park@samsung.com>
0410ae
Date: Thu, 8 May 2014 12:47:10 +0400
0410ae
Subject: [PATCH] glib: fix g_poll early timeout on windows
0410ae
0410ae
g_poll has a problem on Windows when using
0410ae
timeouts < 10ms, in glib/gpoll.c:
0410ae
0410ae
/* If not, and we have a significant timeout, poll again with
0410ae
 * timeout then. Note that this will return indication for only
0410ae
 * one event, or only for messages. We ignore timeouts less than
0410ae
 * ten milliseconds as they are mostly pointless on Windows, the
0410ae
 * MsgWaitForMultipleObjectsEx() call will timeout right away
0410ae
 * anyway.
0410ae
 */
0410ae
if (retval == 0 && (timeout == INFINITE || timeout >= 10))
0410ae
  retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, timeout);
0410ae
0410ae
so whenever g_poll is called with timeout < 10ms it does
0410ae
a quick poll instead of wait, this causes significant performance
0410ae
degradation of QEMU, thus we should use WaitForMultipleObjectsEx
0410ae
directly
0410ae
0410ae
Signed-off-by: Stanislav Vorobiov <s.vorobiov@samsung.com>
0410ae
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
0410ae
(cherry picked from commit 5a007547df76446ab891df93ebc55749716609bf)
0410ae
---
0410ae
 include/glib-compat.h |   9 +++-
0410ae
 util/oslib-win32.c    | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++
0410ae
 2 files changed, 120 insertions(+), 1 deletion(-)
0410ae
0410ae
diff --git a/include/glib-compat.h b/include/glib-compat.h
0410ae
index 8d25900..1280fb2 100644
0410ae
--- a/include/glib-compat.h
0410ae
+++ b/include/glib-compat.h
0410ae
@@ -24,7 +24,14 @@ static inline guint g_timeout_add_seconds(guint interval, GSourceFunc function,
0410ae
 }
0410ae
 #endif
0410ae
 
0410ae
-#if !GLIB_CHECK_VERSION(2, 20, 0)
0410ae
+#ifdef _WIN32
0410ae
+/*
0410ae
+ * g_poll has a problem on Windows when using
0410ae
+ * timeouts < 10ms, so use wrapper.
0410ae
+ */
0410ae
+#define g_poll(fds, nfds, timeout) g_poll_fixed(fds, nfds, timeout)
0410ae
+gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout);
0410ae
+#elif !GLIB_CHECK_VERSION(2, 20, 0)
0410ae
 /*
0410ae
  * Glib before 2.20.0 doesn't implement g_poll, so wrap it to compile properly
0410ae
  * on older systems.
0410ae
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
0410ae
index 93f7d35..69552f7 100644
0410ae
--- a/util/oslib-win32.c
0410ae
+++ b/util/oslib-win32.c
0410ae
@@ -238,3 +238,115 @@ char *qemu_get_exec_dir(void)
0410ae
 {
0410ae
     return g_strdup(exec_dir);
0410ae
 }
0410ae
+
0410ae
+/*
0410ae
+ * g_poll has a problem on Windows when using
0410ae
+ * timeouts < 10ms, in glib/gpoll.c:
0410ae
+ *
0410ae
+ * // If not, and we have a significant timeout, poll again with
0410ae
+ * // timeout then. Note that this will return indication for only
0410ae
+ * // one event, or only for messages. We ignore timeouts less than
0410ae
+ * // ten milliseconds as they are mostly pointless on Windows, the
0410ae
+ * // MsgWaitForMultipleObjectsEx() call will timeout right away
0410ae
+ * // anyway.
0410ae
+ *
0410ae
+ * if (retval == 0 && (timeout == INFINITE || timeout >= 10))
0410ae
+ *   retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, timeout);
0410ae
+ *
0410ae
+ * So whenever g_poll is called with timeout < 10ms it does
0410ae
+ * a quick poll instead of wait, this causes significant performance
0410ae
+ * degradation of QEMU, thus we should use WaitForMultipleObjectsEx
0410ae
+ * directly
0410ae
+ */
0410ae
+gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout)
0410ae
+{
0410ae
+    guint i;
0410ae
+    HANDLE handles[MAXIMUM_WAIT_OBJECTS];
0410ae
+    gint nhandles = 0;
0410ae
+    int num_completed = 0;
0410ae
+
0410ae
+    for (i = 0; i < nfds; i++) {
0410ae
+        gint j;
0410ae
+
0410ae
+        if (fds[i].fd <= 0) {
0410ae
+            continue;
0410ae
+        }
0410ae
+
0410ae
+        /* don't add same handle several times
0410ae
+         */
0410ae
+        for (j = 0; j < nhandles; j++) {
0410ae
+            if (handles[j] == (HANDLE)fds[i].fd) {
0410ae
+                break;
0410ae
+            }
0410ae
+        }
0410ae
+
0410ae
+        if (j == nhandles) {
0410ae
+            if (nhandles == MAXIMUM_WAIT_OBJECTS) {
0410ae
+                fprintf(stderr, "Too many handles to wait for!\n");
0410ae
+                break;
0410ae
+            } else {
0410ae
+                handles[nhandles++] = (HANDLE)fds[i].fd;
0410ae
+            }
0410ae
+        }
0410ae
+    }
0410ae
+
0410ae
+    for (i = 0; i < nfds; ++i) {
0410ae
+        fds[i].revents = 0;
0410ae
+    }
0410ae
+
0410ae
+    if (timeout == -1) {
0410ae
+        timeout = INFINITE;
0410ae
+    }
0410ae
+
0410ae
+    if (nhandles == 0) {
0410ae
+        if (timeout == INFINITE) {
0410ae
+            return -1;
0410ae
+        } else {
0410ae
+            SleepEx(timeout, TRUE);
0410ae
+            return 0;
0410ae
+        }
0410ae
+    }
0410ae
+
0410ae
+    while (1) {
0410ae
+        DWORD res;
0410ae
+        gint j;
0410ae
+
0410ae
+        res = WaitForMultipleObjectsEx(nhandles, handles, FALSE,
0410ae
+            timeout, TRUE);
0410ae
+
0410ae
+        if (res == WAIT_FAILED) {
0410ae
+            for (i = 0; i < nfds; ++i) {
0410ae
+                fds[i].revents = 0;
0410ae
+            }
0410ae
+
0410ae
+            return -1;
0410ae
+        } else if ((res == WAIT_TIMEOUT) || (res == WAIT_IO_COMPLETION) ||
0410ae
+                   ((int)res < (int)WAIT_OBJECT_0) ||
0410ae
+                   (res >= (WAIT_OBJECT_0 + nhandles))) {
0410ae
+            break;
0410ae
+        }
0410ae
+
0410ae
+        for (i = 0; i < nfds; ++i) {
0410ae
+            if (handles[res - WAIT_OBJECT_0] == (HANDLE)fds[i].fd) {
0410ae
+                fds[i].revents = fds[i].events;
0410ae
+            }
0410ae
+        }
0410ae
+
0410ae
+        ++num_completed;
0410ae
+
0410ae
+        if (nhandles <= 1) {
0410ae
+            break;
0410ae
+        }
0410ae
+
0410ae
+        /* poll the rest of the handles
0410ae
+         */
0410ae
+        for (j = res - WAIT_OBJECT_0 + 1; j < nhandles; j++) {
0410ae
+            handles[j - 1] = handles[j];
0410ae
+        }
0410ae
+        --nhandles;
0410ae
+
0410ae
+        timeout = 0;
0410ae
+    }
0410ae
+
0410ae
+    return num_completed;
0410ae
+}