Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the set of available CPUs in a Linux kernel module?

I would like to start one kernel thread per CPU with kthread_create()/kthread_bind(). However, I can't for the life of me figure out how to query the number of available CPUs. I did find the CPU_SET man page but that didn't help either.

Any thoughts?

like image 815
TaborKelly Avatar asked May 29 '26 01:05

TaborKelly


2 Answers

You can use num_online_cpus() to get the number of available cpus. This may be different from things like nr_cpu_ids if the system was booted using a maxcpus setting that is not the same as the number of cpus in the system.

like image 143
seacoder Avatar answered May 31 '26 22:05

seacoder


See following link, cpuinfo.c, proc.c, may be help you. And at line 143, you can use two functions for traversing cpus, cpumask_first, cpumask_next. I think, by try and error, you can find the solutions.

like image 40
Ocean Avatar answered May 31 '26 22:05

Ocean