4

I want detect number of CPUs in linux kernel and write a sys_call that find it.

cpuinfo_x86 gives features of one CPU and cpu_detect fills it but how to find number of CPUs to detect?

user3105001
  • 53
  • 1
  • 3
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Apr 02 '17 at 22:03
  • 7
    How come this is not a programming question? – abhi Jan 04 '19 at 19:29

4 Answers4

6

Using the MACRO NR_CPUS we can find the number of CPU. This Macro should not be used in early_init sequence Since mostly NR_CPUS will not be initialized as it is run time macro.

or you can use num_online_cpus() function to get the number of cpus online.

you can find more options in include/linux/cpumask.h header to find availability of CPUS in various kinds.

2

You can use nr_cpu_ids variable which is set by all architectures during kernel boot up and initialization.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
0xAX
  • 20,957
  • 26
  • 117
  • 206
-2

Try:

$lscpu

This gives me information about CPUs, as well as architecture, etc...

jedruniu
  • 520
  • 4
  • 13
-2

Personally, I cat /proc/cpuinfo to learn about the CPUs on a machine.

rreck
  • 85
  • 1
  • 9