perfcounters: record time running and time enabled for each counter
Impact: new functionality
Currently, if there are more counters enabled than can fit on the CPU, the kernel will multiplex the counters on to the hardware using round-robin scheduling. That isn't too bad for sampling counters, but for counting counters it means that the value read from a counter represents some unknown fraction of the true count of events that occurred while the counter was enabled.
This remedies the situation by keeping track of how long each counter is enabled for, and how long it is actually on the cpu and counting events. These times are recorded in nanoseconds using the task clock for per-task counters and the cpu clock for per-cpu counters.
These values can be supplied to userspace on a read from the counter. Userspace requests that they be supplied after the counter value by setting the PERF_FORMAT_TIME_ENABLED and/or PERF_FORMAT_TIME_RUNNING bits in the hw_event.read_format field when creating the counter. (There is no way to change the read format after the counter is created, though it would be possible to add some way to do that.)
Using this information it is possible for userspace to scale the count it reads from the counter to get an estimate of the true count:
true_count_estimate = count * time_enabled / time_running
This also lets userspace detect the situation where the counter never got to go on the cpu: time_running == 0.
This functionality has been requested by the PAPI developers, and will be generally needed for interpreting the count values from counting counters correctly.
In the implementation, this keeps 5 time values for each counter:time_enabled and time_running are used when the counter is in state OFF or ERROR and for reporting back to userspace. When the counter is in state INACTIVE or ACTIVE, it is the start_enabled, start_running and last_stopped values that are relevant, and time_enabled and time_running are determined from them. (last_stopped is only used in INACTIVE state.) The reason for doing it like this is that it means that only counters being enabled or disabled at sched-in and sched-out time need to be updated. There are no new loops that iterate over all counters to update time_enabled or time_running.
---
I'll rebase this on top of Peter's string of patches that have just gone in, but I'm posting this version to get peoples' comments on the implementation.
arch/powerpc/kernel/perf_counter.c +2 -0
@@ -454,6 +454,8 @@ static void counter_sched_in(struct perf_counter *counter, int cpu) { counter->state =PERF_COUNTER_STATE_ACTIVE ; counter->oncpu = cpu; + counter->start_running += counter->ctx->time_now - + counter->last_stopped;if (is_software_counter (counter)) counter->hw_ops->enable (counter); }
include/linux/perf_counter.h +18 -0
@@ -65,6 +65,16 @@ enum perf_counter_record_type { };/* +* Bits that can be set in hw_event.read_format to request that +* reads on the counter should return the indicated quantities, +* in increasing order of bit value, after the counter value. +*/ +enum perf_counter_read_format { +PERF_FORMAT_TIME_ENABLED =1 , +PERF_FORMAT_TIME_RUNNING =2 , +}; + +/* * Hardware event to monitor via a performance monitoring counter: */ struct perf_counter_hw_event {
@@ -196,6 +206,12 @@ struct perf_counter {enum perf_counter_active_state prev_state;atomic64_t count; +u64 time_enabled; +u64 time_running; +u64 start_enabled; +u64 start_running; +u64 last_stopped; +struct perf_counter_hw_event hw_event;struct hw_perf_counter hw;
@@ -251,6 +267,8 @@ struct perf_counter_context {int nr_active;int is_active;struct task_struct *task; +u64 time_now; +u64 time_lost;#endif };
kernel/perf_counter.c +109 -15
265 lines not on this page · raw message
Quoted 9 lines · Paul Mackerras
Impact: new functionality
Currently, if there are more counters enabled than can fit on the CPU, the kernel will multiplex the counters on to the hardware using round-robin scheduling. That isn't too bad for sampling counters, but for counting counters it means that the value read from a counter represents some unknown fraction of the true count of events that occurred while the counter was enabled.
Quoted 4 lines
This remedies the situation by keeping track of how long each counter is enabled for, and how long it is actually on the cpu and counting events. These times are recorded in nanoseconds using the task clock for per-task counters and the cpu clock for per-cpu counters.
Can't we do this by simply adding some software counters? A task local to each group and a task local on its own.
Since the solo task local will always be scheduled, you can get the sampling fraction by comparing the group task local one to the solo one.
Quoted 15 lines · Peter Zijlstra
Quoted 9 lines · Paul Mackerras
Impact: new functionality
Currently, if there are more counters enabled than can fit on the CPU, the kernel will multiplex the counters on to the hardware using round-robin scheduling. That isn't too bad for sampling counters, but for counting counters it means that the value read from a counter represents some unknown fraction of the true count of events that occurred while the counter was enabled.
Quoted 4 lines
This remedies the situation by keeping track of how long each counter is enabled for, and how long it is actually on the cpu and counting events. These times are recorded in nanoseconds using the task clock for per-task counters and the cpu clock for per-cpu counters.
Quoted 5 lines
Can't we do this by simply adding some software counters? A task local to each group and a task local on its own.
Since the solo task local will always be scheduled, you can get the sampling fraction by comparing the group task local one to the solo one.
You can get time_running that way but you can't really get time_enabled accurately in general, since there's no way to enable or disable two groups simultaneously. (Yes, in the special case where you are doing self-monitoring and you have no other counters, you can use prctl(PR_TASK_PERF_COUNTERS_{EN,DIS}ABLE), but that's not a general solution.)
The other thing is that the two extra software counters will add overhead, whereas my patch will add negligible overhead - just some integer arithmetic and accesses to cache lines which will already be in exclusive state due to the stores we're already doing to things such as counter->state.
Paul.
Quoted 14 lines · Paul Mackerras
Impact: new functionality
Currently, if there are more counters enabled than can fit on the CPU, the kernel will multiplex the counters on to the hardware using round-robin scheduling. That isn't too bad for sampling counters, but for counting counters it means that the value read from a counter represents some unknown fraction of the true count of events that occurred while the counter was enabled.
This remedies the situation by keeping track of how long each counter is enabled for, and how long it is actually on the cpu and counting events. These times are recorded in nanoseconds using the task clock for per-task counters and the cpu clock for per-cpu counters.
Quoted 6 lines
These values can be supplied to userspace on a read from the counter. Userspace requests that they be supplied after the counter value by setting the
PERF_FORMAT_TIME_ENABLEDand/orPERF_FORMAT_TIME_RUNNINGbits in the hw_event.read_format field when creating the counter. (There is no way to change the read format after the counter is created, though it would be possible to add some way to do that.)
[snip]
This would make the implementation of PAPI simpler than using software CPU timers attached to each group (which I have prototyped).
I'm a little confused about the ABI, though. I see that I can specify which times I want to be able to read, but the order that they appear in the stream seems to be unstated. I tried to figure it out by looking at
the kernel code, but there is no reference to PERF_FORMAT_TIME_ENABLED
or PERF_FORMAT_TIME_RUNNING in the patch (besides the definition).
Regards,
- Corey
Corey Ashford
Software Engineer
IBM Linux Technology Center, Linux Toolchain
Beaverton, OR
503-578-3507
[email protected]
Quoted 5 lines · Corey Ashford
I'm a little confused about the ABI, though. I see that I can specify which times I want to be able to read, but the order that they appear in the stream seems to be unstated. I tried to figure it out by looking at
the kernel code, but there is no reference toPERF_FORMAT_TIME_ENABLED
orPERF_FORMAT_TIME_RUNNINGin the patch (besides the definition).
Gah, I posted the wrong version of the patch. Sorry. I'll rebase and repost.
Paul.