All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf, tool, record: Fix the header generation for pipe
@ 2011-08-22 14:23 Jiri Olsa
  2011-08-22 14:38 ` Eric Dumazet
  0 siblings, 1 reply; 35+ messages in thread
From: Jiri Olsa @ 2011-08-22 14:23 UTC (permalink / raw)
  To: acme, a.p.zijlstra, mingo, paulus; +Cc: linux-kernel, Jiri Olsa

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;
+}
+
 /* unfortunately, you can not stat debugfs or proc files for size */
 static void record_file(const char *file, size_t hdr_sz)
 {
 	unsigned long long size = 0;
-	char buf[BUFSIZ], *sizep;
-	off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
+	char buf[BUFSIZ], *sizep = (char *) &size;
+	off_t hdr_pos = -1;
 	int r, fd;
 
 	fd = open(file, O_RDONLY);
 	if (fd < 0)
 		die("Can't read '%s'", file);
 
-	/* put in zeros for file size, then fill true size later */
-	write_or_die(&size, hdr_sz);
+	/*
+	 * If the output_fd is pipe, we need to write the size
+	 * right away, because we cannot seek on pipe later.
+	 *
+	 * In case of regular/seekable file, we can go throught
+	 * only once and use pwrite to update the header value
+	 * afterwards.
+	 *
+	 * And finally the size does not need to be written at all
+	 * if we are in the calc mode - calc_data_size.
+	 */
+	if (!calc_data_size) {
+		if (output_is_pipe) {
+			size = get_file_size(fd);
+
+			/* ugh, handle big-endian hdr_size == 4 */
+			if (bigendian())
+				sizep += sizeof(u64) - hdr_sz;
+		} else
+			hdr_pos = lseek(output_fd, 0, SEEK_CUR);
+	}
+
+	write_or_die(sizep, hdr_sz);
 
 	do {
 		r = read(fd, buf, BUFSIZ);
@@ -205,15 +245,21 @@ static void record_file(const char *file, size_t hdr_sz)
 			write_or_die(buf, r);
 		}
 	} while (r > 0);
-	close(fd);
 
-	/* ugh, handle big-endian hdr_size == 4 */
-	sizep = (char*)&size;
-	if (bigendian())
-		sizep += sizeof(u64) - hdr_sz;
+	if (!output_is_pipe && !calc_data_size) {
+
+		if (hdr_pos == -1)
+			die("BUG: hdr_pos not initialized");
+
+		/* ugh, handle big-endian hdr_size == 4 */
+		if (bigendian())
+			sizep += sizeof(u64) - hdr_sz;
+
+		if (pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
+			die("writing to %s", output_file);
+	}
 
-	if (pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
-		die("writing to %s", output_file);
+	close(fd);
 }
 
 static void read_header_files(void)
@@ -439,6 +485,18 @@ bool have_tracepoints(struct list_head *pattrs)
 	return false;
 }
 
+static int is_fd_pipe(int fd)
+{
+	struct stat s;
+	int ret;
+
+	ret = fstat(fd, &s);
+	if (ret)
+		die("stat failed");
+
+	return ((s.st_mode & S_IFMT) == S_IFIFO);
+}
+
 int read_tracing_data(int fd, struct list_head *pattrs)
 {
 	char buf[BUFSIZ];
@@ -451,6 +509,7 @@ int read_tracing_data(int fd, struct list_head *pattrs)
 		return -1;
 
 	output_fd = fd;
+	output_is_pipe = is_fd_pipe(fd);
 
 	buf[0] = 23;
 	buf[1] = 8;
-- 
1.7.4


^ permalink raw reply related	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2011-10-20 21:29 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-22 14:23 [PATCH] perf, tool, record: Fix the header generation for pipe Jiri Olsa
2011-08-22 14:38 ` Eric Dumazet
2011-08-22 14:52   ` Jiri Olsa
2011-08-22 15:51     ` Eric Dumazet
2011-08-22 16:07       ` Jiri Olsa
2011-08-29 13:20         ` Arnaldo Carvalho de Melo
2011-08-29 13:41           ` Jiri Olsa
2011-08-29 14:25             ` Arnaldo Carvalho de Melo
2011-09-14 13:58               ` [PATCH] perf tools: Fix tracing info recording Jiri Olsa
2011-09-14 15:44                 ` Neil Horman
2011-09-21 15:30                 ` Frederic Weisbecker
2011-09-25 13:34                   ` Jiri Olsa
2011-09-26  9:11                     ` [PATCHv2 1/2] " Jiri Olsa
2011-09-26  9:11                       ` [PATCHv2 1/2] perf tools: Collect tracing event data files directly Jiri Olsa
2011-09-26 13:36                         ` Steven Rostedt
2011-09-26 14:56                           ` Jiri Olsa
2011-09-28 13:55                             ` Frederic Weisbecker
2011-09-28 14:03                               ` Steven Rostedt
2011-09-28 14:17                                 ` Frederic Weisbecker
2011-09-28 14:23                                   ` Steven Rostedt
2011-09-28 16:56                                   ` Ingo Molnar
2011-09-28 17:10                                     ` Steven Rostedt
2011-10-10  5:22                                       ` Ingo Molnar
2011-10-10 12:27                                         ` Steven Rostedt
2011-10-10 14:21                                           ` Frederic Weisbecker
2011-09-26 13:43                         ` David Ahern
2011-09-26 14:58                           ` Jiri Olsa
2011-09-26  9:11                       ` [PATCHv2 2/2] perf tools: Fix tracing info recording Jiri Olsa
2011-09-29 15:05                       ` [PATCHv3 0/2] " Jiri Olsa
2011-09-29 15:05                         ` [PATCHv3 1/2] perf tools: Fix raw sample reading Jiri Olsa
2011-09-29 15:34                           ` David Ahern
2011-09-29 15:05                         ` [PATCHv3 2/2] perf tools: Fix tracing info recording Jiri Olsa
2011-10-13 14:00                           ` Jiri Olsa
2011-10-20 13:59                             ` [PATCHv4] " Jiri Olsa
2011-10-20 21:28                               ` Arnaldo Carvalho de Melo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.