Skip to contentSkip to conversation

perf_counter: powerpc: allow use of limited-function counters

PATCH Applied Paul Mackerras 2 participants 5 messages Apr 29, 2009 – Apr 30, 2009 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf_counter: powerpc: allow use of limited-function counters

POWER5+ and POWER6 have two hardware counters with limited functionality:
PMC5 counts instructions completed in run state and PMC6 counts cycles in run state. (Run state is the state when a hardware RUN bit is 1; the idle task clears RUN while waiting for work to do and sets it when there is work to do.)

These counters can't be written to by the kernel, can't generate interrupts, and don't obey the freeze conditions. That means we can only use them for per-task counters (where we know we'll always be in run state; we can't put a per-task counter on an idle task), and only if we don't want interrupts and we do want to count in all processor modes.

Obviously some counters can't go on a limited hardware counter, but there are also situations where we can only put a counter on a limited hardware counter - if there are already counters on that exclude some processor modes and we want to put on a per-task cycle or instruction counter that doesn't exclude any processor mode, it could go on if it can use a limited hardware counter.

To keep track of these constraints, this adds a flags argument to the processor-specific get_alternatives() functions, with three bits defined:
one to say that we can accept alternative event codes that go on limited counters, one to say we only want alternatives on limited counters, and one to say that this is a per-task counter and therefore events that are gated by run state are equivalent to those that aren't (e.g. a "cycles" event is equivalent to a "cycles in run state" event). These flags are computed for each counter and stored in the counter->hw.counter_base field (slightly wonky name for what it does, but it was an existing unused field).

Since the limited counters don't freeze when we freeze the other counters, we need some special handling to avoid getting skew between things counted on the limited counters and those counted on normal counters. To minimize this skew, if we are using any limited counters, we read PMC5 and PMC6 immediately after setting and clearing the freeze bit. This is done in a single asm in the new write_mmcr0() function.

The code here is specific to PMC5 and PMC6 being the limited hardware counters. Being more general (e.g. having a bitmap of limited hardware counter numbers) would have meant more complex code to read the limited counters when freezing and unfreezing the normal counters, with conditional branches, which would have increased the skew. Since it isn't necessary for the code to be more general at this stage, it isn't.

This also extends the back-ends for POWER5+ and POWER6 to be able to handle up to 6 counters rather than the 4 they previously handled.

---
 arch/powerpc/include/asm/perf_counter.h |   13 ++-
 arch/powerpc/kernel/perf_counter.c      |  297 +++++++++++++++++++++++++++----
 arch/powerpc/kernel/power4-pmu.c        |    3 +-
 arch/powerpc/kernel/power5+-pmu.c       |  117 ++++++++++--
 arch/powerpc/kernel/power5-pmu.c        |    3 +-
 arch/powerpc/kernel/power6-pmu.c        |  119 +++++++++++--
 arch/powerpc/kernel/ppc970-pmu.c        |    3 +-
 7 files changed, 479 insertions(+), 76 deletions(-)
arch/powerpc/include/asm/perf_counter.h +12 -1
index 9d7ff6d..56d66c3 100644
@@ -12,6 +12,7 @@
 
 #define MAX_HWCOUNTERS		8
 #define MAX_EVENT_ALTERNATIVES	8
+#define MAX_LIMITED_HWCOUNTERS	2
 
 /*
  * This struct provides the constants and functions needed to
@@ -25,8 +26,11 @@ struct power_pmu {
 	int	(*compute_mmcr)(unsigned int events[], int n_ev,
 				unsigned int hwc[], u64 mmcr[]);
 	int	(*get_constraint)(unsigned int event, u64 *mskp, u64 *valp);
-	int	(*get_alternatives)(unsigned int event, unsigned int alt[]);
+	int	(*get_alternatives)(unsigned int event, unsigned int flags,
+				    unsigned int alt[]);
 	void	(*disable_pmc)(unsigned int pmc, u64 mmcr[]);
+	int	(*limited_pmc_event)(unsigned int event);
+	int	limited_pmc5_6;	/* PMC5 and PMC6 have limited function */
 	int	n_generic;
 	int	*generic_events;
 };
@@ -34,6 +38,13 @@ struct power_pmu {
 extern struct power_pmu *ppmu;
 
 /*
+ * Values for flags to get_alternatives()
+ */
+#define PPMU_LIMITED_PMC_OK	1	/* can put this on a limited PMC */
+#define PPMU_LIMITED_PMC_REQD	2	/* have to put this on a limited PMC */
+#define PPMU_ONLY_COUNT_RUN	4	/* only counting in run state */
+
+/*
  * The power_pmu.get_constraint function returns a 64-bit value and
  * a 64-bit mask that express the constraints between this event and
  * other events.
arch/powerpc/kernel/perf_counter.c +262 -35

486 lines not on this page · raw message

arch/powerpc/kernel/power4-pmu.c +2 -1
index 1407b19..744a275 100644
@@ -320,7 +320,8 @@ static unsigned int ppc_inst_cmpl[] = {
 	0x1001, 0x4001, 0x6001, 0x7001, 0x8001
 };
 
-static int p4_get_alternatives(unsigned int event, unsigned int alt[])
+static int p4_get_alternatives(unsigned int event, unsigned int flags,
+			       unsigned int alt[])
 {
 	int i, j, na;
 
arch/powerpc/kernel/power5+-pmu.c +98 -19

219 lines not on this page · raw message

arch/powerpc/kernel/power5-pmu.c +2 -1
index 116c4bb..6e667dc 100644
@@ -269,7 +269,8 @@ static int find_alternative_bdecode(unsigned int event)
 	return -1;
 }
 
-static int power5_get_alternatives(unsigned int event, unsigned int alt[])
+static int power5_get_alternatives(unsigned int event, unsigned int flags,
+				   unsigned int alt[])
 {
 	int i, j, ae, nalt = 1;
 
arch/powerpc/kernel/power6-pmu.c +101 -18

212 lines not on this page · raw message

arch/powerpc/kernel/ppc970-pmu.c +2 -1
index aed8ccd..af2d188 100644
@@ -243,7 +243,8 @@ static int p970_get_constraint(unsigned int event, u64 *maskp, u64 *valp)
 	return 0;
 }
 
-static int p970_get_alternatives(unsigned int event, unsigned int alt[])
+static int p970_get_alternatives(unsigned int event, unsigned int flags,
+				 unsigned int alt[])
 {
 	alt[0] = event;
 
Signature · 1 line
1.6.0.4
Applied
#1 lore raw
Quoted 53 lines · Paul Mackerras

POWER5+ and POWER6 have two hardware counters with limited
functionality: PMC5 counts instructions completed in run state and PMC6 counts cycles in run state. (Run state is the state when a hardware RUN bit is 1; the idle task clears RUN while waiting for work to do and sets it when there is work to do.)

These counters can't be written to by the kernel, can't generate interrupts, and don't obey the freeze conditions. That means we can only use them for per-task counters (where we know we'll always be in run state; we can't put a per-task counter on an idle task), and only if we don't want interrupts and we do want to count in all processor modes.

Obviously some counters can't go on a limited hardware counter, but there are also situations where we can only put a counter on a limited hardware counter - if there are already counters on that exclude some processor modes and we want to put on a per-task cycle or instruction counter that doesn't exclude any processor mode, it could go on if it can use a limited hardware counter.

To keep track of these constraints, this adds a flags argument to the processor-specific get_alternatives() functions, with three bits defined: one to say that we can accept alternative event codes that go on limited counters, one to say we only want alternatives on limited counters, and one to say that this is a per-task counter and therefore events that are gated by run state are equivalent to those that aren't (e.g. a "cycles" event is equivalent to a "cycles in run state" event). These flags are computed for each counter and stored in the counter->hw.counter_base field (slightly wonky name for what it does, but it was an existing unused field).

Since the limited counters don't freeze when we freeze the other counters, we need some special handling to avoid getting skew between things counted on the limited counters and those counted on normal counters. To minimize this skew, if we are using any limited counters, we read PMC5 and PMC6 immediately after setting and clearing the freeze bit. This is done in a single asm in the new write_mmcr0() function.

The code here is specific to PMC5 and PMC6 being the limited hardware counters. Being more general (e.g. having a bitmap of limited hardware counter numbers) would have meant more complex code to read the limited counters when freezing and unfreezing the normal counters, with conditional branches, which would have increased the skew. Since it isn't necessary for the code to be more general at this stage, it isn't.

This also extends the back-ends for POWER5+ and POWER6 to be able to handle up to 6 counters rather than the 4 they previously handled.

Quoted 9 lines
---
 arch/powerpc/include/asm/perf_counter.h |   13 ++-
 arch/powerpc/kernel/perf_counter.c      |  297 +++++++++++++++++++++++++++----
 arch/powerpc/kernel/power4-pmu.c        |    3 +-
 arch/powerpc/kernel/power5+-pmu.c       |  117 ++++++++++--
 arch/powerpc/kernel/power5-pmu.c        |    3 +-
 arch/powerpc/kernel/power6-pmu.c        |  119 +++++++++++--
 arch/powerpc/kernel/ppc970-pmu.c        |    3 +-
 7 files changed, 479 insertions(+), 76 deletions(-)

Applied, thanks Paul!

[ Note, i saw some fuzz in those files. It applied fine but maybe
you have some local changes in them that are not in tip:perfcounters/core yet? You might want to double check that. ]

Ingo

#2 lore raw
Quoted 3 lines · Ingo Molnar

[ Note, i saw some fuzz in those files. It applied fine but maybe
you have some local changes in them that are not in tip:perfcounters/core yet? You might want to double check that. ]

I think it was just Robert Richter's changes that caused the fuzz. The end result seems to be OK.

Paul.

#3 lore raw
Quoted 8 lines · Paul Mackerras
Quoted 3 lines · Ingo Molnar

[ Note, i saw some fuzz in those files. It applied fine but maybe
you have some local changes in them that are not in tip:perfcounters/core yet? You might want to double check that. ]

I think it was just Robert Richter's changes that caused the fuzz. The end result seems to be OK.

ah, indeed - the rename:

4aeb0b4: perfcounters: rename struct hw_perf_counter_ops into struct pmu

changed context lines in your patch causing the fuzz. The mechanic rename changed quite a bit of stuff in the powerpc driver - we were lucky that your rather large patch still merged cleanly.

Btw, if in the future such renames cause problems for any pending patch of yours we can always pick up your patches first and then re-do the rename.

Ingo

#4 lore raw
In reply to Paul Mackerras
Commit-ID:  ab7ef2e50a557af92f4f90689f51fadadafc16b2
Gitweb:     http://git.kernel.org/tip/ab7ef2e50a557af92f4f90689f51fadadafc16b2
Author:     Paul Mackerras <[email protected]>
AuthorDate: Wed, 29 Apr 2009 22:38:51 +1000
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 29 Apr 2009 14:58:35 +0200

perf_counter: powerpc: allow use of limited-function counters

POWER5+ and POWER6 have two hardware counters with limited functionality:
PMC5 counts instructions completed in run state and PMC6 counts cycles in run state. (Run state is the state when a hardware RUN bit is 1; the idle task clears RUN while waiting for work to do and sets it when there is work to do.)

These counters can't be written to by the kernel, can't generate interrupts, and don't obey the freeze conditions. That means we can only use them for per-task counters (where we know we'll always be in run state; we can't put a per-task counter on an idle task), and only if we don't want interrupts and we do want to count in all processor modes.

Obviously some counters can't go on a limited hardware counter, but there are also situations where we can only put a counter on a limited hardware counter - if there are already counters on that exclude some processor modes and we want to put on a per-task cycle or instruction counter that doesn't exclude any processor mode, it could go on if it can use a limited hardware counter.

To keep track of these constraints, this adds a flags argument to the processor-specific get_alternatives() functions, with three bits defined:
one to say that we can accept alternative event codes that go on limited counters, one to say we only want alternatives on limited counters, and one to say that this is a per-task counter and therefore events that are gated by run state are equivalent to those that aren't (e.g. a "cycles" event is equivalent to a "cycles in run state" event). These flags are computed for each counter and stored in the counter->hw.counter_base field (slightly wonky name for what it does, but it was an existing unused field).

Since the limited counters don't freeze when we freeze the other counters, we need some special handling to avoid getting skew between things counted on the limited counters and those counted on normal counters. To minimize this skew, if we are using any limited counters, we read PMC5 and PMC6 immediately after setting and clearing the freeze bit. This is done in a single asm in the new write_mmcr0() function.

The code here is specific to PMC5 and PMC6 being the limited hardware counters. Being more general (e.g. having a bitmap of limited hardware counter numbers) would have meant more complex code to read the limited counters when freezing and unfreezing the normal counters, with conditional branches, which would have increased the skew. Since it isn't necessary for the code to be more general at this stage, it isn't.

This also extends the back-ends for POWER5+ and POWER6 to be able to handle up to 6 counters rather than the 4 they previously handled.

LKML-Reference: <[email protected]>

---
 arch/powerpc/include/asm/perf_counter.h |   13 ++-
 arch/powerpc/kernel/perf_counter.c      |  297 +++++++++++++++++++++++++++----
 arch/powerpc/kernel/power4-pmu.c        |    3 +-
 arch/powerpc/kernel/power5+-pmu.c       |  117 ++++++++++--
 arch/powerpc/kernel/power5-pmu.c        |    3 +-
 arch/powerpc/kernel/power6-pmu.c        |  119 +++++++++++--
 arch/powerpc/kernel/ppc970-pmu.c        |    3 +-
 7 files changed, 479 insertions(+), 76 deletions(-)
arch/powerpc/include/asm/perf_counter.h +12 -1
index 9d7ff6d..56d66c3 100644
@@ -12,6 +12,7 @@
 
 #define MAX_HWCOUNTERS		8
 #define MAX_EVENT_ALTERNATIVES	8
+#define MAX_LIMITED_HWCOUNTERS	2
 
 /*
  * This struct provides the constants and functions needed to
@@ -25,8 +26,11 @@ struct power_pmu {
 	int	(*compute_mmcr)(unsigned int events[], int n_ev,
 				unsigned int hwc[], u64 mmcr[]);
 	int	(*get_constraint)(unsigned int event, u64 *mskp, u64 *valp);
-	int	(*get_alternatives)(unsigned int event, unsigned int alt[]);
+	int	(*get_alternatives)(unsigned int event, unsigned int flags,
+				    unsigned int alt[]);
 	void	(*disable_pmc)(unsigned int pmc, u64 mmcr[]);
+	int	(*limited_pmc_event)(unsigned int event);
+	int	limited_pmc5_6;	/* PMC5 and PMC6 have limited function */
 	int	n_generic;
 	int	*generic_events;
 };
@@ -34,6 +38,13 @@ struct power_pmu {
 extern struct power_pmu *ppmu;
 
 /*
+ * Values for flags to get_alternatives()
+ */
+#define PPMU_LIMITED_PMC_OK	1	/* can put this on a limited PMC */
+#define PPMU_LIMITED_PMC_REQD	2	/* have to put this on a limited PMC */
+#define PPMU_ONLY_COUNT_RUN	4	/* only counting in run state */
+
+/*
  * The power_pmu.get_constraint function returns a 64-bit value and
  * a 64-bit mask that express the constraints between this event and
  * other events.
arch/powerpc/kernel/perf_counter.c +262 -35

486 lines not on this page · raw message

arch/powerpc/kernel/power4-pmu.c +2 -1
index 1407b19..744a275 100644
@@ -320,7 +320,8 @@ static unsigned int ppc_inst_cmpl[] = {
 	0x1001, 0x4001, 0x6001, 0x7001, 0x8001
 };
 
-static int p4_get_alternatives(unsigned int event, unsigned int alt[])
+static int p4_get_alternatives(unsigned int event, unsigned int flags,
+			       unsigned int alt[])
 {
 	int i, j, na;
 
arch/powerpc/kernel/power5+-pmu.c +98 -19

219 lines not on this page · raw message

arch/powerpc/kernel/power5-pmu.c +2 -1
index 116c4bb..6e667dc 100644
@@ -269,7 +269,8 @@ static int find_alternative_bdecode(unsigned int event)
 	return -1;
 }
 
-static int power5_get_alternatives(unsigned int event, unsigned int alt[])
+static int power5_get_alternatives(unsigned int event, unsigned int flags,
+				   unsigned int alt[])
 {
 	int i, j, ae, nalt = 1;
 
arch/powerpc/kernel/power6-pmu.c +101 -18

212 lines not on this page · raw message

arch/powerpc/kernel/ppc970-pmu.c +2 -1
index aed8ccd..af2d188 100644
@@ -243,7 +243,8 @@ static int p970_get_constraint(unsigned int event, u64 *maskp, u64 *valp)
 	return 0;
 }
 
-static int p970_get_alternatives(unsigned int event, unsigned int alt[])
+static int p970_get_alternatives(unsigned int event, unsigned int flags,
+				 unsigned int alt[])
 {
 	alt[0] = event;