Skip to content

Commit d81283d

Browse files
Kui-Feng Leeanakryiko
authored andcommitted
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
1 parent 0ba3929 commit d81283d

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

tools/lib/bpf/btf.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,20 +1620,37 @@ static int btf_commit_type(struct btf *btf, int data_sz)
16201620
struct btf_pipe {
16211621
const struct btf *src;
16221622
struct btf *dst;
1623+
struct hashmap *str_off_map; /* map string offsets from src to dst */
16231624
};
16241625

16251626
static int btf_rewrite_str(__u32 *str_off, void *ctx)
16261627
{
16271628
struct btf_pipe *p = ctx;
1628-
int off;
1629+
void *mapped_off;
1630+
int off, err;
16291631

16301632
if (!*str_off) /* nothing to do for empty strings */
16311633
return 0;
16321634

1635+
if (p->str_off_map &&
1636+
hashmap__find(p->str_off_map, (void *)(long)*str_off, &mapped_off)) {
1637+
*str_off = (__u32)(long)mapped_off;
1638+
return 0;
1639+
}
1640+
16331641
off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off));
16341642
if (off < 0)
16351643
return off;
16361644

1645+
/* Remember string mapping from src to dst. It avoids
1646+
* performing expensive string comparisons.
1647+
*/
1648+
if (p->str_off_map) {
1649+
err = hashmap__append(p->str_off_map, (void *)(long)*str_off, (void *)(long)off);
1650+
if (err)
1651+
return err;
1652+
}
1653+
16371654
*str_off = off;
16381655
return 0;
16391656
}
@@ -1680,6 +1697,9 @@ static int btf_rewrite_type_ids(__u32 *type_id, void *ctx)
16801697
return 0;
16811698
}
16821699

1700+
static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx);
1701+
static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx);
1702+
16831703
int btf__add_btf(struct btf *btf, const struct btf *src_btf)
16841704
{
16851705
struct btf_pipe p = { .src = src_btf, .dst = btf };
@@ -1713,6 +1733,11 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf)
17131733
if (!off)
17141734
return libbpf_err(-ENOMEM);
17151735

1736+
/* Map the string offsets from src_btf to the offsets from btf to improve performance */
1737+
p.str_off_map = hashmap__new(btf_dedup_identity_hash_fn, btf_dedup_equal_fn, NULL);
1738+
if (IS_ERR(p.str_off_map))
1739+
return libbpf_err(-ENOMEM);
1740+
17161741
/* bulk copy types data for all types from src_btf */
17171742
memcpy(t, src_btf->types_data, data_sz);
17181743

@@ -1754,6 +1779,8 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf)
17541779
btf->hdr->str_off += data_sz;
17551780
btf->nr_types += cnt;
17561781

1782+
hashmap__free(p.str_off_map);
1783+
17571784
/* return type ID of the first added BTF type */
17581785
return btf->start_id + btf->nr_types - cnt;
17591786
err_out:
@@ -1767,6 +1794,8 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf)
17671794
* wasn't modified, so doesn't need restoring, see big comment above */
17681795
btf->hdr->str_len = old_strs_len;
17691796

1797+
hashmap__free(p.str_off_map);
1798+
17701799
return libbpf_err(err);
17711800
}
17721801

0 commit comments

Comments
 (0)