Re: GetCurrentProcessorNumber() for XP?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Maybe I don't understand you here, Skywing, but it seems like you're just
building a one->one mapping of APIC ID to CPU number; that doesn't really
speak to whether or not the OS can context switch you immediately after the
instruction executes (but before the very next instruction executes),
causing an incorrect result.

The more I think about it, the more I think it'd be rather unlikely that the
answer would actually be wrong very often. This would only happen if your
quantum expires and if the OS then switches your thread as a result (which
is not necessarily the case). On an otherwise lightly loaded system, I bet
this is very accurate indeed. Good lord, how many instructions execute in
17ms or whatever the quantum is on the OS/computer in question... We're
probably talking about round-off error here.

And, I can't think of too many (any?) cases in which that level of
uncertainty would matter, and particularly not in the arena of perf
monitoring. Heck, the monitoring itself skews the results; a little extra
uncertainty about the active processor will probably just vanish in the
noise floor.

On 5/14/06 6:23 PM, in article #vMXfe7dGHA.2416@xxxxxxxxxxxxxxxxxxxx,
"Skywing" <skywing_NO_SPAM_@xxxxxxxxxxxxxxxxxxx> wrote:

You would set the affinity for each processor in sequence to build a mapping
of apic id to cpu number.

Then, later, when you execute cpuid on a thread with multiprocessor
affinity, you can map the returned id to say processor 0, processor 1, etc.

"Alexander Grigoriev" <alegr@xxxxxxxxxxxxx> wrote in message
news:el$Cz71dGHA.1320@xxxxxxxxxxxxxxxxxxxxxxx
Well, if you set the affinity, you know on what process you'll be running
and then there's no point whatsoever to query the processor number!

On the other hand, I think the problem being discussed (custom CPU meter
of an multimedia app) is not worth bothering. That's one of bells and
whistles without real return on investment, other than bragging rights of
the programmer.

"Skywing" <skywing_NO_SPAM_@xxxxxxxxxxxxxxxxxxx> wrote in message
news:O5suzsvdGHA.1204@xxxxxxxxxxxxxxxxxxxxxxx
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








.



Relevant Pages

  • Re: GetCurrentProcessorNumber() for XP?
    ... have a correct mapping of apic id to processor number. ... building a one->one mapping of APIC ID to CPU number; ... instruction executes, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: [Lit.] Buffer overruns
    ... A CPU still executes only one instruction at a time. ... >Of all the nonsense you've ever come out with, ...
    (sci.crypt)
  • Re: [PATCH] x86 - Enhance DEBUG_RODATA support - alternatives
    ... has been pulled out of the x86 tree. ... text_poke required to support this. ... correctly and so the CPU HOTPLUG special case can be removed. ... When you use this code to patch more than one byte of an instruction ...
    (Linux-Kernel)
  • Re: Simple function arguments
    ... are 2 names refering to the same memory location and use that. ... In the internals of a CPU there are various registers. ... address is stored from where the next instruction from memory is read and executed. ... what is generally referred to as 'The stack'. ...
    (comp.lang.cpp)
  • Re: wikipedia article
    ... parallel but skewed by one instruction. ... If the first CPU instruction execution causes a miss, ... memory access. ... distinguish between instruction and data references, ...
    (freebsd-questions)