Re: [PATCH] perf, tool, record: Fix the header generation for pipe

Eric Dumazet
2011-08-22T14:38:34Z

Le lundi 22 août 2011 à 16:23 +0200, Jiri Olsa a écrit :
> The generation of the perf data file fails when using pipe
> as the output file descriptor.
>
> When record command generates the data header, several files are put
> inside and the file size is stored ahead of the file contents itself.
>
> The problem is that debugfs files cannot be stat-ed for size so we
> need to read the whole file, count the size and update the file size
> via seek&write (pwrite call).
> This cannot be done for pipes, since it's not allowed to seek on it.
>
> The attached patch changes current behaviour for pipes to get the
> file size first and write correct data within the first pass.
> For other than pipe files, the current behaviour stands.
>
> This issue was caught when running the script command:
>
> # perf script syscall-counts ls
>
> since it connects record and report via pipe.
>
> Signed-off-by: Jiri Olsa <jolsa@redhat.com>
> ---
> tools/perf/util/trace-event-info.c | 81 +++++++++++++++++++++++++++++++-----
> 1 files changed, 70 insertions(+), 11 deletions(-)
>
> diff --git a/tools/perf/util/trace-event-info.c b/tools/perf/util/trace-event-info.c
> index 3403f81..62ab9a2 100644
> --- a/tools/perf/util/trace-event-info.c
> +++ b/tools/perf/util/trace-event-info.c
> @@ -59,6 +59,7 @@ unsigned int page_size;
>
> static const char *output_file = "trace.info";
> static int output_fd;
> +static int output_is_pipe;
>
> struct event_list {
> struct event_list *next;
> @@ -183,20 +184,59 @@ int bigendian(void)
> return *ptr == 0x01020304;
> }
>
> +static off_t get_file_size(int fd)
> +{
> + off_t size = 0;
> + char buf[BUFSIZ];
> + int r;
> +
> + do {
> + r = read(fd, buf, BUFSIZ);
> + if (r > 0)
> + size += r;
> + } while (r > 0);
> +
> + if (lseek(fd, 0, SEEK_SET))
> + die("seek failed");
> +
> + return size;
> +}

This part makes no sense :

If fd is a pipe, you'll call die("seek failed")

If it's not a pipe, you can try lseek(fd, 0, SEEK_END)+lseek(fd, 0,
SEEK_SET) or fstat(fd, &st) instead of the read() loop.