Skip to contentSkip to conversation

perf, tool, record: Fix the header generation for pipe

PATCH In review Jiri Olsa 8 participants 35 messages Aug 22, 2011 – Oct 20, 2011 perf events

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH 1/1 perf, tool, record: Fix the header generation for pipe

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.

---
 tools/perf/util/trace-event-info.c |   81 +++++++++++++++++++++++++++++++-----
 1 files changed, 70 insertions(+), 11 deletions(-)
tools/perf/util/trace-event-info.c +70 -11
index 3403f81..62ab9a2 100644
@@ -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;
Signature · 1 line
1.7.4
#1 lore raw
Quoted 49 lines · 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.

---
 tools/perf/util/trace-event-info.c |   81 +++++++++++++++++++++++++++++++-----
 1 files changed, 70 insertions(+), 11 deletions(-)
tools/perf/util/trace-event-info.c +8 -0
index 3403f81..62ab9a2 100644
@@ -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 {

tools/perf/util/trace-event-info.c int bigendian(void) · line 203

Quoted 10 lines
tools/perf/util/trace-event-info.c +10 -0
+		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.

author
#2 lore raw
Quoted 63 lines · Eric Dumazet
Quoted 49 lines · 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.

---
 tools/perf/util/trace-event-info.c |   81 +++++++++++++++++++++++++++++++-----
 1 files changed, 70 insertions(+), 11 deletions(-)
tools/perf/util/trace-event-info.c +8 -0
index 3403f81..62ab9a2 100644
@@ -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 {

tools/perf/util/trace-event-info.c int bigendian(void) · line 203

Quoted 10 lines
tools/perf/util/trace-event-info.c +10 -0
+		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 :

Quoted 4 lines

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.

- fd is always file.. the descriptor, which might be pipe, is in output_fd variable but, maybe the die call is not necessary.. this should not fail

- the record_file function is called only on debugfs or procfs files:
events/header_page
events/header_event
events/**/format
printk_formats
/proc/kallsyms

so I think I need to read the whole file as in current code.

thanks,
jirka

#3 lore raw
Quoted 12 lines · Jiri Olsa

- fd is always file.. the descriptor, which might be pipe, is in output_fd variable but, maybe the die call is not necessary.. this should not fail

- the record_file function is called only on debugfs or procfs files:
events/header_page
events/header_event
events/**/format
printk_formats
/proc/kallsyms

so I think I need to read the whole file as in current code.

You read the file twice. Is it the right fix ?

Once to compute the length to be able to write the output header, once to process the content.

Are you sure length cannot change between the two phases ?

/proc/kallsyms can definitely change when a module is loaded.

Usually, when input "file" is not seekable or procfs based (fstat() returns st_size=0), we must load/cache it in memory (or using a temporary file) to get a consistent view of it.

This is what is done by following commands :

cat /proc/kallsyms | less
less /proc/kallsyms

author
#4 lore raw
Quoted 15 lines · Eric Dumazet
Quoted 12 lines · Jiri Olsa

- fd is always file.. the descriptor, which might be pipe, is in output_fd variable but, maybe the die call is not necessary.. this should not fail

- the record_file function is called only on debugfs or procfs files:
events/header_page
events/header_event
events/**/format
printk_formats
/proc/kallsyms

so I think I need to read the whole file as in current code.

Quoted 8 lines

You read the file twice. Is it the right fix ?

Once to compute the length to be able to write the output header, once to process the content.

Are you sure length cannot change between the two phases ?

/proc/kallsyms can definitely change when a module is loaded.

Right, better to store it to a temp file as you suggest, so we have consistent view.. I'll prepare new patch.

thanks,
jirka

Quoted 12 lines

Usually, when input "file" is not seekable or procfs based (fstat() returns st_size=0), we must load/cache it in memory (or using a temporary file) to get a consistent view of it.

This is what is done by following commands :

cat /proc/kallsyms | less
less /proc/kallsyms

#5 lore raw

Em Mon, Aug 22, 2011 at 06:07:13PM +0200, Jiri Olsa escreveu:

Quoted 19 lines · Eric Dumazet
Quoted 17 lines · Jiri Olsa
Quoted 11 lines

- fd is always file.. the descriptor, which might be pipe, is in output_fd variable but, maybe the die call is not necessary.. this should not fail

- the record_file function is called only on debugfs or procfs files:
events/header_page
events/header_event
events/**/format
printk_formats
/proc/kallsyms

so I think I need to read the whole file as in current code.

You read the file twice. Is it the right fix ?

Once to compute the length to be able to write the output header, once to process the content.

Quoted 6 lines
Quoted 3 lines

Are you sure length cannot change between the two phases ?

/proc/kallsyms can definitely change when a module is loaded.

Right, better to store it to a temp file as you suggest, so we have consistent view.. I'll prepare new patch.

Hi Jiri, any news here?

I just stumbled in this bug while testing Neil's net_dropmonitor script :-\

- Arnaldo

author
#6 lore raw
Quoted 24 lines · Arnaldo Carvalho de Melo

Em Mon, Aug 22, 2011 at 06:07:13PM +0200, Jiri Olsa escreveu:

Quoted 22 lines · Eric Dumazet
Quoted 21 lines · Jiri Olsa
Quoted 11 lines

- fd is always file.. the descriptor, which might be pipe, is in output_fd variable but, maybe the die call is not necessary.. this should not fail

- the record_file function is called only on debugfs or procfs files:
events/header_page
events/header_event
events/**/format
printk_formats
/proc/kallsyms

so I think I need to read the whole file as in current code.

You read the file twice. Is it the right fix ?

Once to compute the length to be able to write the output header, once to process the content.

Are you sure length cannot change between the two phases ?

/proc/kallsyms can definitely change when a module is loaded.

Quoted 7 lines
Quoted 2 lines

Right, better to store it to a temp file as you suggest, so we have consistent view.. I'll prepare new patch.

Hi Jiri, any news here?

I just stumbled in this bug while testing Neil's net_dropmonitor script :-\

well, I was thinking about taking files snapshot, so we dont read them twice and omit the reading for size.. but it'll be probably bigger change, I'll try to send out something by the end of the week

if you need workaround, the initial patch should work

jirka

#7 lore raw

Em Mon, Aug 29, 2011 at 03:41:47PM +0200, Jiri Olsa escreveu:

Quoted 10 lines · Arnaldo Carvalho de Melo
Quoted 9 lines

Em Mon, Aug 22, 2011 at 06:07:13PM +0200, Jiri Olsa escreveu:

Quoted 7 lines · Eric Dumazet
Quoted 3 lines

Are you sure length cannot change between the two phases ?

/proc/kallsyms can definitely change when a module is loaded.

Right, better to store it to a temp file as you suggest, so we have consistent view.. I'll prepare new patch.

Quoted 8 lines
Quoted 4 lines

Hi Jiri, any news here?

I just stumbled in this bug while testing Neil's net_dropmonitor script :-\

well, I was thinking about taking files snapshot, so we dont read them twice and omit the reading for size.. but it'll be probably bigger change, I'll try to send out something by the end of the week

Yeah, at least one thing there I plan to remove, i.e. no need to copy kallsyms when we have build ids, just stash the build id and use it later to get the right symtab to resolve symbols.

Quoted 1 line

if you need workaround, the initial patch should work

Ok, thanks.

- Arnaldo

author
#8 lore raw

PATCH 1/1 perf tools: Fix tracing info recording

On Mon, Aug 29, 2011 at 11:25:47AM -0300, Arnaldo Carvalho de Melo wrote:

SNIP

Quoted 9 lines
Quoted 9 lines
Quoted 5 lines

Hi Jiri, any news here?

I just stumbled in this bug while testing Neil's net_dropmonitor script :-\

well, I was thinking about taking files snapshot, so we dont read them twice and omit the reading for size.. but it'll be probably bigger change, I'll try to send out something by the end of the week

sry, I did not make it and then I was out for vacation.. sending now ;)

jirka

---
The tracing information is part of the perf data file. It contains several files from within the tracing debugfs and procs directories.

Beside some static header files, for each tracing event the format file is added. The /proc/kallsyms file is also added.

The tracing data are stored with preceeding size. This is causing some dificulties for pipe output, since there's no way to tell debugfs/proc file size before reading it. So, for pipe output, all the debugfs files were read twice. Once to get the overall size and once to store the content itself. This can cause problem in case any of these file changed, within the storage time.

Fixing this behaviour by using temp file in case of pipe output. The debugfs/proc files are being read only once, ensuring the integrity of the tracing data.

Also changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

---
 tools/perf/util/header.c           |   39 ++++-
 tools/perf/util/parse-events.h     |    1 +
 tools/perf/util/trace-event-info.c |  333 +++++++++++++++++++-----------------
 tools/perf/util/trace-event.h      |    7 +
 4 files changed, 215 insertions(+), 165 deletions(-)
tools/perf/util/header.c +31 -8
index b6c1ad1..6a9fd5b 100644
@@ -387,13 +387,21 @@ static int perf_header__adds_write(struct perf_header *header,
 
 	if (perf_header__has_feat(header, HEADER_TRACE_INFO)) {
 		struct perf_file_section *trace_sec;
+		struct tracing_data *tdata;
 
 		trace_sec = &feat_sec[idx++];
-
-		/* Write trace info */
 		trace_sec->offset = lseek(fd, 0, SEEK_CUR);
-		read_tracing_data(fd, &evlist->entries);
-		trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
+
+		/*
+		 * We work over the real file, we can write data
+		 * directly, not temp file is needed.
+		 */
+		tdata = tracing_data_get(&evlist->entries, fd, false);
+		if (!tdata)
+			goto out_free;
+
+		trace_sec->size = tracing_data_size(tdata);
+		tracing_data_put(tdata);
 	}
 
 	if (perf_header__has_feat(header, HEADER_BUILD_ID)) {
@@ -1100,15 +1108,25 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 				   struct perf_session *session __unused)
 {
 	union perf_event ev;
+	struct tracing_data *tdata;
 	ssize_t size = 0, aligned_size = 0, padding;
 	int err __used = 0;
 
+	/*
+	 * The fd descriptor is pipe, se we need to:
+	 * - write the tracing data to the temp file
+	 * - get/write the data size to pipe
+	 * - write the tracing data from the temp file
+	 *   to the pipe
+	 */
+	tdata = tracing_data_get(&evlist->entries, fd, true);
+	if (!tdata)
+		return -1;
+
 	memset(&ev, 0, sizeof(ev));
 
 	ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
-	size = read_tracing_data_size(fd, &evlist->entries);
-	if (size <= 0)
-		return size;
+	size = tracing_data_size(tdata);
 	aligned_size = ALIGN(size, sizeof(u64));
 	padding = aligned_size - size;
 	ev.tracing_data.header.size = sizeof(ev.tracing_data);
@@ -1116,7 +1134,12 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 
 	process(&ev, NULL, session);
 
-	err = read_tracing_data(fd, &evlist->entries);
+	/*
+	 * The put function will copy all the tracing data
+	 * stored in temp file to the pipe.
+	 */
+	tracing_data_put(tdata);
+
 	write_padded(fd, NULL, 0, padding);
 
 	return aligned_size;
tools/perf/util/parse-events.h +1 -0
index 2f8e375..1ea02ad 100644
@@ -15,6 +15,7 @@ struct option;
 struct tracepoint_path {
 	char *system;
 	char *name;
+	bool done;
 	struct tracepoint_path *next;
 };
 
tools/perf/util/trace-event-info.c +176 -157

381 lines not on this page · raw message

tools/perf/util/trace-event.h +7 -0
index f674dda..2a23893 100644
@@ -265,6 +265,13 @@ unsigned long long eval_flag(const char *flag);
 int read_tracing_data(int fd, struct list_head *pattrs);
 ssize_t read_tracing_data_size(int fd, struct list_head *pattrs);
 
+struct tracing_data;
+struct tracing_data *tracing_data_get(struct list_head *pattrs,
+				      int fd, bool temp);
+void tracing_data_put(struct tracing_data *tdata);
+ssize_t tracing_data_size(struct tracing_data *td);
+
+
 /* taken from kernel/trace/trace.h */
 enum trace_flag_type {
 	TRACE_FLAG_IRQS_OFF		= 0x01,
Signature · 1 line
1.7.4
#9 lore raw

Re: [PATCH] perf tools: Fix tracing info recording

Quoted 10 lines · Jiri Olsa

On Mon, Aug 29, 2011 at 11:25:47AM -0300, Arnaldo Carvalho de Melo wrote:

SNIP

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Hi Jiri, any news here?

I just stumbled in this bug while testing Neil's net_dropmonitor script :-\

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

well, I was thinking about taking files snapshot, so we dont read them twice and omit the reading for size.. but it'll be probably bigger change, I'll try to send out something by the end of the week

sry, I did not make it and then I was out for vacation.. sending now ;)

jirka

Thanks Jiri!

#10 lore raw

Re: [PATCH] perf tools: Fix tracing info recording

In reply to Jiri Olsa
Quoted 13 lines · Jiri Olsa

The tracing information is part of the perf data file. It contains several files from within the tracing debugfs and procs directories.

Beside some static header files, for each tracing event the format file is added. The /proc/kallsyms file is also added.

The tracing data are stored with preceeding size. This is causing some dificulties for pipe output, since there's no way to tell debugfs/proc file size before reading it. So, for pipe output, all the debugfs files were read twice. Once to get the overall size and once to store the content itself. This can cause problem in case any of these file changed, within the storage time.

Quoted 8 lines

Fixing this behaviour by using temp file in case of pipe output. The debugfs/proc files are being read only once, ensuring the integrity of the tracing data.

Also changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

Looks good overall, but I have some comments about details:

Quoted 17 lines
---
 tools/perf/util/header.c           |   39 ++++-
 tools/perf/util/parse-events.h     |    1 +
 tools/perf/util/trace-event-info.c |  333 +++++++++++++++++++-----------------
 tools/perf/util/trace-event.h      |    7 +
 4 files changed, 215 insertions(+), 165 deletions(-)
tools/perf/util/header.c +1 -0
index b6c1ad1..6a9fd5b 100644
@@ -387,13 +387,21 @@ static int perf_header__adds_write(struct perf_header *header,
 
 	if (perf_header__has_feat(header, HEADER_TRACE_INFO)) {
 		struct perf_file_section *trace_sec;
+		struct tracing_data *tdata;
 

tools/perf/util/header.c static int perf_header__adds_write(struct perf_header *header, · line 397

Quoted 10 lines
tools/perf/util/header.c +4 -4
 		trace_sec = &feat_sec[idx++];
-
-		/* Write trace info */
 		trace_sec->offset = lseek(fd, 0, SEEK_CUR);
-		read_tracing_data(fd, &evlist->entries);
-		trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
+
+		/*
+		 * We work over the real file, we can write data
+		 * directly, not temp file is needed.

s/not/no

May be also briefly explain why we can write directly here. The fact we have reserved space to write the size in the headers already.

Quoted 15 lines
tools/perf/util/header.c +8 -0
+		 */
+		tdata = tracing_data_get(&evlist->entries, fd, false);
+		if (!tdata)
+			goto out_free;
+
+		trace_sec->size = tracing_data_size(tdata);
+		tracing_data_put(tdata);
 	}
 
 	if (perf_header__has_feat(header, HEADER_BUILD_ID)) {
@@ -1100,15 +1108,25 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 				   struct perf_session *session __unused)
 {
 	union perf_event ev;
+	struct tracing_data *tdata;

tools/perf/util/header.c int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist, · line 1121

Quoted 10 lines
tools/perf/util/header.c +7 -0
 	ssize_t size = 0, aligned_size = 0, padding;
 	int err __used = 0;
 
+	/*
+	 * The fd descriptor is pipe, se we need to:
+	 * - write the tracing data to the temp file
+	 * - get/write the data size to pipe
+	 * - write the tracing data from the temp file
+	 *   to the pipe
+	 */

That also needs a brief explanation on why we need to do that.

Quoted 14 lines
tools/perf/util/header.c +5 -3
+	tdata = tracing_data_get(&evlist->entries, fd, true);
+	if (!tdata)
+		return -1;
+
 	memset(&ev, 0, sizeof(ev));
 
 	ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
-	size = read_tracing_data_size(fd, &evlist->entries);
-	if (size <= 0)
-		return size;
+	size = tracing_data_size(tdata);
 	aligned_size = ALIGN(size, sizeof(u64));
 	padding = aligned_size - size;
 	ev.tracing_data.header.size = sizeof(ev.tracing_data);

<snip>

Quoted 5 lines
tools/perf/util/header.c +4 -1
-int read_tracing_data(int fd, struct list_head *pattrs)
+#define FILENAME_SIZE 50
+
+struct tracing_data {
+	ssize_t size;

tools/perf/util/header.c

Quoted 10 lines
tools/perf/util/header.c +7 -2
+	bool temp;
+	char temp_file[FILENAME_SIZE];
+};
+
+ssize_t tracing_data_size(struct tracing_data *td)
 {
-	char buf[BUFSIZ];
-	struct tracepoint_path *tps = get_tracepoints_path(pattrs);
+	return td->size;
+}

May be inline it. Or rather directly access td->size from calling code.

Quoted 7 lines
tools/perf/util/header.c +1 -5
-	/*
-	 * What? No tracepoints? No sense writing anything here, bail out.
-	 */
-	if (tps == NULL)
-		return -1;
+static char *get_format_file(char *sys, char *name)

tools/perf/util/header.c

Quoted 10 lines
tools/perf/util/header.c +8 -1
+{
+	char *file, *path;
 
-	output_fd = fd;
+	path = get_tracing_file("events");
+	if (!path)
+		return NULL;
+
+	file = malloc_or_die(strlen(path) + strlen(sys) +
+			     strlen(name) + strlen("format") + 10);

Why "10" ?

Quoted 69 lines
tools/perf/util/header.c +69 -0
+
+	sprintf(file, "%s/%s/%s/format", path, sys, name);
+	return file;
+}
+
+static void put_format_file(char *file)
+{
+	free(file);
+}
+
+/*
+ * Walk tracepoint event objects and store them.
+ * Only those matching the sys parameter are stored
+ * and marked as done.
+ */
+static void read_event_files_system(struct tracepoint_path *tps,
+				    const char *sys)
+{
+	off_t count_pos;
+	u32 count = 0;
+
+	/* specified events count under single system */
+	count_pos = lseek(output_fd, 0, SEEK_CUR);
+	write_or_die(&count, 4);
+
+	while (tps) {
+		if ((!tps->done) &&
+		    (!strcmp(sys, tps->system))) {
+			char *file;
+
+			file = get_format_file(tps->system, tps->name);
+			if (file) {
+				record_file(file, 8);
+				count++;
+			}
+
+			put_format_file(file);
+			tps->done = 1;
+		}
+
+		tps = tps->next;
+	}
+
+	if (pwrite(output_fd, &count, 4, count_pos) < 0)
+		die("pwrite");
+}
+
+/*
+ * We have all needed tracepoint event files stored in
+ * the tracepoint_path objects.
+ *
+ * - first we store ftrace system events
+ * - then we walk all '!done' event objects
+ *   and process them
+ */
+static void read_event_files(struct tracepoint_path *tps)
+{
+	off_t count_pos;
+	u32 count = 0;
+
+	read_event_files_system(tps, "ftrace");
+
+	/* systems count */
+	count_pos = lseek(output_fd, 0, SEEK_CUR);
+	write_or_die(&count, 4);
+
+	while (tps) {
+		if (!tps->done) {
+			write_or_die(tps->system, strlen(tps->system) + 1);

tools/perf/util/header.c

Quoted 10 lines
tools/perf/util/header.c +9 -0
+			read_event_files_system(tps, tps->system);
+			count++;
+		}
+
+		tps = tps->next;
+	}
 
+	if (pwrite(output_fd, &count, 4, count_pos) < 0)
+		die("write");
+}

There seem to be a significant reorganization of the code in that file that seem to overlap the initial scope of this patch. Should it go to a seperate single purpose patch?

Thanks.

author
#11 lore raw

Re: [PATCH] perf tools: Fix tracing info recording

Quoted 18 lines · Frederic Weisbecker
Quoted 17 lines · Jiri Olsa

The tracing information is part of the perf data file. It contains several files from within the tracing debugfs and procs directories.

Beside some static header files, for each tracing event the format file is added. The /proc/kallsyms file is also added.

The tracing data are stored with preceeding size. This is causing some dificulties for pipe output, since there's no way to tell debugfs/proc file size before reading it. So, for pipe output, all the debugfs files were read twice. Once to get the overall size and once to store the content itself. This can cause problem in case any of these file changed, within the storage time.

Fixing this behaviour by using temp file in case of pipe output. The debugfs/proc files are being read only once, ensuring the integrity of the tracing data.

Quoted 6 lines
Quoted 4 lines

Also changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

Looks good overall, but I have some comments about details:

hi,
thanks for comments,
I'll make the changes and send out new version soon

jirka

author
#12 lore raw

PATCH v2 1/2 perf tools: Fix tracing info recording

hi,
I modified the patch based on your comments and separated it into 2 parts:

- 1/2 perf tools: Collect tracing event data files directly
- 2/2 perf tools: Fix tracing info recording

thanks for comments,
jirka

---
 tools/perf/util/header.c           |   50 +++++-
 tools/perf/util/parse-events.h     |    1 +
 tools/perf/util/trace-event-info.c |  309 +++++++++++++++++++-----------------
 tools/perf/util/trace-event.h      |   11 +-
 4 files changed, 212 insertions(+), 159 deletions(-)
author
#13 lore raw

PATCH v2 1/2 perf tools: Collect tracing event data files directly

Changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

Hopefully this way is more straightforward and faster.

---
 tools/perf/util/parse-events.h     |    1 +
 tools/perf/util/trace-event-info.c |  231 +++++++++++++++--------------------
 2 files changed, 100 insertions(+), 132 deletions(-)
tools/perf/util/parse-events.h +1 -0
index 2f8e375..1ea02ad 100644
@@ -15,6 +15,7 @@ struct option;
 struct tracepoint_path {
 	char *system;
 	char *name;
+	bool done;
 	struct tracepoint_path *next;
 };
 
tools/perf/util/trace-event-info.c +99 -132

281 lines not on this page · raw message

Signature · 1 line
1.7.4
#14 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 4 lines · Jiri Olsa

Changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

Hopefully this way is more straightforward and faster.

Have you looked at my code I posted earlier that uses the libparsevents?

It uses globs such that you could do -e sched:sched* and it will enable all sched events.

-- Steve

Quoted 59 lines
tools/perf/util/trace-event-info.c +36 -14
+	 * and one safety byte ;)
+	 */
+	file = malloc_or_die(strlen(path) + strlen(sys) +
+			     strlen(name) + strlen("format") + 5);
 
-		free(format);
-	}
-	closedir(dir);
+	sprintf(file, "%s/%s/%s/format", path, sys, name);
+	return file;
 }
 
-static void read_ftrace_files(struct tracepoint_path *tps)
+static void put_format_file(char *file)
 {
-	char *path;
-
-	path = get_tracing_file("events/ftrace");
-
-	copy_event_system(path, tps);
-
-	put_tracing_file(path);
+	free(file);
 }
 
-static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
+/*
+ * Walk tracepoint event objects and store them.
+ * Only those matching the sys parameter are stored
+ * and marked as done.
+ */
+static void read_event_files_system(struct tracepoint_path *tps,
+				    const char *sys)
 {
+	off_t count_pos;
+	u32 count = 0;
+
+	/* Place for number of events under single system. */
+	count_pos = lseek(output_fd, 0, SEEK_CUR);
+	write_or_die(&count, 4);
+
 	while (tps) {
-		if (!strcmp(sys, tps->system))
-			return true;
+		if ((!tps->done) &&
+		    (!strcmp(sys, tps->system))) {
+			char *file;
+
+			file = get_format_file(tps->system, tps->name);
+			if (file) {
+				record_file(file, 8);
+				count++;
+			}
+
+			put_format_file(file);
+			tps->done = 1;
+		}
+
 		tps = tps->next;
author
#15 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 10 lines · Steven Rostedt
Quoted 4 lines · Jiri Olsa

Changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

Hopefully this way is more straightforward and faster.

Have you looked at my code I posted earlier that uses the libparsevents?

It uses globs such that you could do -e sched:sched* and it will enable all sched events.

ops, haven't seen those changes yet..
I think I can go only with 2/2 patch, if the 1/2 collides with your changes

I'll check, thanks
jirka

Quoted 66 lines

-- Steve

Quoted 59 lines
tools/perf/util/trace-event-info.c +36 -14
+	 * and one safety byte ;)
+	 */
+	file = malloc_or_die(strlen(path) + strlen(sys) +
+			     strlen(name) + strlen("format") + 5);
 
-		free(format);
-	}
-	closedir(dir);
+	sprintf(file, "%s/%s/%s/format", path, sys, name);
+	return file;
 }
 
-static void read_ftrace_files(struct tracepoint_path *tps)
+static void put_format_file(char *file)
 {
-	char *path;
-
-	path = get_tracing_file("events/ftrace");
-
-	copy_event_system(path, tps);
-
-	put_tracing_file(path);
+	free(file);
 }
 
-static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
+/*
+ * Walk tracepoint event objects and store them.
+ * Only those matching the sys parameter are stored
+ * and marked as done.
+ */
+static void read_event_files_system(struct tracepoint_path *tps,
+				    const char *sys)
 {
+	off_t count_pos;
+	u32 count = 0;
+
+	/* Place for number of events under single system. */
+	count_pos = lseek(output_fd, 0, SEEK_CUR);
+	write_or_die(&count, 4);
+
 	while (tps) {
-		if (!strcmp(sys, tps->system))
-			return true;
+		if ((!tps->done) &&
+		    (!strcmp(sys, tps->system))) {
+			char *file;
+
+			file = get_format_file(tps->system, tps->name);
+			if (file) {
+				record_file(file, 8);
+				count++;
+			}
+
+			put_format_file(file);
+			tps->done = 1;
+		}
+
 		tps = tps->next;
#16 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 7 lines · Jiri Olsa
Quoted 6 lines · Steven Rostedt
Quoted 4 lines · Jiri Olsa

Changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

Hopefully this way is more straightforward and faster.

Quoted 7 lines
Quoted 4 lines

Have you looked at my code I posted earlier that uses the libparsevents?

It uses globs such that you could do -e sched:sched* and it will enable all sched events.

ops, haven't seen those changes yet..
I think I can go only with 2/2 patch, if the 1/2 collides with your changes

But it seems Steve's patches are not completely uncontroversial because of some crazy disagreements on where the libparsevent.so should lay (tools generic or tied to perf).

So until we get that situation solved, we should continue to move forward.

#17 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 13 lines · Frederic Weisbecker
Quoted 12 lines · Jiri Olsa
Quoted 10 lines · Steven Rostedt
Quoted 4 lines · Jiri Olsa

Changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

Hopefully this way is more straightforward and faster.

Have you looked at my code I posted earlier that uses the libparsevents?

It uses globs such that you could do -e sched:sched* and it will enable all sched events.

Quoted 6 lines
Quoted 2 lines

ops, haven't seen those changes yet..
I think I can go only with 2/2 patch, if the 1/2 collides with your changes

But it seems Steve's patches are not completely uncontroversial because of some crazy disagreements on where the libparsevent.so should lay (tools generic or tied to perf).

Which to me seems to be a silly road block, in which I never got a clear answer for.

Quoted 2 lines

So until we get that situation solved, we should continue to move forward.

Sure, but it just forks the code even more, and it's almost to the point where maintaining a fork will just be easier, not to mention quicker to get out to the distros.

-- Steve

#18 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 17 lines · Steven Rostedt
Quoted 16 lines · Frederic Weisbecker
Quoted 14 lines · Jiri Olsa
Quoted 10 lines · Steven Rostedt
Quoted 4 lines · Jiri Olsa

Changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

Hopefully this way is more straightforward and faster.

Have you looked at my code I posted earlier that uses the libparsevents?

It uses globs such that you could do -e sched:sched* and it will enable all sched events.

ops, haven't seen those changes yet..
I think I can go only with 2/2 patch, if the 1/2 collides with your changes

Quoted 6 lines
Quoted 3 lines

But it seems Steve's patches are not completely uncontroversial because of some crazy disagreements on where the libparsevent.so should lay (tools generic or tied to perf).

Which to me seems to be a silly road block, in which I never got a clear answer for.

Yeah we need to sort that out with Ingo.

Quoted 7 lines
Quoted 3 lines

So until we get that situation solved, we should continue to move forward.

Sure, but it just forks the code even more, and it's almost to the point where maintaining a fork will just be easier, not to mention quicker to get out to the distros.

Right. May be Jiri can have a look at what trace-cmd does there.

#19 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 9 lines · Frederic Weisbecker
Quoted 7 lines
Quoted 4 lines

But it seems Steve's patches are not completely uncontroversial because of some crazy disagreements on where the libparsevent.so should lay (tools generic or tied to perf).

Which to me seems to be a silly road block, in which I never got a clear answer for.

Yeah we need to sort that out with Ingo.

And include Arnaldo, as he was the one that suggested to do what I did that Ingo NACKd. And I consider Arnaldo the maintainer of the userspace side of perf, so I will not step on his toes in what I do.

-- Steve

#20 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

In reply to Frederic Weisbecker
Quoted 8 lines · Frederic Weisbecker
Quoted 6 lines
Quoted 3 lines

But it seems Steve's patches are not completely uncontroversial because of some crazy disagreements on where the libparsevent.so should lay (tools generic or tied to perf).

Which to me seems to be a silly road block, in which I never got a clear answer for.

Yeah we need to sort that out with Ingo.

Basically, since it's not at all clear to me where these things (and APIs) will go, I'd be much more comfortable with this starting out as tools/perf/lib/ - we can still split it out later on. Merging it in will be a lot harder.

Thanks,

Ingo

#21 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Good to see your email is back :)

Quoted 9 lines · Ingo Molnar
Quoted 7 lines · Frederic Weisbecker
Quoted 6 lines
Quoted 3 lines

But it seems Steve's patches are not completely uncontroversial because of some crazy disagreements on where the libparsevent.so should lay (tools generic or tied to perf).

Which to me seems to be a silly road block, in which I never got a clear answer for.

Quoted 6 lines
Quoted 1 line

Yeah we need to sort that out with Ingo.

Basically, since it's not at all clear to me where these things (and APIs) will go, I'd be much more comfortable with this starting out as tools/perf/lib/ - we can still split it out later on. Merging it in will be a lot harder.

I would actually argue that merging is much easier than splitting something up. If A depends on B, merging B back into A is trivial. But if B is a part of A, breaking it out of A is a much more difficult task, as the boundaries of A and B are not so easy to find and the coupling of the two is much tighter.

I find trying to break things out of perf is very hard to do as the dependencies are throughout all of perf, and no one seems to agree on how to do so.

-- Steve

#22 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 15 lines · Steven Rostedt

Good to see your email is back :)

Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Frederic Weisbecker
Quoted 6 lines
Quoted 3 lines

But it seems Steve's patches are not completely uncontroversial because of some crazy disagreements on where the libparsevent.so should lay (tools generic or tied to perf).

Which to me seems to be a silly road block, in which I never got a clear answer for.

Yeah we need to sort that out with Ingo.

Quoted 7 lines
Quoted 4 lines

Basically, since it's not at all clear to me where these things (and APIs) will go, I'd be much more comfortable with this starting out as tools/perf/lib/ - we can still split it out later on. Merging it in will be a lot harder.

I would actually argue that merging is much easier than splitting something up. [...]

Technically it's somewhat easier - socially, not.

Quoted 4 lines

[...] If A depends on B, merging B back into A is trivial. But if B is a part of A, breaking it out of A is a much more difficult task, as the boundaries of A and B are not so easy to find and the coupling of the two is much tighter.

That's a technical problem. In reality we can split up and merge projects of very significant size just fine. I've done split-ups and factorings-out from millions of lines of code impact. tools/perf is still well within such size boundaries.

Quoted 3 lines

I find trying to break things out of perf is very hard to do as the dependencies are throughout all of perf, and no one seems to agree on how to do so.

Then it would be absolute madness to make that non-agreement external and hard-code a separate social structure for it!

Libraries are for *boring* infrastructure stuff *everyone agrees on*.

They are absolutely lousy arbitrators of technical disagreement and if there's a serious disagreement they can easily become an absolute nightmare socially and a technical distraction. How difficult is this to understand?

Please work out disagreements with Arnaldo and do librarization within perf if you are interested in that angle. If those internal interfaces become visibly boring and are consistently used by everything in a way that every main contributor agrees on then we can perhaps librarize it. Not the other way around.

Thanks,

Ingo

#23 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 7 lines · Ingo Molnar
Quoted 2 lines · Steven Rostedt

I would actually argue that merging is much easier than splitting something up. [...]

Technically it's somewhat easier - socially, not.

I'm trying to keep this on a technical level.

Quoted 10 lines
Quoted 5 lines

[...] If A depends on B, merging B back into A is trivial. But if B is a part of A, breaking it out of A is a much more difficult task, as the boundaries of A and B are not so easy to find and the coupling of the two is much tighter.

That's a technical problem. In reality we can split up and merge projects of very significant size just fine. I've done split-ups and factorings-out from millions of lines of code impact. tools/perf is still well within such size boundaries.

I find splitting perf up is not so easy. Perhaps if you do it so well then you could help.

Quoted 7 lines
Quoted 4 lines

I find trying to break things out of perf is very hard to do as the dependencies are throughout all of perf, and no one seems to agree on how to do so.

Then it would be absolute madness to make that non-agreement external and hard-code a separate social structure for it!

I seem to agree with Arnaldo, Boris and Frederic, but you do not seem to agree. My arguments with Arnaldo started with "but Ingo wants it this way", his reply was, "What do you want". Honestly, I agreed with Arnaldo.

Quoted 2 lines

Libraries are for *boring* infrastructure stuff *everyone agrees on*.

But what I have proposed is something that we all agreed on (besides you) that it *is* boring infrastructure stuff. Hell, the code never was developed for perf in the first place. Frederic pulled it in from trace-cmd. The update code currently lives in trace-cmd as a separate library, that was designed to work with any other project (including perf). You want me to rip it apart to make it a perf only lib?

Currently perf has an old outdated version, because it never was incorporated into perf as a library. If it was, then it could have easily benefited by the updates. A lot of people want perf to have the functionality that trace-cmd currently has. But because we are fighting over where the library will be, perf is still suffering.

Quoted 5 lines

They are absolutely lousy arbitrators of technical disagreement and if there's a serious disagreement they can easily become an absolute nightmare socially and a technical distraction. How difficult is this to understand?

Must be very difficult to understand, because I do not understand why a library that can work with multiple tools (perf, powertop, latencytrace, trace-cmd, kernelshark) needs to be in tools/perf/lib instead of tools/lib?

Quoted 6 lines

Please work out disagreements with Arnaldo and do librarization within perf if you are interested in that angle. If those internal interfaces become visibly boring and are consistently used by everything in a way that every main contributor agrees on then we can perhaps librarize it. Not the other way around.

For parsing of events, which is what I proposed, it seems to be boring technical stuff that has been well established by the limitations of the ABI forced on the debugfs system. It's already over a year old and used by several developers. One nice feature to come with this is the ability to add plugins to parse the trace events without needing to read all the format files. Things like kvm events will suddenly work.

The one disagreement that we are still sorting out is just the name of the library. I first said libperf (which would make sense to have in tools/perf/lib) but then it wasn't doing perf specific work. It was just a way to parse kernel trace points. Nobody seemed to like libparsevent. I think one of the names that makes sense is libtrace, or libktrace as it has to do with tracepoints in the kernel. Maybe just libktracepoint? But that's quite an ugly name.

-- Steve

#24 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 13 lines · Steven Rostedt
Quoted 5 lines

Please work out disagreements with Arnaldo and do librarization within perf if you are interested in that angle. If those internal interfaces become visibly boring and are consistently used by everything in a way that every main contributor agrees on then we can perhaps librarize it. Not the other way around.

For parsing of events, which is what I proposed, it seems to be boring technical stuff that has been well established by the limitations of the ABI forced on the debugfs system. It's already over a year old and used by several developers. One nice feature to come with this is the ability to add plugins to parse the trace events without needing to read all the format files. Things like kvm events will suddenly work.

Quoted 7 lines

The one disagreement that we are still sorting out is just the name of the library. I first said libperf (which would make sense to have in tools/perf/lib) but then it wasn't doing perf specific work. It was just a way to parse kernel trace points. Nobody seemed to like libparsevent. I think one of the names that makes sense is libtrace, or libktrace as it has to do with tracepoints in the kernel. Maybe just libktracepoint? But that's quite an ugly name.

libtracevent ?

#25 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

In reply to Jiri Olsa
Quoted 86 lines · Jiri Olsa

Changing the way the event files are searched by quering specified event files directly, instead of walking the events directory.

Hopefully this way is more straightforward and faster.

---
 tools/perf/util/parse-events.h     |    1 +
 tools/perf/util/trace-event-info.c |  231 +++++++++++++++--------------------
 2 files changed, 100 insertions(+), 132 deletions(-)
tools/perf/util/parse-events.h +1 -0
index 2f8e375..1ea02ad 100644
@@ -15,6 +15,7 @@ struct option;
 struct tracepoint_path {
 	char *system;
 	char *name;
+	bool done;
 	struct tracepoint_path *next;
 };
 
tools/perf/util/trace-event-info.c +4 -49
index 3403f81..cf8f89e 100644
@@ -238,136 +238,98 @@ static void read_header_files(void)
 	put_tracing_file(path);
 }
 
-static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
+static char *get_format_file(char *sys, char *name)
 {
-	while (tps) {
-		if (!strcmp(sys, tps->name))
-			return true;
-		tps = tps->next;
-	}
-
-	return false;
-}
-
-static void copy_event_system(const char *sys, struct tracepoint_path *tps)
-{
-	struct dirent *dent;
-	struct stat st;
-	char *format;
-	DIR *dir;
-	int count = 0;
-	int ret;
-
-	dir = opendir(sys);
-	if (!dir)
-		die("can't read directory '%s'", sys);
-
-	while ((dent = readdir(dir))) {
-		if (dent->d_type != DT_DIR ||
-		    strcmp(dent->d_name, ".") == 0 ||
-		    strcmp(dent->d_name, "..") == 0 ||
-		    !name_in_tp_list(dent->d_name, tps))
-			continue;
-		format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
-		sprintf(format, "%s/%s/format", sys, dent->d_name);
-		ret = stat(format, &st);
-		free(format);
-		if (ret < 0)
-			continue;
-		count++;
-	}
-
-	write_or_die(&count, 4);
+	char *file, *path;
 
-	rewinddir(dir);
-	while ((dent = readdir(dir))) {
-		if (dent->d_type != DT_DIR ||
-		    strcmp(dent->d_name, ".") == 0 ||
-		    strcmp(dent->d_name, "..") == 0 ||
-		    !name_in_tp_list(dent->d_name, tps))
-			continue;
-		format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
-		sprintf(format, "%s/%s/format", sys, dent->d_name);
-		ret = stat(format, &st);
+	path = get_tracing_file("events");
+	if (!path)

tools/perf/util/trace-event-info.c static void read_header_files(void) · line 254

Quoted 10 lines
tools/perf/util/trace-event-info.c +7 -2
+		return NULL;
 
-		if (ret >= 0)
-			record_file(format, 8);
+	/*
+	 * The '+ 5' is for '/' we add to the patch, plus NULL byte,
+	 * and one safety byte ;)
+	 */
+	file = malloc_or_die(strlen(path) + strlen(sys) +
+			     strlen(name) + strlen("format") + 5);

Why not go the simple route and just use PATH_MAX?

David

Quoted 216 lines
tools/perf/util/trace-event-info.c +88 -81
-		free(format);
-	}
-	closedir(dir);
+	sprintf(file, "%s/%s/%s/format", path, sys, name);
+	return file;
 }
 
-static void read_ftrace_files(struct tracepoint_path *tps)
+static void put_format_file(char *file)
 {
-	char *path;
-
-	path = get_tracing_file("events/ftrace");
-
-	copy_event_system(path, tps);
-
-	put_tracing_file(path);
+	free(file);
 }
 
-static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
+/*
+ * Walk tracepoint event objects and store them.
+ * Only those matching the sys parameter are stored
+ * and marked as done.
+ */
+static void read_event_files_system(struct tracepoint_path *tps,
+				    const char *sys)
 {
+	off_t count_pos;
+	u32 count = 0;
+
+	/* Place for number of events under single system. */
+	count_pos = lseek(output_fd, 0, SEEK_CUR);
+	write_or_die(&count, 4);
+
 	while (tps) {
-		if (!strcmp(sys, tps->system))
-			return true;
+		if ((!tps->done) &&
+		    (!strcmp(sys, tps->system))) {
+			char *file;
+
+			file = get_format_file(tps->system, tps->name);
+			if (file) {
+				record_file(file, 8);
+				count++;
+			}
+
+			put_format_file(file);
+			tps->done = 1;
+		}
+
 		tps = tps->next;
 	}
 
-	return false;
+	if (pwrite(output_fd, &count, 4, count_pos) < 0)
+		die("pwrite");
 }
 
+/*
+ * We have all needed tracepoint event files stored in
+ * the tracepoint_path objects.
+ *
+ * - first we store ftrace system events
+ * - then we walk all '!done' event objects
+ *   and process them
+ */
 static void read_event_files(struct tracepoint_path *tps)
 {
-	struct dirent *dent;
-	struct stat st;
-	char *path;
-	char *sys;
-	DIR *dir;
-	int count = 0;
-	int ret;
-
-	path = get_tracing_file("events");
+	off_t count_pos;
+	u32 count = 0;
 
-	dir = opendir(path);
-	if (!dir)
-		die("can't read directory '%s'", path);
-
-	while ((dent = readdir(dir))) {
-		if (dent->d_type != DT_DIR ||
-		    strcmp(dent->d_name, ".") == 0 ||
-		    strcmp(dent->d_name, "..") == 0 ||
-		    strcmp(dent->d_name, "ftrace") == 0 ||
-		    !system_in_tp_list(dent->d_name, tps))
-			continue;
-		count++;
-	}
+	read_event_files_system(tps, "ftrace");
 
+	/* systems count */
+	count_pos = lseek(output_fd, 0, SEEK_CUR);
 	write_or_die(&count, 4);
 
-	rewinddir(dir);
-	while ((dent = readdir(dir))) {
-		if (dent->d_type != DT_DIR ||
-		    strcmp(dent->d_name, ".") == 0 ||
-		    strcmp(dent->d_name, "..") == 0 ||
-		    strcmp(dent->d_name, "ftrace") == 0 ||
-		    !system_in_tp_list(dent->d_name, tps))
-			continue;
-		sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
-		sprintf(sys, "%s/%s", path, dent->d_name);
-		ret = stat(sys, &st);
-		if (ret >= 0) {
-			write_or_die(dent->d_name, strlen(dent->d_name) + 1);
-			copy_event_system(sys, tps);
+	while (tps) {
+		if (!tps->done) {
+			write_or_die(tps->system, strlen(tps->system) + 1);
+			read_event_files_system(tps, tps->system);
+			count++;
 		}
-		free(sys);
+
+		tps = tps->next;
 	}
 
-	closedir(dir);
-	put_tracing_file(path);
+	if (pwrite(output_fd, &count, 4, count_pos) < 0)
+		die("write");
 }
 
 static void read_proc_kallsyms(void)
@@ -387,6 +349,37 @@ static void read_proc_kallsyms(void)
 	record_file(path, 4);
 }
 
+static void tracing_data_header(void)
+{
+	char buf[50];
+
+	/* just guessing this is someone's birthday.. ;) */
+	buf[0] = 23;
+	buf[1] = 8;
+	buf[2] = 68;
+	memcpy(buf + 3, "tracing", 7);
+
+	write_or_die(buf, 10);
+
+	write_or_die(VERSION, strlen(VERSION) + 1);
+
+	/* save endian */
+	if (bigendian())
+		buf[0] = 1;
+	else
+		buf[0] = 0;
+
+	write_or_die(buf, 1);
+
+	/* save size of long */
+	buf[0] = sizeof(long);
+	write_or_die(buf, 1);
+
+	/* save page_size */
+	page_size = sysconf(_SC_PAGESIZE);
+	write_or_die(&page_size, 4);
+}
+
 static void read_ftrace_printk(void)
 {
 	unsigned int size;
@@ -441,7 +434,6 @@ bool have_tracepoints(struct list_head *pattrs)
 
 int read_tracing_data(int fd, struct list_head *pattrs)
 {
-	char buf[BUFSIZ];
 	struct tracepoint_path *tps = get_tracepoints_path(pattrs);
 
 	/*
@@ -452,33 +444,8 @@ int read_tracing_data(int fd, struct list_head *pattrs)
 
 	output_fd = fd;
 
-	buf[0] = 23;
-	buf[1] = 8;
-	buf[2] = 68;
-	memcpy(buf + 3, "tracing", 7);
-
-	write_or_die(buf, 10);
-
-	write_or_die(VERSION, strlen(VERSION) + 1);
-
-	/* save endian */
-	if (bigendian())
-		buf[0] = 1;
-	else
-		buf[0] = 0;
-
-	write_or_die(buf, 1);
-
-	/* save size of long */
-	buf[0] = sizeof(long);
-	write_or_die(buf, 1);
-
-	/* save page_size */
-	page_size = sysconf(_SC_PAGESIZE);
-	write_or_die(&page_size, 4);
-
+	tracing_data_header();
 	read_header_files();
-	read_ftrace_files(tps);
 	read_event_files(tps);
 	read_proc_kallsyms();
 	read_ftrace_printk();
author
#26 lore raw

Re: [PATCHv2 1/2] perf tools: Collect tracing event data files directly

Quoted 2 lines · David Ahern

SNIP

Quoted 10 lines

tools/perf/util/trace-event-info.c static void read_header_files(void) · line 254

Quoted 6 lines
tools/perf/util/trace-event-info.c +6 -0
+	/*
+	 * The '+ 5' is for '/' we add to the patch, plus NULL byte,
+	 * and one safety byte ;)
+	 */
+	file = malloc_or_die(strlen(path) + strlen(sys) +
+			     strlen(name) + strlen("format") + 5);

Why not go the simple route and just use PATH_MAX?

David

ok, seems more simple.. ;)

thanks,
jirka

author
#27 lore raw

PATCH v2 2/2 perf tools: Fix tracing info recording

In reply to Jiri Olsa

Fixing the way the tracing information is stored within record command. The current implementation is causing inconsistency issues for pipe output (described below), also following commands fail currently:

perf script syscall-counts ls
perf record -e syscalls:sys_exit_read ls | ./perf report -i -

The tracing information is part of the perf data file. It contains several files from within the tracing debugfs and procs directories.

Beside some static header files, for each tracing event the format file is added. The /proc/kallsyms file is also added.

The tracing data are stored with preceeding size. This is causing dificulties for pipe output, since there's no way to tell debugfs/proc file size before reading it. So, for pipe output, all the debugfs files were read twice. Once to get the overall size and once to store the content itself. This can cause problem in case any of these file changed, within the storage time.

To fix this behaviour and ensure the integrity of the tracing data, we:
- read debugfs/proc file into the temp file
- get temp file size and dump it to the pipe
- dump the temp file contents to the pipe

This change also fix current code, which did not distinguish regular file and pipe and failed on seek call over pipe.

---
 tools/perf/util/header.c           |   50 +++++++++++++++++---
 tools/perf/util/trace-event-info.c |   90 ++++++++++++++++++++++++++---------
 tools/perf/util/trace-event.h      |   11 ++++-
 3 files changed, 118 insertions(+), 33 deletions(-)
tools/perf/util/header.c +42 -8
index b6c1ad1..477c73b 100644
@@ -382,18 +382,33 @@ static int perf_header__adds_write(struct perf_header *header,
 
 	sec_size = sizeof(*feat_sec) * nr_sections;
 
+	/*
+	 * We reserve header space for each section, and
+	 * update/store it later once we know the section size.
+	 */
 	sec_start = header->data_offset + header->data_size;
 	lseek(fd, sec_start + sec_size, SEEK_SET);
 
 	if (perf_header__has_feat(header, HEADER_TRACE_INFO)) {
 		struct perf_file_section *trace_sec;
+		struct tracing_data *tdata;
 
 		trace_sec = &feat_sec[idx++];
-
-		/* Write trace info */
 		trace_sec->offset = lseek(fd, 0, SEEK_CUR);
-		read_tracing_data(fd, &evlist->entries);
-		trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
+
+		/*
+		 * We work over the real file, so we can write data
+		 * directly, no temp file is needed.
+		 */
+		tdata = tracing_data_get(&evlist->entries, fd, false);
+		if (!tdata)
+			goto out_free;
+
+		/*
+		 * Update the section header information.
+		 */
+		trace_sec->size = tdata->size;
+		tracing_data_put(tdata);
 	}
 
 	if (perf_header__has_feat(header, HEADER_BUILD_ID)) {
@@ -1100,15 +1115,29 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 				   struct perf_session *session __unused)
 {
 	union perf_event ev;
+	struct tracing_data *tdata;
 	ssize_t size = 0, aligned_size = 0, padding;
 	int err __used = 0;
 
+	/*
+	 * We are going to store the size of the data followed
+	 * by the data contents. Since the fd descriptor is a pipe,
+	 * we cannot seek back to store the size of the data once
+	 * we know it. Instead we:
+	 *
+	 * - write the tracing data to the temp file
+	 * - get/write the data size to pipe
+	 * - write the tracing data from the temp file
+	 *   to the pipe
+	 */
+	tdata = tracing_data_get(&evlist->entries, fd, true);
+	if (!tdata)
+		return -1;
+
 	memset(&ev, 0, sizeof(ev));
 
 	ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
-	size = read_tracing_data_size(fd, &evlist->entries);
-	if (size <= 0)
-		return size;
+	size = tdata->size;
 	aligned_size = ALIGN(size, sizeof(u64));
 	padding = aligned_size - size;
 	ev.tracing_data.header.size = sizeof(ev.tracing_data);
@@ -1116,7 +1145,12 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 
 	process(&ev, NULL, session);
 
-	err = read_tracing_data(fd, &evlist->entries);
+	/*
+	 * The put function will copy all the tracing data
+	 * stored in temp file to the pipe.
+	 */
+	tracing_data_put(tdata);
+
 	write_padded(fd, NULL, 0, padding);
 
 	return aligned_size;
tools/perf/util/trace-event-info.c +67 -23

126 lines not on this page · raw message

tools/perf/util/trace-event.h +9 -2
index f674dda..92274b9 100644
@@ -262,8 +262,15 @@ raw_field_value(struct event *event, const char *name, void *data);
 void *raw_field_ptr(struct event *event, const char *name, void *data);
 unsigned long long eval_flag(const char *flag);
 
-int read_tracing_data(int fd, struct list_head *pattrs);
-ssize_t read_tracing_data_size(int fd, struct list_head *pattrs);
+struct tracing_data {
+	ssize_t size;
+	bool temp;
+	char temp_file[50];
+};
+
+struct tracing_data *tracing_data_get(struct list_head *pattrs,
+				      int fd, bool temp);
+void tracing_data_put(struct tracing_data *tdata);
 
 /* taken from kernel/trace/trace.h */
 enum trace_flag_type {
Signature · 1 line
1.7.4
author
#28 lore raw

v3 cover perf tools: Fix tracing info recording

In reply to Jiri Olsa

hi,
attached patches:
- 1/2 perf tools: Fix raw sample reading
- 2/2 perf tools: Fix tracing info recording

v3 changes:
- I cut off the 1/2 patch from v2, and replaced it with another fix which I found out is needed during the testing.. ;)

- The 2/2 patch is same as in v2, besides changing to the current tip tree.

thanks for comments,
jirka

---
 tools/perf/util/evsel.c            |    7 ++-
 tools/perf/util/header.c           |   50 +++++++++++++++---
 tools/perf/util/trace-event-info.c |  102 ++++++++++++++++++++++++++---------
 tools/perf/util/trace-event.h      |   11 +++-
 4 files changed, 132 insertions(+), 38 deletions(-)
author
#29 lore raw

PATCH v3 1/2 perf tools: Fix raw sample reading

Wrong pointer is being passed for raw data sanity checking, when parsing sample event.

This ends up with invalid event and perf record being stuck in __perf_session__process_events function during processing build IDs (process_buildids function).

Following command hangs up in my setup:
./perf record -e raw_syscalls:sys_enter ls

The fix is to use proper pointer to the raw data instead of the 'u' union.

---
 tools/perf/util/evsel.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
tools/perf/util/evsel.c +5 -2
index c5748c5..e389815 100644
@@ -449,6 +449,8 @@ int perf_event__parse_sample(const union perf_event *event, u64 type,
 	}
 
 	if (type & PERF_SAMPLE_RAW) {
+		const u64 *pdata;
+
 		u.val64 = *array;
 		if (WARN_ONCE(swapped,
 			      "Endianness of raw data not corrected!\n")) {
@@ -462,11 +464,12 @@ int perf_event__parse_sample(const union perf_event *event, u64 type,
 			return -EFAULT;
 
 		data->raw_size = u.val32[0];
+		pdata = (void *) array + sizeof(u32);
 
-		if (sample_overlap(event, &u.val32[1], data->raw_size))
+		if (sample_overlap(event, pdata, data->raw_size))
 			return -EFAULT;
 
-		data->raw_data = &u.val32[1];
+		data->raw_data = (void *) pdata;
 	}
 
 	return 0;
Signature · 1 line
1.7.4
Reviewed-by on v3 1/2
#30 lore raw

Re: [PATCHv3 1/2] perf tools: Fix raw sample reading

Quoted 37 lines · Jiri Olsa

Wrong pointer is being passed for raw data sanity checking, when parsing sample event.

This ends up with invalid event and perf record being stuck in __perf_session__process_events function during processing build IDs (process_buildids function).

Following command hangs up in my setup:
./perf record -e raw_syscalls:sys_enter ls

The fix is to use proper pointer to the raw data instead of the 'u' union.

---
 tools/perf/util/evsel.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
tools/perf/util/evsel.c +3 -0
index c5748c5..e389815 100644
@@ -449,6 +449,8 @@ int perf_event__parse_sample(const union perf_event *event, u64 type,
 	}
 
 	if (type & PERF_SAMPLE_RAW) {
+		const u64 *pdata;
+
 		u.val64 = *array;
 		if (WARN_ONCE(swapped,
 			      "Endianness of raw data not corrected!\n")) {
@@ -462,11 +464,12 @@ int perf_event__parse_sample(const union perf_event *event, u64 type,
 			return -EFAULT;
 
 		data->raw_size = u.val32[0];
+		pdata = (void *) array + sizeof(u32);

tools/perf/util/evsel.c int perf_event__parse_sample(const union perf_event *event, u64 type, · line 475

Quoted 10 lines
tools/perf/util/evsel.c +2 -2
 
-		if (sample_overlap(event, &u.val32[1], data->raw_size))
+		if (sample_overlap(event, pdata, data->raw_size))
 			return -EFAULT;
 
-		data->raw_data = &u.val32[1];
+		data->raw_data = (void *) pdata;
 	}
 
 	return 0;

Oops. Thanks for fixing.

author
#31 lore raw

PATCH v3 2/2 perf tools: Fix tracing info recording

In reply to Jiri Olsa

Fixing the way the tracing information is stored within record command. The current implementation is causing issues for pipe output.

Following commands fail currently:
perf script syscall-counts ls
perf record -e syscalls:sys_exit_read ls | ./perf report -i -

The tracing information is part of the perf data file. It contains several files from within the tracing debugfs and procs directories.

Beside some static header files, for each tracing event the format file is added. The /proc/kallsyms file is also added.

The tracing data are stored with preceeding size. This is causing some dificulties for pipe output, since there's no way to tell debugfs/proc file size before reading it. So, for pipe output, all the debugfs files were read twice. Once to get the overall size and once to store the content itself. This can cause problem in case any of these file changed, within the storage time.

To fix this behaviour and ensure the integrity of the tracing data, we:
- read debugfs/proc file into the temp file
- get temp file size and dump it to the pipe
- dump the temp file contents to the pipe

---
 tools/perf/util/header.c           |   50 +++++++++++++++---
 tools/perf/util/trace-event-info.c |  102 ++++++++++++++++++++++++++---------
 tools/perf/util/trace-event.h      |   11 +++-
 3 files changed, 127 insertions(+), 36 deletions(-)
tools/perf/util/header.c +42 -8
index b6c1ad1..477c73b 100644
@@ -382,18 +382,33 @@ static int perf_header__adds_write(struct perf_header *header,
 
 	sec_size = sizeof(*feat_sec) * nr_sections;
 
+	/*
+	 * We reserve header space for each section, and
+	 * update/store it later once we know the section size.
+	 */
 	sec_start = header->data_offset + header->data_size;
 	lseek(fd, sec_start + sec_size, SEEK_SET);
 
 	if (perf_header__has_feat(header, HEADER_TRACE_INFO)) {
 		struct perf_file_section *trace_sec;
+		struct tracing_data *tdata;
 
 		trace_sec = &feat_sec[idx++];
-
-		/* Write trace info */
 		trace_sec->offset = lseek(fd, 0, SEEK_CUR);
-		read_tracing_data(fd, &evlist->entries);
-		trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
+
+		/*
+		 * We work over the real file, so we can write data
+		 * directly, no temp file is needed.
+		 */
+		tdata = tracing_data_get(&evlist->entries, fd, false);
+		if (!tdata)
+			goto out_free;
+
+		/*
+		 * Update the section header information.
+		 */
+		trace_sec->size = tdata->size;
+		tracing_data_put(tdata);
 	}
 
 	if (perf_header__has_feat(header, HEADER_BUILD_ID)) {
@@ -1100,15 +1115,29 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 				   struct perf_session *session __unused)
 {
 	union perf_event ev;
+	struct tracing_data *tdata;
 	ssize_t size = 0, aligned_size = 0, padding;
 	int err __used = 0;
 
+	/*
+	 * We are going to store the size of the data followed
+	 * by the data contents. Since the fd descriptor is a pipe,
+	 * we cannot seek back to store the size of the data once
+	 * we know it. Instead we:
+	 *
+	 * - write the tracing data to the temp file
+	 * - get/write the data size to pipe
+	 * - write the tracing data from the temp file
+	 *   to the pipe
+	 */
+	tdata = tracing_data_get(&evlist->entries, fd, true);
+	if (!tdata)
+		return -1;
+
 	memset(&ev, 0, sizeof(ev));
 
 	ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
-	size = read_tracing_data_size(fd, &evlist->entries);
-	if (size <= 0)
-		return size;
+	size = tdata->size;
 	aligned_size = ALIGN(size, sizeof(u64));
 	padding = aligned_size - size;
 	ev.tracing_data.header.size = sizeof(ev.tracing_data);
@@ -1116,7 +1145,12 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 
 	process(&ev, NULL, session);
 
-	err = read_tracing_data(fd, &evlist->entries);
+	/*
+	 * The put function will copy all the tracing data
+	 * stored in temp file to the pipe.
+	 */
+	tracing_data_put(tdata);
+
 	write_padded(fd, NULL, 0, padding);
 
 	return aligned_size;
tools/perf/util/trace-event-info.c +76 -26

143 lines not on this page · raw message

tools/perf/util/trace-event.h +9 -2
index f674dda..92274b9 100644
@@ -262,8 +262,15 @@ raw_field_value(struct event *event, const char *name, void *data);
 void *raw_field_ptr(struct event *event, const char *name, void *data);
 unsigned long long eval_flag(const char *flag);
 
-int read_tracing_data(int fd, struct list_head *pattrs);
-ssize_t read_tracing_data_size(int fd, struct list_head *pattrs);
+struct tracing_data {
+	ssize_t size;
+	bool temp;
+	char temp_file[50];
+};
+
+struct tracing_data *tracing_data_get(struct list_head *pattrs,
+				      int fd, bool temp);
+void tracing_data_put(struct tracing_data *tdata);
 
 /* taken from kernel/trace/trace.h */
 enum trace_flag_type {
Signature · 1 line
1.7.4
author
#32 lore raw

Re: [PATCHv3 2/2] perf tools: Fix tracing info recording

hi,
any feedback on this?

I cut off changes colliding with Steven's change,
now it just fixies the pipe/file issue..

thanks,
jirka

Quoted 303 lines · Jiri Olsa

Fixing the way the tracing information is stored within record command. The current implementation is causing issues for pipe output.

Following commands fail currently:
perf script syscall-counts ls
perf record -e syscalls:sys_exit_read ls | ./perf report -i -

The tracing information is part of the perf data file. It contains several files from within the tracing debugfs and procs directories.

Beside some static header files, for each tracing event the format file is added. The /proc/kallsyms file is also added.

The tracing data are stored with preceeding size. This is causing some dificulties for pipe output, since there's no way to tell debugfs/proc file size before reading it. So, for pipe output, all the debugfs files were read twice. Once to get the overall size and once to store the content itself. This can cause problem in case any of these file changed, within the storage time.

To fix this behaviour and ensure the integrity of the tracing data, we:
- read debugfs/proc file into the temp file
- get temp file size and dump it to the pipe
- dump the temp file contents to the pipe

---
 tools/perf/util/header.c           |   50 +++++++++++++++---
 tools/perf/util/trace-event-info.c |  102 ++++++++++++++++++++++++++---------
 tools/perf/util/trace-event.h      |   11 +++-
 3 files changed, 127 insertions(+), 36 deletions(-)
tools/perf/util/header.c +42 -8
index b6c1ad1..477c73b 100644
@@ -382,18 +382,33 @@ static int perf_header__adds_write(struct perf_header *header,
 
 	sec_size = sizeof(*feat_sec) * nr_sections;
 
+	/*
+	 * We reserve header space for each section, and
+	 * update/store it later once we know the section size.
+	 */
 	sec_start = header->data_offset + header->data_size;
 	lseek(fd, sec_start + sec_size, SEEK_SET);
 
 	if (perf_header__has_feat(header, HEADER_TRACE_INFO)) {
 		struct perf_file_section *trace_sec;
+		struct tracing_data *tdata;
 
 		trace_sec = &feat_sec[idx++];
-
-		/* Write trace info */
 		trace_sec->offset = lseek(fd, 0, SEEK_CUR);
-		read_tracing_data(fd, &evlist->entries);
-		trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
+
+		/*
+		 * We work over the real file, so we can write data
+		 * directly, no temp file is needed.
+		 */
+		tdata = tracing_data_get(&evlist->entries, fd, false);
+		if (!tdata)
+			goto out_free;
+
+		/*
+		 * Update the section header information.
+		 */
+		trace_sec->size = tdata->size;
+		tracing_data_put(tdata);
 	}
 
 	if (perf_header__has_feat(header, HEADER_BUILD_ID)) {
@@ -1100,15 +1115,29 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 				   struct perf_session *session __unused)
 {
 	union perf_event ev;
+	struct tracing_data *tdata;
 	ssize_t size = 0, aligned_size = 0, padding;
 	int err __used = 0;
 
+	/*
+	 * We are going to store the size of the data followed
+	 * by the data contents. Since the fd descriptor is a pipe,
+	 * we cannot seek back to store the size of the data once
+	 * we know it. Instead we:
+	 *
+	 * - write the tracing data to the temp file
+	 * - get/write the data size to pipe
+	 * - write the tracing data from the temp file
+	 *   to the pipe
+	 */
+	tdata = tracing_data_get(&evlist->entries, fd, true);
+	if (!tdata)
+		return -1;
+
 	memset(&ev, 0, sizeof(ev));
 
 	ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
-	size = read_tracing_data_size(fd, &evlist->entries);
-	if (size <= 0)
-		return size;
+	size = tdata->size;
 	aligned_size = ALIGN(size, sizeof(u64));
 	padding = aligned_size - size;
 	ev.tracing_data.header.size = sizeof(ev.tracing_data);
@@ -1116,7 +1145,12 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 
 	process(&ev, NULL, session);
 
-	err = read_tracing_data(fd, &evlist->entries);
+	/*
+	 * The put function will copy all the tracing data
+	 * stored in temp file to the pipe.
+	 */
+	tracing_data_put(tdata);
+
 	write_padded(fd, NULL, 0, padding);
 
 	return aligned_size;
tools/perf/util/trace-event-info.c +76 -26
index 3403f81..c9aa4c9 100644
@@ -196,7 +196,8 @@ static void record_file(const char *file, size_t hdr_sz)
 		die("Can't read '%s'", file);
 
 	/* put in zeros for file size, then fill true size later */
-	write_or_die(&size, hdr_sz);
+	if (hdr_sz)
+		write_or_die(&size, hdr_sz);
 
 	do {
 		r = read(fd, buf, BUFSIZ);
@@ -212,7 +213,7 @@ static void record_file(const char *file, size_t hdr_sz)
 	if (bigendian())
 		sizep += sizeof(u64) - hdr_sz;
 
-	if (pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
+	if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
 		die("writing to %s", output_file);
 }
 
@@ -428,6 +429,19 @@ get_tracepoints_path(struct list_head *pattrs)
 	return nr_tracepoints > 0 ? path.next : NULL;
 }
 
+static void
+put_tracepoints_path(struct tracepoint_path *tps)
+{
+	while (tps) {
+		struct tracepoint_path *t = tps;
+
+		tps = tps->next;
+		free(t->name);
+		free(t->system);
+		free(t);
+	}
+}
+
 bool have_tracepoints(struct list_head *pattrs)
 {
 	struct perf_evsel *pos;
@@ -439,19 +453,11 @@ bool have_tracepoints(struct list_head *pattrs)
 	return false;
 }
 
-int read_tracing_data(int fd, struct list_head *pattrs)
+static void tracing_data_header(void)
 {
-	char buf[BUFSIZ];
-	struct tracepoint_path *tps = get_tracepoints_path(pattrs);
-
-	/*
-	 * What? No tracepoints? No sense writing anything here, bail out.
-	 */
-	if (tps == NULL)
-		return -1;
-
-	output_fd = fd;
+	char buf[50];
 
+	/* just guessing this is someone's birthday.. ;) */
 	buf[0] = 23;
 	buf[1] = 8;
 	buf[2] = 68;
@@ -476,28 +482,72 @@ int read_tracing_data(int fd, struct list_head *pattrs)
 	/* save page_size */
 	page_size = sysconf(_SC_PAGESIZE);
 	write_or_die(&page_size, 4);
+}
+
+struct tracing_data *tracing_data_get(struct list_head *pattrs,
+				      int fd, bool temp)
+{
+	struct tracepoint_path *tps;
+	struct tracing_data *tdata;
+
+	output_fd = fd;
+
+	tps = get_tracepoints_path(pattrs);
+	if (!tps)
+		return NULL;
+
+	tdata = malloc_or_die(sizeof(*tdata));
+	tdata->temp = temp;
+	tdata->size = 0;
+
+	if (temp) {
+		int temp_fd;
+
+		snprintf(tdata->temp_file, sizeof(tdata->temp_file),
+			 "/tmp/perf-XXXXXX");
+		if (!mkstemp(tdata->temp_file))
+			die("Can't make temp file");
+
+		temp_fd = open(tdata->temp_file, O_RDWR);
+		if (temp_fd < 0)
+			die("Can't read '%s'", tdata->temp_file);
+
+		/*
+		 * Set the temp file the default output, so all the
+		 * tracing data are stored into it.
+		 */
+		output_fd = temp_fd;
+	} else
+		tdata->size = -lseek(output_fd, 0, SEEK_CUR);
 
+	tracing_data_header();
 	read_header_files();
 	read_ftrace_files(tps);
 	read_event_files(tps);
 	read_proc_kallsyms();
 	read_ftrace_printk();
 
-	return 0;
-}
+	tdata->size += lseek(output_fd, 0, SEEK_CUR);
 
-ssize_t read_tracing_data_size(int fd, struct list_head *pattrs)
-{
-	ssize_t size;
-	int err = 0;
+	/*
+	 * All tracing data are stored by now, we can restore
+	 * the default output file in case we used temp file.
+	 */
+	if (temp) {
+		close(output_fd);
+		output_fd = fd;
+	}
 
-	calc_data_size = 1;
-	err = read_tracing_data(fd, pattrs);
-	size = calc_data_size - 1;
-	calc_data_size = 0;
+	put_tracepoints_path(tps);
+	return tdata;
+}
 
-	if (err < 0)
-		return err;
+void tracing_data_put(struct tracing_data *tdata)
+{
+	if (tdata->temp) {
+		record_file(tdata->temp_file, 0);
+		unlink(tdata->temp_file);
+	}
 
-	return size;
+	free(tdata);
 }
tools/perf/util/trace-event.h +9 -2
index f674dda..92274b9 100644
@@ -262,8 +262,15 @@ raw_field_value(struct event *event, const char *name, void *data);
 void *raw_field_ptr(struct event *event, const char *name, void *data);
 unsigned long long eval_flag(const char *flag);
 
-int read_tracing_data(int fd, struct list_head *pattrs);
-ssize_t read_tracing_data_size(int fd, struct list_head *pattrs);
+struct tracing_data {
+	ssize_t size;
+	bool temp;
+	char temp_file[50];
+};
+
+struct tracing_data *tracing_data_get(struct list_head *pattrs,
+				      int fd, bool temp);
+void tracing_data_put(struct tracing_data *tdata);
 
 /* taken from kernel/trace/trace.h */
 enum trace_flag_type {
Signature · 7 lines
1.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
author
#33 lore raw

PATCH v4 1/1 perf tools: Fix tracing info recording

hi,
sending version updated to the latest tip tree code.

wbr,
jirka

---
Fixing the way the tracing information is stored within record command. The current implementation is causing issues for pipe output.

Following commands fail currently:
perf script syscall-counts ls
perf record -e syscalls:sys_exit_read ls | ./perf report -i -

The tracing information is part of the perf data file. It contains several files from within the tracing debugfs and procs directories.

Beside some static header files, for each tracing event the format file is added. The /proc/kallsyms file is also added.

The tracing data are stored with preceeding size. This is causing some dificulties for pipe output, since there's no way to tell debugfs/proc file size before reading it. So, for pipe output, all the debugfs files were read twice. Once to get the overall size and once to store the content itself. This can cause problem in case any of these file changed, within the storage time.

To fix this behaviour and ensure the integrity of the tracing data, we:
- read debugfs/proc file into the temp file
- get temp file size and dump it to the pipe
- dump the temp file contents to the pipe

---
 tools/perf/util/header.c           |   27 +++++++-
 tools/perf/util/trace-event-info.c |  112 ++++++++++++++++++++++++++++--------
 tools/perf/util/trace-event.h      |   13 ++++-
 3 files changed, 123 insertions(+), 29 deletions(-)
tools/perf/util/header.c +23 -4
index 6a9c041..76c0b2c 100644
@@ -2218,15 +2218,29 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 				   struct perf_session *session __unused)
 {
 	union perf_event ev;
+	struct tracing_data *tdata;
 	ssize_t size = 0, aligned_size = 0, padding;
 	int err __used = 0;
 
+	/*
+	 * We are going to store the size of the data followed
+	 * by the data contents. Since the fd descriptor is a pipe,
+	 * we cannot seek back to store the size of the data once
+	 * we know it. Instead we:
+	 *
+	 * - write the tracing data to the temp file
+	 * - get/write the data size to pipe
+	 * - write the tracing data from the temp file
+	 *   to the pipe
+	 */
+	tdata = tracing_data_get(&evlist->entries, fd, true);
+	if (!tdata)
+		return -1;
+
 	memset(&ev, 0, sizeof(ev));
 
 	ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
-	size = read_tracing_data_size(fd, &evlist->entries);
-	if (size <= 0)
-		return size;
+	size = tdata->size;
 	aligned_size = ALIGN(size, sizeof(u64));
 	padding = aligned_size - size;
 	ev.tracing_data.header.size = sizeof(ev.tracing_data);
@@ -2234,7 +2248,12 @@ int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
 
 	process(&ev, NULL, session);
 
-	err = read_tracing_data(fd, &evlist->entries);
+	/*
+	 * The put function will copy all the tracing data
+	 * stored in temp file to the pipe.
+	 */
+	tracing_data_put(tdata);
+
 	write_padded(fd, NULL, 0, padding);
 
 	return aligned_size;
tools/perf/util/trace-event-info.c +88 -24

155 lines not on this page · raw message

tools/perf/util/trace-event.h +12 -1
index f674dda..a841008 100644
@@ -263,7 +263,18 @@ void *raw_field_ptr(struct event *event, const char *name, void *data);
 unsigned long long eval_flag(const char *flag);
 
 int read_tracing_data(int fd, struct list_head *pattrs);
-ssize_t read_tracing_data_size(int fd, struct list_head *pattrs);
+
+struct tracing_data {
+	/* size is only valid if temp is 'true' */
+	ssize_t size;
+	bool temp;
+	char temp_file[50];
+};
+
+struct tracing_data *tracing_data_get(struct list_head *pattrs,
+				      int fd, bool temp);
+void tracing_data_put(struct tracing_data *tdata);
+
 
 /* taken from kernel/trace/trace.h */
 enum trace_flag_type {
Signature · 1 line
1.7.4
#34 lore raw

Re: [PATCHv4] perf tools: Fix tracing info recording

Em Thu, Oct 20, 2011 at 03:59:43PM +0200, Jiri Olsa escreveu:

Quoted 2 lines

hi,
sending version updated to the latest tip tree code.

Thanks for refreshing it, applied. I'll push it together with some other changes after Ingo pulls what I have in perf/core.

- Arnaldo