[PATCH v3 12/50] perf record: Lazy load kernel symbols

Ian Rogers posted 50 patches 2 years, 2 months ago
There is a newer version of this series
[PATCH v3 12/50] perf record: Lazy load kernel symbols
Posted by Ian Rogers 2 years, 2 months ago
Commit 5b7ba82a7591 ("perf symbols: Load kernel maps before using")
changed it so that loading a kernel dso would cause the symbols for
the dso to be eagerly loaded. For perf record this is overhead as the
symbols won't be used. Add a symbol_conf to control the behavior and
disable it for perf record and perf inject.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-inject.c   | 6 ++++++
 tools/perf/builtin-record.c   | 2 ++
 tools/perf/util/event.c       | 4 ++--
 tools/perf/util/symbol_conf.h | 3 ++-
 4 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index c8cf2fdd9cff..eb3ef5c24b66 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -2265,6 +2265,12 @@ int cmd_inject(int argc, const char **argv)
 		"perf inject [<options>]",
 		NULL
 	};
+
+	if (!inject.itrace_synth_opts.set) {
+		/* Disable eager loading of kernel symbols that adds overhead to perf inject. */
+		symbol_conf.lazy_load_kernel_maps = true;
+	}
+
 #ifndef HAVE_JITDUMP
 	set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
 #endif
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index dcf288a4fb9a..8ec818568662 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -3989,6 +3989,8 @@ int cmd_record(int argc, const char **argv)
 # undef set_nobuild
 #endif
 
+	/* Disable eager loading of kernel symbols that adds overhead to perf record. */
+	symbol_conf.lazy_load_kernel_maps = true;
 	rec->opts.affinity = PERF_AFFINITY_SYS;
 
 	rec->evlist = evlist__new();
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index 923c0fb15122..68f45e9e63b6 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -617,13 +617,13 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
 	if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
 		al->level = 'k';
 		maps = machine__kernel_maps(machine);
-		load_map = true;
+		load_map = !symbol_conf.lazy_load_kernel_maps;
 	} else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
 		al->level = '.';
 	} else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
 		al->level = 'g';
 		maps = machine__kernel_maps(machine);
-		load_map = true;
+		load_map = !symbol_conf.lazy_load_kernel_maps;
 	} else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
 		al->level = 'u';
 	} else {
diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h
index 0b589570d1d0..2b2fb9e224b0 100644
--- a/tools/perf/util/symbol_conf.h
+++ b/tools/perf/util/symbol_conf.h
@@ -42,7 +42,8 @@ struct symbol_conf {
 			inline_name,
 			disable_add2line_warn,
 			buildid_mmap2,
-			guest_code;
+			guest_code,
+			lazy_load_kernel_maps;
 	const char	*vmlinux_name,
 			*kallsyms_name,
 			*source_prefix,
-- 
2.42.0.758.gaed0368e0e-goog
Re: [PATCH v3 12/50] perf record: Lazy load kernel symbols
Posted by Namhyung Kim 2 years, 2 months ago
Hi Ian,

On Tue, Oct 24, 2023 at 3:24 PM Ian Rogers <irogers@google.com> wrote:
>
> Commit 5b7ba82a7591 ("perf symbols: Load kernel maps before using")
> changed it so that loading a kernel dso would cause the symbols for
> the dso to be eagerly loaded. For perf record this is overhead as the
> symbols won't be used. Add a symbol_conf to control the behavior and
> disable it for perf record and perf inject.

I'm curious if it can simply move to lazy loading unconditionally.
In most cases, the code calls machine__resolve() which calls
thread__find_map() and map__find_symbol() to load symbols.

So I think it's unnecessary to do it in the thread__find_map().
If it needs a symbol, it should call map__find_symbol() first
and it'll load the symbol table.

Adrian, what's special in inject or Intel-PT on this?

Thanks,
Namhyung


>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/builtin-inject.c   | 6 ++++++
>  tools/perf/builtin-record.c   | 2 ++
>  tools/perf/util/event.c       | 4 ++--
>  tools/perf/util/symbol_conf.h | 3 ++-
>  4 files changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
> index c8cf2fdd9cff..eb3ef5c24b66 100644
> --- a/tools/perf/builtin-inject.c
> +++ b/tools/perf/builtin-inject.c
> @@ -2265,6 +2265,12 @@ int cmd_inject(int argc, const char **argv)
>                 "perf inject [<options>]",
>                 NULL
>         };
> +
> +       if (!inject.itrace_synth_opts.set) {
> +               /* Disable eager loading of kernel symbols that adds overhead to perf inject. */
> +               symbol_conf.lazy_load_kernel_maps = true;
> +       }
> +
>  #ifndef HAVE_JITDUMP
>         set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
>  #endif
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index dcf288a4fb9a..8ec818568662 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -3989,6 +3989,8 @@ int cmd_record(int argc, const char **argv)
>  # undef set_nobuild
>  #endif
>
> +       /* Disable eager loading of kernel symbols that adds overhead to perf record. */
> +       symbol_conf.lazy_load_kernel_maps = true;
>         rec->opts.affinity = PERF_AFFINITY_SYS;
>
>         rec->evlist = evlist__new();
> diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
> index 923c0fb15122..68f45e9e63b6 100644
> --- a/tools/perf/util/event.c
> +++ b/tools/perf/util/event.c
> @@ -617,13 +617,13 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
>         if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
>                 al->level = 'k';
>                 maps = machine__kernel_maps(machine);
> -               load_map = true;
> +               load_map = !symbol_conf.lazy_load_kernel_maps;
>         } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
>                 al->level = '.';
>         } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
>                 al->level = 'g';
>                 maps = machine__kernel_maps(machine);
> -               load_map = true;
> +               load_map = !symbol_conf.lazy_load_kernel_maps;
>         } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
>                 al->level = 'u';
>         } else {
> diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h
> index 0b589570d1d0..2b2fb9e224b0 100644
> --- a/tools/perf/util/symbol_conf.h
> +++ b/tools/perf/util/symbol_conf.h
> @@ -42,7 +42,8 @@ struct symbol_conf {
>                         inline_name,
>                         disable_add2line_warn,
>                         buildid_mmap2,
> -                       guest_code;
> +                       guest_code,
> +                       lazy_load_kernel_maps;
>         const char      *vmlinux_name,
>                         *kallsyms_name,
>                         *source_prefix,
> --
> 2.42.0.758.gaed0368e0e-goog
>
Re: [PATCH v3 12/50] perf record: Lazy load kernel symbols
Posted by Adrian Hunter 2 years, 2 months ago
On 25/10/23 21:25, Namhyung Kim wrote:
> Hi Ian,
> 
> On Tue, Oct 24, 2023 at 3:24 PM Ian Rogers <irogers@google.com> wrote:
>>
>> Commit 5b7ba82a7591 ("perf symbols: Load kernel maps before using")
>> changed it so that loading a kernel dso would cause the symbols for
>> the dso to be eagerly loaded. For perf record this is overhead as the
>> symbols won't be used. Add a symbol_conf to control the behavior and
>> disable it for perf record and perf inject.
> 
> I'm curious if it can simply move to lazy loading unconditionally.
> In most cases, the code calls machine__resolve() which calls
> thread__find_map() and map__find_symbol() to load symbols.
> 
> So I think it's unnecessary to do it in the thread__find_map().
> If it needs a symbol, it should call map__find_symbol() first
> and it'll load the symbol table.
> 
> Adrian, what's special in inject or Intel-PT on this?

Was a long time ago.  Apart from what the commit says below,
I think there might also be other changes to the kernel maps
that happen at loading, but I would have to have a look.

commit 5b7ba82a75915e739709d0ace4bb559cb280db09
Author: Adrian Hunter <adrian.hunter@intel.com>
Date:   Wed Aug 7 14:38:46 2013 +0300

    perf symbols: Load kernel maps before using
    
    In order to use kernel maps to read object code, those maps must be
    adjusted to map to the dso file offset.  Because lazy-initialization is
    used, that is not done until symbols are loaded.  However the maps are
    first used by thread__find_addr_map() before symbols are loaded.  So
    this patch changes thread__find_addr() to "load" kernel maps before
    using them.



> 
> Thanks,
> Namhyung
> 
> 
>>
>> Signed-off-by: Ian Rogers <irogers@google.com>
>> ---
>>  tools/perf/builtin-inject.c   | 6 ++++++
>>  tools/perf/builtin-record.c   | 2 ++
>>  tools/perf/util/event.c       | 4 ++--
>>  tools/perf/util/symbol_conf.h | 3 ++-
>>  4 files changed, 12 insertions(+), 3 deletions(-)
>>
>> diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
>> index c8cf2fdd9cff..eb3ef5c24b66 100644
>> --- a/tools/perf/builtin-inject.c
>> +++ b/tools/perf/builtin-inject.c
>> @@ -2265,6 +2265,12 @@ int cmd_inject(int argc, const char **argv)
>>                 "perf inject [<options>]",
>>                 NULL
>>         };
>> +
>> +       if (!inject.itrace_synth_opts.set) {
>> +               /* Disable eager loading of kernel symbols that adds overhead to perf inject. */
>> +               symbol_conf.lazy_load_kernel_maps = true;
>> +       }
>> +
>>  #ifndef HAVE_JITDUMP
>>         set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
>>  #endif
>> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
>> index dcf288a4fb9a..8ec818568662 100644
>> --- a/tools/perf/builtin-record.c
>> +++ b/tools/perf/builtin-record.c
>> @@ -3989,6 +3989,8 @@ int cmd_record(int argc, const char **argv)
>>  # undef set_nobuild
>>  #endif
>>
>> +       /* Disable eager loading of kernel symbols that adds overhead to perf record. */
>> +       symbol_conf.lazy_load_kernel_maps = true;
>>         rec->opts.affinity = PERF_AFFINITY_SYS;
>>
>>         rec->evlist = evlist__new();
>> diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
>> index 923c0fb15122..68f45e9e63b6 100644
>> --- a/tools/perf/util/event.c
>> +++ b/tools/perf/util/event.c
>> @@ -617,13 +617,13 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
>>         if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
>>                 al->level = 'k';
>>                 maps = machine__kernel_maps(machine);
>> -               load_map = true;
>> +               load_map = !symbol_conf.lazy_load_kernel_maps;
>>         } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
>>                 al->level = '.';
>>         } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
>>                 al->level = 'g';
>>                 maps = machine__kernel_maps(machine);
>> -               load_map = true;
>> +               load_map = !symbol_conf.lazy_load_kernel_maps;
>>         } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
>>                 al->level = 'u';
>>         } else {
>> diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h
>> index 0b589570d1d0..2b2fb9e224b0 100644
>> --- a/tools/perf/util/symbol_conf.h
>> +++ b/tools/perf/util/symbol_conf.h
>> @@ -42,7 +42,8 @@ struct symbol_conf {
>>                         inline_name,
>>                         disable_add2line_warn,
>>                         buildid_mmap2,
>> -                       guest_code;
>> +                       guest_code,
>> +                       lazy_load_kernel_maps;
>>         const char      *vmlinux_name,
>>                         *kallsyms_name,
>>                         *source_prefix,
>> --
>> 2.42.0.758.gaed0368e0e-goog
>>