On a multi-processor system, what Windows API calls can I make to obtain the
number of the processor a thread is currently running on?
I want to log information from running applications and it might be useful
to know that two lines in the log file were entered by threads executing
simultaneously. I would add the CPU number to the log entry.
--
James Antognini
Windows DDK and WDK Support
This posting is provided "AS IS" with no warranties, and confers no rights.
"Gerry Suggitt" <Gerry Sug...@discussions.microsoft.com> wrote in message
news:524A3003-65E5-443B...@microsoft.com...
Another alternative is to restrict a thread to a specific processor using
SetThreadAffinity -- then you always know it's runinng on a particular
processor.
"Gerry Suggitt" <Gerry Sug...@discussions.microsoft.com> wrote in message
news:524A3003-65E5-443B...@microsoft.com...
If you're interested in whether threads are running concurrently (and
which thread generated which output), just log the thread ID.
Unless tied to a specific processor, the processor number may change
during your log, during the output, when returning from the output,
etc.
--
Sev
> On a multi-processor system, what Windows API calls can I make to obtain the
> number of the processor a thread is currently running on?
A thread can only run on *one* processor at a time...
Take a look at
See also: Multiple Processors
http://msdn.microsoft.com/library/en-us/dllproc/base/multiple_processors.asp
> I want to log information from running applications and it might be useful
> to know that two lines in the log file were entered by threads executing
> simultaneously. I would add the CPU number to the log entry.
For this you should use the ThreadID!
See: GetCurrentThreadId
http://msdn.microsoft.com/library/en-us/dllproc/base/getcurrentthreadid.asp
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
and if you are not on win2003 - use cpuid instruction.
Processor is identified by it's local apic id and CPU index
(logical CPUs of HT processor have same APIC id).
The cpuid app note on intel site has all relevant details.
The problem is that the code sequience for cpuid is not
atomic, so CPU can switch in the middle...
--PA
But you still can distinguish them via cpuid -- this was discussed here a
while ago.
> The cpuid app note on intel site has all relevant details.
CPU can also be differentiated by their IDTs, which also has been discussed
here.
> The problem is that the code sequience for cpuid is not
> atomic, so CPU can switch in the middle...
Who cares, the result hardly means anything anyway.
S
I know The thread can hop in an instant.
The ThreadId will not change when it hops - so logging the threadId does not
help identify the CPU.
But perhaps it is folly, as some have suggested. If I have:
Log("about to do X");
X();
Log("finished X" );
And the log reads:
2005 Aug 27 10:05:03.103 thread=292 cpu=1 about to do X
2005 Aug 27 10:05:03.104 thread=292 cpu=1 finished X
There is no guarantee that X did not execute on CPU 2.
But knowing that X executed on CPU 1 is not really important. I really want
to know that two threads are executing simultaneously. So if I see:
2005 Aug 27 10:05:03.103 thread=292 cpu=1 about to do X
2005 Aug 27 10:05:03.120 thread=306 cpu=2 about to do Y
2005 Aug 27 10:05:03.810 thread=292 cpu=1 finished X
2005 Aug 27 10:05:03.850 thread=306 cpu=2 finished Y
I know that thread 292 and 306 are executing simultaneously.
Of course, if we carry the hopping argument to its extreme, there is no
guarantee that thread 306 executed on CPU 1 but hopped over to CPU 2 just as
the "Log()" function was executed.
But this is not likely especially when the thread has affinity for the CPU
it last executed on.
But in conclusion, perfhaps there is not as much useful information to be
derived from the CPU identification as I originally thought.
> I know The thread can hop in an instant.
>
> The ThreadId will not change when it hops - so logging the threadId does not
> help identify the CPU.
Why do you need to identify the CPU?
You only need to identify the thread!
> But perhaps it is folly, as some have suggested. If I have:
> Log("about to do X");
> X();
> Log("finished X" );
>
> And the log reads:
> 2005 Aug 27 10:05:03.103 thread=292 cpu=1 about to do X
> 2005 Aug 27 10:05:03.104 thread=292 cpu=1 finished X
>
> There is no guarantee that X did not execute on CPU 2.
What does this matter?
> But knowing that X executed on CPU 1 is not really important. I really want
> to know that two threads are executing simultaneously. So if I see:
> 2005 Aug 27 10:05:03.103 thread=292 cpu=1 about to do X
> 2005 Aug 27 10:05:03.120 thread=306 cpu=2 about to do Y
> 2005 Aug 27 10:05:03.810 thread=292 cpu=1 finished X
> 2005 Aug 27 10:05:03.850 thread=306 cpu=2 finished Y
>
> I know that thread 292 and 306 are executing simultaneously.
>
> Of course, if we carry the hopping argument to its extreme, there is no
> guarantee that thread 306 executed on CPU 1 but hopped over to CPU 2 just as
> the "Log()" function was executed.
>
> But this is not likely especially when the thread has affinity for the CPU
> it last executed on.
>
> But in conclusion, perfhaps there is not as much useful information to be
> derived from the CPU identification as I originally thought.
Yes.