Skip to contentSkip to conversation

perf/core: Put size of a sample at the end of it by PERF_SAMPLE_TAILSIZE

PATCH New Wang Nan 1 participant 1 message Dec 8, 2015 perf events

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

PATCH v3 1/1 perf/core: Put size of a sample at the end of it by PERF_SAMPLE_TAILSIZE

This patch introduces a PERF_SAMPLE_TAILSIZE flag which allows a size field attached at the end of a sample. The idea comes from [1] that, with tie size at tail of an event, it is possible for user program who read from the ring buffer parse events backward.

For example:

head

    |
    V
 +--+---+-------+----------+------+---+
 |E6|...|   B  8|   C    11|  D  7|E..|
 +--+---+-------+----------+------+---+

In this case, from the 'head' pointer provided by kernel, user program can first see '6' by (*(head - sizeof(u64))), then it can get the start pointer of record 'E', then it can read size and find start position of record D, C, B in similar way.

The implementation is easy: adding a PERF_SAMPLE_TAILSIZE flag, makes perf_output_sample() output size at the end of a sample.

Following things are done for ensure the ring buffer is safe for backward parsing:

- Don't allow two events with different PERF_SAMPLE_TAILSIZE setting set their output to each other;

- For non-sample events, also output tailsize if required.

This patch has a limitation for perf:

Before reading such ring buffer, perf must ensure all events which may output to it is already stopped, so the 'head' pointer it get is the end of the last record.

[1] http://lkml.kernel.org/g/[email protected]

Compare with v2:
- Consider tracking events.
- tailsize is output once as a u64, instead of skipping 6 bytes and outputing 2 bytes as u16. Which is good for endianess swapping.
- Don't allow two events with different tailsize setting set output to each other.

Cc Adrian Hunter <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc David Ahern <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Yunlong Song <[email protected]>
---
 include/linux/perf_event.h      | 17 ++++++---
 include/uapi/linux/perf_event.h |  3 +-
 kernel/events/core.c            | 82 +++++++++++++++++++++++++++++------------
 kernel/events/ring_buffer.c     |  7 ++--
 4 files changed, 75 insertions(+), 34 deletions(-)
include/linux/perf_event.h +11 -6
index f9828a4..c5df1e82 100644
@@ -835,13 +835,13 @@ extern void perf_event_output(struct perf_event *event,
 				struct pt_regs *regs);
 
 extern void
-perf_event_header__init_id(struct perf_event_header *header,
-			   struct perf_sample_data *data,
-			   struct perf_event *event);
+perf_event_header__init_extra(struct perf_event_header *header,
+			      struct perf_sample_data *data,
+			      struct perf_event *event);
 extern void
-perf_event__output_id_sample(struct perf_event *event,
-			     struct perf_output_handle *handle,
-			     struct perf_sample_data *sample);
+perf_event__output_extra(struct perf_event *event, u64 evt_size,
+			 struct perf_output_handle *handle,
+			 struct perf_sample_data *sample);
 
 extern void
 perf_log_lost_samples(struct perf_event *event, u64 lost);
@@ -1032,6 +1032,11 @@ static inline bool has_aux(struct perf_event *event)
 	return event->pmu->setup_aux;
 }
 
+static inline bool has_tailsize(struct perf_event *event)
+{
+	return event->attr.sample_type & PERF_SAMPLE_TAILSIZE;
+}
+
 extern int perf_output_begin(struct perf_output_handle *handle,
 			     struct perf_event *event, unsigned int size);
 extern void perf_output_end(struct perf_output_handle *handle);
include/uapi/linux/perf_event.h +2 -1
index 1afe962..4e8dde8 100644
@@ -139,8 +139,9 @@ enum perf_event_sample_format {
 	PERF_SAMPLE_IDENTIFIER			= 1U << 16,
 	PERF_SAMPLE_TRANSACTION			= 1U << 17,
 	PERF_SAMPLE_REGS_INTR			= 1U << 18,
+	PERF_SAMPLE_TAILSIZE			= 1U << 19,
 
-	PERF_SAMPLE_MAX = 1U << 19,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 20,		/* non-ABI */
 };
 
 /*
kernel/events/core.c +58 -24

226 lines not on this page · raw message

kernel/events/ring_buffer.c +4 -3
index adfdc05..5f8bd89 100644
@@ -186,10 +186,11 @@ int perf_output_begin(struct perf_output_handle *handle,
 		lost_event.id          = event->id;
 		lost_event.lost        = local_xchg(&rb->lost, 0);
 
-		perf_event_header__init_id(&lost_event.header,
-					   &sample_data, event);
+		perf_event_header__init_extra(&lost_event.header,
+					      &sample_data, event);
 		perf_output_put(handle, lost_event);
-		perf_event__output_id_sample(event, handle, &sample_data);
+		perf_event__output_extra(event, lost_event.header.type,
+					 handle, &sample_data);
 	}
 
 	return 0;
Signature · 1 line
1.8.3.4