Skip to content

Commit 41d5941

Browse files
laoarAlexei Starovoitov
authored andcommitted
bpf: lpm_trie memory usage
trie_mem_usage() is introduced to calculate the lpm_trie memory usage. Some small memory allocations are ignored. The inner node is also ignored. The result as follows, - before 10: lpm_trie flags 0x1 key 8B value 8B max_entries 65536 memlock 1048576B - after 10: lpm_trie flags 0x1 key 8B value 8B max_entries 65536 memlock 2291536B Signed-off-by: Yafang Shao <laoar.shao@gmail.com> Link: https://lore.kernel.org/r/20230305124615.12358-3-laoar.shao@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 90a5527 commit 41d5941

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

kernel/bpf/lpm_trie.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,16 @@ static int trie_check_btf(const struct bpf_map *map,
720720
-EINVAL : 0;
721721
}
722722

723+
static u64 trie_mem_usage(const struct bpf_map *map)
724+
{
725+
struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
726+
u64 elem_size;
727+
728+
elem_size = sizeof(struct lpm_trie_node) + trie->data_size +
729+
trie->map.value_size;
730+
return elem_size * READ_ONCE(trie->n_entries);
731+
}
732+
723733
BTF_ID_LIST_SINGLE(trie_map_btf_ids, struct, lpm_trie)
724734
const struct bpf_map_ops trie_map_ops = {
725735
.map_meta_equal = bpf_map_meta_equal,
@@ -733,5 +743,6 @@ const struct bpf_map_ops trie_map_ops = {
733743
.map_update_batch = generic_map_update_batch,
734744
.map_delete_batch = generic_map_delete_batch,
735745
.map_check_btf = trie_check_btf,
746+
.map_mem_usage = trie_mem_usage,
736747
.map_btf_id = &trie_map_btf_ids[0],
737748
};

0 commit comments

Comments
 (0)