0?前言
2024年的網絡安全檢查又開始了,對于使用基于Linux的國產電腦,我們可以編寫一個腳本來收集系統的有關信息。對于中央處理器CPU比如,我們可以使用cat /proc/cpuinfo命令來收集中央處理器CPU的信息。
1.?/proc/cpuinfo?保存了系統的cpu信息
在linux系統中,/proc目錄下的文件保存了系統的軟硬件信息。
[purpleendurer @ bash /] ls -l /proc
total 0
dr-xr-xr-x ?9 csdn csdn ? ?0 5月 ?28 22:19 1
dr-xr-xr-x ?9 csdn csdn ? ?0 5月 ?28 22:22 102
dr-xr-xr-x ?9 csdn csdn ? ?0 5月 ?28 22:22 127
dr-xr-xr-x ?9 csdn csdn ? ?0 5月 ?28 22:23 155
dr-xr-xr-x ?9 csdn csdn ? ?0 5月 ?28 22:24 170
dr-xr-xr-x ?9 csdn csdn ? ?0 5月 ?28 22:19 47
dr-xr-xr-x ?9 csdn csdn ? ?0 5月 ?28 22:19 72
drwxrwxrwt ?2 root root ? 40 5月 ?28 22:19 acpi
-rw------- ?1 root root ? ?0 5月 ?28 22:23 async_load_calc
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 buddyinfo
dr-xr-xr-x ?4 root root ? ?0 5月 ?28 22:19 bus
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 cgroups
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 cmdline
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 consoles
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 cpuinfo
dr-xr-xr-x ?3 root root ? ?0 5月 ?28 22:23 cpusli
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 crypto
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 devices
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 diskstats
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 dma
dr-xr-xr-x ?4 root root ? ?0 5月 ?28 22:23 driver
dr-xr-xr-x ?3 root root ? ?0 5月 ?28 22:23 dynamic_debug
--w------- ?1 root root ? ?0 5月 ?28 22:23 dyn_isolcpus
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 execdomains
-rw-r--r-- ?1 root root ? ?0 5月 ?28 22:23 fault_events
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 fb
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:19 filesystems
dr-xr-xr-x ?9 root root ? ?0 5月 ?28 22:19 fs
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 interrupts
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 iomem
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 ioports
dr-xr-xr-x 28 root root ? ?0 5月 ?28 22:19 irq
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 kallsyms
crw-rw-rw- ?1 root root 1, 3 5月 ?28 22:19 kcore
crw-rw-rw- ?1 root root 1, 3 5月 ?28 22:19 keys
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 key-users
-r-------- ?1 root root ? ?0 5月 ?28 22:23 kmsg
-r-------- ?1 root root ? ?0 5月 ?28 22:23 kpagecgroup
-r-------- ?1 root root ? ?0 5月 ?28 22:23 kpagecount
-r-------- ?1 root root ? ?0 5月 ?28 22:23 kpageflags
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 loadavg
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 locks
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 mdstat
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:19 meminfo
dr-xr-xr-x ?3 root root ? ?0 5月 ?28 22:23 memsli
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 misc
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 modules
lrwxrwxrwx ?1 root root ? 11 5月 ?28 22:23 mounts -> self/mounts
-rw-r--r-- ?1 root root ? ?0 5月 ?28 22:23 mtrr
lrwxrwxrwx ?1 root root ? ?8 5月 ?28 22:23 net -> self/net
-r-------- ?1 root root ? ?0 5月 ?28 22:23 pagetypeinfo
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 partitions
crw-rw-rw- ?1 root root 1, 3 5月 ?28 22:19 sched_debug
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 schedstat
drwxrwxrwt ?2 root root ? 40 5月 ?28 22:19 scsi
lrwxrwxrwx ?1 root root ? ?0 5月 ?28 22:19 self -> 170
-r-------- ?1 root root ? ?0 5月 ?28 22:23 slabinfo
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 softirqs
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 stat
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 swaps
dr-xr-xr-x ?1 root root ? ?0 5月 ?28 22:19 sys
--w------- ?1 root root ? ?0 5月 ?28 22:19 sysrq-trigger
dr-xr-xr-x ?5 root root ? ?0 5月 ?28 22:23 sysvipc
lrwxrwxrwx ?1 root root ? ?0 5月 ?28 22:19 thread-self -> 170/task/170
crw-rw-rw- ?1 root root 1, 3 5月 ?28 22:19 timer_list
dr-xr-xr-x ?6 root root ? ?0 5月 ?28 22:23 tty
dr-xr-xr-x ?4 root root ? ?0 5月 ?28 22:23 unevictable
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 uptime
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 version
-r-------- ?1 root root ? ?0 5月 ?28 22:23 vmallocinfo
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 vmstat
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 zerobuddyinfo
-r--r--r-- ?1 root root ? ?0 5月 ?28 22:23 zoneinfo
[purpleendurer @ bash /]?
如果想了解和獲取系統中CPU的提供商和相關配置信息,則可以查看/proc/cpuinfo。
2.cat /proc/cpuinfo:查看CPU詳細信息
2.1?實例
2.1.1?實例1
[purpleendurer @ bash /] cat /proc/cpuinfo
processor ? ? ? : 0
vendor_id ? ? ? : GenuineIntel
cpu family ? ? ?: 6
model ? ? ? ? ? : 85
model name ? ? ?: Intel(R) Xeon(R) Platinum
stepping ? ? ? ?: 4
microcode ? ? ? : 0x1
cpu MHz ? ? ? ? : 2499.442
cache size ? ? ?: 33792 KB
physical id ? ? : 0
siblings ? ? ? ?: 2
core id ? ? ? ? : 0
cpu cores ? ? ? : 1
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 mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat
bugs ? ? ? ? ? ?: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa itlb_multihit mmio_stale_data retbleed
bogomips ? ? ? ?: 4998.88
clflush size ? ?: 64
cache_alignment : 64
address sizes ? : 46 bits physical, 48 bits virtual
power management:processor ? ? ? : 1
vendor_id ? ? ? : GenuineIntel
cpu family ? ? ?: 6
model ? ? ? ? ? : 85
model name ? ? ?: Intel(R) Xeon(R) Platinum
stepping ? ? ? ?: 4
microcode ? ? ? : 0x1
cpu MHz ? ? ? ? : 2499.442
cache size ? ? ?: 33792 KB
physical id ? ? : 0
siblings ? ? ? ?: 2
core id ? ? ? ? : 0
cpu cores ? ? ? : 1
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 mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat
bugs ? ? ? ? ? ?: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa itlb_multihit mmio_stale_data retbleed
bogomips ? ? ? ?: 4998.88
clflush size ? ?: 64
cache_alignment : 64
address sizes ? : 46 bits physical, 48 bits virtual
power management:[purpleendurer @ bash /]?
2.1.2?實例2
[purpleendurer @ bash ~] cat /proc/cpuinfo
processor : 0
model name : Phytium,FT-2000/4
bogomips : 3456.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
flags : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x70
CPU architecture: 8
CPU variant : 0x1
CPU part : 0x663
CPU revision : 3processor : 1
model name : Phytium,FT-2000/4
bogomips : 3456.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
flags : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x70
CPU architecture: 8
CPU variant : 0x1
CPU part : 0x663
CPU revision : 3processor : 2
model name : Phytium,FT-2000/4
bogomips : 3456.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
flags : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x70
CPU architecture: 8
CPU variant : 0x1
CPU part : 0x663
CPU revision : 3processor : 3
model name : Phytium,FT-2000/4
bogomips : 3456.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
flags : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x70
CPU architecture: 8
CPU variant : 0x1
CPU part : 0x663
CPU revision : 3
?
?2.2?返回信息說明
不同的系統返回的信息內容也不盡相同,以下是一些常見的信息項。
輸出項 | 含義 | 備注 |
---|---|---|
address sizes | 可訪問地址空間位數 | |
apicid | 用來區分不同邏輯核的編號,系統中每個邏輯核的此編號必然不同,此編號不一定連續 | |
bogomips | 在系統內核啟動時粗略測算的CPU速度 | Million Instructions Per Second |
cache_alignment | 緩存地址對齊單位 | |
cache size | CPU二級緩存大小 | |
clflush size | 每次刷新緩存的大小單位 | |
core id | 當前物理核在其所處CPU中的編號 | 這個編號不一定連續 |
cpu cores | 該邏輯核所處CPU的物理核數 | 比如此處cpu cores 是4個,那么對應core id 可能是 1、3、4、5。 |
cpu family | CPU產品系列代號 | |
cpuid level | 執行cpuid指令前,eax寄存器中的值,根據不同的值cpuid指令會返回不同的內容 | |
cpu MHz | CPU的實際使用主頻 | |
flags | 當前CPU支持的功能 | |
fpu | 是否具有浮點運算單元(Floating Point Unit) | |
fpu_exception | 是否支持浮點計算異常 | |
initial apicid | ||
microcode | ||
model | CPU屬于其系列中的哪一代的代號 | |
model name | CPU全名,包括處理器品牌。 | 一旦您知道您擁有的 CPU 的確切類型,您就可以查看有關處理器規格的產品文檔。CPU屬于的名字及其編號、標稱主頻 |
processor | 系統中邏輯處理核心的編號,從0開始排序。 | 對于單核處理器,則認為是其CPU編號 對于多核處理器則可以是物理核、或者使用超線程技術虛擬的邏輯核 |
physical id | 單個CPU的標號 | |
power management? | 對能源管理的支持 | |
siblings | 單個CPU邏輯物理核數 | |
stepping | CPU屬于制作更新版本 | |
vendor_id | CPU制造商 | |
wp | 表明當前CPU是否在內核態支持對用戶空間的寫保護 | Write Protection |
2.3?關于物理CPU、核數、邏輯cpu數的概念
2.3.1 物理CPU數(physical id)
主板上實際插入的cpu數量,可以數不重復的 physical id 有幾個
2.3.2 CPU核心數(cpu cores)
單塊CPU上面能處理數據的芯片組的數量,如雙核、四核等?
2.3.3邏輯CPU數
一般情況下:
在不支持超線程技術或沒有開啟次技術的情況下:
邏輯CPU=物理CPU個數×每顆核數
在服務器的CPU支持超線程技術的情況下:
邏輯CPU=物理CPU個數×每顆核數 *2??
簡單來說,在這種情況下,處理器中的1 顆內核會像2 顆內核那樣在操作系統中發揮作用。
這樣一來,操作系統可使用的執行資源擴大了一倍,大幅提高了系統的整體性能。
3?應用實例
3.1?grep -m 1 'model name' /proc/cpuinfo :顯示CPU名稱
[purpleendurer @ bash ~] grep -m 1 'model name' /proc/cpuinfo?
model name ? ? ?: Intel(R) Xeon(R) Platinum
[purpleendurer @ bash ~]?
3.2?grep -c 'model name' /proc/cpuinfo:顯示CPU數量
[purpleendurer @ bash ~] grep -c 'model name' /proc/cpuinfo
2
[purpleendurer @ bash ~]?
3.3?grep -c "cpu cores" /proc/cpuinfo :?獲取CPU核數
[purpleendurer @ bash ~] grep -c "cpu cores" /proc/cpuinfo
2
[purpleendurer @ bash ~]?
3.4?grep -c "processor" /proc/cpuinfo:獲取系統中的邏輯CPU數量
[purpleendurer @ bash ~] grep -c "processor" /proc/cpuinfo?
2
[purpleendurer @ bash ~]?
3.5 grep -c "siblings" /proc/cpuinfo:獲取每個物理CPU中邏輯CPU的個數?
[purpleendurer @ bash ~] grep -c "siblings" /proc/cpuinfo
2
[purpleendurer @ bash ~]?
3.6?檢測cpu是否使用了超級程Hyper-Threading
我們可以通過比較cpu cores數量和siblings數量來判斷:
如果cpu cores數量和siblings數量一致,說明沒有啟用超線程,否則就是啟用了超線程。
我們把cpu cores數量保存到變量a,把siblings數量保存到變量b,然后用if語句比變量a和b的值,并輸出相應的結果
[purpleendurer @ bash ~] grep -c "cpu cores" ?/proc/cpuinfo
2
[purpleendurer @ bash ~] grep -c "siblings" ?/proc/cpuinfo
2
[purpleendurer @ bash ~] a=$(grep -c "cpu cores" ?/proc/cpuinfo);b=$(grep -c "siblings" ?/proc/cpuinfo);if [ $a = $b ]; then echo 'No Hyper-Threading'; else echo 'Hyper-Threading'; ?fi
No Hyper-Threading
[purpleendurer @ bash ~]?
?cpu cores數量和siblings數量一致,都是2,說明沒有啟用超線程。
4.其它相關命令
?除了利用/proc/cpuinfo文件,我們還可以使用其它命令,比如?lscpu、nproc,留待下回講解。