Skip to content

Commit e6dab5f

Browse files
avaginingomolnar
authored andcommitted
perf/trace: Add ability to set a target task for events
A few events are interesting not only for a current task. For example, sched_stat_* events are interesting for a task which wakes up. For this reason, it will be good if such events will be delivered to a target task too. Now a target task can be set by using __perf_task(). The original idea and a draft patch belongs to Peter Zijlstra. I need these events for profiling sleep times. sched_switch is used for getting callchains and sched_stat_* is used for getting time periods. These events are combined in user space, then it can be analyzed by perf tools. Inspired-by: Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Arun Sharma <asharma@fb.com> Signed-off-by: Andrew Vagin <avagin@openvz.org> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/1342016098-213063-1-git-send-email-avagin@openvz.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent d07bdfd commit e6dab5f

11 files changed

Lines changed: 60 additions & 14 deletions

File tree

‎include/linux/ftrace_event.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,10 @@ extern void *perf_trace_buf_prepare(int size, unsigned short type,
306306

307307
static inline void
308308
perf_trace_buf_submit(void *raw_data, int size, int rctx, u64 addr,
309-
u64 count, struct pt_regs *regs, void *head)
309+
u64 count, struct pt_regs *regs, void *head,
310+
struct task_struct *task)
310311
{
311-
perf_tp_event(addr, count, raw_data, size, regs, head, rctx);
312+
perf_tp_event(addr, count, raw_data, size, regs, head, rctx, task);
312313
}
313314
#endif
314315

‎include/linux/perf_event.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,8 @@ static inline bool perf_paranoid_kernel(void)
12721272
extern void perf_event_init(void);
12731273
extern void perf_tp_event(u64 addr, u64 count, void *record,
12741274
int entry_size, struct pt_regs *regs,
1275-
struct hlist_head *head, int rctx);
1275+
struct hlist_head *head, int rctx,
1276+
struct task_struct *task);
12761277
extern void perf_bp_event(struct perf_event *event, void *data);
12771278

12781279
#ifndef perf_misc_flags

‎include/trace/events/sched.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ DECLARE_EVENT_CLASS(sched_wakeup_template,
7373
__entry->prio = p->prio;
7474
__entry->success = success;
7575
__entry->target_cpu = task_cpu(p);
76+
)
77+
TP_perf_assign(
78+
__perf_task(p);
7679
),
7780

7881
TP_printk("comm=%s pid=%d prio=%d success=%d target_cpu=%03d",
@@ -325,6 +328,7 @@ DECLARE_EVENT_CLASS(sched_stat_template,
325328
)
326329
TP_perf_assign(
327330
__perf_count(delay);
331+
__perf_task(tsk);
328332
),
329333

330334
TP_printk("comm=%s pid=%d delay=%Lu [ns]",

‎include/trace/ftrace.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,9 @@ __attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
712712
#undef __perf_count
713713
#define __perf_count(c) __count = (c)
714714

715+
#undef __perf_task
716+
#define __perf_task(t) __task = (t)
717+
715718
#undef TP_perf_assign
716719
#define TP_perf_assign(args...) args
717720

@@ -725,6 +728,7 @@ perf_trace_##call(void *__data, proto) \
725728
struct ftrace_raw_##call *entry; \
726729
struct pt_regs __regs; \
727730
u64 __addr = 0, __count = 1; \
731+
struct task_struct *__task = NULL; \
728732
struct hlist_head *head; \
729733
int __entry_size; \
730734
int __data_size; \
@@ -752,7 +756,7 @@ perf_trace_##call(void *__data, proto) \
752756
\
753757
head = this_cpu_ptr(event_call->perf_events); \
754758
perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
755-
__count, &__regs, head); \
759+
__count, &__regs, head, __task); \
756760
}
757761

758762
/*

‎kernel/events/callchain.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ put_callchain_entry(int rctx)
153153
put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
154154
}
155155

156-
struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
156+
struct perf_callchain_entry *
157+
perf_callchain(struct perf_event *event, struct pt_regs *regs)
157158
{
158159
int rctx;
159160
struct perf_callchain_entry *entry;
@@ -178,6 +179,12 @@ struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
178179
}
179180

180181
if (regs) {
182+
/*
183+
* Disallow cross-task user callchains.
184+
*/
185+
if (event->ctx->task && event->ctx->task != current)
186+
goto exit_put;
187+
181188
perf_callchain_store(entry, PERF_CONTEXT_USER);
182189
perf_callchain_user(entry, regs);
183190
}

‎kernel/events/core.c‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,7 +4039,7 @@ void perf_prepare_sample(struct perf_event_header *header,
40394039
if (sample_type & PERF_SAMPLE_CALLCHAIN) {
40404040
int size = 1;
40414041

4042-
data->callchain = perf_callchain(regs);
4042+
data->callchain = perf_callchain(event, regs);
40434043

40444044
if (data->callchain)
40454045
size += data->callchain->nr;
@@ -5209,7 +5209,8 @@ static int perf_tp_event_match(struct perf_event *event,
52095209
}
52105210

52115211
void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5212-
struct pt_regs *regs, struct hlist_head *head, int rctx)
5212+
struct pt_regs *regs, struct hlist_head *head, int rctx,
5213+
struct task_struct *task)
52135214
{
52145215
struct perf_sample_data data;
52155216
struct perf_event *event;
@@ -5228,6 +5229,31 @@ void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
52285229
perf_swevent_event(event, count, &data, regs);
52295230
}
52305231

5232+
/*
5233+
* If we got specified a target task, also iterate its context and
5234+
* deliver this event there too.
5235+
*/
5236+
if (task && task != current) {
5237+
struct perf_event_context *ctx;
5238+
struct trace_entry *entry = record;
5239+
5240+
rcu_read_lock();
5241+
ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5242+
if (!ctx)
5243+
goto unlock;
5244+
5245+
list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5246+
if (event->attr.type != PERF_TYPE_TRACEPOINT)
5247+
continue;
5248+
if (event->attr.config != entry->type)
5249+
continue;
5250+
if (perf_tp_event_match(event, &data, regs))
5251+
perf_swevent_event(event, count, &data, regs);
5252+
}
5253+
unlock:
5254+
rcu_read_unlock();
5255+
}
5256+
52315257
perf_swevent_put_recursion_context(rctx);
52325258
}
52335259
EXPORT_SYMBOL_GPL(perf_tp_event);

‎kernel/events/internal.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ __output_copy(struct perf_output_handle *handle,
101101
}
102102

103103
/* Callchain handling */
104-
extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);
104+
extern struct perf_callchain_entry *
105+
perf_callchain(struct perf_event *event, struct pt_regs *regs);
105106
extern int get_callchain_buffers(void);
106107
extern void put_callchain_buffers(void);
107108

‎kernel/trace/trace_event_perf.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip)
281281

282282
head = this_cpu_ptr(event_function.perf_events);
283283
perf_trace_buf_submit(entry, ENTRY_SIZE, rctx, 0,
284-
1, &regs, head);
284+
1, &regs, head, NULL);
285285

286286
#undef ENTRY_SIZE
287287
}

‎kernel/trace/trace_kprobe.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,8 @@ static __kprobes void kprobe_perf_func(struct kprobe *kp,
10021002
store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
10031003

10041004
head = this_cpu_ptr(call->perf_events);
1005-
perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
1005+
perf_trace_buf_submit(entry, size, rctx,
1006+
entry->ip, 1, regs, head, NULL);
10061007
}
10071008

10081009
/* Kretprobe profile handler */
@@ -1033,7 +1034,8 @@ static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
10331034
store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
10341035

10351036
head = this_cpu_ptr(call->perf_events);
1036-
perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
1037+
perf_trace_buf_submit(entry, size, rctx,
1038+
entry->ret_ip, 1, regs, head, NULL);
10371039
}
10381040
#endif /* CONFIG_PERF_EVENTS */
10391041

‎kernel/trace/trace_syscalls.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
532532
(unsigned long *)&rec->args);
533533

534534
head = this_cpu_ptr(sys_data->enter_event->perf_events);
535-
perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
535+
perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL);
536536
}
537537

538538
int perf_sysenter_enable(struct ftrace_event_call *call)
@@ -608,7 +608,7 @@ static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
608608
rec->ret = syscall_get_return_value(current, regs);
609609

610610
head = this_cpu_ptr(sys_data->exit_event->perf_events);
611-
perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
611+
perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL);
612612
}
613613

614614
int perf_sysexit_enable(struct ftrace_event_call *call)

0 commit comments

Comments
 (0)