Re: [PATCH bpf-next 6/6] selftest/bpf/benchs: Add benchmark for hashmap lookups

Andrii Nakryiko
2023-01-31T00:16:27Z

On Fri, Jan 27, 2023 at 10:14 AM Anton Protopopov <aspsk@isovalent.com> wrote:
>
> Add a new benchmark which measures hashmap lookup operations speed. A user can
> control the following parameters of the benchmark:
>
> * key_size (max 1024): the key size to use
> * max_entries: the hashmap max entries
> * nr_entries: the number of entries to insert/lookup
> * nr_loops: the number of loops for the benchmark
> * map_flags The hashmap flags passed to BPF_MAP_CREATE
>
> The BPF program performing the benchmarks calls two nested bpf_loop:
>
> bpf_loop(nr_loops/nr_entries)
> bpf_loop(nr_entries)
> bpf_map_lookup()
>
> So the nr_loops determines the number of actual map lookups. All lookups are
> successful.
>
> Example (the output is generated on a AMD Ryzen 9 3950X machine):
>
> for nr_entries in `seq 4096 4096 65536`; do echo -n "$((nr_entries*100/65536))% full: "; sudo ./bench -d2 -a bpf-hashmap-lookup --key_size=4 --nr_entries=$nr_entries --max_entries=65536 --nr_loops=1000000 --map_flags=0x40 | grep cpu; done
> 6% full: cpu01: lookup 50.739M ± 0.018M events/sec (approximated from 32 samples of ~19ms)
> 12% full: cpu01: lookup 47.751M ± 0.015M events/sec (approximated from 32 samples of ~20ms)
> 18% full: cpu01: lookup 45.153M ± 0.013M events/sec (approximated from 32 samples of ~22ms)
> 25% full: cpu01: lookup 43.826M ± 0.014M events/sec (approximated from 32 samples of ~22ms)
> 31% full: cpu01: lookup 41.971M ± 0.012M events/sec (approximated from 32 samples of ~23ms)
> 37% full: cpu01: lookup 41.034M ± 0.015M events/sec (approximated from 32 samples of ~24ms)
> 43% full: cpu01: lookup 39.946M ± 0.012M events/sec (approximated from 32 samples of ~25ms)
> 50% full: cpu01: lookup 38.256M ± 0.014M events/sec (approximated from 32 samples of ~26ms)
> 56% full: cpu01: lookup 36.580M ± 0.018M events/sec (approximated from 32 samples of ~27ms)
> 62% full: cpu01: lookup 36.252M ± 0.012M events/sec (approximated from 32 samples of ~27ms)
> 68% full: cpu01: lookup 35.200M ± 0.012M events/sec (approximated from 32 samples of ~28ms)
> 75% full: cpu01: lookup 34.061M ± 0.009M events/sec (approximated from 32 samples of ~29ms)
> 81% full: cpu01: lookup 34.374M ± 0.010M events/sec (approximated from 32 samples of ~29ms)
> 87% full: cpu01: lookup 33.244M ± 0.011M events/sec (approximated from 32 samples of ~30ms)
> 93% full: cpu01: lookup 32.182M ± 0.013M events/sec (approximated from 32 samples of ~31ms)
> 100% full: cpu01: lookup 31.497M ± 0.016M events/sec (approximated from 32 samples of ~31ms)
>
> Signed-off-by: Anton Protopopov <aspsk@isovalent.com>
> ---
> tools/testing/selftests/bpf/Makefile | 5 +-
> tools/testing/selftests/bpf/bench.c | 7 +
> tools/testing/selftests/bpf/bench.h | 3 +
> .../bpf/benchs/bench_bpf_hashmap_lookup.c | 277 ++++++++++++++++++
> .../selftests/bpf/progs/bpf_hashmap_lookup.c | 61 ++++
> 5 files changed, 352 insertions(+), 1 deletion(-)
> create mode 100644 tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_lookup.c
> create mode 100644 tools/testing/selftests/bpf/progs/bpf_hashmap_lookup.c
>

[...]

> +static error_t parse_arg(int key, char *arg, struct argp_state *state)
> +{
> + long ret;
> +
> + switch (key) {
> + case ARG_KEY_SIZE:
> + ret = strtol(arg, NULL, 10);
> + if (ret < 1 || ret > MAX_KEY_SIZE) {
> + fprintf(stderr, "invalid key_size");
> + argp_usage(state);
> + }
> + args.key_size = ret;
> + break;
> + case ARG_MAP_FLAGS:
> + if (!strncasecmp(arg, "0x", 2))
> + ret = strtol(arg, NULL, 0x10);
> + else
> + ret = strtol(arg, NULL, 10);

if you pass base as zero, strtol() will do this for you

> + if (ret < 0 || ret > UINT_MAX) {
> + fprintf(stderr, "invalid map_flags");
> + argp_usage(state);
> + }
> + args.map_flags = ret;
> + break; …