Flash Notes

Hardware - Number of physical cores


• How to get the number of physical cores of a machine ?

Solution

Multiply the number of cores per socket by the number of sockets

Program

#!/bin/bash

awk 'BEGIN {
nb_cores_per_socket = 0;
}
/^cpu cores/ {
if (nb_cores_per_socket == 0) {
nb_cores_per_socket = $0;
sub(/^[^:]+: /, "", nb_cores_per_socket);
}
}
/^physical id/ {
id = $0;
sub(/^[^:]+: /, "", id);
if (!(id in phys_id)) {
nb_phys++;
phys_id[id] = 1;
}
}
END {
printf("%d\n", nb_cores_per_socket * nb_phys);
}' /proc/cpuinfo

Example 1

$ ./phys_cores 
4