戏说x86中的Hyper-threading

Intel IA32/64  手册里面对Hyper-threading/Multi-core有专门的章节进行讲解,这里简单列一下笔记:

1. Hyper-threading(SMT)

 最早出现在Netburst 架构的P4上,期望用相对廉价的方法提高系统的性能。

一个物理processor上如果实现了HT,就会被认为有两个逻辑processor。为什么会这样,手册里面讲:这是因为每个logical processor都有一个对应的Architectural StateAS)。

 

每个AS包括:
•Shared by logical processors in a physical processor
•Shared or duplicated, depending on the implementation
The following features are duplicated for each logical processor:
•General purpose registers (EAX, EBX, ECX, EDX, ESI, EDI, ESP, and EBP)
•Segment registers (CS, DS, SS, ES, FS, and GS)
•EFLAGS and EIP registers. Note that the CS and EIP/RIP registers for each logical processor point to the instruction stream for the thread being executed by the logical processor.
•x87 FPU registers (ST0 through ST7, status word, control word, tag word, data operand pointer, and instruction pointer)
•MMX registers (MM0 through MM7)
•XMM registers (XMM0 through XMM7) and the MXCSR register
•Control registers and system table pointer registers (GDTR, LDTR, IDTR, task register)

•Debug registers (DR0, DR1, DR2, DR3, DR6, DR7) and the debug control MSRs
•Machine check global status (IA32_MCG_STATUS) and machine check capability (IA32_MCG_CAP) MSRs
•Thermal clock modulation and ACPI Power management control MSRs
•Time stamp counter MSRs
•Most of the other MSR registers, including the page attribute table (PAT). See the exceptions below.
•Local APIC registers. 
(整个processor 只有一组 LINT0/LINT1 pins,当其中一个pinassert的时候,只要它们没有被任意一个逻辑processor或者全部 禁止的时候,所有的逻辑processor都会反应进行处理)
•Additional general purpose registers (R8-R15), XMM registers (XMM8-XMM15), control register, IA32_EFER on Intel 64 processors.

整个 Processor 共享的:

Memory type range registers (MTRRs)

CACHE L1/L2  (所以这里硬件会有在Logical processor 之间同步的机制)

BUS

和每种processor实现有关的

一些MSR

结果如下图:

 2. Mutli-Core (CMP)

我们可以基本认为CMP和SMP差别不大,主要cache和总线有共享。linux中也实现了针对CMP的调度器。具体见后续。

架构如图:(此图来自网络)

 

 

 一些需要注意的问题:

Q: 如果一个处理器启用了HT技术,那BIOS/OS/普通软件会有什么变化么

A:  INTEL 手册里面说明:“From a software programming perspective, control transfer of processor operation is managed at the granularity of logical processor (operating systems dispatch a runnable task by allocating an available logical processor on the platform). To manage the topology of shared resources in a multi-threading environment, it may be useful for software to understand and manage resources that are shared by more than one logical processors.”

 翻译一下: 从软件编程的角度,逻辑processor是处理器操作的控制转移的基本单位(粒度)。(操作系统分配一个系统中可用的逻辑processsor 给一个可运行的任务。)。如果尝试管理multi-threading 环境下共享资源的拓扑(信息),那对于软件区了解和管理多个逻辑processors共享的资源可能有用。

 

也就是说对于软件来说,可以认为logical processor phycial processor 一样,对于interrupt/指令流执行,响应都没有什么区别。比如早期的bios/windows 2000/linux都是这样。 对于应用程序更是没有必要了解物理与逻辑processor的区别。

 

但是为了进一步提高系统性能BIOS/OS/Application都进行了改进,增加了对HT的感知能力,并对这种情况进行了专门的优化。

 

Q 针对HTBIOS需要做哪些事情

A HT 环境下,BIOS首先按MP protocol初始化BSPAP. 同时通过CPUID 得出系统中processor 的拓扑结构,填入MADT tableAdded a section describing guidelines for the ordering of processors in the MADT to support proper boot processor and multi-threaded logical processor operation.)。ACPI3.0b之前并没有真正在MADT中区别physcial processor logical processor3.0b以后,ACPI spec 规定了OSPM根据MADTprocessor出现的顺序进行初始化,同时MADT中应该把每个physical processor 中的第一个logical processor 放在前面。否则可能会有未知结果和造成系统性能下降。

声明顺序如下图:

 

 

Q:  针对HTOS需要做哪些事情呢

A:  先说linux, 首先系统要使能CONFIG_X86_HT.

接着linux使用CPUID 来建立系统中processor的拓扑结构,后续会分析。而MADT中的HT信息,现在还没有使用,linuxMADT列出的processor 都是平等的。(如果不对,请指正)

最后使用HT scheduler 来优化调度。

更多详细信息,参见Multi-core and Linux* Kernel papergoogle一下就可以了。

 

对于windowswindows xp2003以后就可以感知HT了。之前的windows系统都会认为HT就是SMP系统。windows会使用MADT提供的HT 信息,同时提供HT scheduler

 

Q:针对HT,应用需要做什么

A:可以什么都不做,让硬件做,OS做。当然也可以识别HT,进行优化,这取决于应用实现。

 

重要参考文档:

Multi-core and Linux* Kernel paper

Windows Support for Hyper-Threading Technology

 

Methods to Utilize Intel's Hyper-Threading Technology with Linux*

http://software.intel.com/en-us/articles/methods-to-utilize-intels-hyper-threading-technology-with-linux/

 

Hyper Threading Implications and Setup on Microsoft Operating Systems

http://software.intel.com/en-us/articles/hyper-threading-implications-and-setup-on-microsoft-operating-systems/

 

Hyper-Threading Technology Implications and Setup on Linux* Operating Systems

http://software.intel.com/en-us/articles/hyper-threading-technology-implications-and-setup-on-linux-operating-systems/

 

 

 

 

 

 

你可能感兴趣的:(X86,linux,kernel,BIOS)