Skip to content

Commit a8fcbd2

Browse files
ahunter6acmel
authored andcommitted
perf tools: Add FIFO file names as alternative options to --control
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>
1 parent 1f4390d commit a8fcbd2

8 files changed

Lines changed: 101 additions & 14 deletions

File tree

‎tools/perf/Documentation/perf-record.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,9 @@ option. The -e option and this one can be mixed and matched. Events
631631
can be grouped using the {} notation.
632632
endif::HAVE_LIBPFM[]
633633

634+
--control=fifo:ctl-fifo[,ack-fifo]::
634635
--control=fd:ctl-fd[,ack-fd]::
636+
ctl-fifo / ack-fifo are opened and used as ctl-fd / ack-fd as follows.
635637
Listen on ctl-fd descriptor for command to control measurement ('enable': enable events,
636638
'disable': disable events). Measurements can be started with events disabled using
637639
--delay=-1 option. Optionally send control command completion ('ack\n') to ack-fd descriptor

‎tools/perf/Documentation/perf-stat.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ with it. --append may be used here. Examples:
180180
3>results perf stat --log-fd 3 -- $cmd
181181
3>>results perf stat --log-fd 3 --append -- $cmd
182182

183+
--control=fifo:ctl-fifo[,ack-fifo]::
183184
--control=fd:ctl-fd[,ack-fd]::
185+
ctl-fifo / ack-fifo are opened and used as ctl-fd / ack-fd as follows.
184186
Listen on ctl-fd descriptor for command to control measurement ('enable': enable events,
185187
'disable': disable events). Measurements can be started with events disabled using
186188
--delay=-1 option. Optionally send control command completion ('ack\n') to ack-fd descriptor

‎tools/perf/builtin-record.c‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,17 @@ static int parse_control_option(const struct option *opt,
22362236
{
22372237
struct record_opts *opts = opt->value;
22382238

2239-
return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack);
2239+
return evlist__parse_control(str, &opts->ctl_fd, &opts->ctl_fd_ack, &opts->ctl_fd_close);
2240+
}
2241+
2242+
static void close_control_option(struct record_opts *opts)
2243+
{
2244+
if (opts->ctl_fd_close) {
2245+
opts->ctl_fd_close = false;
2246+
close(opts->ctl_fd);
2247+
if (opts->ctl_fd_ack >= 0)
2248+
close(opts->ctl_fd_ack);
2249+
}
22402250
}
22412251

22422252
static void switch_output_size_warn(struct record *rec)
@@ -2578,9 +2588,10 @@ static struct option __record_options[] = {
25782588
"libpfm4 event selector. use 'perf list' to list available events",
25792589
parse_libpfm_events_option),
25802590
#endif
2581-
OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd]",
2591+
OPT_CALLBACK(0, "control", &record.opts, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
25822592
"Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n"
2583-
"\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.",
2593+
"\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
2594+
"\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
25842595
parse_control_option),
25852596
OPT_END()
25862597
};
@@ -2653,12 +2664,14 @@ int cmd_record(int argc, const char **argv)
26532664
!perf_can_record_switch_events()) {
26542665
ui__error("kernel does not support recording context switch events\n");
26552666
parse_options_usage(record_usage, record_options, "switch-events", 0);
2656-
return -EINVAL;
2667+
err = -EINVAL;
2668+
goto out_opts;
26572669
}
26582670

26592671
if (switch_output_setup(rec)) {
26602672
parse_options_usage(record_usage, record_options, "switch-output", 0);
2661-
return -EINVAL;
2673+
err = -EINVAL;
2674+
goto out_opts;
26622675
}
26632676

26642677
if (rec->switch_output.time) {
@@ -2669,8 +2682,10 @@ int cmd_record(int argc, const char **argv)
26692682
if (rec->switch_output.num_files) {
26702683
rec->switch_output.filenames = calloc(sizeof(char *),
26712684
rec->switch_output.num_files);
2672-
if (!rec->switch_output.filenames)
2673-
return -EINVAL;
2685+
if (!rec->switch_output.filenames) {
2686+
err = -EINVAL;
2687+
goto out_opts;
2688+
}
26742689
}
26752690

26762691
/*
@@ -2686,7 +2701,8 @@ int cmd_record(int argc, const char **argv)
26862701
rec->affinity_mask.bits = bitmap_alloc(rec->affinity_mask.nbits);
26872702
if (!rec->affinity_mask.bits) {
26882703
pr_err("Failed to allocate thread mask for %zd cpus\n", rec->affinity_mask.nbits);
2689-
return -ENOMEM;
2704+
err = -ENOMEM;
2705+
goto out_opts;
26902706
}
26912707
pr_debug2("thread mask[%zd]: empty\n", rec->affinity_mask.nbits);
26922708
}
@@ -2817,6 +2833,8 @@ int cmd_record(int argc, const char **argv)
28172833
evlist__delete(rec->evlist);
28182834
symbol__exit();
28192835
auxtrace_record__free(rec->itr);
2836+
out_opts:
2837+
close_control_option(&rec->opts);
28202838
return err;
28212839
}
28222840

‎tools/perf/builtin-stat.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,17 @@ static int parse_control_option(const struct option *opt,
10471047
{
10481048
struct perf_stat_config *config = opt->value;
10491049

1050-
return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack);
1050+
return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack, &config->ctl_fd_close);
1051+
}
1052+
1053+
static void close_control_option(struct perf_stat_config *config)
1054+
{
1055+
if (config->ctl_fd_close) {
1056+
config->ctl_fd_close = false;
1057+
close(config->ctl_fd);
1058+
if (config->ctl_fd_ack >= 0)
1059+
close(config->ctl_fd_ack);
1060+
}
10511061
}
10521062

10531063
static struct option stat_options[] = {
@@ -1153,9 +1163,10 @@ static struct option stat_options[] = {
11531163
"libpfm4 event selector. use 'perf list' to list available events",
11541164
parse_libpfm_events_option),
11551165
#endif
1156-
OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd]",
1166+
OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
11571167
"Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n"
1158-
"\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.",
1168+
"\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
1169+
"\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
11591170
parse_control_option),
11601171
OPT_END()
11611172
};
@@ -2398,6 +2409,7 @@ int cmd_stat(int argc, const char **argv)
23982409

23992410
metricgroup__rblist_exit(&stat_config.metric_events);
24002411
runtime_stat_delete(&stat_config);
2412+
close_control_option(&stat_config);
24012413

24022414
return status;
24032415
}

‎tools/perf/util/evlist.c‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,12 +1727,63 @@ struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
17271727
return leader;
17281728
}
17291729

1730-
int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack)
1730+
static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1731+
{
1732+
char *s, *p;
1733+
int ret = 0, fd;
1734+
1735+
if (strncmp(str, "fifo:", 5))
1736+
return -EINVAL;
1737+
1738+
str += 5;
1739+
if (!*str || *str == ',')
1740+
return -EINVAL;
1741+
1742+
s = strdup(str);
1743+
if (!s)
1744+
return -ENOMEM;
1745+
1746+
p = strchr(s, ',');
1747+
if (p)
1748+
*p = '\0';
1749+
1750+
/*
1751+
* O_RDWR avoids POLLHUPs which is necessary to allow the other
1752+
* end of a FIFO to be repeatedly opened and closed.
1753+
*/
1754+
fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1755+
if (fd < 0) {
1756+
pr_err("Failed to open '%s'\n", s);
1757+
ret = -errno;
1758+
goto out_free;
1759+
}
1760+
*ctl_fd = fd;
1761+
*ctl_fd_close = true;
1762+
1763+
if (p && *++p) {
1764+
/* O_RDWR | O_NONBLOCK means the other end need not be open */
1765+
fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1766+
if (fd < 0) {
1767+
pr_err("Failed to open '%s'\n", p);
1768+
ret = -errno;
1769+
goto out_free;
1770+
}
1771+
*ctl_fd_ack = fd;
1772+
}
1773+
1774+
out_free:
1775+
free(s);
1776+
return ret;
1777+
}
1778+
1779+
int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
17311780
{
17321781
char *comma = NULL, *endptr = NULL;
17331782

1783+
*ctl_fd_close = false;
1784+
17341785
if (strncmp(str, "fd:", 3))
1735-
return -EINVAL;
1786+
return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
17361787

17371788
*ctl_fd = strtoul(&str[3], &endptr, 0);
17381789
if (endptr == &str[3])

‎tools/perf/util/evlist.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ enum evlist_ctl_cmd {
373373
EVLIST_CTL_CMD_ACK
374374
};
375375

376-
int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack);
376+
int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close);
377377
int evlist__initialize_ctlfd(struct evlist *evlist, int ctl_fd, int ctl_fd_ack);
378378
int evlist__finalize_ctlfd(struct evlist *evlist);
379379
bool evlist__ctlfd_initialized(struct evlist *evlist);

‎tools/perf/util/record.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct record_opts {
7373
unsigned int nr_threads_synthesize;
7474
int ctl_fd;
7575
int ctl_fd_ack;
76+
bool ctl_fd_close;
7677
};
7778

7879
extern const char * const *record_usage;

‎tools/perf/util/stat.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ struct perf_stat_config {
136136
struct rblist metric_events;
137137
int ctl_fd;
138138
int ctl_fd_ack;
139+
bool ctl_fd_close;
139140
};
140141

141142
void perf_stat__set_big_num(int set);

0 commit comments

Comments
 (0)