perf tools: Add deferred callchain support
Subsystems (3)
Namhyung Kim
maintainerv5 cover perf tools: Add deferred callchain support
Hello,
This is a new version of deferred callchain support as the kernel part is merged to the tip tree. Actually this is based on Steve's work (v16).
https://lore.kernel.org/r/[email protected]
v5 changes)
* update delegate tools (Ian)
* copy and flush remaining samples (Ian)
* add Ian's Reviewed-by tags
v4: https://lore.kernel.org/r/[email protected]
* add --call-graph fp,defer option (Ian, Steve)
* add more comment on the cookie (Ian)
* display cookie part in the deferred callchain (Ian)
v3: https://lore.kernel.org/r/[email protected]
* handle new attr.defer_output to generate deferred callchains
* fix crash when cookies don't match (Steven)
* disable merging for perf inject
* fix missing feature detection bug
* symbolize merged callchains properly
Here's an example session.
$ perf record --call-graph fp,defer pwd /home/namhyung/project/linux [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.010 MB perf.data (29 samples) ]
$ perf evlist -v
cpu/cycles/P: type: 0 (PERF_TYPE_HARDWARE), size: 136, config: 0 (PERF_COUNT_HW_CPU_CYCLES),
{ sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|CALLCHAIN|PERIOD,
read_format: ID|LOST, disabled: 1, inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1,
task: 1, sample_id_all: 1, mmap2: 1, comm_exec: 1, ksymbol: 1, bpf_event: 1, build_id: 1,
defer_callchain: 1, defer_output: 1 $ perf script
...
pwd 2312 121.163435: 249113 cpu/cycles/P:
ffffffff845b78d8 __build_id_parse.isra.0+0x218 ([kernel.kallsyms])
ffffffff83bb5bf6 perf_event_mmap+0x2e6 ([kernel.kallsyms])
ffffffff83c31959 mprotect_fixup+0x1e9 ([kernel.kallsyms])
ffffffff83c31dc5 do_mprotect_pkey+0x2b5 ([kernel.kallsyms])
ffffffff83c3206f __x64_sys_mprotect+0x1f ([kernel.kallsyms])
ffffffff845e6692 do_syscall_64+0x62 ([kernel.kallsyms])
ffffffff8360012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms])
7f18fe337fa7 mprotect+0x7 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe330e0f _dl_sysdep_start+0x7f (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe331448 _dl_start_user+0x0 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
... $ perf script --no-merge-callchains
...
pwd 2312 121.163435: 249113 cpu/cycles/P:
ffffffff845b78d8 __build_id_parse.isra.0+0x218 ([kernel.kallsyms])
ffffffff83bb5bf6 perf_event_mmap+0x2e6 ([kernel.kallsyms])
ffffffff83c31959 mprotect_fixup+0x1e9 ([kernel.kallsyms])
ffffffff83c31dc5 do_mprotect_pkey+0x2b5 ([kernel.kallsyms])
ffffffff83c3206f __x64_sys_mprotect+0x1f ([kernel.kallsyms])
ffffffff845e6692 do_syscall_64+0x62 ([kernel.kallsyms])
ffffffff8360012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms])
b00000006 (cookie) ([unknown]) pwd 2312 121.163447: DEFERRED CALLCHAIN [cookie: b00000006]
7f18fe337fa7 mprotect+0x7 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe330e0f _dl_sysdep_start+0x7f (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe331448 _dl_start_user+0x0 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)...
The code is available at 'perf/defer-callchain-v5' branch in
git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git
Thanks,
Namhyung
Namhyung Kim (6):
tools headers UAPI: Sync linux/perf_event.h for deferred callchains perf tools: Minimal DEFERRED_CALLCHAIN support
perf record: Add --call-graph fp,defer option for deferred callchains perf script: Display PERF_RECORD_CALLCHAIN_DEFERRED
perf tools: Merge deferred user callchains
perf tools: Flush remaining samples w/o deferred callchains
tools/include/uapi/linux/perf_event.h | 21 ++- tools/lib/perf/include/perf/event.h | 13 ++ tools/perf/Documentation/perf-config.txt | 3 + tools/perf/Documentation/perf-record.txt | 4 + tools/perf/Documentation/perf-script.txt | 5 + tools/perf/builtin-inject.c | 1 + tools/perf/builtin-report.c | 1 + tools/perf/builtin-script.c | 93 +++++++++++ tools/perf/util/callchain.c | 45 +++++- tools/perf/util/callchain.h | 4 + tools/perf/util/event.c | 1 + tools/perf/util/evlist.c | 1 + tools/perf/util/evlist.h | 2 + tools/perf/util/evsel.c | 50 +++++- tools/perf/util/evsel.h | 1 + tools/perf/util/evsel_fprintf.c | 5 +- tools/perf/util/machine.c | 1 + tools/perf/util/perf_event_attr_fprintf.c | 2 + tools/perf/util/sample.h | 2 + tools/perf/util/session.c | 183 ++++++++++++++++++++++ tools/perf/util/tool.c | 5 + tools/perf/util/tool.h | 4 +- 22 files changed, 439 insertions(+), 8 deletions(-)
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Namhyung Kim
maintainerPATCH v5 1/6 tools headers UAPI: Sync linux/perf_event.h for deferred callchains
It needs to sync with the kernel to support user space changes for the deferred callchains.
--- tools/include/uapi/linux/perf_event.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
tools/include/uapi/linux/perf_event.h +20 -1
@@ -463,7 +463,9 @@ struct perf_event_attr { inherit_thread : 1, /* children only inherit if cloned with CLONE_THREAD */ remove_on_exec : 1, /* event is removed from task on exec */ sigtrap : 1, /* send synchronous SIGTRAP on event */ - __reserved_1 : 26; + defer_callchain: 1, /* request PERF_RECORD_CALLCHAIN_DEFERRED records */ + defer_output : 1, /* output PERF_RECORD_CALLCHAIN_DEFERRED records */ + __reserved_1 : 24;union {__u32 wakeup_events;/* wake up every n events */
@@ -1239,6 +1241,22 @@ enum perf_event_type {*/ PERF_RECORD_AUX_OUTPUT_HW_ID =21 , +/* +* This user callchain capture was deferred until shortly before +* returning to user space. Previous samples would have kernel +* callchains only and they need to be stitched with this to make full +* callchains. +* +* struct { +* struct perf_event_header header; +* u64 cookie; +* u64 nr; +* u64 ips[nr]; +* struct sample_id sample_id; +* }; +*/ +PERF_RECORD_CALLCHAIN_DEFERRED =22 , + PERF_RECORD_MAX, /* non-ABI */ };
@@ -1269,6 +1287,7 @@ enum perf_callchain_context {PERF_CONTEXT_HV = (__u64)-32 ,PERF_CONTEXT_KERNEL = (__u64)-128 ,PERF_CONTEXT_USER = (__u64)-512 , +PERF_CONTEXT_USER_DEFERRED = (__u64)-640 ,PERF_CONTEXT_GUEST = (__u64)-2048 , PERF_CONTEXT_GUEST_KERNEL = (__u64)-2176,
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Namhyung Kim
maintainerPATCH v5 2/6 perf tools: Minimal DEFERRED_CALLCHAIN support
In reply to Namhyung KimAdd a new event type for deferred callchains and a new callback for the struct perf_tool. For now it doesn't actually handle the deferred callchains but it just marks the sample if it has the PERF_CONTEXT_ USER_DEFFERED in the callchain array.
At least, perf report can dump the raw data with this change. Actually this requires the next commit to enable attr.defer_callchain, but if you already have a data file, it'll show the following result.
$ perf report -D ... [email protected] [0x40]: event: 22 . . ... raw event: size 64 bytes . 0000: 16 00 00 00 02 00 40 00 06 00 00 00 0b 00 00 00 ......@......... . 0010: 03 00 00 00 00 00 00 00 a7 7f 33 fe 18 7f 00 00 ..........3..... . 0020: 0f 0e 33 fe 18 7f 00 00 48 14 33 fe 18 7f 00 00 ..3.....H.3..... . 0030: 08 09 00 00 08 09 00 00 e6 7a e7 35 1c 00 00 00 .........z.5....
121163447014 0x2158 [0x40]: PERF_RECORD_CALLCHAIN_DEFERRED(IP, 0x2): 2312/2312: 0xb00000006 ... FP chain: nr:3
..... 0: 00007f18fe337fa7 ..... 1: 00007f18fe330e0f ..... 2: 00007f18fe331448
: unhandled!
--- tools/lib/perf/include/perf/event.h | 13 ++++++++++ tools/perf/util/event.c | 1 + tools/perf/util/evsel.c | 31 +++++++++++++++++++++-- tools/perf/util/machine.c | 1 + tools/perf/util/perf_event_attr_fprintf.c | 2 ++ tools/perf/util/sample.h | 2 ++ tools/perf/util/session.c | 20 +++++++++++++++ tools/perf/util/tool.c | 3 +++ tools/perf/util/tool.h | 3 ++- 9 files changed, 73 insertions(+), 3 deletions(-)
tools/lib/perf/include/perf/event.h +13 -0
@@ -151,6 +151,18 @@ struct perf_record_switch {__u32 next_prev_tid; }; +struct perf_record_callchain_deferred { +struct perf_event_header header; +/* +* This is to match kernel and (deferred) user stacks together. +* The kernel part will be in the sample callchain array after +* the PERF_CONTEXT_USER_DEFERRED entry. +*/ +__u64 cookie; +__u64 nr; +__u64 ips[]; +}; +struct perf_record_header_attr {struct perf_event_header header;struct perf_event_attr attr;
@@ -523,6 +535,7 @@ union perf_event {struct perf_record_read read;struct perf_record_throttle throttle;struct perf_record_sample sample; +struct perf_record_callchain_deferred callchain_deferred;struct perf_record_bpf_event bpf;struct perf_record_ksymbol ksymbol;struct perf_record_text_poke_event text_poke;
tools/perf/util/event.c +1 -0
@@ -61,6 +61,7 @@ static const char *perf_event__names[] = { [PERF_RECORD_CGROUP] = "CGROUP", [PERF_RECORD_TEXT_POKE] = "TEXT_POKE", [PERF_RECORD_AUX_OUTPUT_HW_ID] = "AUX_OUTPUT_HW_ID", + [PERF_RECORD_CALLCHAIN_DEFERRED] = "CALLCHAIN_DEFERRED", [PERF_RECORD_HEADER_ATTR] = "ATTR", [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
tools/perf/util/evsel.c +29 -2
@@ -3089,6 +3089,20 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event, data->data_src =PERF_MEM_DATA_SRC_NONE ; data->vcpu =-1 ; +if (event->header.type ==PERF_RECORD_CALLCHAIN_DEFERRED ) { +const u64 max_callchain_nr =UINT64_MAX /sizeof (u64); + + data->callchain = (struct ip_callchain *)&event->callchain_deferred.nr; +if (data->callchain->nr > max_callchain_nr) +return -EFAULT ; + + data->deferred_cookie = event->callchain_deferred.cookie; + +if (evsel->core.attr.sample_id_all) +perf_evsel__parse_id_sample (evsel, event, data); +return 0 ; + } +if (event->header.type !=PERF_RECORD_SAMPLE ) {if (!evsel->core.attr.sample_id_all)return 0 ;
@@ -3213,12 +3227,25 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event,if (type &PERF_SAMPLE_CALLCHAIN ) {const u64 max_callchain_nr =UINT64_MAX /sizeof (u64); +u64 callchain_nr;OVERFLOW_CHECK_u64 (array); data->callchain = (struct ip_callchain *)array++; -if (data->callchain->nr > max_callchain_nr) + callchain_nr = data->callchain->nr; +if (callchain_nr > max_callchain_nr)return -EFAULT ; - sz = data->callchain->nr *sizeof (u64); + sz = callchain_nr *sizeof (u64); +/* +* Save the cookie for the deferred user callchain. The last 2 +* entries in the callchain should be the context marker and the +* cookie. The cookie will be used to match PERF_RECORD_ +* CALLCHAIN_DEFERRED later. +*/ +if (evsel->core.attr.defer_callchain && callchain_nr >=2 && + data->callchain->ips[callchain_nr -2 ] ==PERF_CONTEXT_USER_DEFERRED ) { + data->deferred_cookie = data->callchain->ips[callchain_nr -1 ]; + data->deferred_callchain = true; + }OVERFLOW_CHECK (array, sz, max_size); array = (void *)array + sz; }
tools/perf/util/machine.c +1 -0
@@ -2124,6 +2124,7 @@ static int add_callchain_ip(struct thread *thread, *cpumode =PERF_RECORD_MISC_KERNEL ;break ;case PERF_CONTEXT_USER : +case PERF_CONTEXT_USER_DEFERRED : *cpumode =PERF_RECORD_MISC_USER ;break ;default :
tools/perf/util/perf_event_attr_fprintf.c +2 -0
@@ -343,6 +343,8 @@ int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,PRINT_ATTRf (inherit_thread, p_unsigned);PRINT_ATTRf (remove_on_exec, p_unsigned);PRINT_ATTRf (sigtrap, p_unsigned); +PRINT_ATTRf (defer_callchain, p_unsigned); +PRINT_ATTRf (defer_output, p_unsigned);PRINT_ATTRn ("{ wakeup_events, wakeup_watermark }" , wakeup_events, p_unsigned, false);PRINT_ATTRf (bp_type, p_unsigned);
tools/perf/util/sample.h +2 -0
@@ -107,6 +107,8 @@ struct perf_sample {/** @weight3: On x86 holds retire_lat, on powerpc holds p_stage_cyc. */ u16 weight3;bool no_hw_idx;/* No hw_idx collected in branch_stack */ +bool deferred_callchain;/* Has deferred user callchains */ +u64 deferred_cookie;char insn[MAX_INSN ];void *raw_data;struct ip_callchain *callchain;
tools/perf/util/session.c +20 -0
@@ -720,6 +720,7 @@ static perf_event__swap_op perf_event__swap_ops[] = { [PERF_RECORD_CGROUP] = perf_event__cgroup_swap, [PERF_RECORD_TEXT_POKE] = perf_event__text_poke_swap, [PERF_RECORD_AUX_OUTPUT_HW_ID] = perf_event__all64_swap, + [PERF_RECORD_CALLCHAIN_DEFERRED] = perf_event__all64_swap, [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
@@ -854,6 +855,9 @@ static void callchain__printf(struct evsel *evsel,for (i =0 ; i < callchain->nr; i++)printf ("..... %2d: %016" PRIx64"\n" , i, callchain->ips[i]); + +if (sample->deferred_callchain) +printf ("...... (deferred)\n" ); } static void branch_stack__printf(struct perf_sample *sample,
@@ -1123,6 +1127,19 @@ static void dump_sample(struct evsel *evsel, union perf_event *event,sample_read__printf (sample, evsel->core.attr.read_format); } +static void dump_deferred_callchain (struct evsel *evsel,union perf_event *event, +struct perf_sample *sample) +{ +if (!dump_trace) +return ; + +printf ("(IP, 0x%x): %d/%d: %#" PRIx64"\n" , + event->header.misc, sample->pid, sample->tid, sample->deferred_cookie); + +if (evsel__has_callchain (evsel)) +callchain__printf (evsel, sample); +} +static void dump_read (struct evsel *evsel,union perf_event *event) {struct perf_record_read *read_event = &event->read;
@@ -1353,6 +1370,9 @@ static int machines__deliver_event(struct machines *machines,return tool->text_poke (tool, event, sample, machine);case PERF_RECORD_AUX_OUTPUT_HW_ID :return tool->aux_output_hw_id (tool, event, sample, machine); +case PERF_RECORD_CALLCHAIN_DEFERRED : +dump_deferred_callchain (evsel, event, sample); +return tool->callchain_deferred (tool, event, sample, evsel, machine);default : ++evlist->stats.nr_unknown_events;return -1 ;
tools/perf/util/tool.c +3 -0
@@ -287,6 +287,7 @@ void perf_tool__init(struct perf_tool *tool, bool ordered_events) tool->read = process_event_sample_stub; tool->throttle = process_event_stub; tool->unthrottle = process_event_stub; + tool->callchain_deferred = process_event_sample_stub; tool->attr = process_event_synth_attr_stub; tool->event_update = process_event_synth_event_update_stub; tool->tracing_data = process_event_synth_tracing_data_stub;
@@ -335,6 +336,7 @@ bool perf_tool__compressed_is_stub(const struct perf_tool *tool) }CREATE_DELEGATE_SAMPLE (read);CREATE_DELEGATE_SAMPLE (sample); +CREATE_DELEGATE_SAMPLE (callchain_deferred);#define CREATE_DELEGATE_ATTR (name) \ static int delegate_ ## name(const struct perf_tool *tool, \
@@ -468,6 +470,7 @@ void delegate_tool__init(struct delegate_tool *tool, struct perf_tool *delegate) tool->tool.ksymbol = delegate_ksymbol; tool->tool.bpf = delegate_bpf; tool->tool.text_poke = delegate_text_poke; + tool->tool.callchain_deferred = delegate_callchain_deferred; tool->tool.attr = delegate_attr; tool->tool.event_update = delegate_event_update;
tools/perf/util/tool.h +2 -1
@@ -44,7 +44,8 @@ enum show_feature_header {struct perf_tool {event_sample sample, - read; + read, + callchain_deferred;event_op mmap, mmap2, comm,
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Namhyung Kim
maintainerPATCH v5 3/6 perf record: Add --call-graph fp,defer option for deferred callchains
In reply to Namhyung KimAdd a new callchain record mode option for deferred callchains. For now it only works with FP (frame-pointer) mode.
And add the missing feature detection logic to clear the flag on old kernels.
$ perf record --call-graph fp,defer -vv true
...
------------------------------------------------------------
perf_event_attr:
type 0 (PERF_TYPE_HARDWARE)
size 136
config 0 (PERF_COUNT_HW_CPU_CYCLES)
{ sample_period, sample_freq } 4000
sample_type IP|TID|TIME|CALLCHAIN|PERIOD
read_format ID|LOST
disabled 1
inherit 1
mmap 1
comm 1
freq 1
enable_on_exec 1
task 1
sample_id_all 1
mmap2 1
comm_exec 1
ksymbol 1
bpf_event 1
defer_callchain 1
defer_output 1
------------------------------------------------------------
sys_perf_event_open: pid 162755 cpu 0 group_fd -1 flags 0x8
sys_perf_event_open failed, error -22
switching off deferred callchain support--- tools/perf/Documentation/perf-config.txt | 3 +++ tools/perf/Documentation/perf-record.txt | 4 ++++ tools/perf/util/callchain.c | 16 +++++++++++++--- tools/perf/util/callchain.h | 1 + tools/perf/util/evsel.c | 19 +++++++++++++++++++ tools/perf/util/evsel.h | 1 + 6 files changed, 41 insertions(+), 3 deletions(-)
tools/perf/Documentation/perf-config.txt +3 -0
@@ -452,6 +452,9 @@ Variables kernel space is controlled not by this option but by the kernel config (CONFIG_UNWINDER_*). + The 'defer' mode can be used with 'fp' mode to enable deferred + user callchains (like 'fp,defer'). + call-graph.dump-size:: The size of stack to dump in order to do post-unwinding. Default is 8192 (byte). When using dwarf into record-mode, the default size will be used if omitted.
tools/perf/Documentation/perf-record.txt +4 -0
@@ -325,6 +325,10 @@ OPTIONS by default. User can change the number by passing it after comma like "--call-graph fp,32". + Also "defer" can be used with "fp" (like "--call-graph fp,defer") to + enable deferred user callchain which will collect user-space callchains + when the thread returns to the user space. + -q:: --quiet:: Don't print any warnings or messages, useful for scripting.
tools/perf/util/callchain.c +13 -3
@@ -275,9 +275,13 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)if (tok) {unsigned long size; - size =strtoul (tok, &name,0 ); -if (size < (unsigned )sysctl__max_stack ()) - param->max_stack = size; +if (!strncmp (tok,"defer" ,sizeof ("defer" ))) { + param->defer = true; + }else { + size =strtoul (tok, &name,0 ); +if (size < (unsigned )sysctl__max_stack ()) + param->max_stack = size; + } }break ;
@@ -314,6 +318,12 @@ int parse_callchain_record(const char *arg, struct callchain_param *param) }while (0 );free (buf); + +if (param->defer && param->record_mode !=CALLCHAIN_FP ) { +pr_err ("callchain: deferred callchain only works with FP\n" ); +return -EINVAL ; + } +return ret; }
tools/perf/util/callchain.h +1 -0
@@ -98,6 +98,7 @@ extern bool dwarf_callchain_users;struct callchain_param {bool enabled; +bool defer;enum perf_call_graph_mode record_mode;u32 dump_size;enum chain_mode mode;
tools/perf/util/evsel.c +19 -0
@@ -1065,6 +1065,9 @@ static void __evsel__config_callchain(struct evsel *evsel, struct record_opts *opr_info ("Disabling user space callchains for function trace event.\n" ); attr->exclude_callchain_user =1 ; } + +if (param->defer && !attr->exclude_callchain_user) + attr->defer_callchain =1 ; } void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
@@ -1511,6 +1514,7 @@ void evsel__config(struct evsel *evsel, struct record_opts *opts, attr->mmap2 = track && !perf_missing_features.mmap2; attr->comm = track; attr->build_id = track && opts->build_id; + attr->defer_output = track && callchain->defer;/* * ksymbol is tracked separately with text poke because it needs to be
@@ -2199,6 +2203,10 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,static void evsel__disable_missing_features (struct evsel *evsel) { +if (perf_missing_features.defer_callchain && evsel->core.attr.defer_callchain) + evsel->core.attr.defer_callchain =0 ; +if (perf_missing_features.defer_callchain && evsel->core.attr.defer_output) + evsel->core.attr.defer_output =0 ;if (perf_missing_features.inherit_sample_read && evsel->core.attr.inherit && (evsel->core.attr.sample_type &PERF_SAMPLE_READ )) evsel->core.attr.inherit =0 ;
@@ -2473,6 +2481,13 @@ static bool evsel__detect_missing_features(struct evsel *evsel, struct perf_cpu/* Please add new feature detection here. */ + attr.defer_callchain = true; +if (has_attr_feature (&attr,/*flags=*/ 0 )) + goto found; + perf_missing_features.defer_callchain = true; +pr_debug2 ("switching off deferred callchain support\n" ); + attr.defer_callchain = false; + attr.inherit = true; attr.sample_type =PERF_SAMPLE_READ |PERF_SAMPLE_TID ;if (has_attr_feature (&attr,/*flags=*/ 0 ))
@@ -2584,6 +2599,10 @@ static bool evsel__detect_missing_features(struct evsel *evsel, struct perf_cpu errno = old_errno; check: +if ((evsel->core.attr.defer_callchain || evsel->core.attr.defer_output) && + perf_missing_features.defer_callchain) +return true; +if (evsel->core.attr.inherit && (evsel->core.attr.sample_type &PERF_SAMPLE_READ ) && perf_missing_features.inherit_sample_read)
tools/perf/util/evsel.h +1 -0
@@ -221,6 +221,7 @@ struct perf_missing_features {bool branch_counters;bool aux_action;bool inherit_sample_read; +bool defer_callchain; };extern struct perf_missing_features perf_missing_features;
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Namhyung Kim
maintainerPATCH v5 4/6 perf script: Display PERF_RECORD_CALLCHAIN_DEFERRED
In reply to Namhyung KimHandle the deferred callchains in the script output.
$ perf script
...
pwd 2312 121.163435: 249113 cpu/cycles/P:
ffffffff845b78d8 __build_id_parse.isra.0+0x218 ([kernel.kallsyms])
ffffffff83bb5bf6 perf_event_mmap+0x2e6 ([kernel.kallsyms])
ffffffff83c31959 mprotect_fixup+0x1e9 ([kernel.kallsyms])
ffffffff83c31dc5 do_mprotect_pkey+0x2b5 ([kernel.kallsyms])
ffffffff83c3206f __x64_sys_mprotect+0x1f ([kernel.kallsyms])
ffffffff845e6692 do_syscall_64+0x62 ([kernel.kallsyms])
ffffffff8360012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms])
b00000006 (cookie) ([unknown]) pwd 2312 121.163447: DEFERRED CALLCHAIN [cookie: b00000006]
7f18fe337fa7 mprotect+0x7 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe330e0f _dl_sysdep_start+0x7f (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe331448 _dl_start_user+0x0 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)--- tools/perf/builtin-script.c | 89 +++++++++++++++++++++++++++++++++ tools/perf/util/evsel_fprintf.c | 5 +- 2 files changed, 93 insertions(+), 1 deletion(-)
tools/perf/builtin-script.c +89 -0
@@ -2706,6 +2706,94 @@ static int process_sample_event(const struct perf_tool *tool,return ret; } +static int process_deferred_sample_event (const struct perf_tool *tool, +union perf_event *event, +struct perf_sample *sample, +struct evsel *evsel, +struct machine *machine) +{ + struct perf_script *scr = container_of(tool, struct perf_script, tool); +struct perf_event_attr *attr = &evsel->core.attr; +struct evsel_script *es = evsel->priv; +unsigned int type =output_type (attr->type); +struct addr_location al; +FILE *fp = es->fp; +int ret =0 ; + +if (output[type].fields ==0 ) +return 0 ; + +/* Set thread to NULL to indicate addr_al and al are not initialized */ +addr_location__init (&al); + +if (perf_time__ranges_skip_sample (scr->ptime_range, scr->range_num, + sample->time)) { + goto out_put; + } + +if (debug_mode) { +if (sample->time < last_timestamp) { +pr_err ("Samples misordered, previous: %" PRIu64 +" this: %" PRIu64"\n" , last_timestamp, + sample->time); + nr_unordered++; + } + last_timestamp = sample->time; + goto out_put; + } + +if (filter_cpu (sample)) + goto out_put; + +if (machine__resolve (machine, &al, sample) <0 ) { +pr_err ("problem processing %d event, skipping it.\n" , + event->header.type); + ret =-1 ; + goto out_put; + } + +if (al.filtered) + goto out_put; + +if (!show_event (sample, evsel, al.thread, &al,NULL )) + goto out_put; + +if (evswitch__discard (&scr->evswitch, evsel)) + goto out_put; + +perf_sample__fprintf_start (scr, sample, al.thread, evsel, +PERF_RECORD_CALLCHAIN_DEFERRED , fp); +fprintf (fp,"DEFERRED CALLCHAIN [cookie: %llx]" , + (unsigned long long )event->callchain_deferred.cookie); + +if (PRINT_FIELD (IP )) { +struct callchain_cursor *cursor =NULL ; + +if (symbol_conf.use_callchain && sample->callchain) { + cursor =get_tls_callchain_cursor (); +if (thread__resolve_callchain (al.thread, cursor, evsel, + sample,NULL ,NULL , + scripting_max_stack)) { +pr_info ("cannot resolve deferred callchains\n" ); + cursor =NULL ; + } + } + +fputc (cursor ?'\n' :' ' , fp); +sample__fprintf_sym (sample, &al,0 , output[type].print_ip_opts, + cursor, symbol_conf.bt_stop_list, fp); + } + +fprintf (fp,"\n" ); + +if (verbose >0 ) +fflush (fp); + +out_put: +addr_location__exit (&al); +return ret; +} +// Used when scr->per_event_dump is not set static struct evsel_script es_stdout;
@@ -4303,6 +4391,7 @@ int cmd_script(int argc, const char **argv)perf_tool__init (&script.tool, !unsorted_dump); script.tool.sample = process_sample_event; + script.tool.callchain_deferred = process_deferred_sample_event; script.tool.mmap = perf_event__process_mmap; script.tool.mmap2 = perf_event__process_mmap2; script.tool.comm = perf_event__process_comm;
tools/perf/util/evsel_fprintf.c +4 -1
@@ -168,7 +168,10 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, node_al.addr = addr; node_al.map =map__get (map); -if (print_symoffset) { +if (sample->deferred_callchain && + sample->deferred_cookie == node->ip) { + printed +=fprintf (fp,"(cookie)" ); + }else if (print_symoffset) { printed +=__symbol__fprintf_symname_offs (sym, &node_al, print_unknown_as_addr, true, fp);
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Namhyung Kim
maintainerPATCH v5 5/6 perf tools: Merge deferred user callchains
In reply to Namhyung KimSave samples with deferred callchains in a separate list and deliver them after merging the user callchains. If users don't want to merge they can set tool->merge_deferred_callchains to false to prevent the behavior.
With previous result, now perf script will show the merged callchains.
$ perf script
...
pwd 2312 121.163435: 249113 cpu/cycles/P:
ffffffff845b78d8 __build_id_parse.isra.0+0x218 ([kernel.kallsyms])
ffffffff83bb5bf6 perf_event_mmap+0x2e6 ([kernel.kallsyms])
ffffffff83c31959 mprotect_fixup+0x1e9 ([kernel.kallsyms])
ffffffff83c31dc5 do_mprotect_pkey+0x2b5 ([kernel.kallsyms])
ffffffff83c3206f __x64_sys_mprotect+0x1f ([kernel.kallsyms])
ffffffff845e6692 do_syscall_64+0x62 ([kernel.kallsyms])
ffffffff8360012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms])
7f18fe337fa7 mprotect+0x7 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe330e0f _dl_sysdep_start+0x7f (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe331448 _dl_start_user+0x0 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
...The old output can be get using --no-merge-callchain option. Also perf report can get the user callchain entry at the end.
$ perf report --no-children --stdio -q -S __build_id_parse.isra.0
# symbol: __build_id_parse.isra.0
8.40% pwd [kernel.kallsyms]
|
---__build_id_parse.isra.0
perf_event_mmap
mprotect_fixup
do_mprotect_pkey
__x64_sys_mprotect
do_syscall_64
entry_SYSCALL_64_after_hwframe
mprotect
_dl_sysdep_start
_dl_start_user--- tools/perf/Documentation/perf-script.txt | 5 ++ tools/perf/builtin-inject.c | 1 + tools/perf/builtin-report.c | 1 + tools/perf/builtin-script.c | 4 ++ tools/perf/util/callchain.c | 29 ++++++++++ tools/perf/util/callchain.h | 3 ++ tools/perf/util/evlist.c | 1 + tools/perf/util/evlist.h | 2 + tools/perf/util/session.c | 67 +++++++++++++++++++++++- tools/perf/util/tool.c | 2 + tools/perf/util/tool.h | 1 + 11 files changed, 115 insertions(+), 1 deletion(-)
tools/perf/Documentation/perf-script.txt +5 -0
@@ -527,6 +527,11 @@ include::itrace.txt[] The known limitations include exception handing such as setjmp/longjmp will have calls/returns not match. +--merge-callchains:: + Enable merging deferred user callchains if available. This is the + default behavior. If you want to see separate CALLCHAIN_DEFERRED + records for some reason, use --no-merge-callchains explicitly. + :GMEXAMPLECMD: script :GMEXAMPLESUBCMD: include::guest-files.txt[]
tools/perf/builtin-inject.c +1 -0
@@ -2527,6 +2527,7 @@ int cmd_inject(int argc, const char **argv) inject.tool.auxtrace = perf_event__repipe_auxtrace; inject.tool.bpf_metadata = perf_event__repipe_op2_synth; inject.tool.dont_split_sample_group = true; + inject.tool.merge_deferred_callchains = false; inject.session =__perf_session__new (&data, &inject.tool,/*trace_event_repipe=*/ inject.output.is_pipe,/*host_env=*/ NULL );
tools/perf/builtin-report.c +1 -0
@@ -1614,6 +1614,7 @@ int cmd_report(int argc, const char **argv) report.tool.event_update = perf_event__process_event_update; report.tool.feature = process_feature_event; report.tool.ordering_requires_timestamps = true; + report.tool.merge_deferred_callchains = !dump_trace; session =perf_session__new (&data, &report.tool);if (IS_ERR (session)) {
tools/perf/builtin-script.c +4 -0
@@ -4009,6 +4009,7 @@ int cmd_script(int argc, const char **argv)bool header_only = false;bool script_started = false;bool unsorted_dump = false; +bool merge_deferred_callchains = true;char *rec_script_path =NULL ;char *rep_script_path =NULL ;struct perf_session *session;
@@ -4162,6 +4163,8 @@ int cmd_script(int argc, const char **argv) "Guest code can be found in hypervisor process"),OPT_BOOLEAN ('\0' ,"stitch-lbr" , &script.stitch_lbr,"Enable LBR callgraph stitching approach" ), +OPT_BOOLEAN ('\0' ,"merge-callchains" , &merge_deferred_callchains, +"Enable merge deferred user callchains" ),OPTS_EVSWITCH (&script.evswitch), OPT_END() };
@@ -4418,6 +4421,7 @@ int cmd_script(int argc, const char **argv) script.tool.throttle = process_throttle_event; script.tool.unthrottle = process_throttle_event; script.tool.ordering_requires_timestamps = true; + script.tool.merge_deferred_callchains = merge_deferred_callchains; session =perf_session__new (&data, &script.tool);if (IS_ERR (session))return PTR_ERR (session);
tools/perf/util/callchain.c +29 -0
@@ -1838,3 +1838,32 @@ int sample__for_each_callchain_node(struct thread *thread, struct evsel *evsel, }return 0 ; } + +int sample__merge_deferred_callchain (struct perf_sample *sample_orig, +struct perf_sample *sample_callchain) +{ +u64 nr_orig = sample_orig->callchain->nr -1 ; +u64 nr_deferred = sample_callchain->callchain->nr; +struct ip_callchain *callchain; + +if (sample_orig->callchain->nr <2 ) { + sample_orig->deferred_callchain = false; +return -EINVAL ; + } + + callchain =calloc (1 + nr_orig + nr_deferred,sizeof (u64)); +if (callchain ==NULL ) { + sample_orig->deferred_callchain = false; +return -ENOMEM ; + } + + callchain->nr = nr_orig + nr_deferred; +/* copy original including PERF_CONTEXT_USER_DEFERRED (but the cookie) */ +memcpy (callchain->ips, sample_orig->callchain->ips, nr_orig *sizeof (u64)); +/* copy deferred user callchains */ +memcpy (&callchain->ips[nr_orig], sample_callchain->callchain->ips, + nr_deferred *sizeof (u64)); + + sample_orig->callchain = callchain; +return 0 ; +}
tools/perf/util/callchain.h +3 -0
@@ -318,4 +318,7 @@ int sample__for_each_callchain_node(struct thread *thread, struct evsel *evsel, struct perf_sample *sample, int max_stack, bool symbols, callchain_iter_fn cb, void *data); +int sample__merge_deferred_callchain (struct perf_sample *sample_orig, +struct perf_sample *sample_callchain); +#endif /* __PERF_CALLCHAIN_H */
tools/perf/util/evlist.c +1 -0
@@ -85,6 +85,7 @@ void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, evlist->ctl_fd.pos =-1 ; evlist->nr_br_cntr =-1 ;metricgroup__rblist_init (&evlist->metric_events); +INIT_LIST_HEAD (&evlist->deferred_samples); }struct evlist *evlist__new (void )
tools/perf/util/evlist.h +2 -0
@@ -92,6 +92,8 @@ struct evlist {* of struct metric_expr. */ struct rblist metric_events; +/* samples with deferred_callchain would wait here. */ +struct list_head deferred_samples; };struct evsel_str_handler {
tools/perf/util/session.c +66 -1
85 lines not on this page · raw message
tools/perf/util/tool.c +2 -0
@@ -266,6 +266,7 @@ void perf_tool__init(struct perf_tool *tool, bool ordered_events) tool->cgroup_events = false; tool->no_warn = false; tool->show_feat_hdr =SHOW_FEAT_NO_HEADER ; + tool->merge_deferred_callchains = true; tool->sample = process_event_sample_stub; tool->mmap = process_event_stub;
@@ -448,6 +449,7 @@ void delegate_tool__init(struct delegate_tool *tool, struct perf_tool *delegate) tool->tool.cgroup_events = delegate->cgroup_events; tool->tool.no_warn = delegate->no_warn; tool->tool.show_feat_hdr = delegate->show_feat_hdr; + tool->tool.merge_deferred_callchains = delegate->merge_deferred_callchains; tool->tool.sample = delegate_sample; tool->tool.read = delegate_read;
tools/perf/util/tool.h +1 -0
@@ -90,6 +90,7 @@ struct perf_tool {bool cgroup_events;bool no_warn;bool dont_split_sample_group; +bool merge_deferred_callchains;enum show_feature_header show_feat_hdr; };
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Ian Rogers
reviewerRe: [PATCH v5 5/6] perf tools: Merge deferred user callchains
Quoted 204 lines · Namhyung Kim
Save samples with deferred callchains in a separate list and deliver them after merging the user callchains. If users don't want to merge they can set
tool->merge_deferred_callchainsto false to prevent the behavior.With previous result, now perf script will show the merged callchains.
$ perf script ... pwd 2312 121.163435: 249113 cpu/cycles/P: ffffffff845b78d8 __build_id_parse.isra.0+0x218 ([kernel.kallsyms]) ffffffff83bb5bf6 perf_event_mmap+0x2e6 ([kernel.kallsyms]) ffffffff83c31959 mprotect_fixup+0x1e9 ([kernel.kallsyms]) ffffffff83c31dc5 do_mprotect_pkey+0x2b5 ([kernel.kallsyms]) ffffffff83c3206f __x64_sys_mprotect+0x1f ([kernel.kallsyms]) ffffffff845e6692 do_syscall_64+0x62 ([kernel.kallsyms]) ffffffff8360012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms]) 7f18fe337fa7 mprotect+0x7 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2) 7f18fe330e0f _dl_sysdep_start+0x7f (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2) 7f18fe331448 _dl_start_user+0x0 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2) ...The old output can be get using --no-merge-callchain option. Also perf report can get the user callchain entry at the end.
$ perf report --no-children --stdio -q -S __build_id_parse.isra.0 # symbol: __build_id_parse.isra.0 8.40% pwd [kernel.kallsyms] | ---__build_id_parse.isra.0 perf_event_mmap mprotect_fixup do_mprotect_pkey __x64_sys_mprotect do_syscall_64 entry_SYSCALL_64_after_hwframe mprotect _dl_sysdep_start _dl_start_userSigned-off-by Namhyung Kim <[email protected]>--- tools/perf/Documentation/perf-script.txt | 5 ++ tools/perf/builtin-inject.c | 1 + tools/perf/builtin-report.c | 1 + tools/perf/builtin-script.c | 4 ++ tools/perf/util/callchain.c | 29 ++++++++++ tools/perf/util/callchain.h | 3 ++ tools/perf/util/evlist.c | 1 + tools/perf/util/evlist.h | 2 + tools/perf/util/session.c | 67 +++++++++++++++++++++++- tools/perf/util/tool.c | 2 + tools/perf/util/tool.h | 1 + 11 files changed, 115 insertions(+), 1 deletion(-)tools/perf/Documentation/perf-script.txt +5
-0@@ -527,6 +527,11 @@ include::itrace.txt[] The known limitations include exception handing such as setjmp/longjmp will have calls/returns not match. +--merge-callchains:: + Enable merging deferred user callchains if available. This is the + default behavior. If you want to see separate CALLCHAIN_DEFERRED + records for some reason, use --no-merge-callchains explicitly. + :GMEXAMPLECMD: script :GMEXAMPLESUBCMD: include::guest-files.txt[]tools/perf/builtin-inject.c +1
-0@@ -2527,6 +2527,7 @@ int cmd_inject(int argc, const char **argv) inject.tool.auxtrace = perf_event__repipe_auxtrace; inject.tool.bpf_metadata = perf_event__repipe_op2_synth; inject.tool.dont_split_sample_group = true; + inject.tool.merge_deferred_callchains = false; inject.session =__perf_session__new (&data, &inject.tool,/*trace_event_repipe=*/ inject.output.is_pipe,/*host_env=*/ NULL );tools/perf/builtin-report.c +1
-0@@ -1614,6 +1614,7 @@ int cmd_report(int argc, const char **argv) report.tool.event_update = perf_event__process_event_update; report.tool.feature = process_feature_event; report.tool.ordering_requires_timestamps = true; + report.tool.merge_deferred_callchains = !dump_trace; session =perf_session__new (&data, &report.tool);if (IS_ERR (session)) {tools/perf/builtin-script.c +4
-0@@ -4009,6 +4009,7 @@ int cmd_script(int argc, const char **argv)bool header_only = false;bool script_started = false;bool unsorted_dump = false; +bool merge_deferred_callchains = true;char *rec_script_path =NULL ;char *rep_script_path =NULL ;struct perf_session *session;@@ -4162,6 +4163,8 @@ int cmd_script(int argc, const char **argv) "Guest code can be found in hypervisor process"),OPT_BOOLEAN ('\0' ,"stitch-lbr" , &script.stitch_lbr,"Enable LBR callgraph stitching approach" ), +OPT_BOOLEAN ('\0' ,"merge-callchains" , &merge_deferred_callchains, +"Enable merge deferred user callchains" ),OPTS_EVSWITCH (&script.evswitch), OPT_END() };@@ -4418,6 +4421,7 @@ int cmd_script(int argc, const char **argv) script.tool.throttle = process_throttle_event; script.tool.unthrottle = process_throttle_event; script.tool.ordering_requires_timestamps = true; + script.tool.merge_deferred_callchains = merge_deferred_callchains; session =perf_session__new (&data, &script.tool);if (IS_ERR (session))return PTR_ERR (session);tools/perf/util/callchain.c +29
-0@@ -1838,3 +1838,32 @@ int sample__for_each_callchain_node(struct thread *thread, struct evsel *evsel, }return 0 ; } + +int sample__merge_deferred_callchain (struct perf_sample *sample_orig, +struct perf_sample *sample_callchain) +{ +u64 nr_orig = sample_orig->callchain->nr -1 ; +u64 nr_deferred = sample_callchain->callchain->nr; +struct ip_callchain *callchain; + +if (sample_orig->callchain->nr <2 ) { + sample_orig->deferred_callchain = false; +return -EINVAL ; + } + + callchain =calloc (1 + nr_orig + nr_deferred,sizeof (u64)); +if (callchain ==NULL ) { + sample_orig->deferred_callchain = false; +return -ENOMEM ; + } + + callchain->nr = nr_orig + nr_deferred; +/* copy original including PERF_CONTEXT_USER_DEFERRED (but the cookie) */ +memcpy (callchain->ips, sample_orig->callchain->ips, nr_orig *sizeof (u64)); +/* copy deferred user callchains */ +memcpy (&callchain->ips[nr_orig], sample_callchain->callchain->ips, + nr_deferred *sizeof (u64)); + + sample_orig->callchain = callchain; +return 0 ; +}tools/perf/util/callchain.h +3
-0@@ -318,4 +318,7 @@ int sample__for_each_callchain_node(struct thread *thread, struct evsel *evsel, struct perf_sample *sample, int max_stack, bool symbols, callchain_iter_fn cb, void *data); +int sample__merge_deferred_callchain (struct perf_sample *sample_orig, +struct perf_sample *sample_callchain); +#endif /* __PERF_CALLCHAIN_H */ tools/perf/util/evlist.c +1
-0@@ -85,6 +85,7 @@ void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, evlist->ctl_fd.pos =-1 ; evlist->nr_br_cntr =-1 ;metricgroup__rblist_init (&evlist->metric_events); +INIT_LIST_HEAD (&evlist->deferred_samples); }struct evlist *evlist__new (void )tools/perf/util/evlist.h +2
-0@@ -92,6 +92,8 @@ struct evlist {* of struct metric_expr. */ struct rblist metric_events; +/* samples with deferred_callchain would wait here. */ +struct list_head deferred_samples; };struct evsel_str_handler {tools/perf/util/session.c +0
-0
tools/perf/util/session.c static int evlist__deliver_sample(struct evlist *evlist, const struct perf_tool · line 1290
Quoted 6 lines
tools/perf/util/session.c +3
-0@@ -1285,6 +1285,60 @@ static int evlist__deliver_sample(struct evlist *evlist, const struct perf_tool per_thread); } +struct deferred_event { +struct list_head list; +union perf_event *event;
Is this the old version of the patch? No comment and it seems the event's memory isn't copied. I'm worried as we have events in stack allocated memory such as:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n1618
or copies:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-inject.c?h=perf-tools-next#n336
or just because the ring buffer overwrites itself. My belief is that we don't hold events, and the associated parsed sample, longer than the tool callback because it'll be wrong/corrupt after that. Here the deferred callchain events are all being held longer than a single tool event callback.
Quoted 33 lines
tools/perf/util/session.c +33
-0+}; + +static int evlist__deliver_deferred_samples (struct evlist *evlist, +const struct perf_tool *tool, +union perf_event *event, +struct perf_sample *sample, +struct machine *machine) +{ +struct deferred_event *de, *tmp; +struct evsel *evsel; +int ret =0 ; + +if (!tool->merge_deferred_callchains) { + evsel =evlist__id2evsel (evlist, sample->id); +return tool->callchain_deferred (tool, event, sample, + evsel, machine); + } + +list_for_each_entry_safe (de, tmp, &evlist->deferred_samples, list) { +struct perf_sample orig_sample; + + ret =evlist__parse_sample (evlist, de->event, &orig_sample); +if (ret <0 ) { +pr_err ("failed to parse original sample\n" ); +break ; + } + +if (sample->tid != orig_sample.tid) +continue ; + +if (event->callchain_deferred.cookie == orig_sample.deferred_cookie) +sample__merge_deferred_callchain (&orig_sample, sample); +else
tools/perf/util/session.c static int evlist__deliver_sample(struct evlist *evlist, const struct perf_tool · line 1333
Quoted 10 lines
tools/perf/util/session.c +10
-0+ orig_sample.deferred_callchain = false; + + evsel =evlist__id2evsel (evlist, orig_sample.id); + ret =evlist__deliver_sample (evlist, tool, de->event, + &orig_sample, evsel, machine); + +if (orig_sample.deferred_callchain) +free (orig_sample.callchain); + +list_del (&de->list);
There's no free of de->event.
Quoted 12 lines
tools/perf/util/session.c +8
-0+free (de); + +if (ret) +break ; + } +return ret; +} + static int machines__deliver_event(struct machines *machines, struct evlist *evlist, union perf_event *event,
tools/perf/util/session.c static int machines__deliver_event(struct machines *machines, · line 1376
Quoted 10 lines
tools/perf/util/session.c +7
-0@@ -1313,6 +1367,16 @@ static int machines__deliver_event(struct machines *machines,return 0 ; }dump_sample (evsel, event, sample,perf_env__arch (machine->env)); +if (sample->deferred_callchain && tool->merge_deferred_callchains) { +struct deferred_event *de =malloc (sizeof (*de)); + +if (de ==NULL ) +return -ENOMEM ; + + de->event = event;
Here the event is assigned but not copied.
Thanks,
Ian
Quoted 51 lines
tools/perf/util/session.c +5
-1+list_add_tail (&de->list, &evlist->deferred_samples); +return 0 ; + }return evlist__deliver_sample (evlist, tool, event, sample, evsel, machine);case PERF_RECORD_MMAP :return tool->mmap (tool, event, sample, machine);@@ -1372,7 +1436,8 @@ static int machines__deliver_event(struct machines *machines,return tool->aux_output_hw_id (tool, event, sample, machine);case PERF_RECORD_CALLCHAIN_DEFERRED :dump_deferred_callchain (evsel, event, sample); -return tool->callchain_deferred (tool, event, sample, evsel, machine); +return evlist__deliver_deferred_samples (evlist, tool, event, + sample, machine);default : ++evlist->stats.nr_unknown_events;return -1 ;tools/perf/util/tool.c +2
-0@@ -266,6 +266,7 @@ void perf_tool__init(struct perf_tool *tool, bool ordered_events) tool->cgroup_events = false; tool->no_warn = false; tool->show_feat_hdr =SHOW_FEAT_NO_HEADER ; + tool->merge_deferred_callchains = true; tool->sample = process_event_sample_stub; tool->mmap = process_event_stub;@@ -448,6 +449,7 @@ void delegate_tool__init(struct delegate_tool *tool, struct perf_tool *delegate) tool->tool.cgroup_events = delegate->cgroup_events; tool->tool.no_warn = delegate->no_warn; tool->tool.show_feat_hdr = delegate->show_feat_hdr; + tool->tool.merge_deferred_callchains = delegate->merge_deferred_callchains; tool->tool.sample = delegate_sample; tool->tool.read = delegate_read;tools/perf/util/tool.h +1
-1@@ -90,6 +90,7 @@ struct perf_tool {bool cgroup_events;bool no_warn;bool dont_split_sample_group; +bool merge_deferred_callchains;enum show_feature_header show_feat_hdr; }; --2.52.0.rc1.455.g30608eb744-goog
Namhyung Kim
maintainerRe: [PATCH v5 5/6] perf tools: Merge deferred user callchains
Quoted 208 lines · Ian Rogers
Quoted 207 lines · Namhyung Kim
Save samples with deferred callchains in a separate list and deliver them after merging the user callchains. If users don't want to merge they can set
tool->merge_deferred_callchainsto false to prevent the behavior.With previous result, now perf script will show the merged callchains.
$ perf script ... pwd 2312 121.163435: 249113 cpu/cycles/P: ffffffff845b78d8 __build_id_parse.isra.0+0x218 ([kernel.kallsyms]) ffffffff83bb5bf6 perf_event_mmap+0x2e6 ([kernel.kallsyms]) ffffffff83c31959 mprotect_fixup+0x1e9 ([kernel.kallsyms]) ffffffff83c31dc5 do_mprotect_pkey+0x2b5 ([kernel.kallsyms]) ffffffff83c3206f __x64_sys_mprotect+0x1f ([kernel.kallsyms]) ffffffff845e6692 do_syscall_64+0x62 ([kernel.kallsyms]) ffffffff8360012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms]) 7f18fe337fa7 mprotect+0x7 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2) 7f18fe330e0f _dl_sysdep_start+0x7f (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2) 7f18fe331448 _dl_start_user+0x0 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2) ...The old output can be get using --no-merge-callchain option. Also perf report can get the user callchain entry at the end.
$ perf report --no-children --stdio -q -S __build_id_parse.isra.0 # symbol: __build_id_parse.isra.0 8.40% pwd [kernel.kallsyms] | ---__build_id_parse.isra.0 perf_event_mmap mprotect_fixup do_mprotect_pkey __x64_sys_mprotect do_syscall_64 entry_SYSCALL_64_after_hwframe mprotect _dl_sysdep_start _dl_start_userSigned-off-by Namhyung Kim <[email protected]>--- tools/perf/Documentation/perf-script.txt | 5 ++ tools/perf/builtin-inject.c | 1 + tools/perf/builtin-report.c | 1 + tools/perf/builtin-script.c | 4 ++ tools/perf/util/callchain.c | 29 ++++++++++ tools/perf/util/callchain.h | 3 ++ tools/perf/util/evlist.c | 1 + tools/perf/util/evlist.h | 2 + tools/perf/util/session.c | 67 +++++++++++++++++++++++- tools/perf/util/tool.c | 2 + tools/perf/util/tool.h | 1 + 11 files changed, 115 insertions(+), 1 deletion(-)tools/perf/Documentation/perf-script.txt +5
-0@@ -527,6 +527,11 @@ include::itrace.txt[] The known limitations include exception handing such as setjmp/longjmp will have calls/returns not match. +--merge-callchains:: + Enable merging deferred user callchains if available. This is the + default behavior. If you want to see separate CALLCHAIN_DEFERRED + records for some reason, use --no-merge-callchains explicitly. + :GMEXAMPLECMD: script :GMEXAMPLESUBCMD: include::guest-files.txt[]tools/perf/builtin-inject.c +1
-0@@ -2527,6 +2527,7 @@ int cmd_inject(int argc, const char **argv) inject.tool.auxtrace = perf_event__repipe_auxtrace; inject.tool.bpf_metadata = perf_event__repipe_op2_synth; inject.tool.dont_split_sample_group = true; + inject.tool.merge_deferred_callchains = false; inject.session =__perf_session__new (&data, &inject.tool,/*trace_event_repipe=*/ inject.output.is_pipe,/*host_env=*/ NULL );tools/perf/builtin-report.c +1
-0@@ -1614,6 +1614,7 @@ int cmd_report(int argc, const char **argv) report.tool.event_update = perf_event__process_event_update; report.tool.feature = process_feature_event; report.tool.ordering_requires_timestamps = true; + report.tool.merge_deferred_callchains = !dump_trace; session =perf_session__new (&data, &report.tool);if (IS_ERR (session)) {tools/perf/builtin-script.c +4
-0@@ -4009,6 +4009,7 @@ int cmd_script(int argc, const char **argv)bool header_only = false;bool script_started = false;bool unsorted_dump = false; +bool merge_deferred_callchains = true;char *rec_script_path =NULL ;char *rep_script_path =NULL ;struct perf_session *session;@@ -4162,6 +4163,8 @@ int cmd_script(int argc, const char **argv) "Guest code can be found in hypervisor process"),OPT_BOOLEAN ('\0' ,"stitch-lbr" , &script.stitch_lbr,"Enable LBR callgraph stitching approach" ), +OPT_BOOLEAN ('\0' ,"merge-callchains" , &merge_deferred_callchains, +"Enable merge deferred user callchains" ),OPTS_EVSWITCH (&script.evswitch), OPT_END() };@@ -4418,6 +4421,7 @@ int cmd_script(int argc, const char **argv) script.tool.throttle = process_throttle_event; script.tool.unthrottle = process_throttle_event; script.tool.ordering_requires_timestamps = true; + script.tool.merge_deferred_callchains = merge_deferred_callchains; session =perf_session__new (&data, &script.tool);if (IS_ERR (session))return PTR_ERR (session);tools/perf/util/callchain.c +29
-0@@ -1838,3 +1838,32 @@ int sample__for_each_callchain_node(struct thread *thread, struct evsel *evsel, }return 0 ; } + +int sample__merge_deferred_callchain (struct perf_sample *sample_orig, +struct perf_sample *sample_callchain) +{ +u64 nr_orig = sample_orig->callchain->nr -1 ; +u64 nr_deferred = sample_callchain->callchain->nr; +struct ip_callchain *callchain; + +if (sample_orig->callchain->nr <2 ) { + sample_orig->deferred_callchain = false; +return -EINVAL ; + } + + callchain =calloc (1 + nr_orig + nr_deferred,sizeof (u64)); +if (callchain ==NULL ) { + sample_orig->deferred_callchain = false; +return -ENOMEM ; + } + + callchain->nr = nr_orig + nr_deferred; +/* copy original including PERF_CONTEXT_USER_DEFERRED (but the cookie) */ +memcpy (callchain->ips, sample_orig->callchain->ips, nr_orig *sizeof (u64)); +/* copy deferred user callchains */ +memcpy (&callchain->ips[nr_orig], sample_callchain->callchain->ips, + nr_deferred *sizeof (u64)); + + sample_orig->callchain = callchain; +return 0 ; +}tools/perf/util/callchain.h +3
-0@@ -318,4 +318,7 @@ int sample__for_each_callchain_node(struct thread *thread, struct evsel *evsel, struct perf_sample *sample, int max_stack, bool symbols, callchain_iter_fn cb, void *data); +int sample__merge_deferred_callchain (struct perf_sample *sample_orig, +struct perf_sample *sample_callchain); +#endif /* __PERF_CALLCHAIN_H */ tools/perf/util/evlist.c +1
-0@@ -85,6 +85,7 @@ void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, evlist->ctl_fd.pos =-1 ; evlist->nr_br_cntr =-1 ;metricgroup__rblist_init (&evlist->metric_events); +INIT_LIST_HEAD (&evlist->deferred_samples); }struct evlist *evlist__new (void )tools/perf/util/evlist.h +2
-0@@ -92,6 +92,8 @@ struct evlist {* of struct metric_expr. */ struct rblist metric_events; +/* samples with deferred_callchain would wait here. */ +struct list_head deferred_samples; };struct evsel_str_handler {tools/perf/util/session.c +0
-0@@ -1285,6 +1285,60 @@ static int evlist__deliver_sample(struct evlist *evlist, const struct perf_tool per_thread); }
Quoted 15 lines
tools/perf/util/session.c static int evlist__deliver_sample(struct evlist *evlist, const struct perf_tool · line 1290
Quoted 3 lines
tools/perf/util/session.c +3
-0+struct deferred_event { +struct list_head list; +union perf_event *event;Is this the old version of the patch? No comment and it seems the event's memory isn't copied. I'm worried as we have events in stack allocated memory such as:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n1618
or copies:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-inject.c?h=perf-tools-next#n336
or just because the ring buffer overwrites itself. My belief is that we don't hold events, and the associated parsed sample, longer than the tool callback because it'll be wrong/corrupt after that. Here the deferred callchain events are all being held longer than a single tool event callback.
I added the conditional copy in the next patch, but as you said, it'd be safer to copy always. The concern is the performance of perf report which is already slow and I don't want to make it worse. But let's start with the simple, safe version and optimize it later.
Thanks,
Namhyung
Namhyung Kim
maintainerPATCH v5 6/6 perf tools: Flush remaining samples w/o deferred callchains
In reply to Namhyung KimIt's possible that some kernel samples don't have matching deferred callchain records when the profiling session was ended before the threads came back to userspace. Let's flush the samples before finish the session.
Also 32-bit systems can see partial mmap for the data. In that case, deferred samples won't point to the correct data once the mapping moves to the next portion of the file. Copy the original sample before it unmaps the current data.
--- tools/perf/util/session.c | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+)
tools/perf/util/session.c +98 -0
@@ -1288,8 +1288,13 @@ static int evlist__deliver_sample(struct evlist *evlist, const struct perf_toolstruct deferred_event {struct list_head list;union perf_event *event; +bool allocated; }; +/* +* This is called when a deferred callchain record comes up. Find all matching +* samples, merge the callchains and process them. +*/ static int evlist__deliver_deferred_samples(struct evlist *evlist, const struct perf_tool *tool, union perf_event *event,
@@ -1331,6 +1336,86 @@ static int evlist__deliver_deferred_samples(struct evlist *evlist,free (orig_sample.callchain);list_del (&de->list); +if (de->allocated) +free (de->event); +free (de); + +if (ret) +break ; + } +return ret; +} + +/* +* This is called when the backing mmap is about to go away. It needs to save +* the original sample data until it finds the matching deferred callchains. +*/ +static void evlist__copy_deferred_samples (struct evlist *evlist, +const struct perf_tool *tool, +struct machine *machine) +{ +struct deferred_event *de, *tmp; +struct evsel *evsel; +int ret =0 ; + +list_for_each_entry_safe (de, tmp, &evlist->deferred_samples, list) { +struct perf_sample sample; +size_t sz = de->event->header.size; +void *buf; + +if (de->allocated) +continue ; + + buf =malloc (sz); +if (buf) { +memcpy (buf, de->event, sz); + de->event = buf; + de->allocated = true; +continue ; + } + +/* The allocation failed, flush the sample now */ + ret =evlist__parse_sample (evlist, de->event, &sample); +if (ret ==0 ) { + evsel =evlist__id2evsel (evlist, sample.id); +evlist__deliver_sample (evlist, tool, de->event, + &sample, evsel, machine); + } + +list_del (&de->list); +BUG_ON (de->allocated); +free (de); + } +} + +/* +* This is called at the end of the data processing for the session. Flush the +* remaining samples as there's no hope for matching deferred callchains. +*/ +static int evlist__flush_deferred_samples (struct evlist *evlist, +const struct perf_tool *tool, +struct machine *machine) +{ +struct deferred_event *de, *tmp; +struct evsel *evsel; +int ret =0 ; + +list_for_each_entry_safe (de, tmp, &evlist->deferred_samples, list) { +struct perf_sample sample; + + ret =evlist__parse_sample (evlist, de->event, &sample); +if (ret <0 ) { +pr_err ("failed to parse original sample\n" ); +break ; + } + + evsel =evlist__id2evsel (evlist, sample.id); + ret =evlist__deliver_sample (evlist, tool, de->event, + &sample, evsel, machine); + +list_del (&de->list); +if (de->allocated) +free (de->event);free (de);if (ret)
@@ -1374,6 +1459,7 @@ static int machines__deliver_event(struct machines *machines,return -ENOMEM ; de->event = event; + de->allocated = false;list_add_tail (&de->list, &evlist->deferred_samples);return 0 ; }
@@ -2218,6 +2304,8 @@ reader__mmap(struct reader *rd, struct perf_session *session) }if (mmaps[rd->mmap_idx]) { +evlist__copy_deferred_samples (session->evlist, session->tool, + &session->machines.host);munmap (mmaps[rd->mmap_idx], rd->mmap_size); mmaps[rd->mmap_idx] =NULL ; }
@@ -2372,6 +2460,11 @@ static int __perf_session__process_events(struct perf_session *session)if (err) goto out_err; err =auxtrace__flush_events (session, tool); +if (err) + goto out_err; + err =evlist__flush_deferred_samples (session->evlist, + session->tool, + &session->machines.host);if (err) goto out_err; err =perf_session__flush_thread_stacks (session);
@@ -2494,6 +2587,11 @@ static int __perf_session__process_dir_events(struct perf_session *session)if (ret) goto out_err; + ret =evlist__flush_deferred_samples (session->evlist, tool, + &session->machines.host); +if (ret) + goto out_err; + ret =perf_session__flush_thread_stacks (session); out_err:ui_progress__finish ();
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Ian Rogers
reviewerRe: [PATCH v5 6/6] perf tools: Flush remaining samples w/o deferred callchains
Quoted 10 lines · Namhyung Kim
It's possible that some kernel samples don't have matching deferred callchain records when the profiling session was ended before the threads came back to userspace. Let's flush the samples before finish the session.
Also 32-bit systems can see partial mmap for the data. In that case, deferred samples won't point to the correct data once the mapping moves to the next portion of the file. Copy the original sample before it unmaps the current data.
I think it is simpler to always copy. We may have events from synthesis, inject, .. and not the reader. Relying on callers to know that someone made a copy of the event and to make a defensive copy on their behalf just feels error prone.
In the python session API I need to deal with the lifetime of events. Currently the events are copied:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/python.c?h=perf-tools-next#n507
and I'm doing this for session tool callbacks:
https://lore.kernel.org/lkml/[email protected]/
I think it can be made lazier by knowing the tool callback can assume the event and sample are valid. We can delay the copying of the event/sample for if the pyevent has a reference count >1 and we're returning out of the tool callback. Doing some kind of global knowledge in the reader for maintaining the correctness of memory, I'm just not clear on how to make it always work.
Thanks,
Ian
Quoted 154 lines
Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/util/session.c | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+)tools/perf/util/session.c +98
-1@@ -1288,8 +1288,13 @@ static int evlist__deliver_sample(struct evlist *evlist, const struct perf_toolstruct deferred_event {struct list_head list;union perf_event *event; +bool allocated; }; +/* +* This is called when a deferred callchain record comes up. Find all matching +* samples, merge the callchains and process them. +*/ static int evlist__deliver_deferred_samples(struct evlist *evlist, const struct perf_tool *tool, union perf_event *event,@@ -1331,6 +1336,86 @@ static int evlist__deliver_deferred_samples(struct evlist *evlist,free (orig_sample.callchain);list_del (&de->list); +if (de->allocated) +free (de->event); +free (de); + +if (ret) +break ; + } +return ret; +} + +/* +* This is called when the backing mmap is about to go away. It needs to save +* the original sample data until it finds the matching deferred callchains. +*/ +static void evlist__copy_deferred_samples (struct evlist *evlist, +const struct perf_tool *tool, +struct machine *machine) +{ +struct deferred_event *de, *tmp; +struct evsel *evsel; +int ret =0 ; + +list_for_each_entry_safe (de, tmp, &evlist->deferred_samples, list) { +struct perf_sample sample; +size_t sz = de->event->header.size; +void *buf; + +if (de->allocated) +continue ; + + buf =malloc (sz); +if (buf) { +memcpy (buf, de->event, sz); + de->event = buf; + de->allocated = true; +continue ; + } + +/* The allocation failed, flush the sample now */ + ret =evlist__parse_sample (evlist, de->event, &sample); +if (ret ==0 ) { + evsel =evlist__id2evsel (evlist, sample.id); +evlist__deliver_sample (evlist, tool, de->event, + &sample, evsel, machine); + } + +list_del (&de->list); +BUG_ON (de->allocated); +free (de); + } +} + +/* +* This is called at the end of the data processing for the session. Flush the +* remaining samples as there's no hope for matching deferred callchains. +*/ +static int evlist__flush_deferred_samples (struct evlist *evlist, +const struct perf_tool *tool, +struct machine *machine) +{ +struct deferred_event *de, *tmp; +struct evsel *evsel; +int ret =0 ; + +list_for_each_entry_safe (de, tmp, &evlist->deferred_samples, list) { +struct perf_sample sample; + + ret =evlist__parse_sample (evlist, de->event, &sample); +if (ret <0 ) { +pr_err ("failed to parse original sample\n" ); +break ; + } + + evsel =evlist__id2evsel (evlist, sample.id); + ret =evlist__deliver_sample (evlist, tool, de->event, + &sample, evsel, machine); + +list_del (&de->list); +if (de->allocated) +free (de->event);free (de);if (ret)@@ -1374,6 +1459,7 @@ static int machines__deliver_event(struct machines *machines,return -ENOMEM ; de->event = event; + de->allocated = false;list_add_tail (&de->list, &evlist->deferred_samples);return 0 ; }@@ -2218,6 +2304,8 @@ reader__mmap(struct reader *rd, struct perf_session *session) }if (mmaps[rd->mmap_idx]) { +evlist__copy_deferred_samples (session->evlist, session->tool, + &session->machines.host);munmap (mmaps[rd->mmap_idx], rd->mmap_size); mmaps[rd->mmap_idx] =NULL ; }@@ -2372,6 +2460,11 @@ static int __perf_session__process_events(struct perf_session *session)if (err) goto out_err; err =auxtrace__flush_events (session, tool); +if (err) + goto out_err; + err =evlist__flush_deferred_samples (session->evlist, + session->tool, + &session->machines.host);if (err) goto out_err; err =perf_session__flush_thread_stacks (session);@@ -2494,6 +2587,11 @@ static int __perf_session__process_dir_events(struct perf_session *session)if (ret) goto out_err; + ret =evlist__flush_deferred_samples (session->evlist, tool, + &session->machines.host); +if (ret) + goto out_err; + ret =perf_session__flush_thread_stacks (session); out_err:ui_progress__finish (); --2.52.0.rc1.455.g30608eb744-goog
Ian Rogers
reviewerRe: [PATCH v5 6/6] perf tools: Flush remaining samples w/o deferred callchains
Quoted 18 lines · Ian Rogers
Quoted 10 lines · Namhyung Kim
It's possible that some kernel samples don't have matching deferred callchain records when the profiling session was ended before the threads came back to userspace. Let's flush the samples before finish the session.
Also 32-bit systems can see partial mmap for the data. In that case, deferred samples won't point to the correct data once the mapping moves to the next portion of the file. Copy the original sample before it unmaps the current data.
I think it is simpler to always copy. We may have events from synthesis, inject, .. and not the reader. Relying on callers to know that someone made a copy of the event and to make a defensive copy on their behalf just feels error prone.
Quoted 11 lines
In the python session API I need to deal with the lifetime of events. Currently the events are copied:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/python.c?h=perf-tools-next#n507
and I'm doing this for session tool callbacks:
https://lore.kernel.org/lkml/[email protected]/
I think it can be made lazier by knowing the tool callback can assume the event and sample are valid. We can delay the copying of the event/sample for if the pyevent has a reference count >1 and we're returning out of the tool callback. Doing some kind of global knowledge in the reader for maintaining the correctness of memory, I'm just not clear on how to make it always work.
I believe we always reuse the memory for the event, per event, in pipe mode:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n1868
so a lazy copy will be broken for the pipe mode case.
Thanks,
Ian
Quoted 154 lines
Quoted 154 lines
Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/util/session.c | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+)tools/perf/util/session.c +98
-1@@ -1288,8 +1288,13 @@ static int evlist__deliver_sample(struct evlist *evlist, const struct perf_toolstruct deferred_event {struct list_head list;union perf_event *event; +bool allocated; }; +/* +* This is called when a deferred callchain record comes up. Find all matching +* samples, merge the callchains and process them. +*/ static int evlist__deliver_deferred_samples(struct evlist *evlist, const struct perf_tool *tool, union perf_event *event,@@ -1331,6 +1336,86 @@ static int evlist__deliver_deferred_samples(struct evlist *evlist,free (orig_sample.callchain);list_del (&de->list); +if (de->allocated) +free (de->event); +free (de); + +if (ret) +break ; + } +return ret; +} + +/* +* This is called when the backing mmap is about to go away. It needs to save +* the original sample data until it finds the matching deferred callchains. +*/ +static void evlist__copy_deferred_samples (struct evlist *evlist, +const struct perf_tool *tool, +struct machine *machine) +{ +struct deferred_event *de, *tmp; +struct evsel *evsel; +int ret =0 ; + +list_for_each_entry_safe (de, tmp, &evlist->deferred_samples, list) { +struct perf_sample sample; +size_t sz = de->event->header.size; +void *buf; + +if (de->allocated) +continue ; + + buf =malloc (sz); +if (buf) { +memcpy (buf, de->event, sz); + de->event = buf; + de->allocated = true; +continue ; + } + +/* The allocation failed, flush the sample now */ + ret =evlist__parse_sample (evlist, de->event, &sample); +if (ret ==0 ) { + evsel =evlist__id2evsel (evlist, sample.id); +evlist__deliver_sample (evlist, tool, de->event, + &sample, evsel, machine); + } + +list_del (&de->list); +BUG_ON (de->allocated); +free (de); + } +} + +/* +* This is called at the end of the data processing for the session. Flush the +* remaining samples as there's no hope for matching deferred callchains. +*/ +static int evlist__flush_deferred_samples (struct evlist *evlist, +const struct perf_tool *tool, +struct machine *machine) +{ +struct deferred_event *de, *tmp; +struct evsel *evsel; +int ret =0 ; + +list_for_each_entry_safe (de, tmp, &evlist->deferred_samples, list) { +struct perf_sample sample; + + ret =evlist__parse_sample (evlist, de->event, &sample); +if (ret <0 ) { +pr_err ("failed to parse original sample\n" ); +break ; + } + + evsel =evlist__id2evsel (evlist, sample.id); + ret =evlist__deliver_sample (evlist, tool, de->event, + &sample, evsel, machine); + +list_del (&de->list); +if (de->allocated) +free (de->event);free (de);if (ret)@@ -1374,6 +1459,7 @@ static int machines__deliver_event(struct machines *machines,return -ENOMEM ; de->event = event; + de->allocated = false;list_add_tail (&de->list, &evlist->deferred_samples);return 0 ; }@@ -2218,6 +2304,8 @@ reader__mmap(struct reader *rd, struct perf_session *session) }if (mmaps[rd->mmap_idx]) { +evlist__copy_deferred_samples (session->evlist, session->tool, + &session->machines.host);munmap (mmaps[rd->mmap_idx], rd->mmap_size); mmaps[rd->mmap_idx] =NULL ; }@@ -2372,6 +2460,11 @@ static int __perf_session__process_events(struct perf_session *session)if (err) goto out_err; err =auxtrace__flush_events (session, tool); +if (err) + goto out_err; + err =evlist__flush_deferred_samples (session->evlist, + session->tool, + &session->machines.host);if (err) goto out_err; err =perf_session__flush_thread_stacks (session);@@ -2494,6 +2587,11 @@ static int __perf_session__process_dir_events(struct perf_session *session)if (ret) goto out_err; + ret =evlist__flush_deferred_samples (session->evlist, tool, + &session->machines.host); +if (ret) + goto out_err; + ret =perf_session__flush_thread_stacks (session); out_err:ui_progress__finish (); --2.52.0.rc1.455.g30608eb744-goog