Re: getting a thread's state and CPU utilization

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: Polling, Interrupts, DMA, Synchronous, Asynchronous I/O Definitions
    ... > Sorry, but even with blocking, polling is still polling ... > (though blocking makes it less costly). ... > Even though a blocking API is called, the loop is still ... "GetMessage" does with messages, but on the condition of "key ...
    (alt.lang.asm)
  • Re: non blocking sockets
    ... > consider anything that requires waiting for a totally external event ... If these are your opinions about what the standard should ... want to say blocking is or isn't, ... > waiting for a page fault or local memory to become available to be ...
    (comp.unix.programmer)
  • FIFO Proccess deadlocked
    ... specific time and check again in a loop. ... a delay loop waiting for 1 message to finish. ... I don't see the dehydration mechanism is letting the desiered msg the ...
    (microsoft.public.biztalk.general)
  • Re: Please help a young, clueless implementor: meaning of O_NONBLOCK
    ... At least for pipes, sockets it is implemented ... (Of course, some waiting can be ... to support nonblocking, and most implementations doesn't support such. ... blocking and nonblocking mode you have to implement this switching for ...
    (comp.unix.programmer)
  • Re: myriad/event comment (was RE: [ANN] Rio 0.3.4)
    ... You need to just access a DB without blocking all the other libevent ... something to support registering a Ruby thread with libevent for ... And myriad will loop the block every 2 seconds without blocking libevent. ...
    (comp.lang.ruby)