4

I am aware that sysconf(_SC_NPROCESSORS_ONLN) will give me the number of logical processors. But how can I get the number of physical processors?

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101

4 Answers4

3

@Havoc P: offline cpus aren't shown in /proc/cpuinfo .

CPU topology is described by /sys/devices/system/cpu/cpu*/topology/*

adobriyan
  • 2,594
  • 16
  • 8
1

You could parse /proc/cpuinfo and count the number of distinct "physical id:" lines. Sort of annoying, but I don't know if there's a better option. If you're using GLib or another library with regex support it'd be easier. Or you could popen() a command line to do it if you're feeling really hacky. example command line at: http://www.brandonhutchinson.com/Understanding_proc_cpuinfo.html

Havoc P
  • 8,365
  • 1
  • 31
  • 46
1

I think the best way to get this information is to use hwloc: http://www.open-mpi.org/projects/hwloc/.

They provide a bunch of user tools that let you get at the cpu topology of a system, but they also provide a library that you can use from C: http://www.open-mpi.org/projects/hwloc/doc/v1.1.1/#interface

Mark Drago
  • 1,978
  • 14
  • 10
0

Perhaps this answer to a similar question does help. There's a comment about the code not being correct, but it could be a good starting point.

Community
  • 1
  • 1
schnaader
  • 49,103
  • 10
  • 104
  • 136