veryconfusedrobot
veryconfusedrobot

Reputation: 95

BPF map find the number of elements

How do you find the number of elements in a BPF map? I can't find any useful information in the bcc reference guide.

Upvotes: 2

Views: 1128

Answers (1)

Qeole
Qeole

Reputation: 9174

Low-level

Once you have a file descriptor on your map (retrieved from a pinned path or from the map id for example), you can call the bpf() system call with its BPF_OBJ_GET_INFO_BY_FD subcommand. This will fill the attr argument with a pointer to a struct bpf_map_info, defined in the user API header file. In particular, attr->max_entries will be set to the maximum number of entries for the map. Wrappers such as bpf_obj_get_info_by_fd() from libbpf can help with the syscall details.

Array maps have a fixed number of entries, but some types such as hash maps have a maximum number, and an actual number of entries set at a current time. There is no counter available for the current number of entries in such case, the solution consists in iterating over all the entries to count them.

In BCC

For BCC, using the Python wrappers, I think (but have not tested) that you can access the maximum number of entries for a map foo with foo.max_entries (the attribute is defined in the definition of the base class TableBase). I also believe that you can use the operator len to get the current number of elements for some map types, for example for hash tables: len(foo). But again, I have not tried.

Upvotes: 1

Related Questions