Skip to contentSkip to conversation

perf: Don't schedule tracepoints when exclude_kernel is set

PATCH In review Frederic Weisbecker 3 participants 6 messages Apr 8, 2011 – Apr 12, 2011

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

PATCH 1/1 perf: Don't schedule tracepoints when exclude_kernel is set

Instead of checking attr.exclude_kernel anytime a tracepoint event triggers, simply don't schedule the tracepoint it that attribute is set. This makes one test less in the tracing path.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 kernel/perf_event.c             |    5 -----
 kernel/trace/trace_event_perf.c |    6 +++++-
 2 files changed, 5 insertions(+), 6 deletions(-)
kernel/perf_event.c +0 -5
index c75925c..2f3fb90 100644
@@ -5491,11 +5491,6 @@ static int perf_tp_event_match(struct perf_event *event,
 {
 	if (event->hw.state & PERF_HES_STOPPED)
 		return 0;
-	/*
-	 * All tracepoints are from kernel-space.
-	 */
-	if (event->attr.exclude_kernel)
-		return 0;
 
 	if (!perf_tp_filter_match(event, data))
 		return 0;
kernel/trace/trace_event_perf.c +5 -1
index 19a359d..427314b 100644
@@ -136,6 +136,10 @@ int perf_trace_add(struct perf_event *p_event, int flags)
 	struct hlist_head __percpu *pcpu_list;
 	struct hlist_head *list;
 
+	/* Tracepoints always trigger in the kernel */
+	if (p_event->attr.exclude_kernel)
+		return 0;
+
 	pcpu_list = tp_event->perf_events;
 	if (WARN_ON_ONCE(!pcpu_list))
 		return -EINVAL;
@@ -151,7 +155,7 @@ int perf_trace_add(struct perf_event *p_event, int flags)
 
 void perf_trace_del(struct perf_event *p_event, int flags)
 {
-	hlist_del_rcu(&p_event->hlist_entry);
+	hlist_del_init_rcu(&p_event->hlist_entry);
 }
 
 void perf_trace_destroy(struct perf_event *p_event)
Signature · 1 line
1.7.3.2
#1 lore raw
Quoted 4 lines · Frederic Weisbecker

Instead of checking attr.exclude_kernel anytime a tracepoint event triggers, simply don't schedule the tracepoint it that attribute is set. This makes one test less in the tracing path.

Meh, I'd much rather someone spend some time on finishing the below, which is a much bigger improvement for trace-events.

---
Subject: perf: Tracepoint collection support
From: Peter Zijlstra <[email protected]>
Date: Sat Nov 20 18:09:34 CET 2010

Due to popular demand this implements a tracepoint collection event { .type = PERF_TYPE_TRACEPOINT, .config = ~0ULL }. by default it contains no tracepoints, but tracepoints can be added using:
ioctl(fd, PERF_EVENT_IOC_ADD_TP, tp_id);

In order to provide a dense ID space for tracepoints replace the tracepoint ID generation with an IDR tree.

Furthermore, replace the whole trace-event <-> perf infrastructure with multiple IDR trees.

We keep an IDR tree per perf_event, this tree collects all the tracepoints the perf_event is interested in and stores the corresponding 'node'. This tree manages the node life-time.

Then we keep an IDR tree per task and per CPU. Both of these trees are accumulation trees, they're the union of all events of that particular task/CPU. It stores a list of 'node's.

Then, when a trace-event happens, we look for it in both the CPU tree as well as the current task tree. For both, if present we iterate the node list and deliver the event to the corresponding perf_events.

We manage the IDR trees on perf_event creation/destruction and ioctl(ADD_TP) time. This mean that pmu::{add,remove} are empty ops, the per-task stat is taken care of in the per-task tree after all.

The patch got hammered by tglx, but wants some more review as well as solving the open points before merging.

The open points are:
- like the breakpoint muck it suffers from the pmu::event_init() vs context-attach inversion, and
- the whole fancy inherited context avoid switch logic needs a fix.

[ merged a cleanup contributed by Lin Ming ]

---
 include/linux/ftrace_event.h    |   10 
 include/linux/perf_event.h      |    9 
 include/linux/sched.h           |    9 
 include/trace/ftrace.h          |    4 
 kernel/perf_event.c             |  416 ++++++++++++++++++++++++++++++++++++++--
 kernel/trace/trace_event_perf.c |   95 +++------
 kernel/trace/trace_kprobe.c     |   10 
 kernel/trace/trace_output.c     |  116 +++--------
 kernel/trace/trace_syscalls.c   |    8 
 9 files changed, 503 insertions(+), 174 deletions(-)
include/linux/ftrace_event.h +4 -6
Index: linux-2.6/include/linux/ftrace_event.h
===================================================================
@@ -86,8 +86,6 @@ struct trace_event_functions {
 };
 
 struct trace_event {
-	struct hlist_node		node;
-	struct list_head		list;
 	int				type;
 	struct trace_event_functions	*funcs;
 };
@@ -193,7 +191,6 @@ struct ftrace_event_call {
 
 #ifdef CONFIG_PERF_EVENTS
 	int				perf_refcount;
-	struct hlist_head __percpu	*perf_events;
 #endif
 };
 
@@ -262,8 +259,9 @@ struct perf_event;
 
 DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
 
-extern int  perf_trace_init(struct perf_event *event);
+extern int  perf_trace_init(struct perf_event *event, int event_id);
 extern void perf_trace_destroy(struct perf_event *event);
+extern void perf_trace_destroy_id(int id);
 extern int  perf_trace_add(struct perf_event *event, int flags);
 extern void perf_trace_del(struct perf_event *event, int flags);
 extern int  ftrace_profile_set_filter(struct perf_event *event, int event_id,
@@ -274,9 +272,9 @@ extern void *perf_trace_buf_prepare(int 
 
 static inline void
 perf_trace_buf_submit(void *raw_data, int size, int rctx, u64 addr,
-		       u64 count, struct pt_regs *regs, void *head)
+		       u64 count, struct pt_regs *regs, int id)
 {
-	perf_tp_event(addr, count, raw_data, size, regs, head, rctx);
+	perf_tp_event(addr, count, raw_data, size, regs, rctx, id);
 }
 #endif
 
include/linux/perf_event.h +8 -1
Index: linux-2.6/include/linux/perf_event.h
===================================================================
@@ -245,6 +245,7 @@ struct perf_event_attr {
 #define PERF_EVENT_IOC_PERIOD		_IOW('$', 4, __u64)
 #define PERF_EVENT_IOC_SET_OUTPUT	_IO ('$', 5)
 #define PERF_EVENT_IOC_SET_FILTER	_IOW('$', 6, char *)
+#define PERF_EVENT_IOC_ADD_TP		_IO ('$', 7)
 
 enum perf_event_ioc_flags {
 	PERF_IOC_FLAG_GROUP		= 1U << 0,
@@ -566,6 +567,11 @@ struct hw_perf_event {
 			struct task_struct		*bp_target;
 		};
 #endif
+		/*
+		 * Same fudge as for breakpoints, trace-events needs
+		 * it too,.. convert the bp crap over..
+		 */
+		struct task_struct *event_target;
 	};
 	int				state;
 	local64_t			prev_count;
@@ -857,6 +863,7 @@ struct perf_event {
 #ifdef CONFIG_EVENT_TRACING
 	struct ftrace_event_call	*tp_event;
 	struct event_filter		*filter;
+	struct perf_tp_idr		tp_idr;
 #endif
 
 #ifdef CONFIG_CGROUP_PERF
@@ -1139,7 +1146,7 @@ static inline bool perf_paranoid_kernel(
 extern void perf_event_init(void);
 extern void perf_tp_event(u64 addr, u64 count, void *record,
 			  int entry_size, struct pt_regs *regs,
-			  struct hlist_head *head, int rctx);
+			  int rctx, int id);
 extern void perf_bp_event(struct perf_event *event, void *data);
 
 #ifndef perf_misc_flags
include/linux/sched.h +9 -0
Index: linux-2.6/include/linux/sched.h
===================================================================
@@ -82,6 +82,7 @@ struct sched_param {
 #include <linux/rculist.h>
 #include <linux/rtmutex.h>
 
+#include <linux/idr.h>
 #include <linux/time.h>
 #include <linux/param.h>
 #include <linux/resource.h>
@@ -1190,6 +1191,11 @@ enum perf_event_task_context {
 	perf_nr_task_contexts,
 };
 
+struct perf_tp_idr {
+	struct mutex	lock;
+	struct idr	idr;
+};
+
 struct task_struct {
 	volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
 	void *stack;
@@ -1473,6 +1479,9 @@ struct task_struct {
 	struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
 	struct mutex perf_event_mutex;
 	struct list_head perf_event_list;
+#ifdef CONFIG_EVENT_TRACING
+	struct perf_tp_idr *perf_tp_idr;
+#endif
 #endif
 #ifdef CONFIG_NUMA
 	struct mempolicy *mempolicy;	/* Protected by alloc_lock */
include/trace/ftrace.h +1 -3
Index: linux-2.6/include/trace/ftrace.h
===================================================================
@@ -708,7 +708,6 @@ perf_trace_##call(void *__data, proto)		
 	struct ftrace_raw_##call *entry;				\
 	struct pt_regs __regs;						\
 	u64 __addr = 0, __count = 1;					\
-	struct hlist_head *head;					\
 	int __entry_size;						\
 	int __data_size;						\
 	int rctx;							\
@@ -733,9 +732,8 @@ perf_trace_##call(void *__data, proto)		
 									\
 	{ assign; }							\
 									\
-	head = this_cpu_ptr(event_call->perf_events);			\
 	perf_trace_buf_submit(entry, __entry_size, rctx, __addr,	\
-		__count, &__regs, head);				\
+		__count, &__regs, event_call->event.type);		\
 }
 
 /*
kernel/perf_event.c +396 -20

594 lines not on this page · raw message

kernel/trace/trace_event_perf.c +40 -55

148 lines not on this page · raw message

kernel/trace/trace_kprobe.c +4 -6
Index: linux-2.6/kernel/trace/trace_kprobe.c
===================================================================
@@ -1660,7 +1660,6 @@ static __kprobes void kprobe_perf_func(s
 	struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
 	struct ftrace_event_call *call = &tp->call;
 	struct kprobe_trace_entry_head *entry;
-	struct hlist_head *head;
 	int size, __size, dsize;
 	int rctx;
 
@@ -1680,8 +1679,8 @@ static __kprobes void kprobe_perf_func(s
 	memset(&entry[1], 0, dsize);
 	store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
 
-	head = this_cpu_ptr(call->perf_events);
-	perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
+	perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs,
+			      call->event.type);
 }
 
 /* Kretprobe profile handler */
@@ -1691,7 +1690,6 @@ static __kprobes void kretprobe_perf_fun
 	struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
 	struct ftrace_event_call *call = &tp->call;
 	struct kretprobe_trace_entry_head *entry;
-	struct hlist_head *head;
 	int size, __size, dsize;
 	int rctx;
 
@@ -1711,8 +1709,8 @@ static __kprobes void kretprobe_perf_fun
 	entry->ret_ip = (unsigned long)ri->ret_addr;
 	store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
 
-	head = this_cpu_ptr(call->perf_events);
-	perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
+	perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1,
+			      regs, call->event.type);
 }
 
 static int probe_perf_enable(struct ftrace_event_call *call)
kernel/trace/trace_output.c +39 -77

173 lines not on this page · raw message

kernel/trace/trace_syscalls.c +2 -6
Index: linux-2.6/kernel/trace/trace_syscalls.c
===================================================================
@@ -499,7 +499,6 @@ static void perf_syscall_enter(void *ign
 {
 	struct syscall_metadata *sys_data;
 	struct syscall_trace_enter *rec;
-	struct hlist_head *head;
 	int syscall_nr;
 	int rctx;
 	int size;
@@ -530,8 +529,7 @@ static void perf_syscall_enter(void *ign
 	syscall_get_arguments(current, regs, 0, sys_data->nb_args,
 			       (unsigned long *)&rec->args);
 
-	head = this_cpu_ptr(sys_data->enter_event->perf_events);
-	perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
+	perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, rec->ent.type);
 }
 
 int perf_sysenter_enable(struct ftrace_event_call *call)
@@ -573,7 +571,6 @@ static void perf_syscall_exit(void *igno
 {
 	struct syscall_metadata *sys_data;
 	struct syscall_trace_exit *rec;
-	struct hlist_head *head;
 	int syscall_nr;
 	int rctx;
 	int size;
@@ -606,8 +603,7 @@ static void perf_syscall_exit(void *igno
 	rec->nr = syscall_nr;
 	rec->ret = syscall_get_return_value(current, regs);
 
-	head = this_cpu_ptr(sys_data->exit_event->perf_events);
-	perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
+	perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, rec->ent.type);
 }
 
 int perf_sysexit_enable(struct ftrace_event_call *call)
#2 lore raw
Quoted 14 lines · Peter Zijlstra
Quoted 4 lines · Frederic Weisbecker

Instead of checking attr.exclude_kernel anytime a tracepoint event triggers, simply don't schedule the tracepoint it that attribute is set. This makes one test less in the tracing path.

Meh, I'd much rather someone spend some time on finishing the below, which is a much bigger improvement for trace-events.

---
Subject: perf: Tracepoint collection support
From: Peter Zijlstra <[email protected]>
Date: Sat Nov 20 18:09:34 CET 2010

Quoted 4 lines

Due to popular demand this implements a tracepoint collection event { .type = PERF_TYPE_TRACEPOINT, .config = ~0ULL }. by default it contains no tracepoints, but tracepoints can be added using:
ioctl(fd, PERF_EVENT_IOC_ADD_TP, tp_id);

+1 !!!

This is like a super-important feature.

Thanks,

Ingo

#3 lore raw
In reply to Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 4 lines · Frederic Weisbecker

Instead of checking attr.exclude_kernel anytime a tracepoint event triggers, simply don't schedule the tracepoint it that attribute is set. This makes one test less in the tracing path.

Meh, I'd much rather someone spend some time on finishing the below, which is a much bigger improvement for trace-events.

I secretely added that to my pile already :)
That's indeed something we really want.

The above is just a little thing I noticed yesterday and I wanted to fix. Nothing more.

About that tracepoint collection, I'm not sure I like the idr though. That thing seems to be O(log(n)), I which we can rather approach O(1) when possible, using a hlist perhaps.

#4 lore raw
Quoted 13 lines · Frederic Weisbecker
Quoted 8 lines · Peter Zijlstra
Quoted 4 lines · Frederic Weisbecker

Instead of checking attr.exclude_kernel anytime a tracepoint event triggers, simply don't schedule the tracepoint it that attribute is set. This makes one test less in the tracing path.

Meh, I'd much rather someone spend some time on finishing the below, which is a much bigger improvement for trace-events.

I secretely added that to my pile already :)
That's indeed something we really want.

Quoted 6 lines

The above is just a little thing I noticed yesterday and I wanted to fix. Nothing more.

About that tracepoint collection, I'm not sure I like the idr though. That thing seems to be O(log(n)), I which we can rather approach O(1) when possible, using a hlist perhaps.

We only do 2 idr lookups at most and don't get to muck about with hash table collisions etc. But if you can show that that is the bottleneck you can replace the task/cpu idrs with a hashtable.

You will still need an IDR to deal with the traceevents (trace_type_idr) in order to get the dense ID space, and the per-event idr isn't used in the fast path at all.

#5 lore raw
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Frederic Weisbecker
Quoted 8 lines · Peter Zijlstra
Quoted 4 lines · Frederic Weisbecker

Instead of checking attr.exclude_kernel anytime a tracepoint event triggers, simply don't schedule the tracepoint it that attribute is set. This makes one test less in the tracing path.

Meh, I'd much rather someone spend some time on finishing the below, which is a much bigger improvement for trace-events.

I secretely added that to my pile already :)
That's indeed something we really want.

The above is just a little thing I noticed yesterday and I wanted to fix. Nothing more.

Quoted 7 lines
Quoted 3 lines

About that tracepoint collection, I'm not sure I like the idr though. That thing seems to be O(log(n)), I which we can rather approach O(1) when possible, using a hlist perhaps.

We only do 2 idr lookups at most and don't get to muck about with hash table collisions etc. But if you can show that that is the bottleneck you can replace the task/cpu idrs with a hashtable.

We can probably start with the irds to begin with. The implementation can be enhanced anytime.

Quoted 4 lines

You will still need an IDR to deal with the traceevents (trace_type_idr) in order to get the dense ID space, and the per-event idr isn't used in the fast path at all.

Yep, no problem with that.