From patchwork Tue Apr 15 08:41:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Vorobiov X-Patchwork-Id: 339200 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 68FD314007B for ; Tue, 15 Apr 2014 18:42:15 +1000 (EST) Received: from localhost ([::1]:47612 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WZywX-0001V2-B1 for incoming@patchwork.ozlabs.org; Tue, 15 Apr 2014 04:42:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59321) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WZyw9-000178-QK for qemu-devel@nongnu.org; Tue, 15 Apr 2014 04:41:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WZyw2-0000BT-Ft for qemu-devel@nongnu.org; Tue, 15 Apr 2014 04:41:49 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:47993) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WZyw2-0008U9-95 for qemu-devel@nongnu.org; Tue, 15 Apr 2014 04:41:42 -0400 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0N42006IYDHDGE70@mailout1.w1.samsung.com> for qemu-devel@nongnu.org; Tue, 15 Apr 2014 09:41:37 +0100 (BST) X-AuditID: cbfec7f4-b7f796d000005a13-99-534cf0c2dff5 Received: from eusync1.samsung.com ( [203.254.199.211]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 76.11.23059.2C0FC435; Tue, 15 Apr 2014 09:41:38 +0100 (BST) Received: from stas-pc.rnd.samsung.ru ([106.109.9.208]) by eusync1.samsung.com (Oracle Communications Messaging Server 7u4-23.01 (7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0N42000MTDH9J190@eusync1.samsung.com>; Tue, 15 Apr 2014 09:41:38 +0100 (BST) From: Stanislav Vorobiov To: qemu-devel@nongnu.org Date: Tue, 15 Apr 2014 12:41:32 +0400 Message-id: <1397551292-1010-1-git-send-email-s.vorobiov@samsung.com> X-Mailer: git-send-email 1.7.9.5 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrJJMWRmVeSWpSXmKPExsVy+t/xy7qHPvgEG6y6wmRxvHcHi8W9pyYW N9afZnNg9nhybTOTR9+WVYwBTFFcNimpOZllqUX6dglcGdubnzMWvJOp+PJ0J2sD4yOhLkZO DgkBE4nJJ++wQthiEhfurWcDsYUEljJKzNrC1MXIBWT3MkncODEJrIhNwFBiw9mpzCC2iICk xO+u02A2s4CFxL8Jn8GahQUcJZachBjEIqAq8a77PhOIzSvgKnHx9Wb2LkYOoGUKEnMm2Uxg 5F7AyLCKUTS1NLmgOCk911CvODG3uDQvXS85P3cTI8S7X3YwLj5mdYhRgINRiYfX4o13sBBr YllxZe4hRgkOZiUR3i8vfYKFeFMSK6tSi/Lji0pzUosPMTJxcEo1MKqekrCsXChwTbkhNbnY b2dIWdX9m+9KdnSv+J2+8PmRQI12qR9Fy0+Lr41uYlwgu0fgwrK3B24lr0ue8jo6d+eCdrNt ocJHD0RmMgV27JL/f2+iwxMX599WC7S4Fe6uOr1cK3TlOSv2npMlUt+1v61eUqhxUyrXt3Kb xtdnzyOOnmKJkk87GKXEUpyRaKjFXFScCABvegHTzAEAAA== X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 210.118.77.11 Cc: syeon.hwang@samsung.com, sangho1206.park@samsung.com Subject: [Qemu-devel] [PATCH] timer: fix qemu_poll_ns early timeout on windows X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Sangho Park g_poll has a problem on windows when using timeouts < 10ms, in glib/gpoll.c: /* If not, and we have a significant timeout, poll again with * timeout then. Note that this will return indication for only * one event, or only for messages. We ignore timeouts less than * ten milliseconds as they are mostly pointless on Windows, the * MsgWaitForMultipleObjectsEx() call will timeout right away * anyway. */ if (retval == 0 && (timeout == INFINITE || timeout >= 10)) retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, timeout); so whenever g_poll is called with timeout < 10ms it does a quick poll instead of wait, this causes significant performance degradation of qemu, thus we should use WaitForMultipleObjectsEx directly Signed-off-by: Stanislav Vorobiov --- qemu-timer.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/qemu-timer.c b/qemu-timer.c index e15ce47..9fb92cb 100644 --- a/qemu-timer.c +++ b/qemu-timer.c @@ -315,6 +315,97 @@ int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout) ts.tv_nsec = timeout % 1000000000LL; return ppoll((struct pollfd *)fds, nfds, &ts, NULL); } +#elif defined(_WIN32) + guint i; + HANDLE handles[MAXIMUM_WAIT_OBJECTS]; + gint nhandles = 0; + int num_completed = 0; + gint timeout_ms = qemu_timeout_ns_to_ms(timeout); + + for (i = 0; i < nfds; i++) { + gint j; + + if (fds[i].fd <= 0) { + continue; + } + + /* don't add same handle several times + */ + for (j = 0; j < nhandles; j++) { + if (handles[j] == (HANDLE)fds[i].fd) { + break; + } + } + + if (j == nhandles) { + if (nhandles == MAXIMUM_WAIT_OBJECTS) { + fprintf(stderr, "Too many handles to wait for!\n"); + break; + } else { + handles[nhandles++] = (HANDLE)fds[i].fd; + } + } + } + + for (i = 0; i < nfds; ++i) { + fds[i].revents = 0; + } + + if (timeout_ms == -1) { + timeout_ms = INFINITE; + } + + if (nhandles == 0) { + if (timeout_ms == INFINITE) { + return -1; + } else { + SleepEx(timeout_ms, TRUE); + return 0; + } + } + + while (1) { + DWORD res; + gint j; + + res = WaitForMultipleObjectsEx(nhandles, handles, FALSE, + timeout_ms, TRUE); + + if (res == WAIT_FAILED) { + for (i = 0; i < nfds; ++i) { + fds[i].revents = 0; + } + + return -1; + } else if ((res == WAIT_TIMEOUT) || (res == WAIT_IO_COMPLETION) || + ((int)res < WAIT_OBJECT_0) || + (res >= (WAIT_OBJECT_0 + nhandles))) { + break; + } + + for (i = 0; i < nfds; ++i) { + if (handles[res - WAIT_OBJECT_0] == (HANDLE)fds[i].fd) { + fds[i].revents = fds[i].events; + } + } + + ++num_completed; + + if (nhandles <= 1) { + break; + } + + /* poll the rest of the handles + */ + for (j = res - WAIT_OBJECT_0 + 1; j < nhandles; j++) { + handles[j - 1] = handles[j]; + } + --nhandles; + + timeout_ms = 0; + } + + return num_completed; #else return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout)); #endif