I did some web searching and learned that there is a way to do this using
CPUID, by manipulating the APIC ID and also knowing the number of physical
processors and number of cores per processor. However there is no clear
cookbook example that does exactly what GetCurrentProcessorNumber() does.
Does anyone have any pointers or code fragments for doing this?
You can use the following APIs to retrieve information about
the current system, including the the number of processors in
the system:
GetSystemInfo()
GetNativeSystemInfo()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsysteminfo.asp
Hope these suggestions helps,
Kellie.
Thanks for the info, but it's not quite what I need. I already know about
these APIs, which tell me how many processors are on the system. What I need
to know (for my multithreaded app), is for a thread to query which processor
it is running on at any particular instance. IOW, I need
GetCurrentProcessorNumber() -- but on Win XP.
-Ron
The problem is that by the time you get the data it can be incorrect,
you call for the processor number and a scheduling event occurs and you
change processors. There is such a call in the kernel, but even there it
is of limited use unless you either block scheduling or set the threads
affinitiy to a specific processor.
--
Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply
"Ron Kuper" <RonK...@discussions.microsoft.com> wrote in message
news:9F858DC7-F47F-4997...@microsoft.com...
How about the following API, you can explicitly decide on
which CPU each thread will be executed:
SetThreadIdealProcessor()
Kellie.
I know that the data might be incorrect. What we're trying to do is
implement an "approximate CPU meter" in our application (multithreaded
digital audio workstation). It doesn't have to be perfect. I just want to
know, before one of our threads starts performing expensive DSP, which CPU is
it running on, so that we can show a per-CPU measure. (Note I don't want to
use the APIs that perfom uses because that measures more overhead than I want
to provide.)
So back to the original question -- anybody have code to mimic what
GetCurrentProcessorNumber does?
-Ron
-Ron
...
//
// Get the current processor number
//
__inline ULONG KeGetCurrentProcessorNumber(VOID)
{
__asm { movzx eax, _PCR KPCR.Number }
}
with
#define _PCR fs:[0]
...
"frank" <fr...@discussions.microsoft.com> wrote in message
news:DB239684-C416-462A...@microsoft.com...
Because of the high overhead of kernel call.
--PA
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.
> 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
"Steve Dispensa" <disp...@positivenetworks.net> wrote in message
news:C08BBADB.AC51%disp...@positivenetworks.net...
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_...@valhallalegends.com> wrote in message
news:O5suzsvd...@TK2MSFTNGP02.phx.gbl...
Then... how you explain that this useless API that has no value but
bells and whistes has been added in win2003? What for?
--PA
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" <al...@earthlink.net> wrote in message
news:el$Cz71dG...@TK2MSFTNGP04.phx.gbl...
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 #vMXfe7d...@TK2MSFTNGP03.phx.gbl,
The question was how to do something like GetCurrentProcessorNumber in user
mode on pre-Win2003 systems. By saving the apic id and then using the
mapping of apic id to processor number you can log which processor a thread
appeared to have been running on with a similar degree of accuracy as to
what GetCurrentProcessorNumber would have provided on more recent platforms.
"Steve Dispensa" <disp...@positivenetworks.net> wrote in message
news:C08D5BBE.AD2A%disp...@positivenetworks.net...