All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf_events: fixing time tracking for event with pid != -1 and cpu != -1
@ 2010-08-26 14:40 Stephane Eranian
  2010-08-30 15:48 ` [tip:perf/urgent] perf_events: Fix time tracking for events " tip-bot for Stephane Eranian
  0 siblings, 1 reply; 4+ messages in thread
From: Stephane Eranian @ 2010-08-26 14:40 UTC (permalink / raw)
  To: linux-kernel
  Cc: peterz, mingo, paulus, davem, fweisbec, perfmon2-devel, eranian,
	eranian

Per-thread events with a cpu filter, i.e., cpu != -1, were not reporting
correct timings when the thread never ran on the monitored cpu. The time
enabled was reported as a negative value.

This patch fixes the problem by updating tstamp_stopped, tstamp_running
in event_sched_out() for events with filters and which are marked as
INACTIVE.

The function group_sched_out() is modified to systematically call into
event_sched_out() to avoid duplicating the timing adjustment code twice.

With the patch, I now get:

$ task_cpu -i -e unhalted_core_cycles,unhalted_core_cycles noploop 2
noploop for 2 seconds
CPU0 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)
CPU0 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)

CPU1 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)
CPU1 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)

CPU2 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)
CPU2 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)

CPU3 4,747,990,931 unhalted_core_cycles (ena=1,991,136,594, run=1,991,136,594)
CPU3 4,747,990,931 unhalted_core_cycles (ena=1,991,136,594, run=1,991,136,594)

Signed-off-by: Stephane Eranian <eranian@gmail.com>

diff --git a/kernel/perf_event.c b/kernel/perf_event.c
index 0d38f27..d196412 100644
--- a/kernel/perf_event.c
+++ b/kernel/perf_event.c
@@ -402,11 +402,31 @@ static void perf_group_detach(struct perf_event *event)
 	}
 }
 
+static inline int
+event_filter_match(struct perf_event *event)
+{
+	return event->cpu == -1 || event->cpu == smp_processor_id();
+}
+
 static void
 event_sched_out(struct perf_event *event,
 		  struct perf_cpu_context *cpuctx,
 		  struct perf_event_context *ctx)
 {
+	u64 delta;
+	/*
+	 * an event which could not be activated because of
+	 * filter mismatch still needs to have its timings
+	 * maintained, otherwise bogus information is return
+	 * via read() for time_enabled, time_running
+	 */
+	if (event->state == PERF_EVENT_STATE_INACTIVE
+	    && !event_filter_match(event)) {
+		delta = ctx->time - event->tstamp_stopped;
+		event->tstamp_running += delta;
+		event->tstamp_stopped = ctx->time;
+	}
+
 	if (event->state != PERF_EVENT_STATE_ACTIVE)
 		return;
 
@@ -432,9 +452,7 @@ group_sched_out(struct perf_event *group_event,
 		struct perf_event_context *ctx)
 {
 	struct perf_event *event;
-
-	if (group_event->state != PERF_EVENT_STATE_ACTIVE)
-		return;
+	int state = group_event->state;
 
 	event_sched_out(group_event, cpuctx, ctx);
 
@@ -444,7 +462,7 @@ group_sched_out(struct perf_event *group_event,
 	list_for_each_entry(event, &group_event->sibling_list, group_entry)
 		event_sched_out(event, cpuctx, ctx);
 
-	if (group_event->attr.exclusive)
+	if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
 		cpuctx->exclusive = 0;
 }
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [tip:perf/urgent] perf_events: Fix time tracking for events with pid != -1 and cpu != -1
  2010-08-26 14:40 [PATCH] perf_events: fixing time tracking for event with pid != -1 and cpu != -1 Stephane Eranian
@ 2010-08-30 15:48 ` tip-bot for Stephane Eranian
  2010-08-31  1:10   ` Lin Ming
  0 siblings, 1 reply; 4+ messages in thread
From: tip-bot for Stephane Eranian @ 2010-08-30 15:48 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: linux-kernel, eranian, hpa, mingo, a.p.zijlstra, tglx, eranian,
	mingo

Commit-ID:  fa66f07aa1f0950e1dc78b7ab39728b3f8aa77a1
Gitweb:     http://git.kernel.org/tip/fa66f07aa1f0950e1dc78b7ab39728b3f8aa77a1
Author:     Stephane Eranian <eranian@google.com>
AuthorDate: Thu, 26 Aug 2010 16:40:01 +0200
Committer:  Ingo Molnar <mingo@elte.hu>
CommitDate: Mon, 30 Aug 2010 12:16:55 +0200

perf_events: Fix time tracking for events with pid != -1 and cpu != -1

Per-thread events with a cpu filter, i.e., cpu != -1, were not
reporting correct timings when the thread never ran on the
monitored cpu. The time enabled was reported as a negative
value.

This patch fixes the problem by updating tstamp_stopped,
tstamp_running in event_sched_out() for events with filters and
which are marked as INACTIVE.

The function group_sched_out() is modified to systematically
call into event_sched_out() to avoid duplicating the timing
adjustment code twice.

With the patch, I now get:

$ task_cpu -i -e unhalted_core_cycles,unhalted_core_cycles
noploop 2 noploop for 2 seconds
CPU0 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)
CPU0 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)

CPU1 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)
CPU1 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)

CPU2 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)
CPU2 0		   unhalted_core_cycles (ena=1,991,136,594, run=0)

CPU3 4,747,990,931 unhalted_core_cycles (ena=1,991,136,594, run=1,991,136,594)
CPU3 4,747,990,931 unhalted_core_cycles (ena=1,991,136,594, run=1,991,136,594)

Signed-off-by: Stephane Eranian <eranian@gmail.com>
Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: paulus@samba.org
Cc: davem@davemloft.net
Cc: fweisbec@gmail.com
Cc: perfmon2-devel@lists.sf.net
Cc: eranian@google.com
LKML-Reference: <4c76802d.aae9d80a.115d.70fe@mx.google.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
---
 kernel/perf_event.c |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/kernel/perf_event.c b/kernel/perf_event.c
index 403d180..657555a 100644
--- a/kernel/perf_event.c
+++ b/kernel/perf_event.c
@@ -402,11 +402,31 @@ static void perf_group_detach(struct perf_event *event)
 	}
 }
 
+static inline int
+event_filter_match(struct perf_event *event)
+{
+	return event->cpu == -1 || event->cpu == smp_processor_id();
+}
+
 static void
 event_sched_out(struct perf_event *event,
 		  struct perf_cpu_context *cpuctx,
 		  struct perf_event_context *ctx)
 {
+	u64 delta;
+	/*
+	 * An event which could not be activated because of
+	 * filter mismatch still needs to have its timings
+	 * maintained, otherwise bogus information is return
+	 * via read() for time_enabled, time_running:
+	 */
+	if (event->state == PERF_EVENT_STATE_INACTIVE
+	    && !event_filter_match(event)) {
+		delta = ctx->time - event->tstamp_stopped;
+		event->tstamp_running += delta;
+		event->tstamp_stopped = ctx->time;
+	}
+
 	if (event->state != PERF_EVENT_STATE_ACTIVE)
 		return;
 
@@ -432,9 +452,7 @@ group_sched_out(struct perf_event *group_event,
 		struct perf_event_context *ctx)
 {
 	struct perf_event *event;
-
-	if (group_event->state != PERF_EVENT_STATE_ACTIVE)
-		return;
+	int state = group_event->state;
 
 	event_sched_out(group_event, cpuctx, ctx);
 
@@ -444,7 +462,7 @@ group_sched_out(struct perf_event *group_event,
 	list_for_each_entry(event, &group_event->sibling_list, group_entry)
 		event_sched_out(event, cpuctx, ctx);
 
-	if (group_event->attr.exclusive)
+	if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
 		cpuctx->exclusive = 0;
 }
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [tip:perf/urgent] perf_events: Fix time tracking for events with pid != -1 and cpu != -1
  2010-08-30 15:48 ` [tip:perf/urgent] perf_events: Fix time tracking for events " tip-bot for Stephane Eranian
@ 2010-08-31  1:10   ` Lin Ming
  2010-08-31  8:03     ` stephane eranian
  0 siblings, 1 reply; 4+ messages in thread
From: Lin Ming @ 2010-08-31  1:10 UTC (permalink / raw)
  To: mingo, hpa, eranian, linux-kernel, a.p.zijlstra, tglx, eranian,
	mingo
  Cc: linux-tip-commits

>
> perf_events: Fix time tracking for events with pid != -1 and cpu != -1

Hi,

I thought pid != -1 and cpu != -1 is not valid.

# perf top -p 23268 -C 1
WARNING: PID switch overriding CPU

In which case this could be valid?

Thanks,
Lin Ming

>
> Per-thread events with a cpu filter, i.e., cpu != -1, were not
> reporting correct timings when the thread never ran on the
> monitored cpu. The time enabled was reported as a negative
> value.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [tip:perf/urgent] perf_events: Fix time tracking for events with pid != -1 and cpu != -1
  2010-08-31  1:10   ` Lin Ming
@ 2010-08-31  8:03     ` stephane eranian
  0 siblings, 0 replies; 4+ messages in thread
From: stephane eranian @ 2010-08-31  8:03 UTC (permalink / raw)
  To: Lin Ming
  Cc: mingo, hpa, eranian, linux-kernel, a.p.zijlstra, tglx, mingo,
	linux-tip-commits

On Tue, Aug 31, 2010 at 3:10 AM, Lin Ming <lin@ming.vg> wrote:
>>
>> perf_events: Fix time tracking for events with pid != -1 and cpu != -1
>
> Hi,
>
> I thought pid != -1 and cpu != -1 is not valid.
>

This is in per-thread mode. This is what happens when you do
perf record foo for instance. This is sampling, but you can do
the same with counting.


> # perf top -p 23268 -C 1
> WARNING: PID switch overriding CPU
>
> In which case this could be valid?
>
> Thanks,
> Lin Ming
>
>>
>> Per-thread events with a cpu filter, i.e., cpu != -1, were not
>> reporting correct timings when the thread never ran on the
>> monitored cpu. The time enabled was reported as a negative
>> value.
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-08-31  8:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-26 14:40 [PATCH] perf_events: fixing time tracking for event with pid != -1 and cpu != -1 Stephane Eranian
2010-08-30 15:48 ` [tip:perf/urgent] perf_events: Fix time tracking for events " tip-bot for Stephane Eranian
2010-08-31  1:10   ` Lin Ming
2010-08-31  8:03     ` stephane eranian

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.