Skip to contentSkip to conversation

perfcounters: add an mmap method to allow userspace to read hardware counters

PATCH In review Paul Mackerras 2 participants 5 messages Mar 17, 2009 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 perfcounters: add an mmap method to allow userspace to read hardware counters

Impact: new feature giving performance improvement

This adds the ability for userspace to do an mmap on a hardware counter fd and get access to a read-only page that contains the information needed to translate a hardware counter value to the full 64-bit counter value that would be returned by a read on the fd. This is useful on architectures that allow user programs to read the hardware counters, such as PowerPC.

The mmap will only succeed if the counter is a hardware counter monitoring the current process.

On my quad 2.5GHz PowerPC 970MP machine, userspace can read a counter and translate it to the full 64-bit value in about 30ns using the mmapped page, compared to about 830ns for the read syscall on the counter, so this does give a significant performance improvement.

---
 arch/powerpc/kernel/perf_counter.c |    6 +++
 include/linux/perf_counter.h       |   15 +++++++
 kernel/perf_counter.c              |   78 ++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 0 deletions(-)
arch/powerpc/kernel/perf_counter.c +6 -0
index 5008762..44271b6 100644
@@ -417,6 +417,8 @@ void hw_perf_restore(u64 disable)
 		atomic64_set(&counter->hw.prev_count, val);
 		counter->hw.idx = hwc_index[i] + 1;
 		write_pmc(counter->hw.idx, val);
+		if (counter->user_page)
+			perf_counter_update_userpage(counter);
 	}
 	mb();
 	cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
@@ -572,6 +574,8 @@ static void power_perf_disable(struct perf_counter *counter)
 			ppmu->disable_pmc(counter->hw.idx - 1, cpuhw->mmcr);
 			write_pmc(counter->hw.idx, 0);
 			counter->hw.idx = 0;
+			if (counter->user_page)
+				perf_counter_update_userpage(counter);
 			break;
 		}
 	}
@@ -732,6 +736,8 @@ static void record_and_restart(struct perf_counter *counter, long val,
 	write_pmc(counter->hw.idx, val);
 	atomic64_set(&counter->hw.prev_count, val);
 	atomic64_set(&counter->hw.period_left, left);
+	if (counter->user_page)
+		perf_counter_update_userpage(counter);
 
 	/*
 	 * Finally record data if requested.
include/linux/perf_counter.h +15 -0
index 08c11a6..3f9309f 100644
@@ -100,6 +100,17 @@ struct perf_counter_hw_event {
 #define PERF_COUNTER_IOC_ENABLE		_IO('$', 0)
 #define PERF_COUNTER_IOC_DISABLE	_IO('$', 1)
 
+/*
+ * Structure of the page that can be mapped via mmap
+ */
+struct perf_counter_mmap_page {
+	__u32	version;		/* version number of this structure */
+	__u32	compat_version;		/* lowest version this is compat with */
+	__u32	lock;			/* seqlock for synchronization */
+	__u32	index;			/* hardware counter identifier */
+	__s64	offset;			/* add to hardware counter value */
+};
+
 #ifdef __KERNEL__
 /*
  * Kernel-internal data types and definitions:
@@ -214,6 +225,9 @@ struct perf_counter {
 	int				oncpu;
 	int				cpu;
 
+	/* pointer to page shared with userspace via mmap */
+	unsigned long			user_page;
+
 	/* read() / irq related data */
 	wait_queue_head_t		waitq;
 	/* optional: for NMIs */
@@ -289,6 +303,7 @@ extern int perf_counter_task_enable(void);
 extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
 	       struct perf_cpu_context *cpuctx,
 	       struct perf_counter_context *ctx, int cpu);
+extern void perf_counter_update_userpage(struct perf_counter *counter);
 
 /*
  * Return 1 for a software counter, 0 for a hardware counter
kernel/perf_counter.c +78 -0

108 lines not on this page · raw message

Signature · 1 line
1.5.5.rc3.7.gba13
#1 lore raw
Quoted 9 lines · Paul Mackerras

Impact: new feature giving performance improvement

This adds the ability for userspace to do an mmap on a hardware counter fd and get access to a read-only page that contains the information needed to translate a hardware counter value to the full 64-bit counter value that would be returned by a read on the fd. This is useful on architectures that allow user programs to read the hardware counters, such as PowerPC.

Quoted 7 lines

The mmap will only succeed if the counter is a hardware counter monitoring the current process.

On my quad 2.5GHz PowerPC 970MP machine, userspace can read a counter and translate it to the full 64-bit value in about 30ns using the mmapped page, compared to about 830ns for the read syscall on the counter, so this does give a significant performance improvement.

While I think mmap'ed counters is a great idea, I really dont like this patch. It adds a second output format unrelated to the regular output format, and it doesn't appear to honor the regular output rules either. PERF_RECORD_GROUP thingies won't work for example.

Nor is there any kind of queuing, one might want to have multiple events in the mmap buffer..

I was planning to do this after cleaning up the normal output bits, as our current output stuff is a mess:
- its spread out over arch code (seems daft to me, we should all output the same)
- its useless for pretty much anything but the two apps we currently have

In particular, it lacks the tid information for sampled data I hinted to in the previous email.

Furthermore, in order to reliably profile userspace we need mmap information in the output stream as well.

#2 lore raw
Quoted 7 lines · Peter Zijlstra

While I think mmap'ed counters is a great idea, I really dont like this patch. It adds a second output format unrelated to the regular output format, and it doesn't appear to honor the regular output rules either. PERF_RECORD_GROUP thingies won't work for example.

Nor is there any kind of queuing, one might want to have multiple events in the mmap buffer..

I think you have misunderstood. This is not about sampling counters *at all*. This is about simple counting counters.

On powerpc, userspace can read the hardware counters directly. This stuff lets a program that is counting hardware events on itself do that and translate the result into a full 64-bit value. The information the program needs in order to do this is (a) which hardware counter (if any) has been assigned to this particular perf_counter and (b) what the offset between the hardware counter value and the full 64-bit perf_counter value is. That, plus a seqlock-style lock, is what's in the mmapped page.

Quoted 9 lines

I was planning to do this after cleaning up the normal output bits, as our current output stuff is a mess:
- its spread out over arch code (seems daft to me, we should all output the same)
- its useless for pretty much anything but the two apps we currently have

In particular, it lacks the tid information for sampled data I hinted to in the previous email.

Ingo has talked about reusing some of the tracing infrastructure for reporting perf_counter events to userspace. That sounds like an excellent idea to me, and that is why I didn't bother with putting the event queue into the mmapped page at this stage. If it makes sense to add it, it can be added later.

Paul.

#3 lore raw
Quoted 7 lines · Paul Mackerras
Quoted 5 lines · Peter Zijlstra

While I think mmap'ed counters is a great idea, I really dont like this patch. It adds a second output format unrelated to the regular output format, and it doesn't appear to honor the regular output rules either. PERF_RECORD_GROUP thingies won't work for example.

Quoted 5 lines
Quoted 2 lines

Nor is there any kind of queuing, one might want to have multiple events in the mmap buffer..

I think you have misunderstood. This is not about sampling counters *at all*. This is about simple counting counters.

I think I did indeed.

Quoted 8 lines

On powerpc, userspace can read the hardware counters directly. This stuff lets a program that is counting hardware events on itself do that and translate the result into a full 64-bit value. The information the program needs in order to do this is (a) which hardware counter (if any) has been assigned to this particular perf_counter and (b) what the offset between the hardware counter value and the full 64-bit perf_counter value is. That, plus a seqlock-style lock, is what's in the mmapped page.

Ah, right. I think some of the intel chips can do similar things with rdpmc instructions.

Quoted 7 lines
Quoted 7 lines

I was planning to do this after cleaning up the normal output bits, as our current output stuff is a mess:
- its spread out over arch code (seems daft to me, we should all output the same)
- its useless for pretty much anything but the two apps we currently have

Quoted 8 lines
Quoted 2 lines

In particular, it lacks the tid information for sampled data I hinted to in the previous email.

Ingo has talked about reusing some of the tracing infrastructure for reporting perf_counter events to userspace. That sounds like an excellent idea to me, and that is why I didn't bother with putting the event queue into the mmapped page at this stage. If it makes sense to add it, it can be added later.

Yeah, I've been looking into that, but so far I'm a bit at a loss, all that tracing stuff is per-cpu, and that's massive overkill for us, since we're dealing with single cpu streams.

One worry though, supposedly we want to mmap() such buffers too at some point, how would that interact with that you proposed?

#4 lore raw
Quoted 2 lines · Peter Zijlstra

Ah, right. I think some of the intel chips can do similar things with rdpmc instructions.

Ah OK, I didn't know that. Cool.

Quoted 9 lines
Quoted 5 lines

Ingo has talked about reusing some of the tracing infrastructure for reporting perf_counter events to userspace. That sounds like an excellent idea to me, and that is why I didn't bother with putting the event queue into the mmapped page at this stage. If it makes sense to add it, it can be added later.

Yeah, I've been looking into that, but so far I'm a bit at a loss, all that tracing stuff is per-cpu, and that's massive overkill for us, since we're dealing with single cpu streams.

I almost certainly know less about the tracing infrastructure than you. :) Per-cpu buffers does indeed sound like overkill for what we want - though we might actually want per-cpu buffers if we have inherited sampling counters and a deep process hierarchy.

Quoted 2 lines

One worry though, supposedly we want to mmap() such buffers too at some point, how would that interact with that you proposed?

I think we just have separate pages for the event ring buffer and the info for reading the counter in userspace, and then there isn't any interaction. We can do that either by having two fds and mmapping on each, or by using the file offset argument to mmap.

Paul.