Re: GetCurrentProcessorNumber() for XP?
- From: "Skywing" <skywing_NO_SPAM_@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 13 May 2006 22:07:47 -0400
The former can be easily solved by taking the process affinity mask and then
in sequence setting a thread to be assigned to only one processor, executing
the appropriate cpuid instruction, then changing the affinity to the next
processor.
"Steve Dispensa" <dispensa@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:C08BBADB.AC51%dispensa@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: GetCurrentProcessorNumber() for XP?
- From: Alexander Grigoriev
- Re: GetCurrentProcessorNumber() for XP?
- References:
- Re: GetCurrentProcessorNumber() for XP?
- From: Skywing
- Re: GetCurrentProcessorNumber() for XP?
- From: Pavel A.
- Re: GetCurrentProcessorNumber() for XP?
- From: Steve Dispensa
- Re: GetCurrentProcessorNumber() for XP?
- Prev by Date: Re: Determining a DLL's preferred load address, how?
- Next by Date: Re: CreateProcess parameter interception at NtCreateSection
- Previous by thread: Re: GetCurrentProcessorNumber() for XP?
- Next by thread: Re: GetCurrentProcessorNumber() for XP?
- Index(es):