Skip to contentSkip to conversation

perfcounters: Support for ftrace event records sampling

PATCH In review Frederic Weisbecker 4 participants 13 messages Aug 6, 2009 – Aug 7, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perfcounters: Support for ftrace event records sampling

This patch brings the kernel side support for ftrace event record sampling.

A new counter attribute is added: PERF_SAMPLE_TP_RECORD which requests ftrace events record sampling.

For now, the events record are always copied from the event profiling callback and passed down to perfcounter. Perfcounter will just zap the record copy if the attribute is not set.

Result, after forcing PERF_SAMPLE_TP_RECORD attribute from perf record:

perf record -f -F 1 -a -e workqueue:workqueue_execution perf report -D

0x21e18 [0x48]: event: 9
.
. ... raw event: size 72 bytes

.  0000:  09 00 00 00 01 00 48 00 d0 c7 00 81 ff ff ff ff  ......H........
.  0010:  0a 00 00 00 0a 00 00 00 21 00 00 00 00 00 00 00  ........!......
.  0020:  2b 00 01 02 0a 00 00 00 0a 00 00 00 65 76 65 6e  +...........eve
.  0030:  74 73 2f 31 00 00 00 00 00 00 00 00 0a 00 00 00  ts/1...........
.  0040:  e0 b1 31 81 ff ff ff ff                          .......

.
0x21e18 [0x48]: PERF_EVENT_SAMPLE (IP, 1): 10: 0xffffffff8100c7d0 period: 33

The ftrace record starts at offset 0020.
Translation:

struct trace_entry {
	type		= 0x2b = 43;
	flags		= 1;
	preempt_count	= 2;
	pid		= 0xa = 10;
	tgid		= 0xa = 10;
}

thread_comm = "events/1"
thread_pid = 0xa = 10;
func = 0xffffffff8131b1e0 = flush_to_ldisc()

What will come next?

- The unconditional copy from the profiling callback brings some costs however, and may require to be fixed in the future. For that we need to have an instant access to the perf counter attribute. This is a matter of a flag to add in the struct ftrace_event.

- Be care of the events recursivity! Don't ever try to record a lock event for example, it seems some locking is used in the profiling fast path and lead to a tracing recursivity. That will be fixed using raw spinlock or recursivity protection.

- Userspace support

- [...]

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Gabriel Munteanu <[email protected]>
Cc Li Zefan <[email protected]>
Cc Lai Jiangshan <[email protected]>
---
 include/linux/ftrace_event.h |    4 +-
 include/linux/perf_counter.h |    9 +++-
 include/trace/ftrace.h       |  130 +++++++++++++++++++++++++++++++-----------
 kernel/perf_counter.c        |   18 ++++++-
 kernel/trace/trace.h         |    4 -
 tools/perf/builtin-record.c  |    1 +
 6 files changed, 125 insertions(+), 41 deletions(-)
include/linux/ftrace_event.h +3 -1
index c04e6d9..ac8c6f8 100644
@@ -89,7 +89,9 @@ enum print_line_t {
 	TRACE_TYPE_NO_CONSUME	= 3	/* Handled but ask to not consume */
 };
 
-
+void tracing_generic_entry_update(struct trace_entry *entry,
+				  unsigned long flags,
+				  int pc);
 struct ring_buffer_event *
 trace_current_buffer_lock_reserve(int type, unsigned long len,
 				  unsigned long flags, int pc);
include/linux/perf_counter.h +8 -1
index e604e6e..a67dd5c 100644
@@ -121,8 +121,9 @@ enum perf_counter_sample_format {
 	PERF_SAMPLE_CPU				= 1U << 7,
 	PERF_SAMPLE_PERIOD			= 1U << 8,
 	PERF_SAMPLE_STREAM_ID			= 1U << 9,
+	PERF_SAMPLE_TP_RECORD			= 1U << 10,
 
-	PERF_SAMPLE_MAX = 1U << 10,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 11,		/* non-ABI */
 };
 
 /*
@@ -413,6 +414,11 @@ struct perf_callchain_entry {
 	__u64				ip[PERF_MAX_STACK_DEPTH];
 };
 
+struct perf_tracepoint_record {
+	int				size;
+	char				*record;
+};
+
 struct task_struct;
 
 /**
@@ -681,6 +687,7 @@ struct perf_sample_data {
 	struct pt_regs			*regs;
 	u64				addr;
 	u64				period;
+	void				*private;
 };
 
 extern int perf_counter_overflow(struct perf_counter *counter, int nmi,
include/trace/ftrace.h +96 -34

156 lines not on this page · raw message

kernel/perf_counter.c +17 -1
index 06d210c..93f4312 100644
@@ -2646,6 +2646,7 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 		u64 counter;
 	} group_entry;
 	struct perf_callchain_entry *callchain = NULL;
+	struct perf_tracepoint_record *tp;
 	int callchain_size = 0;
 	u64 time;
 	struct {
@@ -2714,6 +2715,11 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 			header.size += sizeof(u64);
 	}
 
+	if (sample_type & PERF_SAMPLE_TP_RECORD) {
+		tp = data->private;
+		header.size += tp->size;
+	}
+
 	ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
 	if (ret)
 		return;
@@ -2777,6 +2783,9 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 		}
 	}
 
+	if (sample_type & PERF_SAMPLE_TP_RECORD)
+		perf_output_copy(&handle, tp->record, tp->size);
+
 	perf_output_end(&handle);
 }
 
@@ -3727,11 +3736,18 @@ static const struct pmu perf_ops_task_clock = {
 };
 
 #ifdef CONFIG_EVENT_PROFILE
-void perf_tpcounter_event(int event_id, u64 addr, u64 count)
+void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
+			  int entry_size)
 {
+	struct perf_tracepoint_record tp = {
+		.size = entry_size,
+		.record = record,
+	};
+
 	struct perf_sample_data data = {
 		.regs = get_irq_regs(),
 		.addr = addr,
+		.private = &tp,
 	};
 
 	if (!data.regs)
kernel/trace/trace.h +0 -4
index 87b004f..44308f3 100644
@@ -454,10 +454,6 @@ struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
 					  int *ent_cpu, u64 *ent_ts);
 
-void tracing_generic_entry_update(struct trace_entry *entry,
-				  unsigned long flags,
-				  int pc);
-
 void default_wait_pipe(struct trace_iterator *iter);
 void poll_wait_pipe(struct trace_iterator *iter);
 
tools/perf/builtin-record.c +1 -0
index 6da0992..90c9808 100644
@@ -412,6 +412,7 @@ static void create_counter(int counter, int cpu, pid_t pid)
 	if (call_graph)
 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
 
+
 	attr->mmap		= track;
 	attr->comm		= track;
 	attr->inherit		= (cpu < 0) && inherit;
Signature · 1 line
1.6.2.3
#1 lore raw

[tip:perfcounters/tracing] perf_counter: Support for ftrace event records sampling

Commit-ID:  24cb4fabe71f1d896b504cec82277ad7426a3d6d
Gitweb:     http://git.kernel.org/tip/24cb4fabe71f1d896b504cec82277ad7426a3d6d
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Fri, 7 Aug 2009 01:25:54 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 7 Aug 2009 09:30:33 +0200

perf_counter: Support for ftrace event records sampling

This patch implements the kernel side support for ftrace event record sampling.

A new counter sampling attribute is added:

PERF_SAMPLE_TP_RECORD

which requests ftrace events record sampling. In this case if a PERF_TYPE_TRACEPOINT counter is active and a tracepoint fires, we emit the tracepoint binary record to the perfcounter event buffer, as a sample.

Result, after setting PERF_SAMPLE_TP_RECORD attribute from perf record:

perf record -f -F 1 -a -e workqueue:workqueue_execution perf report -D

0x21e18 [0x48]: event: 9
.
. ... raw event: size 72 bytes

 .  0000:  09 00 00 00 01 00 48 00 d0 c7 00 81 ff ff ff ff  ......H........
 .  0010:  0a 00 00 00 0a 00 00 00 21 00 00 00 00 00 00 00  ........!......
 .  0020:  2b 00 01 02 0a 00 00 00 0a 00 00 00 65 76 65 6e  +...........eve
 .  0030:  74 73 2f 31 00 00 00 00 00 00 00 00 0a 00 00 00  ts/1...........
 .  0040:  e0 b1 31 81 ff ff ff ff                          .......

.
0x21e18 [0x48]: PERF_EVENT_SAMPLE (IP, 1): 10: 0xffffffff8100c7d0 period: 33

The raw ftrace binary record starts at offset 0020.

Translation:

 struct trace_entry {
	type		= 0x2b = 43;
	flags		= 1;
	preempt_count	= 2;
	pid		= 0xa = 10;
	tgid		= 0xa = 10;

}

thread_comm = "events/1"
thread_pid = 0xa = 10;
func = 0xffffffff8131b1e0 = flush_to_ldisc()

What will come next?

- Userspace support ('perf trace'), 'flight data recorder' mode for perf trace, etc.

- The unconditional copy from the profiling callback brings some costs however if someone wants no such sampling to occur, and needs to be fixed in the future. For that we need to have an instant access to the perf counter attribute. This is a matter of a flag to add in the struct ftrace_event.

- Take care of the events recursivity! Don't ever try to record a lock event for example, it seems some locking is used in the profiling fast path and lead to a tracing recursivity. That will be fixed using raw spinlock or recursivity protection.

- [...]

- Profit! :-)

Cc Li Zefan <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Gabriel Munteanu <[email protected]>
Cc Li Zefan <[email protected]>
Cc Lai Jiangshan <[email protected]>

LKML-Reference: <[email protected]>

---
 include/linux/ftrace_event.h |    4 +-
 include/linux/perf_counter.h |    9 +++-
 include/trace/ftrace.h       |  130 +++++++++++++++++++++++++++++++-----------
 kernel/perf_counter.c        |   18 ++++++-
 kernel/trace/trace.h         |    4 -
 tools/perf/builtin-record.c  |    1 +
 6 files changed, 125 insertions(+), 41 deletions(-)
include/linux/ftrace_event.h +3 -1
index 5c093ff..8872ec9 100644
@@ -89,7 +89,9 @@ enum print_line_t {
 	TRACE_TYPE_NO_CONSUME	= 3	/* Handled but ask to not consume */
 };
 
-
+void tracing_generic_entry_update(struct trace_entry *entry,
+				  unsigned long flags,
+				  int pc);
 struct ring_buffer_event *
 trace_current_buffer_lock_reserve(int type, unsigned long len,
 				  unsigned long flags, int pc);
include/linux/perf_counter.h +8 -1
index bd15d7a..e175982 100644
@@ -121,8 +121,9 @@ enum perf_counter_sample_format {
 	PERF_SAMPLE_CPU				= 1U << 7,
 	PERF_SAMPLE_PERIOD			= 1U << 8,
 	PERF_SAMPLE_STREAM_ID			= 1U << 9,
+	PERF_SAMPLE_TP_RECORD			= 1U << 10,
 
-	PERF_SAMPLE_MAX = 1U << 10,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 11,		/* non-ABI */
 };
 
 /*
@@ -402,6 +403,11 @@ struct perf_callchain_entry {
 	__u64				ip[PERF_MAX_STACK_DEPTH];
 };
 
+struct perf_tracepoint_record {
+	int				size;
+	char				*record;
+};
+
 struct task_struct;
 
 /**
@@ -670,6 +676,7 @@ struct perf_sample_data {
 	struct pt_regs			*regs;
 	u64				addr;
 	u64				period;
+	void				*private;
 };
 
 extern int perf_counter_overflow(struct perf_counter *counter, int nmi,
include/trace/ftrace.h +96 -34

156 lines not on this page · raw message

kernel/perf_counter.c +17 -1
index 7d281b4..40155bc 100644
@@ -2631,6 +2631,7 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 		u64 counter;
 	} group_entry;
 	struct perf_callchain_entry *callchain = NULL;
+	struct perf_tracepoint_record *tp;
 	int callchain_size = 0;
 	u64 time;
 	struct {
@@ -2699,6 +2700,11 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 			header.size += sizeof(u64);
 	}
 
+	if (sample_type & PERF_SAMPLE_TP_RECORD) {
+		tp = data->private;
+		header.size += tp->size;
+	}
+
 	ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
 	if (ret)
 		return;
@@ -2762,6 +2768,9 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 		}
 	}
 
+	if (sample_type & PERF_SAMPLE_TP_RECORD)
+		perf_output_copy(&handle, tp->record, tp->size);
+
 	perf_output_end(&handle);
 }
 
@@ -3673,11 +3682,18 @@ static const struct pmu perf_ops_task_clock = {
 };
 
 #ifdef CONFIG_EVENT_PROFILE
-void perf_tpcounter_event(int event_id, u64 addr, u64 count)
+void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
+			  int entry_size)
 {
+	struct perf_tracepoint_record tp = {
+		.size = entry_size,
+		.record = record,
+	};
+
 	struct perf_sample_data data = {
 		.regs = get_irq_regs(),
 		.addr = addr,
+		.private = &tp,
 	};
 
 	if (!data.regs)
kernel/trace/trace.h +0 -4
index 3548ae5..8b9f4f6 100644
@@ -438,10 +438,6 @@ struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
 					  int *ent_cpu, u64 *ent_ts);
 
-void tracing_generic_entry_update(struct trace_entry *entry,
-				  unsigned long flags,
-				  int pc);
-
 void default_wait_pipe(struct trace_iterator *iter);
 void poll_wait_pipe(struct trace_iterator *iter);
 
tools/perf/builtin-record.c +1 -0
index 6da0992..90c9808 100644
@@ -412,6 +412,7 @@ static void create_counter(int counter, int cpu, pid_t pid)
 	if (call_graph)
 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
 
+
 	attr->mmap		= track;
 	attr->comm		= track;
 	attr->inherit		= (cpu < 0) && inherit;
#2 lore raw

Re: [tip:perfcounters/tracing] perf_counter: Support for ftrace event records sampling

Quoted 8 lines · tip-bot for Frederic Weisbecker
Commit-ID:  24cb4fabe71f1d896b504cec82277ad7426a3d6d
Gitweb:     http://git.kernel.org/tip/24cb4fabe71f1d896b504cec82277ad7426a3d6d
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Fri, 7 Aug 2009 01:25:54 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 7 Aug 2009 09:30:33 +0200

perf_counter: Support for ftrace event records sampling

this commit triggers a new build warning:

kernel/perf_counter.c: In function ‘perf_counter_output’:
kernel/perf_counter.c:2650: warning: ‘tp’ may be used uninitialized in this function

Ingo

#3 lore raw

Re: [tip:perfcounters/tracing] perf_counter: Support for ftrace event records sampling

Quoted 10 lines · Ingo Molnar
Quoted 7 lines · tip-bot for Frederic Weisbecker
Commit-ID:  24cb4fabe71f1d896b504cec82277ad7426a3d6d
Gitweb:     http://git.kernel.org/tip/24cb4fabe71f1d896b504cec82277ad7426a3d6d
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Fri, 7 Aug 2009 01:25:54 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 7 Aug 2009 09:30:33 +0200
Quoted 8 lines
Quoted 1 line

perf_counter: Support for ftrace event records sampling

this commit triggers a new build warning:

kernel/perf_counter.c: In function ‘perf_counter_output’:
kernel/perf_counter.c:2650: warning: ‘tp’ may be used uninitialized in this function

Ingo

Ok, I'll fix it, thanks.

#4 lore raw
In reply to Frederic Weisbecker

include/linux/perf_counter.h enum perf_counter_sample_format { · line 124

Quoted 8 lines · Frederic Weisbecker

This patch brings the kernel side support for ftrace event record sampling.

A new counter attribute is added: PERF_SAMPLE_TP_RECORD which requests ftrace events record sampling.

include/linux/perf_counter.h +1 -0
+	PERF_SAMPLE_TP_RECORD			= 1U << 10,

I'd really want this thing called PERF_SAMPLE_RAW

include/linux/perf_counter.h struct perf_callchain_entry { · line 420

Quoted 13 lines
include/linux/perf_counter.h +5 -1
-	PERF_SAMPLE_MAX = 1U << 10,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 11,		/* non-ABI */
 };
 
 /*
@@ -413,6 +414,11 @@ struct perf_callchain_entry {
 	__u64				ip[PERF_MAX_STACK_DEPTH];
 };
 
+struct perf_tracepoint_record {
+	int				size;
+	char				*record;
+};

Which would make this:

struct perf_raw_record {
	u32	size;
	void 	*data;
};

include/linux/perf_counter.h struct perf_sample_data { · line 691

Quoted 9 lines
include/linux/perf_counter.h +1 -0
 struct task_struct;
 
 /**
@@ -681,6 +687,7 @@ struct perf_sample_data {
 	struct pt_regs			*regs;
 	u64				addr;
 	u64				period;
+	void				*private;
 };

might as well make that struct perf_raw_record *raw;

Quoted 84 lines
include/trace/ftrace.h +80 -0
@@ -649,5 +617,99 @@ __attribute__((section("_ftrace_events"))) event_##call = {		\
 
 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
 
+/*
+ * Define the insertion callback to profile events
+ *
+ * The job is very similar to ftrace_raw_event_<call> except that we don't
+ * insert in the ring buffer but in a perf counter.
+ *
+ * static void ftrace_profile_<call>(proto)
+ * {
+ *	struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
+ *	struct ftrace_event_call *event_call = &event_<call>;
+ *	extern void perf_tpcounter_event(int, u64, u64, void *, int);
+ *	struct ftrace_raw_##call *entry;
+ *	u64 __addr = 0, __count = 1;
+ *	unsigned long irq_flags;
+ *	int __entry_size;
+ *	int __data_size;
+ *	int pc;
+ *
+ *	local_save_flags(irq_flags);
+ *	pc = preempt_count();
+ *
+ *	__data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
+ *	__entry_size = __data_size + sizeof(*entry);
+ *
+ *	do {
+ *		char raw_data[__entry_size]; <- allocate our sample in the stack
+ *		struct trace_entry *ent;
+ *
+ *		entry = (struct ftrace_raw_<call> *)raw_data;
+ *		ent = &entry->ent;
+ *		tracing_generic_entry_update(ent, irq_flags, pc);
+ *		ent->type = event_call->id;
+ *
+ *		<tstruct> <- do some jobs with dynamic arrays
+ *
+ *		<assign>  <- affect our values
+ *
+ *		perf_tpcounter_event(event_call->id, __addr, __count, entry,
+ *			     __entry_size);  <- submit them to perf counter
+ *	} while (0);
+ *
+ * }
+ */
+
+#ifdef CONFIG_EVENT_PROFILE
+
+#undef __perf_addr
+#define __perf_addr(a) __addr = (a)
+
+#undef __perf_count
+#define __perf_count(c) __count = (c)
+
+#undef TRACE_EVENT
+#define TRACE_EVENT(call, proto, args, tstruct, assign, print)		\
+static void ftrace_profile_##call(proto)				\
+{									\
+	struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
+	struct ftrace_event_call *event_call = &event_##call;		\
+	extern void perf_tpcounter_event(int, u64, u64, void *, int);	\
+	struct ftrace_raw_##call *entry;				\
+	u64 __addr = 0, __count = 1;					\
+	unsigned long irq_flags;					\
+	int __entry_size;						\
+	int __data_size;						\
+	int pc;								\
+									\
+	local_save_flags(irq_flags);					\
+	pc = preempt_count();						\
+									\
+	__data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
+	__entry_size = __data_size + sizeof(*entry);			\
+									\
+	do {								\
+		char raw_data[__entry_size];				\
+		struct trace_entry *ent;				\
+									\
+		entry = (struct ftrace_raw_##call *)raw_data;		\
+		ent = &entry->ent;					\
+		tracing_generic_entry_update(ent, irq_flags, pc);	\
+		ent->type = event_call->id;				\

include/trace/ftrace.h __attribute__((section("_ftrace_events"))) event_##call = { \ · line 709

Quoted 10 lines
include/trace/ftrace.h +10 -0
+									\
+		tstruct							\
+									\
+		{ assign; }						\
+									\
+		perf_tpcounter_event(event_call->id, __addr, __count, entry,\
+			     __entry_size);				\
+	} while (0);							\
+									\
+}

ok, so the one concern I have here is that the data needs to fit on the stack. What if someone puts a large string in the data?

Quoted 31 lines
include/trace/ftrace.h +4 -0
+
+#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
+#endif /* CONFIG_EVENT_PROFILE */
+
 #undef _TRACE_PROFILE_INIT
 
kernel/perf_counter.c +6 -0
index 06d210c..93f4312 100644
@@ -2646,6 +2646,7 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 		u64 counter;
 	} group_entry;
 	struct perf_callchain_entry *callchain = NULL;
+	struct perf_tracepoint_record *tp;
 	int callchain_size = 0;
 	u64 time;
 	struct {
@@ -2714,6 +2715,11 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 			header.size += sizeof(u64);
 	}
 
+	if (sample_type & PERF_SAMPLE_TP_RECORD) {
+		tp = data->private;
+		header.size += tp->size;
+	}
+
 	ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
 	if (ret)
 		return;

kernel/perf_counter.c static void perf_counter_output(struct perf_counter *counter, int nmi, · line 2790

Quoted 8 lines
kernel/perf_counter.c +3 -0
@@ -2777,6 +2783,9 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 		}
 	}
 
+	if (sample_type & PERF_SAMPLE_TP_RECORD)
+		perf_output_copy(&handle, tp->record, tp->size);
+
 	perf_output_end(&handle);
 }

You seem to fail to round up to a multiple of u64 somewhere along the line, that'll mess things up as events are supposed to be u64 aligned.

tools/perf/builtin-record.c static void create_counter(int counter, int cpu, pid_t pid) · line 418

Quoted 12 lines
tools/perf/builtin-record.c +1 -0
index 6da0992..90c9808 100644
@@ -412,6 +412,7 @@ static void create_counter(int counter, int cpu, pid_t pid)
 	if (call_graph)
 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
 
+
 	attr->mmap		= track;
 	attr->comm		= track;
 	attr->inherit		= (cpu < 0) && inherit;

Do we really need that extra whitespace?

#5 lore raw
Quoted 11 lines · Peter Zijlstra

include/linux/perf_counter.h enum perf_counter_sample_format { · line 124

Quoted 8 lines · Frederic Weisbecker

This patch brings the kernel side support for ftrace event record sampling.

A new counter attribute is added: PERF_SAMPLE_TP_RECORD which requests ftrace events record sampling.

include/linux/perf_counter.h +1 -0
+	PERF_SAMPLE_TP_RECORD			= 1U << 10,

I'd really want this thing called PERF_SAMPLE_RAW

yeah, i'd agree. I suggested to Frederic to use TP_RECORD but in hindsight we are better off to handle PERF_SAMPLE_RAW as a 'non-ABI' embelishment of samples, of not firmly defined content.

In the tracepoint case, that extra raw content is defined by:

/debug/tracing/events/*/format

and subject to changes.

Ingo

maintainer
#6 lore raw
In reply to Peter Zijlstra
Quoted 31 lines · Peter Zijlstra
Quoted 31 lines
include/trace/ftrace.h +31 -0
+static void ftrace_profile_##call(proto)                             \
+{                                                                    \
+     struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
+     struct ftrace_event_call *event_call = &event_##call;           \
+     extern void perf_tpcounter_event(int, u64, u64, void *, int);   \
+     struct ftrace_raw_##call *entry;                                \
+     u64 __addr = 0, __count = 1;                                    \
+     unsigned long irq_flags;                                        \
+     int __entry_size;                                               \
+     int __data_size;                                                \
+     int pc;                                                         \
+                                                                     \
+     local_save_flags(irq_flags);                                    \
+     pc = preempt_count();                                           \
+                                                                     \
+     __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
+     __entry_size = __data_size + sizeof(*entry);                    \
+                                                                     \
+     do {                                                            \
+             char raw_data[__entry_size];                            \
+             struct trace_entry *ent;                                \
+                                                                     \
+             entry = (struct ftrace_raw_##call *)raw_data;           \
+             ent = &entry->ent;                                      \
+             tracing_generic_entry_update(ent, irq_flags, pc);       \
+             ent->type = event_call->id;                             \
+                                                                     \
+             tstruct                                                 \
+                                                                     \
+             { assign; }                                             \
+                                                                     \
Quoted 8 lines

include/trace/ftrace.h __attribute__((section("_ftrace_events"))) event_##call = { \ · line 709

Quoted 5 lines
include/trace/ftrace.h +5 -0
+             perf_tpcounter_event(event_call->id, __addr, __count, entry,\
+                          __entry_size);                             \
+     } while (0);                                                    \
+                                                                     \
+}

ok, so the one concern I have here is that the data needs to fit on the stack. What if someone puts a large string in the data?

Also, how NMI safe is all that?

#7 lore raw
Quoted 41 lines · Peter Zijlstra
Quoted 31 lines · Peter Zijlstra
Quoted 31 lines
include/trace/ftrace.h +31 -0
+static void ftrace_profile_##call(proto)                             \
+{                                                                    \
+     struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
+     struct ftrace_event_call *event_call = &event_##call;           \
+     extern void perf_tpcounter_event(int, u64, u64, void *, int);   \
+     struct ftrace_raw_##call *entry;                                \
+     u64 __addr = 0, __count = 1;                                    \
+     unsigned long irq_flags;                                        \
+     int __entry_size;                                               \
+     int __data_size;                                                \
+     int pc;                                                         \
+                                                                     \
+     local_save_flags(irq_flags);                                    \
+     pc = preempt_count();                                           \
+                                                                     \
+     __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
+     __entry_size = __data_size + sizeof(*entry);                    \
+                                                                     \
+     do {                                                            \
+             char raw_data[__entry_size];                            \
+             struct trace_entry *ent;                                \
+                                                                     \
+             entry = (struct ftrace_raw_##call *)raw_data;           \
+             ent = &entry->ent;                                      \
+             tracing_generic_entry_update(ent, irq_flags, pc);       \
+             ent->type = event_call->id;                             \
+                                                                     \
+             tstruct                                                 \
+                                                                     \
+             { assign; }                                             \
+                                                                     \
Quoted 8 lines

include/trace/ftrace.h __attribute__((section("_ftrace_events"))) event_##call = { \ · line 709

Quoted 5 lines
include/trace/ftrace.h +5 -0
+             perf_tpcounter_event(event_call->id, __addr, __count, entry,\
+                          __entry_size);                             \
+     } while (0);                                                    \
+                                                                     \
+}

ok, so the one concern I have here is that the data needs to fit on the stack. What if someone puts a large string in the data?

Quoted 1 line

Also, how NMI safe is all that?

Everything is allocated in the stack. It's NMI safe AFAICS, am I missing something?

maintainer
#8 lore raw
Quoted 42 lines · Frederic Weisbecker
Quoted 41 lines · Peter Zijlstra
Quoted 39 lines · Peter Zijlstra
Quoted 26 lines
include/trace/ftrace.h +26 -0
+static void ftrace_profile_##call(proto)                             \
+{                                                                    \
+     struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
+     struct ftrace_event_call *event_call = &event_##call;           \
+     extern void perf_tpcounter_event(int, u64, u64, void *, int);   \
+     struct ftrace_raw_##call *entry;                                \
+     u64 __addr = 0, __count = 1;                                    \
+     unsigned long irq_flags;                                        \
+     int __entry_size;                                               \
+     int __data_size;                                                \
+     int pc;                                                         \
+                                                                     \
+     local_save_flags(irq_flags);                                    \
+     pc = preempt_count();                                           \
+                                                                     \
+     __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
+     __entry_size = __data_size + sizeof(*entry);                    \
+                                                                     \
+     do {                                                            \
+             char raw_data[__entry_size];                            \
+             struct trace_entry *ent;                                \
+                                                                     \
+             entry = (struct ftrace_raw_##call *)raw_data;           \
+             ent = &entry->ent;                                      \
+             tracing_generic_entry_update(ent, irq_flags, pc);       \
+             ent->type = event_call->id;                             \

include/trace/ftrace.h __attribute__((section("_ftrace_events"))) event_##call = { \ · line 709

Quoted 10 lines
include/trace/ftrace.h +10 -0
+                                                                     \
+             tstruct                                                 \
+                                                                     \
+             { assign; }                                             \
+                                                                     \
+             perf_tpcounter_event(event_call->id, __addr, __count, entry,\
+                          __entry_size);                             \
+     } while (0);                                                    \
+                                                                     \
+}

ok, so the one concern I have here is that the data needs to fit on the stack. What if someone puts a large string in the data?

Quoted 5 lines
Quoted 1 line

Also, how NMI safe is all that?

Everything is allocated in the stack. It's NMI safe AFAICS, am I missing something?

I've no idea what all those trace calls do, I was hoping you would and say, sure, no problem ;-)

#9 lore raw
Quoted 43 lines · Peter Zijlstra
Quoted 42 lines · Frederic Weisbecker
Quoted 41 lines · Peter Zijlstra
Quoted 39 lines · Peter Zijlstra
Quoted 26 lines
include/trace/ftrace.h +26 -0
+static void ftrace_profile_##call(proto)                             \
+{                                                                    \
+     struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
+     struct ftrace_event_call *event_call = &event_##call;           \
+     extern void perf_tpcounter_event(int, u64, u64, void *, int);   \
+     struct ftrace_raw_##call *entry;                                \
+     u64 __addr = 0, __count = 1;                                    \
+     unsigned long irq_flags;                                        \
+     int __entry_size;                                               \
+     int __data_size;                                                \
+     int pc;                                                         \
+                                                                     \
+     local_save_flags(irq_flags);                                    \
+     pc = preempt_count();                                           \
+                                                                     \
+     __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
+     __entry_size = __data_size + sizeof(*entry);                    \
+                                                                     \
+     do {                                                            \
+             char raw_data[__entry_size];                            \
+             struct trace_entry *ent;                                \
+                                                                     \
+             entry = (struct ftrace_raw_##call *)raw_data;           \
+             ent = &entry->ent;                                      \
+             tracing_generic_entry_update(ent, irq_flags, pc);       \
+             ent->type = event_call->id;                             \

include/trace/ftrace.h __attribute__((section("_ftrace_events"))) event_##call = { \ · line 709

Quoted 10 lines
include/trace/ftrace.h +10 -0
+                                                                     \
+             tstruct                                                 \
+                                                                     \
+             { assign; }                                             \
+                                                                     \
+             perf_tpcounter_event(event_call->id, __addr, __count, entry,\
+                          __entry_size);                             \
+     } while (0);                                                    \
+                                                                     \
+}

ok, so the one concern I have here is that the data needs to fit on the stack. What if someone puts a large string in the data?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Also, how NMI safe is all that?

Everything is allocated in the stack. It's NMI safe AFAICS, am I missing something?

I've no idea what all those trace calls do, I was hoping you would and say, sure, no problem ;-)

Ah ok :)
To sum up, everything there is allocated in the stack, it's an issue as you reported but beside that it makes it all NMI safe.

ftrace_get_offset_#call() get the __dynamic_array/__string things and computes the total _local_ string length.
It also fills __data_offsets with the dynamic arrays offset, __data_offset is also in the stack.

tracing_entry_generic_update() fills *ent (stack allocated) with the standard informations (irq_flags, preempt_count, etc...).

And that's it :)

#10 lore raw
In reply to Peter Zijlstra
Quoted 35 lines · Peter Zijlstra

include/linux/perf_counter.h enum perf_counter_sample_format { · line 124

Quoted 8 lines · Frederic Weisbecker

This patch brings the kernel side support for ftrace event record sampling.

A new counter attribute is added: PERF_SAMPLE_TP_RECORD which requests ftrace events record sampling.

include/linux/perf_counter.h +1 -0
+	PERF_SAMPLE_TP_RECORD			= 1U << 10,

I'd really want this thing called PERF_SAMPLE_RAW

include/linux/perf_counter.h struct perf_callchain_entry { · line 420

Quoted 13 lines
include/linux/perf_counter.h +5 -1
-	PERF_SAMPLE_MAX = 1U << 10,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 11,		/* non-ABI */
 };
 
 /*
@@ -413,6 +414,11 @@ struct perf_callchain_entry {
 	__u64				ip[PERF_MAX_STACK_DEPTH];
 };
 
+struct perf_tracepoint_record {
+	int				size;
+	char				*record;
+};

Which would make this:

struct perf_raw_record {
	u32	size;
	void 	*data;
};
Quoted 2 lines
include/linux/perf_counter.h +0 -0
 struct task_struct;
 
Quoted 9 lines

include/linux/perf_counter.h struct perf_sample_data { · line 691

Quoted 7 lines
include/linux/perf_counter.h +1 -0
 /**
@@ -681,6 +687,7 @@ struct perf_sample_data {
 	struct pt_regs			*regs;
 	u64				addr;
 	u64				period;
+	void				*private;
 };

might as well make that struct perf_raw_record *raw;

Ok. Yeah that's far more generic (and typed).

Quoted 90 lines
Quoted 90 lines
include/trace/ftrace.h +85 -0
@@ -649,5 +617,99 @@ __attribute__((section("_ftrace_events"))) event_##call = {		\
 
 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
 
+/*
+ * Define the insertion callback to profile events
+ *
+ * The job is very similar to ftrace_raw_event_<call> except that we don't
+ * insert in the ring buffer but in a perf counter.
+ *
+ * static void ftrace_profile_<call>(proto)
+ * {
+ *	struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
+ *	struct ftrace_event_call *event_call = &event_<call>;
+ *	extern void perf_tpcounter_event(int, u64, u64, void *, int);
+ *	struct ftrace_raw_##call *entry;
+ *	u64 __addr = 0, __count = 1;
+ *	unsigned long irq_flags;
+ *	int __entry_size;
+ *	int __data_size;
+ *	int pc;
+ *
+ *	local_save_flags(irq_flags);
+ *	pc = preempt_count();
+ *
+ *	__data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
+ *	__entry_size = __data_size + sizeof(*entry);
+ *
+ *	do {
+ *		char raw_data[__entry_size]; <- allocate our sample in the stack
+ *		struct trace_entry *ent;
+ *
+ *		entry = (struct ftrace_raw_<call> *)raw_data;
+ *		ent = &entry->ent;
+ *		tracing_generic_entry_update(ent, irq_flags, pc);
+ *		ent->type = event_call->id;
+ *
+ *		<tstruct> <- do some jobs with dynamic arrays
+ *
+ *		<assign>  <- affect our values
+ *
+ *		perf_tpcounter_event(event_call->id, __addr, __count, entry,
+ *			     __entry_size);  <- submit them to perf counter
+ *	} while (0);
+ *
+ * }
+ */
+
+#ifdef CONFIG_EVENT_PROFILE
+
+#undef __perf_addr
+#define __perf_addr(a) __addr = (a)
+
+#undef __perf_count
+#define __perf_count(c) __count = (c)
+
+#undef TRACE_EVENT
+#define TRACE_EVENT(call, proto, args, tstruct, assign, print)		\
+static void ftrace_profile_##call(proto)				\
+{									\
+	struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
+	struct ftrace_event_call *event_call = &event_##call;		\
+	extern void perf_tpcounter_event(int, u64, u64, void *, int);	\
+	struct ftrace_raw_##call *entry;				\
+	u64 __addr = 0, __count = 1;					\
+	unsigned long irq_flags;					\
+	int __entry_size;						\
+	int __data_size;						\
+	int pc;								\
+									\
+	local_save_flags(irq_flags);					\
+	pc = preempt_count();						\
+									\
+	__data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
+	__entry_size = __data_size + sizeof(*entry);			\
+									\
+	do {								\
+		char raw_data[__entry_size];				\
+		struct trace_entry *ent;				\
+									\
+		entry = (struct ftrace_raw_##call *)raw_data;		\
+		ent = &entry->ent;					\
+		tracing_generic_entry_update(ent, irq_flags, pc);	\
+		ent->type = event_call->id;				\
+									\
+		tstruct							\
+									\
+		{ assign; }						\
+									\
Quoted 8 lines

include/trace/ftrace.h __attribute__((section("_ftrace_events"))) event_##call = { \ · line 709

Quoted 5 lines
include/trace/ftrace.h +5 -0
+		perf_tpcounter_event(event_call->id, __addr, __count, entry,\
+			     __entry_size);				\
+	} while (0);							\
+									\
+}

ok, so the one concern I have here is that the data needs to fit on the stack. What if someone puts a large string in the data?

Yeah, I'll try to fix this by storing strings as pointers and copy them in perf counter output only.

TODO-listed, with all the above.

Quoted 34 lines
Quoted 34 lines
include/trace/ftrace.h +4 -0
+
+#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
+#endif /* CONFIG_EVENT_PROFILE */
+
 #undef _TRACE_PROFILE_INIT
 
kernel/perf_counter.c +6 -0
index 06d210c..93f4312 100644
@@ -2646,6 +2646,7 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 		u64 counter;
 	} group_entry;
 	struct perf_callchain_entry *callchain = NULL;
+	struct perf_tracepoint_record *tp;
 	int callchain_size = 0;
 	u64 time;
 	struct {
@@ -2714,6 +2715,11 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 			header.size += sizeof(u64);
 	}
 
+	if (sample_type & PERF_SAMPLE_TP_RECORD) {
+		tp = data->private;
+		header.size += tp->size;
+	}
+
 	ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
 	if (ret)
 		return;
@@ -2777,6 +2783,9 @@ static void perf_counter_output(struct perf_counter *counter, int nmi,
 		}
 	}
 
Quoted 8 lines

kernel/perf_counter.c static void perf_counter_output(struct perf_counter *counter, int nmi, · line 2790

Quoted 5 lines
kernel/perf_counter.c +3 -0
+	if (sample_type & PERF_SAMPLE_TP_RECORD)
+		perf_output_copy(&handle, tp->record, tp->size);
+
 	perf_output_end(&handle);
 }

You seem to fail to round up to a multiple of u64 somewhere along the line, that'll mess things up as events are supposed to be u64 aligned.

Indeed.

Quoted 9 lines
Quoted 9 lines
tools/perf/builtin-record.c +0 -0
index 6da0992..90c9808 100644
@@ -412,6 +412,7 @@ static void create_counter(int counter, int cpu, pid_t pid)
 	if (call_graph)
 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
 
Quoted 6 lines

tools/perf/builtin-record.c static void create_counter(int counter, int cpu, pid_t pid) · line 418

Quoted 4 lines
tools/perf/builtin-record.c +1 -0
+
 	attr->mmap		= track;
 	attr->comm		= track;
 	attr->inherit		= (cpu < 0) && inherit;

Do we really need that extra whitespace?

Sorry, I added the explicit PERF_SAMPLE_TP_RECORD here to test this patch and forgot the newline when I removed it :-s

#11 lore raw
In reply to Frederic Weisbecker
Quoted 24 lines · Frederic Weisbecker
(quoted diff) +24 -0
+static void ftrace_profile_##call(proto)                               \
+{                                                                      \
+       struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
+       struct ftrace_event_call *event_call = &event_##call;           \
+       extern void perf_tpcounter_event(int, u64, u64, void *, int);   \
+       struct ftrace_raw_##call *entry;                                \
+       u64 __addr = 0, __count = 1;                                    \
+       unsigned long irq_flags;                                        \
+       int __entry_size;                                               \
+       int __data_size;                                                \
+       int pc;                                                         \
+                                                                       \
+       local_save_flags(irq_flags);                                    \
+       pc = preempt_count();                                           \
+                                                                       \
+       __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
+       __entry_size = __data_size + sizeof(*entry);                    \
+                                                                       \
+       do {                                                            \
+               char raw_data[__entry_size];                            \
+               struct trace_entry *ent;                                \
+                                                                       \
+               entry = (struct ftrace_raw_##call *)raw_data;           \
+               ent = &entry->ent;                                      \

Review

Quoted 10 lines
(quoted diff) +10 -0
+               tracing_generic_entry_update(ent, irq_flags, pc);       \
+               ent->type = event_call->id;                             \
+                                                                       \
+               tstruct                                                 \
+                                                                       \
+               { assign; }                                             \
+                                                                       \
+               perf_tpcounter_event(event_call->id, __addr, __count, entry,\
+                            __entry_size);                             \
+       } while (0);                                                    \

Causes:

ERROR: "tracing_generic_entry_update" [fs/jbd2/jbd2.ko] undefined!
ERROR: "tracing_generic_entry_update" [fs/ext4/ext4.ko] undefined!

Which will be fixed by:

---
 kernel/trace/trace.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
kernel/trace/trace.c +1 -0
index 6344573..e793cda 100644
@@ -848,6 +848,7 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
 		((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
 		(need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
 }
+EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
 
 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
 						    int type,
#12 lore raw

[tip:perfcounters/core] perfcounters: Support for ftrace event records sampling, fix modules

Commit-ID:  11c1870ca7e8dfede9f8aab654814a7a67378857
Gitweb:     http://git.kernel.org/tip/11c1870ca7e8dfede9f8aab654814a7a67378857
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 7 Aug 2009 17:54:38 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 7 Aug 2009 18:14:58 +0200

perfcounters: Support for ftrace event records sampling, fix modules

Fix:

ERROR: "tracing_generic_entry_update" [fs/jbd2/jbd2.ko] undefined!
ERROR: "tracing_generic_entry_update" [fs/ext4/ext4.ko] undefined!

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Gabriel Munteanu <[email protected]>
Cc Li Zefan <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Frederic Weisbecker <[email protected]>

LKML-Reference: <1249660478.32113.736.camel@twins>

---
 kernel/trace/trace.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
kernel/trace/trace.c +1 -0
index 8930e39..c22b40f 100644
@@ -848,6 +848,7 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
 		((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
 		(need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
 }
+EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
 
 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
 						    int type,