All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <a.p.zijlstra@chello.nl>
To: eranian@google.com, fweisbec@gmail.com, paulus@samba.org,
	mingo@elte.hu, davem@davemloft.net, robert.richter@amd.com
Cc: linux-kernel@vger.kernel.org, Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: [PATCH 02/10] perf_events: Add fast-path to the rescheduling code
Date: Fri, 22 Jan 2010 16:50:46 +0100	[thread overview]
Message-ID: <20100122155535.318710047@chello.nl> (raw)
In-Reply-To: 20100122155044.391857484@chello.nl

[-- Attachment #1: stephane-perf_events-improve_x86_event_scheduling__v6_incremental.patch --]
[-- Type: text/plain, Size: 8390 bytes --]

The 6th version does:
- implement correct fastpath scheduling, i.e., reuse previous assignment
- skip reprogramming counters in hw_perf_enable() with a generation number

Signed-off-by: Stephane Eranian <eranian@google.com>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
LKML-Reference: <4b588464.1818d00a.4456.383b@mx.google.com>
---
 arch/x86/kernel/cpu/perf_event.c |  148 +++++++++++++++++++++++----------------
 include/linux/perf_event.h       |    2 
 2 files changed, 93 insertions(+), 57 deletions(-)

Index: linux-2.6/arch/x86/kernel/cpu/perf_event.c
===================================================================
--- linux-2.6.orig/arch/x86/kernel/cpu/perf_event.c
+++ linux-2.6/arch/x86/kernel/cpu/perf_event.c
@@ -87,6 +87,7 @@ struct cpu_hw_events {
 	int			n_events;
 	int			n_added;
 	int			assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
+	u64			tags[X86_PMC_IDX_MAX];
 	struct perf_event	*event_list[X86_PMC_IDX_MAX]; /* in enabled order */
 };
 
@@ -1013,6 +1014,8 @@ static int __hw_perf_event_init(struct p
 	hwc->config = ARCH_PERFMON_EVENTSEL_INT;
 
 	hwc->idx = -1;
+	hwc->last_cpu = -1;
+	hwc->last_tag = ~0ULL;
 
 	/*
 	 * Count user and OS events unless requested not to.
@@ -1245,6 +1248,46 @@ static int x86_schedule_events(struct cp
 	}
 
 	/*
+	 * fastpath, try to reuse previous register
+	 */
+	for (i = 0, num = n; i < n; i++, num--) {
+		hwc = &cpuc->event_list[i]->hw;
+		c = (unsigned long *)constraints[i];
+
+		/* never assigned */
+		if (hwc->idx == -1)
+			break;
+
+		/* constraint still honored */
+		if (!test_bit(hwc->idx, c))
+			break;
+
+		/* not already used */
+		if (test_bit(hwc->idx, used_mask))
+			break;
+
+#if 0
+		pr_debug("CPU%d fast config=0x%llx idx=%d assign=%c\n",
+			 smp_processor_id(),
+			 hwc->config,
+			 hwc->idx,
+			 assign ? 'y' : 'n');
+#endif
+
+		set_bit(hwc->idx, used_mask);
+		if (assign)
+			assign[i] = hwc->idx;
+	}
+	if (!num)
+		goto done;
+
+	/*
+	 * begin slow path
+	 */
+
+	bitmap_zero(used_mask, X86_PMC_IDX_MAX);
+
+	/*
 	 * weight = number of possible counters
 	 *
 	 * 1    = most constrained, only works on one counter
@@ -1263,10 +1306,9 @@ static int x86_schedule_events(struct cp
 	if (x86_pmu.num_events_fixed)
 		wmax++;
 
-	num = n;
-	for (w = 1; num && w <= wmax; w++) {
+	for (w = 1, num = n; num && w <= wmax; w++) {
 		/* for each event */
-		for (i = 0; i < n; i++) {
+		for (i = 0; num && i < n; i++) {
 			c = (unsigned long *)constraints[i];
 			hwc = &cpuc->event_list[i]->hw;
 
@@ -1274,28 +1316,6 @@ static int x86_schedule_events(struct cp
 			if (weight != w)
 				continue;
 
-			/*
-			 * try to reuse previous assignment
-			 *
-			 * This is possible despite the fact that
-			 * events or events order may have changed.
-			 *
-			 * What matters is the level of constraints
-			 * of an event and this is constant for now.
-			 *
-			 * This is possible also because we always
-			 * scan from most to least constrained. Thus,
-			 * if a counter can be reused, it means no,
-			 * more constrained events, needed it. And
-			 * next events will either compete for it
-			 * (which cannot be solved anyway) or they
-			 * have fewer constraints, and they can use
-			 * another counter.
-			 */
-			j = hwc->idx;
-			if (j != -1 && !test_bit(j, used_mask))
-				goto skip;
-
 			for_each_bit(j, c, X86_PMC_IDX_MAX) {
 				if (!test_bit(j, used_mask))
 					break;
@@ -1303,22 +1323,23 @@ static int x86_schedule_events(struct cp
 
 			if (j == X86_PMC_IDX_MAX)
 				break;
-skip:
-			set_bit(j, used_mask);
 
 #if 0
-			pr_debug("CPU%d config=0x%llx idx=%d assign=%c\n",
+			pr_debug("CPU%d slow config=0x%llx idx=%d assign=%c\n",
 				smp_processor_id(),
 				hwc->config,
 				j,
 				assign ? 'y' : 'n');
 #endif
 
+			set_bit(j, used_mask);
+
 			if (assign)
 				assign[i] = j;
 			num--;
 		}
 	}
+done:
 	/*
 	 * scheduling failed or is just a simulation,
 	 * free resources if necessary
@@ -1357,7 +1378,7 @@ static int collect_events(struct cpu_hw_
 
 	list_for_each_entry(event, &leader->sibling_list, group_entry) {
 		if (!is_x86_event(event) ||
-		    event->state == PERF_EVENT_STATE_OFF)
+		    event->state <= PERF_EVENT_STATE_OFF)
 			continue;
 
 		if (n >= max_count)
@@ -1369,11 +1390,15 @@ static int collect_events(struct cpu_hw_
 	return n;
 }
 
-
 static inline void x86_assign_hw_event(struct perf_event *event,
-				struct hw_perf_event *hwc, int idx)
+				struct cpu_hw_events *cpuc,
+				int idx)
 {
+	struct hw_perf_event *hwc = &event->hw;
+
 	hwc->idx = idx;
+	hwc->last_cpu = smp_processor_id();
+	hwc->last_tag = ++cpuc->tags[idx];
 
 	if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
 		hwc->config_base = 0;
@@ -1392,6 +1417,14 @@ static inline void x86_assign_hw_event(s
 	}
 }
 
+static bool match_prev_assignment(struct hw_perf_event *hwc,
+				  struct cpu_hw_events *cpuc, int idx)
+{
+	return hwc->idx == cpuc->assign[idx] &&
+	       hwc->last_cpu == smp_processor_id() &&
+	       hwc->last_tag == cpuc->tags[idx];
+}
+
 void hw_perf_enable(void)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
@@ -1401,45 +1434,33 @@ void hw_perf_enable(void)
 
 	if (!x86_pmu_initialized())
 		return;
+
 	if (cpuc->n_added) {
 		/*
 		 * apply assignment obtained either from
 		 * hw_perf_group_sched_in() or x86_pmu_enable()
 		 *
-		 * step1: save events moving to new counters
-		 * step2: reprogram moved events into new counters
+		 * We either re-enable or re-program and re-enable.
+		 * All events are disabled by the time we come here.
+		 * That means their state has been saved already.
 		 */
 		for (i = 0; i < cpuc->n_events; i++) {
-
 			event = cpuc->event_list[i];
 			hwc = &event->hw;
 
-			if (hwc->idx == -1 || hwc->idx == cpuc->assign[i])
-				continue;
-
-			x86_pmu.disable(hwc, hwc->idx);
-
-			clear_bit(hwc->idx, cpuc->active_mask);
-			barrier();
-			cpuc->events[hwc->idx] = NULL;
-
-			x86_perf_event_update(event, hwc, hwc->idx);
-
-			hwc->idx = -1;
-		}
-
-		for (i = 0; i < cpuc->n_events; i++) {
-
-			event = cpuc->event_list[i];
-			hwc = &event->hw;
-
-			if (hwc->idx == -1) {
-				x86_assign_hw_event(event, hwc, cpuc->assign[i]);
+			/*
+			 * we can avoid reprogramming counter if:
+			 * - assigned same counter as last time
+			 * - running on same CPU as last time
+			 * - no other event has used the counter since
+			 */
+			if (!match_prev_assignment(hwc, cpuc, i)) {
+				x86_assign_hw_event(event, cpuc, cpuc->assign[i]);
 				x86_perf_event_set_period(event, hwc, hwc->idx);
 			}
 			/*
 			 * need to mark as active because x86_pmu_disable()
-			 * clear active_mask and eventsp[] yet it preserves
+			 * clear active_mask and events[] yet it preserves
 			 * idx
 			 */
 			set_bit(hwc->idx, cpuc->active_mask);
@@ -2191,6 +2212,8 @@ static void amd_get_event_constraints(st
 				      struct perf_event *event,
 				      u64 *idxmsk)
 {
+	/* no constraints, means supports all generic counters */
+	bitmap_fill((unsigned long *)idxmsk, x86_pmu.num_events);
 }
 
 static int x86_event_sched_in(struct perf_event *event,
@@ -2265,7 +2288,7 @@ int hw_perf_group_sched_in(struct perf_e
 
 	n1 = 1;
 	list_for_each_entry(sub, &leader->sibling_list, group_entry) {
-		if (sub->state != PERF_EVENT_STATE_OFF) {
+		if (sub->state > PERF_EVENT_STATE_OFF) {
 			ret = x86_event_sched_in(sub, cpuctx, cpu);
 			if (ret)
 				goto undo;
@@ -2620,12 +2643,23 @@ static int validate_group(struct perf_ev
 
 const struct pmu *hw_perf_event_init(struct perf_event *event)
 {
+	const struct pmu *tmp;
 	int err;
 
 	err = __hw_perf_event_init(event);
 	if (!err) {
+		/*
+		 * we temporarily connect event to its pmu
+		 * such that validate_group() can classify
+		 * it as an x86 event using is_x86_event()
+		 */
+		tmp = event->pmu;
+		event->pmu = &pmu;
+
 		if (event->group_leader != event)
 			err = validate_group(event);
+
+		event->pmu = tmp;
 	}
 	if (err) {
 		if (event->destroy)
Index: linux-2.6/include/linux/perf_event.h
===================================================================
--- linux-2.6.orig/include/linux/perf_event.h
+++ linux-2.6/include/linux/perf_event.h
@@ -478,9 +478,11 @@ struct hw_perf_event {
 	union {
 		struct { /* hardware */
 			u64		config;
+			u64		last_tag;
 			unsigned long	config_base;
 			unsigned long	event_base;
 			int		idx;
+			int		last_cpu;
 		};
 		struct { /* software */
 			s64		remaining;

-- 


  parent reply	other threads:[~2010-01-22 15:59 UTC|newest]

Thread overview: 126+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-22 15:50 [PATCH 00/10] perf/x86 queue Peter Zijlstra
2010-01-22 15:50 ` [PATCH 01/10] perf_events: improve x86 event scheduling (v5) Peter Zijlstra
2010-01-22 15:50 ` Peter Zijlstra [this message]
2010-01-22 15:50 ` [PATCH 03/10] perf_event: x86: Allocate the fake_cpuc Peter Zijlstra
2010-01-29  9:27   ` [tip:perf/core] " tip-bot for Peter Zijlstra
2010-01-22 15:50 ` [PATCH 04/10] perf_event: x86: Fixup weight tying issue Peter Zijlstra
2010-01-29  9:27   ` [tip:perf/core] perf_event: x86: Fixup constraints typing issue tip-bot for Peter Zijlstra
2010-01-22 15:50 ` [PATCH 05/10] perf_event: x86: Clean up some of the u64/long bitmask casting Peter Zijlstra
2010-01-29  9:27   ` [tip:perf/core] " tip-bot for Peter Zijlstra
2010-01-22 15:50 ` [PATCH 06/10] perf_event: x86: Reduce some overly long lines with some MACROs Peter Zijlstra
2010-01-29  9:27   ` [tip:perf/core] " tip-bot for Peter Zijlstra
2010-01-22 15:50 ` [PATCH 07/10] bitops: Provide compile time HWEIGHT{8,16,32,64} Peter Zijlstra
2010-01-29  9:28   ` [tip:perf/core] " tip-bot for Peter Zijlstra
2010-01-29 10:01     ` Andrew Morton
2010-01-29 10:04       ` Ingo Molnar
2010-01-29 10:13         ` Andrew Morton
2010-01-29 11:03       ` Peter Zijlstra
2010-01-29 16:24         ` Linus Torvalds
2010-01-29 22:50         ` H. Peter Anvin
2010-01-30 16:28           ` Peter Zijlstra
2010-02-01 12:43             ` Peter Zijlstra
2010-02-01 19:06               ` H. Peter Anvin
2010-04-06 23:03               ` [tip:core/hweight] bitops: Optimize hweight() by making use of compile-time evaluation tip-bot for Peter Zijlstra
2010-01-29 10:32   ` [PATCH 07/10] bitops: Provide compile time HWEIGHT{8,16,32,64} John Kacur
2010-01-29 11:05     ` Peter Zijlstra
2010-01-29 11:13       ` John Kacur
2010-01-30  0:09       ` H. Peter Anvin
2010-01-30  7:34       ` Ingo Molnar
2010-01-22 15:50 ` [PATCH 08/10] perf_event: Optimize the constraint searching bits Peter Zijlstra
2010-01-22 16:08   ` Stephane Eranian
2010-01-22 16:22     ` Peter Zijlstra
2010-01-22 16:28       ` Stephane Eranian
2010-01-29  9:28   ` [tip:perf/core] perf_event: x86: " tip-bot for Peter Zijlstra
2010-01-22 15:50 ` [PATCH 09/10] perf_event: x86: Optimize constraint weight computation Peter Zijlstra
2010-01-29  9:28   ` [tip:perf/core] " tip-bot for Peter Zijlstra
2010-01-22 15:50 ` [PATCH 10/10] perf_event: Optimize the fast path a little more Peter Zijlstra
2010-01-29  9:28   ` [tip:perf/core] perf_event: x86: " tip-bot for Peter Zijlstra
  -- strict thread matches above, loose matches on Subject: below --
2010-01-30  9:45 [PATCH 0/5] [RESEND] FMODE_NONOTIFY and FMODE_NEG_OFFSET bits Wu Fengguang
2010-01-30  9:45 ` [PATCH 1/5] fanotify: fix FMODE_NONOTIFY bit number Wu Fengguang
2010-02-01 20:44   ` Andrew Morton
2010-01-30  9:45 ` [PATCH 2/5] bitops: compile time optimization for hweight_long(CONSTANT) Wu Fengguang
2010-02-01 20:48   ` Andrew Morton
2010-02-03 13:39     ` Wu Fengguang
2010-02-03 15:08       ` Andrew Morton
2010-02-03 15:15         ` Peter Zijlstra
2010-02-03 15:42           ` Andrew Morton
2010-02-03 15:47             ` Peter Zijlstra
2010-02-03 17:11               ` H. Peter Anvin
2010-02-03 18:14             ` Borislav Petkov
2010-02-03 18:47               ` Peter Zijlstra
2010-02-03 19:49                 ` H. Peter Anvin
2010-02-04 15:10                   ` Borislav Petkov
2010-02-04 15:13                     ` Peter Zijlstra
2010-02-04 15:54                       ` Borislav Petkov
2010-02-04 16:04                         ` Peter Zijlstra
2010-02-05 12:11                           ` Borislav Petkov
2010-02-05 12:14                             ` Peter Zijlstra
2010-02-05 21:54                             ` H. Peter Anvin
2010-02-06  9:36                               ` Borislav Petkov
2010-02-07  1:55                                 ` H. Peter Anvin
2010-02-08  9:28                                   ` Borislav Petkov
2010-02-08  9:35                                     ` H. Peter Anvin
2010-02-08  9:35                                       ` H. Peter Anvin
2010-02-08  9:59                                       ` Borislav Petkov
2010-02-11 17:24                                         ` Borislav Petkov
2010-02-11 17:33                                           ` H. Peter Anvin
2010-02-12 17:06                                             ` Borislav Petkov
2010-02-12 17:28                                               ` H. Peter Anvin
2010-02-12 17:47                                                 ` Borislav Petkov
2010-02-12 19:05                                                   ` H. Peter Anvin
2010-02-17 13:57                                                     ` Michal Marek
2010-02-17 17:20                                                       ` Borislav Petkov
2010-02-17 17:31                                                         ` Michal Marek
2010-02-17 17:34                                                           ` Borislav Petkov
2010-02-17 17:39                                                           ` Michal Marek
2010-02-18  6:19                                                             ` Borislav Petkov
2010-02-19 14:22                                                               ` [PATCH] x86: Add optimized popcnt variants Borislav Petkov
2010-02-19 16:06                                                                 ` H. Peter Anvin
2010-02-19 16:45                                                                   ` Borislav Petkov
2010-02-19 16:53                                                                     ` H. Peter Anvin
2010-02-22 14:17                                                                       ` Borislav Petkov
2010-02-22 17:21                                                                         ` H. Peter Anvin
2010-02-22 18:49                                                                           ` Borislav Petkov
2010-02-22 19:55                                                                             ` H. Peter Anvin
2010-02-23  6:37                                                                               ` Borislav Petkov
2010-02-23 15:58                                                                               ` Borislav Petkov
2010-02-23 17:34                                                                                 ` H. Peter Anvin
2010-02-23 17:54                                                                                   ` Borislav Petkov
2010-02-23 17:54                                                                                     ` Borislav Petkov
2010-02-23 18:17                                                                                     ` H. Peter Anvin
2010-02-23 19:06                                                                                       ` Borislav Petkov
2010-02-26  5:27                                                                                         ` H. Peter Anvin
2010-02-26  7:47                                                                                           ` Borislav Petkov
2010-02-26 17:48                                                                                             ` H. Peter Anvin
2010-02-26 17:48                                                                                               ` H. Peter Anvin
2010-02-27  8:28                                                                                               ` Borislav Petkov
2010-02-27 20:00                                                                                                 ` H. Peter Anvin
2010-03-09 15:36                                                                                                   ` Borislav Petkov
2010-03-09 15:50                                                                                                     ` Peter Zijlstra
2010-03-09 16:23                                                                                                       ` Borislav Petkov
2010-03-09 16:32                                                                                                         ` Peter Zijlstra
2010-03-09 17:32                                                                                                           ` Borislav Petkov
2010-03-09 17:37                                                                                                             ` Peter Zijlstra
2010-03-18 11:17                                                                                                   ` Borislav Petkov
2010-03-18 11:19                                                                                                   ` [PATCH 1/2] bitops: Optimize hweight() by making use of compile-time evaluation Borislav Petkov
2010-03-18 11:20                                                                                                   ` [PATCH 2/2] x86: Add optimized popcnt variants Borislav Petkov
2010-04-06 23:04                                                                                                     ` [tip:core/hweight] " tip-bot for Borislav Petkov
2010-04-07  7:02                                                                                                       ` Borislav Petkov
2010-02-18 10:51                                                       ` [PATCH 2/5] bitops: compile time optimization for hweight_long(CONSTANT) Peter Zijlstra
2010-02-18 11:51                                                         ` Borislav Petkov
2010-02-14 10:12                                           ` Peter Zijlstra
2010-02-14 11:24                                             ` Borislav Petkov
2010-02-14 12:23                                               ` Peter Zijlstra
2010-02-14 14:19                                                 ` Borislav Petkov
2010-02-14 18:36                                               ` H. Peter Anvin
2010-02-14 18:36                                                 ` H. Peter Anvin
2010-02-14 20:28                                                 ` Borislav Petkov
2010-02-14 22:13                                                   ` H. Peter Anvin
2010-02-14 22:13                                                   ` H. Peter Anvin
2010-02-04 15:16                     ` H. Peter Anvin
2010-02-04 15:39                     ` Brian Gerst
2010-02-04 15:39                       ` Brian Gerst
2010-02-03 17:10       ` H. Peter Anvin
2010-01-30  9:45 ` [PATCH 3/5] vfs: O_* bit numbers uniqueness check Wu Fengguang
2010-01-30  9:45 ` [PATCH 4/5] vfs: introduce FMODE_NEG_OFFSET for allowing negative f_pos Wu Fengguang
2010-01-30  9:45 ` [PATCH 5/5] devmem: dont allow seek to last page Wu Fengguang

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=20100122155535.318710047@chello.nl \
    --to=a.p.zijlstra@chello.nl \
    --cc=davem@davemloft.net \
    --cc=eranian@google.com \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=paulus@samba.org \
    --cc=robert.richter@amd.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.