btf: Sort BTF types by name and kind to optimize btf_find_by_name_kind lookup
RFC PATCH 1/1 btf: Sort BTF types by name and kind to optimize btf_find_by_name_kind lookup
From: pengdonglin <[email protected]>
Currently, when the funcgraph-args feature is in use, the btf_find_by_name_kind function is invoked quite frequently. However, this function only supports linear search. When the number of btf_type entries to search through is large, such as in the vmlinux BTF which contains over 80,000 named btf_types, it consumes a significant amount of time.
This patch optimizes the btf_find_by_name_kind lookup by sorting BTF types according to their names and kinds. Additionally, it modifies the search direction. Now, it first searches the BTF and then its base.
It should be noted that this change incurs some additional memory and boot-time overhead. Therefore, the option is disabled by default.
Here is a test case:
# echo 1 > options/funcgraph-args # echo function_graph > current_tracer
Before:
# time cat trace | wc -l 124176
real 0m16.154s user 0m0.000s sys 0m15.962s
After:
# time cat trace | wc -l 124176
real 0m0.948s user 0m0.000s sys 0m0.973s
An improvement of more than 20 times can be observed.
--- include/linux/btf.h | 1 + kernel/bpf/Kconfig | 13 ++++ kernel/bpf/btf.c | 160 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 165 insertions(+), 9 deletions(-)
include/linux/btf.h +1 -0
@@ -220,6 +220,7 @@ bool btf_is_module(const struct btf *btf);bool btf_is_vmlinux (const struct btf *btf);struct module *btf_try_get_module (const struct btf *btf);u32 btf_nr_types (const struct btf *btf); +u32 btf_type_cnt (const struct btf *btf);struct btf *btf_base_btf (const struct btf *btf);bool btf_type_is_i32 (const struct btf_type *t);bool btf_type_is_i64 (const struct btf_type *t);
kernel/bpf/Kconfig +13 -0
@@ -101,4 +101,17 @@ config BPF_LSM If you are unsure how to answer this question, answer N. +config BPF_SORT_BTF_BY_NAME_KIND + bool "Sort BTF type by name and kind" + depends on DEBUG_INFO_BTF + default n + help + Sort BTF types by name and kind to enable binary search, improving + the performance of btf_find_by_name_kind. Currently applies to + vmlinux and kernel module BTFs. Note that this option introduces + extra memory and boot-time overhead. + + For instance, a BTF file with 80,000 named btf_types consumes + approximately 312 KB of additional memory. + endmenu # "BPF subsystem"
kernel/bpf/btf.c +151 -9
221 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 10 lines · pengdonglin
From: pengdonglin <[email protected]>
Currently, when the funcgraph-args feature is in use, the
btf_find_by_name_kindfunction is invoked quite frequently. However, this function only supports linear search. When the number ofbtf_typeentries to search through is large, such as in the vmlinux BTF which contains over 80,000 namedbtf_types, it consumes a significant amount of time.
Quoted 3 lines
This patch optimizes the
btf_find_by_name_kindlookup by sorting BTF types according to their names and kinds. Additionally, it modifies the search direction. Now, it first searches the BTF and then its base.
Well, the latter is a meaningful change outside of sorting. Split it out and justify separately?
Quoted 35 lines
It should be noted that this change incurs some additional memory and boot-time overhead. Therefore, the option is disabled by default.
Here is a test case:
# echo 1 > options/funcgraph-args # echo function_graph > current_tracerBefore:
# time cat trace | wc -l 124176real 0m16.154s user 0m0.000s sys 0m15.962sAfter:
# time cat trace | wc -l 124176real 0m0.948s user 0m0.000s sys 0m0.973sAn improvement of more than 20 times can be observed.
Cc Eduard Zingerman <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Song Liu <[email protected]>Cc Masami Hiramatsu (Google) <[email protected]>Cc Steven Rostedt <[email protected]>Signed-off-by pengdonglin <[email protected]>Signed-off-by pengdonglin <[email protected]>
Quoted 6 lines
--- include/linux/btf.h | 1 + kernel/bpf/Kconfig | 13 ++++ kernel/bpf/btf.c | 160 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 165 insertions(+), 9 deletions(-)
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
- given we always know kind we are searching for, I'd sort by kind, then by name, it probably will be a touch faster because we'll be quickly skipping lots of elements clustered by kind we don't care about;
- instead of having BPF_SORT_BTF_BY_NAME_KIND, we should probably just have a lazy sorting approach, and maybe employ a bit more sophisticated heuristic. E.g., not by number of BTF types (or at least not just by that), but by the total number of entries we had to skip to find something. For small BTFs we might not reach this budget ever. For vmlinux BTF we are almost definitely hitting it on first-second-third search. Once the condition is hit, allocate sorted_ids index, sort, remember. On subsequent searches use the index.
WDYT?
[...]
Quoted 10 lines
kernel/bpf/btf.c +10
-0+static void btf_sort_by_name_kind (struct btf *btf) +{ +const struct btf_type *t; +struct btf_sorted_ids *sorted_ids; +const char *name; +u32 *ids; +u32 total, cnt =0 ; +u32 i, j =0 ; + + total =btf_type_cnt (btf);
kernel/bpf/btf.c int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_ty · line 6298
Quoted 10 lines
kernel/bpf/btf.c +10
-0+for (i = btf->start_id; i < total; i++) { + t =btf_type_by_id (btf, i); + name =btf_name_by_offset (btf, t->name_off); +if (str_is_empty (name)) +continue ; + cnt++; + } + +/* Use linear search when the number is below the threshold */ +if (cnt <8 )
kind of a random threshold, at least give it a name
Quoted 7 lines
kernel/bpf/btf.c +7
-0+return ; + + sorted_ids =kvmalloc (struct_size (sorted_ids, ids, cnt),GFP_KERNEL ); +if (!sorted_ids) { +pr_warn ("Failed to allocate memory for sorted_ids\n" ); +return ; + }
[...]
Quoted 3 lines · Andrii Nakryiko
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
iirc we discussed it in the past and decided to do sorting in pahole and let the kernel verify whether it's sorted or not. Then no extra memory is needed.
Or was that idea discarded for some reason?
Quoted 11 lines · Alexei Starovoitov
Quoted 3 lines · Andrii Nakryiko
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
iirc we discussed it in the past and decided to do sorting in pahole and let the kernel verify whether it's sorted or not. Then no extra memory is needed.
Or was that idea discarded for some reason?
Don't really remember at this point, tbh. Pre-sorting should work (though I'd argue that then we should only sort by name to make this sorting universally useful, doing linear search over kinds is fast, IMO). Pre-sorting won't work for program BTFs, don't know how important that is. This indexing on demand approach would be universal. ¯\_(ツ)_/¯
Overall, paying 300KB for sorted index for vmlinux BTF for cases where we repeatedly need this seems ok to me, tbh.
Quoted 22 lines · Andrii Nakryiko
Quoted 11 lines · Alexei Starovoitov
Quoted 3 lines · Andrii Nakryiko
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
iirc we discussed it in the past and decided to do sorting in pahole and let the kernel verify whether it's sorted or not. Then no extra memory is needed.
Or was that idea discarded for some reason?Don't really remember at this point, tbh. Pre-sorting should work (though I'd argue that then we should only sort by name to make this sorting universally useful, doing linear search over kinds is fast, IMO). Pre-sorting won't work for program BTFs, don't know how important that is. This indexing on demand approach would be universal. ¯\_(ツ)_/¯
Quoted 2 lines
Overall, paying 300KB for sorted index for vmlinux BTF for cases where we repeatedly need this seems ok to me, tbh.
If pahole sorting works I don't see why consuming even 300k is ok. kallsyms are sorted during the build too.
In the other thread we discuss adding LOCSEC for ~6M. That thing should be pahole-sorted too.
Quoted 25 lines · Alexei Starovoitov
Quoted 22 lines · Andrii Nakryiko
Quoted 11 lines · Alexei Starovoitov
Quoted 3 lines · Andrii Nakryiko
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
iirc we discussed it in the past and decided to do sorting in pahole and let the kernel verify whether it's sorted or not. Then no extra memory is needed.
Or was that idea discarded for some reason?Don't really remember at this point, tbh. Pre-sorting should work (though I'd argue that then we should only sort by name to make this sorting universally useful, doing linear search over kinds is fast, IMO). Pre-sorting won't work for program BTFs, don't know how important that is. This indexing on demand approach would be universal. ¯\_(ツ)_/¯
Quoted 5 lines
Quoted 2 lines
Overall, paying 300KB for sorted index for vmlinux BTF for cases where we repeatedly need this seems ok to me, tbh.
If pahole sorting works I don't see why consuming even 300k is ok. kallsyms are sorted during the build too.
Thanks. We did discuss pre-sorting in pahole in the threads:
https://lore.kernel.org/all/CAADnVQLMHUNE95eBXdy6=+gHoFHRsihmQ75GZvGy-hSuHoaT5A@mail.gmail.com/
https://lore.kernel.org/all/CAEf4BzaXHrjoEWmEcvK62bqKuT3de__+juvGctR3=e8avRWpMQ@mail.gmail.com/
However, since that approach depends on newer pahole features and btf_find_by_name_kind is already being called quite frequently, I suggest we first implement sorting within the kernel, and subsequently add pre-sorting support in pahole.
Quoted 3 lines
In the other thread we discuss adding LOCSEC for ~6M. That thing should be pahole-sorted too.
Quoted 36 lines · Donglin Peng
Quoted 25 lines · Alexei Starovoitov
Quoted 22 lines · Andrii Nakryiko
Quoted 11 lines · Alexei Starovoitov
Quoted 3 lines · Andrii Nakryiko
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
iirc we discussed it in the past and decided to do sorting in pahole and let the kernel verify whether it's sorted or not. Then no extra memory is needed.
Or was that idea discarded for some reason?Don't really remember at this point, tbh. Pre-sorting should work (though I'd argue that then we should only sort by name to make this sorting universally useful, doing linear search over kinds is fast, IMO). Pre-sorting won't work for program BTFs, don't know how important that is. This indexing on demand approach would be universal. ¯\_(ツ)_/¯
Quoted 5 lines
Quoted 2 lines
Overall, paying 300KB for sorted index for vmlinux BTF for cases where we repeatedly need this seems ok to me, tbh.
If pahole sorting works I don't see why consuming even 300k is ok. kallsyms are sorted during the build too.
Thanks. We did discuss pre-sorting in pahole in the threads:
Quoted 7 lines
https://lore.kernel.org/all/CAADnVQLMHUNE95eBXdy6=+gHoFHRsihmQ75GZvGy-hSuHoaT5A@mail.gmail.com/
https://lore.kernel.org/all/CAEf4BzaXHrjoEWmEcvK62bqKuT3de__+juvGctR3=e8avRWpMQ@mail.gmail.com/However, since that approach depends on newer pahole features and
btf_find_by_name_kindis already being called quite frequently, I suggest we first implement sorting within the kernel, and subsequently add pre-sorting support in pahole.
and then what? Remove it from the kernel when pahole is newer? I'd rather not do this churn in the first place.
Since you revived that thread from 2024 and did not follow up with pahole changes since then, I don't believe that you will do them if we land kernel changes first.
Quoted 41 lines · Alexei Starovoitov
Quoted 39 lines · Donglin Peng
Quoted 25 lines · Alexei Starovoitov
Quoted 22 lines · Andrii Nakryiko
Quoted 11 lines · Alexei Starovoitov
Quoted 3 lines · Andrii Nakryiko
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
iirc we discussed it in the past and decided to do sorting in pahole and let the kernel verify whether it's sorted or not. Then no extra memory is needed.
Or was that idea discarded for some reason?Don't really remember at this point, tbh. Pre-sorting should work (though I'd argue that then we should only sort by name to make this sorting universally useful, doing linear search over kinds is fast, IMO). Pre-sorting won't work for program BTFs, don't know how important that is. This indexing on demand approach would be universal. ¯\_(ツ)_/¯
Quoted 5 lines
Quoted 2 lines
Overall, paying 300KB for sorted index for vmlinux BTF for cases where we repeatedly need this seems ok to me, tbh.
If pahole sorting works I don't see why consuming even 300k is ok. kallsyms are sorted during the build too.
Thanks. We did discuss pre-sorting in pahole in the threads:
https://lore.kernel.org/all/CAADnVQLMHUNE95eBXdy6=+gHoFHRsihmQ75GZvGy-hSuHoaT5A@mail.gmail.com/
https://lore.kernel.org/all/CAEf4BzaXHrjoEWmEcvK62bqKuT3de__+juvGctR3=e8avRWpMQ@mail.gmail.com/
Quoted 7 lines
Quoted 4 lines
However, since that approach depends on newer pahole features and
btf_find_by_name_kindis already being called quite frequently, I suggest we first implement sorting within the kernel, and subsequently add pre-sorting support in pahole.and then what? Remove it from the kernel when pahole is newer? I'd rather not do this churn in the first place.
Apologies for the formatting issues in my previous email—sending this again
for clarity.
Thank you for your feedback. Your concerns are completely valid.
I’d like to suggest a dual-mechanism approach:
1. If BTF is generated by a newer pahole (with pre-sorting support), the kernel would use the pre-sorted data directly.
2. For BTF from older pahole versions, the kernel would handle sorting at load time or later.
This would provide performance benefits immediately while preserving
backward compatibility. The kernel-side sorting would remain intact
moving forward, avoiding future churn.
Quoted 4 lines
Since you revived that thread from 2024 and did not follow up with pahole changes since then, I don't believe that you will do them if we land kernel changes first.
Regarding the pahole changes: this is now my highest priority. I’ve already incorporated it into my development plan and will begin working on the patches shortly.
What do you think about this approach? Would this be acceptable?
Quoted 62 lines · Donglin Peng
Quoted 41 lines · Alexei Starovoitov
Quoted 39 lines · Donglin Peng
Quoted 25 lines · Alexei Starovoitov
Quoted 22 lines · Andrii Nakryiko
Quoted 11 lines · Alexei Starovoitov
Quoted 3 lines · Andrii Nakryiko
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
iirc we discussed it in the past and decided to do sorting in pahole and let the kernel verify whether it's sorted or not. Then no extra memory is needed.
Or was that idea discarded for some reason?Don't really remember at this point, tbh. Pre-sorting should work (though I'd argue that then we should only sort by name to make this sorting universally useful, doing linear search over kinds is fast, IMO). Pre-sorting won't work for program BTFs, don't know how important that is. This indexing on demand approach would be universal. ¯\_(ツ)_/¯
Quoted 5 lines
Quoted 2 lines
Overall, paying 300KB for sorted index for vmlinux BTF for cases where we repeatedly need this seems ok to me, tbh.
If pahole sorting works I don't see why consuming even 300k is ok. kallsyms are sorted during the build too.
Thanks. We did discuss pre-sorting in pahole in the threads:
https://lore.kernel.org/all/CAADnVQLMHUNE95eBXdy6=+gHoFHRsihmQ75GZvGy-hSuHoaT5A@mail.gmail.com/
https://lore.kernel.org/all/CAEf4BzaXHrjoEWmEcvK62bqKuT3de__+juvGctR3=e8avRWpMQ@mail.gmail.com/Quoted 7 lines
Quoted 4 lines
However, since that approach depends on newer pahole features and
btf_find_by_name_kindis already being called quite frequently, I suggest we first implement sorting within the kernel, and subsequently add pre-sorting support in pahole.and then what? Remove it from the kernel when pahole is newer? I'd rather not do this churn in the first place.
Apologies for the formatting issues in my previous email—sending this again
for clarity.Thank you for your feedback. Your concerns are completely valid.
I’d like to suggest a dual-mechanism approach:
1. If BTF is generated by a newer pahole (with pre-sorting support), the kernel would use the pre-sorted data directly.
2. For BTF from older pahole versions, the kernel would handle sorting at load time or later.
Quoted 4 lines
This would provide performance benefits immediately while preserving
backward compatibility. The kernel-side sorting would remain intact
moving forward, avoiding future churn.
If you're taking the approach of doing both - which is best I think - I'd suggest it might be helpful to look at the bpf_relocate.c code; it's shared between libbpf and kernel, so you could potentially add shared code to do sorting in libbpf (which pahole would use) and the kernel would use too; this would help ensure the behaviour is identical.
Maybe for pahole/libbpf sorting could be done via a new BTF dedup() option, since dedup is the time we finalize the BTF representation?
Alan
Quoted 69 lines · Alan Maguire
Quoted 62 lines · Donglin Peng
Quoted 41 lines · Alexei Starovoitov
Quoted 39 lines · Donglin Peng
Quoted 25 lines · Alexei Starovoitov
Quoted 22 lines · Andrii Nakryiko
Quoted 11 lines · Alexei Starovoitov
Quoted 3 lines · Andrii Nakryiko
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
iirc we discussed it in the past and decided to do sorting in pahole and let the kernel verify whether it's sorted or not. Then no extra memory is needed.
Or was that idea discarded for some reason?Don't really remember at this point, tbh. Pre-sorting should work (though I'd argue that then we should only sort by name to make this sorting universally useful, doing linear search over kinds is fast, IMO). Pre-sorting won't work for program BTFs, don't know how important that is. This indexing on demand approach would be universal. ¯\_(ツ)_/¯
Quoted 5 lines
Quoted 2 lines
Overall, paying 300KB for sorted index for vmlinux BTF for cases where we repeatedly need this seems ok to me, tbh.
If pahole sorting works I don't see why consuming even 300k is ok. kallsyms are sorted during the build too.
Thanks. We did discuss pre-sorting in pahole in the threads:
https://lore.kernel.org/all/CAADnVQLMHUNE95eBXdy6=+gHoFHRsihmQ75GZvGy-hSuHoaT5A@mail.gmail.com/
https://lore.kernel.org/all/CAEf4BzaXHrjoEWmEcvK62bqKuT3de__+juvGctR3=e8avRWpMQ@mail.gmail.com/Quoted 7 lines
Quoted 4 lines
However, since that approach depends on newer pahole features and
btf_find_by_name_kindis already being called quite frequently, I suggest we first implement sorting within the kernel, and subsequently add pre-sorting support in pahole.and then what? Remove it from the kernel when pahole is newer? I'd rather not do this churn in the first place.
Apologies for the formatting issues in my previous email—sending this again
for clarity.Thank you for your feedback. Your concerns are completely valid.
I’d like to suggest a dual-mechanism approach:
1. If BTF is generated by a newer pahole (with pre-sorting support), the kernel would use the pre-sorted data directly.
2. For BTF from older pahole versions, the kernel would handle sorting at load time or later.Quoted 4 lines
This would provide performance benefits immediately while preserving
backward compatibility. The kernel-side sorting would remain intact
moving forward, avoiding future churn.
Quoted 8 lines
If you're taking the approach of doing both - which is best I think - I'd suggest it might be helpful to look at the
bpf_relocate.ccode; it's shared between libbpf and kernel, so you could potentially add shared code to do sorting in libbpf (which pahole would use) and the kernel would use too; this would help ensure the behaviour is identical.Maybe for pahole/libbpf sorting could be done via a new BTF
dedup()option, since dedup is the time we finalize the BTF representation?
Thanks for the suggestion. I'll look into that.
Quoted 2 lines
Alan
Quoted 6 lines · Donglin Peng
I’d like to suggest a dual-mechanism approach:
1. If BTF is generated by a newer pahole (with pre-sorting support), the kernel would use the pre-sorted data directly.
2. For BTF from older pahole versions, the kernel would handle sorting at load time or later.
The problem with 2 is extra memory consumption for narrow use case. The "time cat trace" example shows that search is in critical path, but I suspect ftrace can do it differently. I don't know why it's doing the search so much. Everyelse in bpf we don't call it that often.
So optimizing the search is nice, but not at the expense of so much extra memory.
Hence I don't think 2 is worth doing.
Quoted 3 lines
Regarding the pahole changes: this is now my highest priority. I’ve already incorporated it into my development plan and will begin working on the patches shortly.
let's land pahole changes first.
Quoted 9 lines · Alexei Starovoitov
Quoted 6 lines · Donglin Peng
I’d like to suggest a dual-mechanism approach:
1. If BTF is generated by a newer pahole (with pre-sorting support), the kernel would use the pre-sorted data directly.
2. For BTF from older pahole versions, the kernel would handle sorting at load time or later.
Quoted 4 lines
The problem with 2 is extra memory consumption for narrow use case. The "time cat trace" example shows that search is in critical path, but I suspect ftrace can do it differently. I don't know why it's doing the search so much.
Thanks. The reason is that ftrace supports outputting parameters of traced functions through funcgraph-args, like this:
0) | vfs_write(file=0xffff888102b17380,
buf=0x7ffd1e9faaf7, count=0x1, pos=0xffffc90006f83ef0) {
0) | rw_verify_area(read_write=1,
file=0xffff888102b17380, ppos=0xffffc90006f83ef0, count=0x1) {
0) |security_file_permission(file=0xffff888102b17380, mask=2) {
0) |selinux_file_permission(file=0xffff888102b17380, mask=2) {
0) 0.111 us | avc_policy_seqno(); 0) 0.380 us | } 0) 0.585 us | } 0) 0.782 us | }
which requires obtaining function parameter names and types from BTF. However, there is currently no direct mapping from function addresses to btf_type index information. Therefore, it first obtains the function name from the function address, and then searches the BTF file by the function name to get the corresponding btf_type.
Quoted 4 lines
Everyelse in bpf we don't call it that often. So optimizing the search is nice, but not at the expense of so much extra memory.
Hence I don't think 2 is worth doing.
Thanks, I agree.
Quoted 6 lines
Quoted 4 lines
Regarding the pahole changes: this is now my highest priority. I’ve already incorporated it into my development plan and will begin working on the patches shortly.
let's land pahole changes first.
Understood, thanks
Quoted 32 lines · Donglin Peng
Quoted 9 lines · Alexei Starovoitov
Quoted 6 lines · Donglin Peng
I’d like to suggest a dual-mechanism approach:
1. If BTF is generated by a newer pahole (with pre-sorting support), the kernel would use the pre-sorted data directly.
2. For BTF from older pahole versions, the kernel would handle sorting at load time or later.Quoted 4 lines
The problem with 2 is extra memory consumption for narrow use case. The "time cat trace" example shows that search is in critical path, but I suspect ftrace can do it differently. I don't know why it's doing the search so much.
Thanks. The reason is that ftrace supports outputting parameters of traced functions through funcgraph-args, like this:
0) | vfs_write(file=0xffff888102b17380,
buf=0x7ffd1e9faaf7, count=0x1, pos=0xffffc90006f83ef0) {
0) | rw_verify_area(read_write=1,
file=0xffff888102b17380, ppos=0xffffc90006f83ef0, count=0x1) {
0) |security_file_permission(file=0xffff888102b17380, mask=2){
0) |selinux_file_permission(file=0xffff888102b17380, mask=2){0) 0.111 us | avc_policy_seqno(); 0) 0.380 us | } 0) 0.585 us | } 0) 0.782 us | }
Quoted 5 lines
which requires obtaining function parameter names and types from BTF. However, there is currently no direct mapping from function addresses to
btf_typeindex information. Therefore, it first obtains the function name from the function address, and then searches the BTF file by the function name to get the correspondingbtf_type.
The problem here is we have a lookup every time we collect function args, right? Binary search of sorted function names will make that better but it will still be slow if it has to happen every time we dump function args. Would it make sense then perhaps to have a more tailored solution like a cache of BTF type ids for functions that could be mapped directly from kallsyms symbols? Mentioned this before [1] but maybe we could figure something out now?
For example, we have to look up kallsym name for the address via lookup_symbol_name(); it uses get_symbol_pos() internally to find the index within the kallsyms_offsets array. If we had a similar array for kallsyms_btf_ids we could use the same index to populate it with function BTF ids, we could later do O(1) lookup. We would just need a kallsyms lookup that returned the index, or indeed a new API which returned the name and the BTF id (if we added such an index to kallsyms code directly). We could even just populate the entries on first use and then it would function as a cache. We would need a module+btf_id in the index, so 64 bits per entry to support both module and kernel BTF. Seems possible though?
[1]
https://lore.kernel.org/linux-trace-kernel/[email protected]/
Quoted 12 lines · Andrii Nakryiko
Quoted 10 lines · pengdonglin
From: pengdonglin <[email protected]>
Currently, when the funcgraph-args feature is in use, the
btf_find_by_name_kindfunction is invoked quite frequently. However, this function only supports linear search. When the number ofbtf_typeentries to search through is large, such as in the vmlinux BTF which contains over 80,000 namedbtf_types, it consumes a significant amount of time.
Quoted 6 lines
Quoted 3 lines
This patch optimizes the
btf_find_by_name_kindlookup by sorting BTF types according to their names and kinds. Additionally, it modifies the search direction. Now, it first searches the BTF and then its base.Well, the latter is a meaningful change outside of sorting. Split it out and justify separately?
Thanks, I will split it out in v2.
Quoted 43 lines
Quoted 36 lines
It should be noted that this change incurs some additional memory and boot-time overhead. Therefore, the option is disabled by default.
Here is a test case:
# echo 1 > options/funcgraph-args # echo function_graph > current_tracerBefore:
# time cat trace | wc -l 124176real 0m16.154s user 0m0.000s sys 0m15.962sAfter:
# time cat trace | wc -l 124176real 0m0.948s user 0m0.000s sys 0m0.973sAn improvement of more than 20 times can be observed.
Cc Eduard Zingerman <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Song Liu <[email protected]>Cc Masami Hiramatsu (Google) <[email protected]>Cc Steven Rostedt <[email protected]>Signed-off-by pengdonglin <[email protected]>Signed-off-by pengdonglin <[email protected]>Quoted 6 lines
--- include/linux/btf.h | 1 + kernel/bpf/Kconfig | 13 ++++ kernel/bpf/btf.c | 160 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 165 insertions(+), 9 deletions(-)
Quoted 7 lines
Just a few observations (if we decide to do the sorting of BTF by name in the kernel):
- given we always know kind we are searching for, I'd sort by kind, then by name, it probably will be a touch faster because we'll be quickly skipping lots of elements clustered by kind we don't care about;
Good catch, thanks.
Quoted 10 lines
- instead of having
BPF_SORT_BTF_BY_NAME_KIND, we should probably just have a lazy sorting approach, and maybe employ a bit more sophisticated heuristic. E.g., not by number of BTF types (or at least not just by that), but by the total number of entries we had to skip to find something. For small BTFs we might not reach this budget ever. For vmlinux BTF we are almost definitely hitting it on first-second-third search. Once the condition is hit, allocatesorted_idsindex, sort, remember. On subsequent searches use the index.
Thanks, I appreciate the suggestion and will include it in v2. However, due to the
memory overhead, I believe a BPF_SORT_BTF_BY_NAME_KIND option might be necessary.
Quoted 19 lines
WDYT?
[...]
Quoted 14 lines
kernel/bpf/btf.c +14
-0+static void btf_sort_by_name_kind (struct btf *btf) +{ +const struct btf_type *t; +struct btf_sorted_ids *sorted_ids; +const char *name; +u32 *ids; +u32 total, cnt =0 ; +u32 i, j =0 ; + + total =btf_type_cnt (btf); +for (i = btf->start_id; i < total; i++) { + t =btf_type_by_id (btf, i); + name =btf_name_by_offset (btf, t->name_off); +if (str_is_empty (name))
Quoted 8 lines
kernel/bpf/btf.c int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_ty · line 6298
Quoted 6 lines
kernel/bpf/btf.c +6
-0+continue ; + cnt++; + } + +/* Use linear search when the number is below the threshold */ +if (cnt <8 )kind of a random threshold, at least give it a name
Thanks, I will fix it in v2.
Quoted 10 lines
Quoted 8 lines
kernel/bpf/btf.c +7
-0+return ; + + sorted_ids =kvmalloc (struct_size (sorted_ids, ids, cnt),GFP_KERNEL ); +if (!sorted_ids) { +pr_warn ("Failed to allocate memory for sorted_ids\n" ); +return ; + }[...]