-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu-core-count
24 lines (19 loc) · 1.37 KB
/
cpu-core-count
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Surprisingly it is not very straightforward to query, in a scriptable way, how many real cpu cores are present.
# While it is trivial find the total number of logical cores, it is a lot more difficult to ascertain if
# these are multi-threading frontends or real cpu cores.
# The following will return the real core count of a system, as a single integer value, I have
# confirmed it works on a 4-way E7-8895 v2 and 2-way E5-2640 v3 system with hyper-threading enabled.
awk '{if (/^siblings/) {siblings+=$3}}; {if (/^cpu cores/) {count+=1; cores+=$4}} END {printf "%.0f\n",count/(siblings/cores)}' /proc/cpuinfo;
# It will round to the nearest integer, so that if you have some kind of quirky archtecture with
# a heterogeneous configuration it will still return a useful value that can be used in scripts.
# Example:
# nbritton@lab01:~$ cores=$(awk '{if (/^siblings/) {siblings+=$3}}; {if (/^cpu cores/) {count+=1; cores+=$4}} END {printf "%.0f\n",count/(siblings/cores)}' /proc/cpuinfo)
# nbritton@lab01:~$ echo $cores;
# 60
# Debug Example:
# Column 1: Total sibling count
# Column 2: Total cores count
# Column 3: Number of entries in /proc/cpuinfo (i.e. logical cores)
# Real_Cores = Column_3 / ( Column_1 / Column_2 )
# nbritton@lab01:~$ awk '{if (/^siblings/) {siblings+=$3}}; {if (/^cpu cores/) {count+=1; cores+=$4}} END {print siblings" "cores" "count}' /proc/cpuinfo
# 3600 1800 120