DEV Community

SATO, Yoshiyuki
SATO, Yoshiyuki

Posted on

Check number of CPUs

Check number of CPUs in Linux

I wanted to know the number of CPUs in my Linux environment. The following Japanese version of the page was helpful.

物理 CPU、CPU コア、および論理 CPU の数を確認する

The corresponding English page looks like this:

How to find the number of physical cpus, cpu cores, and logical cpus

Number of physical CPUs

Some machines have multiple physical CPUs.

fgrep 'physical id' /proc/cpuinfo | sort -u | wc -l
Enter fullscreen mode Exit fullscreen mode

Number of CPU cores

Some modern CPUs have multiple cores on a single physical CPU.

fgrep 'cpu cores' /proc/cpuinfo | sort -u | sed 's/.*: //'
Enter fullscreen mode Exit fullscreen mode

Number of logical processors

Modern CPUs can do multiple things with a single core using features like hyper-threading.

Therefore, the number of logical processors can be greater than the number of physical CPUs times the number of cores.

fgrep 'processor' /proc/cpuinfo | wc -l
Enter fullscreen mode Exit fullscreen mode

Simple shell script

Assuming all physical CPUs have the same number of cores, and hyper-threading as well, what would it look like?

#!/bin/bash

P_CPU=$( fgrep 'physical id' /proc/cpuinfo | sort -u | wc -l )
CORES=$( fgrep 'cpu cores' /proc/cpuinfo | sort -u | sed 's/.*: //' )
L_PRC=$( fgrep 'processor' /proc/cpuinfo | wc -l )
H_TRD=$(( L_PRC /  P_CPU / CORES ))

echo -n "${L_PRC} processer" ; [ "${L_PRC}" -ne 1 ] && echo -n "s"
echo -n " = ${P_CPU} socket" ; [ "${P_CPU}" -ne 1 ] && echo -n "s"
echo -n " x ${CORES} core"   ; [ "${CORES}" -ne 1 ] && echo -n "s"
echo -n " x ${H_TRD} thread" ; [ "${H_TRD}" -ne 1 ] && echo -n "s"
echo
Enter fullscreen mode Exit fullscreen mode
$ cpucore.sh
8 processers = 1 socket x 4 cores x 2 threads
Enter fullscreen mode Exit fullscreen mode

lscpu command

As I learned later, there seems to be a command called lscpu.

$ lscpu
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          8
On-line CPU(s) list:             0-7
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           142
Model name:                      Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
Stepping:                        10
CPU MHz:                         1799.998
BogoMIPS:                        3599.99
Virtualization:                  VT-x
Hypervisor vendor:               Microsoft
Virtualization type:             full
L1d cache:                       128 KiB
L1i cache:                       128 KiB
L2 cache:                        1 MiB
L3 cache:                        6 MiB
...Omitted below
Enter fullscreen mode Exit fullscreen mode

I think the following will be the same information.

  • Thread(s) per core: 2
  • Core(s) per socket: 4
  • Socket(s): 1

Check number of CPUs in Windows

On Windows, it seems that you can check the number of CPUs in the same way as above with the following API.

Win32_Processor class

It seems that C ++ and C #, as well as PowerShell and VB can be acquired.

For the time being, in the case of VBS, I think that it will be as follows.

Option Explicit

Dim locator, resultSet, processor
Dim nSocket, nCore, nThread, totalThread
Dim msg

nSocket = 0
totalThread = 0

Set locator = WScript.CreateObject("WbemScripting.SWbemLocator")
Set resultSet = locator.ConnectServer.ExecQuery("SELECT * FROM Win32_Processor")
nSocket = resultSet.count
For Each processor In resultSet
  nCore = processor.NumberOfCores
  nThread= processor.NumberOfLogicalProcessors / processor.NumberOfCores
  totalThread= totalThread + processor.NumberOfLogicalProcessors
Next

msg = totalThread& " processer"
If 1 < totalThread Then
  msg = msg & "s"
End If

msg = msg & " = " & nSocket & " socket"
If 1 < nSocket Then
  msg = msg & "s"
End If

msg = msg & " x " & nCore & " core"
If 1 < nCore Then
  msg = msg & "s"
End If

msg = msg & " x " & nThread& " thread"
If 1 < nThread Then
  msg = msg & "s"
End If

WScript.Echo msg
Enter fullscreen mode Exit fullscreen mode

Top comments (0)