OSDN Git Service

libbpf: Improve btf__add_btf() with an additional hashmap for strings.
authorKui-Feng Lee <kuifeng@fb.com>
Wed, 19 Jan 2022 18:02:14 +0000 (10:02 -0800)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 19 Jan 2022 18:57:27 +0000 (10:57 -0800)
commitd81283d272661094ecc564709f25c7b7543308e0
tree242dacc3fc1f5bf2e931124ebeb0bcd4e02449d4
parent0ba3929e5b3d3fda05d3b9c8d0d20a90a084c19e
libbpf: Improve btf__add_btf() with an additional hashmap for strings.

Add a hashmap to map the string offsets from a source btf to the
string offsets from a target btf to reduce overheads.

btf__add_btf() calls btf__add_str() to add strings from a source to a
target btf.  It causes many string comparisons, and it is a major
hotspot when adding a big btf.  btf__add_str() uses strcmp() to check
if a hash entry is the right one.  The extra hashmap here compares
offsets of strings, that are much cheaper.  It remembers the results
of btf__add_str() for later uses to reduce the cost.

We are parallelizing BTF encoding for pahole by creating separated btf
instances for worker threads.  These per-thread btf instances will be
added to the btf instance of the main thread by calling btf__add_str()
to deduplicate and write out.  With this patch and -j4, the running
time of pahole drops to about 6.0s from 6.6s.

The following lines are the summary of 'perf stat' w/o the change.

       6.668126396 seconds time elapsed

      13.451054000 seconds user
       0.715520000 seconds sys

The following lines are the summary w/ the change.

       5.986973919 seconds time elapsed

      12.939903000 seconds user
       0.724152000 seconds sys

V4 fixes a bug of error checking against the pointer returned by
hashmap__new().

[v3] https://lore.kernel.org/bpf/20220118232053.2113139-1-kuifeng@fb.com/
[v2] https://lore.kernel.org/bpf/20220114193713.461349-1-kuifeng@fb.com/

Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220119180214.255634-1-kuifeng@fb.com
tools/lib/bpf/btf.c