perf tools: Add FIFO file names as alternative options to --control
authorAdrian Hunter <adrian.hunter@intel.com>
Wed, 2 Sep 2020 10:57:07 +0000 (13:57 +0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 4 Sep 2020 17:38:15 +0000 (14:38 -0300)
Enable the --control option to accept file names as an alternative to
file descriptors.

Example:

  $ mkfifo perf.control
  $ mkfifo perf.ack
  $ cat perf.ack &
  [1] 6808
  $ perf record --control fifo:perf.control,perf.ack -- sleep 300 &
  [2] 6810
  $ echo disable > perf.control
  $ Events disabled
  ack

  $ echo enable > perf.control
  $ Events enabled
  ack

  $ echo disable > perf.control
  $ Events disabled
  ack

  $ kill %2
  [ perf record: Woken up 4 times to write data ]
  $ [ perf record: Captured and wrote 0.018 MB perf.data (7 samples) ]

  [1]-  Done                    cat perf.ack
  [2]+  Terminated              perf record --control fifo:perf.control,perf.ack -- sleep 300
  $

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Acked-by: Alexey Budankov <alexey.budankov@linux.intel.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: http://lore.kernel.org/lkml/20200902105707.11491-1-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/Documentation/perf-record.txt
tools/perf/Documentation/perf-stat.txt
tools/perf/builtin-record.c
tools/perf/builtin-stat.c
tools/perf/util/evlist.c
tools/perf/util/evlist.h
tools/perf/util/record.h
tools/perf/util/stat.h

index 34c80aa559481310008a0140a7bce55358da52f3..c1f44633abed8025e6f106fa19f3fabc9b37bc73 100644 (file)
@@ -631,7 +631,9 @@ option. The -e option and this one can be mixed and matched.  Events
 can be grouped using the {} notation.
 endif::HAVE_LIBPFM[]
 
+--control=fifo:ctl-fifo[,ack-fifo]::
 --control=fd:ctl-fd[,ack-fd]::
+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 ('enable': enable events,
 'disable': disable events). Measurements can be started with events disabled using
 --delay=-1 option. Optionally send control command completion ('ack\n') to ack-fd descriptor
index 224795e97b1cc9b32abafbc9679346685535436d..7d18694e592af26a29fdeabf032deb7a17b1091d 100644 (file)
@@ -180,7 +180,9 @@ with it.  --append may be used here.  Examples:
      3>results  perf stat --log-fd 3          -- $cmd
      3>>results perf stat --log-fd 3 --append -- $cmd
 
+--control=fifo:ctl-fifo[,ack-fifo]::
 --control=fd:ctl-fd[,ack-fd]::
+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 ('enable': enable events,
 'disable': disable events). Measurements can be started with events disabled using
 --delay=-1 option. Optionally send control command completion ('ack\n') to ack-fd descriptor
index 50591e07aa4fdc31e202277ff8d7325069b69ec3..c83aec4940de7594656184e494a1753f30933a4d 100644 (file)
@@ -2236,7 +2236,17 @@ static int parse_control_option(const struct option *opt,
 {
        struct record_opts *opts = opt->value;
 
-       return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack);
+       return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close);
+}
+
+static void close_control_option(struct record_opts *opts)
+{
+       if (opts->ctl_fd_close) {
+               opts->ctl_fd_close = false;
+               close(opts->ctl_fd);
+               if (opts->ctl_fd_ack >= 0)
+                       close(opts->ctl_fd_ack);
+       }
 }
 
 static void switch_output_size_warn(struct record *rec)
@@ -2578,9 +2588,10 @@ static struct option __record_options[] = {
                "libpfm4 event selector. use 'perf list' to list available events",
                parse_libpfm_events_option),
 #endif
-       OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd]",
+       OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
                     "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n"
-                    "\t\t\t  Optionally send control command completion ('ack\\n') to ack-fd descriptor.",
+                    "\t\t\t  Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
+                    "\t\t\t  Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
                      parse_control_option),
        OPT_END()
 };
@@ -2653,12 +2664,14 @@ int cmd_record(int argc, const char **argv)
            !perf_can_record_switch_events()) {
                ui__error("kernel does not support recording context switch events\n");
                parse_options_usage(record_usage, record_options, "switch-events", 0);
-               return -EINVAL;
+               err = -EINVAL;
+               goto out_opts;
        }
 
        if (switch_output_setup(rec)) {
                parse_options_usage(record_usage, record_options, "switch-output", 0);
-               return -EINVAL;
+               err = -EINVAL;
+               goto out_opts;
        }
 
        if (rec->switch_output.time) {
@@ -2669,8 +2682,10 @@ int cmd_record(int argc, const char **argv)
        if (rec->switch_output.num_files) {
                rec->switch_output.filenames = calloc(sizeof(char *),
                                                      rec->switch_output.num_files);
-               if (!rec->switch_output.filenames)
-                       return -EINVAL;
+               if (!rec->switch_output.filenames) {
+                       err = -EINVAL;
+                       goto out_opts;
+               }
        }
 
        /*
@@ -2686,7 +2701,8 @@ int cmd_record(int argc, const char **argv)
                rec->affinity_mask.bits = bitmap_alloc(rec->affinity_mask.nbits);
                if (!rec->affinity_mask.bits) {
                        pr_err("Failed to allocate thread mask for %zd cpus\n", rec->affinity_mask.nbits);
-                       return -ENOMEM;
+                       err = -ENOMEM;
+                       goto out_opts;
                }
                pr_debug2("thread mask[%zd]: empty\n", rec->affinity_mask.nbits);
        }
@@ -2817,6 +2833,8 @@ out:
        evlist__delete(rec->evlist);
        symbol__exit();
        auxtrace_record__free(rec->itr);
+out_opts:
+       close_control_option(&rec->opts);
        return err;
 }
 
index ea072d47c5601afd27e25941b3dc90ef400da266..14688710195cb8e77cd98086032f02b48c3188da 100644 (file)
@@ -1047,7 +1047,17 @@ static int parse_control_option(const struct option *opt,
 {
        struct perf_stat_config *config = opt->value;
 
-       return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack);
+       return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack, &config->ctl_fd_close);
+}
+
+static void close_control_option(struct perf_stat_config *config)
+{
+       if (config->ctl_fd_close) {
+               config->ctl_fd_close = false;
+               close(config->ctl_fd);
+               if (config->ctl_fd_ack >= 0)
+                       close(config->ctl_fd_ack);
+       }
 }
 
 static struct option stat_options[] = {
@@ -1153,9 +1163,10 @@ static struct option stat_options[] = {
                "libpfm4 event selector. use 'perf list' to list available events",
                parse_libpfm_events_option),
 #endif
-       OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd]",
+       OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
                     "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n"
-                    "\t\t\t  Optionally send control command completion ('ack\\n') to ack-fd descriptor.",
+                    "\t\t\t  Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
+                    "\t\t\t  Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
                      parse_control_option),
        OPT_END()
 };
@@ -2398,6 +2409,7 @@ out:
 
        metricgroup__rblist_exit(&stat_config.metric_events);
        runtime_stat_delete(&stat_config);
+       close_control_option(&stat_config);
 
        return status;
 }
index 47d1045a19af50a63bf7ed3bef3fd10835b2e504..00593e5f2a9d58c32775d39d72227c9bbc82309c 100644 (file)
@@ -1727,12 +1727,63 @@ struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
        return leader;
 }
 
-int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack)
+static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
+{
+       char *s, *p;
+       int ret = 0, fd;
+
+       if (strncmp(str, "fifo:", 5))
+               return -EINVAL;
+
+       str += 5;
+       if (!*str || *str == ',')
+               return -EINVAL;
+
+       s = strdup(str);
+       if (!s)
+               return -ENOMEM;
+
+       p = strchr(s, ',');
+       if (p)
+               *p = '\0';
+
+       /*
+        * O_RDWR avoids POLLHUPs which is necessary to allow the other
+        * end of a FIFO to be repeatedly opened and closed.
+        */
+       fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
+       if (fd < 0) {
+               pr_err("Failed to open '%s'\n", s);
+               ret = -errno;
+               goto out_free;
+       }
+       *ctl_fd = fd;
+       *ctl_fd_close = true;
+
+       if (p && *++p) {
+               /* O_RDWR | O_NONBLOCK means the other end need not be open */
+               fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
+               if (fd < 0) {
+                       pr_err("Failed to open '%s'\n", p);
+                       ret = -errno;
+                       goto out_free;
+               }
+               *ctl_fd_ack = fd;
+       }
+
+out_free:
+       free(s);
+       return ret;
+}
+
+int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
 {
        char *comma = NULL, *endptr = NULL;
 
+       *ctl_fd_close = false;
+
        if (strncmp(str, "fd:", 3))
-               return -EINVAL;
+               return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
 
        *ctl_fd = strtoul(&str[3], &endptr, 0);
        if (endptr == &str[3])
index a5a5a07d5c55d9232de985510fd7c97f1729031e..a5678eb5ee6012c3bff8a06dea073f140b72db94 100644 (file)
@@ -373,7 +373,7 @@ enum evlist_ctl_cmd {
        EVLIST_CTL_CMD_ACK
 };
 
-int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack);
+int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close);
 int evlist__initialize_ctlfd(struct evlist *evlist, int ctl_fd, int ctl_fd_ack);
 int evlist__finalize_ctlfd(struct evlist *evlist);
 bool evlist__ctlfd_initialized(struct evlist *evlist);
index 03678ff255391eb23b4c2f31739b2d9cf79f51a2..266760ac9143ff72bce6dcde0a018316df971b1d 100644 (file)
@@ -73,6 +73,7 @@ struct record_opts {
        unsigned int  nr_threads_synthesize;
        int           ctl_fd;
        int           ctl_fd_ack;
+       bool          ctl_fd_close;
 };
 
 extern const char * const *record_usage;
index aa3bed48511b328957cfb7c6bd6b6ac5c013a681..9911fc6adbfdd7e843be3212f051cc984545b790 100644 (file)
@@ -136,6 +136,7 @@ struct perf_stat_config {
        struct rblist            metric_events;
        int                      ctl_fd;
        int                      ctl_fd_ack;
+       bool                     ctl_fd_close;
 };
 
 void perf_stat__set_big_num(int set);