lib: optimize cpumask_next_and()
Subsystems (4)
PATCH 1/1 lib: optimize cpumask_next_and()
We've measured that we spend ~0.6% of sys cpu time in cpumask_next_and(). It's essentially a joined iteration in search for a non-zero bit, which is currently implemented as a lookup join (find a nonzero bit on the lhs, lookup the rhs to see if it's set there).
Implement a direct join (find a nonzero bit on the incrementally built join). Direct benchmarking shows that it's 1.17x to 14x faster with a geometric mean of 2.1 on 32 CPUs. No impact on memory usage.
Approximate benchmark code:
```
unsigned long src1p[nr_cpumask_longs] = {pattern1};
unsigned long src2p[nr_cpumask_longs] = {pattern2};
for (/*a bunch of repetitions*/) {
for (int n = -1; n <= nr_cpu_ids; ++n) {
asm volatile("" : "+rm"(src1p)); // prevent any optimization
asm volatile("" : "+rm"(src2p));
unsigned long result = cpumask_next_and(n, src1p, src2p);
asm volatile("" : "+rm"(result));
}
}
```Results:
pattern1 pattern2 time_before/time_after
0x0000ffff 0x0000ffff 1.65
0x0000ffff 0x00005555 2.24
0x0000ffff 0x00001111 2.94
0x0000ffff 0x00000000 14.0
0x00005555 0x0000ffff 1.67
0x00005555 0x00005555 1.71
0x00005555 0x00001111 1.90
0x00005555 0x00000000 6.58
0x00001111 0x0000ffff 1.46
0x00001111 0x00005555 1.49
0x00001111 0x00001111 1.45
0x00001111 0x00000000 3.10
0x00000000 0x0000ffff 1.18
0x00000000 0x00005555 1.18
0x00000000 0x00001111 1.17
0x00000000 0x00000000 1.25
-----------------------------
geo.mean 2.06--- include/asm-generic/bitops/find.h | 16 ++++++++++++++++ include/linux/bitmap.h | 2 ++ lib/cpumask.c | 9 +++++---- lib/find_bit.c | 34 +++++++++++++++++++++++++++++++++ tools/include/asm-generic/bitops/find.h | 16 ++++++++++++++++ 5 files changed, 73 insertions(+), 4 deletions(-)
include/asm-generic/bitops/find.h +16 -0
@@ -15,6 +15,22 @@ extern unsigned long find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset);#endif +#ifndef find_next_and_bit +/** +* find_next_and_bit - find the next set bit in both memory regions +* @addr1: The first address to base the search on +* @addr2: The second address to base the search on +* @offset: The bitnumber to start searching at +* @size: The bitmap size in bits +* +* Returns the bit number for the next set bit +* If no bits are set, returns @size. +*/ +extern unsigned long find_next_and_bit (const unsigned long *addr1, +const unsigned long *addr2,unsigned long size, +unsigned long offset); +#endif +#ifndef find_next_zero_bit/** * find_next_zero_bit - find the next cleared bit in a memory region
include/linux/bitmap.h +2 -0
@@ -77,6 +77,8 @@* find_first_bit(addr, nbits) Position first set bit in *addr * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit +* find_next_and_bit(addr1, addr2, nbits, bit) Same as find_first_bit, but in +* (*addr1 & *addr2) */ /*
lib/cpumask.c +5 -4
@@ -32,10 +32,11 @@ EXPORT_SYMBOL(cpumask_next);int cpumask_next_and (int n,const struct cpumask *src1p,const struct cpumask *src2p) { -while ((n =cpumask_next (n, src1p)) < nr_cpu_ids) -if (cpumask_test_cpu (n, src2p)) -break ; -return n; +/* -1 is a legal arg here. */ +if (n !=-1 ) +cpumask_check (n); +return find_next_and_bit (cpumask_bits (src1p),cpumask_bits (src2p), + nr_cpumask_bits, n +1 ); }EXPORT_SYMBOL (cpumask_next_and);
lib/find_bit.c +34 -0
@@ -75,6 +75,40 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,EXPORT_SYMBOL (find_next_zero_bit);#endif +#if !defined(find_next_and_bit) + +/* +* Find the next set bit in a memory region. +*/ +unsigned long find_next_and_bit (const unsigned long *addr1, +const unsigned long *addr2,unsigned long nbits, +unsigned long start) +{ +unsigned long tmp; + +if (!nbits || start >= nbits) +return nbits; + + tmp = addr1[start /BITS_PER_LONG ] & addr2[start /BITS_PER_LONG ]; + +/* Handle 1st word. */ + tmp &=BITMAP_FIRST_WORD_MASK (start); + start =round_down (start,BITS_PER_LONG ); + +while (!tmp) { + start +=BITS_PER_LONG ; +if (start >= nbits) +return nbits; + + tmp = addr1[start /BITS_PER_LONG ] & + addr2[start /BITS_PER_LONG ]; + } + +return min (start +__ffs (tmp), nbits); +} +EXPORT_SYMBOL (find_next_and_bit); +#endif +#ifndef find_first_bit/* * Find the first set bit in a memory region.
tools/include/asm-generic/bitops/find.h +16 -0
@@ -15,6 +15,22 @@ extern unsigned long find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset);#endif +#ifndef find_next_and_bit +/** +* find_next_and_bit - find the next set bit in both memory regions +* @addr1: The first address to base the search on +* @addr2: The second address to base the search on +* @offset: The bitnumber to start searching at +* @size: The bitmap size in bits +* +* Returns the bit number for the next set bit +* If no bits are set, returns @size. +*/ +extern unsigned long find_next_and_bit (const unsigned long *addr1, +const unsigned long *addr2,unsigned long size, +unsigned long offset); +#endif +#ifndef find_next_zero_bit/**
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
Hi Clement,
Quoted 11 lines
lib/find_bit.c +3
-0@@ -75,6 +75,40 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,EXPORT_SYMBOL (find_next_zero_bit);#endif +#if !defined(find_next_and_bit) + +/*
lib/find_bit.c unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, · line 90
Quoted 10 lines
lib/find_bit.c +10
-0+* Find the next set bit in a memory region. +*/ +unsigned long find_next_and_bit (const unsigned long *addr1, +const unsigned long *addr2,unsigned long nbits, +unsigned long start) +{ +unsigned long tmp; + +if (!nbits || start >= nbits) +return nbits;
It should be:
if (unlikely(start >= nbits))
return nbits;
See patch e4afd2e5567f (lib/find_bit.c: micro-optimise find_next_*_bit) from Matthew Wilcox.
Quoted 10 lines
lib/find_bit.c +10
-0+ + tmp = addr1[start /BITS_PER_LONG ] & addr2[start /BITS_PER_LONG ]; + +/* Handle 1st word. */ + tmp &=BITMAP_FIRST_WORD_MASK (start); + start =round_down (start,BITS_PER_LONG ); + +while (!tmp) { + start +=BITS_PER_LONG ; +if (start >= nbits)
lib/find_bit.c unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, · line 110
Quoted 10 lines
lib/find_bit.c +10
-0+return nbits; + + tmp = addr1[start /BITS_PER_LONG ] & + addr2[start / BITS_PER_LONG]; + } + +return min (start +__ffs (tmp), nbits); +} +EXPORT_SYMBOL (find_next_and_bit); +#endif
This function is looking very based on _find_next_bit(). The original _find_next_bit() implements the invert-trick that allows share the same code for find_next_bit() and find_next_zero_bit(). Did you consider to take this approach for new function? If no objections from performance side, I think it worth to do for sane of completeness.
Yury