topology: make for_each_node_with_cpus() O(N)
Yury Norov
maintainerPATCH 1/1 topology: make for_each_node_with_cpus() O(N)
From: Yury Norov [NVIDIA] <[email protected]>
for_each_node_with_cpus() calls nr_cpus_node() at every iteration, which makes it O(N^2). Kernel tracks such nodes with N_CPU record in node_states array. Switching to it makes for_each_node_with_cpus() O(N).
--- include/linux/nodemask.h | 1 + include/linux/topology.h | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-)
include/linux/nodemask.h +1 -0
@@ -541,6 +541,7 @@ static __always_inline int node_random(const nodemask_t *maskp)#define for_each_node (node) for_each_node_state(node, N_POSSIBLE)#define for_each_online_node (node) for_each_node_state(node, N_ONLINE) +#define for_each_node_with_cpus (node) for_each_node_state(node, N_CPU)/* * For nodemask scratch area.
include/linux/topology.h +1 -4
@@ -29,6 +29,7 @@#include <linux/arch_topology.h> #include <linux/cpumask.h> +#include <linux/nodemask.h> #include <linux/bitops.h> #include <linux/mmzone.h> #include <linux/smp.h>
@@ -39,10 +40,6 @@#define nr_cpus_node (node) cpumask_weight(cpumask_of_node(node))#endif -#define for_each_node_with_cpus (node) \ - for_each_online_node(node) \ - if (nr_cpus_node(node)) -int arch_update_cpu_topology (void );/* Conform to ACPI 2.0 SLIT distance definitions */
Signature · 1 line
2.43.0
Hi Yury,
Quoted 5 lines · Yury Norov
From: Yury Norov [NVIDIA] <[email protected]>
for_each_node_with_cpus()callsnr_cpus_node()at every iteration, which makes it O(N^2). Kernel tracks such nodes withN_CPUrecord innode_statesarray. Switching to it makesfor_each_node_with_cpus()O(N).
Makes sense to me.
Maybe we should mention that previously we were only considering online nodes with CPUs assigned. Now, we can include also offline nodes with CPUs assigned (assuming it's possible)?
Semantically speaking, since the name doesn't include "online", it seems more logical to ignore the state of the node. And if checking the online state is required, the user can just use node_online() within the loop.
Thanks,
-Andrea
Quoted 45 lines
Signed-off-by Yury Norov [NVIDIA] <[email protected]>--- include/linux/nodemask.h | 1 + include/linux/topology.h | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-)include/linux/nodemask.h +1
-0@@ -541,6 +541,7 @@ static __always_inline int node_random(const nodemask_t *maskp)#define for_each_node (node) for_each_node_state(node, N_POSSIBLE)#define for_each_online_node (node) for_each_node_state(node, N_ONLINE) +#define for_each_node_with_cpus (node) for_each_node_state(node, N_CPU)/* * For nodemask scratch area. include/linux/topology.h +1
-4@@ -29,6 +29,7 @@#include <linux/arch_topology.h> #include <linux/cpumask.h> +#include <linux/nodemask.h> #include <linux/bitops.h> #include <linux/mmzone.h> #include <linux/smp.h> @@ -39,10 +40,6 @@#define nr_cpus_node (node) cpumask_weight(cpumask_of_node(node))#endif -#define for_each_node_with_cpus (node) \ - for_each_online_node(node) \ - if (nr_cpus_node(node)) -int arch_update_cpu_topology (void );/* Conform to ACPI 2.0 SLIT distance definitions */ Signature · 1 line
2.43.0
Yury Norov
maintainerQuoted 11 lines · Andrea Righi
Hi Yury,
Quoted 5 lines · Yury Norov
From: Yury Norov [NVIDIA] <[email protected]>
for_each_node_with_cpus()callsnr_cpus_node()at every iteration, which makes it O(N^2). Kernel tracks such nodes withN_CPUrecord innode_statesarray. Switching to it makesfor_each_node_with_cpus()O(N).Makes sense to me.
Quoted 7 lines
Maybe we should mention that previously we were only considering online nodes with CPUs assigned. Now, we can include also offline nodes with CPUs assigned (assuming it's possible)?
Semantically speaking, since the name doesn't include "online", it seems more logical to ignore the state of the node. And if checking the online state is required, the user can just use
node_online()within the loop.
OK. I'll take your comment and move the patch with bitmap-for-next, if no objections.
Thanks,
Yury