Skip to contentSkip to conversation

perf tools: add meta-data header support in pipe mode

PATCH Superseded Stephane Eranian 2 participants 9 messages Apr 11, 2012 – May 14, 2012 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover perf tools: add meta-data header support in pipe mode

This patch series adds meta-data support to perf record/report in pipe mode:
perf record -o - foo | perf inject -b | perf report -i -

We had meta-data (information about host configuration, perf tool version,...) in regular (file) mode but it was lacking in pipe mode. This patch series fixes this.

In pipe mode, there is no meta-data header structures at the beginning of the streamed data given we cannot seek in a pipe. Instead, we need to create pseudo record types for each of the possible features, e.g., hostname, cpuid, event_desc, and so on. Those pseudo records are guaranteed to be before any actual sample records, therefore perf report/annotate are guaranteed to get the information they need before processing the first sample.

The series also fixes perf inject to actually inject build-ids and buildid-list to work better with pipe mode, i.e., print the build-ids.

With this series:

 $ perf record -o - noploop 2 | perf inject -b | perf report -i -
      # ========
      # captured on: Fri Jan 20 18:13:55 2012
      # ========
      #
      # hostname : quad
      # os release : 3.2.0-rc7-tip
      # perf version : 3.2.0
      # arch : x86_64
      # nrcpus online : 4
      # nrcpus avail : 4
      # cpudesc : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
      # cpuid : GenuineIntel,6,15,11
      # total memory : 8092884 kB
      ...
      # HEADER_CPU_TOPOLOGY info available, use -I to display
      noploop for 2 seconds
      [ perf record: Woken up 1 times to write data ]
      [ perf record: Captured and wrote 0.084 MB - (~3677 samples) ]
        99.80%  noploop  noploop            [.] noploop
         0.19%  noploop  [kernel.kallsyms]  [k] radix_tree_gang_lookup

Stephane Eranian (5):
perf inject: fix broken perf inject -b
perf tools: fix piped mode read code
perf tools: rename HEADER_TRACE_INFO to HEADER_TRACING_DATA perf record: add meta-data support for pipe-mode
perf: make perf buildid-list work better with pipe mode

 tools/perf/builtin-annotate.c     |    5 +
 tools/perf/builtin-buildid-list.c |    6 +-
 tools/perf/builtin-inject.c       |    6 +
 tools/perf/builtin-record.c       |   11 +-
 tools/perf/builtin-report.c       |    7 +-
 tools/perf/util/build-id.c        |    2 +
 tools/perf/util/event.c           |   12 +
 tools/perf/util/event.h           |   18 ++
 tools/perf/util/header.c          |  538 ++++++++++++++++++++++++++-----------
 tools/perf/util/header.h          |   11 +-
 tools/perf/util/session.c         |   52 +++-
 tools/perf/util/tool.h            |    4 +-
 12 files changed, 494 insertions(+), 178 deletions(-)
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH 1/5 perf inject: fix broken perf inject -b

perf inject -b was broken. It would not inject any build_id into the stream. Furthermore, it would strip samples from the stream.

The reason was a missing initialization of the event attribute structure. The perf_tool.tool.attr() callback was pointing to a simple repipe. But there was no initialization of the internal data structures to keep track of events and event ids. That later caused event id lookups to fail, and sample would get removed.

The patch simply adds back the call to perf_event__process_attr() to initialize the evlist structure and now build_ids are again injected.

---
 tools/perf/builtin-inject.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
tools/perf/builtin-inject.c +5 -0
index 09c1061..3beab48 100644
@@ -60,6 +60,11 @@ static int perf_event__repipe_tracing_data_synth(union perf_event *event,
 static int perf_event__repipe_attr(union perf_event *event,
 				   struct perf_evlist **pevlist __used)
 {
+	int ret;
+	ret = perf_event__process_attr(event, pevlist);
+	if (ret)
+		return ret;
+
 	return perf_event__repipe_synth(NULL, event, NULL);
 }
 
Signature · 1 line
1.7.4.1
#2 lore raw

PATCH 2/5 perf tools: fix piped mode read code

In reply to Stephane Eranian

In __perf_session__process_pipe_events(), there was a risk we would read more than what a union perf_event struct can hold. this could happen in case, perf is reading a file which contains new record types it does not know about and which are larger than anything it knows about.

In general, perf is supposed to skip records it does not understand, but in pipe mode, those have to be read and ignored. The fixed size header contains the size of the record, but that size may be larger than union perf_event, yet it was used as the backing to the read in:

union perf_event event;
void *p;

  size = event->header.size;

  p = &event;
  p += sizeof(struct perf_event_header);
  if (size - sizeof(struct perf_event_header)) {
    err = readn(self->fd, p, size - sizeof(struct perf_event_header));

We fix this by allocating a buffer based on the size reported in the header. We reuse the buffer as much as we can. We realloc in case it becomes too small. In the common case, the performance impact is negligible.

---
 tools/perf/util/session.c |   35 +++++++++++++++++++++++++++--------
 1 files changed, 27 insertions(+), 8 deletions(-)
tools/perf/util/session.c +27 -8
index 9412e3b..d13e915 100644
@@ -1056,8 +1056,9 @@ volatile int session_done;
 static int __perf_session__process_pipe_events(struct perf_session *self,
 					       struct perf_tool *tool)
 {
-	union perf_event event;
-	uint32_t size;
+	union perf_event *event;
+	uint32_t size, cur_size = 0;
+	void *buf = NULL;
 	int skip = 0;
 	u64 head;
 	int err;
@@ -1066,8 +1067,14 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
 	perf_tool__fill_defaults(tool);
 
 	head = 0;
+	cur_size = sizeof(union perf_event);
+
+	buf = malloc(cur_size);
+	if (!buf)
+		return -errno;
 more:
-	err = readn(self->fd, &event, sizeof(struct perf_event_header));
+	event = buf;
+	err = readn(self->fd, event, sizeof(struct perf_event_header));
 	if (err <= 0) {
 		if (err == 0)
 			goto done;
@@ -1077,13 +1084,23 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
 	}
 
 	if (self->header.needs_swap)
-		perf_event_header__bswap(&event.header);
+		perf_event_header__bswap(&event->header);
 
-	size = event.header.size;
+	size = event->header.size;
 	if (size == 0)
 		size = 8;
 
-	p = &event;
+	if (size > cur_size) {
+		void *new = realloc(buf, size);
+		if (!new) {
+			pr_err("failed to allocate memory to read event\n");
+			goto out_err;
+		}
+		buf = new;
+		cur_size = size;
+		event = buf;
+	}
+	p = event;
 	p += sizeof(struct perf_event_header);
 
 	if (size - sizeof(struct perf_event_header)) {
@@ -1099,9 +1116,10 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
 		}
 	}
 
-	if ((skip = perf_session__process_event(self, &event, tool, head)) < 0) {
+	skip = perf_session__process_event(self, event, tool, head);
+	if (skip < 0) {
 		dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
-			    head, event.header.size, event.header.type);
+			    head, event->header.size, event->header.type);
 		/*
 		 * assume we lost track of the stream, check alignment, and
 		 * increment a single u64 in the hope to catch on again 'soon'.
@@ -1122,6 +1140,7 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
 done:
 	err = 0;
 out_err:
+	free(buf);
 	perf_session__warn_about_errors(self, tool);
 	perf_session_free_sample_buffers(self);
 	return err;
Signature · 1 line
1.7.4.1
#3 lore raw

Re: [PATCH 2/5] perf tools: fix piped mode read code

Hi Stephane:

This patch no longer applies cleanly. Can you update the series?

David

Quoted 112 lines · Stephane Eranian

In __perf_session__process_pipe_events(), there was a risk we would read more than what a union perf_event struct can hold. this could happen in case, perf is reading a file which contains new record types it does not know about and which are larger than anything it knows about.

In general, perf is supposed to skip records it does not understand, but in pipe mode, those have to be read and ignored. The fixed size header contains the size of the record, but that size may be larger than union perf_event, yet it was used as the backing to the read in:

   union perf_event event;
   void *p;

   size = event->header.size;

   p =&event;
   p += sizeof(struct perf_event_header);
   if (size - sizeof(struct perf_event_header)) {
     err = readn(self->fd, p, size - sizeof(struct perf_event_header));

We fix this by allocating a buffer based on the size reported in the header. We reuse the buffer as much as we can. We realloc in case it becomes too small. In the common case, the performance impact is negligible.

---
  tools/perf/util/session.c |   35 +++++++++++++++++++++++++++--------
  1 files changed, 27 insertions(+), 8 deletions(-)
tools/perf/util/session.c +27 -8
index 9412e3b..d13e915 100644
@@ -1056,8 +1056,9 @@ volatile int session_done;
  static int __perf_session__process_pipe_events(struct perf_session *self,
  					       struct perf_tool *tool)
  {
-	union perf_event event;
-	uint32_t size;
+	union perf_event *event;
+	uint32_t size, cur_size = 0;
+	void *buf = NULL;
  	int skip = 0;
  	u64 head;
  	int err;
@@ -1066,8 +1067,14 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
  	perf_tool__fill_defaults(tool);
 
  	head = 0;
+	cur_size = sizeof(union perf_event);
+
+	buf = malloc(cur_size);
+	if (!buf)
+		return -errno;
  more:
-	err = readn(self->fd,&event, sizeof(struct perf_event_header));
+	event = buf;
+	err = readn(self->fd, event, sizeof(struct perf_event_header));
  	if (err<= 0) {
  		if (err == 0)
  			goto done;
@@ -1077,13 +1084,23 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
  	}
 
  	if (self->header.needs_swap)
-		perf_event_header__bswap(&event.header);
+		perf_event_header__bswap(&event->header);
 
-	size = event.header.size;
+	size = event->header.size;
  	if (size == 0)
  		size = 8;
 
-	p =&event;
+	if (size>  cur_size) {
+		void *new = realloc(buf, size);
+		if (!new) {
+			pr_err("failed to allocate memory to read event\n");
+			goto out_err;
+		}
+		buf = new;
+		cur_size = size;
+		event = buf;
+	}
+	p = event;
  	p += sizeof(struct perf_event_header);
 
  	if (size - sizeof(struct perf_event_header)) {
@@ -1099,9 +1116,10 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
  		}
  	}
 
-	if ((skip = perf_session__process_event(self,&event, tool, head))<  0) {
+	skip = perf_session__process_event(self, event, tool, head);
+	if (skip<  0) {
  		dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
-			    head, event.header.size, event.header.type);
+			    head, event->header.size, event->header.type);
  		/*
  		 * assume we lost track of the stream, check alignment, and
  		 * increment a single u64 in the hope to catch on again 'soon'.
@@ -1122,6 +1140,7 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
  done:
  	err = 0;
  out_err:
+	free(buf);
  	perf_session__warn_about_errors(self, tool);
  	perf_session_free_sample_buffers(self);
  	return err;
#4 lore raw

Re: [PATCH 2/5] perf tools: fix piped mode read code

Quoted 4 lines · David Ahern

Hi Stephane:

This patch no longer applies cleanly. Can you update the series?

Will post the series again tomorrow.
Arnaldo, you need to take a look at this, it's been posted weeks ago. We need this patch to make perf in pipe mode behave like perf in file mode.
Thanks

Quoted 129 lines

David

Quoted 123 lines · Stephane Eranian

In __perf_session__process_pipe_events(), there was a risk we would read more than what a union perf_event struct can hold. this could happen in case, perf is reading a file which contains new record types it does not know about and which are larger than anything it knows about.

In general, perf is supposed to skip records it does not understand, but in pipe mode, those have to be read and ignored. The fixed size header contains the size of the record, but that size may be larger than union perf_event, yet it was used as the backing to the read in:

  union perf_event event;
  void *p;

  size = event->header.size;

  p =&event;
  p += sizeof(struct perf_event_header);
  if (size - sizeof(struct perf_event_header)) {
    err = readn(self->fd, p, size - sizeof(struct perf_event_header));

We fix this by allocating a buffer based on the size reported in the header. We reuse the buffer as much as we can. We realloc in case it becomes too small. In the  common case, the performance impact is negligible.

---
 tools/perf/util/session.c |   35 +++++++++++++++++++++++++++--------

 1 files changed, 27 insertions(+), 8 deletions(-)
tools/perf/util/session.c +8 -2
index 9412e3b..d13e915 100644
@@ -1056,8 +1056,9 @@ volatile int session_done;
  static int __perf_session__process_pipe_events(struct perf_session *self,
                                               struct perf_tool *tool)
  {
-       union perf_event event;
-       uint32_t size;
+       union perf_event *event;
+       uint32_t size, cur_size = 0;
+       void *buf = NULL;
        int skip = 0;
        u64 head;
        int err;
@@ -1066,8 +1067,14 @@ static int
 __perf_session__process_pipe_events(struct perf_session *self,
        perf_tool__fill_defaults(tool);
 
        head = 0;
+       cur_size = sizeof(union perf_event);
+
+       buf = malloc(cur_size);
+       if (!buf)
+               return -errno;

 more:
-       err = readn(self->fd,&event, sizeof(struct perf_event_header));

+       event = buf;
+       err = readn(self->fd, event, sizeof(struct perf_event_header)); if (err<= 0) {
               if (err == 0)
                       goto done;

tools/perf/util/session.c +13 -3
@@ -1077,13 +1084,23 @@ static int
 __perf_session__process_pipe_events(struct perf_session *self,
        }
 
        if (self->header.needs_swap)
-               perf_event_header__bswap(&event.header);
+               perf_event_header__bswap(&event->header);
 
-       size = event.header.size;
+       size = event->header.size;
        if (size == 0)
                size = 8;
 
-       p =&event;
+       if (size>  cur_size) {
+               void *new = realloc(buf, size);
+               if (!new) {
+                       pr_err("failed to allocate memory to read
 event\n");
+                       goto out_err;
+               }
+               buf = new;
+               cur_size = size;
+               event = buf;
+       }
+       p = event;
        p += sizeof(struct perf_event_header);

       if (size - sizeof(struct perf_event_header)) {

tools/perf/util/session.c +2 -1
@@ -1099,9 +1116,10 @@ static int
 __perf_session__process_pipe_events(struct perf_session *self,
                }
        }
 
-       if ((skip = perf_session__process_event(self,&event, tool, head))<
  0) {
 
+       skip = perf_session__process_event(self, event, tool, head);
+       if (skip<  0) {

               dump_printf("%#" PRIx64 " [%#x]: skipping unknown header
type: %d\n",
-                           head, event.header.size, event.header.type);
+                           head, event->header.size, event->header.type); /*
                * assume we lost track of the stream, check alignment, and
                * increment a single u64 in the hope to catch on again 'soon'.

tools/perf/util/session.c +0 -0
@@ -1122,6 +1140,7 @@ static int
 __perf_session__process_pipe_events(struct perf_session *self,

 done:
       err = 0;
 out_err:
+       free(buf); perf_session__warn_about_errors(self, tool); perf_session_free_sample_buffers(self); return err;

#5 lore raw

PATCH 3/5 perf tools: rename HEADER_TRACE_INFO to HEADER_TRACING_DATA

In reply to Stephane Eranian

To match the PERF_RECORD_HEADER_TRACING_DATA record type. This is the same info as the one used for pipe mode whereas the other one is for regular file output. This will help in the later patch to add meta-data infos in pipe mode.

---
 tools/perf/builtin-record.c |    4 ++--
 tools/perf/util/header.c    |   10 +++++-----
 tools/perf/util/header.h    |    2 +-
 3 files changed, 8 insertions(+), 8 deletions(-)
tools/perf/builtin-record.c +2 -2
index be4e1ee..2a64679 100644
@@ -389,7 +389,7 @@ static void perf_record__mmap_read_all(struct perf_record *rec)
 			perf_record__mmap_read(rec, &rec->evlist->mmap[i]);
 	}
 
-	if (perf_header__has_feat(&rec->session->header, HEADER_TRACE_INFO))
+	if (perf_header__has_feat(&rec->session->header, HEADER_TRACING_DATA))
 		write_output(rec, &finished_round_event, sizeof(finished_round_event));
 }
 
@@ -471,7 +471,7 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
 		perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
 
 	if (!have_tracepoints(&evsel_list->entries))
-		perf_header__clear_feat(&session->header, HEADER_TRACE_INFO);
+		perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
 
 	if (!rec->opts.branch_stack)
 		perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
tools/perf/util/header.c +5 -5
index 4c7c2d7..99af448 100644
@@ -442,7 +442,7 @@ static bool perf_session__read_build_ids(struct perf_session *session, bool with
 	return ret;
 }
 
-static int write_trace_info(int fd, struct perf_header *h __used,
+static int write_tracing_data(int fd, struct perf_header *h __used,
 			    struct perf_evlist *evlist)
 {
 	return read_tracing_data(fd, &evlist->entries);
@@ -1477,7 +1477,7 @@ static int perf_header__read_build_ids(struct perf_header *header,
 	return err;
 }
 
-static int process_trace_info(struct perf_file_section *section __unused,
+static int process_tracing_data(struct perf_file_section *section __unused,
 			      struct perf_header *ph __unused,
 			      int feat __unused, int fd)
 {
@@ -1513,11 +1513,11 @@ struct feature_ops {
 		.full_only = true }
 
 /* feature_ops not implemented: */
-#define print_trace_info		NULL
-#define print_build_id			NULL
+#define print_tracing_data	NULL
+#define print_build_id		NULL
 
 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
-	FEAT_OPP(HEADER_TRACE_INFO,	trace_info),
+	FEAT_OPP(HEADER_TRACING_DATA,	tracing_data),
 	FEAT_OPP(HEADER_BUILD_ID,	build_id),
 	FEAT_OPA(HEADER_HOSTNAME,	hostname),
 	FEAT_OPA(HEADER_OSRELEASE,	osrelease),
tools/perf/util/header.h +1 -1
index 21a6be0..2d42b3e 100644
@@ -12,7 +12,7 @@
 enum {
 	HEADER_RESERVED		= 0,	/* always cleared */
 	HEADER_FIRST_FEATURE	= 1,
-	HEADER_TRACE_INFO	= 1,
+	HEADER_TRACING_DATA	= 1,
 	HEADER_BUILD_ID,
 
 	HEADER_HOSTNAME,
Signature · 1 line
1.7.4.1
#6 lore raw

PATCH 4/5 perf record: add meta-data support for pipe-mode

In reply to Stephane Eranian

This patch adds the meta-data header support for perf record when used in pipe mode: perf record -o -

Up until now, meta-data was only available when perf record was used in "regular" mode, i.e., generating a perf.data file. For users depending on pipe mode, no host, event header information was gathered. This patch addresses this limitation.

The difficulty in pipe mode is that information needs to be written sequentially to the pipe. Meta data headers are usually generated (and also expected) at the beginning of the file (or piped output). To solve this problem, we introduce new synthetic record types, one for each meta-data type. The approach is similar to what is already used for BUILD_ID and TRACING_DATA.

We have modified util/header.c such that the same routines are used to generate and read the meta-data information regardless of pipe-mode vs. regular mode. To make this work, we added a new struct called feat_fd which encapsulates all the information necessary to read or write meta-data information to a file/pipe or from a file/pipe.

It should be noted that there is a limitation with the current perf in terms of endianess in pipe mode. Perf assumes the records are generated using the same endianess during collection and analysis. That is always the case with the example shown below. However, one could also do:

$ perf record -o - noploop 2 | perf inject -b >perf.data
$ cat perf.data | perf report -i -

With this patch, it is possible to get:

 $ perf record -o - noploop 2 | perf inject -b | perf report -i -
      # ========
      # captured on: Fri Jan 20 18:13:55 2012
      # ========
      #
      # hostname : quad
      # os release : 3.2.0-rc7-tip
      # perf version : 3.2.0
      # arch : x86_64
      # nrcpus online : 4
      # nrcpus avail : 4
      # cpudesc : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
      # cpuid : GenuineIntel,6,15,11
      # total memory : 8092884 kB
      ...
      # HEADER_CPU_TOPOLOGY info available, use -I to display
      noploop for 2 seconds
      [ perf record: Woken up 1 times to write data ]
      [ perf record: Captured and wrote 0.084 MB - (~3677 samples) ]
        99.80%  noploop  noploop            [.] noploop
         0.19%  noploop  [kernel.kallsyms]  [k] radix_tree_gang_lookup
---
 tools/perf/builtin-annotate.c |    5 +
 tools/perf/builtin-inject.c   |    1 +
 tools/perf/builtin-record.c   |    7 +
 tools/perf/builtin-report.c   |    7 +-
 tools/perf/util/event.c       |   12 +
 tools/perf/util/event.h       |   18 ++
 tools/perf/util/header.c      |  532 +++++++++++++++++++++++++++++------------
 tools/perf/util/header.h      |    9 +
 tools/perf/util/session.c     |   17 ++-
 tools/perf/util/tool.h        |    4 +-
 10 files changed, 449 insertions(+), 163 deletions(-)
tools/perf/builtin-annotate.c +5 -0
index 806e0a2..fe42298 100644
@@ -247,6 +247,11 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
 			.mmap	= perf_event__process_mmap,
 			.comm	= perf_event__process_comm,
 			.fork	= perf_event__process_task,
+			.attr	= perf_event__process_attr,
+			.event_type	 = perf_event__process_event_type,
+			.tracing_data	 = perf_event__process_tracing_data,
+			.build_id	 = perf_event__process_build_id,
+			.feature	 = perf_event__process_feature,
 			.ordered_samples = true,
 			.ordering_requires_timestamps = true,
 		},
tools/perf/builtin-inject.c +1 -0
index 3beab48..65b0529 100644
@@ -217,6 +217,7 @@ struct perf_tool perf_inject = {
 	.event_type	= perf_event__repipe_event_type_synth,
 	.tracing_data	= perf_event__repipe_tracing_data_synth,
 	.build_id	= perf_event__repipe_op2_synth,
+	.feature	= perf_event__repipe_op2_synth,
 };
 
 extern volatile int session_done;
tools/perf/builtin-record.c +7 -0
index 2a64679..175acdf 100644
@@ -524,6 +524,13 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
 	}
 
 	if (opts->pipe_output) {
+		err = perf_event__synthesize_features(tool, session, evsel_list,
+						   process_synthesized_event);
+		if (err < 0) {
+			pr_err("Couldn't synthesize features.\n");
+			return err;
+		}
+
 		err = perf_event__synthesize_attrs(tool, session,
 						   process_synthesized_event);
 		if (err < 0) {
tools/perf/builtin-report.c +4 -3
index 2e31743..350b888 100644
@@ -43,7 +43,6 @@ struct perf_report {
 	bool			force, use_tui, use_gtk, use_stdio;
 	bool			hide_unresolved;
 	bool			dont_use_callchains;
-	bool			show_full_info;
 	bool			show_threads;
 	bool			inverted_callchain;
 	struct perf_read_values	show_threads_values;
@@ -355,7 +354,8 @@ static int __cmd_report(struct perf_report *rep)
 	}
 
 	if (use_browser <= 0)
-		perf_session__fprintf_info(session, stdout, rep->show_full_info);
+		perf_session__fprintf_info(session, stdout,
+					   rep->tool.show_all_features);
 
 	if (rep->show_threads)
 		perf_read_values_init(&rep->show_threads_values);
@@ -552,6 +552,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __used)
 			.event_type	 = perf_event__process_event_type,
 			.tracing_data	 = perf_event__process_tracing_data,
 			.build_id	 = perf_event__process_build_id,
+			.feature	 = perf_event__process_feature,
 			.ordered_samples = true,
 			.ordering_requires_timestamps = true,
 		},
@@ -615,7 +616,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __used)
 		    "Look for files with symbols relative to this directory"),
 	OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
 		   "list of cpus to profile"),
-	OPT_BOOLEAN('I', "show-info", &report.show_full_info,
+	OPT_BOOLEAN('I', "show-info", &report.tool.show_all_features,
 		    "Display extended information about perf.data file"),
 	OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
 		    "Interleave source code with assembly code (default)"),
tools/perf/util/event.c +12 -0
index 2a6f33c..da3551f 100644
@@ -23,6 +23,18 @@ static const char *perf_event__names[] = {
 	[PERF_RECORD_HEADER_TRACING_DATA]	= "TRACING_DATA",
 	[PERF_RECORD_HEADER_BUILD_ID]		= "BUILD_ID",
 	[PERF_RECORD_FINISHED_ROUND]		= "FINISHED_ROUND",
+	[PERF_RECORD_HEADER_HOSTNAME]		= "HOSTNAME",
+	[PERF_RECORD_HEADER_OSRELEASE]		= "OSRELEASE",
+	[PERF_RECORD_HEADER_VERSION]		= "VERSION",
+	[PERF_RECORD_HEADER_ARCH]		= "ARCH",
+	[PERF_RECORD_HEADER_NRCPUS]		= "NRCPUS",
+	[PERF_RECORD_HEADER_CPUDESC]		= "CPUDESC",
+	[PERF_RECORD_HEADER_CPUID]		= "CPUID",
+	[PERF_RECORD_HEADER_TOTAL_MEM]		= "TOTAL_MEM",
+	[PERF_RECORD_HEADER_CMDLINE]		= "CMDLINE",
+	[PERF_RECORD_HEADER_EVENT_DESC]		= "EVENT_DESC",
+	[PERF_RECORD_HEADER_CPU_TOPOLOGY]	= "CPU_TOPOLOGY",
+	[PERF_RECORD_HEADER_NUMA_TOPOLOGY]	= "NUMA_TOPOLOGY",
 };
 
 const char *perf_event__name(unsigned int id)
tools/perf/util/event.h +18 -0
index 1b19728..e44aaec 100644
@@ -100,6 +100,18 @@ enum perf_user_event_type { /* above any possible kernel type */
 	PERF_RECORD_HEADER_TRACING_DATA		= 66,
 	PERF_RECORD_HEADER_BUILD_ID		= 67,
 	PERF_RECORD_FINISHED_ROUND		= 68,
+	PERF_RECORD_HEADER_HOSTNAME		= 69,
+	PERF_RECORD_HEADER_OSRELEASE		= 70,
+	PERF_RECORD_HEADER_VERSION		= 71,
+	PERF_RECORD_HEADER_ARCH			= 72,
+	PERF_RECORD_HEADER_NRCPUS		= 73,
+	PERF_RECORD_HEADER_CPUDESC		= 74,
+	PERF_RECORD_HEADER_CPUID		= 75,
+	PERF_RECORD_HEADER_TOTAL_MEM		= 76,
+	PERF_RECORD_HEADER_CMDLINE		= 77,
+	PERF_RECORD_HEADER_EVENT_DESC		= 78,
+	PERF_RECORD_HEADER_CPU_TOPOLOGY		= 79,
+	PERF_RECORD_HEADER_NUMA_TOPOLOGY	= 80,
 	PERF_RECORD_HEADER_MAX
 };
 
@@ -126,6 +138,11 @@ struct tracing_data_event {
 	u32 size;
 };
 
+struct feature_event {
+	struct perf_event_header header;
+	char data[]; /* size bytes of raw data specific to the feature */
+};
+
 union perf_event {
 	struct perf_event_header	header;
 	struct ip_event			ip;
@@ -139,6 +156,7 @@ union perf_event {
 	struct event_type_event		event_type;
 	struct tracing_data_event	tracing_data;
 	struct build_id_event		build_id;
+	struct feature_event		feat;
 };
 
 void perf_event__print_totals(void);
tools/perf/util/header.c +375 -157

1,076 lines not on this page · raw message

tools/perf/util/header.h +9 -0
index 2d42b3e..af4db9e 100644
@@ -98,6 +98,15 @@ int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
 			  const char *name, bool is_kallsyms);
 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir);
 
+int perf_event__synthesize_features(struct perf_tool *tool,
+				    struct perf_session *session,
+				    struct perf_evlist *evlist,
+				    perf_event__handler_t process);
+
+int perf_event__process_feature(struct perf_tool *tool,
+				union perf_event *event,
+				 struct perf_session *session);
+
 int perf_event__synthesize_attr(struct perf_tool *tool,
 				struct perf_event_attr *attr, u16 ids, u64 *id,
 				perf_event__handler_t process);
tools/perf/util/session.c +15 -2
index d13e915..84012fa 100644
@@ -390,6 +390,14 @@ static int process_event_type_stub(struct perf_tool *tool __used,
 	return 0;
 }
 
+static int process_feature_stub(struct perf_tool *tool __used,
+				union perf_event *event __used,
+				struct perf_session *session __used)
+{
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
 static int process_finished_round(struct perf_tool *tool,
 				  union perf_event *event,
 				  struct perf_session *session);
@@ -428,6 +436,8 @@ static void perf_tool__fill_defaults(struct perf_tool *tool)
 		else
 			tool->finished_round = process_finished_round_stub;
 	}
+	if (tool->feature == NULL)
+		tool->feature = process_feature_stub;
 }
 
 void mem_bswap_64(void *src, int byte_size)
@@ -530,8 +540,8 @@ static perf_event__swap_op perf_event__swap_ops[] = {
 	[PERF_RECORD_SAMPLE]		  = perf_event__all64_swap,
 	[PERF_RECORD_HEADER_ATTR]	  = perf_event__hdr_attr_swap,
 	[PERF_RECORD_HEADER_EVENT_TYPE]	  = perf_event__event_type_swap,
-	[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
 	[PERF_RECORD_HEADER_BUILD_ID]	  = NULL,
+	[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
 	[PERF_RECORD_HEADER_MAX]	  = NULL,
 };
 
@@ -939,6 +949,8 @@ static int perf_session__process_user_event(struct perf_session *session, union
 		return tool->build_id(tool, event, session);
 	case PERF_RECORD_FINISHED_ROUND:
 		return tool->finished_round(tool, event, session);
+	case PERF_RECORD_HEADER_HOSTNAME ... PERF_RECORD_HEADER_NUMA_TOPOLOGY:
+		return tool->feature(tool, event, session);
 	default:
 		return -EINVAL;
 	}
@@ -1514,6 +1526,7 @@ void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
 
 	fprintf(fp, "# ========\n");
 	fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
-	perf_header__fprintf_info(session, fp, full);
 	fprintf(fp, "# ========\n#\n");
+	if (!session->fd_pipe)
+		perf_header__fprintf_info(session, fp, full);
 }
tools/perf/util/tool.h +3 -1
index b0e1aad..af16af1f 100644
@@ -42,9 +42,11 @@ struct perf_tool {
 	event_synth_op	tracing_data;
 	event_simple_op	event_type;
 	event_op2	finished_round,
-			build_id;
+			build_id,
+			feature;
 	bool		ordered_samples;
 	bool		ordering_requires_timestamps;
+	bool		show_all_features;
 };
 
 #endif /* __PERF_TOOL_H */
Signature · 1 line
1.7.4.1
#7 lore raw

PATCH 5/5 perf: make perf buildid-list work better with pipe mode

In reply to Stephane Eranian

In order for perf buildid-list to work with pipe-mode files, it needs to process buildids and event attr structs.

$ perf record -o - noploop 2 | ./perf inject -b | perf buildid-list -i - -H
noploop for 2 seconds
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.084 MB - (~3678 samples) ]
0000000000000000000000000000000000000000 [kernel.kallsyms]
3a0d0629efe74a8da3eeba372cdbd74ad9b8f5d5 /usr/local/bin/noploop

The reason [kernel.kallsyms] shows a 0 build-id comes from the way buildids are injected in the stream. The buildid for the kernel is provided by a BUILD_ID record. The [kernel.kallsyms] is provided by a MMAP record. There is no clean and obvious way to link the two, unfortunately. In regular mode, the kernel buildid is generated from reading the ELF image or kallsyms and perf knows to associate [kernel.kallsyms] to it. Later on, when perf processes the [kernel.kallsyms] MMAP record, it will already have a dso for it.

So for now, make sure perf buildid-list shows the buildids for everything but the kernel image.

---
 tools/perf/builtin-buildid-list.c |    6 +++++-
 tools/perf/util/build-id.c        |    2 ++
 2 files changed, 7 insertions(+), 1 deletions(-)
tools/perf/builtin-buildid-list.c +5 -1
index 5248046..6b2bcfb 100644
@@ -84,7 +84,11 @@ static int perf_session__list_build_ids(void)
 	if (filename__fprintf_build_id(session->filename, stdout))
 		goto out;
 
-	if (with_hits)
+	/*
+	 * in pipe-mode, the only way to get the buildids is to parse
+	 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
+	 */
+	if (with_hits || session->fd_pipe)
 		perf_session__process_events(session, &build_id__mark_dso_hit_ops);
 
 	perf_session__fprintf_dsos_buildid(session, stdout, with_hits);
tools/perf/util/build-id.c +2 -0
index dff9c7a..fd9a594 100644
@@ -65,6 +65,8 @@ struct perf_tool build_id__mark_dso_hit_ops = {
 	.mmap	= perf_event__process_mmap,
 	.fork	= perf_event__process_task,
 	.exit	= perf_event__exit_del_thread,
+	.attr		 = perf_event__process_attr,
+	.build_id	 = perf_event__process_build_id,
 };
 
 char *dso__build_id_filename(struct dso *self, char *bf, size_t size)
Signature · 1 line
1.7.4.1
#8 lore raw
In reply to Stephane Eranian

Arnaldo,

Any comment on this series?

Quoted 68 lines · Stephane Eranian

This patch series adds meta-data support to perf record/report in pipe mode:
       perf record -o - foo | perf inject -b | perf report -i -

We had meta-data (information about host configuration, perf tool version,...) in regular (file) mode but it was lacking in pipe mode. This patch series fixes this.

In pipe mode, there is no meta-data header structures at the beginning of the streamed data given we cannot seek in a pipe. Instead, we need to create pseudo record types for each of the possible features, e.g., hostname, cpuid, event_desc, and so on. Those pseudo records are guaranteed to be before any actual sample records, therefore perf report/annotate are guaranteed to get the information they need before processing the first sample.

The series also fixes perf inject to actually inject build-ids and buildid-list to work better with pipe mode, i.e., print the build-ids.

With this series:

 $ perf record -o - noploop 2 | perf inject -b | perf report -i -
     # ========
     # captured on: Fri Jan 20 18:13:55 2012
     # ========
     #
     # hostname : quad
     # os release : 3.2.0-rc7-tip
     # perf version : 3.2.0
     # arch : x86_64
     # nrcpus online : 4
     # nrcpus avail : 4
     # cpudesc : Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
     # cpuid : GenuineIntel,6,15,11
     # total memory : 8092884 kB
     ...
     # HEADER_CPU_TOPOLOGY info available, use -I to display
     noploop for 2 seconds
     [ perf record: Woken up 1 times to write data ]
     [ perf record: Captured and wrote 0.084 MB - (~3677 samples) ]
       99.80%  noploop  noploop            [.] noploop
        0.19%  noploop  [kernel.kallsyms]  [k] radix_tree_gang_lookup

Stephane Eranian (5):
 perf inject: fix broken perf inject -b
 perf tools: fix piped mode read code
 perf tools: rename HEADER_TRACE_INFO to HEADER_TRACING_DATA perf record: add meta-data support for pipe-mode
 perf: make perf buildid-list work better with pipe mode

 tools/perf/builtin-annotate.c     |    5 +
 tools/perf/builtin-buildid-list.c |    6 +-

 tools/perf/builtin-inject.c       |    6 +
 tools/perf/builtin-record.c       |   11 +-
 tools/perf/builtin-report.c       |    7 +-
 tools/perf/util/build-id.c        |    2 +
 tools/perf/util/event.c           |   12 +
 tools/perf/util/event.h           |   18 ++
 tools/perf/util/header.c          |  538 ++++++++++++++++++++++++++-----------
 tools/perf/util/header.h          |   11 +-
 tools/perf/util/session.c         |   52 +++-
 tools/perf/util/tool.h            |    4 +-
 12 files changed, 494 insertions(+), 178 deletions(-)

--
1.7.4.1