From patchwork Fri Jul 9 17:19:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Ivanov X-Patchwork-Id: 1503300 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GM0JQ5bLQz9sSs for ; Sat, 10 Jul 2021 03:19:18 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 2156F40620; Fri, 9 Jul 2021 17:19:16 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id IHM1n65uGTvy; Fri, 9 Jul 2021 17:19:15 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp2.osuosl.org (Postfix) with ESMTPS id 5B67640155; Fri, 9 Jul 2021 17:19:14 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 1D43AC001A; Fri, 9 Jul 2021 17:19:14 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) by lists.linuxfoundation.org (Postfix) with ESMTP id C9D58C000E for ; Fri, 9 Jul 2021 17:19:12 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id A690640603 for ; Fri, 9 Jul 2021 17:19:12 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NtGMrxJyzWpg for ; Fri, 9 Jul 2021 17:19:12 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 Received: from www.kot-begemot.co.uk (ivanoab7.miniserver.com [37.128.132.42]) by smtp2.osuosl.org (Postfix) with ESMTPS id E2BBF405F5 for ; Fri, 9 Jul 2021 17:19:11 +0000 (UTC) Received: from tun252.jain.kot-begemot.co.uk ([192.168.18.6] helo=jain.kot-begemot.co.uk) by www.kot-begemot.co.uk with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1u9e-0005W9-Al for ovs-dev@openvswitch.org; Fri, 09 Jul 2021 17:19:10 +0000 Received: from jain.kot-begemot.co.uk ([192.168.3.3]) by jain.kot-begemot.co.uk with esmtp (Exim 4.92) (envelope-from ) id 1m1u9b-0002oY-A9; Fri, 09 Jul 2021 18:19:09 +0100 From: anton.ivanov@cambridgegreys.com To: ovs-dev@openvswitch.org Date: Fri, 9 Jul 2021 18:19:06 +0100 Message-Id: <20210709171906.10755-1-anton.ivanov@cambridgegreys.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett Cc: Anton Ivanov Subject: [ovs-dev] [PATCH] Optimize the poll loop for poll_immediate_wake() X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Anton Ivanov If we are not obtaining any useful information out of the poll(), such as is a fd busy or not, we do not need to do a poll() if an immediate_wake() has been requested. This cuts out all the pollfd hash additions, forming the poll arguments and the actual poll() after a call to poll_immediate_wake() Signed-off-by: Anton Ivanov --- lib/poll-loop.c | 67 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/lib/poll-loop.c b/lib/poll-loop.c index 4e751ff2c..8e418a8ce 100644 --- a/lib/poll-loop.c +++ b/lib/poll-loop.c @@ -53,6 +53,7 @@ struct poll_loop { * wake up immediately, or LLONG_MAX to wait forever. */ long long int timeout_when; /* In msecs as returned by time_msec(). */ const char *timeout_where; /* Where 'timeout_when' was set. */ + bool immediate_wake; }; static struct poll_loop *poll_loop(void); @@ -107,6 +108,13 @@ poll_create_node(int fd, HANDLE wevent, short int events, const char *where) COVERAGE_INC(poll_create_node); + if (loop->immediate_wake) { + /* We have been asked to bail out of this poll loop. + * There is no point to engage in yack shaving a poll hmap. + */ + return; + } + /* Both 'fd' and 'wevent' cannot be set. */ ovs_assert(!fd != !wevent); @@ -181,8 +189,15 @@ poll_wevent_wait_at(HANDLE wevent, const char *where) void poll_timer_wait_at(long long int msec, const char *where) { - long long int now = time_msec(); + long long int now; long long int when; + struct poll_loop *loop = poll_loop(); + + if (loop->immediate_wake) { + return; + } + + now = time_msec(); if (msec <= 0) { /* Wake up immediately. */ @@ -229,7 +244,9 @@ poll_timer_wait_until_at(long long int when, const char *where) void poll_immediate_wake_at(const char *where) { + struct poll_loop *loop = poll_loop(); poll_timer_wait_at(0, where); + loop->immediate_wake = true; } /* Logs, if appropriate, that the poll loop was awakened by an event @@ -320,10 +337,10 @@ poll_block(void) { struct poll_loop *loop = poll_loop(); struct poll_node *node; - struct pollfd *pollfds; + struct pollfd *pollfds = NULL; HANDLE *wevents = NULL; int elapsed; - int retval; + int retval = 0; int i; /* Register fatal signal events before actually doing any real work for @@ -335,34 +352,36 @@ poll_block(void) } timewarp_run(); - pollfds = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *pollfds); + if (!loop->immediate_wake) { + pollfds = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *pollfds); #ifdef _WIN32 - wevents = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *wevents); + wevents = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *wevents); #endif - /* Populate with all the fds and events. */ - i = 0; - HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) { - pollfds[i] = node->pollfd; + /* Populate with all the fds and events. */ + i = 0; + HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) { + pollfds[i] = node->pollfd; #ifdef _WIN32 - wevents[i] = node->wevent; - if (node->pollfd.fd && node->wevent) { - short int wsa_events = 0; - if (node->pollfd.events & POLLIN) { - wsa_events |= FD_READ | FD_ACCEPT | FD_CLOSE; + wevents[i] = node->wevent; + if (node->pollfd.fd && node->wevent) { + short int wsa_events = 0; + if (node->pollfd.events & POLLIN) { + wsa_events |= FD_READ | FD_ACCEPT | FD_CLOSE; + } + if (node->pollfd.events & POLLOUT) { + wsa_events |= FD_WRITE | FD_CONNECT | FD_CLOSE; + } + WSAEventSelect(node->pollfd.fd, node->wevent, wsa_events); } - if (node->pollfd.events & POLLOUT) { - wsa_events |= FD_WRITE | FD_CONNECT | FD_CLOSE; - } - WSAEventSelect(node->pollfd.fd, node->wevent, wsa_events); - } #endif - i++; - } + i++; + } - retval = time_poll(pollfds, hmap_count(&loop->poll_nodes), wevents, - loop->timeout_when, &elapsed); + retval = time_poll(pollfds, hmap_count(&loop->poll_nodes), wevents, + loop->timeout_when, &elapsed); + } if (retval < 0) { static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); VLOG_ERR_RL(&rl, "poll: %s", ovs_strerror(-retval)); @@ -381,6 +400,7 @@ poll_block(void) free_poll_nodes(loop); loop->timeout_when = LLONG_MAX; loop->timeout_where = NULL; + loop->immediate_wake = false; free(pollfds); free(wevents); @@ -417,6 +437,7 @@ poll_loop(void) loop = xzalloc(sizeof *loop); loop->timeout_when = LLONG_MAX; hmap_init(&loop->poll_nodes); + loop->immediate_wake = false; xpthread_setspecific(key, loop); } return loop;