Is it possible to get such info by some API or function, rather than parsing the /proc/cpuinfo
?
You can also use the command called lscpu to display information on CPU architecture on modern Linux distributions.
The way to tell how may cores you have is to look for "cpu cores" in your /proc/cpuinfo file. This line will show up for each virtual processor. If the number of cores shown is less than the number of virtual processors, your system is multi-threading.
Right-click your taskbar and select “Task Manager” or press Ctrl+Shift+Esc to launch it. Click the “Performance” tab and select “CPU.” The name and speed of your computer's CPU appear here. (If you don't see the Performance tab, click “More Details.”)
From man 5 proc
:
/proc/cpuinfo This is a collection of CPU and system architecture dependent items, for each supported architecture a different list. Two common entries are processor which gives CPU number and bogomips; a system constant that is calculated during kernel initialization. SMP machines have information for each CPU.
Here is sample code that reads and prints the info to console, stolen from forums - It really is just a specialized cat
command.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *cpuinfo = fopen("/proc/cpuinfo", "rb");
char *arg = 0;
size_t size = 0;
while(getdelim(&arg, &size, 0, cpuinfo) != -1)
{
puts(arg);
}
free(arg);
fclose(cpuinfo);
return 0;
}
Please note that you need to parse and compare the physical id
, core id
and cpu cores
to get an accurate result, if you really care about the number of CPUs vs. CPU cores. Also please note that if there is a htt
in flags
, you are running a hyper-threading CPU, which means that your mileage may vary.
Please also note that if you run your kernel in a virtual machine, you only see the CPU cores dedicated to the VM guest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With