multipath: optimizations for large mptable
v3 cover multipath: optimizations for large mptable
From: Martin Wilck <[email protected]>
We observe that multipath operations take a long time if the multipaths section in multipath.conf contains a lot of alias settings (10000+ in our case). This hurts in particular in udev rules, when multipath -u or multipath -U is invoked, but also for command line invocations like "multipath -ll".
This series provides a few optimizations for this use case. It speeds up simple multipath operations in the test case by a factor of 20.
Changes v2->v3, after discussion with Benjamin Marzinski:
01, 02: added msort.c from glibc and adapted to our needs.
Numbering changes accordingly.
03, 04: (was 01, 02): remove pointer comparisons from v2 again, this was a
dumb idea. Use the stable msort algorithm instead.
Changes wrt v1, after suggestions from Benjamin Marzinski:
01, 02: Use pointer comparisons to achieve stable sorting with qsort 02: Fix return without popping the cleanup handler. The way I fixed this
leaves the possibility that some memory won't be freed if a thread is killed while executing vector_convert(). I think this is acceptible; avoiding it would complicate the code, with very small benefit.
02: Remove unnecessary checks and break loop if alias==NULL is encountered.
Martin Wilck (5):
libmultipath: add msort.c from glibc
libmultipath: modifications for msort.c
libmultipath: merge_mptable(): sort table by wwid
libmultipath: check_alias_settings(): pre-sort mptable by alias
multipath: optimize program startup for frequent invocations
libmultipath/Makefile | 2 +- libmultipath/alias.c | 37 +++++- libmultipath/config.c | 15 ++- libmultipath/msort.c | 268 ++++++++++++++++++++++++++++++++++++++++++ libmultipath/msort.h | 6 + libmultipath/vector.c | 9 ++ libmultipath/vector.h | 1 + multipath/main.c | 33 +++--- 8 files changed, 349 insertions(+), 22 deletions(-) create mode 100644 libmultipath/msort.c create mode 100644 libmultipath/msort.h
Signature · 6 lines
2.37.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v3 1/5 libmultipath: add msort.c from glibc
From: Martin Wilck <[email protected]>
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.
--- libmultipath/msort.c | 309 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 libmultipath/msort.c
libmultipath/msort.c new file +309 -0
@@ -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 +<https://www.gnu.org/licenses/>. */ + +#include <alloca.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <memcopy.h> +#include <errno.h> +#include <atomic.h> + +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)
Signature · 6 lines
2.37.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v3 2/5 libmultipath: modifications for msort.c
In reply to [email protected]From: Martin Wilck <[email protected]>
We don't want to fallback to qsort if memory is tight, as libc's routine does. Other than that, compile error fixes.
--- libmultipath/Makefile | 2 +- libmultipath/msort.c | 75 ++++++++++--------------------------------- libmultipath/msort.h | 6 ++++ 3 files changed, 24 insertions(+), 59 deletions(-) create mode 100644 libmultipath/msort.h
libmultipath/Makefile +1 -1
@@ -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)
libmultipath/msort.c +17 -58
107 lines not on this page · raw message
libmultipath/msort.h new file +6 -0
@@ -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
Signature · 6 lines
2.37.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v3 3/5 libmultipath: merge_mptable(): sort table by wwid
In reply to [email protected]From: Martin Wilck <[email protected]>
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.
--- libmultipath/config.c | 15 +++++++++++++-- libmultipath/vector.c | 9 +++++++++ libmultipath/vector.h | 1 + 3 files changed, 23 insertions(+), 2 deletions(-)
libmultipath/config.c +13 -2
@@ -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);
libmultipath/vector.c +9 -0
@@ -21,6 +21,7 @@#include <stdlib.h> #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); + +}
libmultipath/vector.h +1 -0
@@ -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
Signature · 6 lines
2.37.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v3 4/5 libmultipath: check_alias_settings(): pre-sort mptable by alias
In reply to [email protected]From: Martin Wilck <[email protected]>
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.
--- libmultipath/alias.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-)
libmultipath/alias.c +34 -3
@@ -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 );
Signature · 6 lines
2.37.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel
PATCH v3 5/5 multipath: optimize program startup for frequent invocations
In reply to [email protected]From: Martin Wilck <[email protected]>
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.
--- multipath/main.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)
multipath/main.c +17 -16
@@ -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;
Signature · 6 lines
2.37.1 -- dm-devel mailing list [email protected] https://listman.redhat.com/mailman/listinfo/dm-devel