perfcounters: Make s/w counters in a group only count when group is on
PATCH 1/1 perfcounters: Make s/w counters in a group only count when group is on
Impact: bug fix
If one has a group that has one or more hardware counters in it and also a software counter, the software counter should count only when the hardware counters are on the PMU. At present, the software counters count all the time, even when the hardware counters can't go on the PMU.
Fundamentally, the problem is that currently we are using sched-out for two distinct things: one is "take the counters off because this task is being taken off the CPU" and the other is "take the counters off because this group can't go on, or because we're changing things around". In the first case we need the software counters to keep counting, and in the second case we need them to stop, but currently we use sched_out functions for both.
To make things clearer, this uses "sched_out" for the first case and "inactivate" for the second case. So, adds counter_inactivate, group_inactivate and perf_counter_inactivate_ctx functions, which differ from the corresponding *_sched_out functions in that they always disable all the counters and put them into the inactive state.
The *_sched_out functions now are only used for the situation where a task is being taken off the CPU. If a counter has a sched_out function in its hw_ops structure, counter_sched_out will now call that and leave the counter in the active state (if it doesn't, it will call the counter's disable function and put it in the inactive state as before). Then, when the task gets scheduled in again, counter_sched_in will see that the counter is active and call the counter's sched_in function (if it has one) instead of the counter's enable function.
This means that we can add sched_out functions for the software counters which just do a counter update. The enable functions no longer need to check counter->prev_state, and in fact the prev_state field gets removed. Because the software counters work off per-task variables, they don't need sched_in functions.
This adds a perf_counter_activate_ctx function for symmetry with perf_counter_inactivate_ctx. It just calls __perf_counter_sched_in.
This also adds calls to group_inactivate in various places to make sure that a group gets inactivated if it can't go on the PMU. In future it would be useful to have an indication on the group leader as to whether there are any active counters in the group (the leader could be an inactive hardware counter but there could now be an active software counter in the group).
A corollary of all this is that it is now possible to have counters in active state when their context is inactive. Therefore this adds checks to perf_counter_read and __read that the context is active before calling the counter's read function.
It would have been possible to combine counter_inactive with counter_sched_out, group_inactivate with group_sched_out, etc., by passing down a 'disable_all' parameter. I made them separate functions because it seemed clearer in the callers and because this will make it easier to implement lazy PMU context-switching and caching of the PMU scheduling decisions in future.
This also fixes a bug where enabling a group would only put the group leader on the PMU, not the other group members, because __perf_counter_enable was calling counter_sched_in rather than group_sched_in.
---
This is in the master branch of my perfcounters.git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/paulus/perfcounters.git master
Ingo, please pull if it looks OK. Peter, I realize this will conflict with your generic software counter patches, but I think it's important that this bug gets fixed.
arch/powerpc/kernel/perf_counter.c | 8 +- include/linux/perf_counter.h | 2 + kernel/perf_counter.c | 216 +++++++++++++++++++++++------------- 3 files changed, 145 insertions(+), 81 deletions(-)
arch/powerpc/kernel/perf_counter.c +5 -3
@@ -452,10 +452,12 @@ static int collect_events(struct perf_counter *group, int max_count,static void counter_sched_in (struct perf_counter *counter,int cpu) { - counter->state =PERF_COUNTER_STATE_ACTIVE ; counter->oncpu = cpu; -if (is_software_counter (counter)) - counter->hw_ops->enable (counter); +if (counter->state ==PERF_COUNTER_STATE_INACTIVE ) { + counter->state =PERF_COUNTER_STATE_ACTIVE ; +if (is_software_counter (counter)) + counter->hw_ops->enable (counter); + } }/*
include/linux/perf_counter.h +2 -0
@@ -155,6 +155,8 @@ struct perf_counter;struct hw_perf_counter_ops {int (*enable) (struct perf_counter *counter);void (*disable) (struct perf_counter *counter); +int (*sched_in) (struct perf_counter *counter); +void (*sched_out) (struct perf_counter *counter);void (*read) (struct perf_counter *counter); };
kernel/perf_counter.c +138 -78
489 lines not on this page · raw message
Signature · 1 line
1.5.6.3
Quoted 7 lines · Paul Mackerras
Impact: bug fix
If one has a group that has one or more hardware counters in it and also a software counter, the software counter should count only when the hardware counters are on the PMU. At present, the software counters count all the time, even when the hardware counters can't go on the PMU.
Quoted 8 lines
Fundamentally, the problem is that currently we are using sched-out for two distinct things: one is "take the counters off because this task is being taken off the CPU" and the other is "take the counters off because this group can't go on, or because we're changing things around".
In the first case we need the software counters to keep counting, and in the second case we need them to stop, but currently we use
sched_outfunctions for both.
I'm not getting it.
The latter case first -- we seem to agree on that:
In group_sched_in() we first call hw_perf_group_sched_in(), which can validate if the group is at all schedulable.
Then we simply iterate the group counter list and add each counter, if one, for whatever reason, fails, we simply remove all counters of that group we scheduled in.
So we either schedule a full group or non of them.
The former case however, you seem to say we should keep software counters active even though their associated task is scheduled out? That doesn't appear to make sense to me.
Why would you want to do that?
Quoted 5 lines · Peter Zijlstra
The former case however, you seem to say we should keep software counters active even though their associated task is scheduled out? That doesn't appear to make sense to me.
Why would you want to do that?
Because the things that they are based on can get incremented when the task is scheduled out. This is most noticeable in the case of the context switch counter and also happens with the task migrations counter. These *always* get incremented when the task is scheduled out from the perf_counter subsystem's point of view, i.e. after perf_counter_task_sched_out is called for the task and before the next perf_counter_task_sched_in call. I believe page faults can also happen while the task is scheduled out, via access_process_vm.
I also originally thought that software counters should only count while their task is scheduled in, which is why I introduced the bug that I fixed in c07c99b67233ccaad38a961c17405dc1e1542aa4. That commit however left us with software counters that counted even when their group wasn't on; hence the current patch.
Paul.
Quoted 8 lines · Paul Mackerras
Quoted 5 lines · Peter Zijlstra
The former case however, you seem to say we should keep software counters active even though their associated task is scheduled out? That doesn't appear to make sense to me.
Why would you want to do that?
Quoted 7 lines
Because the things that they are based on can get incremented when the task is scheduled out. This is most noticeable in the case of the context switch counter and also happens with the task migrations counter. These *always* get incremented when the task is scheduled out from the
perf_countersubsystem's point of view, i.e. afterperf_counter_task_sched_outis called for the task and before the nextperf_counter_task_sched_incall.
Ah, I would rather special case these two counters than do what you did.
The issue I have with your approach is two-fold:
- it breaks the symmetry between software and hardware counters by treating them differently.
- it doesn't make much conceptual sense to me
For the context switch counter, we could count the event right before we schedule out, which would make it behave like expected.
The same for task migration, most migrations happen when they are in fact running, so there too we can account the migration either before we rip it off the src cpu, or after we place it on the dst cpu.
There are a few places where this isn't quite so, like affine wakeups, but there we can account after the placement.
Quoted 2 lines
I believe page faults can also
happen while the task is scheduled out, viaaccess_process_vm.
Very rare, and once could conceivably argue that the fault would then belong to the task doing access_process_vm().
Quoted 5 lines
I also originally thought that software counters should only count while their task is scheduled in, which is why I introduced the bug that I fixed in
c07c99b67233ccaad38a961c17405dc1e1542aa4. That commit however left us with software counters that counted even when their group wasn't on; hence the current patch.
Right.
Anyway, I would like to keep the behaviour that software and hardware counters are symmetric, that means disable them when their associated task goes.
Like stated above, we can fix these special cases by accounting in different places.
Quoted 3 lines · Peter Zijlstra
The same for task migration, most migrations happen when they are in fact running, so there too we can account the migration either before we rip it off the src cpu, or after we place it on the dst cpu.
Right, I got confused between being on the cpu and having ->state = R.
Migrations are the odd one out indeed, but I'd rather fudge a little with the migration counter itself than have this weird asymmetry.
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
The same for task migration, most migrations happen when they are in fact running, so there too we can account the migration either before we rip it off the src cpu, or after we place it on the dst cpu.
Quoted 6 lines
Right, I got confused between being on the cpu and having ->state = R.
Migrations are the odd one out indeed, but I'd rather fudge a little with the migration counter itself than have this weird asymmetry.
Agreed. There should really be no difference between software and hardware counters as far as the generic perfcounters code goes. It's a magic "metric" that gets read out somehow, and which generates events somehow.
We can have various grades of hardware versus software counters:
- 'pure hardware counters' where both the count and events come from some hw register
- 'pure software counters' where both the count and events are generated by software
- 'hybride counters' where for example the count might be from a hardware register, but the event is generated by a hrtimer (because the hardware is not capable of generating events).
the is_software_counter() assymetry broke this generally relaxed model of counters.
Ingo
Quoted 4 lines · Ingo Molnar
Agreed. There should really be no difference between software and hardware counters as far as the generic perfcounters code goes. It's a magic "metric" that gets read out somehow, and which generates events somehow.
And my patch didn't create any new difference in the core between software and hardware counters. It just gave the low-level code a way to distinguish between sched-out and disable events, and between sched-in and enable events - for any kind of counter.
That is useful now for making software counters behave correctly, and will IMO be useful in future for doing lazy PMU switching.
Quoted 8 lines
We can have various grades of hardware versus software counters:
- 'pure hardware counters' where both the count and events come from some hw register
- 'pure software counters' where both the count and events are generated by software
Quoted 6 lines
- 'hybride counters' where for example the count might be from a hardware register, but the event is generated by a hrtimer (because the hardware is not capable of generating events).
the
is_software_counter()assymetry broke this generally relaxed model of counters.
There are currently two asymmetries that I can see in the core relating to software vs. hardware groups:
- we don't bother checking with the CPU-specific low-level code whether a group with only software counters can go on; we assume it always can.
- the exclusive group mechanism only applies to hardware counters, since it is there to let the user do funky things with the PMU.
Do you see any other asymmetry? Note that the patch under discussion did *not* introduce any additional asymmetry (despite its possibly ill-chosen title :-).
Paul.
Quoted 3 lines · Peter Zijlstra
The issue I have with your approach is two-fold:
- it breaks the symmetry between software and hardware counters by treating them differently.
So... I was about to restore that symmetry by implementing lazy PMU context switching. In the case where we have inherited counters, and we are switching from one task to another that both have the same set of inherited counters, we don't really need to do anything, because it doesn't matter which set of counters the events get added into, because they all get added together at the end anyway.
That is another situation where you can have counters that are active when their associated task is not scheduled in, this time for hardware counters as well as software counters. So this is not just some weird special case for software counters, but is actually going to be more generally useful.
Quoted 1 line
- it doesn't make much conceptual sense to me
It seems quite reasonable to me that things could happen that are attributable to a task, but which happen when the task isn't running. Not just context switches and migrations - there's a whole class of things that the system does on behalf of a process that can happen asynchronously. I wouldn't want to say that those kind of things can never be counted with software counters.
Quoted 9 lines
For the context switch counter, we could count the event right before we schedule out, which would make it behave like expected.
The same for task migration, most migrations happen when they are in fact running, so there too we can account the migration either before we rip it off the src cpu, or after we place it on the dst cpu.
There are a few places where this isn't quite so, like affine wakeups, but there we can account after the placement.
Right - but how do you know whether to do that accounting or not? At the moment there simply isn't enough state information in the counter to tell you whether or not you should be adding in those things that happened while the task wasn't running. At the moment you can't tell whether a counter is inactive merely because its task is scheduled out, or because it's in a group that won't currently fit on the PMU.
By the way, I notice that x86 will do the wrong thing if you have a group where the leader is an interrupting hardware counter with record_type == PERF_RECORD_GROUP and there is a software counter in the group, because perf_handle_group calls x86_perf_counter_update on each group member unconditionally, and x86_perf_counter_update assumes its argument is a hardware counter.
Paul.
Quoted 8 lines · Paul Mackerras
Quoted 3 lines · Peter Zijlstra
The issue I have with your approach is two-fold:
- it breaks the symmetry between software and hardware counters by treating them differently.So... I was about to restore that symmetry by implementing lazy PMU context switching. [...]
That would be _really_ neat to have.
Ingo
Quoted 6 lines · Paul Mackerras
Quoted 3 lines · Peter Zijlstra
The issue I have with your approach is two-fold:
- it breaks the symmetry between software and hardware counters by treating them differently.
Quoted 6 lines
So... I was about to restore that symmetry by implementing lazy PMU context switching. In the case where we have inherited counters, and we are switching from one task to another that both have the same set of inherited counters, we don't really need to do anything, because it doesn't matter which set of counters the events get added into, because they all get added together at the end anyway.
That is only true for actual counting counters, not the sampling kind.
Quoted 6 lines
That is another situation where you can have counters that are active when their associated task is not scheduled in, this time for hardware counters as well as software counters. So this is not just some weird special case for software counters, but is actually going to be more generally useful.
Quoted 8 lines
Quoted 1 line
- it doesn't make much conceptual sense to me
It seems quite reasonable to me that things could happen that are attributable to a task, but which happen when the task isn't running. Not just context switches and migrations - there's a whole class of things that the system does on behalf of a process that can happen asynchronously. I wouldn't want to say that those kind of things can never be counted with software counters.
I've been thinking too much about sampling I think. It makes absolutely no sense in that light to have events that occur when the task isn't running, quite simply because its impossible to relate it to whatever the task is doing at that moment.
However for simple counting events it might make sense to have something like that.
Still HW counters can simply never do anything like that, and the lazy PMU thing you propose, while cool for simple stuff like perfstat, is something all-together different -- it doesn't keep counters enabled while their task is gone from the cpu, it avoids a counter update between related tasks.
Quoted 10 lines
Quoted 9 lines
For the context switch counter, we could count the event right before we schedule out, which would make it behave like expected.
The same for task migration, most migrations happen when they are in fact running, so there too we can account the migration either before we rip it off the src cpu, or after we place it on the dst cpu.
There are a few places where this isn't quite so, like affine wakeups, but there we can account after the placement.
Quoted 6 lines
Right - but how do you know whether to do that accounting or not? At the moment there simply isn't enough state information in the counter to tell you whether or not you should be adding in those things that happened while the task wasn't running. At the moment you can't tell whether a counter is inactive merely because its task is scheduled out, or because it's in a group that won't currently fit on the PMU.
Well, for things like the migration count its easy, we already always count those, so fudging the perf counter interface isn't too hard. Other things, yeah, that'll be a tad tricky.
Quoted 6 lines
By the way, I notice that x86 will do the wrong thing if you have a group where the leader is an interrupting hardware counter with
record_type==PERF_RECORD_GROUPand there is a software counter in the group, becauseperf_handle_groupcallsx86_perf_counter_updateon each group member unconditionally, andx86_perf_counter_updateassumes its argument is a hardware counter.
Ah, right, I fixed that for the generic swcounter stuff but then didn't do the x86 part.. d'oh.
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines
So... I was about to restore that symmetry by implementing lazy PMU context switching. In the case where we have inherited counters, and we are switching from one task to another that both have the same set of inherited counters, we don't really need to do anything, because it doesn't matter which set of counters the events get added into, because they all get added together at the end anyway.
That is only true for actual counting counters, not the sampling kind.
Hmmm... I don't think inherited sampling counters work at present anyway. :) The events for a child process will go into the child struct perf_counter, and the code doesn't currently provide any way to read them out (unless I missed something).
Quoted 12 lines
Quoted 6 lines
It seems quite reasonable to me that things could happen that are attributable to a task, but which happen when the task isn't running. Not just context switches and migrations - there's a whole class of things that the system does on behalf of a process that can happen asynchronously. I wouldn't want to say that those kind of things can never be counted with software counters.
I've been thinking too much about sampling I think. It makes absolutely no sense in that light to have events that occur when the task isn't running, quite simply because its impossible to relate it to whatever the task is doing at that moment.
Quoted 8 lines
However for simple counting events it might make sense to have something like that.
Still HW counters can simply never do anything like that, and the lazy PMU thing you propose, while cool for simple stuff like perfstat, is something all-together different -- it doesn't keep counters enabled while their task is gone from the cpu, it avoids a counter update between related tasks.
As an implementation detail, I think we could get the situation where a counter is active but its task isn't running in two cases: software counters that count while their task is switched out, and hardware counters that have been lazily left running past a context switch. I was trying to handle both cases in a similar manner.
However, your new software counter code seems to be doing the right thing with a task clock counter in a group that also has a hardware counter, so my patch is no longer required. But, I notice that the counter->prev_state thing is still there. It would be nice to get rid of that.
Paul.