cpumask_local_spread(9)
π cpumask_local_spread(9) β a Kernel API Manual moment
okay so bestie, welcome to the Kernel API Manual π this one's called cpumask_local_spread and it's genuinely iconic once u get it, i promise.
β¨ NAME β¨
cpumask_local_spread β pick the i'th cpu based on NUMA distances πΊοΈ
πΈ SYNOPSIS πΈ
unsigned int cpumask_local_spread (unsigned int i , int node );
π ARGUMENTS π
iβ the index number, like which one u want in the lineup πnodeβ the localnuma_node, aka ur home base π
π¦ RETURN π¦
returns an online CPU according to a numa aware policy β and the order is the whole vibe, bestie:
- local cpus come back first π₯
- then the non-local ones πΆ
- then it wraps around π
π₯ DESCRIPTION π₯
so if ur the kind of person who wants to enumerate all the CPUs based on their NUMA distances β i.e. u wanna call this function in a loop β it looks like this:
for (i = 0; i < num_online_cpus; i++) {
cpu = cpumask_local_spread(i, node);
do_something(cpu);
}
but bestie, there's a better alternative based on for_each-like iterators:
for_each_numa_hop_mask(mask, node) {
for_each_cpu_andnot(cpu, mask, prev)
do_something(cpu);
prev = mask;
}
the docs say it's simpler and more verbose than the above (yes, that's literally what it says π just relaying the message fr fr).
as for complexity, ngl this is the part u screenshot:
- iterator-based enumeration is
O(sched_domains_numa_levels * nr_cpu_ids)π cpumask_local_spreadwhen called for each cpu isO(sched_domains_numa_levels * nr_cpu_ids * log(nr_cpu_ids))π
so like... the iterator version is the one ur actually gonna want. take the hint, periodt.
π SEE ALSO π
- Kernel file
./lib/cpumask.c alloc_cpumask_var_node(9)alloc_bootmem_cpumask_var(9)free_cpumask_var(9)free_bootmem_cpumask_var(9)cpumask_any_and_distribute(9)cpumask_any_distribute(9)
go read those next, iykwim π«Ά
and that's the page!! π§ this little masterpiece lives in ./lib, dated October 2026, and it's filed under cpumask_local_spread(9). mwah π slay, bye! πβ¨
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms