perf tools: Allow to enable/disable events via control file
authorJiri Olsa <jolsa@kernel.org>
Sat, 26 Dec 2020 23:20:35 +0000 (00:20 +0100)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 20 Jan 2021 17:34:21 +0000 (14:34 -0300)
Adding new control events to enable/disable specific event.
The interface string for control file are:

  'enable <EVENT NAME>'
  'disable <EVENT NAME>'

when received the command, perf will scan the current evlist
for <EVENT NAME> and if found it's enabled/disabled.

Example session:

  terminal 1:
    # mkfifo control ack perf.pipe
    # perf record --control=fifo:control,ack -D -1 --no-buffering -e 'sched:*' -o - > perf.pipe

  terminal 2:
    # cat perf.pipe | perf --no-pager script -i -

  terminal 1:
    Events disabled

  NOTE Above message will show only after read side of the pipe ('>')
  is started on 'terminal 2'. The 'terminal 1's bash does not execute
  perf before that, hence the delyaed perf record message.

  terminal 3:
    # echo 'enable sched:sched_process_fork' > control

  terminal 1:
    event sched:sched_process_fork enabled

  terminal 2:
    bash 33349 [034] 149587.674295: sched:sched_process_fork: comm=bash pid=33349 child_comm=bash child_pid=34056
    bash 33349 [034] 149588.239521: sched:sched_process_fork: comm=bash pid=33349 child_comm=bash child_pid=34057

  terminal 3:
    # echo 'enable sched:sched_wakeup_new' > control

  terminal 1:
    event sched:sched_wakeup_new enabled

  terminal 2:
    bash 33349 [034] 149632.228023: sched:sched_process_fork: comm=bash pid=33349 child_comm=bash child_pid=34059
    bash 33349 [034] 149632.228050:   sched:sched_wakeup_new: bash:34059 [120] success=1 CPU:036
    bash 33349 [034] 149633.950005: sched:sched_process_fork: comm=bash pid=33349 child_comm=bash child_pid=34060
    bash 33349 [034] 149633.950030:   sched:sched_wakeup_new: bash:34060 [120] success=1 CPU:036

Committer testing:

If I use 'sched:*' and then enable all events, I can't get 'perf record'
to react to further commands, so I tested it with:

  [root@five ~]# perf record --control=fifo:control,ack -D -1 --no-buffering -e 'sched:sched_process_*' -o - > perf.pipe
  Events disabled
  Events enabled
  Events disabled

And then it works as expected, so we need to fix this pre-existing
problem.

Another issue, we need to check if a event is already enabled or
disabled and change the message to be clearer, i.e.:

  [root@five ~]# perf record --control=fifo:control,ack -D -1 --no-buffering -e 'sched:sched_process_*' -o - > perf.pipe
  Events disabled

If we receive a 'disable' command, then it should say:

  [root@five ~]# perf record --control=fifo:control,ack -D -1 --no-buffering -e 'sched:sched_process_*' -o - > perf.pipe
  Events disabled
  Events already disabled

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexei Budankov <abudankov@huawei.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/20201226232038.390883-2-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/Documentation/perf-record.txt
tools/perf/builtin-record.c
tools/perf/builtin-stat.c
tools/perf/util/evlist.c

index 9087b22..5ab78dc 100644 (file)
@@ -646,9 +646,11 @@ ctl-fifo / ack-fifo are opened and used as ctl-fd / ack-fd as follows.
 Listen on ctl-fd descriptor for command to control measurement.
 
 Available commands:
-  'enable'  : enable events
-  'disable' : disable events
-  'snapshot': AUX area tracing snapshot).
+  'enable'       : enable events
+  'disable'      : disable events
+  'enable name'  : enable event 'name'
+  'disable name' : disable event 'name'
+  'snapshot'     : AUX area tracing snapshot).
 
 Measurements can be started with events disabled using --delay=-1 option. Optionally
 send control command completion ('ack\n') to ack-fd descriptor to synchronize with the
index 7704c33..71061a9 100644 (file)
@@ -1938,18 +1938,14 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 
                if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) {
                        switch (cmd) {
-                       case EVLIST_CTL_CMD_ENABLE:
-                               pr_info(EVLIST_ENABLED_MSG);
-                               break;
-                       case EVLIST_CTL_CMD_DISABLE:
-                               pr_info(EVLIST_DISABLED_MSG);
-                               break;
                        case EVLIST_CTL_CMD_SNAPSHOT:
                                hit_auxtrace_snapshot_trigger(rec);
                                evlist__ctlfd_ack(rec->evlist);
                                break;
                        case EVLIST_CTL_CMD_ACK:
                        case EVLIST_CTL_CMD_UNSUPPORTED:
+                       case EVLIST_CTL_CMD_ENABLE:
+                       case EVLIST_CTL_CMD_DISABLE:
                        default:
                                break;
                        }
index 3c054b8..3ec1236 100644 (file)
@@ -611,14 +611,12 @@ static void process_evlist(struct evlist *evlist, unsigned int interval)
        if (evlist__ctlfd_process(evlist, &cmd) > 0) {
                switch (cmd) {
                case EVLIST_CTL_CMD_ENABLE:
-                       pr_info(EVLIST_ENABLED_MSG);
                        if (interval)
                                process_interval();
                        break;
                case EVLIST_CTL_CMD_DISABLE:
                        if (interval)
                                process_interval();
-                       pr_info(EVLIST_DISABLED_MSG);
                        break;
                case EVLIST_CTL_CMD_SNAPSHOT:
                case EVLIST_CTL_CMD_ACK:
index 05363a7..c71c7e0 100644 (file)
@@ -1957,6 +1957,64 @@ int evlist__ctlfd_ack(struct evlist *evlist)
        return err;
 }
 
+static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
+{
+       char *data = cmd_data + cmd_size;
+
+       /* no argument */
+       if (!*data)
+               return 0;
+
+       /* there's argument */
+       if (*data == ' ') {
+               *arg = data + 1;
+               return 1;
+       }
+
+       /* malformed */
+       return -1;
+}
+
+static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
+{
+       struct evsel *evsel;
+       char *name;
+       int err;
+
+       err = get_cmd_arg(cmd_data,
+                         enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
+                                  sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
+                         &name);
+       if (err < 0) {
+               pr_info("failed: wrong command\n");
+               return -1;
+       }
+
+       if (err) {
+               evsel = evlist__find_evsel_by_str(evlist, name);
+               if (evsel) {
+                       if (enable)
+                               evlist__enable_evsel(evlist, name);
+                       else
+                               evlist__disable_evsel(evlist, name);
+                       pr_info("Event %s %s\n", evsel->name,
+                               enable ? "enabled" : "disabled");
+               } else {
+                       pr_info("failed: can't find '%s' event\n", name);
+               }
+       } else {
+               if (enable) {
+                       evlist__enable(evlist);
+                       pr_info(EVLIST_ENABLED_MSG);
+               } else {
+                       evlist__disable(evlist);
+                       pr_info(EVLIST_DISABLED_MSG);
+               }
+       }
+
+       return 0;
+}
+
 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
 {
        int err = 0;
@@ -1973,10 +2031,9 @@ int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
                if (err > 0) {
                        switch (*cmd) {
                        case EVLIST_CTL_CMD_ENABLE:
-                               evlist__enable(evlist);
-                               break;
                        case EVLIST_CTL_CMD_DISABLE:
-                               evlist__disable(evlist);
+                               err = evlist__ctlfd_enable(evlist, cmd_data,
+                                                          *cmd == EVLIST_CTL_CMD_ENABLE);
                                break;
                        case EVLIST_CTL_CMD_SNAPSHOT:
                                break;