bpf: stackmap memory usage
authorYafang Shao <laoar.shao@gmail.com>
Sun, 5 Mar 2023 12:46:02 +0000 (12:46 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 7 Mar 2023 17:33:42 +0000 (09:33 -0800)
A new helper is introduced to get stackmap memory usage. Some small
memory allocations are ignored as their memory size is quite small
compared to the totol usage.

The result as follows,
- before
16: stack_trace  name count_map  flags 0x0
        key 4B  value 8B  max_entries 65536  memlock 1048576B

- after
16: stack_trace  name count_map  flags 0x0
        key 4B  value 8B  max_entries 65536  memlock 2097472B

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Link: https://lore.kernel.org/r/20230305124615.12358-6-laoar.shao@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/stackmap.c

index aecea7451b6103785203e32a832f2e9590b4abae..0f1d8dced9337e99c2f89488bb49261cdd063dd8 100644 (file)
@@ -654,6 +654,19 @@ static void stack_map_free(struct bpf_map *map)
        put_callchain_buffers();
 }
 
+static u64 stack_map_mem_usage(const struct bpf_map *map)
+{
+       struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
+       u64 value_size = map->value_size;
+       u64 n_buckets = smap->n_buckets;
+       u64 enties = map->max_entries;
+       u64 usage = sizeof(*smap);
+
+       usage += n_buckets * sizeof(struct stack_map_bucket *);
+       usage += enties * (sizeof(struct stack_map_bucket) + value_size);
+       return usage;
+}
+
 BTF_ID_LIST_SINGLE(stack_trace_map_btf_ids, struct, bpf_stack_map)
 const struct bpf_map_ops stack_trace_map_ops = {
        .map_meta_equal = bpf_map_meta_equal,
@@ -664,5 +677,6 @@ const struct bpf_map_ops stack_trace_map_ops = {
        .map_update_elem = stack_map_update_elem,
        .map_delete_elem = stack_map_delete_elem,
        .map_check_btf = map_check_no_btf,
+       .map_mem_usage = stack_map_mem_usage,
        .map_btf_id = &stack_trace_map_btf_ids[0],
 };