perf: Add CPU hotplug support for events
cover perf: Add CPU hotplug support for events
The embedded world, specifically Android mobile SoCs, rely on CPU hotplugs to manage power and thermal constraints. These hotplugs can happen at a very rapid pace. Adjacently, they also relies on many perf event counters for its management. Therefore, there is a need to preserve these events across hotplugs.
In such a scenario, a perf client (kernel or user-space) can create events even when the CPU is offline. If the CPU comes online during the lifetime of the event, the registered event can start counting spontaneously. As an extension to this, the events' count can also be preserved across CPU hotplugs. This takes the burden off of the clients to monitor the state of the CPU.
The tests were conducted on x86_64 PC. Sample results:
/* CPU-1 is offline: Event created when CPU is offline */
# ./perf stat -C 1 -e cycles -v -I 1000
Using CPUID GenuineIntel-6-5E
cycles: 0 0 0
# time counts unit events
1.000143727 <not counted> cycles
cycles: 0 0 0
2.000309394 <not counted> cycles
cycles: 0 0 0
3.000466412 <not counted> cycles
cycles: 0 0 0
4.000599885 <not counted> cycles
cycles: 0 0 0
5.000750095 <not counted> cycles
cycles: 0 0 0
6.000901789 <not counted> cycles
cycles: 0 0 0
7.001035315 <not counted> cycles
cycles: 0 0 0
8.001195270 <not counted> cycles/* CPU-1 made online: Event started counting */
cycles: 21046256 719464572 719464572
9.001379820 21,046,256 cycles
cycles: 19537953 1000222653 1000222653
10.001634052 19,537,953 cycles
cycles: 27480135 1000095468 1000095468
11.001764556 27,480,135 cycles
cycles: 27723233 1000108008 1000108008
12.001909310 27,723,233 cycles
cycles: 19137349 1000133462 1000133462
13.002079041 19,137,349 cycles
cycles: 48913285 1000101268 1000101268
14.002218928 48,913,285 cycles
cycles: 27259199 1000128972 1000128972
15.002388700 27,259,199 cycles
cycles: 19249055 1000095789 1000095789
16.002514765 19,249,055 cycles
cycles: 27530051 1000246860 1000246860
17.002801610 27,530,051 cycles
cycles: 34348072 1000272874 1000272874
18.003110100 34,348,072 cycles
cycles: 11526457 107984264 107984264
19.003435811 11,526,457 cycles/* CPU-1 made offline */
cycles: 0 0 0
20.003582803 <not counted> cycles
cycles: 0 0 0
21.003896484 <not counted> cycles
cycles: 0 0 0
22.004212989 <not counted> cycles
cycles: 0 0 0
23.004346689 <not counted> cycles
cycles: 0 0 0
24.004668259 <not counted> cycles
cycles: 0 0 0
25.004983504 <not counted> cycles
cycles: 0 0 0
# time counts unit events
26.005315741 <not counted> cycles/* CPU-1 made online: Event preserved across hotplug */
cycles: 27210082 933493459 933493459
27.005652287 27,210,082 cycles
cycles: 41950431 1000112865 1000112865
28.005805475 41,950,431 cycles
cycles: 35075124 1000141146 1000141146
29.005974101 35,075,124 cycles
cycles: 45240055 1000132743 1000132743
30.006140008 45,240,055 cycles
cycles: 43426180 1000077828 1000077828
31.006253035 43,426,180 cycles
cycles: 34593167 1000315835 1000315835
32.006605393 34,593,167 cycles
cycles: 105078270 1000136171 1000136171
33.006773971 105,078,270 cyclesRaghavendra Rao Ananta (1):
perf: Add CPU hotplug support for events
include/linux/perf_event.h | 7 +++ kernel/events/core.c | 123 +++++++++++++++++++++++++++++++++------------ 2 files changed, 97 insertions(+), 33 deletions(-)
--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 1/1 perf: Add CPU hotplug support for events
Perf framework doesn't allow prevserving CPU events across CPU hotplugs. The events are scheduled out as and when the CPU walks offline. Moreover, the framework also doesn't allow the clients to create events on an offline CPU. As a result, the clients have to keep on monitoring the CPU state until it comes back online.
Therefore, introducing the perf framework to support creation and preserving of (CPU) events for offline CPUs. Through this, the CPU's online state would be transparent to the client and it not have to worry about monitoring the CPU's state. Success would be returned to the client even while creating the event on an offline CPU. If during the lifetime of the event the CPU walks offline, the event would be preserved and would continue to count as soon as (and if) the CPU comes back online.
--- include/linux/perf_event.h | 7 +++ kernel/events/core.c | 123 +++++++++++++++++++++++++++++++++------------ 2 files changed, 97 insertions(+), 33 deletions(-)
include/linux/perf_event.h +7 -0
@@ -489,6 +489,7 @@ struct perf_addr_filters_head {* enum perf_event_state - the states of a event */ enum perf_event_state { +PERF_EVENT_STATE_DORMANT =-5 ,PERF_EVENT_STATE_DEAD =-4 ,PERF_EVENT_STATE_EXIT =-3 ,PERF_EVENT_STATE_ERROR =-2 ,
@@ -687,6 +688,12 @@ struct perf_event {#endif struct list_head sb_list; + +/* Entry into the list that holds the events whose CPUs +* are offline. These events will be removed from the +* list and installed once the CPU wakes up. +*/ +struct list_head dormant_entry;#endif /* CONFIG_PERF_EVENTS */ };
kernel/events/core.c +90 -33
178 lines not on this page · raw message
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Peter Zijlstra
maintainerQuoted 36 lines · Raghavendra Rao Ananta
Perf framework doesn't allow prevserving CPU events across CPU hotplugs. The events are scheduled out as and when the CPU walks offline. Moreover, the framework also doesn't allow the clients to create events on an offline CPU. As a result, the clients have to keep on monitoring the CPU state until it comes back online.
Therefore, introducing the perf framework to support creation and preserving of (CPU) events for offline CPUs. Through this, the CPU's online state would be transparent to the client and it not have to worry about monitoring the CPU's state. Success would be returned to the client even while creating the event on an offline CPU. If during the lifetime of the event the CPU walks offline, the event would be preserved and would continue to count as soon as (and if) the CPU comes back online.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- include/linux/perf_event.h | 7 +++ kernel/events/core.c | 123 +++++++++++++++++++++++++++++++++------------ 2 files changed, 97 insertions(+), 33 deletions(-)include/linux/perf_event.h +1
-0@@ -489,6 +489,7 @@ struct perf_addr_filters_head {* enum perf_event_state - the states of a event */ enum perf_event_state { +PERF_EVENT_STATE_DORMANT =-5 ,PERF_EVENT_STATE_DEAD =-4 ,PERF_EVENT_STATE_EXIT =-3 ,PERF_EVENT_STATE_ERROR =-2 ,
include/linux/perf_event.h struct perf_event { · line 696
Quoted 9 lines
include/linux/perf_event.h +6
-0@@ -687,6 +688,12 @@ struct perf_event {#endif struct list_head sb_list; + +/* Entry into the list that holds the events whose CPUs +* are offline. These events will be removed from the +* list and installed once the CPU wakes up. +*/ +struct list_head dormant_entry;
No this is absolutely disguisting. You can simply keep the events in the dead CPU's context. It's really not that hard.
Also, you _still_ don't explain why you care about dead CPUs.
Quoted 39 lines · Peter Zijlstra
Quoted 38 lines · Raghavendra Rao Ananta
Perf framework doesn't allow prevserving CPU events across CPU hotplugs. The events are scheduled out as and when the CPU walks offline. Moreover, the framework also doesn't allow the clients to create events on an offline CPU. As a result, the clients have to keep on monitoring the CPU state until it comes back online.
Therefore, introducing the perf framework to support creation and preserving of (CPU) events for offline CPUs. Through this, the CPU's online state would be transparent to the client and it not have to worry about monitoring the CPU's state. Success would be returned to the client even while creating the event on an offline CPU. If during the lifetime of the event the CPU walks offline, the event would be preserved and would continue to count as soon as (and if) the CPU comes back online.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- include/linux/perf_event.h | 7 +++ kernel/events/core.c | 123 +++++++++++++++++++++++++++++++++------------ 2 files changed, 97 insertions(+), 33 deletions(-)include/linux/perf_event.h +1
-0@@ -489,6 +489,7 @@ struct perf_addr_filters_head {* enum perf_event_state - the states of a event */ enum perf_event_state { +PERF_EVENT_STATE_DORMANT =-5 ,PERF_EVENT_STATE_DEAD =-4 ,PERF_EVENT_STATE_EXIT =-3 ,PERF_EVENT_STATE_ERROR =-2 ,@@ -687,6 +688,12 @@ struct perf_event {#endif
Quoted 10 lines
include/linux/perf_event.h struct perf_event { · line 696
Quoted 7 lines
include/linux/perf_event.h +6
-0struct list_head sb_list; + +/* Entry into the list that holds the events whose CPUs +* are offline. These events will be removed from the +* list and installed once the CPU wakes up. +*/ +struct list_head dormant_entry;No this is absolutely disguisting. You can simply keep the events in the dead CPU's context. It's really not that hard.
Keeping the events in the dead CPU's context was also an idea that we had. However, detaching that event from the PMU when the CPU is offline would be a pain. Consider the scenario in which an event is about to be destroyed when the CPU is offline (yet still attached to the CPU). During it's destruction, a cross-cpu call is made (from perf_remove_from_context()) to the offlined CPU to detach the event from the CPU's PMU. As the CPU is offline, that would not be possible, and again a separate logic has to be written for cleaning up the events whose CPUs are offlined. Hence, I thought it would be a cleaner way to maintain the events.
Quoted 3 lines
Also, you _still_ don't explain why you care about dead CPUs.
It's just not only about dead CPUs. It's the fact that the CPUs can come and go online. The embedded world, specifically Android mobile SoCs, rely on CPU hotplugs to manage power and thermal constraints. These hotplugs can happen at a very rapid pace. Adjacently, they also rely on many perf event counters for its management. Therefore, there is a need to preserve these events across hotplugs.
In such a scenario, a perf client (kernel or user-space) can create
events even when the CPU is offline. If the CPU comes online during
the lifetime of the event, the registered event can start counting
spontaneously. As an extension to this, the events' count can also
be preserved across CPU hotplugs. This takes the burden off of the
clients to monitor the state of the CPU.
-- Raghavendra
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Peter Zijlstra
maintainerQuoted 10 lines · Raghavendra Rao Ananta
Quoted 2 lines
No this is absolutely disguisting. You can simply keep the events in the dead CPU's context. It's really not that hard.
Keeping the events in the dead CPU's context was also an idea that we had. However, detaching that event from the PMU when the CPU is offline would be a pain. Consider the scenario in which an event is about to be destroyed when the CPU is offline (yet still attached to the CPU). During it's destruction, a cross-cpu call is made (from
perf_remove_from_context()) to the offlined CPU to detach the event from the CPU's PMU. As the CPU is offline, that would not be possible, and again a separate logic has to be written for cleaning up the events whose CPUs are offlined.
That is actually really simple to deal with. The real problems are with semantics, is an event enabled when the CPU is dead? Can you disable/enable an event on a dead CPU.
The below patch (_completely_ untested) should do most of it, but needs help with the details. I suspect we want to allow enable/disable on events that are on a dead CPU, and equally I think we want to account the time an enabled event spends on a dead CPU to go towards the 'enabled' bucket.
Quoted 7 lines
Quoted 2 lines
Also, you _still_ don't explain why you care about dead CPUs.
It's just not only about dead CPUs. It's the fact that the CPUs can come and go online. The embedded world, specifically Android mobile SoCs, rely on CPU hotplugs to manage power and thermal constraints. These hotplugs can happen at a very rapid pace.
That's batshit insane... and that sounds like the primary thing you should be fixing.
--- include/linux/perf_event.h | 2 + kernel/events/core.c | 109 ++++++++++++++++++++++++++++----------------- 2 files changed, 69 insertions(+), 42 deletions(-)
include/linux/perf_event.h +2 -0
@@ -495,6 +495,8 @@ enum perf_event_state {PERF_EVENT_STATE_OFF =-1 ,PERF_EVENT_STATE_INACTIVE =0 ,PERF_EVENT_STATE_ACTIVE =1 , + + PERF_EVENT_STATE_HOTPLUG_OFFSET = -32, };struct file ;
kernel/events/core.c +67 -42
183 lines not on this page · raw message
Quoted 16 lines · Peter Zijlstra
Quoted 10 lines · Raghavendra Rao Ananta
Quoted 2 lines
No this is absolutely disguisting. You can simply keep the events in the dead CPU's context. It's really not that hard.
Keeping the events in the dead CPU's context was also an idea that we had. However, detaching that event from the PMU when the CPU is offline would be a pain. Consider the scenario in which an event is about to be destroyed when the CPU is offline (yet still attached to the CPU). During it's destruction, a cross-cpu call is made (from
perf_remove_from_context()) to the offlined CPU to detach the event from the CPU's PMU. As the CPU is offline, that would not be possible, and again a separate logic has to be written for cleaning up the events whose CPUs are offlined.That is actually really simple to deal with. The real problems are with semantics, is an event enabled when the CPU is dead? Can you disable/enable an event on a dead CPU.
Quoted 5 lines
The below patch (_completely_ untested) should do most of it, but needs help with the details. I suspect we want to allow enable/disable on events that are on a dead CPU, and equally I think we want to account the time an enabled event spends on a dead CPU to go towards the 'enabled' bucket.
I've gone through your diff, and it gave me a hint of similar texture what we are trying to do (except for maintaining an offline event list). Nevertheless, I tried to test your patch. I created an hw event, and tried to offline the CPU in parallel, and I immediately hit a watchdog soft lockup bug! Tried the same this by first switching off the CPU (without any event created), and I hit into similar issue. I am sure we can fix it, but apart from the "why we are doing hotplug?" question, was was there specifically any issue with our patch?
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Also, you _still_ don't explain why you care about dead CPUs.
I wanted to understand, if we no longer care about hotplugging of CPUs, then why do we still have exported symbols such as cpu_up() and cpu_down()? Moreover, we also have the hotplug interface exposed to users-space as well (through sysfs). As long as these interfaces exist, there's always a potential chance of bringing the CPU up/down. Can you please clear this thing up for me?
-- Raghavendra
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Peter Zijlstra
maintainerQuoted 3 lines · Raghavendra Rao Ananta
I am sure we can fix it, but apart
from the "why we are doing hotplug?" question, was was there specifically any issue with our patch?
Yes, the extra list is crazy. We don't keep events in extra lists when a task isn't currently running either. A CPU being offline shouldn't be (much) different from that.
Thinking more, we should also get rid of that HOTPLUG offset thing.
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Also, you _still_ don't explain why you care about dead CPUs.
I wanted to understand, if we no longer care about hotplugging of CPUs, then why do we still have exported symbols such as
cpu_up()andcpu_down()?
Not sure why we have them, legacy probably. The rcu/lock torture module is the only legitimate user of them.
But if having those exports gives people the impression its a sane thing to do hotplug from modules, we should just take it out.
Quoted 4 lines
Moreover, we also have the hotplug interface exposed to users-space as well (through sysfs). As long as these interfaces exist, there's always a potential chance of bringing the CPU up/down. Can you please clear this thing up for me?
Hotplug is an absolute utter slow path. We do our absolute best to put the entire burden on the hotplug path such that we don't perturb normal things.
Its primary existence is for physical CPU hotplug, not resource management. Although there seems to be a misguided 'I have this hammer, everthing is a nail' thing going on.
I suppose these 'once' things like changing the topology of the machine -- eg. 'unplug' all but one of the SMT threads, are OK as well.
And RAS things that take a CPU down when there's 'trouble' is also fine.
But anything that does hotplug semi regularly is batshit insane. One of the first things hotplug does is synchronize_rcu(), that can take a _long_ time.