Skip to content

Commit 304849a

Browse files
laoarAlexei Starovoitov
authored andcommitted
bpf: hashtab memory usage
htab_map_mem_usage() is introduced to calculate hashmap memory usage. In this helper, some small memory allocations are ignore, as their size is quite small compared with the total size. The inner_map_meta in hash_of_map is also ignored. The result for hashtab as follows, - before this change 1: hash name count_map flags 0x1 <<<< no prealloc, fully set key 16B value 24B max_entries 1048576 memlock 41943040B 2: hash name count_map flags 0x1 <<<< no prealloc, none set key 16B value 24B max_entries 1048576 memlock 41943040B 3: hash name count_map flags 0x0 <<<< prealloc key 16B value 24B max_entries 1048576 memlock 41943040B The memlock is always a fixed size whatever it is preallocated or not, and whatever the count of allocated elements is. - after this change 1: hash name count_map flags 0x1 <<<< non prealloc, fully set key 16B value 24B max_entries 1048576 memlock 117441536B 2: hash name count_map flags 0x1 <<<< non prealloc, non set key 16B value 24B max_entries 1048576 memlock 16778240B 3: hash name count_map flags 0x0 <<<< prealloc key 16B value 24B max_entries 1048576 memlock 109056000B The memlock now is hashtab actually allocated. The result for percpu hash map as follows, - before this change 4: percpu_hash name count_map flags 0x0 <<<< prealloc key 16B value 24B max_entries 1048576 memlock 822083584B 5: percpu_hash name count_map flags 0x1 <<<< no prealloc key 16B value 24B max_entries 1048576 memlock 822083584B - after this change 4: percpu_hash name count_map flags 0x0 key 16B value 24B max_entries 1048576 memlock 897582080B 5: percpu_hash name count_map flags 0x1 key 16B value 24B max_entries 1048576 memlock 922748736B At worst, the difference can be 10x, for example, - before this change 6: hash name count_map flags 0x0 key 4B value 4B max_entries 1048576 memlock 8388608B - after this change 6: hash name count_map flags 0x0 key 4B value 4B max_entries 1048576 memlock 83889408B Signed-off-by: Yafang Shao <laoar.shao@gmail.com> Acked-by: Hou Tao <houtao1@huawei.com> Link: https://lore.kernel.org/r/20230305124615.12358-4-laoar.shao@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 41d5941 commit 304849a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

kernel/bpf/hashtab.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,44 @@ static int bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_f
21902190
return num_elems;
21912191
}
21922192

2193+
static u64 htab_map_mem_usage(const struct bpf_map *map)
2194+
{
2195+
struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
2196+
u32 value_size = round_up(htab->map.value_size, 8);
2197+
bool prealloc = htab_is_prealloc(htab);
2198+
bool percpu = htab_is_percpu(htab);
2199+
bool lru = htab_is_lru(htab);
2200+
u64 num_entries;
2201+
u64 usage = sizeof(struct bpf_htab);
2202+
2203+
usage += sizeof(struct bucket) * htab->n_buckets;
2204+
usage += sizeof(int) * num_possible_cpus() * HASHTAB_MAP_LOCK_COUNT;
2205+
if (prealloc) {
2206+
num_entries = map->max_entries;
2207+
if (htab_has_extra_elems(htab))
2208+
num_entries += num_possible_cpus();
2209+
2210+
usage += htab->elem_size * num_entries;
2211+
2212+
if (percpu)
2213+
usage += value_size * num_possible_cpus() * num_entries;
2214+
else if (!lru)
2215+
usage += sizeof(struct htab_elem *) * num_possible_cpus();
2216+
} else {
2217+
#define LLIST_NODE_SZ sizeof(struct llist_node)
2218+
2219+
num_entries = htab->use_percpu_counter ?
2220+
percpu_counter_sum(&htab->pcount) :
2221+
atomic_read(&htab->count);
2222+
usage += (htab->elem_size + LLIST_NODE_SZ) * num_entries;
2223+
if (percpu) {
2224+
usage += (LLIST_NODE_SZ + sizeof(void *)) * num_entries;
2225+
usage += value_size * num_possible_cpus() * num_entries;
2226+
}
2227+
}
2228+
return usage;
2229+
}
2230+
21932231
BTF_ID_LIST_SINGLE(htab_map_btf_ids, struct, bpf_htab)
21942232
const struct bpf_map_ops htab_map_ops = {
21952233
.map_meta_equal = bpf_map_meta_equal,
@@ -2206,6 +2244,7 @@ const struct bpf_map_ops htab_map_ops = {
22062244
.map_seq_show_elem = htab_map_seq_show_elem,
22072245
.map_set_for_each_callback_args = map_set_for_each_callback_args,
22082246
.map_for_each_callback = bpf_for_each_hash_elem,
2247+
.map_mem_usage = htab_map_mem_usage,
22092248
BATCH_OPS(htab),
22102249
.map_btf_id = &htab_map_btf_ids[0],
22112250
.iter_seq_info = &iter_seq_info,
@@ -2227,6 +2266,7 @@ const struct bpf_map_ops htab_lru_map_ops = {
22272266
.map_seq_show_elem = htab_map_seq_show_elem,
22282267
.map_set_for_each_callback_args = map_set_for_each_callback_args,
22292268
.map_for_each_callback = bpf_for_each_hash_elem,
2269+
.map_mem_usage = htab_map_mem_usage,
22302270
BATCH_OPS(htab_lru),
22312271
.map_btf_id = &htab_map_btf_ids[0],
22322272
.iter_seq_info = &iter_seq_info,
@@ -2378,6 +2418,7 @@ const struct bpf_map_ops htab_percpu_map_ops = {
23782418
.map_seq_show_elem = htab_percpu_map_seq_show_elem,
23792419
.map_set_for_each_callback_args = map_set_for_each_callback_args,
23802420
.map_for_each_callback = bpf_for_each_hash_elem,
2421+
.map_mem_usage = htab_map_mem_usage,
23812422
BATCH_OPS(htab_percpu),
23822423
.map_btf_id = &htab_map_btf_ids[0],
23832424
.iter_seq_info = &iter_seq_info,
@@ -2397,6 +2438,7 @@ const struct bpf_map_ops htab_lru_percpu_map_ops = {
23972438
.map_seq_show_elem = htab_percpu_map_seq_show_elem,
23982439
.map_set_for_each_callback_args = map_set_for_each_callback_args,
23992440
.map_for_each_callback = bpf_for_each_hash_elem,
2441+
.map_mem_usage = htab_map_mem_usage,
24002442
BATCH_OPS(htab_lru_percpu),
24012443
.map_btf_id = &htab_map_btf_ids[0],
24022444
.iter_seq_info = &iter_seq_info,
@@ -2534,6 +2576,7 @@ const struct bpf_map_ops htab_of_maps_map_ops = {
25342576
.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
25352577
.map_gen_lookup = htab_of_map_gen_lookup,
25362578
.map_check_btf = map_check_no_btf,
2579+
.map_mem_usage = htab_map_mem_usage,
25372580
BATCH_OPS(htab),
25382581
.map_btf_id = &htab_map_btf_ids[0],
25392582
};

0 commit comments

Comments
 (0)