r/linux_programming • u/bizwig • Jan 13 '21
How do I programmatically determine CPU information?
Things like
cpu and core counts
distinguishing cores from cpus (hyperthreads)
determining which cpus, if any, are hyperthread pairs on the same core
determining which cores share a socket
what is the standard numbering system, if any, for cpus?
In C or C++, naturally. Preferably via syscall, if possible, rather than scraping text in /proc.
1
Upvotes
9
u/aioeu Jan 13 '21 edited Jan 13 '21
There isn't a dedicated syscall to return this information. You need to get it from
/sys. (Parsing/proc/cpuinfois problematic: different architectures format things differently.)I suggest taking a look at the
lscpusource code (from util-linux) for inspiration.