Re: getting a thread's state and CPU utilization
- From: David Jones <ncic@xxxxxxxxxx>
- Date: Sun, 14 Jan 2007 19:00:57 -0600
Dennis Jones <nospam@xxxxxxxxxx> wrote...
Using the Win32 API, does the OS provide the means to determine the state of
a thread -- that is, running, suspended, or waiting on a synchronization
object via one of the Wait... calls (knowing the type of synchronization
object and/or what Wait... function was used would be nice too, but not
critical)?
From user mode, the Debug API might give you most of what you need:
http://msdn.microsoft.com/library/en-us/dndebug/html/msdn_debugEH.asp
I'm not really sure how to get all that information specifically,
although I know Process Explorer is able to get it somehow. I'm sure
someone else in this group knows how to get this and can shed more light
than I can.
Also, is it possible to determine a thread's current CPU utilization (i.e. %
of CPU time currently being used)? I can get the total time used by a
thread via GetThreadTimes, but that doesn't tell me what the thread is doing
*right now.*
At any particular point, a thread is either using 100% of a processor or
0% of a processor (running or not running). So, for total CPU usage, a
thread is either using 0% or (100/#processors)%.
Note that on a uniprocessor machine, you don't even need an API call to
determine which threads are running at that instant and which are not --
if you're measuring, it means your thread is running (and therefore no
other threads are running).
The only really meaningful data is an average over some time span, so
you'd have to use GetThreadTimes: (current - previous) / timespan.
Barring any of the above, are there known techniques for detecting when/if a
thread is :
a) one of the participants of dead-lock
b) a particpant in a race condition
c) in an infinite (possibly very tight) loop
Wow. These are pretty difficult to detect these in the general case,
although you are theoretically able to check for a few specific common
cases (eg: deadlock by waiting on objects that form a loop, such as
threads 1,2,3 holding locks A,B,C and waiting on B,C,A).
For example, how would the system know that a deadlock occurred in the
following example...?
Process A -- Waits on event X and then sends a message M to a window W
in process B.
Process B -- Sets event X when processing message M on window W.
Granted this is a totally contrived and really stupid example and no one
would (intentionally) code their application this way, but deadlock
usually looks like this when isolated anyway. :) How would the system
know the condition in process B without analyzing the source code of the
process?
You couldn't just look for a long blocking wait since it might be
legitimate, such as a shell replacement program blocking on a process
handle that the user hasn't terminated or an application blocking on an
event triggered by hardware that only occurs rarely.
Similarly, there's so many ways to create race conditions and infinite
loops that it's very complex to try to detect all situations that would
cause them. How could you tell an infinite loop from a polling loop
around some circumstance that takes a really long time to complete? (As
in the long blocking wait above.)
Maybe if you were more specific as to why you need this information we
might be able to point you in the right direction. Sorry I couldn't be
any more help.
David
.
- Follow-Ups:
- Re: getting a thread's state and CPU utilization
- From: Dennis Jones
- Re: getting a thread's state and CPU utilization
- References:
- getting a thread's state and CPU utilization
- From: Dennis Jones
- getting a thread's state and CPU utilization
- Prev by Date: getting a thread's state and CPU utilization
- Next by Date: Re: How to eject removable devices programmatically?
- Previous by thread: getting a thread's state and CPU utilization
- Next by thread: Re: getting a thread's state and CPU utilization
- Index(es):
Relevant Pages
|