Message ID | 20240217003918.52229-11-richard.henderson@linaro.org |
---|---|
State | New |
Headers | show |
Series | Optimize buffer_is_zero | expand |
On Fri, 16 Feb 2024, Richard Henderson wrote: > Benchmark each acceleration function vs an aligned buffer of zeros. > > Signed-off-by: Richard Henderson <richard.henderson@linaro.org> > --- > + > +static void test(const void *opaque) > +{ > + size_t len = 64 * KiB; This exceeds L1 cache capacity, so the performance ceiling of L2 cache throughput is easier to hit with a suboptimal implementation. It also seems to vastly exceed typical buffer sizes in Qemu. When preparing the patch we mostly tested at 8 KiB. The size decides whether the branch exiting the loop becomes perfectly predictable in the microbenchmark, e.g. at 128 bytes per iteration it exits on the 63'rd iteration, which Intel predictors cannot track, so we get one mispredict per call. (so perhaps smaller sizes like 2 or 4 KiB are better) Alexander
On 2/16/24 23:49, Alexander Monakov wrote: > > On Fri, 16 Feb 2024, Richard Henderson wrote: > >> Benchmark each acceleration function vs an aligned buffer of zeros. >> >> Signed-off-by: Richard Henderson <richard.henderson@linaro.org> >> --- >> + >> +static void test(const void *opaque) >> +{ >> + size_t len = 64 * KiB; > > This exceeds L1 cache capacity, so the performance ceiling of L2 cache > throughput is easier to hit with a suboptimal implementation. It also > seems to vastly exceed typical buffer sizes in Qemu. > > When preparing the patch we mostly tested at 8 KiB. The size decides > whether the branch exiting the loop becomes perfectly predictable in > the microbenchmark, e.g. at 128 bytes per iteration it exits on the > 63'rd iteration, which Intel predictors cannot track, so we get > one mispredict per call. > > (so perhaps smaller sizes like 2 or 4 KiB are better) Fair. I've adjusted to loop over 1, 4, 16, 64 KiB. # Start of bufferiszero tests # buffer_is_zero #0: 1KB 49227.29 MB/sec # buffer_is_zero #0: 4KB 137461.28 MB/sec # buffer_is_zero #0: 16KB 224220.41 MB/sec # buffer_is_zero #0: 64KB 142461.00 MB/sec # buffer_is_zero #1: 1KB 45423.59 MB/sec # buffer_is_zero #1: 4KB 91409.69 MB/sec # buffer_is_zero #1: 16KB 123819.94 MB/sec # buffer_is_zero #1: 64KB 71173.75 MB/sec # buffer_is_zero #2: 1KB 35465.03 MB/sec # buffer_is_zero #2: 4KB 56110.46 MB/sec # buffer_is_zero #2: 16KB 68852.28 MB/sec # buffer_is_zero #2: 64KB 39043.80 MB/sec r~
On Sat, Feb 17, 2024 at 09:21:50AM -1000, Richard Henderson wrote: > On 2/16/24 23:49, Alexander Monakov wrote: > > > > On Fri, 16 Feb 2024, Richard Henderson wrote: > > > > > Benchmark each acceleration function vs an aligned buffer of zeros. > > > > > > Signed-off-by: Richard Henderson <richard.henderson@linaro.org> > > > --- > > > + > > > +static void test(const void *opaque) > > > +{ > > > + size_t len = 64 * KiB; > > > > This exceeds L1 cache capacity, so the performance ceiling of L2 cache > > throughput is easier to hit with a suboptimal implementation. It also > > seems to vastly exceed typical buffer sizes in Qemu. > > > > When preparing the patch we mostly tested at 8 KiB. The size decides > > whether the branch exiting the loop becomes perfectly predictable in > > the microbenchmark, e.g. at 128 bytes per iteration it exits on the > > 63'rd iteration, which Intel predictors cannot track, so we get > > one mispredict per call. > > > > (so perhaps smaller sizes like 2 or 4 KiB are better) > > Fair. I've adjusted to loop over 1, 4, 16, 64 KiB. > > # Start of bufferiszero tests > # buffer_is_zero #0: 1KB 49227.29 MB/sec > # buffer_is_zero #0: 4KB 137461.28 MB/sec > # buffer_is_zero #0: 16KB 224220.41 MB/sec > # buffer_is_zero #0: 64KB 142461.00 MB/sec > # buffer_is_zero #1: 1KB 45423.59 MB/sec > # buffer_is_zero #1: 4KB 91409.69 MB/sec > # buffer_is_zero #1: 16KB 123819.94 MB/sec > # buffer_is_zero #1: 64KB 71173.75 MB/sec > # buffer_is_zero #2: 1KB 35465.03 MB/sec > # buffer_is_zero #2: 4KB 56110.46 MB/sec > # buffer_is_zero #2: 16KB 68852.28 MB/sec > # buffer_is_zero #2: 64KB 39043.80 MB/sec Totally nit-picking, but it would be easier to read with a little alignment and blanks lines: # buffer_is_zero #0: 1KB 49227.29 MB/sec # buffer_is_zero #0: 4KB 137461.28 MB/sec # buffer_is_zero #0: 16KB 224220.41 MB/sec # buffer_is_zero #0: 64KB 142461.00 MB/sec # buffer_is_zero #1: 1KB 45423.59 MB/sec # buffer_is_zero #1: 4KB 91409.69 MB/sec # buffer_is_zero #1: 16KB 123819.94 MB/sec # buffer_is_zero #1: 64KB 71173.75 MB/sec # buffer_is_zero #2: 1KB 35465.03 MB/sec # buffer_is_zero #2: 4KB 56110.46 MB/sec # buffer_is_zero #2: 16KB 68852.28 MB/sec # buffer_is_zero #2: 64KB 39043.80 MB/sec With regards, Daniel
diff --git a/tests/bench/bufferiszero-bench.c b/tests/bench/bufferiszero-bench.c new file mode 100644 index 0000000000..1fa2eb6973 --- /dev/null +++ b/tests/bench/bufferiszero-bench.c @@ -0,0 +1,42 @@ +/* + * QEMU buffer_is_zero speed benchmark + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + */ +#include "qemu/osdep.h" +#include "qemu/cutils.h" +#include "qemu/units.h" + +static void test(const void *opaque) +{ + size_t len = 64 * KiB; + void *buf = g_malloc0(len); + int accel_index = 0; + + do { + double total = 0.0; + + g_test_timer_start(); + do { + buffer_is_zero_ge256(buf, len); + total += len; + } while (g_test_timer_elapsed() < 5.0); + + total /= MiB; + g_test_message("buffer_is_zero #%d: %.2f MB/sec", + accel_index, total / g_test_timer_last()); + + accel_index++; + } while (test_buffer_is_zero_next_accel()); + + g_free(buf); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + g_test_add_data_func("/cutils/bufferiszero/speed", NULL, test); + return g_test_run(); +} diff --git a/tests/bench/meson.build b/tests/bench/meson.build index 7e76338a52..70d45ff400 100644 --- a/tests/bench/meson.build +++ b/tests/bench/meson.build @@ -17,7 +17,9 @@ executable('atomic64-bench', dependencies: [qemuutil], build_by_default: false) -benchs = {} +benchs = { + 'bufferiszero-bench': [], +} if have_block benchs += {
Benchmark each acceleration function vs an aligned buffer of zeros. Signed-off-by: Richard Henderson <richard.henderson@linaro.org> --- tests/bench/bufferiszero-bench.c | 42 ++++++++++++++++++++++++++++++++ tests/bench/meson.build | 4 ++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/bench/bufferiszero-bench.c