超线程
分类
我们平时说的超线程可能指同时多线程(SMT),也可能指 Intel 的超线程(HT)。
flowchart TD
SMT("Simultaneous multithreading (SMT)")
HT("Hyper-threading (HT)")
TMT("Temporal multithreading, or super-threading")
SMT -->|Intel's implementation| HT
TMT <-->|SMT 同一条流水线能执行来自多个线程的指令,另外一个不能| SMT
HT -.-> HT_note("两个逻辑线程共享包括 TLB 和 cache 等资源,但 CR3 等资源独立")
也存在一个物理核心对应超过 2 个逻辑线程的 SMT 实现,比如 IBM Power8: 12 cores, 8T each, (32 FX + 32 FP) registers per thread,FX 是定点数,FP 是浮点数。
更完整的分类:
We can consider that Super-Threading the same as Temporal multithreading (coarse-grain). I.e. types of multithreading is vary as follows - from shared anything to shared nothing in CPU-core between threads: None (fully software multithreading), TM (fine-grained) in barrel processors, TM (coarse-grained) - Super Threading, SMT - Hyper Threading, CMT (Chip Multithreading) - multicore with shared expensive resource (single FPU on some cores), CMP (Chip Multiprocessors) - multicore with shared Last Level Cache & interconnect.
这段文字来自 https://stackoverflow.com/a/33702200 ,是对 https://ece757.ece.wisc.edu/lect03-cores-multithread.pdf#page=9 这一页幻灯片的提炼。
我的理解:Temporal multithreading(super-threading)也是硬件实现,肯定是比软件实现的单核多线程要快,但是不能像 SMT 那样充分利用 CPU 的资源。CMT 和 CMP 都是指芯片级别的多线程,指的是核心间的合作方式,而其他几个指的是单核的多线程方式。现在的 CPU 用 SMT + CMP 比较常见,未来有去除超线程的趋势。
Hyper-threading
逻辑线程不共享的资源:
- CR3 register,这是顶级页表寄存器。
- 通用寄存器,比如 x86 中的 AX, BX, CX, DX。
- 控制寄存器。
- 指令标志寄存器,比如 x86 的 EFLAGS。
- 中断掩码寄存器。
- 内存管理单元(MMU)寄存器。
- 状态寄存器 / 标志寄存器 / 条件码寄存器,都是一个意思,比如 x86 的 FLAGS。
是否并非所有寄存器都需要多一份?为何没有文章有“所有寄存器”这个说法?
But essentially, a hyper-threaded core duplicates the minimal amount of state required to make it seem as if there’s another full core present (basically the registers). – https://stackoverflow.com/a/39076156
Hyper-threading works by duplicating certain sections of the processor—those that store the architectural state—but not duplicating the main execution resources. – https://en.wikipedia.org/wiki/Hyper-threading
参考资料
- https://en.wikipedia.org/wiki/Simultaneous_multithreading
- https://en.wikipedia.org/wiki/Temporal_multithreading
- https://stackoverflow.com/questions/39075942/hyper-threads-sipi-and-registers
- https://exfly.github.io/experiment-to-understand-how-hyperthreading-works/ 这个博客里面有一个做
nop
和pause
绑定同一个物理核、不同逻辑核的实验,符合我的“超线程共享同一物理核心上的 IPC 上限”的预期。