From patchwork Fri Aug 26 16:21:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 12956268 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1F466ECAAD4 for ; Fri, 26 Aug 2022 16:22:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661530955; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=qhbs2rShylzDT18KWz9u3IkNygyhs2yIE8sjrUSU8mI=; b=GKw05GAoHzlrQHyMSXjyHz8toZqthplJaajCIYO1ReO5cqBnGo6ZLxCQSQxHKhKBtgCNPF oW0Gaee4YSkagRah+zA24MGoN9JPSPaq7PgLV0DUm2hSsHlfrfe+BlUclB/GCKNRpb8T+b hML3v/X8D9duU/3Qz9q0UIQeejOPFsA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-647-ZsbB9YGINbKCnjZRnUme7Q-1; Fri, 26 Aug 2022 12:22:34 -0400 X-MC-Unique: ZsbB9YGINbKCnjZRnUme7Q-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 55777907339; Fri, 26 Aug 2022 16:22:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 42D5E40315A; Fri, 26 Aug 2022 16:22:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 374291946A58; Fri, 26 Aug 2022 16:22:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id B948F1946A70 for ; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id A88E6C15BBD; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast08.extmail.prod.ext.rdu2.redhat.com [10.11.55.24]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A4C85C15BBA for ; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8B51D3801F4D for ; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-54-GhvTXYiiOIipXCds6SV41w-1; Fri, 26 Aug 2022 12:22:25 -0400 X-MC-Unique: GhvTXYiiOIipXCds6SV41w-1 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C065B1F889; Fri, 26 Aug 2022 16:22:24 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 8AD3213421; Fri, 26 Aug 2022 16:22:24 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id oDBzIEDzCGPocAAAMHmgww (envelope-from ); Fri, 26 Aug 2022 16:22:24 +0000 From: mwilck@suse.com To: Christophe Varoqui , Benjamin Marzinski Date: Fri, 26 Aug 2022 18:21:59 +0200 Message-Id: <20220826162203.20317-2-mwilck@suse.com> In-Reply-To: <20220826162203.20317-1-mwilck@suse.com> References: <20220826162203.20317-1-mwilck@suse.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 Subject: [dm-devel] [PATCH v3 1/5] libmultipath: add msort.c from glibc X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dm-devel@redhat.com, Martin Wilck Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com From: Martin Wilck glibc's msort.c contains a stable sort algorithm based on mergesort. This will be useful for us, as qsort(3) is not necessarily stable. The added code is under LGPL-2.1. Signed-off-by: Martin Wilck --- libmultipath/msort.c | 309 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 libmultipath/msort.c diff --git a/libmultipath/msort.c b/libmultipath/msort.c new file mode 100644 index 0000000..cbe9a4a --- /dev/null +++ b/libmultipath/msort.c @@ -0,0 +1,309 @@ +/* An alternative to qsort, with an identical interface. + This file is part of the GNU C Library. + Copyright (C) 1992-2022 Free Software Foundation, Inc. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include + +struct msort_param +{ + size_t s; + size_t var; + __compar_d_fn_t cmp; + void *arg; + char *t; +}; +static void msort_with_tmp (const struct msort_param *p, void *b, size_t n); + +static void +msort_with_tmp (const struct msort_param *p, void *b, size_t n) +{ + char *b1, *b2; + size_t n1, n2; + + if (n <= 1) + return; + + n1 = n / 2; + n2 = n - n1; + b1 = b; + b2 = (char *) b + (n1 * p->s); + + msort_with_tmp (p, b1, n1); + msort_with_tmp (p, b2, n2); + + char *tmp = p->t; + const size_t s = p->s; + __compar_d_fn_t cmp = p->cmp; + void *arg = p->arg; + switch (p->var) + { + case 0: + while (n1 > 0 && n2 > 0) + { + if ((*cmp) (b1, b2, arg) <= 0) + { + *(uint32_t *) tmp = *(uint32_t *) b1; + b1 += sizeof (uint32_t); + --n1; + } + else + { + *(uint32_t *) tmp = *(uint32_t *) b2; + b2 += sizeof (uint32_t); + --n2; + } + tmp += sizeof (uint32_t); + } + break; + case 1: + while (n1 > 0 && n2 > 0) + { + if ((*cmp) (b1, b2, arg) <= 0) + { + *(uint64_t *) tmp = *(uint64_t *) b1; + b1 += sizeof (uint64_t); + --n1; + } + else + { + *(uint64_t *) tmp = *(uint64_t *) b2; + b2 += sizeof (uint64_t); + --n2; + } + tmp += sizeof (uint64_t); + } + break; + case 2: + while (n1 > 0 && n2 > 0) + { + unsigned long *tmpl = (unsigned long *) tmp; + unsigned long *bl; + + tmp += s; + if ((*cmp) (b1, b2, arg) <= 0) + { + bl = (unsigned long *) b1; + b1 += s; + --n1; + } + else + { + bl = (unsigned long *) b2; + b2 += s; + --n2; + } + while (tmpl < (unsigned long *) tmp) + *tmpl++ = *bl++; + } + break; + case 3: + while (n1 > 0 && n2 > 0) + { + if ((*cmp) (*(const void **) b1, *(const void **) b2, arg) <= 0) + { + *(void **) tmp = *(void **) b1; + b1 += sizeof (void *); + --n1; + } + else + { + *(void **) tmp = *(void **) b2; + b2 += sizeof (void *); + --n2; + } + tmp += sizeof (void *); + } + break; + default: + while (n1 > 0 && n2 > 0) + { + if ((*cmp) (b1, b2, arg) <= 0) + { + tmp = (char *) __mempcpy (tmp, b1, s); + b1 += s; + --n1; + } + else + { + tmp = (char *) __mempcpy (tmp, b2, s); + b2 += s; + --n2; + } + } + break; + } + + if (n1 > 0) + memcpy (tmp, b1, n1 * s); + memcpy (b, p->t, (n - n2) * s); +} + + +void +__qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) +{ + size_t size = n * s; + char *tmp = NULL; + struct msort_param p; + + /* For large object sizes use indirect sorting. */ + if (s > 32) + size = 2 * n * sizeof (void *) + s; + + if (size < 1024) + /* The temporary array is small, so put it on the stack. */ + p.t = __alloca (size); + else + { + /* We should avoid allocating too much memory since this might + have to be backed up by swap space. */ + static long int phys_pages; + static int pagesize; + + if (pagesize == 0) + { + phys_pages = __sysconf (_SC_PHYS_PAGES); + + if (phys_pages == -1) + /* Error while determining the memory size. So let's + assume there is enough memory. Otherwise the + implementer should provide a complete implementation of + the `sysconf' function. */ + phys_pages = (long int) (~0ul >> 1); + + /* The following determines that we will never use more than + a quarter of the physical memory. */ + phys_pages /= 4; + + /* Make sure phys_pages is written to memory. */ + atomic_write_barrier (); + + pagesize = __sysconf (_SC_PAGESIZE); + } + + /* Just a comment here. We cannot compute + phys_pages * pagesize + and compare the needed amount of memory against this value. + The problem is that some systems might have more physical + memory then can be represented with a `size_t' value (when + measured in bytes. */ + + /* If the memory requirements are too high don't allocate memory. */ + if (size / pagesize > (size_t) phys_pages) + { + _quicksort (b, n, s, cmp, arg); + return; + } + + /* It's somewhat large, so malloc it. */ + int save = errno; + tmp = malloc (size); + __set_errno (save); + if (tmp == NULL) + { + /* Couldn't get space, so use the slower algorithm + that doesn't need a temporary array. */ + _quicksort (b, n, s, cmp, arg); + return; + } + p.t = tmp; + } + + p.s = s; + p.var = 4; + p.cmp = cmp; + p.arg = arg; + + if (s > 32) + { + /* Indirect sorting. */ + char *ip = (char *) b; + void **tp = (void **) (p.t + n * sizeof (void *)); + void **t = tp; + void *tmp_storage = (void *) (tp + n); + + while ((void *) t < tmp_storage) + { + *t++ = ip; + ip += s; + } + p.s = sizeof (void *); + p.var = 3; + msort_with_tmp (&p, p.t + n * sizeof (void *), n); + + /* tp[0] .. tp[n - 1] is now sorted, copy around entries of + the original array. Knuth vol. 3 (2nd ed.) exercise 5.2-10. */ + char *kp; + size_t i; + for (i = 0, ip = (char *) b; i < n; i++, ip += s) + if ((kp = tp[i]) != ip) + { + size_t j = i; + char *jp = ip; + memcpy (tmp_storage, ip, s); + + do + { + size_t k = (kp - (char *) b) / s; + tp[j] = jp; + memcpy (jp, kp, s); + j = k; + jp = kp; + kp = tp[k]; + } + while (kp != ip); + + tp[j] = jp; + memcpy (jp, tmp_storage, s); + } + } + else + { + if ((s & (sizeof (uint32_t) - 1)) == 0 + && ((char *) b - (char *) 0) % __alignof__ (uint32_t) == 0) + { + if (s == sizeof (uint32_t)) + p.var = 0; + else if (s == sizeof (uint64_t) + && ((char *) b - (char *) 0) % __alignof__ (uint64_t) == 0) + p.var = 1; + else if ((s & (sizeof (unsigned long) - 1)) == 0 + && ((char *) b - (char *) 0) + % __alignof__ (unsigned long) == 0) + p.var = 2; + } + msort_with_tmp (&p, b, n); + } + free (tmp); +} +libc_hidden_def (__qsort_r) +weak_alias (__qsort_r, qsort_r) + + +void +qsort (void *b, size_t n, size_t s, __compar_fn_t cmp) +{ + return __qsort_r (b, n, s, (__compar_d_fn_t) cmp, NULL); +} +libc_hidden_def (qsort) From patchwork Fri Aug 26 16:22:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 12956271 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 79DB4ECAAA3 for ; Fri, 26 Aug 2022 16:22:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661530957; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=x9Dkw3VxmfkTyRk5W1GseLkQTw1EzBQuyVYYYcjVNuc=; b=Xv8ElpdW6Sn0HKaVZvQjKJmrA+61cEJr+PLYx7xB8IPguAEjRz3is/ix4Gis2YCX1Eynqu jnUcjfKVZ4ZQBbFTHqMqTKCj/OKcTIItd9/5wOjIIgSsEStMpMlAgYagWZi8VY4vGCRIE1 Lpx1XF+xXa6Wgyx6rHWwZeRGn98m7A8= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-47-s2wsiTaQOA2ML-BOPcyc6Q-1; Fri, 26 Aug 2022 12:22:35 -0400 X-MC-Unique: s2wsiTaQOA2ML-BOPcyc6Q-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3EE2829AB44C; Fri, 26 Aug 2022 16:22:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 887D32166B32; Fri, 26 Aug 2022 16:22:30 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 8AF411946A5E; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 8B9691946A4C for ; Fri, 26 Aug 2022 16:22:28 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 60D02492CA2; Fri, 26 Aug 2022 16:22:28 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast09.extmail.prod.ext.rdu2.redhat.com [10.11.55.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5D24E492C3B for ; Fri, 26 Aug 2022 16:22:27 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CCCB4280EA37 for ; Fri, 26 Aug 2022 16:22:27 +0000 (UTC) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-378-OO0wSfvJNaKW1Xu1e_VoJw-1; Fri, 26 Aug 2022 12:22:26 -0400 X-MC-Unique: OO0wSfvJNaKW1Xu1e_VoJw-1 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 08C201F917; Fri, 26 Aug 2022 16:22:25 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C7F6213421; Fri, 26 Aug 2022 16:22:24 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id MB4tL0DzCGPocAAAMHmgww (envelope-from ); Fri, 26 Aug 2022 16:22:24 +0000 From: mwilck@suse.com To: Christophe Varoqui , Benjamin Marzinski Date: Fri, 26 Aug 2022 18:22:00 +0200 Message-Id: <20220826162203.20317-3-mwilck@suse.com> In-Reply-To: <20220826162203.20317-1-mwilck@suse.com> References: <20220826162203.20317-1-mwilck@suse.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Subject: [dm-devel] [PATCH v3 2/5] libmultipath: modifications for msort.c X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dm-devel@redhat.com, Martin Wilck Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com From: Martin Wilck We don't want to fallback to qsort if memory is tight, as libc's routine does. Other than that, compile error fixes. Signed-off-by: Martin Wilck --- libmultipath/Makefile | 2 +- libmultipath/msort.c | 75 ++++++++++--------------------------------- libmultipath/msort.h | 6 ++++ 3 files changed, 24 insertions(+), 59 deletions(-) create mode 100644 libmultipath/msort.h diff --git a/libmultipath/Makefile b/libmultipath/Makefile index fb03200..a70acab 100644 --- a/libmultipath/Makefile +++ b/libmultipath/Makefile @@ -64,7 +64,7 @@ OBJS-O := parser.o vector.o devmapper.o \ log.o configure.o structs_vec.o sysfs.o \ lock.o file.o wwids.o prioritizers/alua_rtpg.o prkey.o \ io_err_stat.o dm-generic.o generic.o nvme-lib.o \ - libsg.o valid.o strbuf.o + libsg.o valid.o strbuf.o msort.o OBJS := $(OBJS-O) $(OBJS-U) diff --git a/libmultipath/msort.c b/libmultipath/msort.c index cbe9a4a..5ea05a7 100644 --- a/libmultipath/msort.c +++ b/libmultipath/msort.c @@ -21,9 +21,10 @@ #include #include #include -#include +#include #include -#include + +typedef int(*__compar_d_fn_t)(const void *, const void *, void *); struct msort_param { @@ -160,8 +161,8 @@ msort_with_tmp (const struct msort_param *p, void *b, size_t n) } -void -__qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) +static void +msort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) { size_t size = n * s; char *tmp = NULL; @@ -173,60 +174,15 @@ __qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) if (size < 1024) /* The temporary array is small, so put it on the stack. */ - p.t = __alloca (size); + p.t = alloca (size); else { - /* We should avoid allocating too much memory since this might - have to be backed up by swap space. */ - static long int phys_pages; - static int pagesize; - - if (pagesize == 0) - { - phys_pages = __sysconf (_SC_PHYS_PAGES); - - if (phys_pages == -1) - /* Error while determining the memory size. So let's - assume there is enough memory. Otherwise the - implementer should provide a complete implementation of - the `sysconf' function. */ - phys_pages = (long int) (~0ul >> 1); - - /* The following determines that we will never use more than - a quarter of the physical memory. */ - phys_pages /= 4; - - /* Make sure phys_pages is written to memory. */ - atomic_write_barrier (); - - pagesize = __sysconf (_SC_PAGESIZE); - } - - /* Just a comment here. We cannot compute - phys_pages * pagesize - and compare the needed amount of memory against this value. - The problem is that some systems might have more physical - memory then can be represented with a `size_t' value (when - measured in bytes. */ - - /* If the memory requirements are too high don't allocate memory. */ - if (size / pagesize > (size_t) phys_pages) - { - _quicksort (b, n, s, cmp, arg); - return; - } - /* It's somewhat large, so malloc it. */ int save = errno; tmp = malloc (size); - __set_errno (save); + errno = save; if (tmp == NULL) - { - /* Couldn't get space, so use the slower algorithm - that doesn't need a temporary array. */ - _quicksort (b, n, s, cmp, arg); - return; - } + return; p.t = tmp; } @@ -297,13 +253,16 @@ __qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) } free (tmp); } -libc_hidden_def (__qsort_r) -weak_alias (__qsort_r, qsort_r) - +/* + * glibc apparently doesn't use -Wcast-function-type. + * If this is safe for them, it should be for us, too. + */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-function-type" void -qsort (void *b, size_t n, size_t s, __compar_fn_t cmp) +msort (void *b, size_t n, size_t s, __compar_fn_t cmp) { - return __qsort_r (b, n, s, (__compar_d_fn_t) cmp, NULL); + return msort_r (b, n, s, (__compar_d_fn_t)cmp, NULL); } -libc_hidden_def (qsort) +#pragma GCC diagnostic pop diff --git a/libmultipath/msort.h b/libmultipath/msort.h new file mode 100644 index 0000000..caef9b6 --- /dev/null +++ b/libmultipath/msort.h @@ -0,0 +1,6 @@ +#ifndef __MSORT_H +#define __MSORT_H +typedef int(*__compar_fn_t)(const void *, const void *); +void msort (void *b, size_t n, size_t s, __compar_fn_t cmp); + +#endif From patchwork Fri Aug 26 16:22:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 12956269 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9B283ECAAD5 for ; Fri, 26 Aug 2022 16:22:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661530956; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=WsQ1CbRrBOqdsOUO6qKL7FXbcY+Q8PzALndp7rTbT7I=; b=PhCvxaI92XK0909qVCJUrCVHTOXA8q202hMFKXnbzDeK6PKouw90ylwHnbXP5cJ/dpJm5y onA7z49GcHQsdWAd93hFsSrUjJVyEvdgj7xHGXVs5otC5W4kBliJei8KsIdQjzfMScVhpm as0POngMDCInrWzitpiJgdoLzIw/7x4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-462--ONB9NyZNdeo8uuF5Boi5g-1; Fri, 26 Aug 2022 12:22:35 -0400 X-MC-Unique: -ONB9NyZNdeo8uuF5Boi5g-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3EEA08039C5; Fri, 26 Aug 2022 16:22:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 178C040E7F29; Fri, 26 Aug 2022 16:22:31 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id B7B461946A6F; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 70E4A1946A43 for ; Fri, 26 Aug 2022 16:22:28 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 60670C15BBD; Fri, 26 Aug 2022 16:22:28 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast05.extmail.prod.ext.rdu2.redhat.com [10.11.55.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5CB69C15BBA for ; Fri, 26 Aug 2022 16:22:28 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EC9538037AF for ; Fri, 26 Aug 2022 16:22:27 +0000 (UTC) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-168-MGuphPG3MFWUpOI4vIHlhQ-1; Fri, 26 Aug 2022 12:22:26 -0400 X-MC-Unique: MGuphPG3MFWUpOI4vIHlhQ-1 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 3B2291F91D; Fri, 26 Aug 2022 16:22:25 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0DE5E13A82; Fri, 26 Aug 2022 16:22:25 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id CL7iAUHzCGPocAAAMHmgww (envelope-from ); Fri, 26 Aug 2022 16:22:25 +0000 From: mwilck@suse.com To: Christophe Varoqui , Benjamin Marzinski Date: Fri, 26 Aug 2022 18:22:01 +0200 Message-Id: <20220826162203.20317-4-mwilck@suse.com> In-Reply-To: <20220826162203.20317-1-mwilck@suse.com> References: <20220826162203.20317-1-mwilck@suse.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 Subject: [dm-devel] [PATCH v3 3/5] libmultipath: merge_mptable(): sort table by wwid X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dm-devel@redhat.com, Martin Wilck Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com From: Martin Wilck If the mptable is very large (for example, in a configuration with lots of maps assigned individual aliases), merge_mptable may get very slow because it needs to make O(n^2) string comparisons (with n being the number of mptable entries). WWID strings often differ only in the last few bytes, causing a lot of CPU time spent in strcmp(). merge_mptable is executed whenever multipath or multipathd is started, that is, also for "multipath -u" and "multipath -U" invocations from udev rules. Optimize it by sorting the mptable before merging. This way we don't need to iterate towards the end of the vector searching for duplicates. Because of the way merge_mpe() works, we must make sure not to change the order of entries with the same WWID. In other words, a stable sort algorithm is required, which isn't the case for qsort(3) in general. Therefore we explicitly use the msort() routine from glibc, which is a stable merge sort algorithm, without fallback to quicksort. Signed-off-by: Martin Wilck --- libmultipath/config.c | 15 +++++++++++++-- libmultipath/vector.c | 9 +++++++++ libmultipath/vector.h | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/libmultipath/config.c b/libmultipath/config.c index ab8b26e..a2c79a4 100644 --- a/libmultipath/config.c +++ b/libmultipath/config.c @@ -520,6 +520,14 @@ merge_mpe(struct mpentry *dst, struct mpentry *src) merge_num(mode); } +static int wwid_compar(const void *p1, const void *p2) +{ + const char *wwid1 = (*(struct mpentry * const *)p1)->wwid; + const char *wwid2 = (*(struct mpentry * const *)p2)->wwid; + + return strcmp(wwid1, wwid2); +} + void merge_mptable(vector mptable) { struct mpentry *mp1, *mp2; @@ -533,10 +541,13 @@ void merge_mptable(vector mptable) free_mpe(mp1); continue; } + } + vector_sort(mptable, wwid_compar); + vector_foreach_slot(mptable, mp1, i) { j = i + 1; vector_foreach_slot_after(mptable, mp2, j) { - if (!mp2->wwid || strcmp(mp1->wwid, mp2->wwid)) - continue; + if (strcmp(mp1->wwid, mp2->wwid)) + break; condlog(1, "%s: duplicate multipath config section for %s", __func__, mp1->wwid); merge_mpe(mp2, mp1); diff --git a/libmultipath/vector.c b/libmultipath/vector.c index e2d1ec9..df59db5 100644 --- a/libmultipath/vector.c +++ b/libmultipath/vector.c @@ -21,6 +21,7 @@ #include #include "vector.h" +#include "msort.h" /* * Initialize vector struct. @@ -208,3 +209,11 @@ int vector_find_or_add_slot(vector v, void *value) vector_set_slot(v, value); return VECTOR_SIZE(v) - 1; } + +void vector_sort(vector v, int (*compar)(const void *, const void *)) +{ + if (!v || !v->slot || !v->allocated) + return; + return msort((void *)v->slot, v->allocated, sizeof(void *), compar); + +} diff --git a/libmultipath/vector.h b/libmultipath/vector.h index 2862dc2..c0b09cb 100644 --- a/libmultipath/vector.h +++ b/libmultipath/vector.h @@ -89,4 +89,5 @@ extern void vector_repack(vector v); extern void vector_dump(vector v); extern void dump_strvec(vector strvec); extern int vector_move_up(vector v, int src, int dest); +void vector_sort(vector v, int (*compar)(const void *, const void *)); #endif From patchwork Fri Aug 26 16:22:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 12956272 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 01063ECAAD6 for ; Fri, 26 Aug 2022 16:22:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661530956; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=kqtQy+NOInndUogagzaNGf9aMXpkbERJiqkstBfFxVk=; b=Vow7SInq6DVE8tGXVfhm9Z1GhXQOQFBdVSqrzfuNw4ikdUpbc0x6jj6bTA1OyFvGLHUI1d aH/TqQEG/6xdrl0rwDo7DOp6QAigMjbKK9FNN8HJvgI4CCJC1kFrbiKm6NZotdBZY2h9M6 yRYI1Afrbvy/wT6tf6ANdKOabnyW86U= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-668-SYIFel6iMVyILVB-qbwVRg-1; Fri, 26 Aug 2022 12:22:35 -0400 X-MC-Unique: SYIFel6iMVyILVB-qbwVRg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 70D9A3801F5B; Fri, 26 Aug 2022 16:22:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1B7634C816; Fri, 26 Aug 2022 16:22:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id B75791946A51; Fri, 26 Aug 2022 16:22:31 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 9C5431946A4D for ; Fri, 26 Aug 2022 16:22:30 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 8C83F1121319; Fri, 26 Aug 2022 16:22:30 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 888951121315 for ; Fri, 26 Aug 2022 16:22:30 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6C53C802529 for ; Fri, 26 Aug 2022 16:22:30 +0000 (UTC) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-359-vEUyLvRgNSuQve5buOe18Q-1; Fri, 26 Aug 2022 12:22:26 -0400 X-MC-Unique: vEUyLvRgNSuQve5buOe18Q-1 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 6D67A1F938; Fri, 26 Aug 2022 16:22:25 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 4352313421; Fri, 26 Aug 2022 16:22:25 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id KOkCD0HzCGPocAAAMHmgww (envelope-from ); Fri, 26 Aug 2022 16:22:25 +0000 From: mwilck@suse.com To: Christophe Varoqui , Benjamin Marzinski Date: Fri, 26 Aug 2022 18:22:02 +0200 Message-Id: <20220826162203.20317-5-mwilck@suse.com> In-Reply-To: <20220826162203.20317-1-mwilck@suse.com> References: <20220826162203.20317-1-mwilck@suse.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 Subject: [dm-devel] [PATCH v3 4/5] libmultipath: check_alias_settings(): pre-sort mptable by alias X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dm-devel@redhat.com, Martin Wilck Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com From: Martin Wilck add_binding() contains an optimization; it assumes that the list of bindings is alphabetically sorted by alias, and tries to maintain this order. But conf->mptable is sorted by wwid. Therefore check_alias_settings() may take a long time if the mptable is large. Create a copy of the mptable, sorted by alias, and use it for add_bindings(). This speeds up check_alias_settings by roughly a factor of 10 (0.1s vs. 1s) for my test setup with 10000 entries in the mptable. Signed-off-by: Martin Wilck --- libmultipath/alias.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/libmultipath/alias.c b/libmultipath/alias.c index 548a118..af3e24f 100644 --- a/libmultipath/alias.c +++ b/libmultipath/alias.c @@ -672,6 +672,24 @@ static void cleanup_fclose(void *p) fclose(p); } +static int alias_compar(const void *p1, const void *p2) +{ + const char *alias1 = (*(struct mpentry * const *)p1)->alias; + const char *alias2 = (*(struct mpentry * const *)p2)->alias; + + if (alias1 && alias2) + return strcmp(alias1, alias2); + else + /* Move NULL alias to the end */ + return alias1 ? -1 : alias2 ? 1 : 0; +} + +static void cleanup_vector_free(void *arg) +{ + if (arg) + vector_free((vector)arg); +} + /* * check_alias_settings(): test for inconsistent alias configuration * @@ -693,12 +711,24 @@ int check_alias_settings(const struct config *conf) int can_write; int rc = 0, i, fd; Bindings bindings = {.allocated = 0, }; + vector mptable = NULL; struct mpentry *mpe; + mptable = vector_convert(NULL, conf->mptable, struct mpentry *, identity); + if (!mptable) + return -1; + pthread_cleanup_push_cast(free_bindings, &bindings); - vector_foreach_slot(conf->mptable, mpe, i) { - if (!mpe->wwid || !mpe->alias) - continue; + pthread_cleanup_push(cleanup_vector_free, mptable); + + vector_sort(mptable, alias_compar); + vector_foreach_slot(mptable, mpe, i) { + if (!mpe->alias) + /* + * alias_compar() sorts NULL alias at the end, + * so we can stop if we encounter this. + */ + break; if (add_binding(&bindings, mpe->alias, mpe->wwid) == BINDING_CONFLICT) { condlog(0, "ERROR: alias \"%s\" bound to multiple wwids in multipath.conf, " @@ -710,6 +740,7 @@ int check_alias_settings(const struct config *conf) } /* This clears the bindings */ pthread_cleanup_pop(1); + pthread_cleanup_pop(1); pthread_cleanup_push_cast(free_bindings, &bindings); fd = open_file(conf->bindings_file, &can_write, BINDINGS_FILE_HEADER); From patchwork Fri Aug 26 16:22:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 12956273 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9E55EECAAD2 for ; Fri, 26 Aug 2022 16:25:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661531106; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=laV7MnrRrvp6BKiwNf6/q5ekpWJb5uUVoHlrNnxQv0Q=; b=OI4+dB5K/6+6dInenQEb8lZ476iABzfw+5Zu8I8AF3hzL3u7NxUz8aoONBU8mopdxeEzNp GE/q3XRRvSq+vU19bOtpf74TKJADgxJR0bFXN3/u5hC+4vF88ti14bva9aGNNdGiAFzqKZ oGamjNkZENQBoL0HKJABO+HvVqUXWl8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-622-6zUtt8jYMlSXN_KjWuBb2g-1; Fri, 26 Aug 2022 12:22:35 -0400 X-MC-Unique: 6zUtt8jYMlSXN_KjWuBb2g-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3EF901857F1A; Fri, 26 Aug 2022 16:22:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 96C07492C3B; Fri, 26 Aug 2022 16:22:31 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 03C281946A4E; Fri, 26 Aug 2022 16:22:31 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id ADDB51946A6C for ; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 92102492CA2; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8DBEE492CA5 for ; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 766331857F06 for ; Fri, 26 Aug 2022 16:22:29 +0000 (UTC) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-387-Bvf_TAHbMaOpEurz_R4ptg-1; Fri, 26 Aug 2022 12:22:27 -0400 X-MC-Unique: Bvf_TAHbMaOpEurz_R4ptg-1 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id A320E1F940; Fri, 26 Aug 2022 16:22:25 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 764AD13421; Fri, 26 Aug 2022 16:22:25 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id CAN6G0HzCGPocAAAMHmgww (envelope-from ); Fri, 26 Aug 2022 16:22:25 +0000 From: mwilck@suse.com To: Christophe Varoqui , Benjamin Marzinski Date: Fri, 26 Aug 2022 18:22:03 +0200 Message-Id: <20220826162203.20317-6-mwilck@suse.com> In-Reply-To: <20220826162203.20317-1-mwilck@suse.com> References: <20220826162203.20317-1-mwilck@suse.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Subject: [dm-devel] [PATCH v3 5/5] multipath: optimize program startup for frequent invocations X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dm-devel@redhat.com, Martin Wilck Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com From: Martin Wilck Neither "multipath -u" nor "multipath -U" need initialization of the prioritizers, checkers, and foreign libraries. Also, these commands need not fail if the bindings file is inconsistent. Move these possibly slow initialization steps after these special command invocations. Signed-off-by: Martin Wilck Reviewed-by: Benjamin Marzinski --- multipath/main.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/multipath/main.c b/multipath/main.c index 034dd2f..8e5154a 100644 --- a/multipath/main.c +++ b/multipath/main.c @@ -957,11 +957,6 @@ main (int argc, char *argv[]) exit(RTVL_FAIL); } - if (check_alias_settings(conf)) { - fprintf(stderr, "fatal configuration error, aborting"); - exit(RTVL_FAIL); - } - if (optind < argc) { dev = calloc(1, FILE_NAME_SIZE); @@ -988,20 +983,9 @@ main (int argc, char *argv[]) libmp_udev_set_sync_support(1); - if (init_checkers()) { - condlog(0, "failed to initialize checkers"); - goto out; - } - if (init_prio()) { - condlog(0, "failed to initialize prioritizers"); - goto out; - } - if ((cmd == CMD_LIST_SHORT || cmd == CMD_LIST_LONG) && enable_foreign) conf->enable_foreign = strdup(""); - /* Failing here is non-fatal */ - init_foreign(conf->enable_foreign); if (cmd == CMD_USABLE_PATHS) { r = check_usable_paths(conf, dev, dev_type) ? RTVL_FAIL : RTVL_OK; @@ -1036,6 +1020,23 @@ main (int argc, char *argv[]) break; } + if (check_alias_settings(conf)) { + fprintf(stderr, "fatal configuration error, aborting"); + exit(RTVL_FAIL); + } + + if (init_checkers()) { + condlog(0, "failed to initialize checkers"); + goto out; + } + if (init_prio()) { + condlog(0, "failed to initialize prioritizers"); + goto out; + } + + /* Failing here is non-fatal */ + init_foreign(conf->enable_foreign); + if (cmd == CMD_RESET_WWIDS) { struct multipath * mpp; int i;