Skip to contentSkip to conversation

Generalise ARM perf-events backend for oprofile

PATCH In review Matt Fleming 4 participants 18 messages Oct 9, 2010 – Oct 11, 2010

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v5 cover Generalise ARM perf-events backend for oprofile

The perf-events backend for OProfile that Will Deacon wrote in 8c1fc96f6fd1f361428ba805103af0d0eee65179 ("ARM: 6072/1: oprofile: use perf-events framework as backend") is of use to more architectures than just ARM. Move the code into drivers/oprofile/ so that SH can use it instead of the nearly identical copy of its OProfile code.

The benefit of the backend is that it becomes necessary to only maintain one copy of the PMU accessor functions for each architecture, with bug fixes and new features benefiting both OProfile and perf.

Note that I haven't been able to test these patches on an ARM board to see if I've caused any regressions. If anyone else could do that I'd appreciate it. Though, I have been able to compile this version of the series.

This patch series is based on,

git://git.kernel.org/pub/scm/linux/kernel/git/rric/oprofile.git core

These patches can also be found at,

git://git.kernel.org/pub/scm/linux/kernel/git/mfleming/sh-2.6.git perf-oprofile

Robert, I haven't rebased this work on your oprofile updates for 2.6.37. If you need me to do that, just shout. If you have any comments I'll respin this ASAP but it's probably ready for merging now.

Changes from v4:
- SH is the only arch that doesn't default perf_pmu_name()
- Changed perf_nr_counters to num_counters

Changes from v3:
- New perf_pmu_name() function
- Sprinkled an EXPORT_SYMBOL_GPL()
- Don't repeatedly call perf_num_counters()
- Change name of oprofile_driver to "oprofile-perf"
- Hard overwrite the oprofile_operations in oprofile_perf_init()
- Dropped the patch that made oprofile depend on HW_PERF_EVENTS for ARM

Changes from v2:
- Rebased against Robert's oprofile core branch
- Moved even more of the ARM code into the generic oprofile code
- Broke the patches up into more logical steps

Changes from v1:
- Prefix the new functons with "oprofile_" instead of "op_".
- Fix ARM compilation errors
- Move all the oprofile-perf logic into oprofile_perf.c
- Include cleanup patch from Will

Matt Fleming (7):
perf: Add helper function to return number of counters
perf: New helper function for pmu name
oprofile: Make op_name_from_perf_id() global
ARM: oprofile: Rename op_arm to oprofile_perf
ARM: oprofile: Move non-ARM code into separate init/exit
oprofile: Abstract the perf-events backend
sh: oprofile: Use perf-events oprofile backend

 arch/arm/kernel/perf_event.c     |    6 +
 arch/arm/oprofile/Makefile       |    4 +
 arch/arm/oprofile/common.c       |  309 +-----------------------------------
 arch/sh/Kconfig                  |   13 ++
 arch/sh/kernel/perf_event.c      |   18 ++
 arch/sh/oprofile/Makefile        |    4 +
 arch/sh/oprofile/common.c        |  115 +++-----------
 arch/sh/oprofile/op_impl.h       |   33 ----
 drivers/oprofile/oprofile_perf.c |  326 ++++++++++++++++++++++++++++++++++++++
 include/linux/oprofile.h         |    7 +
 include/linux/perf_event.h       |    2 +
 kernel/perf_event.c              |    5 +
 12 files changed, 412 insertions(+), 430 deletions(-)
 delete mode 100644 arch/sh/oprofile/op_impl.h
 create mode 100644 drivers/oprofile/oprofile_perf.c
#1 lore raw

PATCH v5 1/7 perf: Add helper function to return number of counters

The number of counters for the registered pmu is needed in a few places so provide a helper function that returns this number.

---
 arch/arm/kernel/perf_event.c |    6 ++++++
 arch/arm/oprofile/common.c   |   31 ++++++++++++++++++-------------
 arch/sh/kernel/perf_event.c  |    9 +++++++++
 include/linux/perf_event.h   |    1 +
 4 files changed, 34 insertions(+), 13 deletions(-)
arch/arm/kernel/perf_event.c +6 -0
index ecbb028..ef3bc33 100644
@@ -123,6 +123,12 @@ armpmu_get_max_events(void)
 }
 EXPORT_SYMBOL_GPL(armpmu_get_max_events);
 
+int perf_num_counters(void)
+{
+	return armpmu_get_max_events();
+}
+EXPORT_SYMBOL_GPL(perf_num_counters);
+
 #define HW_OP_UNSUPPORTED		0xFFFF
 
 #define C(_x) \
arch/arm/oprofile/common.c +18 -13
index d660cb8..1e971a7 100644
@@ -43,7 +43,7 @@ static DEFINE_MUTEX(op_arm_mutex);
 
 static struct op_counter_config *counter_config;
 static struct perf_event **perf_events[nr_cpumask_bits];
-static int perf_num_counters;
+static int num_counters;
 
 /*
  * Overflow callback for oprofile.
@@ -54,11 +54,11 @@ static void op_overflow_handler(struct perf_event *event, int unused,
 	int id;
 	u32 cpu = smp_processor_id();
 
-	for (id = 0; id < perf_num_counters; ++id)
+	for (id = 0; id < num_counters; ++id)
 		if (perf_events[cpu][id] == event)
 			break;
 
-	if (id != perf_num_counters)
+	if (id != num_counters)
 		oprofile_add_sample(regs, id);
 	else
 		pr_warning("oprofile: ignoring spurious overflow "
@@ -76,7 +76,7 @@ static void op_perf_setup(void)
 	u32 size = sizeof(struct perf_event_attr);
 	struct perf_event_attr *attr;
 
-	for (i = 0; i < perf_num_counters; ++i) {
+	for (i = 0; i < num_counters; ++i) {
 		attr = &counter_config[i].attr;
 		memset(attr, 0, size);
 		attr->type		= PERF_TYPE_RAW;
@@ -131,7 +131,7 @@ static int op_perf_start(void)
 	int cpu, event, ret = 0;
 
 	for_each_online_cpu(cpu) {
-		for (event = 0; event < perf_num_counters; ++event) {
+		for (event = 0; event < num_counters; ++event) {
 			ret = op_create_counter(cpu, event);
 			if (ret)
 				goto out;
@@ -150,7 +150,7 @@ static void op_perf_stop(void)
 	int cpu, event;
 
 	for_each_online_cpu(cpu)
-		for (event = 0; event < perf_num_counters; ++event)
+		for (event = 0; event < num_counters; ++event)
 			op_destroy_counter(cpu, event);
 }
 
@@ -179,7 +179,7 @@ static int op_arm_create_files(struct super_block *sb, struct dentry *root)
 {
 	unsigned int i;
 
-	for (i = 0; i < perf_num_counters; i++) {
+	for (i = 0; i < num_counters; i++) {
 		struct dentry *dir;
 		char buf[4];
 
@@ -353,14 +353,19 @@ int __init oprofile_arch_init(struct oprofile_operations *ops)
 
 	memset(&perf_events, 0, sizeof(perf_events));
 
-	perf_num_counters = armpmu_get_max_events();
+	num_counters = perf_num_counters();
+	if (num_counters <= 0) {
+		pr_info("oprofile: no performance counters\n");
+		ret = -ENODEV;
+		goto out;
+	}
 
-	counter_config = kcalloc(perf_num_counters,
+	counter_config = kcalloc(num_counters,
 			sizeof(struct op_counter_config), GFP_KERNEL);
 
 	if (!counter_config) {
 		pr_info("oprofile: failed to allocate %d "
-				"counters\n", perf_num_counters);
+				"counters\n", num_counters);
 		ret = -ENOMEM;
 		goto out;
 	}
@@ -370,11 +375,11 @@ int __init oprofile_arch_init(struct oprofile_operations *ops)
 		goto out;
 
 	for_each_possible_cpu(cpu) {
-		perf_events[cpu] = kcalloc(perf_num_counters,
+		perf_events[cpu] = kcalloc(num_counters,
 				sizeof(struct perf_event *), GFP_KERNEL);
 		if (!perf_events[cpu]) {
 			pr_info("oprofile: failed to allocate %d perf events "
-					"for cpu %d\n", perf_num_counters, cpu);
+					"for cpu %d\n", num_counters, cpu);
 			ret = -ENOMEM;
 			goto out;
 		}
@@ -409,7 +414,7 @@ void __exit oprofile_arch_exit(void)
 	struct perf_event *event;
 
 	for_each_possible_cpu(cpu) {
-		for (id = 0; id < perf_num_counters; ++id) {
+		for (id = 0; id < num_counters; ++id) {
 			event = perf_events[cpu][id];
 			if (event)
 				perf_event_release_kernel(event);
arch/sh/kernel/perf_event.c +9 -0
index 7a3dc35..2cb9ad5 100644
@@ -59,6 +59,15 @@ static inline int sh_pmu_initialized(void)
 	return !!sh_pmu;
 }
 
+int perf_num_counters(void)
+{
+	if (!sh_pmu)
+		return 0;
+
+	return sh_pmu->num_events;
+}
+EXPORT_SYMBOL_GPL(perf_num_counters);
+
 /*
  * Release the PMU if this is the last perf_event.
  */
include/linux/perf_event.h +1 -0
index 716f99b..1a02192 100644
@@ -849,6 +849,7 @@ extern int perf_max_events;
 
 extern const struct pmu *hw_perf_event_init(struct perf_event *event);
 
+extern int perf_num_counters(void);
 extern void perf_event_task_sched_in(struct task_struct *task);
 extern void perf_event_task_sched_out(struct task_struct *task, struct task_struct *next);
 extern void perf_event_task_tick(struct task_struct *task);
Signature · 1 line
1.7.1
#2 lore raw

PATCH v5 2/7 perf: New helper function for pmu name

In reply to Matt Fleming

Introduce perf_pmu_name() helper function that returns the name of the pmu. This gives us a generic way to get the name of a pmu regardless of how an architecture identifies it internally.

---
 arch/sh/kernel/perf_event.c |    9 +++++++++
 include/linux/perf_event.h  |    1 +
 kernel/perf_event.c         |    5 +++++
 3 files changed, 15 insertions(+), 0 deletions(-)
arch/sh/kernel/perf_event.c +9 -0
index 2cb9ad5..55fe89b 100644
@@ -59,6 +59,15 @@ static inline int sh_pmu_initialized(void)
 	return !!sh_pmu;
 }
 
+const char *perf_pmu_name(void)
+{
+	if (!sh_pmu)
+		return NULL;
+
+	return sh_pmu->name;
+}
+EXPORT_SYMBOL_GPL(perf_pmu_name);
+
 int perf_num_counters(void)
 {
 	if (!sh_pmu)
include/linux/perf_event.h +1 -0
index 1a02192..33f08da 100644
@@ -850,6 +850,7 @@ extern int perf_max_events;
 extern const struct pmu *hw_perf_event_init(struct perf_event *event);
 
 extern int perf_num_counters(void);
+extern const char *perf_pmu_name(void);
 extern void perf_event_task_sched_in(struct task_struct *task);
 extern void perf_event_task_sched_out(struct task_struct *task, struct task_struct *next);
 extern void perf_event_task_tick(struct task_struct *task);
kernel/perf_event.c +5 -0
index db5b560..fc51268 100644
@@ -85,6 +85,11 @@ void __weak hw_perf_enable(void)		{ barrier(); }
 
 void __weak perf_event_print_debug(void)	{ }
 
+extern __weak const char *perf_pmu_name(void)
+{
+	return "pmu";
+}
+
 static DEFINE_PER_CPU(int, perf_disable_count);
 
 void perf_disable(void)
Signature · 1 line
1.7.1
#3 lore raw

Re: [PATCH 2/7] perf: New helper function for pmu name

Quoted 5 lines · Matt Fleming

Introduce perf_pmu_name() helper function that returns the name of the pmu. This gives us a generic way to get the name of a pmu regardless of how an architecture identifies it internally.

Peter, I need your ack here.

Thanks,

-Robert

Quoted 58 lines
---
 arch/sh/kernel/perf_event.c |    9 +++++++++
 include/linux/perf_event.h  |    1 +
 kernel/perf_event.c         |    5 +++++
 3 files changed, 15 insertions(+), 0 deletions(-)
arch/sh/kernel/perf_event.c +9 -0
index 2cb9ad5..55fe89b 100644
@@ -59,6 +59,15 @@ static inline int sh_pmu_initialized(void)
 	return !!sh_pmu;
 }
 
+const char *perf_pmu_name(void)
+{
+	if (!sh_pmu)
+		return NULL;
+
+	return sh_pmu->name;
+}
+EXPORT_SYMBOL_GPL(perf_pmu_name);
+
 int perf_num_counters(void)
 {
 	if (!sh_pmu)
include/linux/perf_event.h +1 -0
index 1a02192..33f08da 100644
@@ -850,6 +850,7 @@ extern int perf_max_events;
 extern const struct pmu *hw_perf_event_init(struct perf_event *event);
 
 extern int perf_num_counters(void);
+extern const char *perf_pmu_name(void);
 extern void perf_event_task_sched_in(struct task_struct *task);
 extern void perf_event_task_sched_out(struct task_struct *task, struct task_struct *next);
 extern void perf_event_task_tick(struct task_struct *task);
kernel/perf_event.c +5 -0
index db5b560..fc51268 100644
@@ -85,6 +85,11 @@ void __weak hw_perf_enable(void)		{ barrier(); }
 
 void __weak perf_event_print_debug(void)	{ }
 
+extern __weak const char *perf_pmu_name(void)
+{
+	return "pmu";
+}
+
 static DEFINE_PER_CPU(int, perf_disable_count);
 
 void perf_disable(void)
Signature · 1 line
1.7.1
Signature · 2 lines
Advanced Micro Devices, Inc.
Operating System Research Center
maintainer Acked-by
#4 lore raw

Re: [PATCH 2/7] perf: New helper function for pmu name

Quoted 8 lines · Robert Richter
Quoted 5 lines · Matt Fleming

Introduce perf_pmu_name() helper function that returns the name of the pmu. This gives us a generic way to get the name of a pmu regardless of how an architecture identifies it internally.

Peter, I need your ack here.

Sure,

Acked-by on v5 2/7
#5 lore raw

Re: [PATCH 2/7] perf: New helper function for pmu name

In reply to Matt Fleming
Quoted 11 lines · Matt Fleming

Introduce perf_pmu_name() helper function that returns the name of the pmu. This gives us a generic way to get the name of a pmu regardless of how an architecture identifies it internally.

---
 arch/sh/kernel/perf_event.c |    9 +++++++++
 include/linux/perf_event.h  |    1 +
 kernel/perf_event.c         |    5 +++++
 3 files changed, 15 insertions(+), 0 deletions(-)

This part is obviously fine with me, too.

#6 lore raw

PATCH v5 3/7 oprofile: Make op_name_from_perf_id() global

In reply to Matt Fleming

Make op_name_from_perf_id() global so that we have a way for each architecture to construct an oprofile name for op->cpu_type. We need to remove the argument from the function prototype so that we can hide all implementation details inside the function.

---
 arch/arm/oprofile/common.c |    6 ++++--
 include/linux/oprofile.h   |    4 ++++
 2 files changed, 8 insertions(+), 2 deletions(-)
arch/arm/oprofile/common.c +4 -2
index 1e971a7..4f67cfa 100644
@@ -155,8 +155,10 @@ static void op_perf_stop(void)
 }
 
 
-static char *op_name_from_perf_id(enum arm_perf_pmu_ids id)
+char *op_name_from_perf_id(void)
 {
+	enum arm_perf_pmu_ids id = armpmu_get_pmu_id();
+
 	switch (id) {
 	case ARM_PERF_PMU_ID_XSCALE1:
 		return "arm/xscale1";
@@ -391,7 +393,7 @@ int __init oprofile_arch_init(struct oprofile_operations *ops)
 	ops->start		= op_arm_start;
 	ops->stop		= op_arm_stop;
 	ops->shutdown		= op_arm_stop;
-	ops->cpu_type		= op_name_from_perf_id(armpmu_get_pmu_id());
+	ops->cpu_type		= op_name_from_perf_id();
 
 	if (!ops->cpu_type)
 		ret = -ENODEV;
include/linux/oprofile.h +4 -0
index 5171639..1574d4a 100644
@@ -185,4 +185,8 @@ int oprofile_add_data(struct op_entry *entry, unsigned long val);
 int oprofile_add_data64(struct op_entry *entry, u64 val);
 int oprofile_write_commit(struct op_entry *entry);
 
+#ifdef CONFIG_PERF_EVENTS
+char *op_name_from_perf_id(void);
+#endif /* CONFIG_PERF_EVENTS */
+
 #endif /* OPROFILE_H */
Signature · 1 line
1.7.1
#7 lore raw

PATCH v5 4/7 ARM: oprofile: Rename op_arm to oprofile_perf

In reply to Matt Fleming

In preparation for moving the generic functions out of this file, give the functions more general names (e.g. remove "arm" from the names).

---
 arch/arm/oprofile/common.c |   58 ++++++++++++++++++++++----------------------
 1 files changed, 29 insertions(+), 29 deletions(-)
arch/arm/oprofile/common.c +29 -29
index 4f67cfa..fd6e323 100644
@@ -38,8 +38,8 @@ struct op_counter_config {
 	struct perf_event_attr attr;
 };
 
-static int op_arm_enabled;
-static DEFINE_MUTEX(op_arm_mutex);
+static int oprofile_perf_enabled;
+static DEFINE_MUTEX(oprofile_perf_mutex);
 
 static struct op_counter_config *counter_config;
 static struct perf_event **perf_events[nr_cpumask_bits];
@@ -66,7 +66,7 @@ static void op_overflow_handler(struct perf_event *event, int unused,
 }
 
 /*
- * Called by op_arm_setup to create perf attributes to mirror the oprofile
+ * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile
  * settings in counter_config. Attributes are created as `pinned' events and
  * so are permanently scheduled on the PMU.
  */
@@ -123,7 +123,7 @@ static void op_destroy_counter(int cpu, int event)
 }
 
 /*
- * Called by op_arm_start to create active perf events based on the
+ * Called by oprofile_perf_start to create active perf events based on the
  * perviously configured attributes.
  */
 static int op_perf_start(void)
@@ -143,7 +143,7 @@ out:
 }
 
 /*
- * Called by op_arm_stop at the end of a profiling run.
+ * Called by oprofile_perf_stop at the end of a profiling run.
  */
 static void op_perf_stop(void)
 {
@@ -177,7 +177,7 @@ char *op_name_from_perf_id(void)
 	}
 }
 
-static int op_arm_create_files(struct super_block *sb, struct dentry *root)
+static int oprofile_perf_create_files(struct super_block *sb, struct dentry *root)
 {
 	unsigned int i;
 
@@ -198,7 +198,7 @@ static int op_arm_create_files(struct super_block *sb, struct dentry *root)
 	return 0;
 }
 
-static int op_arm_setup(void)
+static int oprofile_perf_setup(void)
 {
 	spin_lock(&oprofilefs_lock);
 	op_perf_setup();
@@ -206,54 +206,54 @@ static int op_arm_setup(void)
 	return 0;
 }
 
-static int op_arm_start(void)
+static int oprofile_perf_start(void)
 {
 	int ret = -EBUSY;
 
-	mutex_lock(&op_arm_mutex);
-	if (!op_arm_enabled) {
+	mutex_lock(&oprofile_perf_mutex);
+	if (!oprofile_perf_enabled) {
 		ret = 0;
 		op_perf_start();
-		op_arm_enabled = 1;
+		oprofile_perf_enabled = 1;
 	}
-	mutex_unlock(&op_arm_mutex);
+	mutex_unlock(&oprofile_perf_mutex);
 	return ret;
 }
 
-static void op_arm_stop(void)
+static void oprofile_perf_stop(void)
 {
-	mutex_lock(&op_arm_mutex);
-	if (op_arm_enabled)
+	mutex_lock(&oprofile_perf_mutex);
+	if (oprofile_perf_enabled)
 		op_perf_stop();
-	op_arm_enabled = 0;
-	mutex_unlock(&op_arm_mutex);
+	oprofile_perf_enabled = 0;
+	mutex_unlock(&oprofile_perf_mutex);
 }
 
 #ifdef CONFIG_PM
-static int op_arm_suspend(struct platform_device *dev, pm_message_t state)
+static int oprofile_perf_suspend(struct platform_device *dev, pm_message_t state)
 {
-	mutex_lock(&op_arm_mutex);
-	if (op_arm_enabled)
+	mutex_lock(&oprofile_perf_mutex);
+	if (oprofile_perf_enabled)
 		op_perf_stop();
-	mutex_unlock(&op_arm_mutex);
+	mutex_unlock(&oprofile_perf_mutex);
 	return 0;
 }
 
-static int op_arm_resume(struct platform_device *dev)
+static int oprofile_perf_resume(struct platform_device *dev)
 {
-	mutex_lock(&op_arm_mutex);
-	if (op_arm_enabled && op_perf_start())
-		op_arm_enabled = 0;
-	mutex_unlock(&op_arm_mutex);
+	mutex_lock(&oprofile_perf_mutex);
+	if (oprofile_perf_enabled && op_perf_start())
+		oprofile_perf_enabled = 0;
+	mutex_unlock(&oprofile_perf_mutex);
 	return 0;
 }
 
 static struct platform_driver oprofile_driver = {
 	.driver		= {
-		.name		= "arm-oprofile",
+		.name		= "oprofile-perf",
 	},
-	.resume		= op_arm_resume,
-	.suspend	= op_arm_suspend,
+	.resume		= oprofile_perf_resume,
+	.suspend	= oprofile_perf_suspend,
 };
 
 static struct platform_device *oprofile_pdev;
Signature · 1 line
1.7.1
#8 lore raw

PATCH v5 5/7 ARM: oprofile: Move non-ARM code into separate init/exit

In reply to Matt Fleming

In preparation for moving the majority of this oprofile code into an architecture-neutral place separate the architecture-independent code into oprofile_perf_init() and oprofile_perf_exit().

---
 arch/arm/oprofile/common.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)
arch/arm/oprofile/common.c +19 -8
index fd6e323..8718311 100644
@@ -349,7 +349,7 @@ static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
 		tail = user_backtrace(tail);
 }
 
-int __init oprofile_arch_init(struct oprofile_operations *ops)
+int __init oprofile_perf_init(struct oprofile_operations *ops)
 {
 	int cpu, ret = 0;
 
@@ -387,12 +387,11 @@ int __init oprofile_arch_init(struct oprofile_operations *ops)
 		}
 	}
 
-	ops->backtrace		= arm_backtrace;
-	ops->create_files	= op_arm_create_files;
-	ops->setup		= op_arm_setup;
-	ops->start		= op_arm_start;
-	ops->stop		= op_arm_stop;
-	ops->shutdown		= op_arm_stop;
+	ops->create_files	= oprofile_perf_create_files;
+	ops->setup		= oprofile_perf_setup;
+	ops->start		= oprofile_perf_start;
+	ops->stop		= oprofile_perf_stop;
+	ops->shutdown		= oprofile_perf_stop;
 	ops->cpu_type		= op_name_from_perf_id();
 
 	if (!ops->cpu_type)
@@ -410,7 +409,14 @@ out:
 	return ret;
 }
 
-void __exit oprofile_arch_exit(void)
+int __init oprofile_arch_init(struct oprofile_operations *ops)
+{
+	ops->backtrace		= arm_backtrace;
+
+	return oprofile_perf_init(ops);
+}
+
+void __exit oprofile_perf_exit(void)
 {
 	int cpu, id;
 	struct perf_event *event;
@@ -428,6 +434,11 @@ void __exit oprofile_arch_exit(void)
 	kfree(counter_config);
 	exit_driverfs();
 }
+
+void __exit oprofile_arch_exit(void)
+{
+	oprofile_perf_exit();
+}
 #else
 int __init oprofile_arch_init(struct oprofile_operations *ops)
 {
Signature · 1 line
1.7.1
#9 lore raw

Re: [PATCH 5/7] ARM: oprofile: Move non-ARM code into separate init/exit

Quoted 28 lines · Matt Fleming

In preparation for moving the majority of this oprofile code into an architecture-neutral place separate the architecture-independent code into oprofile_perf_init() and oprofile_perf_exit().

---
 arch/arm/oprofile/common.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)
arch/arm/oprofile/common.c +1 -2
index fd6e323..8718311 100644
@@ -349,7 +349,7 @@ static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
 		tail = user_backtrace(tail);
 }
 
-int __init oprofile_arch_init(struct oprofile_operations *ops)
+int __init oprofile_perf_init(struct oprofile_operations *ops)
 {
 	int cpu, ret = 0;
 
@@ -387,12 +387,11 @@ int __init oprofile_arch_init(struct oprofile_operations *ops)
 		}
 	}
 
-	ops->backtrace		= arm_backtrace;

arch/arm/oprofile/common.c int __init oprofile_arch_init(struct oprofile_operations *ops) · line 394

Quoted 10 lines
arch/arm/oprofile/common.c +5 -5
-	ops->create_files	= op_arm_create_files;
-	ops->setup		= op_arm_setup;
-	ops->start		= op_arm_start;
-	ops->stop		= op_arm_stop;
-	ops->shutdown		= op_arm_stop;
+	ops->create_files	= oprofile_perf_create_files;
+	ops->setup		= oprofile_perf_setup;
+	ops->start		= oprofile_perf_start;
+	ops->stop		= oprofile_perf_stop;
+	ops->shutdown		= oprofile_perf_stop;

There is a compile breakage caused by patch #4. Will move the renames to patch #4 when applying the patch.

-Robert

Quoted 35 lines
arch/arm/oprofile/common.c +13 -1
 	ops->cpu_type		= op_name_from_perf_id();
 
 	if (!ops->cpu_type)
@@ -410,7 +409,14 @@ out:
 	return ret;
 }
 
-void __exit oprofile_arch_exit(void)
+int __init oprofile_arch_init(struct oprofile_operations *ops)
+{
+	ops->backtrace		= arm_backtrace;
+
+	return oprofile_perf_init(ops);
+}
+
+void __exit oprofile_perf_exit(void)
 {
 	int cpu, id;
 	struct perf_event *event;
@@ -428,6 +434,11 @@ void __exit oprofile_arch_exit(void)
 	kfree(counter_config);
 	exit_driverfs();
 }
+
+void __exit oprofile_arch_exit(void)
+{
+	oprofile_perf_exit();
+}
 #else
 int __init oprofile_arch_init(struct oprofile_operations *ops)
 {
Signature · 1 line
1.7.1
Signature · 2 lines
Advanced Micro Devices, Inc.
Operating System Research Center
#10 lore raw

Re: [PATCH 5/7] ARM: oprofile: Move non-ARM code into separate init/exit

Quoted 2 lines · Robert Richter

There is a compile breakage caused by patch #4. Will move the renames to patch #4 when applying the patch.

(For some reason I did not receive your mail 0/7, so I reply here.)

Matt,

on the first glance you patches are looking good (the above is the only thing I found). Will do some further testing and waiting for more replies. If all goes well I am going to apply them.

Thanks,

-Robert

Signature · 2 lines
Advanced Micro Devices, Inc.
Operating System Research Center
#11 lore raw

Re: [PATCH 5/7] ARM: oprofile: Move non-ARM code into separate init/exit

In reply to Robert Richter
Quoted 28 lines · Robert Richter
Quoted 27 lines · Matt Fleming

In preparation for moving the majority of this oprofile code into an architecture-neutral place separate the architecture-independent code into oprofile_perf_init() and oprofile_perf_exit().

---
 arch/arm/oprofile/common.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)
arch/arm/oprofile/common.c +1 -1
index fd6e323..8718311 100644
@@ -349,7 +349,7 @@ static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
 		tail = user_backtrace(tail);
 }
 
-int __init oprofile_arch_init(struct oprofile_operations *ops)
+int __init oprofile_perf_init(struct oprofile_operations *ops)
 {
 	int cpu, ret = 0;
 
@@ -387,12 +387,11 @@ int __init oprofile_arch_init(struct oprofile_operations *ops)
 		}
 	}
 
Quoted 14 lines

arch/arm/oprofile/common.c int __init oprofile_arch_init(struct oprofile_operations *ops) · line 394

Quoted 11 lines
arch/arm/oprofile/common.c +5 -6
-	ops->backtrace		= arm_backtrace;
-	ops->create_files	= op_arm_create_files;
-	ops->setup		= op_arm_setup;
-	ops->start		= op_arm_start;
-	ops->stop		= op_arm_stop;
-	ops->shutdown		= op_arm_stop;
+	ops->create_files	= oprofile_perf_create_files;
+	ops->setup		= oprofile_perf_setup;
+	ops->start		= oprofile_perf_start;
+	ops->stop		= oprofile_perf_stop;
+	ops->shutdown		= oprofile_perf_stop;

There is a compile breakage caused by patch #4. Will move the renames to patch #4 when applying the patch.

Sorry, I don't know how this happened. These renames were originally in patch #4 but seem to have moved into #5 when I was reshuffling stuff.

#12 lore raw

PATCH v5 6/7 oprofile: Abstract the perf-events backend

In reply to Matt Fleming

Move the perf-events backend from arch/arm/oprofile into drivers/oprofile so that the code can be shared between architectures.

This allows each architecture to maintain only a single copy of the PMU accessor functions instead of one for both perf and OProfile. It also becomes possible for other architectures to delete much of their OProfile code in favour of the common code now available in drivers/oprofile/oprofile_perf.c.

---
 arch/arm/oprofile/Makefile       |    4 +
 arch/arm/oprofile/common.c       |  319 -------------------------------------
 drivers/oprofile/oprofile_perf.c |  326 ++++++++++++++++++++++++++++++++++++++
 include/linux/oprofile.h         |    3 +
 4 files changed, 333 insertions(+), 319 deletions(-)
 create mode 100644 drivers/oprofile/oprofile_perf.c
arch/arm/oprofile/Makefile +4 -0
index e666eaf..b2215c6 100644
@@ -6,4 +6,8 @@ DRIVER_OBJS = $(addprefix ../../../drivers/oprofile/, \
 		oprofilefs.o oprofile_stats.o \
 		timer_int.o )
 
+ifeq ($(CONFIG_HW_PERF_EVENTS),y)
+DRIVER_OBJS += $(addprefix ../../../drivers/oprofile/, oprofile_perf.o)
+endif
+
 oprofile-y				:= $(DRIVER_OBJS) common.o
arch/arm/oprofile/common.c +0 -319

343 lines not on this page · raw message

drivers/oprofile/oprofile_perf.c new file +326 -0

326 lines not on this page · raw message

include/linux/oprofile.h +3 -0
index 1574d4a..d67a833 100644
@@ -15,6 +15,7 @@
 
 #include <linux/types.h>
 #include <linux/spinlock.h>
+#include <linux/init.h>
 #include <asm/atomic.h>
  
 /* Each escaped entry is prefixed by ESCAPE_CODE
@@ -186,6 +187,8 @@ int oprofile_add_data64(struct op_entry *entry, u64 val);
 int oprofile_write_commit(struct op_entry *entry);
 
 #ifdef CONFIG_PERF_EVENTS
+int __init oprofile_perf_init(struct oprofile_operations *ops);
+void __exit oprofile_perf_exit(void);
 char *op_name_from_perf_id(void);
 #endif /* CONFIG_PERF_EVENTS */
 
Signature · 1 line
1.7.1
#13 lore raw

PATCH v5 7/7 sh: oprofile: Use perf-events oprofile backend

In reply to Matt Fleming

Now that we've got a generic perf-events based oprofile backend we might as well make use of it seeing as SH doesn't do anything special with its oprofile backend. Also introduce a new CONFIG_HW_PERF_EVENTS symbol so that we can fallback to using the timer interrupt for oprofile if the CPU doesn't support perf events.

Also, to avoid a section mismatch warning we need to annotate oprofile_arch_exit() with an __exit marker.

---
 arch/sh/Kconfig            |   13 +++++
 arch/sh/oprofile/Makefile  |    4 ++
 arch/sh/oprofile/common.c  |  115 +++++++++-----------------------------------
 arch/sh/oprofile/op_impl.h |   33 -------------
 4 files changed, 40 insertions(+), 125 deletions(-)
 delete mode 100644 arch/sh/oprofile/op_impl.h
arch/sh/Kconfig +13 -0
index 33990fa..35b6c3f 100644
@@ -249,6 +249,11 @@ config ARCH_SHMOBILE
 	select PM
 	select PM_RUNTIME
 
+config CPU_HAS_PMU
+       depends on CPU_SH4 || CPU_SH4A
+       default y
+       bool
+
 if SUPERH32
 
 choice
@@ -738,6 +743,14 @@ config GUSA_RB
 	  LLSC, this should be more efficient than the other alternative of
 	  disabling interrupts around the atomic sequence.
 
+config HW_PERF_EVENTS
+	bool "Enable hardware performance counter support for perf events"
+	depends on PERF_EVENTS && CPU_HAS_PMU
+	default y
+	help
+	  Enable hardware performance counter support for perf events. If
+	  disabled, perf events will use software events only.
+
 source "drivers/sh/Kconfig"
 
 endmenu
arch/sh/oprofile/Makefile +4 -0
index 4886c5c..e85aae7 100644
@@ -6,4 +6,8 @@ DRIVER_OBJS = $(addprefix ../../../drivers/oprofile/, \
 		oprofilefs.o oprofile_stats.o \
 		timer_int.o )
 
+ifeq ($(CONFIG_HW_PERF_EVENTS),y)
+DRIVER_OBJS += $(addprefix ../../../drivers/oprofile/, oprofile_perf.o)
+endif
+
 oprofile-y	:= $(DRIVER_OBJS) common.o backtrace.o
arch/sh/oprofile/common.c +23 -92

137 lines not on this page · raw message

arch/sh/oprofile/op_impl.h deleted +0 -33
deleted file mode 100644
index 1244479..0000000
@@ -1,33 +0,0 @@
-#ifndef __OP_IMPL_H
-#define __OP_IMPL_H
-
-/* Per-counter configuration as set via oprofilefs.  */
-struct op_counter_config {
-	unsigned long enabled;
-	unsigned long event;
-
-	unsigned long count;
-
-	/* Dummy values for userspace tool compliance */
-	unsigned long kernel;
-	unsigned long user;
-	unsigned long unit_mask;
-};
-
-/* Per-architecture configury and hooks.  */
-struct op_sh_model {
-	void (*reg_setup)(struct op_counter_config *);
-	int (*create_files)(struct super_block *sb, struct dentry *dir);
-	void (*cpu_setup)(void *dummy);
-	int (*init)(void);
-	void (*exit)(void);
-	void (*cpu_start)(void *args);
-	void (*cpu_stop)(void *args);
-	char *cpu_type;
-	unsigned char num_counters;
-};
-
-/* arch/sh/oprofile/common.c */
-extern void sh_backtrace(struct pt_regs * const regs, unsigned int depth);
-
-#endif /* __OP_IMPL_H */
Signature · 1 line
1.7.1
#14 lore raw

Re: [PATCH 7/7] sh: oprofile: Use perf-events oprofile backend

Quoted 27 lines · Matt Fleming

Now that we've got a generic perf-events based oprofile backend we might as well make use of it seeing as SH doesn't do anything special with its oprofile backend. Also introduce a new CONFIG_HW_PERF_EVENTS symbol so that we can fallback to using the timer interrupt for oprofile if the CPU doesn't support perf events.

Also, to avoid a section mismatch warning we need to annotate oprofile_arch_exit() with an __exit marker.

---
 arch/sh/Kconfig            |   13 +++++
 arch/sh/oprofile/Makefile  |    4 ++
 arch/sh/oprofile/common.c  |  115 +++++++++-----------------------------------
 arch/sh/oprofile/op_impl.h |   33 -------------
 4 files changed, 40 insertions(+), 125 deletions(-)
 delete mode 100644 arch/sh/oprofile/op_impl.h
arch/sh/oprofile/common.c +4 -2
-static int op_sh_create_files(struct super_block *sb, struct dentry *root)
+char *op_name_from_perf_id(void)
 {
-	int i, ret = 0;
+	const char *pmu;
+	char buf[20];
+	int size;
 

arch/sh/oprofile/common.c line 36

Quoted 10 lines
arch/sh/oprofile/common.c +4 -5
-	for (i = 0; i < model->num_counters; i++) {
-		struct dentry *dir;
-		char buf[4];
+	pmu = perf_pmu_name();
+	if (!pmu)
+		return NULL;
 
-		snprintf(buf, sizeof(buf), "%d", i);
-		dir = oprofilefs_mkdir(sb, root, buf);
+	size = snprintf(buf, sizeof(buf), "sh/%s", pmu);

Matt and Paul,

are those (upper case) cpu_type strings already supported by the oprofile userland?

-Robert

Quoted 20 lines
arch/sh/oprofile/common.c +3 -15
+	if (size > -1 && size < sizeof(buf))
+		return buf;
 
-		ret |= oprofilefs_create_ulong(sb, dir, "enabled", &ctr[i].enabled);
-		ret |= oprofilefs_create_ulong(sb, dir, "event", &ctr[i].event);
-		ret |= oprofilefs_create_ulong(sb, dir, "kernel", &ctr[i].kernel);
-		ret |= oprofilefs_create_ulong(sb, dir, "user", &ctr[i].user);
-
-		if (model->create_files)
-			ret |= model->create_files(sb, dir);
-		else
-			ret |= oprofilefs_create_ulong(sb, dir, "count", &ctr[i].count);
-
-		/* Dummy entries */
-		ret |= oprofilefs_create_ulong(sb, dir, "unit_mask", &ctr[i].unit_mask);
-	}
-
-	return ret;
+	return NULL;
 }
Signature · 2 lines
Advanced Micro Devices, Inc.
Operating System Research Center
#15 lore raw

Re: [PATCH 7/7] sh: oprofile: Use perf-events oprofile backend

Quoted 5 lines · Robert Richter

Matt and Paul,

are those (upper case) cpu_type strings already supported by the oprofile userland?

I've only ever used out-of-tree patches with it due to the generally volatile nature of support. Now that we have a more consistent interface to go through, it probably makes sense to get those bits merged, too. We're certainly not tied down to any ABI at this point.

#16 lore raw
In reply to Matt Fleming
Quoted 19 lines · Matt Fleming

The perf-events backend for OProfile that Will Deacon wrote in 8c1fc96f6fd1f361428ba805103af0d0eee65179 ("ARM: 6072/1: oprofile: use perf-events framework as backend") is of use to more architectures than just ARM. Move the code into drivers/oprofile/ so that SH can use it instead of the nearly identical copy of its OProfile code.

The benefit of the backend is that it becomes necessary to only maintain one copy of the PMU accessor functions for each architecture, with bug fixes and new features benefiting both OProfile and perf.

Matt Fleming (7):
perf: Add helper function to return number of counters
perf: New helper function for pmu name
oprofile: Make op_name_from_perf_id() global
ARM: oprofile: Rename op_arm to oprofile_perf
ARM: oprofile: Move non-ARM code into separate init/exit
oprofile: Abstract the perf-events backend
sh: oprofile: Use perf-events oprofile backend

Quoted 15 lines
 arch/arm/kernel/perf_event.c     |    6 +
 arch/arm/oprofile/Makefile       |    4 +
 arch/arm/oprofile/common.c       |  309 +-----------------------------------
 arch/sh/Kconfig                  |   13 ++
 arch/sh/kernel/perf_event.c      |   18 ++
 arch/sh/oprofile/Makefile        |    4 +
 arch/sh/oprofile/common.c        |  115 +++-----------
 arch/sh/oprofile/op_impl.h       |   33 ----
 drivers/oprofile/oprofile_perf.c |  326 ++++++++++++++++++++++++++++++++++++++
 include/linux/oprofile.h         |    7 +
 include/linux/perf_event.h       |    2 +
 kernel/perf_event.c              |    5 +
 12 files changed, 412 insertions(+), 430 deletions(-)
 delete mode 100644 arch/sh/oprofile/op_impl.h
 create mode 100644 drivers/oprofile/oprofile_perf.c

Thanks Matt, I applied your patches to the oprofile tree and merged them with the core branch. As I already wrote, I modified patch #4 to fix the compile breakage.

The patches are available for testing in

git://git.kernel.org/pub/scm/linux/kernel/git/rric/oprofile.git core

Thanks again,

-Robert

Signature · 2 lines
Advanced Micro Devices, Inc.
Operating System Research Center
#17 lore raw
Quoted 5 lines · Robert Richter

Thanks Matt, I applied your patches to the oprofile tree and merged them with the core branch. As I already wrote, I modified patch #4 to fix the compile breakage.

Quoted 7 lines

The patches are available for testing in

git://git.kernel.org/pub/scm/linux/kernel/git/rric/oprofile.git core

Thanks again,

-Robert

Thanks Robert, I've just read through the merge and it looks good!