在 Linux 环境中,有许多命令可以用来查看 CPU 的相关信息。这些命令对于系统管理、性能优化和故障排除非常有用。
1. lscpu
lscpu 命令是查看 CPU 信息的最全面命令之一。它提供了有关 CPU 架构、内核、插槽、频率、缓存大小和功能集的详细信息。使用以下命令运行 lscpu:
lscpu
它将输出类似于以下的详细信息:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 2
NUMA node(s): 2
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 2M
NUMA node0 CPU(s): 0,2
NUMA node1 CPU(s): 1,3
2. cat /proc/cpuinfo
cat /proc/cpuinfo 命令从 proc 文件系统中的 /proc/cpuinfo 文件读取信息并将其输出到终端。该文件包含有关每个 CPU 的详细信息,包括其型号、速度、缓存大小和功能集。
cat /proc/cpuinfo
输出示例:
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 63
model name : Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
stepping : 9
microcode : 0x89
cpu MHz : 1800.000
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 0
cpu cores : 4
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
3. top
top 命令提供有关系统资源使用情况的实时信息,包括 CPU 使用率。它以交互式界面显示信息,允许用户实时监控系统活动。
top
输出示例:
top - 18:25:02 up 11 days, 21:24, 1 user, load average: 0.07, 0.06, 0.05
Tasks: 183 total, 1 running, 182 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.5 us, 0.3 sy, 0.0 ni, 99.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 16092800 total, 12376472 used, 3716328 free, 265184 buffers
KiB Swap: 3276796 free
4. vmstat
vmstat 命令提供有关系统性能的统计信息,包括 CPU 使用率。它以周期性报告的形式显示信息,允许用户查看系统随时间的变化。
vmstat 1 5
输出示例:
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 96828 1343868 118016 1203872 0 0 0 0 134 167 0 1 98 0 0
0 0 96828 1343868 118016 1203876 0 0 0 0 1842 1906 0 1 98 0 0
5. sar
sar 命令收集和报告系统活动信息,包括 CPU 使用率。它可以生成历史报告,允许用户分析系统性能趋势。
sar -u 1 5
输出示例:
Linux 5.4.0-66-generic (xxxxxx.localdomain) 01/16/2023 _x86_64_ (16 CPU)
Average load: 0.13, 0.10, 0.09
CPU %usr %nice %sys %iowait %irq %soft %steal %guest
all 0.38 0.00 0.06 0.00 0.01 0.03 0.00 0.01
0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
6 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
7 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
8 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
9 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
11 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
12 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
13 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
14 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
15 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
通过使用这些命令,系统管理员和用户可以轻松地查看有关 Linux 系统 CPU 的详细性能信息,从而帮助他们做出明智的决策,优化系统性能并解决问题。