Dumping a map takes two BPF syscalls per element: one to get the next key and one to look it up. Use BPF_MAP_LOOKUP_BATCH instead, with keys and values bounded to 1 MiB and per-CPU value strides accounted for. The output is unchanged. For a hash map with 100,000 4-byte keys and values, a plain dump takes 5 BPF syscalls instead of 200,005. With output to /dev/null, the median of 15 runs in an x86-64 KVM guest on Linux 7.3 dropped from 86 ms to 52 ms (JSON: 78 ms to 42 ms). Printing to a terminal dominates either way. If a batch lookup fails, discard its output and continue with individual lookups after the last printed key. bpf_map_get_next_key() walks the map in the same order. This covers maps without batch support, and hash maps whose bucket lock cannot be taken, which individual lookups do not need. Link: https://github.com/libbpf/bpftool/issues/63 Suggested-by: Alexei Starovoitov Assisted-by: LLM Signed-off-by: Tianyi Chen --- tools/bpf/bpftool/map.c | 97 ++++++++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 15 deletions(-) diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 4b65edf74859b..c0bd9bf846492 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -59,13 +59,16 @@ static int map_type_from_str(const char *type) return -1; } -static void *alloc_value(struct bpf_map_info *info) +static size_t map_value_size(struct bpf_map_info *info) { if (map_is_per_cpu(info->type)) - return malloc(round_up(info->value_size, 8) * - get_possible_cpus()); - else - return malloc(info->value_size); + return (size_t)round_up(info->value_size, 8) * get_possible_cpus(); + return info->value_size; +} + +static void *alloc_value(struct bpf_map_info *info) +{ + return malloc(map_value_size(info)); } static int do_dump_btf(const struct btf_dumper *d, @@ -741,15 +744,10 @@ static int do_show(int argc, char **argv) return errno == ENOENT ? 0 : -1; } -static int dump_map_elem(int fd, void *key, void *value, - struct bpf_map_info *map_info, struct btf *btf, - json_writer_t *btf_wtr, const int *cpu_ids, int cpu_cnt) +static void print_map_elem(void *key, void *value, + struct bpf_map_info *map_info, struct btf *btf, + json_writer_t *btf_wtr, const int *cpu_ids, int cpu_cnt) { - if (bpf_map_lookup_elem(fd, key, value)) { - print_entry_error(map_info, key, errno); - return -1; - } - if (json_output) { print_entry_json(map_info, key, value, btf, cpu_ids, cpu_cnt); } else if (btf) { @@ -763,10 +761,74 @@ static int dump_map_elem(int fd, void *key, void *value, } else { print_entry_plain(map_info, key, value, cpu_ids, cpu_cnt); } +} +static int dump_map_elem(int fd, void *key, void *value, + struct bpf_map_info *map_info, struct btf *btf, + json_writer_t *btf_wtr, const int *cpu_ids, int cpu_cnt) +{ + if (bpf_map_lookup_elem(fd, key, value)) { + print_entry_error(map_info, key, errno); + return -1; + } + + print_map_elem(key, value, map_info, btf, btf_wtr, cpu_ids, cpu_cnt); return 0; } +#define MAP_DUMP_BATCH_BYTES (1U << 20) + +/* + * Print the elements returned by batched lookups, leaving the last one in + * @key. Return true at the end of the map. After any error, the caller + * continues with bpf_map_get_next_key(), which walks the map in the same order. + */ +static bool dump_map_batch(int fd, void *key, void *value, + struct bpf_map_info *info, struct btf *btf, + json_writer_t *wtr, const int *cpu_ids, int cpu_cnt, + unsigned int *num_elems) +{ + size_t value_size = map_value_size(info); + void *keys, *values, *batch, *in_batch = NULL; + __u32 capacity, count, i; + bool done = false; + int err; + + capacity = min(MAP_DUMP_BATCH_BYTES / (info->key_size + value_size), + (size_t)info->max_entries); + if (!capacity) + return false; + + /* Hash table batch cursors are u32 bucket indices; other maps use keys. */ + batch = malloc(max(info->key_size, 4U)); + keys = malloc((size_t)capacity * info->key_size); + values = malloc((size_t)capacity * value_size); + if (!batch || !keys || !values) + goto out; + + do { + count = capacity; + err = bpf_map_lookup_batch(fd, in_batch, batch, keys, values, + &count, NULL); + if (err && err != -ENOENT) + goto out; + for (i = 0; i < count; i++) { + /* Preserve alignment for odd-sized BTF keys and values. */ + memcpy(key, keys + (size_t)i * info->key_size, info->key_size); + memcpy(value, values + (size_t)i * value_size, value_size); + print_map_elem(key, value, info, btf, wtr, cpu_ids, cpu_cnt); + } + *num_elems += count; + in_batch = batch; + } while (!err); + done = true; +out: + free(batch); + free(keys); + free(values); + return done; +} + static int maps_have_btf(int *fds, int nb_fds) { struct bpf_map_info info = {}; @@ -836,7 +898,8 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr, struct btf *btf = NULL; int *cpu_ids = NULL; int cpu_cnt = 0; - int err; + bool done = false; + int err = 0; key = malloc(info->key_size); value = alloc_value(info); @@ -895,7 +958,11 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr, p_info("Warning: cannot read values from %s map with value_size != 8", map_type_str); } - while (true) { + done = dump_map_batch(fd, key, value, info, btf, wtr, cpu_ids, cpu_cnt, + &num_elems); + if (num_elems) + prev_key = key; + while (!done) { err = bpf_map_get_next_key(fd, prev_key, key); if (err) { if (errno == ENOENT) { -- 2.56.0