Skip to contentSkip to conversation

perf events for ARMv6

PATCH New Jamie Iles 2 participants 6 messages Dec 11, 2009

Subsystems (4)
lore
Share & tools
lore

Jamie Iles

author
#0 lore raw

This patch series provides support for software perf events on all ARM platforms and hardware support for v6 CPUs. In addition to adding support for the hardware counters, these patches provide a way of arbitrating access to the PMU and patches the oprofile code to use this.

Jamie

author
#1 lore raw

PATCH 1/4 arm: provide a mechanism to reserve performance counters

To add support for perf events and to allow the hardware counters to be shared with oprofile, we need a way to reserve access to the pmu (performance monitor unit).

---
 arch/arm/include/asm/pmu.h |   54 ++++++++++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/Makefile   |    1 +
 arch/arm/kernel/pmu.c      |   32 ++++++++++++++++++++++++++
 arch/arm/mm/Kconfig        |    4 +++
 4 files changed, 91 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/include/asm/pmu.h
 create mode 100644 arch/arm/kernel/pmu.c
arch/arm/include/asm/pmu.h new file +54 -0
new file mode 100644
index 0000000..94d6c67
@@ -0,0 +1,54 @@
+/*
+ *  linux/arch/arm/include/asm/pmu.h
+ *
+ *  Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#ifndef __ARM_PMU_H__
+#define __ARM_PMU_H__
+
+#ifdef CONFIG_CPU_HAS_PMU
+
+/**
+ * reserve_pmu() - Reserve the hardware performance counters
+ *
+ * Reserve the hardware performance counters in the system for exclusive use.
+ * Note: success here does not guarantee exclusive access - callers must
+ * respect the return value.
+ *
+ * Returns zero on success, negative error on failure.
+ */
+extern int
+reserve_pmu(void);
+
+/**
+ * release_pmu() - Relinquish control of the performance counters
+ *
+ * Release the performance counters and allow someone else to use them.
+ * Callers must have disabled the counters and released IRQs before calling
+ * this.
+ */
+extern void
+release_pmu(void);
+
+#else /* CONFIG_CPU_HAS_PMU */
+
+static inline int
+reserve_pmu(void)
+{
+	return -ENODEV;
+}
+
+static inline void
+release_pmu(void)
+{
+}
+
+#endif /* CONFIG_CPU_HAS_PMU */
+
+#endif /* __ARM_PMU_H__ */
arch/arm/kernel/Makefile +1 -0
index e7ccf7e..286a276 100644
@@ -46,6 +46,7 @@ obj-$(CONFIG_CPU_XSCALE)	+= xscale-cp0.o
 obj-$(CONFIG_CPU_XSC3)		+= xscale-cp0.o
 obj-$(CONFIG_CPU_MOHAWK)	+= xscale-cp0.o
 obj-$(CONFIG_IWMMXT)		+= iwmmxt.o
+obj-$(CONFIG_CPU_HAS_PMU)	+= pmu.o
 AFLAGS_iwmmxt.o			:= -Wa,-mcpu=iwmmxt
 
 ifneq ($(CONFIG_ARCH_EBSA110),y)
arch/arm/kernel/pmu.c new file +32 -0
new file mode 100644
index 0000000..a2b1a26
@@ -0,0 +1,32 @@
+/*
+ *  linux/arch/arm/kernel/pmu.c
+ *
+ *  Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/semaphore.h>
+
+#include <asm/pmu.h>
+
+static DECLARE_MUTEX(pmu_mutex);
+
+int
+reserve_pmu(void)
+{
+	return down_trylock(&pmu_mutex) ? -EBUSY : 0;
+}
+EXPORT_SYMBOL_GPL(reserve_pmu);
+
+void
+release_pmu(void)
+{
+	up(&pmu_mutex);
+}
+EXPORT_SYMBOL_GPL(release_pmu);
arch/arm/mm/Kconfig +4 -0
index dd4698c..60edbfe 100644
@@ -398,6 +398,7 @@ config CPU_V6
 	select CPU_HAS_ASID if MMU
 	select CPU_COPY_V6 if MMU
 	select CPU_TLB_V6 if MMU
+	select CPU_HAS_PMU
 
 # ARMv6k
 config CPU_32v6K
@@ -536,6 +537,9 @@ config CPU_COPY_FA
 config CPU_COPY_V6
 	bool
 
+config CPU_HAS_PMU
+	bool
+
 # This selects the TLB model
 config CPU_TLB_V3
 	bool
Signature · 1 line
1.6.5.4
author
#2 lore raw

PATCH 2/4 arm/oprofile: reserve the PMU when starting

Make sure that we have access to the performance counters and that they aren't being used by perf events or anything else.

---
 arch/arm/oprofile/op_model_mpcore.c |    9 ++++++++-
 arch/arm/oprofile/op_model_v6.c     |    8 +++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)
arch/arm/oprofile/op_model_mpcore.c +8 -1
index 4ce0f98..b14e2d9 100644
@@ -43,6 +43,7 @@
 #include <mach/hardware.h>
 #include <mach/board-eb.h>
 #include <asm/system.h>
+#include <asm/pmu.h>
 
 #include "op_counter.h"
 #include "op_arm_model.h"
@@ -234,7 +235,10 @@ static int arm11_irqs[] = {
 
 static int em_start(void)
 {
-	int ret;
+	int ret = reserve_pmu();
+
+	if (ret)
+		goto out;
 
 	ret = arm11_request_interrupts(arm11_irqs, ARRAY_SIZE(arm11_irqs));
 	if (ret == 0) {
@@ -244,6 +248,8 @@ static int em_start(void)
 		if (ret)
 			arm11_release_interrupts(arm11_irqs, ARRAY_SIZE(arm11_irqs));
 	}
+
+out:
 	return ret;
 }
 
@@ -252,6 +258,7 @@ static void em_stop(void)
 	em_call_function(arm11_stop_pmu);
 	arm11_release_interrupts(arm11_irqs, ARRAY_SIZE(arm11_irqs));
 	scu_stop();
+	release_pmu();
 }
 
 /*
arch/arm/oprofile/op_model_v6.c +7 -1
index e468017..e37720a 100644
@@ -24,6 +24,7 @@
 #include <linux/interrupt.h>
 #include <asm/irq.h>
 #include <asm/system.h>
+#include <asm/pmu.h>
 
 #include "op_counter.h"
 #include "op_arm_model.h"
@@ -45,16 +46,21 @@ static void armv6_pmu_stop(void)
 {
 	arm11_stop_pmu();
 	arm11_release_interrupts(irqs, ARRAY_SIZE(irqs));
+	release_pmu();
 }
 
 static int armv6_pmu_start(void)
 {
-	int ret;
+	int ret = reserve_pmu();
+
+	if (ret)
+		goto out;
 
 	ret = arm11_request_interrupts(irqs, ARRAY_SIZE(irqs));
 	if (ret >= 0)
 		ret = arm11_start_pmu();
 
+out:
 	return ret;
 }
 
Signature · 1 line
1.6.5.4
author
#3 lore raw

PATCH 3/4 arm: enable support for software perf events

The perf events subsystem allows counting of both hardware and software events. This patch implements the bare minimum for software performance events.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 arch/arm/Kconfig                  |    3 ++
 arch/arm/include/asm/atomic.h     |    1 +
 arch/arm/include/asm/perf_event.h |   38 +++++++++++++++++++++++++++++++++++++
 arch/arm/mm/fault.c               |    7 ++++++
 4 files changed, 49 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/include/asm/perf_event.h
arch/arm/Kconfig +3 -0
index c40efec..1e35f21 100644
@@ -18,6 +18,9 @@ config ARM
 	select HAVE_KRETPROBES if (HAVE_KPROBES)
 	select HAVE_FUNCTION_TRACER if (!XIP_KERNEL)
 	select HAVE_GENERIC_DMA_COHERENT
+	select HAVE_PERF_EVENTS
+	select GENERIC_ATOMIC64
+	select PERF_USE_VMALLOC
 	help
 	  The ARM series is a line of low-power-consumption RISC chip designs
 	  licensed by ARM Ltd and targeted at embedded applications and
arch/arm/include/asm/atomic.h +1 -0
index d0daeab..5861cc4 100644
@@ -14,6 +14,7 @@
 #include <linux/compiler.h>
 #include <linux/types.h>
 #include <asm/system.h>
+#include <asm-generic/atomic64.h>
 
 #define ATOMIC_INIT(i)	{ (i) }
 
arch/arm/include/asm/perf_event.h new file +38 -0
new file mode 100644
index 0000000..32a66ac
@@ -0,0 +1,38 @@
+/*
+ *  linux/arch/arm/include/asm/perf_event.h
+ *
+ *  Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#ifndef __ARM_PERF_EVENT_H__
+#define __ARM_PERF_EVENT_H__
+
+/*
+ * NOP: on *most* (read: all supported) ARM platforms, the performance
+ * counter interrupts are regular interrupts and not an NMI. This
+ * means that when we receive the interrupt we can call
+ * perf_event_do_pending() that handles all of the work with
+ * interrupts enabled.
+ */
+static inline void
+set_perf_event_pending(void)
+{
+}
+
+/* Get the PC. Make sure that we have a 64bit value with the upper 32 cleared.
+ */
+#define perf_instruction_pointer(_regs) \
+	((u64)instruction_pointer(regs) & 0xFFFFFFFFLU)
+#define perf_misc_flags(regs)   (user_mode(regs) ? PERF_RECORD_MISC_USER : \
+                                 PERF_RECORD_MISC_KERNEL)
+
+/* ARM performance counters start from 1 (in the cp15 accesses) so use the
+ * same indexes here for consistency. */
+#define PERF_EVENT_INDEX_OFFSET 1
+
+#endif /* __ARM_PERF_EVENT_H__ */
arch/arm/mm/fault.c +7 -0
index 10e0680..9d40c34 100644
@@ -18,6 +18,7 @@
 #include <linux/page-flags.h>
 #include <linux/sched.h>
 #include <linux/highmem.h>
+#include <linux/perf_event.h>
 
 #include <asm/system.h>
 #include <asm/pgtable.h>
@@ -302,6 +303,12 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
 	fault = __do_page_fault(mm, addr, fsr, tsk);
 	up_read(&mm->mmap_sem);
 
+	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, addr);
+	if (fault & VM_FAULT_MAJOR)
+		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0, regs, addr);
+	else if (fault & VM_FAULT_MINOR)
+		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0, regs, addr);
+
 	/*
 	 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
 	 */
Signature · 1 line
1.6.5.4
author
#4 lore raw

PATCH 4/4 arm/perfevents: implement perf event support for ARMv6

This patch implements support for ARMv6 performance counters in the Linux performance events subsystem. ARMv6 architectures that have the performance counters should enable ARMV6_PERF_EVENTS and define the interrupts for the counters in arch/arm/kernel/perf_event.c

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 arch/arm/Kconfig                  |    5 +
 arch/arm/include/asm/perf_event.h |    2 -
 arch/arm/kernel/Makefile          |    1 +
 arch/arm/kernel/perf_event.c      |  993 +++++++++++++++++++++++++++++++++++++
 4 files changed, 999 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm/kernel/perf_event.c
arch/arm/Kconfig +5 -0
index 1e35f21..1a1b53b 100644
@@ -162,6 +162,11 @@ config ARCH_MTD_XIP
 config GENERIC_HARDIRQS_NO__DO_IRQ
 	def_bool y
 
+config HW_PERF_EVENTS
+	bool
+	depends on PERF_EVENTS && CPU_HAS_PMU
+	default y
+
 if OPROFILE
 
 config OPROFILE_ARMV6
arch/arm/include/asm/perf_event.h +0 -2
index 32a66ac..c29f84e 100644
@@ -31,8 +31,6 @@ set_perf_event_pending(void)
 #define perf_misc_flags(regs)   (user_mode(regs) ? PERF_RECORD_MISC_USER : \
                                  PERF_RECORD_MISC_KERNEL)
 
-/* ARM performance counters start from 1 (in the cp15 accesses) so use the
- * same indexes here for consistency. */
 #define PERF_EVENT_INDEX_OFFSET 1
 
 #endif /* __ARM_PERF_EVENT_H__ */
arch/arm/kernel/Makefile +1 -0
index 286a276..44ebf36 100644
@@ -47,6 +47,7 @@ obj-$(CONFIG_CPU_XSC3)		+= xscale-cp0.o
 obj-$(CONFIG_CPU_MOHAWK)	+= xscale-cp0.o
 obj-$(CONFIG_IWMMXT)		+= iwmmxt.o
 obj-$(CONFIG_CPU_HAS_PMU)	+= pmu.o
+obj-$(CONFIG_HW_PERF_EVENTS)	+= perf_event.o
 AFLAGS_iwmmxt.o			:= -Wa,-mcpu=iwmmxt
 
 ifneq ($(CONFIG_ARCH_EBSA110),y)
arch/arm/kernel/perf_event.c new file +993 -0

993 lines not on this page · raw message

Signature · 1 line
1.6.5.4
#5 lore raw

PATCH 4/4 arm/perfevents: implement perf event support for ARMv6

Quoted 8 lines · Jamie Iles

This patch implements support for ARMv6 performance counters in the Linux performance events subsystem. ARMv6 architectures that have the performance counters should enable ARMV6_PERF_EVENTS and define the interrupts for the counters in arch/arm/kernel/perf_event.c

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Quoted 7 lines
---
 arch/arm/Kconfig                  |    5 +
 arch/arm/include/asm/perf_event.h |    2 -
 arch/arm/kernel/Makefile          |    1 +
 arch/arm/kernel/perf_event.c      |  993 +++++++++++++++++++++++++++++++++++++
 4 files changed, 999 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm/kernel/perf_event.c

Very nice!

The code looks sane - i've only got a few (minor) stylistic comments.

Firstly, you seem to be using several different, inconsistent multi-line comment styles:

arch/arm/kernel/perf_event.c

Quoted 9 lines
arch/arm/kernel/perf_event.c new file +8 -0
+	/*
+	 * The interrupts are cleared by writing the overflow flags back to
+	 * the control register. All of the other bits don't have any effect
+	 * if they are rewritten, so write the whole value back.
+	 */
 
+/* Define the IRQs for the system. We could use something like a platform
+ * device but that seems fairly heavyweight for this. Also, the performance
+ * counters can't be removed or hotplugged. */

please use the first variant everywhere, that's what is specified in Documentation/CodingStyle as well:

/*
* Comment .....
* ...... goes here.
*/

There's also some whitespace damage as well:

arch/arm/kernel/perf_event.c line 909

Quoted 2 lines
arch/arm/kernel/perf_event.c new file +2 -0
+        /* frame pointers should strictly progress back up the stack
+         * (towards higher addresses) */

(should be tabs, not spaces)

Thanks,

Ingo