From patchwork Wed Apr 27 11:10:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 8955281 Return-Path: X-Original-To: patchwork-dm-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 7CBFE9F65D for ; Wed, 27 Apr 2016 11:15:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7D0D4200F0 for ; Wed, 27 Apr 2016 11:15:14 +0000 (UTC) Received: from mx3-phx2.redhat.com (mx3-phx2.redhat.com [209.132.183.24]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5626C200DC for ; Wed, 27 Apr 2016 11:15:13 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx3-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u3RBBsbN029210; Wed, 27 Apr 2016 07:11:54 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u3RBBIk7012330 for ; Wed, 27 Apr 2016 07:11:18 -0400 Received: from mx1.redhat.com (ext-mx02.extmail.prod.ext.phx2.redhat.com [10.5.110.26]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3RBBIwE006233 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 27 Apr 2016 07:11:18 -0400 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7D77A7F09A; Wed, 27 Apr 2016 11:11:17 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 9A352AD92; Wed, 27 Apr 2016 11:11:09 +0000 (UTC) From: Hannes Reinecke To: Christophe Varoqui Date: Wed, 27 Apr 2016 13:10:32 +0200 Message-Id: <1461755458-29225-32-git-send-email-hare@suse.de> In-Reply-To: <1461755458-29225-1-git-send-email-hare@suse.de> References: <1461755458-29225-1-git-send-email-hare@suse.de> X-RedHat-Spam-Score: -0.607 (BAYES_50, DCC_REPUT_00_12, RCVD_IN_DNSWL_NONE, RP_MATCHES_RCVD, SPF_PASS) 195.135.220.15 mx2.suse.de 195.135.220.15 mx2.suse.de X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Scanned-By: MIMEDefang 2.75 on 10.5.110.26 X-loop: dm-devel@redhat.com Cc: Hannes Reinecke , dm-devel@redhat.com, Mike Snitzer Subject: [dm-devel] [PATCH 31/57] multipathd: improve uxlsnr X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Improve uxlsnr by adding a lock around the poll structure and not reallocating memory for every poll, thereby significantly speedup processing. Signed-off-by: Hannes Reinecke --- multipathd/main.c | 6 ++-- multipathd/uxlsnr.c | 84 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/multipathd/main.c b/multipathd/main.c index 61b82f6..c1b94bd 100644 --- a/multipathd/main.c +++ b/multipathd/main.c @@ -966,8 +966,10 @@ uevqloop (void * ap) static void * uxlsnrloop (void * ap) { - if (cli_init()) + if (cli_init()) { + condlog(1, "Failed to init uxsock listener"); return NULL; + } set_handler_callback(LIST+PATHS, cli_list_paths); set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt); @@ -1278,7 +1280,7 @@ check_path (struct vectors * vecs, struct path * pp) sysfs_attr_set_value(pp->udev, "uevent", "change", strlen("change")); return 0; - } + } /* * provision a next check soonest, diff --git a/multipathd/uxlsnr.c b/multipathd/uxlsnr.c index e5c5d90..77efa8a 100644 --- a/multipathd/uxlsnr.c +++ b/multipathd/uxlsnr.c @@ -44,6 +44,8 @@ struct client { int fd; }; +#define MIN_POLLS 1023 + LIST_HEAD(clients); pthread_mutex_t client_lock = PTHREAD_MUTEX_INITIALIZER; struct pollfd *polls; @@ -133,6 +135,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data) char *inbuf; char *reply; sigset_t mask; + int old_clients = MIN_POLLS; ux_sock = ux_socket_listen(DEFAULT_SOCKET); @@ -142,20 +145,22 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data) } if (!conf) { - condlog(1, "configuration changed"); + condlog(1, "uxsock: configuration changed"); return NULL; } - timeout = conf->uxsock_timeout; - pthread_cleanup_push(uxsock_cleanup, NULL); - polls = (struct pollfd *)MALLOC(0); + condlog(3, "uxsock: startup listener"); + polls = (struct pollfd *)MALLOC(MIN_POLLS + 1); + if (!polls) { + condlog(0, "uxsock: failed to allocate poll fds"); + return NULL; + } pthread_sigmask(SIG_SETMASK, NULL, &mask); sigdelset(&mask, SIGHUP); sigdelset(&mask, SIGUSR1); while (1) { - struct pollfd *new; struct client *c, *tmp; int i, poll_count, num_clients; @@ -173,19 +178,25 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data) list_for_each_entry(c, &clients, node) { num_clients++; } - new = REALLOC(polls, (1+num_clients) * sizeof(*polls)); - /* If we can't allocate poliing space for the new client, - * close it */ - if (!new) { - if (!num_clients) { - condlog(1, "can't listen for new clients"); - return NULL; + if (num_clients != old_clients) { + struct pollfd *new; + if (num_clients < MIN_POLLS) { + new = REALLOC(polls, (1 + MIN_POLLS) * + sizeof(struct pollfd)); + } else { + new = REALLOC(polls, (1+num_clients) * + sizeof(struct pollfd)); } - dead_client(list_entry(clients.prev, - typeof(struct client), node)); - } - else + if (!new) { + pthread_mutex_unlock(&client_lock); + condlog(0, "%s: failed to realloc %d poll fds", + "uxsock", 1 + num_clients); + pthread_yield(); + continue; + } + num_clients = old_clients; polls = new; + } polls[0].fd = ux_sock; polls[0].events = POLLIN; @@ -208,8 +219,8 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data) } /* something went badly wrong! */ - condlog(0, "poll"); - pthread_exit(NULL); + condlog(0, "uxsock: poll failed with %d", errno); + break; } if (poll_count == 0) @@ -230,7 +241,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data) } pthread_mutex_unlock(&client_lock); if (!c) { - condlog(3, "cli%d: invalid fd %d", + condlog(4, "cli%d: new fd %d", i, polls[i].fd); continue; } @@ -238,24 +249,27 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data) start_time.tv_sec = 0; if (recv_packet(c->fd, &inbuf, timeout) != 0) { dead_client(c); - } else { - condlog(4, "Got request [%s]", inbuf); - uxsock_trigger(inbuf, &reply, &rlen, - trigger_data); - if (reply) { - if (send_packet(c->fd, - reply) != 0) { - dead_client(c); - } - condlog(4, "Reply [%d bytes]", - rlen); - FREE(reply); - reply = NULL; + continue; + } + condlog(4, "cli[%d]: Got request [%s]", + i, inbuf); + uxsock_trigger(inbuf, &reply, &rlen, + trigger_data); + if (reply) { + if (send_packet(c->fd, + reply) != 0) { + dead_client(c); + } else { + condlog(4, "cli[%d]: " + "Reply [%d bytes]", + i, rlen); } - check_timeout(start_time, inbuf, - timeout); - FREE(inbuf); + FREE(reply); + reply = NULL; } + check_timeout(start_time, inbuf, + timeout); + FREE(inbuf); } }