xen/smp: Speed up on_selected_cpus()
PATCH 1/1 xen/smp: Speed up on_selected_cpus()
cpumask_weight() is a horribly expensive way to find if no bits are set, made worse by the fact that the calculation is performed with the global call_lock held.
Switch to using cpumask_empty() instead, which will short circuit as soon as it find any set bit in the cpumask.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
I have not done performance testing, but I would be surprised if this cannot be measured on a busy or large box.
--- xen/common/smp.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
xen/common/smp.c +1 -4
@@ -50,8 +50,6 @@ void on_selected_cpus(void *info, int wait) { -unsigned int nr_cpus; -ASSERT (local_irq_is_enabled ());ASSERT (cpumask_subset (selected, &cpu_online_map));
@@ -59,8 +57,7 @@ void on_selected_cpus(cpumask_copy (&call_data.selected, selected); - nr_cpus =cpumask_weight (&call_data.selected); -if ( nr_cpus ==0 ) +if (cpumask_empty (&call_data.selected) ) goto out; call_data.func = func;
Signature · 1 line
2.11.0
Hi,
Quoted 3 lines · Andrew Cooper
cpumask_weight()is a horribly expensive way to find if no bits are set, made worse by the fact that the calculation is performed with the globalcall_lockheld.
I looked at the archive because I was wondering why we were using cpumask_weight here. It looks like this was a left-over of the rework in ac3fc35d919c "x86: Fix flush_area_mask() and on_selected_cpus() to not race updates".
Quoted 5 lines
Switch to using
cpumask_empty()instead, which will short circuit as soon as it find any set bit in the cpumask.Signed-off-by Andrew Cooper <[email protected]>
Quoted 39 lines
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>I have not done performance testing, but I would be surprised if this cannot be measured on a busy or large box.
--- xen/common/smp.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)xen/common/smp.c +1
-4@@ -50,8 +50,6 @@ void on_selected_cpus(void *info, int wait) { -unsigned int nr_cpus; -ASSERT (local_irq_is_enabled ());ASSERT (cpumask_subset (selected, &cpu_online_map));@@ -59,8 +57,7 @@ void on_selected_cpus(cpumask_copy (&call_data.selected, selected); - nr_cpus =cpumask_weight (&call_data.selected); -if ( nr_cpus ==0 ) +if (cpumask_empty (&call_data.selected) ) goto out; call_data.func = func;
Signature · 1 line
Julien Grall
Quoted 9 lines · Julien Grall
Hi,
Quoted 5 lines · Andrew Cooper
cpumask_weight()is a horribly expensive way to find if no bits are set, made
worse by the fact that the calculation is performed with the globalcall_lock
held.
Quoted 4 lines
I looked at the archive because I was wondering why we were using
cpumask_weighthere. It looks like this was a left-over of the rework inac3fc35d919c"x86: Fixflush_area_mask()andon_selected_cpus()to not race updates".
That change shuffled the code, but didn't introduce the problem.
I'm pretty sure it was 433f14699d48 which dropped the !=0 user of nr_cpus.
Talking of, there is more efficiency to be gained by reworking the second cpumask_empty() call to not restart from 0 on failure, because that removes useless reads.
Quoted 9 lines
Quoted 7 lines
Switch to using
cpumask_empty()instead, which will short circuit as soon as
it find any set bit in the cpumask.Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Julien Grall <[email protected]>
Thanks.
~Andrew
Quoted 8 lines · Andrew Cooper
cpumask_weight()is a horribly expensive way to find if no bits are set, made worse by the fact that the calculation is performed with the globalcall_lockheld.Switch to using
cpumask_empty()instead, which will short circuit as soon as it find any set bit in the cpumask.Signed-off-by Andrew Cooper <[email protected]>
May I suggest to drop "horribly"? How expensive one is compared to the other depends on the number of CPUs actually enumerated in the system. (And of course I still have that conversion to POPCNT alternatives patching pending, where Roger did ask for some re-work in reply to v2, but where it has remained unclear whether investing time into that wouldn't be in vein, considering some of your replies on v1. Thus would have further shrunk the difference, without me meaning to say the change here isn't a good one.)
Jan
Quoted 11 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
cpumask_weight()is a horribly expensive way to find if no bits are set, made worse by the fact that the calculation is performed with the globalcall_lockheld.Switch to using
cpumask_empty()instead, which will short circuit as soon as it find any set bit in the cpumask.Signed-off-by Andrew Cooper <[email protected]>May I suggest to drop "horribly"? How expensive one is compared to the other depends on the number of CPUs actually enumerated in the system.
In absolute terms perhaps, but they both scale as O(nr_cpus). Hamming weight has a far larger constant.
Quoted 6 lines
(And of
course I still have that conversion to POPCNT alternatives patching pending, where Roger did ask for some re-work in reply to v2, but where it has remained unclear whether investing time into that wouldn't be in vein, considering some of your replies on v1. Thus would have further shrunk the difference, without me meaning to say the change here isn't a good one.)
There is a perfectly clear and simple way forward. It's the one which doesn't fight the optimiser and actively regress the code generation in the calling functions, and add an unreasonable quantity technical debt into the marginal paths.
I will ack a version where you're not adding complexity for negative gains.
~Andrew
Quoted 8 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
(And of
course I still have that conversion to POPCNT alternatives patching pending, where Roger did ask for some re-work in reply to v2, but where it has remained unclear whether investing time into that wouldn't be in vein, considering some of your replies on v1. Thus would have further shrunk the difference, without me meaning to say the change here isn't a good one.)
Quoted 6 lines
There is a perfectly clear and simple way forward. It's the one which doesn't fight the optimiser and actively regress the code generation in the calling functions, and add an unreasonable quantity technical debt into the marginal paths.
I will ack a version where you're not adding complexity for negative gains.
Thanks, at least some form of a reply. I'm afraid I can't really translate this to which parts of the change you'd be okay with and which parts need changing. I didn't think I would "fight the optimiser and actively regress the code generation in the calling functions" in v2 (this may have been different in v1, but I haven't gone back to check; I wonder though whether you're mixing this with e.g. the BMI patching series I've long given up on).
Jan