While collecting samples using perf record, __cmd_record() disables the evlist once recording is complete. After that, event fds are no longer read and any remaining PMU-specific data cannot be drained.
Add a weak arch_perf_record__need_read() hook so architecture code can indicate that more data remains to be collected before events are disabled and closed. When the hook reports pending data, perf record performs another read pass. This allows architectures such as powerpc HTM to drain trace data and associated metadata before the event is closed. Signed-off-by: Athira Rajeev <[email protected]> --- Changes in V2: - V1's callback was responsible for driving the read loop including evlist__enable cycling. Removed that logic - Use bytes written to check if session needs to be continued. - Patch is now 3/6 instead of 3/9. tools/perf/builtin-record.c | 45 +++++++++++++++++++++++++++++++++++++ tools/perf/util/record.h | 3 +++ 2 files changed, 48 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index f58d7e3c7879..ae44e452d148 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -2437,6 +2437,45 @@ static unsigned long record__waking(struct record *rec) return waking; } +/* + * Weak symbol - architecture can override to indicate if more + * data needs to be collected before finishing output. + * + * Returns: 1 if more data exists, 0 if collection is complete + */ +__weak int arch_perf_record__need_read(struct evlist *evlist __maybe_unused) +{ + return 0; /* Default: no arch-specific data to collect */ +} + +static void record__final_data(struct record *rec) +{ + u64 last_bytes_written = 0; + /* + * Collect any remaining architecture-specific data. + * The arch code checks if more data exists, and we do the actual + * reading here since we have access to record__mmap_read_all(). + * This code performs the additional read pass while events are + * still live. + */ + while (arch_perf_record__need_read(rec->evlist)) { + /* If user presses Ctrl+C again during draining, abort cleanly */ + if (done > 1) + break; + + last_bytes_written = rec->bytes_written; + + if (record__mmap_read_all(rec, true) < 0) + break; + + if (rec->bytes_written == last_bytes_written) { + pr_warning("Final data drain made no forward progress.\n"); + break; + } + usleep(100); + } +} + static int __cmd_record(struct record *rec, int argc, const char **argv) { int err; @@ -2451,6 +2490,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) float ratio = 0; enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED; struct perf_env *env; + bool final_data_drained = false; atexit(record__sig_exit); signal(SIGCHLD, sig_handler); @@ -2857,6 +2897,11 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) done = 1; } + if (done && !disabled && !final_data_drained) { + record__final_data(rec); + final_data_drained = true; + } + /* * When perf is starting the traced process, at the end events * die with the process and we wait for that. Thus no need to diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h index 93627c9a7338..56de4f95a836 100644 --- a/tools/perf/util/record.h +++ b/tools/perf/util/record.h @@ -10,6 +10,7 @@ #include "util/target.h" struct option; +struct evlist; struct record_opts { struct target target; @@ -95,4 +96,6 @@ static inline bool record_opts__no_switch_events(const struct record_opts *opts) return opts->record_switch_events_set && !opts->record_switch_events; } +int arch_perf_record__need_read(struct evlist *evlist); + #endif // _PERF_RECORD_H -- 2.43.0
