All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Vagin <avagin@openvz.org>
To: linux-kernel@vger.kernel.org
Cc: Steven Rostedt <rostedt@goodmis.org>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Paul Mackerras <paulus@samba.org>,
	Arnaldo Carvalho de Melo <acme@ghostprotocols.net>,
	avagin@openvz.org
Subject: [PATCH 3/4] trace: add ability to collect call chain of non-current task.
Date: Mon, 26 Sep 2011 19:55:34 +0400	[thread overview]
Message-ID: <1317052535-1765247-4-git-send-email-avagin@openvz.org> (raw)
In-Reply-To: <1317052535-1765247-1-git-send-email-avagin@openvz.org>

An event may requires call chain of non-current task.  E.g. You may want
to know where and how long a task is sleeping. A time slice is known
when the task wakes up.  We can send event in this moment, but a target
task isn't "current".

The macro __perf_task set a task for which a call chain will be
collected.

Know issues:
* Now call chains for non-current tasks are collected on x86 only,
  but it may be done for other architectures simply.
* It collects only kernel call chains, because we can't get direct
  access to memory of other processes and this operation should be
  fast enough.

Signed-off-by: Andrew Vagin <avagin@openvz.org>
---
 arch/x86/kernel/cpu/perf_event.c |    8 +++-----
 include/trace/ftrace.h           |   11 +++++++++--
 kernel/events/core.c             |   30 ++++++++++++++++++------------
 3 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/arch/x86/kernel/cpu/perf_event.c b/arch/x86/kernel/cpu/perf_event.c
index c3d229d..18817a8 100644
--- a/arch/x86/kernel/cpu/perf_event.c
+++ b/arch/x86/kernel/cpu/perf_event.c
@@ -1839,17 +1839,15 @@ void
 perf_callchain_kernel(struct perf_callchain_entry *entry,
 			struct task_struct *tsk, struct pt_regs *regs)
 {
-	if (!regs)
-		return;
-
 	if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
 		/* TODO: We don't support guest os callchain now */
 		return;
 	}
 
-	perf_callchain_store(entry, regs->ip);
+	if (regs)
+		perf_callchain_store(entry, regs->ip);
 
-	dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
+	dump_trace(tsk, regs, NULL, 0, &backtrace_ops, entry);
 }
 
 #ifdef CONFIG_COMPAT
diff --git a/include/trace/ftrace.h b/include/trace/ftrace.h
index 5e4c72a..d03e926 100644
--- a/include/trace/ftrace.h
+++ b/include/trace/ftrace.h
@@ -711,6 +711,9 @@ __attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
 #undef __perf_count
 #define __perf_count(c) __count = (c)
 
+#undef __perf_task
+#define __perf_task(t) __task = (t)
+
 #undef TP_perf_assign
 #define TP_perf_assign(args...) args
 
@@ -722,7 +725,8 @@ perf_trace_##call(void *__data, proto)					\
 	struct ftrace_event_call *event_call = __data;			\
 	struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
 	struct ftrace_raw_##call *entry;				\
-	struct pt_regs __regs;						\
+	struct pt_regs __regs, *__pregs = &__regs;			\
+	struct task_struct *__task = current;				\
 	u64 __addr = 0, __count = 1;					\
 	struct hlist_head *head;					\
 	int __entry_size;						\
@@ -749,9 +753,12 @@ perf_trace_##call(void *__data, proto)					\
 									\
 	{ assign; }							\
 									\
+	if (__task != current)						\
+		__pregs = NULL;						\
+									\
 	head = this_cpu_ptr(event_call->perf_events);			\
 	perf_tp_event(__addr, __count, entry, __entry_size,		\
-				current, &__regs, head, rctx);		\
+				__task, __pregs, head, rctx);		\
 }
 
 /*
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 41ce4db..2dce0f9 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2757,16 +2757,16 @@ static struct perf_callchain_entry
 
 	entry->nr = 0;
 
-	if (!user_mode(regs)) {
+	if (!regs || !user_mode(regs)) {
 		perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
 		perf_callchain_kernel(entry, tsk, regs);
-		if (current->mm)
-			regs = task_pt_regs(current);
+		if (tsk->mm)
+			regs = task_pt_regs(tsk);
 		else
 			regs = NULL;
 	}
 
-	if (regs) {
+	if (tsk == current && regs) {
 		perf_callchain_store(entry, PERF_CONTEXT_USER);
 		perf_callchain_user(entry, regs);
 	}
@@ -3746,7 +3746,8 @@ EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
 
 static void __perf_event_header__init_id(struct perf_event_header *header,
 					 struct perf_sample_data *data,
-					 struct perf_event *event)
+					 struct perf_event *event,
+					 struct task_struct *tsk)
 {
 	u64 sample_type = event->attr.sample_type;
 
@@ -3755,8 +3756,8 @@ static void __perf_event_header__init_id(struct perf_event_header *header,
 
 	if (sample_type & PERF_SAMPLE_TID) {
 		/* namespace issues */
-		data->tid_entry.pid = perf_event_pid(event, current);
-		data->tid_entry.tid = perf_event_tid(event, current);
+		data->tid_entry.pid = perf_event_pid(event, tsk);
+		data->tid_entry.tid = perf_event_tid(event, tsk);
 	}
 
 	if (sample_type & PERF_SAMPLE_TIME)
@@ -3779,7 +3780,7 @@ void perf_event_header__init_id(struct perf_event_header *header,
 				struct perf_event *event)
 {
 	if (event->attr.sample_id_all)
-		__perf_event_header__init_id(header, data, event);
+		__perf_event_header__init_id(header, data, event, current);
 }
 
 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
@@ -4000,12 +4001,17 @@ void perf_prepare_sample(struct perf_event_header *header,
 	header->size = sizeof(*header) + event->header_size;
 
 	header->misc = 0;
-	header->misc |= perf_misc_flags(regs);
 
-	__perf_event_header__init_id(header, data, event);
+	__perf_event_header__init_id(header, data, event, tsk);
 
-	if (sample_type & PERF_SAMPLE_IP)
-		data->ip = perf_instruction_pointer(regs);
+	if (regs) {
+		header->misc |= perf_misc_flags(regs);
+		if (sample_type & PERF_SAMPLE_IP)
+			data->ip = perf_instruction_pointer(regs);
+	} else {
+		header->misc |= PERF_RECORD_MISC_KERNEL;
+		data->ip = 0;
+	}
 
 	if (sample_type & PERF_SAMPLE_CALLCHAIN) {
 		int size = 1;
-- 
1.7.1


  parent reply	other threads:[~2011-09-26 15:55 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-26 15:55 [PATCH 0/4] trace: add ability to collect call chains " Andrew Vagin
2011-09-26 15:55 ` [PATCH 1/4] perf: fix counter of ftrace events Andrew Vagin
2011-10-14 19:11   ` Arun Sharma
2011-09-26 15:55 ` [PATCH 2/4] trace: prepare to collect call chains of non-current task Andrew Vagin
2011-09-27 14:02   ` Peter Zijlstra
2011-10-14 19:12   ` Arun Sharma
2011-09-26 15:55 ` Andrew Vagin [this message]
2011-09-27 14:05   ` [PATCH 3/4] trace: add ability to collect call chain " Peter Zijlstra
2011-09-27 20:55     ` Frederic Weisbecker
2011-09-28 13:53       ` Andrew Vagin
2011-12-08  2:02       ` Arun Sharma
2011-12-09 11:07         ` Andrey Vagin
2011-12-14 20:14           ` Arun Sharma
2011-12-15 15:19             ` Andrew Vagin
2011-12-15 19:08               ` Arun Sharma
2011-12-16  0:15                 ` Arun Sharma
2011-09-26 15:55 ` [PATCH 4/4] events: sched_stat_template saves call chains of a target task Andrew Vagin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1317052535-1765247-4-git-send-email-avagin@openvz.org \
    --to=avagin@openvz.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@ghostprotocols.net \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=paulus@samba.org \
    --cc=rostedt@goodmis.org \
    --subject='Re: [PATCH 3/4] trace: add ability to collect call chain of non-current task.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.