Skip to contentSkip to conversation

perf_counter: Add support for pinned and exclusive counter groups

RFC PATCH In review Paul Mackerras 2 participants 7 messages Jan 14, 2009 – Jan 15, 2009 PowerPC

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 perf_counter: Add support for pinned and exclusive counter groups

Impact: New perf_counter features

A pinned counter group is one that the user wants to have on the CPU whenever possible, i.e. whenever the associated task is running, for a per-task group, or always for a per-cpu group. If the system cannot satisfy that, it puts the group into an error state where it is not scheduled any more and reads from it return EOF (i.e. 0 bytes read). The group can be released from error state and made readable again using prctl(PR_TASK_PERF_COUNTERS_ENABLE). When we have finer-grained enable/disable controls on counters we'll be able to reset the error state on individual groups.

An exclusive group is one that the user wants to be the only group using the CPU performance monitor hardware whenever it is on. The counter group scheduler will not schedule an exclusive group if there are already other groups on the CPU and will not schedule other groups onto the CPU if there is an exclusive group scheduled (that statement does not apply to groups containing only software counters, which can always go on and which do not prevent an exclusive group from going on). With an exclusive group, we will be able to let users program PMU registers at a low level without the concern that those settings will perturb other measurements.

Along the way this reorganizes things a little:
- is_software_counter() is moved to perf_counter.h.
- cpuctx->active_oncpu now records the number of hardware counters on the CPU, i.e. it now excludes software counters. Nothing was reading cpuctx->active_oncpu before, so this change is harmless.
- A new cpuctx->exclusive field records whether we currently have an exclusive group on the CPU.
- counter_sched_out moves higher up in perf_counter.c and gets called from __perf_counter_remove_from_context and __perf_counter_exit_task, where we used to have essentially the same code.
- __perf_counter_sched_in now goes through the counter list twice, doing the pinned counters in the first loop and the non-pinned counters in the second loop, in order to give the pinned counters the best chance to be scheduled in.

Note that only a group leader can be exclusive or pinned, and that attribute applies to the whole group. This avoids some awkwardness in some corner cases (e.g. where a group leader is closed and the other group members get added to the context list). If we want to relax that restriction later, we can, and it is easier to relax a restriction than to apply a new one.

---
What this doesn't handle is when a pinned counter gets inherited and goes into error state in the child. The sensible thing would be to put the parent counter into error state in __perf_counter_exit_task, but that might mean taking it off the PMU on some other CPU, and I was nervous about doing anything substantial to parent_counter. Maybe what we need instead of an error value for counter->state is a separate error flag that can be set atomically by exiting children. BTW, I think we need an smp_wmb after updating parent_counter->count, so that when the parent sees the child has exited it is guaranteed to see the updated ->count value.

arch/powerpc/kernel/perf_counter.c +1 -9
index 85ad259..5b02113 100644
@@ -36,14 +36,6 @@ void perf_counter_print_debug(void)
 }
 
 /*
- * Return 1 for a software counter, 0 for a hardware counter
- */
-static inline int is_software_counter(struct perf_counter *counter)
-{
-	return !counter->hw_event.raw && counter->hw_event.type < 0;
-}
-
-/*
  * Read one performance monitor counter (PMC).
  */
 static unsigned long read_pmc(int idx)
@@ -443,6 +435,7 @@ int hw_perf_group_sched_in(struct perf_counter *group_leader,
 	 */
 	for (i = n0; i < n0 + n; ++i)
 		cpuhw->counter[i]->hw.config = cpuhw->events[i];
+	cpuctx->active_oncpu += n;
 	n = 1;
 	counter_sched_in(group_leader, cpu);
 	list_for_each_entry(sub, &group_leader->sibling_list, list_entry) {
@@ -451,7 +444,6 @@ int hw_perf_group_sched_in(struct perf_counter *group_leader,
 			++n;
 		}
 	}
-	cpuctx->active_oncpu += n;
 	ctx->nr_active += n;
 
 	return 1;
include/linux/perf_counter.h +14 -1
index b21d1ea..7ab8e5f 100644
@@ -86,7 +86,10 @@ struct perf_counter_hw_event {
 				nmi	     :  1, /* NMI sampling        */
 				raw	     :  1, /* raw event type      */
 				inherit	     :  1, /* children inherit it */
-				__reserved_1 : 28;
+				pinned	     :  1, /* must always be on PMU */
+				exclusive    :  1, /* only counter on PMU */
+
+				__reserved_1 : 26;
 
 	u64			__reserved_2;
 };
@@ -141,6 +144,7 @@ struct hw_perf_counter_ops {
  * enum perf_counter_active_state - the states of a counter
  */
 enum perf_counter_active_state {
+	PERF_COUNTER_STATE_ERROR	= -2,
 	PERF_COUNTER_STATE_OFF		= -1,
 	PERF_COUNTER_STATE_INACTIVE	=  0,
 	PERF_COUNTER_STATE_ACTIVE	=  1,
@@ -214,6 +218,7 @@ struct perf_cpu_context {
 	struct perf_counter_context	*task_ctx;
 	int				active_oncpu;
 	int				max_pertask;
+	int				exclusive;
 };
 
 /*
@@ -240,6 +245,14 @@ extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
 	       struct perf_cpu_context *cpuctx,
 	       struct perf_counter_context *ctx, int cpu);
 
+/*
+ * Return 1 for a software counter, 0 for a hardware counter
+ */
+static inline int is_software_counter(struct perf_counter *counter)
+{
+	return !counter->hw_event.raw && counter->hw_event.type < 0;
+}
+
 #else
 static inline void
 perf_counter_task_sched_in(struct task_struct *task, int cpu)		{ }
kernel/perf_counter.c +154 -72

360 lines not on this page · raw message

#1 lore raw
Quoted 11 lines · Paul Mackerras

Impact: New perf_counter features

A pinned counter group is one that the user wants to have on the CPU whenever possible, i.e. whenever the associated task is running, for a per-task group, or always for a per-cpu group. If the system cannot satisfy that, it puts the group into an error state where it is not scheduled any more and reads from it return EOF (i.e. 0 bytes read). The group can be released from error state and made readable again using prctl(PR_TASK_PERF_COUNTERS_ENABLE). When we have finer-grained enable/disable controls on counters we'll be able to reset the error state on individual groups.

ok, that looks like quite sensible semantics.

Quoted 10 lines

An exclusive group is one that the user wants to be the only group using the CPU performance monitor hardware whenever it is on. The counter group scheduler will not schedule an exclusive group if there are already other groups on the CPU and will not schedule other groups onto the CPU if there is an exclusive group scheduled (that statement does not apply to groups containing only software counters, which can always go on and which do not prevent an exclusive group from going on). With an exclusive group, we will be able to let users program PMU registers at a low level without the concern that those settings will perturb other measurements.

ok, this sounds good too. The disadvantage is the reduction in utility. Actual applications and users will decide which variant is more useful in practice - it does not complicate the design unreasonably.

Quoted 5 lines

Along the way this reorganizes things a little:
- is_software_counter() is moved to perf_counter.h.
- cpuctx->active_oncpu now records the number of hardware counters on the CPU, i.e. it now excludes software counters. Nothing was reading cpuctx->active_oncpu before, so this change is harmless.

(btw., the percpu allocation code seems to have bitrotten a bit - the logic around perf_reserved_percpu looks wrong and somewhat complicated.)

Quoted 9 lines

- A new cpuctx->exclusive field records whether we currently have an exclusive group on the CPU.
- counter_sched_out moves higher up in perf_counter.c and gets called from __perf_counter_remove_from_context and __perf_counter_exit_task, where we used to have essentially the same code.
- __perf_counter_sched_in now goes through the counter list twice, doing the pinned counters in the first loop and the non-pinned counters in the second loop, in order to give the pinned counters the best chance to be scheduled in.

hm, i guess this could be improved: by queueing pinned counters in front of the list and unpinned counters to the tail.

Quoted 6 lines

Note that only a group leader can be exclusive or pinned, and that attribute applies to the whole group. This avoids some awkwardness in some corner cases (e.g. where a group leader is closed and the other group members get added to the context list). If we want to relax that restriction later, we can, and it is easier to relax a restriction than to apply a new one.

yeah, agreed.

Quoted 10 lines

What this doesn't handle is when a pinned counter gets inherited and goes into error state in the child. The sensible thing would be to put the parent counter into error state in __perf_counter_exit_task, but that might mean taking it off the PMU on some other CPU, and I was nervous about doing anything substantial to parent_counter. Maybe what we need instead of an error value for counter->state is a separate error flag that can be set atomically by exiting children. BTW, I think we need an smp_wmb after updating parent_counter->count, so that when the parent sees the child has exited it is guaranteed to see the updated ->count value.

Yeah. Such artifacts at inheritance stem from the reduction in utility that comes from any exclusive-resource-usage scheme, and are expected.

For example, right now it works just fine to nest 'timec' in itself [there is a reduction in statistical value if we start round-robining, but there's still full utility]. If it used exclusive or pinned counters that might not work.

Again, which restrictions users/developers are more willing to live with will be shown in actual usage of these facilities.

Ingo

#2 lore raw
Quoted 7 lines · Ingo Molnar

Yeah. Such artifacts at inheritance stem from the reduction in utility that comes from any exclusive-resource-usage scheme, and are expected.

For example, right now it works just fine to nest 'timec' in itself [there is a reduction in statistical value if we start round-robining, but there's still full utility]. If it used exclusive or pinned counters that might not work.

for example, this measurement works just fine currently:

aldebaran:~/linux/linux> timec -e 1 timec -e 1 timec -e 1 make -j16 kernel/
[...]
Performance counter stats for 'make':

59561.510176 task clock ticks (msecs)

    92223201071  instructions         (events)

Wall-clock time elapsed: 6016.011818 msecs

Performance counter stats for 'timec':

59562.300425 task clock ticks (msecs)

    92223534177  instructions         (events)

Wall-clock time elapsed: 6017.117985 msecs

Performance counter stats for 'timec':

59563.307588 task clock ticks (msecs)

    92223867295  instructions         (events)

Wall-clock time elapsed: 6018.590227 msecs

The performance counters are layered upon each other in each nested timec instance. In the innermost instance ls runs with 3+3 performance counters active at once - and it all just works, without the timec apps having any knowledge about each other.

And that is i think what powerful kernel abstractions are about. If we provide nice and useful abstractions then the hw folks will eventually extend the hw side resources, if users keep bumping into them.

If we structure our design around the limitations then users wont actually ever bump into the hw limitations in a clear-cut way - they will bump into our design. So they'll pester us, not the hw folks ;-)

Such details are the main difference between the perfmon design and the perfcounters design.

Ingo

#3 lore raw
In reply to Ingo Molnar
Quoted 2 lines · Ingo Molnar

(btw., the percpu allocation code seems to have bitrotten a bit - the logic around perf_reserved_percpu looks wrong and somewhat complicated.)

Yes, it has. I don't think it was ever fixed to apply only to hardware counters when software counters were added.

Side question - were you intending to make the various software counters able to act as interrupting counters? That will mean e.g. that anything that increments current->maj_flt or current->min_flt (i.e. do_page_fault, __get_user_pages) will need to check if that causes any counter to overflow, which could be a bit invasive.

Quoted 2 lines

hm, i guess this could be improved: by queueing pinned counters in front of the list and unpinned counters to the tail.

Yes - and there are other little optimizations we could do too, such as recording the number of hardware counters in the group leader's struct perf_counter.

Quoted 11 lines
Quoted 10 lines

What this doesn't handle is when a pinned counter gets inherited and goes into error state in the child. The sensible thing would be to put the parent counter into error state in __perf_counter_exit_task, but that might mean taking it off the PMU on some other CPU, and I was nervous about doing anything substantial to parent_counter. Maybe what we need instead of an error value for counter->state is a separate error flag that can be set atomically by exiting children. BTW, I think we need an smp_wmb after updating parent_counter->count, so that when the parent sees the child has exited it is guaranteed to see the updated ->count value.

Quoted 2 lines

Yeah. Such artifacts at inheritance stem from the reduction in utility that comes from any exclusive-resource-usage scheme, and are expected.

Hmmm, I don't see pinning as being about exclusivity, and the reason my patch didn't handle it was because I haven't completely got my head around the relevant lifetime and locking rules.

Quoted 4 lines

For example, right now it works just fine to nest 'timec' in itself [there is a reduction in statistical value if we start round-robining, but there's still full utility]. If it used exclusive or pinned counters that might not work.

Should be fine with pinned counters, and even with exclusive counters as long as the counters are in one group.

Quoted 2 lines

Again, which restrictions users/developers are more willing to live with will be shown in actual usage of these facilities.

Indeed.

Regards,
Paul.

#4 lore raw
Quoted 5 lines · Paul Mackerras
Quoted 2 lines · Ingo Molnar

(btw., the percpu allocation code seems to have bitrotten a bit - the logic around perf_reserved_percpu looks wrong and somewhat complicated.)

Quoted 8 lines

Yes, it has. I don't think it was ever fixed to apply only to hardware counters when software counters were added.

Side question - were you intending to make the various software counters able to act as interrupting counters? That will mean e.g. that anything that increments current->maj_flt or current->min_flt (i.e. do_page_fault, __get_user_pages) will need to check if that causes any counter to overflow, which could be a bit invasive.

Yes, eventually my intention was to bring all the sw counters up to that level, and allow system sampling via sw counter overflows. It's an arguably powerful concept.

I first wanted to see where they all go though, and how many of them we want, and how nuanced we want to make them.

Initially we can sample via __builtin_return_address(0) addresses, or pt_regs(current)->rip or so. Later on we could use save_stack_trace() perhaps, for a more vectored sample.

It would result in some rather cool kerneltop output: we could see a profile of which functions generate pagefaults, or which places generate context-switches, which places migrate a lot, etc.

Quoted 4 lines
Quoted 2 lines

Again, which restrictions users/developers are more willing to live with will be shown in actual usage of these facilities.

Indeed.

So my approach was always to have a good guess about what the best usage pattern is, but also to not be rigid and ignore/exclude other usecases. The main principle of this subsystem is to be maximally useful in performance analysis, via modern and flexible abstractions. Pinning and exclusivity fits into that just fine - they are mechanism to improve the quality of the statistics.

Ingo

#5 lore raw
In reply to Paul Mackerras

This patch is in the rfc branch of my perfcounters.git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/paulus/perfcounters.git rfc

Ingo - you can pull it into your perfcounters branch if you like.

Paul.

#6 lore raw
Quoted 5 lines · Paul Mackerras

This patch is in the rfc branch of my perfcounters.git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/paulus/perfcounters.git rfc

Ingo - you can pull it into your perfcounters branch if you like.

Pulled into tip/perfcounters/core, thanks Paul!

Ingo