LoginSignup
40
37

[memo] Linux で CPU の数を調べる

Last updated at Posted at 2019-09-03

はじめに

Linux 環境上で、CPU の数を調べたいことがあったのでメモ。

物理 CPU の数

マシンによっては、物理的に複数の CPU を積んでいるものがある。

fgrep 'physical id' /proc/cpuinfo | sort -u | wc -l

CPU コア数

最近の CPU では、物理的に 1 つの CPU 上に、複数のコアがあるものがある。

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

論理プロセッサー数

最近の CPU では、ハイパースレッディングなどの機能を使って、1 つのコアで複数の処理をすることができる。

そのため、論理プロセッサー数は、物理 CPU 数×コア数よりも大きいことがある。

fgrep 'processor' /proc/cpuinfo | wc -l

簡易的なコマンド

物理的な CPU がすべて同じコア数で、ハイパースレッディングも同様とすると以下のような感じ?

cpucore.sh
#!/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
$ cpucore.sh
8 processers = 1 socket x 4 cores x 2 threads

lscpu コマンド

ちなみに、あとで知ったのですが 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
(以下略)

以下のあたりが同じ情報になると思います

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

Windows で CPU の数を調べる

以下で上記と同じようにCPU数を調べることができるみたい。

C++やC#はもちろん、PowerShellやVBなどでも取得できそう。

とりあえずVBSの場合(あまり自信はないけど)

cpucore.vbs
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

40
37
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
37