因為最近在研究AMD CPU的控制電壓頻率的方法,
想來寫個rmclock-like的程式.
最主要的原因是rmclock沒有再更新了,
手上這顆AMD Phenome X2不支援,
只好自己來寫寫看嚕~
因為需要讓每顆CPU都要設定到,
所以使用SetThreadAffinityMask去設定要用哪顆CPU,
之後再Call Sleep(1),
這是讓目前的thread有機會content-switch.
這樣下次再被執行時,就會根據 AffinityMask 去挑選 CPU.
問題是, 這時候我想到那我怎麼確定目前thread有在某顆CPU上執行.
直覺上, 覺得 CPUID 這個x86指令應該會提供,
找了半天找到了一個ACPI ID.
你可以用CPUID EAX:1 得到在 EBX 的 31:24,
這就是 ACPI ID.
而在網路上找到這段話,
In MP systems, the local APIC ID is also used as a processor ID by the BIOS and the operating system. Some processors permit software to modify the APIC ID. However, the ability of software to modify the APIC ID is processor model specific. Because of this, operating system software should avoid writing to the local APIC ID register. The value returned by bits 31-24 of the EBX register (when the CPUID instruction is executed with a source operand value of 1 in the EAX register) is always the Initial APIC ID (determined by the platform initialization). This is true even if software has changed the value in the Local APIC ID register.
在我這台notebook上試了一下.
在CPU 0 是會對應到 ACPI ID: 0,
而在CPU 1 則會對應到 ACPI ID: 1,
基本上是沒問題的,
但我換到另一台i5是四核心,
卻出現了0,2,4,6,
查了一下,似乎是因為 i5 是有 hyper-threading,
但是被disabled掉, 就會出現這種情況.
後來, T同事找到一個API KeGetcurrentProcessorNumber,
但是這個API是要在 kernel 裡使用的.
靈機一動, Google 看看有沒有 GetcurrentProcessorNumber,
沒想到還真的有這個API.
DWORD WINAPI GetCurrentProcessorNumber(void);
但只有 vista 之後才可以使用.
不過, 這也足夠了.