Skip to content

Commit 899e5ff

Browse files
committed
perf record: Introduce --switch-output-event
Now we can use it with --overwrite to have a flight recorder mode that gets snapshot requests from arbitrary events that are processed in the side band thread together with the PERF_RECORD_BPF_EVENT processing. Example: To collect scheduler events until a recvmmsg syscall happens, system wide: [root@five a]# rm -f perf.data.2020042717* [root@five a]# perf record --overwrite -e sched:*switch,syscalls:*recvmmsg --switch-output-event syscalls:sys_enter_recvmmsg [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2020042717585458 ] [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2020042717590235 ] [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2020042717590398 ] ^C[ perf record: Woken up 1 times to write data ] [ perf record: Dump perf.data.2020042717590511 ] [ perf record: Captured and wrote 7.244 MB perf.data.<timestamp> ] So in the above case we had 3 snapshots, the fourth was forced by control+C: [root@five a]# ls -la total 20440 drwxr-xr-x. 2 root root 4096 Apr 27 17:59 . dr-xr-x---. 12 root root 4096 Apr 27 17:46 .. -rw-------. 1 root root 3936125 Apr 27 17:58 perf.data.2020042717585458 -rw-------. 1 root root 5074869 Apr 27 17:59 perf.data.2020042717590235 -rw-------. 1 root root 4291037 Apr 27 17:59 perf.data.2020042717590398 -rw-------. 1 root root 7617037 Apr 27 17:59 perf.data.2020042717590511 [root@five a]# One can make this more precise by adding the switch output event to the main -e events list, as since this is done asynchronously, a few events after the signal event will appear in the snapshots, as can be seen with: [root@five a]# rm -f perf.data.20200427175* [root@five a]# perf record --overwrite -e sched:*switch,syscalls:*recvmmsg --switch-output-event syscalls:sys_enter_recvmmsg [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2020042718024203 ] [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2020042718024301 ] [ perf record: dump data: Woken up 1 times ] [ perf record: Dump perf.data.2020042718024484 ] ^C[ perf record: Woken up 1 times to write data ] [ perf record: Dump perf.data.2020042718024562 ] [ perf record: Captured and wrote 7.337 MB perf.data.<timestamp> ] [root@five a]# perf script -i perf.data.2020042718024203 | tail -15 PacerThread 148586 [005] 122.830729: sched:sched_switch: prev_comm=PacerThread prev_pid=148586... swapper 0 [000] 122.833588: sched:sched_switch: prev_comm=swapper/0 prev_pid=... NetworkManager 1251 [000] 122.833619: syscalls:sys_enter_recvmmsg: fd: 0x0000001c, mmsg: 0x7ffe83054a1... swapper 0 [002] 122.833624: sched:sched_switch: prev_comm=swapper/2 prev_pid=... swapper 0 [003] 122.833624: sched:sched_switch: prev_comm=swapper/3 prev_pid=... NetworkManager 1251 [000] 122.833626: syscalls:sys_exit_recvmmsg: 0x1 kworker/3:3-eve 158946 [003] 122.833628: sched:sched_switch: prev_comm=kworker/3:3 prev_pid=15894... swapper 0 [004] 122.833641: sched:sched_switch: prev_comm=swapper/4 prev_pid=... NetworkManager 1251 [000] 122.833642: sched:sched_switch: prev_comm=NetworkManage... perf 228273 [002] 122.833645: sched:sched_switch: prev_comm=perf prev_pid=22827... swapper 0 [011] 122.833646: sched:sched_switch: prev_comm=swapper/1... swapper 0 [002] 122.833648: sched:sched_switch: prev_comm=swapper/... kworker/0:2-eve 207387 [000] 122.833648: sched:sched_switch: prev_comm=kworker/0:2 prev_pid=20738... kworker/2:3-eve 232038 [002] 122.833652: sched:sched_switch: prev_comm=kworker/2:3 prev_pid=23203... perf 235825 [003] 122.833653: sched:sched_switch: prev_comm=perf prev_pid=23582... [root@five a]# Acked-by: Jiri Olsa <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Song Liu <[email protected]> Cc: Wang Nan <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 636eb4d commit 899e5ff

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

tools/perf/Documentation/perf-record.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,19 @@ overhead. You can still switch them on with:
556556

557557
--switch-output --no-no-buildid --no-no-buildid-cache
558558

559+
--switch-output-event::
560+
Events that will cause the switch of the perf.data file, auto-selecting
561+
--switch-output=signal, the results are similar as internally the side band
562+
thread will also send a SIGUSR2 to the main one.
563+
564+
Uses the same syntax as --event, it will just not be recorded, serving only to
565+
switch the perf.data file as soon as the --switch-output event is processed by
566+
a separate sideband thread.
567+
568+
This sideband thread is also used to other purposes, like processing the
569+
PERF_RECORD_BPF_EVENT records as they happen, asking the kernel for extra BPF
570+
information, etc.
571+
559572
--switch-max-files=N::
560573

561574
When rotating perf.data with --switch-output, only keep N files.

tools/perf/builtin-record.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ struct record {
8888
struct evlist *evlist;
8989
struct perf_session *session;
9090
struct evlist *sb_evlist;
91+
pthread_t thread_id;
9192
int realtime_prio;
93+
bool switch_output_event_set;
9294
bool no_buildid;
9395
bool no_buildid_set;
9496
bool no_buildid_cache;
@@ -1437,6 +1439,13 @@ static int record__synthesize(struct record *rec, bool tail)
14371439
return err;
14381440
}
14391441

1442+
static int record__process_signal_event(union perf_event *event __maybe_unused, void *data)
1443+
{
1444+
struct record *rec = data;
1445+
pthread_kill(rec->thread_id, SIGUSR2);
1446+
return 0;
1447+
}
1448+
14401449
static int __cmd_record(struct record *rec, int argc, const char **argv)
14411450
{
14421451
int err;
@@ -1581,12 +1590,24 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
15811590
goto out_child;
15821591
}
15831592

1584-
if (!opts->no_bpf_event) {
1585-
rec->sb_evlist = evlist__new();
1593+
if (rec->sb_evlist != NULL) {
1594+
/*
1595+
* We get here if --switch-output-event populated the
1596+
* sb_evlist, so associate a callback that will send a SIGUSR2
1597+
* to the main thread.
1598+
*/
1599+
evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec);
1600+
rec->thread_id = pthread_self();
1601+
}
15861602

1603+
if (!opts->no_bpf_event) {
15871604
if (rec->sb_evlist == NULL) {
1588-
pr_err("Couldn't create side band evlist.\n.");
1589-
goto out_child;
1605+
rec->sb_evlist = evlist__new();
1606+
1607+
if (rec->sb_evlist == NULL) {
1608+
pr_err("Couldn't create side band evlist.\n.");
1609+
goto out_child;
1610+
}
15901611
}
15911612

15921613
if (evlist__add_bpf_sb_event(rec->sb_evlist, &session->header.env)) {
@@ -2180,10 +2201,19 @@ static int switch_output_setup(struct record *rec)
21802201
};
21812202
unsigned long val;
21822203

2204+
/*
2205+
* If we're using --switch-output-events, then we imply its
2206+
* --switch-output=signal, as we'll send a SIGUSR2 from the side band
2207+
* thread to its parent.
2208+
*/
2209+
if (rec->switch_output_event_set)
2210+
goto do_signal;
2211+
21832212
if (!s->set)
21842213
return 0;
21852214

21862215
if (!strcmp(s->str, "signal")) {
2216+
do_signal:
21872217
s->signal = true;
21882218
pr_debug("switch-output with SIGUSR2 signal\n");
21892219
goto enabled;
@@ -2441,6 +2471,9 @@ static struct option __record_options[] = {
24412471
&record.switch_output.set, "signal or size[BKMG] or time[smhd]",
24422472
"Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
24432473
"signal"),
2474+
OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event",
2475+
"switch output event selector. use 'perf list' to list available events",
2476+
parse_events_option_new_evlist),
24442477
OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
24452478
"Limit number of switch output generated files"),
24462479
OPT_BOOLEAN(0, "dry-run", &dry_run,

0 commit comments

Comments
 (0)