bcachefs: eytzinger code
v3 cover bcachefs: eytzinger code
Kent,
here is another update. Changes:
* Added a self test for eytzinger0_find().
* The patches are now in fully bisectable order, with slightly improved patch descriptions.
* The patch that runs the self tests on modprobe has been moved to the end of the patch queue. This patch is useful for testing, but it SHOULD NOT BE MERGED.
A git tree of the patches can again be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/agruen/linux.git/log/?h=bcachefs
Base: v6.13
Head: d79258df8791 ("bcachefs: Run the eytzinger tests on modprobe")
The previous CI test results at:
https://evilpiepirate.org/~testdashboard/ci?user=kmo&branch=eytzinger
against the previous version of this patch queue and with head:
94865378e1a3 ("bcachefs: eytzinger1_{next,prev} cleanup")
show a large number of passed as well as failed tests even for upstream v6.13, so I don't know what to conclude from that.
In any case, there is one failure reporting that eytzinger0_find_ge() didn't find the first matching element in an array with duplicate elements:
This is imtentional and expected because at this point in the patch queue, eytzinger0_find_ge() hasn't been fixed to always return the first matching element. The current version of the patch queue adds the fix before adding the test, so this failure will be gone.
That being said, as per request, the updated patch queue only enables the self tests in the last patch, so we wouldn't see failures of that kind anymore, anyway.
Thanks,
Andreas
Andreas Gruenbacher (21):
bcachefs: bch2_blacklist_entries_gc cleanup
bcachefs: EYTZINGER_DEBUG fix
bcachefs: eytzinger self tests: loop cleanups
bcachefs: eytzinger self tests: missing newline termination
bcachefs: eytzinger self tests: fix cmp_u16 typo
bcachefs: eytzinger[01]_test improvement
bcachefs: eytzinger0_find_test improvement
bcachefs: add eytzinger0_for_each_prev
bcachefs: improve eytzinger0_find_le self test
bcachefs: convert eytzinger0_find_le to be 1-based
bcachefs: simplify eytzinger0_find_le
bcachefs: add eytzinger0_find_gt self test
bcachefs: implement eytzinger0_find_gt directly
bcachefs: implement eytzinger0_find_ge directly
bcachefs: add eytzinger0_find_ge self test
bcachefs: Add eytzinger0_find self test
bcachefs: convert eytzinger0_find to be 1-based
bcachefs: convert eytzinger sort to be 1-based (1)
bcachefs: convert eytzinger sort to be 1-based (2)
bcachefs: eytzinger1_{next,prev} cleanup
bcachefs: Run the eytzinger tests on modprobe
fs/bcachefs/eytzinger.c | 88 ++++++------ fs/bcachefs/eytzinger.h | 99 ++++++------- fs/bcachefs/journal_seq_blacklist.c | 7 +- fs/bcachefs/super.c | 5 + fs/bcachefs/util.c | 212 ++++++++++++++++++++++------ fs/bcachefs/util.h | 4 + 6 files changed, 269 insertions(+), 146 deletions(-)
Signature · 1 line
2.48.1
PATCH v3 1/21 bcachefs: bch2_blacklist_entries_gc cleanup
Use an eytzinger0_for_each() loop here.
--- fs/bcachefs/journal_seq_blacklist.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
fs/bcachefs/journal_seq_blacklist.c +3 -4
@@ -231,15 +231,14 @@ bool bch2_blacklist_entries_gc(struct bch_fs *c)struct journal_seq_blacklist_table *t = c->journal_seq_blacklist_table;BUG_ON (nr != t->nr); -unsigned i; -for (src = bl->start, i = t->nr ==0 ?0 :eytzinger0_first (t->nr); - src < bl->start + nr; - src++, i =eytzinger0_next (i, nr)) { + src = bl->start; +eytzinger0_for_each (i, nr) {BUG_ON (t->entries[i].start !=le64_to_cpu (src->start));BUG_ON (t->entries[i].end !=le64_to_cpu (src->end));if (t->entries[i].dirty || t->entries[i].end >= c->journal.oldest_seq_found_ondisk) *dst++ = *src; + src++; }unsigned new_nr = dst - bl->start;
Signature · 1 line
2.48.1
PATCH v3 2/21 bcachefs: EYTZINGER_DEBUG fix
In reply to Andreas GruenbacherWhen EYTZINGER_DEBUG is defined, <linux/bug.h> needs to be included.
--- fs/bcachefs/eytzinger.h | 1 + 1 file changed, 1 insertion(+)
fs/bcachefs/eytzinger.h +1 -0
@@ -6,6 +6,7 @@#include <linux/log2.h> #ifdef EYTZINGER_DEBUG +#include <linux/bug.h> #define EYTZINGER_BUG_ON (cond) BUG_ON(cond)#else #define EYTZINGER_BUG_ON (cond)
Signature · 1 line
2.48.1
PATCH v3 3/21 bcachefs: eytzinger self tests: loop cleanups
In reply to Andreas GruenbacherThe iterator variable of eytzinger0_for_each() loops has been changed to be locally scoped at some point, so remove variables defined outside the loop that are now unused. In addition and for clarity, use a different variable inside those loops where an outside variable would be shadowed.
--- fs/bcachefs/util.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
fs/bcachefs/util.c +11 -11
@@ -473,10 +473,10 @@ void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *statsu64 last_q =0 ;prt_printf (out,"quantiles (%s):\t" , u->name); -eytzinger0_for_each (i,NR_QUANTILES ) { -bool is_last =eytzinger0_next (i,NR_QUANTILES ) ==-1 ; +eytzinger0_for_each (j,NR_QUANTILES ) { +bool is_last =eytzinger0_next (j,NR_QUANTILES ) ==-1 ; -u64 q =max (quantiles->entries[i].m, last_q); +u64 q =max (quantiles->entries[j].m, last_q);prt_printf (out,"%llu " ,div64_u64 (q, u->nsecs));if (is_last)prt_newline (out);
@@ -701,7 +701,7 @@ void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)#if 0 void eytzinger1_test (void ) { -unsigned inorder, eytz, size; +unsigned inorder, size;pr_info ("1 based eytzinger test:" );
@@ -734,7 +734,7 @@ void eytzinger1_test(void)void eytzinger0_test (void ) { -unsigned inorder, eytz, size; +unsigned inorder, size;pr_info ("0 based eytzinger test:" );
@@ -764,7 +764,7 @@ void eytzinger0_test(void) } } -static inline int cmp_u16 (const void *_l,const void *_r,size_t size) +static inline int cmp_u16 (const void *_l,const void *_r) {const u16 *l = _l, *r = _r;
@@ -787,8 +787,8 @@ static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search) c2 = test_array[i];if (c1 != c2) { -eytzinger0_for_each (i, nr) -pr_info ("[%3u] = %12u" , i, test_array[i]); +eytzinger0_for_each (j, nr) +pr_info ("[%3u] = %12u" , j, test_array[j]);pr_info ("find_le(%2u) -> [%2zi] = %2i should be %2i" , i, r, c1, c2); }
@@ -806,9 +806,9 @@ void eytzinger0_find_test(void)eytzinger0_sort (test_array, nr,sizeof (test_array[0 ]), cmp_u16,NULL );/* verify array is sorted correctly: */ -eytzinger0_for_each (i, nr) -BUG_ON (i !=eytzinger0_last (nr) && - test_array[i] > test_array[eytzinger0_next (i, nr)]); +eytzinger0_for_each (j, nr) +BUG_ON (j !=eytzinger0_last (nr) && + test_array[j] > test_array[eytzinger0_next (j, nr)]);for (i =0 ; i <U16_MAX ; i +=1 <<12 )eytzinger0_find_test_val (test_array, nr, i);
Signature · 1 line
2.48.1
PATCH v3 4/21 bcachefs: eytzinger self tests: missing newline termination
In reply to Andreas Gruenbacherpr_info() format strings need to be newline terminated.
--- fs/bcachefs/util.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
fs/bcachefs/util.c +7 -7
@@ -703,7 +703,7 @@ void eytzinger1_test(void) {unsigned inorder, size; -pr_info ("1 based eytzinger test:" ); +pr_info ("1 based eytzinger test:\n" ); for (size = 2; size < 65536;
@@ -711,7 +711,7 @@ void eytzinger1_test(void)unsigned extra =eytzinger1_extra (size);if (!(size %4096 )) -pr_info ("tree size %u" , size); +pr_info ("tree size %u\n" , size);BUG_ON (eytzinger1_prev (0 , size) !=eytzinger1_last (size));BUG_ON (eytzinger1_next (0 , size) !=eytzinger1_first (size));
@@ -736,7 +736,7 @@ void eytzinger0_test(void)unsigned inorder, size; -pr_info ("0 based eytzinger test:" ); +pr_info ("0 based eytzinger test:\n" ); for (size = 1; size < 65536;
@@ -744,7 +744,7 @@ void eytzinger0_test(void)unsigned extra =eytzinger0_extra (size);if (!(size %4096 )) -pr_info ("tree size %u" , size); +pr_info ("tree size %u\n" , size);BUG_ON (eytzinger0_prev (-1 , size) !=eytzinger0_last (size));BUG_ON (eytzinger0_next (-1 , size) !=eytzinger0_first (size));
@@ -788,8 +788,8 @@ static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)if (c1 != c2) {eytzinger0_for_each (j, nr) -pr_info ("[%3u] = %12u" , j, test_array[j]); -pr_info ("find_le(%2u) -> [%2zi] = %2i should be %2i" , +pr_info ("[%3u] = %12u\n" , j, test_array[j]); +pr_info ("find_le(%2u) -> [%2zi] = %2i should be %2i\n" , i, r, c1, c2); } }
@@ -800,7 +800,7 @@ void eytzinger0_find_test(void)u16 *test_array =kmalloc_array (allocated,sizeof (test_array[0 ]),GFP_KERNEL );for (nr =1 ; nr < allocated; nr++) { -pr_info ("testing %u elems" , nr); +pr_info ("testing %u elems\n" , nr);get_random_bytes (test_array, nr *sizeof (test_array[0 ]));eytzinger0_sort (test_array, nr,sizeof (test_array[0 ]), cmp_u16,NULL );
Signature · 1 line
2.48.1
PATCH v3 5/21 bcachefs: eytzinger self tests: fix cmp_u16 typo
In reply to Andreas GruenbacherFix an obvious typo in cmp_u16().
--- fs/bcachefs/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/bcachefs/util.c +1 -1
@@ -768,7 +768,7 @@ static inline int cmp_u16(const void *_l, const void *_r) {const u16 *l = _l, *r = _r; -return (*l > *r) - (*r - *l); +return (*l > *r) - (*r > *l); }static void eytzinger0_find_test_val (u16 *test_array,unsigned nr,u16 search)
Signature · 1 line
2.48.1
PATCH v3 6/21 bcachefs: eytzinger[01]_test improvement
In reply to Andreas GruenbacherIn eytzinger[01]_test(), make sure that eytzinger[01]_for_each() iterates over all array elements.
--- fs/bcachefs/util.c | 2 ++ 1 file changed, 2 insertions(+)
fs/bcachefs/util.c +2 -0
@@ -728,6 +728,7 @@ void eytzinger1_test(void) inorder++; } +BUG_ON (inorder -1 != size); } }
@@ -761,6 +762,7 @@ void eytzinger0_test(void) inorder++; } +BUG_ON (inorder != size); } }
Signature · 1 line
2.48.1
PATCH v3 7/21 bcachefs: eytzinger0_find_test improvement
In reply to Andreas GruenbacherIn eytzinger0_find_test(), remember the smallest element seen so far instead of comparing adjacent array elements.
--- fs/bcachefs/util.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
fs/bcachefs/util.c +6 -3
@@ -802,15 +802,18 @@ void eytzinger0_find_test(void)u16 *test_array =kmalloc_array (allocated,sizeof (test_array[0 ]),GFP_KERNEL );for (nr =1 ; nr < allocated; nr++) { +u16 prev =0 ; +pr_info ("testing %u elems\n" , nr);get_random_bytes (test_array, nr *sizeof (test_array[0 ]));eytzinger0_sort (test_array, nr,sizeof (test_array[0 ]), cmp_u16,NULL );/* verify array is sorted correctly: */ -eytzinger0_for_each (j, nr) -BUG_ON (j !=eytzinger0_last (nr) && - test_array[j] > test_array[eytzinger0_next (j, nr)]); +eytzinger0_for_each (j, nr) { +BUG_ON (test_array[j] < prev); + prev = test_array[j]; + }for (i =0 ; i <U16_MAX ; i +=1 <<12 )eytzinger0_find_test_val (test_array, nr, i);
Signature · 1 line
2.48.1
PATCH v3 8/21 bcachefs: add eytzinger0_for_each_prev
In reply to Andreas GruenbacherAdd an eytzinger0_for_each_prev() macro for iterating through an eytzinger array in reverse.
--- fs/bcachefs/eytzinger.h | 5 +++++ fs/bcachefs/util.c | 9 +++++++++ 2 files changed, 14 insertions(+)
fs/bcachefs/eytzinger.h +5 -0
@@ -244,6 +244,11 @@ static inline unsigned inorder_to_eytzinger0(unsigned i, unsigned size) (_i) !=-1 ; \ (_i) = eytzinger0_next((_i), (_size))) +#define eytzinger0_for_each_prev (_i, _size) \ + for (unsigned (_i) = eytzinger0_last((_size)); \ + (_i) != -1; \ + (_i) = eytzinger0_prev((_i), (_size))) +/* return greatest node <= @search, or -1 if not found */ static inline int eytzinger0_find_le (void *base,size_t nr,size_t size,cmp_func_t cmp,const void *search)
fs/bcachefs/util.c +9 -0
@@ -763,6 +763,15 @@ void eytzinger0_test(void) inorder++; }BUG_ON (inorder != size); + + inorder = size -1 ; +eytzinger0_for_each_prev (eytz, size) { +BUG_ON (eytz !=eytzinger0_first (size) && +eytzinger0_next (eytzinger0_prev (eytz, size), size) != eytz); + + inorder--; + } +BUG_ON (inorder !=-1 ); } }
Signature · 1 line
2.48.1
PATCH v3 9/21 bcachefs: improve eytzinger0_find_le self test
In reply to Andreas GruenbacherRename eytzinger0_find_test_val() to eytzinger0_find_test_le() and add a new eytzinger0_find_test_val() wrapper that calls it.
We have already established that the array is sorted in eytzinger order, so we can use the eytzinger iterator functions and check the boundary conditions to verify the result of eytzinger0_find_le().
Only scan the entire array if we get an incorrect result. When we need to scan, use eytzinger0_for_each_prev() so that we'll stop at the highest matching element in the array in case there are duplicates; going through the array linearly wouldn't give us that.
--- fs/bcachefs/util.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-)
fs/bcachefs/util.c +30 -11
@@ -782,29 +782,48 @@ static inline int cmp_u16(const void *_l, const void *_r)return (*l > *r) - (*r > *l); } -static void eytzinger0_find_test_val (u16 *test_array,unsigned nr,u16 search) +static void eytzinger0_find_test_le (u16 *test_array,unsigned nr,u16 search) { -int i, c1 =-1 , c2 =-1 ; -ssize_t r; +int r, s; +bool bad; r =eytzinger0_find_le (test_array, nr,sizeof (test_array[0 ]), cmp_u16, &search); -if (r >=0 ) - c1 = test_array[r]; +if (r >=0 ) { +if (test_array[r] > search) { + bad = true; + }else { + s =eytzinger0_next (r, nr); + bad = s >=0 && test_array[s] <= search; + } + }else { + s =eytzinger0_last (nr); + bad = s >=0 && test_array[s] <= search; + } -for (i =0 ; i < nr; i++) -if (test_array[i] <= search && test_array[i] > c2) - c2 = test_array[i]; +if (bad) { + s =-1 ; +eytzinger0_for_each_prev (j, nr) { +if (test_array[j] <= search) { + s = j; +break ; + } + } -if (c1 != c2) {eytzinger0_for_each (j, nr)pr_info ("[%3u] = %12u\n" , j, test_array[j]); -pr_info ("find_le(%2u) -> [%2zi] = %2i should be %2i\n" , - i, r, c1, c2); +pr_info ("find_le(%12u) = %3i should be %3i\n" , + search, r, s); +BUG (); } } +static void eytzinger0_find_test_val (u16 *test_array,unsigned nr,u16 search) +{ +eytzinger0_find_test_le (test_array, nr, search); +} +void eytzinger0_find_test (void ) {unsigned i, nr, allocated =1 <<12 ;
Signature · 1 line
2.48.1
PATCH v3 10/21 bcachefs: convert eytzinger0_find_le to be 1-based
In reply to Andreas Gruenbachereytzinger0_find_le() is also easy to concert to 1-based eytzinger (but see the next commit).
--- fs/bcachefs/eytzinger.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
fs/bcachefs/eytzinger.h +8 -8
@@ -253,27 +253,27 @@ static inline unsigned inorder_to_eytzinger0(unsigned i, unsigned size)static inline int eytzinger0_find_le (void *base,size_t nr,size_t size,cmp_func_t cmp,const void *search) { -unsigned i, n =0 ; +void *base1 = base - size; +unsigned i, n =1 ;if (!nr)return -1 ;do { i = n; - n =eytzinger0_child (i,cmp (base + i * size, search) <=0 ); - }while (n < nr); + n =eytzinger1_child (i,cmp (base1 + i * size, search) <=0 ); + }while (n <= nr); -if (n &1 ) { +if (!(n &1 )) {/* * @i was greater than @search, return previous node: * * if @i was leftmost/smallest element, -* eytzinger0_prev(eytzinger0_first())) returns -1, as expected +* eytzinger1_prev(eytzinger1_first())) returns 0, as expected */ -return eytzinger0_prev (i, nr); - }else { -return i; + i =eytzinger1_prev (i, nr); } +return i -1 ; } static inline int eytzinger0_find_gt(void *base, size_t nr, size_t size,
Signature · 1 line
2.48.1
PATCH v3 11/21 bcachefs: simplify eytzinger0_find_le
In reply to Andreas GruenbacherReplace the over-complicated implementation of eytzinger0_find_le() by an equivalent, simpler version.
--- fs/bcachefs/eytzinger.h | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-)
fs/bcachefs/eytzinger.h +6 -20
@@ -254,26 +254,12 @@ static inline int eytzinger0_find_le(void *base, size_t nr, size_t size, cmp_func_t cmp, const void *search) { void *base1 = base - size; -unsigned i, n =1 ; - -if (!nr) -return -1 ; - -do { - i = n; - n =eytzinger1_child (i,cmp (base1 + i * size, search) <=0 ); - }while (n <= nr); - -if (!(n &1 )) { -/* -* @i was greater than @search, return previous node: -* -* if @i was leftmost/smallest element, -* eytzinger1_prev(eytzinger1_first())) returns 0, as expected -*/ - i =eytzinger1_prev (i, nr); - } -return i -1 ; +unsigned n =1 ; + +while (n <= nr) + n =eytzinger1_child (n,cmp (base1 + n * size, search) <=0 ); + n >>=__ffs (n) +1 ; +return n -1 ; } static inline int eytzinger0_find_gt(void *base, size_t nr, size_t size,
Signature · 1 line
2.48.1
PATCH v3 12/21 bcachefs: add eytzinger0_find_gt self test
In reply to Andreas GruenbacherAdd an eytzinger0_find_gt() self test similar to eytzinger0_find_le().
--- fs/bcachefs/util.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
fs/bcachefs/util.c +38 -0
@@ -819,9 +819,47 @@ static void eytzinger0_find_test_le(u16 *test_array, unsigned nr, u16 search) } } +static void eytzinger0_find_test_gt (u16 *test_array,unsigned nr,u16 search) +{ +int r, s; +bool bad; + + r =eytzinger0_find_gt (test_array, nr, +sizeof (test_array[0 ]), + cmp_u16, &search); +if (r >=0 ) { +if (test_array[r] <= search) { + bad = true; + }else { + s =eytzinger0_prev (r, nr); + bad = s >=0 && test_array[s] > search; + } + }else { + s =eytzinger0_first (nr); + bad = s >=0 && test_array[s] > search; + } + +if (bad) { + s =-1 ; +eytzinger0_for_each (j, nr) { +if (test_array[j] > search) { + s = j; +break ; + } + } + +eytzinger0_for_each (j, nr) +pr_info ("[%3u] = %12u\n" , j, test_array[j]); +pr_info ("find_gt(%12u) = %3i should be %3i\n" , + search, r, s); +BUG (); + } +} +static void eytzinger0_find_test_val (u16 *test_array,unsigned nr,u16 search) {eytzinger0_find_test_le (test_array, nr, search); +eytzinger0_find_test_gt (test_array, nr, search); }void eytzinger0_find_test (void )
Signature · 1 line
2.48.1
PATCH v3 13/21 bcachefs: implement eytzinger0_find_gt directly
In reply to Andreas GruenbacherInstead of implementing eytzinger0_find_gt() in terms of eytzinger0_find_le() and adjusting the result, implement it directly.
--- fs/bcachefs/eytzinger.h | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
fs/bcachefs/eytzinger.h +7 -10
@@ -262,20 +262,17 @@ static inline int eytzinger0_find_le(void *base, size_t nr, size_t size,return n -1 ; } +/* return smallest node > @search, or -1 if not found */ static inline int eytzinger0_find_gt (void *base,size_t nr,size_t size,cmp_func_t cmp,const void *search) { -ssize_t idx =eytzinger0_find_le (base, nr, size, cmp, search); +void *base1 = base - size; +unsigned n =1 ; -/* -* if eytitzinger0_find_le() returned -1 - no element was <= search - we -* want to return the first element; next/prev identities mean this work -* as expected -* -* similarly if find_le() returns last element, we should return -1; -* identities mean this all works out: -*/ -return eytzinger0_next (idx, nr); +while (n <= nr) + n =eytzinger1_child (n,cmp (base1 + n * size, search) <=0 ); + n >>=__ffs (n +1 ) +1 ; +return n -1 ; } static inline int eytzinger0_find_ge(void *base, size_t nr, size_t size,
Signature · 1 line
2.48.1
PATCH v3 14/21 bcachefs: implement eytzinger0_find_ge directly
In reply to Andreas GruenbacherImplement eytzinger0_find_ge() directly instead of implementing it in terms of eytzinger0_find_le() and adjusting the result.
This turns eytzinger0_find_ge() into a minimum search, so when there are duplicate elements, the result of eytzinger0_find_ge() will now always point at the first matching element.
--- fs/bcachefs/eytzinger.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
fs/bcachefs/eytzinger.h +7 -5
@@ -275,15 +275,17 @@ static inline int eytzinger0_find_gt(void *base, size_t nr, size_t size,return n -1 ; } +/* return smallest node >= @search, or -1 if not found */ static inline int eytzinger0_find_ge (void *base,size_t nr,size_t size,cmp_func_t cmp,const void *search) { -ssize_t idx =eytzinger0_find_le (base, nr, size, cmp, search); - -if (idx < nr && !cmp (base + idx * size, search)) -return idx; +void *base1 = base - size; +unsigned n =1 ; -return eytzinger0_next (idx, nr); +while (n <= nr) + n =eytzinger1_child (n,cmp (base1 + n * size, search) <0 ); + n >>=__ffs (n +1 ) +1 ; +return n -1 ; }#define eytzinger0_find (base, nr, size, _cmp, search) \
Signature · 1 line
2.48.1
PATCH v3 15/21 bcachefs: add eytzinger0_find_ge self test
In reply to Andreas GruenbacherAdd an eytzinger0_find_ge() self test similar to eytzinger0_find_gt().
Note that this test requires eytzinger0_find_ge() to return the first matching element in the array in case of duplicates. To prevent bisection errors, we only add this test after strenghening the original implementation (see the previous commit).
--- fs/bcachefs/util.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
fs/bcachefs/util.c +38 -0
@@ -856,10 +856,48 @@ static void eytzinger0_find_test_gt(u16 *test_array, unsigned nr, u16 search) } } +static void eytzinger0_find_test_ge (u16 *test_array,unsigned nr,u16 search) +{ +int r, s; +bool bad; + + r =eytzinger0_find_ge (test_array, nr, +sizeof (test_array[0 ]), + cmp_u16, &search); +if (r >=0 ) { +if (test_array[r] < search) { + bad = true; + }else { + s =eytzinger0_prev (r, nr); + bad = s >=0 && test_array[s] >= search; + } + }else { + s =eytzinger0_first (nr); + bad = s >=0 && test_array[s] >= search; + } + +if (bad) { + s =-1 ; +eytzinger0_for_each (j, nr) { +if (test_array[j] >= search) { + s = j; +break ; + } + } + +eytzinger0_for_each (j, nr) +pr_info ("[%3u] = %12u\n" , j, test_array[j]); +pr_info ("find_ge(%12u) = %3i should be %3i\n" , + search, r, s); +BUG (); + } +} +static void eytzinger0_find_test_val (u16 *test_array,unsigned nr,u16 search) {eytzinger0_find_test_le (test_array, nr, search);eytzinger0_find_test_gt (test_array, nr, search); +eytzinger0_find_test_ge (test_array, nr, search); }void eytzinger0_find_test (void )
Signature · 1 line
2.48.1
PATCH v3 16/21 bcachefs: Add eytzinger0_find self test
In reply to Andreas GruenbacherFunction eytzinger0_find() isn't currently covered, so add a self test.
We can rely on eytzinger0_find_le() here because it is being tested independently.
--- fs/bcachefs/util.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
fs/bcachefs/util.c +29 -0
@@ -893,11 +893,40 @@ static void eytzinger0_find_test_ge(u16 *test_array, unsigned nr, u16 search) } } +static void eytzinger0_find_test_eq (u16 *test_array,unsigned nr,u16 search) +{ +unsigned r; +int s; +bool bad; + + r =eytzinger0_find (test_array, nr, +sizeof (test_array[0 ]), + cmp_u16, &search); + +if (r < nr) { + bad = test_array[r] != search; + }else { + s =eytzinger0_find_le (test_array, nr, +sizeof (test_array[0 ]), + cmp_u16, &search); + bad = s >=0 && test_array[s] == search; + } + +if (bad) { +eytzinger0_for_each (j, nr) +pr_info ("[%3u] = %12u\n" , j, test_array[j]); +pr_info ("find(%12u) = %3i is incorrect\n" , + search, r); +BUG (); + } +} +static void eytzinger0_find_test_val (u16 *test_array,unsigned nr,u16 search) {eytzinger0_find_test_le (test_array, nr, search);eytzinger0_find_test_gt (test_array, nr, search);eytzinger0_find_test_ge (test_array, nr, search); +eytzinger0_find_test_eq (test_array, nr, search); }void eytzinger0_find_test (void )
Signature · 1 line
2.48.1
PATCH v3 17/21 bcachefs: convert eytzinger0_find to be 1-based
In reply to Andreas GruenbacherSeveral of the algorithms on eytzinger trees are implemented in terms of the eytzinger0 primitives. However, those algorithms can just as easily be expressed in terms of the eytzinger1 primitives, and that leads to better and easier to understand code. Start by converting eytzinger0_find().
--- fs/bcachefs/eytzinger.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
fs/bcachefs/eytzinger.h +7 -7
@@ -290,17 +290,17 @@ static inline int eytzinger0_find_ge(void *base, size_t nr, size_t size,#define eytzinger0_find (base, nr, size, _cmp, search) \ ({ \ - void *_base = (base); \ + size_t _size = (size); \ + void *_base1 = (void *)(base) - _size; \ const void *_search = (search); \ size_t _nr = (nr); \ - size_t _size = (size); \ - size_t _i = 0; \ + size_t _i = 1; \ int _res; \ \ - while (_i < _nr && \ - (_res = _cmp(_search, _base + _i * _size))) \ - _i = eytzinger0_child(_i, _res > 0); \ - _i; \ + while (_i <= _nr && \ + (_res = _cmp(_search, _base1 + _i * _size))) \ + _i = eytzinger1_child(_i, _res > 0); \ + _i - 1; \ }) void eytzinger0_sort_r(void *, size_t, size_t,
Signature · 1 line
2.48.1
PATCH v3 18/21 bcachefs: convert eytzinger sort to be 1-based (1)
In reply to Andreas GruenbacherIn this first step, convert the eytzinger sort functions to use 1-based primitives.
--- fs/bcachefs/eytzinger.c | 48 +++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 19 deletions(-)
fs/bcachefs/eytzinger.c +29 -19
@@ -148,28 +148,28 @@ static int do_cmp(const void *a, const void *b, cmp_r_func_t cmp, const void *prreturn cmp (a, b, priv); } -static inline int eytzinger0_do_cmp (void *base,size_t n,size_t size, +static inline int eytzinger1_do_cmp (void *base1,size_t n,size_t size,cmp_r_func_t cmp_func,const void *priv,size_t l,size_t r) { -return do_cmp (base +inorder_to_eytzinger0 (l, n) * size, - base +inorder_to_eytzinger0 (r, n) * size, +return do_cmp (base1 +inorder_to_eytzinger1 (l, n) * size, + base1 +inorder_to_eytzinger1 (r, n) * size, cmp_func, priv); } -static inline void eytzinger0_do_swap (void *base,size_t n,size_t size, +static inline void eytzinger1_do_swap (void *base1,size_t n,size_t size,swap_r_func_t swap_func,const void *priv,size_t l,size_t r) { -do_swap (base +inorder_to_eytzinger0 (l, n) * size, - base +inorder_to_eytzinger0 (r, n) * size, +do_swap (base1 +inorder_to_eytzinger1 (l, n) * size, + base1 +inorder_to_eytzinger1 (r, n) * size, size, swap_func, priv); } -void eytzinger0_sort_r (void *base,size_t n,size_t size, -cmp_r_func_t cmp_func, -swap_r_func_t swap_func, -const void *priv) +static void eytzinger1_sort_r (void *base1,size_t n,size_t size, +cmp_r_func_t cmp_func, +swap_r_func_t swap_func, +const void *priv) {int i, j, k;
@@ -178,9 +178,9 @@ void eytzinger0_sort_r(void *base, size_t n, size_t size, swap_func =NULL ;if (!swap_func) { -if (is_aligned (base, size,8 )) +if (is_aligned (base1, size,8 )) swap_func =SWAP_WORDS_64 ; -else if (is_aligned (base, size,4 )) +else if (is_aligned (base1, size,4 )) swap_func =SWAP_WORDS_32 ;else swap_func =SWAP_BYTES ;
@@ -190,47 +190,57 @@ void eytzinger0_sort_r(void *base, size_t n, size_t size,for (i = n /2 -1 ; i >=0 ; --i) {/* Find the sift-down path all the way to the leaves. */ for (j = i; k = j *2 +1 , k +1 < n;) - j =eytzinger0_do_cmp (base, n, size, cmp_func, priv, k, k +1 ) >0 ? k : k +1 ; + j =eytzinger1_do_cmp (base1, n, size, cmp_func, priv, k +1 , k +2 ) >0 ? k : k +1 ;/* Special case for the last leaf with no sibling. */ if (j *2 +2 == n) j = j *2 +1 ;/* Backtrack to the correct location. */ -while (j != i &&eytzinger0_do_cmp (base, n, size, cmp_func, priv, i, j) >=0 ) +while (j != i &&eytzinger1_do_cmp (base1, n, size, cmp_func, priv, i +1 , j +1 ) >=0 ) j = (j -1 ) /2 ;/* Shift the element into its correct place. */ for (k = j; j != i;) { j = (j -1 ) /2 ; -eytzinger0_do_swap (base, n, size, swap_func, priv, j, k); +eytzinger1_do_swap (base1, n, size, swap_func, priv, j +1 , k +1 ); } }/* sort */ for (i = n -1 ; i >0 ; --i) { -eytzinger0_do_swap (base, n, size, swap_func, priv,0 , i); +eytzinger1_do_swap (base1, n, size, swap_func, priv,1 , i +1 );/* Find the sift-down path all the way to the leaves. */ for (j =0 ; k = j *2 +1 , k +1 < i;) - j =eytzinger0_do_cmp (base, n, size, cmp_func, priv, k, k +1 ) >0 ? k : k +1 ; + j =eytzinger1_do_cmp (base1, n, size, cmp_func, priv, k +1 , k +2 ) >0 ? k : k +1 ;/* Special case for the last leaf with no sibling. */ if (j *2 +2 == i) j = j *2 +1 ;/* Backtrack to the correct location. */ -while (j &&eytzinger0_do_cmp (base, n, size, cmp_func, priv,0 , j) >=0 ) +while (j &&eytzinger1_do_cmp (base1, n, size, cmp_func, priv,1 , j +1 ) >=0 ) j = (j -1 ) /2 ;/* Shift the element into its correct place. */ for (k = j; j;) { j = (j -1 ) /2 ; -eytzinger0_do_swap (base, n, size, swap_func, priv, j, k); +eytzinger1_do_swap (base1, n, size, swap_func, priv, j +1 , k +1 ); } } } +void eytzinger0_sort_r (void *base,size_t n,size_t size, +cmp_r_func_t cmp_func, +swap_r_func_t swap_func, +const void *priv) +{ +void *base1 = base - size; + +return eytzinger1_sort_r (base1, n, size, cmp_func, swap_func, priv); +} +void eytzinger0_sort (void *base,size_t n,size_t size,cmp_func_t cmp_func,swap_func_t swap_func)
Signature · 1 line
2.48.1
PATCH v3 19/21 bcachefs: convert eytzinger sort to be 1-based (2)
In reply to Andreas GruenbacherIn this second step, transform the eytzinger indexes i, j, and k in eytzinger1_sort_r() from 0-based to 1-based. This step looks a bit messy, but the resulting code is slightly better.
--- fs/bcachefs/eytzinger.c | 42 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
fs/bcachefs/eytzinger.c +21 -21
@@ -171,7 +171,7 @@ static void eytzinger1_sort_r(void *base1, size_t n, size_t size,swap_r_func_t swap_func, const void *priv) { - int i, j, k; + unsigned i, j, k;/* called from 'sort' without swap function, let's pick the default */ if (swap_func ==SWAP_WRAPPER && !((struct wrapper *)priv)->swap_func)
@@ -187,46 +187,46 @@ static void eytzinger1_sort_r(void *base1, size_t n, size_t size, }/* heapify */ -for (i = n /2 -1 ; i >=0 ; --i) { +for (i = n /2 ; i >=1 ; --i) {/* Find the sift-down path all the way to the leaves. */ -for (j = i; k = j *2 +1 , k +1 < n;) - j =eytzinger1_do_cmp (base1, n, size, cmp_func, priv, k +1 , k +2 ) >0 ? k : k +1 ; +for (j = i; k = j *2 , k < n;) + j =eytzinger1_do_cmp (base1, n, size, cmp_func, priv, k, k +1 ) >0 ? k : k +1 ;/* Special case for the last leaf with no sibling. */ -if (j *2 +2 == n) - j = j *2 +1 ; +if (j *2 == n) + j *=2 ;/* Backtrack to the correct location. */ -while (j != i &&eytzinger1_do_cmp (base1, n, size, cmp_func, priv, i +1 , j +1 ) >=0 ) - j = (j -1 ) /2 ; +while (j != i &&eytzinger1_do_cmp (base1, n, size, cmp_func, priv, i, j) >=0 ) + j /=2 ;/* Shift the element into its correct place. */ for (k = j; j != i;) { - j = (j -1 ) /2 ; -eytzinger1_do_swap (base1, n, size, swap_func, priv, j +1 , k +1 ); + j /=2 ; +eytzinger1_do_swap (base1, n, size, swap_func, priv, j, k); } }/* sort */ -for (i = n -1 ; i >0 ; --i) { -eytzinger1_do_swap (base1, n, size, swap_func, priv,1 , i +1 ); +for (i = n; i >1 ; --i) { +eytzinger1_do_swap (base1, n, size, swap_func, priv,1 , i);/* Find the sift-down path all the way to the leaves. */ -for (j =0 ; k = j *2 +1 , k +1 < i;) - j =eytzinger1_do_cmp (base1, n, size, cmp_func, priv, k +1 , k +2 ) >0 ? k : k +1 ; +for (j =1 ; k = j *2 , k +1 < i;) + j =eytzinger1_do_cmp (base1, n, size, cmp_func, priv, k, k +1 ) >0 ? k : k +1 ;/* Special case for the last leaf with no sibling. */ -if (j *2 +2 == i) - j = j *2 +1 ; +if (j *2 +1 == i) + j *=2 ;/* Backtrack to the correct location. */ -while (j &&eytzinger1_do_cmp (base1, n, size, cmp_func, priv,1 , j +1 ) >=0 ) - j = (j -1 ) /2 ; +while (j >=1 &&eytzinger1_do_cmp (base1, n, size, cmp_func, priv,1 , j) >=0 ) + j /=2 ;/* Shift the element into its correct place. */ -for (k = j; j;) { - j = (j -1 ) /2 ; -eytzinger1_do_swap (base1, n, size, swap_func, priv, j +1 , k +1 ); +for (k = j; j >1 ;) { + j /=2 ; +eytzinger1_do_swap (base1, n, size, swap_func, priv, j, k); } } }
Signature · 1 line
2.48.1
PATCH v3 20/21 bcachefs: eytzinger1_{next,prev} cleanup
In reply to Andreas GruenbacherThe eytzinger code was previously relying on the following wrap-around properties and their "eytzinger0" equivalents:
eytzinger1_prev(0, size) == eytzinger1_last(size) eytzinger1_next(0, size) == eytzinger1_first(size)
However, these properties are no longer relied upon and no longer necessary, so remove the corresponding asserts and forbid the use of eytzinger1_prev(0, size) and eytzinger1_next(0, size).
This allows to further simplify the code in eytzinger1_next() and eytzinger1_prev(): where the left shifting happens, eytzinger1_next() is trying to move i to the lowest child on the left, which is equivalent to doubling i until the next doubling would cause it to be greater than size. This is implemented by shifting i to the left so that the most significant bits align and then shifting i to the right by one if the result is greater than size.
Likewise, eytzinger1_prev() is trying to move to the lowest child on the right; the same applies here.
The 1-offset in (size - 1) in eytzinger1_next() isn't needed at all, but the equivalent offset in eytzinger1_prev() is surprisingly needed to preserve the 'eytzinger1_prev(0, size) == eytzinger1_last(size)' property. However, since we no longer support that property, we can get rid of these offsets as well. This saves one addition in each function and makes the code less confusing.
--- fs/bcachefs/eytzinger.h | 18 ++++-------------- fs/bcachefs/util.c | 12 ------------ 2 files changed, 4 insertions(+), 26 deletions(-)
fs/bcachefs/eytzinger.h +4 -14
@@ -57,24 +57,14 @@ static inline unsigned eytzinger1_last(unsigned size)return rounddown_pow_of_two (size +1 ) -1 ; } -/* -* eytzinger1_next() and eytzinger1_prev() have the nice properties that -* -* eytzinger1_next(0) == eytzinger1_first()) -* eytzinger1_prev(0) == eytzinger1_last()) -* -* eytzinger1_prev(eytzinger1_first()) == 0 -* eytzinger1_next(eytzinger1_last()) == 0 -*/ -static inline unsigned eytzinger1_next (unsigned i,unsigned size) { -EYTZINGER_BUG_ON (i > size); +EYTZINGER_BUG_ON (i ==0 || i > size);if (eytzinger1_right_child (i) <= size) { i =eytzinger1_right_child (i); - i <<=__fls (size +1 ) -__fls (i); + i <<=__fls (size) -__fls (i); i >>= i > size; }else { i >>=ffz (i) +1 ;
@@ -85,12 +75,12 @@ static inline unsigned eytzinger1_next(unsigned i, unsigned size)static inline unsigned eytzinger1_prev (unsigned i,unsigned size) { -EYTZINGER_BUG_ON (i > size); +EYTZINGER_BUG_ON (i ==0 || i > size);if (eytzinger1_left_child (i) <= size) { i =eytzinger1_left_child (i) +1 ; - i <<=__fls (size +1 ) -__fls (i); + i <<=__fls (size) -__fls (i); i -=1 ; i >>= i > size; }else {
fs/bcachefs/util.c +0 -12
@@ -713,12 +713,6 @@ void eytzinger1_test(void)if (!(size %4096 ))pr_info ("tree size %u\n" , size); -BUG_ON (eytzinger1_prev (0 , size) !=eytzinger1_last (size)); -BUG_ON (eytzinger1_next (0 , size) !=eytzinger1_first (size)); - -BUG_ON (eytzinger1_prev (eytzinger1_first (size), size) !=0 ); -BUG_ON (eytzinger1_next (eytzinger1_last (size), size) !=0 ); - inorder =1 ;eytzinger1_for_each (eytz, size) {BUG_ON (__inorder_to_eytzinger1 (inorder, size, extra) != eytz);
@@ -747,12 +741,6 @@ void eytzinger0_test(void)if (!(size %4096 ))pr_info ("tree size %u\n" , size); -BUG_ON (eytzinger0_prev (-1 , size) !=eytzinger0_last (size)); -BUG_ON (eytzinger0_next (-1 , size) !=eytzinger0_first (size)); - -BUG_ON (eytzinger0_prev (eytzinger0_first (size), size) !=-1 ); -BUG_ON (eytzinger0_next (eytzinger0_last (size), size) !=-1 ); - inorder =0 ;eytzinger0_for_each (eytz, size) {BUG_ON (__inorder_to_eytzinger0 (inorder, size, extra) != eytz);
Signature · 1 line
2.48.1
PATCH v3 21/21 bcachefs: Run the eytzinger tests on modprobe
In reply to Andreas GruenbacherFOR DEBUGGING ONLY -- DO NOT MERGE!
--- fs/bcachefs/eytzinger.c | 12 +++--------- fs/bcachefs/eytzinger.h | 4 ++++ fs/bcachefs/super.c | 5 +++++ fs/bcachefs/util.c | 2 +- fs/bcachefs/util.h | 4 ++++ 5 files changed, 17 insertions(+), 10 deletions(-)
fs/bcachefs/eytzinger.c +3 -9
@@ -253,7 +253,7 @@ void eytzinger0_sort(void *base, size_t n, size_t size,return eytzinger0_sort_r (base, n, size, _CMP_WRAPPER,SWAP_WRAPPER , &w); } -#if 0 +#if 1 #include <linux/slab.h> #include <linux/random.h> #include <linux/ktime.h>
@@ -274,7 +274,7 @@ static int mycmp(const void *a, const void *b)return 0 ; } -static int test (void ) +void eytzinger_sort_test (void ) {size_t N , i;ktime_t start, end;
@@ -299,17 +299,11 @@ static int test(void)u32 prev =0 ;eytzinger0_for_each (i,N ) { -if (prev > arr[i]) - goto err; +BUG_ON (prev > arr[i]); prev = arr[i]; } kfree(arr); } -return 0 ; - -err: -kfree (arr); -return -1 ; }#endif
fs/bcachefs/eytzinger.h +4 -0
@@ -5,6 +5,8 @@#include <linux/bitops.h> #include <linux/log2.h> +#define EYTZINGER_DEBUG +#ifdef EYTZINGER_DEBUG #include <linux/bug.h> #define EYTZINGER_BUG_ON (cond) BUG_ON(cond)
@@ -297,4 +299,6 @@ void eytzinger0_sort_r(void *, size_t, size_t, cmp_r_func_t, swap_r_func_t, const void *);void eytzinger0_sort (void *,size_t ,size_t ,cmp_func_t ,swap_func_t ); +void eytzinger_sort_test (void ); +#endif /* _EYTZINGER_H */
fs/bcachefs/super.c +5 -0
@@ -2120,6 +2120,11 @@ static int __init bcachefs_init(void) {bch2_bkey_pack_test (); +eytzinger_sort_test (); +eytzinger1_test (); +eytzinger0_test (); +eytzinger0_find_test (); + if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || bch2_btree_key_cache_init() || bch2_chardev_init() ||
fs/bcachefs/util.c +1 -1
@@ -698,7 +698,7 @@ void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter) } } -#if 0 +#if 1 void eytzinger1_test (void ) {unsigned inorder, size;
fs/bcachefs/util.h +4 -0
@@ -696,4 +696,8 @@ static inline bool test_bit_le64(size_t bit, __le64 *addr) return (addr[bit / 64] & cpu_to_le64(BIT_ULL(bit % 64))) != 0; } +void eytzinger1_test (void ); +void eytzinger0_test (void ); +void eytzinger0_find_test (void ); +#endif /* _BCACHEFS_UTIL_H */
Signature · 1 line
2.48.1
Kent Overstreet
AppliedQuoted 39 lines · Andreas Gruenbacher
Kent,
here is another update. Changes:
* Added a self test for
eytzinger0_find().* The patches are now in fully bisectable order, with slightly improved patch descriptions.
* The patch that runs the self tests on modprobe has been moved to the end of the patch queue. This patch is useful for testing, but it SHOULD NOT BE MERGED.
A git tree of the patches can again be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/agruen/linux.git/log/?h=bcachefs
Base: v6.13
Head:d79258df8791("bcachefs: Run the eytzinger tests on modprobe")The previous CI test results at:
https://evilpiepirate.org/~testdashboard/ci?user=kmo&branch=eytzinger
against the previous version of this patch queue and with head:
94865378e1a3("bcachefs: eytzinger1_{next,prev} cleanup")show a large number of passed as well as failed tests even for upstream v6.13, so I don't know what to conclude from that.
In any case, there is one failure reporting that
eytzinger0_find_ge()didn't find the first matching element in an array with duplicate elements:
Quoted 8 lines
This is imtentional and expected because at this point in the patch queue,
eytzinger0_find_ge()hasn't been fixed to always return the first matching element. The current version of the patch queue adds the fix before adding the test, so this failure will be gone.That being said, as per request, the updated patch queue only enables the self tests in the last patch, so we wouldn't see failures of that kind anymore, anyway.
Thanks! Applied to my master & for-next branches.