本文簡單介紹在Linux上查看CPU和內存情況和一款系統資源查看工具htop。
查看CPU情況
以下是個人工作會經常使用到的服務器的信息。
查看所有CPU信息
可以通過如下命令查看所有CPU信息:
# cat /proc/cpuinfo
[starnight@344eeb24-3fac-11e9-b49d-0014101d20c6 ~]$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
stepping : 3
microcode : 0x7c
cpu MHz : 3699.890
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 : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch ida arat epb pln pts dtherm hwp hwp_noitfy hwp_act_window hwp_epp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves
bogomips : 6816.08
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
stepping : 3
microcode : 0x7c
cpu MHz : 3699.890
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 1
cpu cores : 4
apicid : 2
initial apicid : 2
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch ida arat epb pln pts dtherm hwp hwp_noitfy hwp_act_window hwp_epp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves
bogomips : 6816.08
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 2
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
stepping : 3
microcode : 0x7c
cpu MHz : 3699.890
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 2
cpu cores : 4
apicid : 4
initial apicid : 4
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch ida arat epb pln pts dtherm hwp hwp_noitfy hwp_act_window hwp_epp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves
bogomips : 6816.08
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 3
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
stepping : 3
microcode : 0x7c
cpu MHz : 3699.890
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 3
cpu cores : 4
apicid : 6
initial apicid : 6
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch ida arat epb pln pts dtherm hwp hwp_noitfy hwp_act_window hwp_epp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves
bogomips : 6816.08
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 4
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
stepping : 3
microcode : 0x7c
cpu MHz : 3699.890
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 0
cpu cores : 4
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch ida arat epb pln pts dtherm hwp hwp_noitfy hwp_act_window hwp_epp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves
bogomips : 6816.08
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 5
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
stepping : 3
microcode : 0x7c
cpu MHz : 3699.890
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 1
cpu cores : 4
apicid : 3
initial apicid : 3
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch ida arat epb pln pts dtherm hwp hwp_noitfy hwp_act_window hwp_epp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves
bogomips : 6816.08
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 6
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
stepping : 3
microcode : 0x7c
cpu MHz : 3699.890
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 2
cpu cores : 4
apicid : 5
initial apicid : 5
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch ida arat epb pln pts dtherm hwp hwp_noitfy hwp_act_window hwp_epp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves
bogomips : 6816.08
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 7
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
stepping : 3
microcode : 0x7c
cpu MHz : 3699.890
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 3
cpu cores : 4
apicid : 7
initial apicid : 7
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch ida arat epb pln pts dtherm hwp hwp_noitfy hwp_act_window hwp_epp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves
bogomips : 6816.08
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
通過一些正則命令,對信息進行簡單處理得到我們想要直觀了解的。
查看CPU處理器個數及核心數
[starnight@344eeb24-3fac-11e9-b49d-0014101d20c6 ~]$ cat /proc/cpuinfo | grep processor
processor : 0
processor : 1
processor : 2
processor : 3
processor : 4
processor : 5
processor : 6
processor : 7
[starnight@344eeb24-3fac-11e9-b49d-0014101d20c6 ~]$ cat /proc/cpuinfo | grep processor | wc -l
8
[starnight@344eeb24-3fac-11e9-b49d-0014101d20c6 ~]$ cat /proc/cpuinfo | grep cores
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
我們可以看到總共有8個CPU處理器,每個CPU含4個核心,所以總共是32核處理器。
查看內存情況
可以通過如下命令查看系統內存情況。
[starnight@344eeb24-3fac-11e9-b49d-0014101d20c6 ~]$ cat /proc/meminfo
MemTotal: 32702400 kB
MemFree: 3832872 kB
MemAvailable: 9085276 kB
Buffers: 581664 kB
Cached: 4648776 kB
SwapCached: 0 kB
Active: 24087688 kB
Inactive: 3200156 kB
Active(anon): 22058572 kB
Inactive(anon): 15576 kB
Active(file): 2029116 kB
Inactive(file): 3184580 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 3932156 kB
SwapFree: 3932156 kB
Dirty: 1640 kB
Writeback: 0 kB
AnonPages: 22055476 kB
Mapped: 214600 kB
Shmem: 16764 kB
Slab: 366336 kB
SReclaimable: 292124 kB
SUnreclaim: 74212 kB
KernelStack: 47264 kB
PageTables: 92440 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 20283356 kB
Committed_AS: 44230900 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 605820 kB
VmallocChunk: 34359129668 kB
HardwareCorrupted: 0 kB
AnonHugePages: 18122752 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 100336 kB
DirectMap2M: 2965504 kB
DirectMap1G: 30408704 kB
關於內存,更常用的命令是free。
free查看內存使用情況
[starnight@344eeb24-3fac-11e9-b49d-0014101d20c6 ~]$ free
total used free shared buff/cache available
Mem: 32702400 23234672 3867356 16812 5600372 9123264
Swap: 3932156 0 3932156
[starnight@344eeb24-3fac-11e9-b49d-0014101d20c6 ~]$ free -m # 以M為單位顯示
total used free shared buff/cache available
Mem: 31935 22690 3776 16 5469 8908
Swap: 3839 0 3839
[starnight@344eeb24-3fac-11e9-b49d-0014101d20c6 ~]$ free -h # 以人可讀的方式顯示,單位G
total used free shared buff/cache available
Mem: 31G 22G 3.7G 16M 5.3G 8.7G
Swap: 3.7G 0B 3.7G
可以看到內存總共31G左右,可用8.7G。
查看CPU和內存使用情況
Linux下可以使用Top命令查看系統CPU和內存的使用情況。

top命令詳解
進程列表部分:
PID:進程的ID
USER:進程所有者
PR:進程的優先級別,越小越優先被執行
NInice:值
VIRT:進程占用的虛擬內存
RES:進程占用的物理內存
SHR:進程使用的共享內存
S:進程的狀態。S表示休眠,R表示正在運行,Z表示僵死狀態,N表示該進程優先值為負數
%CPU:進程占用CPU的使用率
%MEM:進程使用的物理內存和總內存的百分比
TIME+:該進程啟動后占用的總的CPU時間,即占用CPU使用時間的累加值。
COMMAND:進程啟動命令名稱
系統信息欄:
第一行(top):
系統當前時刻;
系統啟動后到現在的運作時間;
當前登錄到系統的用戶,更確切的說是登錄到用戶的終端數;
當前系統負載的平均值,三個值分別為1分鍾前、5分鍾前、15分鍾前進程的平均數;
第二行(Tasks):
total:當前系統進程總數;
running:當前運行中的進程數;
sleeping:當前處於睡眠狀態中的進程數;
stopped:被停止的系統進程數;
zombie:僵屍進程數;
第三行(Cpus):
分別表示了 CPU 當前的使用率;
第四行(Mem):
分別表示了內存總量、當前使用量、空閑內存量、以及緩沖使用中的內存量;
第五行(Swap):
表示類別同第四行(Mem),但此處反映着交換分區(Swap)的使用情況。
htop工具
官網描述是Unix下一款交互式的進程查看工具,可以方便地查看運行中進程的資源占用情況。
htop - an interactive process viewer for Unix
Ubuntu下可簡單使用命令安裝:
# sudo apt install htop
直接輸入htop即可查看。
上面紅框部分為摘要信息(Summary),其中1,2為兩個CPU核心,CPU、內存、交換分區資源占用大小一目了然。
中部跟前述的top命令類似,便不贅述。
底部“工具欄”顯示可使用的快捷鍵。

比如可以用F5以樹形結構顯示進程/線程之間的父子關系:

參考:
