Commit fc3e4d07 authored by Stephane Eranian's avatar Stephane Eranian Committed by Arnaldo Carvalho de Melo

perf stat: Fix default output file

The following commit:

commit 56f3bae7
Author: Jim Cromie <jim.cromie@gmail.com>
Date:   Wed Sep 7 17:14:00 2011 -0600

    perf stat: Add --log-fd <N> option to redirect stderr elsewhere

introduced a bug in the way perf stat outputs the results by default,
i.e., without the --log-fd or --output option. It would default to
writing to file descriptor 0, i.e., stdin. Writing to stdin is allowed
and is equivalent to writing to stdout. However, there is a major
difference for any script that was already capturing the output of perf
stat via redirection:

    perf stat >/tmp/log .... or perf stat 2>/tmp/log ....

They would not capture anything anymore. They would have to do:
    perf stat 0>/tmp/log ...

This breaks compatibility with existing scripts and does not look very
natural.

This patch fixes the problem by looking at output_fd only when it was
modified by user (> 0). It also checks that the value if positive.
Passing --log-fd 0 is ignored.

I would also argue that defaulting to stderr for the results is not the
right thing to do, though this patch does not address this specific
issue.
Signed-off-by: default avatarStephane Eranian <eranian@google.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Jim Cromie <jim.cromie@gmail.com>
Link: http://lkml.kernel.org/r/20120515111111.GA9870@quadSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 80c0120a
...@@ -1179,6 +1179,12 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) ...@@ -1179,6 +1179,12 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
fprintf(stderr, "cannot use both --output and --log-fd\n"); fprintf(stderr, "cannot use both --output and --log-fd\n");
usage_with_options(stat_usage, options); usage_with_options(stat_usage, options);
} }
if (output_fd < 0) {
fprintf(stderr, "argument to --log-fd must be a > 0\n");
usage_with_options(stat_usage, options);
}
if (!output) { if (!output) {
struct timespec tm; struct timespec tm;
mode = append_file ? "a" : "w"; mode = append_file ? "a" : "w";
...@@ -1190,7 +1196,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) ...@@ -1190,7 +1196,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
} }
clock_gettime(CLOCK_REALTIME, &tm); clock_gettime(CLOCK_REALTIME, &tm);
fprintf(output, "# started on %s\n", ctime(&tm.tv_sec)); fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
} else if (output_fd != 2) { } else if (output_fd > 0) {
mode = append_file ? "a" : "w"; mode = append_file ? "a" : "w";
output = fdopen(output_fd, mode); output = fdopen(output_fd, mode);
if (!output) { if (!output) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment