Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux 伺服器

Linux 下怎麼判斷 CPU 是雙核還是單核

在 Linux 上,/proc/cpuinfo 會列出每個邏輯處理器的詳細資訊。問題是:雙核 CPU 也會顯示成兩筆,很容易讓人誤以為機器裡有兩顆單核 CPU。

關鍵欄位:Physical Processor ID

判斷方式不是數 cpuinfo 有幾段,而是看 Physical Processor ID:同一顆實體 CPU 上的多個核心,會共用同一個 Physical Processor ID。這個資訊在 cpuinfo 或 dmesg 都能找到。

用 dmesg 快速篩出:

dmesg | grep CPU | grep ID

輸出可能像:

CPU0: Initial APIC ID: 0, Physical Processor ID: 0
CPU1: Initial APIC ID: 1, Physical Processor ID: 0
CPU2: Initial APIC ID: 6, Physical Processor ID: 3
CPU3: Initial APIC ID: 7, Physical Processor ID: 3

這裡出現了兩個不同的 Physical Processor ID(0 與 3),代表機器上有兩顆雙核 CPU,總共四個邏輯核心——而不是四顆單核 CPU。

更簡潔的寫法

直接統計實體 CPU 數量:

grep 'physical id' /proc/cpuinfo | sort -u | wc -l    # 實體 CPU 數
grep 'cpu cores'   /proc/cpuinfo | sort -u             # 每顆的核數

兩者相乘就是實體核心總數;若再除以邏輯處理器數(nproc),就能看出是否啟用了 Hyper-Threading。

記住這個判準:看 Physical ID 有幾種,就知道有幾顆 CPU;看每個 ID 出現幾次,就知道每顆有幾個核心。

Copyright © Linux教程網 All Rights Reserved