Re: GetCurrentProcessorNumber() for XP?



On 5/12/06 12:34 PM, in article
D203BD13-2430-4A4B-9321-D0E8076B1A71@xxxxxxxxxxxxx, "Ron Kuper"
<RonKuper@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Thanks for the help, everyone. I figured out a way to do it.

The CPUID instruction, called with EAX=1, returns the APIC ID in the upper 8
bits of EBX. This isn't the processor number, but it is unique for each
logical CPU. I simply take that number and use a tiny map (implemented as
256 byte array) to map APIC ID -> processor number, i.e., if I don't see an
APIC ID in the map I increment a processor number counter and assign it to
the APIC ID. Does the trick, all in user mode, and quite efficient.

I'm sure you understand this already, but just for the record, the results
of your CPUID call can be wrong by the very next CPU instruction. There is
nothing stopping the OS from context switching away and scheduling you
elsewhere next time you come up for cycles. This will probably be right a
lot of the time, but it will be wrong some of the time.

Also, this is obviously not terribly portable or easy to maintain (no inline
ASM on x64, etc).

Otherwise, interesting solution.

-Steve

.