物理核心数和逻辑核心数的解释

2022/08/09 计算机基础

在讨论计算机CPU时经常会提到物理核心数和逻辑核心数,以及四核八线程的概念,那么他们之间的区别和联系是什么呢? 在linux上我们可以通过cat /proc/cpuinfo 查看CPU的信息:

processor: 0
vendor_id: GenuineIntel
cpu family: 6
model: 79
model name: Intel(R) Xeon(R) CPU E5-26xx v4
stepping: 1
cpu MHz: 2394.454
cache size: 4096 KB
physical id: 0
siblings: 1
core id: 0
cpu cores: 1
apicid: 0
initial apicid: 0
fpu: yes
fpu_exception: yes
cpuid level: 13
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 lm constant_tsc rep_good unfair_spinlock 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 xsaveopt bmi1 avx2 bmi2
bogomips: 4788.90
clflush size: 64
cache_alignment: 64
address sizes: 40 bits physical, 48 bits virtual
power management:

processor: 1
vendor_id: GenuineIntel
cpu family: 6
model: 79
model name: Intel(R) Xeon(R) CPU E5-26xx v4
stepping: 1
cpu MHz: 2394.454
cache size: 4096 KB
physical id: 1
siblings: 1
core id: 0
cpu cores: 1
apicid: 1
initial apicid: 1
fpu: yes
fpu_exception: yes
cpuid level: 13
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 lm constant_tsc rep_good unfair_spinlock 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 xsaveopt bmi1 avx2 bmi2
bogomips: 4788.90
clflush size: 64
cache_alignment: 64
address sizes: 40 bits physical, 48 bits virtual
power management:

processor: 2
vendor_id: GenuineIntel
cpu family: 6
model: 79
model name: Intel(R) Xeon(R) CPU E5-26xx v4
stepping: 1
cpu MHz: 2394.454
cache size: 4096 KB
physical id: 2
siblings: 1
core id: 0
cpu cores: 1
apicid: 2
initial apicid: 2
fpu: yes
fpu_exception: yes
cpuid level: 13
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 lm constant_tsc rep_good unfair_spinlock 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 xsaveopt bmi1 avx2 bmi2
bogomips: 4788.90
clflush size: 64
cache_alignment: 64
address sizes: 40 bits physical, 48 bits virtual
power management:

processor: 3
vendor_id: GenuineIntel
cpu family: 6
model: 79
model name: Intel(R) Xeon(R) CPU E5-26xx v4
stepping: 1
cpu MHz: 2394.454
cache size: 4096 KB
physical id: 3
siblings: 1
core id: 0
cpu cores: 1
apicid: 3
initial apicid: 3
fpu: yes
fpu_exception: yes
cpuid level: 13
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 lm constant_tsc rep_good unfair_spinlock 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 xsaveopt bmi1 avx2 bmi2
bogomips: 4788.90
clflush size: 64
cache_alignment: 64
address sizes: 40 bits physical, 48 bits virtual
power management:

其中model name就是CPU型号名字,physical id就是CPU的编号,每一个physical id代表一个CPU。

首先我们先明确几个概念:

物理CPU

物理CPU就是指计算机上实际配置的CPU个数,绝大多数计算机都是只有一个CPU,也就是我们购买主板时会提到支持几代CPU,一般的主板都只支持一个CPU,支持多个CPU的主板一般常见于服务器。 physical id的个数就代表CPU的数量,这里可以看到这台主机有4个CPU。

CPU核数

每个CPU有多个核心数,上面参数cpu cores代表该CPU的物理核心数,也就是1个。每个CPU至少要有一个核心数处理数据

逻辑CPU

操作系统为了充分使用现有的物理CPU,会使用逻辑CPU模拟多个类似物理CPU的效果,之前没有多核处理器的情况下都是通过这种方式处理的,而现在利用多核技术模拟多个CPU的效果。当计算机没有开启超线程时,逻辑CPU的个数就是计算机的核数,而当超线程开启后,逻辑CPU的个数是真实核数的两倍,一个核心只能同时执行一个线程。

实际上逻辑CPU的数量就是平时我们所说的几核几线程中的线程数量,在linux中逻辑CPU就是cpuinfo中的processor的数量。所谓的四核八线程就是指四个物理核数。通过超线程技术,用一个物理核模拟两个虚拟核,每个虚拟核就是一个线程,总数就是八线程。单核CPU和多核CPU都是指一个CPU,不同的是每个CPU物理核数,多个CPU是多个单核CPU的替代方案,实现了同样效果,但是体积更小,功耗也更低。

  • 物理核数=物理CPU数量*每个CPU的物理核数
  • 虚拟核数=物理核数*每个物理核的虚拟核数

Search

    欢迎关注我的微信公众号

    够浪程序员

    Table of Contents