Skip to contentSkip to conversation

perf record: Add an option to take an AUX snapshot on exit

PATCH In review Alexander Shishkin 1 participant 2 messages May 11, 2017 – Aug 6, 2019 perf events

lore
Share & tools
lore

Alexander Shishkin

author reviewer
#0 lore raw

PATCH 1/1 perf record: Add an option to take an AUX snapshot on exit

It is sometimes useful to generate a snapshot when perf record exits; I've been using a wrapper script around the workload that would do a killall -USR2 perf when the workload exits.

This patch makes it easier and also works when perf record is attached to a pre-existing task. A new snapshot option 'e' can be specified in -S to enable this behavior:

root@elsewhere:~# perf record -e intel_pt// -Se sleep 1
[ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.085 MB perf.data ]
---
 tools/perf/Documentation/perf-record.txt | 11 ++++++++---
 tools/perf/builtin-record.c              |  4 ++++
 tools/perf/perf.h                        |  1 +
 tools/perf/util/auxtrace.c               | 10 ++++++++++
 4 files changed, 23 insertions(+), 3 deletions(-)
tools/perf/Documentation/perf-record.txt +8 -3
index ea3789d05e..164ffce680 100644
@@ -387,9 +387,14 @@ CLOCK_BOOTTIME, CLOCK_REALTIME and CLOCK_TAI.
 -S::
 --snapshot::
 Select AUX area tracing Snapshot Mode. This option is valid only with an
-AUX area tracing event. Optionally the number of bytes to capture per
-snapshot can be specified. In Snapshot Mode, trace data is captured only when
-signal SIGUSR2 is received.
+AUX area tracing event. Optionally, certain snapshot capturing parameters
+can be specified in a string that follows this option:
+  'e': take one last snapshot on exit; guarantees that there is at least one
+       snapshot in the output file;
+  <size>: if the PMU supports this, specify the desired snapshot size.
+
+In Snapshot Mode trace data is captured only when signal SIGUSR2 is received
+and on exit if the above 'e' option is given.
 
 --proc-map-timeout::
 When processing pre-existing threads /proc/XXX/mmap, it may take a long time,
tools/perf/builtin-record.c +4 -0
index 70340ff200..42d1b7aeee 100644
@@ -1136,6 +1136,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			disabled = true;
 		}
 	}
+
+	if (opts->auxtrace_snapshot_on_exit)
+		record__read_auxtrace_snapshot(rec);
+
 	trigger_off(&auxtrace_snapshot_trigger);
 	trigger_off(&switch_output_trigger);
 
tools/perf/perf.h +1 -0
index 806c216a10..b79c57485b 100644
@@ -50,6 +50,7 @@ struct record_opts {
 	bool	     running_time;
 	bool	     full_auxtrace;
 	bool	     auxtrace_snapshot_mode;
+	bool	     auxtrace_snapshot_on_exit;
 	bool	     record_namespaces;
 	bool	     record_switch_events;
 	bool	     all_kernel;
tools/perf/util/auxtrace.c +10 -0
index 0daf63b9ee..25ad4445b0 100644
@@ -564,6 +564,16 @@ int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
 	if (!str)
 		return 0;
 
+	/* PMU-agnostic options */
+	switch (*str) {
+	case 'e':
+		opts->auxtrace_snapshot_on_exit = true;
+		str++;
+		break;
+	default:
+		break;
+	}
+
 	if (itr)
 		return itr->parse_snapshot_options(itr, opts, str);
 
Signature · 1 line
2.11.0
author reviewer
#1 lore raw
Quoted 92 lines · Alexander Shishkin

It is sometimes useful to generate a snapshot when perf record exits; I've been using a wrapper script around the workload that would do a killall -USR2 perf when the workload exits.

This patch makes it easier and also works when perf record is attached to a pre-existing task. A new snapshot option 'e' can be specified in -S to enable this behavior:

root@elsewhere:~# perf record -e intel_pt// -Se sleep 1
[ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.085 MB perf.data ]
---
 tools/perf/Documentation/perf-record.txt | 11 ++++++++---
 tools/perf/builtin-record.c              |  4 ++++
 tools/perf/perf.h                        |  1 +
 tools/perf/util/auxtrace.c               | 10 ++++++++++
 4 files changed, 23 insertions(+), 3 deletions(-)
tools/perf/Documentation/perf-record.txt +8 -3
index ea3789d05e..164ffce680 100644
@@ -387,9 +387,14 @@ CLOCK_BOOTTIME, CLOCK_REALTIME and CLOCK_TAI.
 -S::
 --snapshot::
 Select AUX area tracing Snapshot Mode. This option is valid only with an
-AUX area tracing event. Optionally the number of bytes to capture per
-snapshot can be specified. In Snapshot Mode, trace data is captured only when
-signal SIGUSR2 is received.
+AUX area tracing event. Optionally, certain snapshot capturing parameters
+can be specified in a string that follows this option:
+  'e': take one last snapshot on exit; guarantees that there is at least one
+       snapshot in the output file;
+  <size>: if the PMU supports this, specify the desired snapshot size.
+
+In Snapshot Mode trace data is captured only when signal SIGUSR2 is received
+and on exit if the above 'e' option is given.
 
 --proc-map-timeout::
 When processing pre-existing threads /proc/XXX/mmap, it may take a long time,
tools/perf/builtin-record.c +4 -0
index 70340ff200..42d1b7aeee 100644
@@ -1136,6 +1136,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			disabled = true;
 		}
 	}
+
+	if (opts->auxtrace_snapshot_on_exit)
+		record__read_auxtrace_snapshot(rec);
+
 	trigger_off(&auxtrace_snapshot_trigger);
 	trigger_off(&switch_output_trigger);
 
tools/perf/perf.h +1 -0
index 806c216a10..b79c57485b 100644
@@ -50,6 +50,7 @@ struct record_opts {
 	bool	     running_time;
 	bool	     full_auxtrace;
 	bool	     auxtrace_snapshot_mode;
+	bool	     auxtrace_snapshot_on_exit;
 	bool	     record_namespaces;
 	bool	     record_switch_events;
 	bool	     all_kernel;
tools/perf/util/auxtrace.c +10 -0
index 0daf63b9ee..25ad4445b0 100644
@@ -564,6 +564,16 @@ int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
 	if (!str)
 		return 0;
 
+	/* PMU-agnostic options */
+	switch (*str) {
+	case 'e':
+		opts->auxtrace_snapshot_on_exit = true;
+		str++;
+		break;
+	default:
+		break;
+	}
+
 	if (itr)
 		return itr->parse_snapshot_options(itr, opts, str);
 
Signature · 1 line
2.11.0