I was thinking about getting physical CPUs in Linux using C. I know, I can simply do this:
if(!(cpus = popen("cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
", "r")))
{
// ...
}
but that's not the point, it won't work when I don't have grep installed. I came up with another idea:
- simply parse
/proc/cpuinfo count number of physical ids:
if(sscanf(buff, "physical id : %d", &physicalID) == 1) i++;save them into an array:
if(sscanf(buff, "physical id : %d", &physicalID) == 1) { ids[i] = physicalID; i ++; }check if there are different numbers in the array, if so, count them = it will give me number of physical sockets (CPUs), right?
And I did it but I'm not quite sure if it will be always true ... And how about couting cores, logical CPUs and checking if hyperthreading is enabled? How can I do this with /proc/cpuinfo ?
/sys/devices/system/cpuinstead of trying to parse/proc/cpuinfo. For example, to get number of physical dies on your system, simply go through/sys/devices/system/cpu/cpu*/topology/physical_package_id, the number of dies is maximum of values you find - 1. Other things you ask for could be done the same way, without parsing the/proc/cpuinfo.