🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
8 messages
8 total messages Started by Dennis Jones Sun, 14 Jan 2007 08:19
Getting a thread's state and CPU utilization
#2710
Author: Dennis Jones
Date: Sun, 14 Jan 2007 08:19
18 lines
1648 bytes
Hello,

Does anyone know if there is a way to determine the state of a currently 
running thread?  I'd like to be able to find out if a thread is executing, 
terminated, or waiting on a synchronization object (such as a critical 
section or mutex).  There seems to be support for this capability in .NET 
(according to MDSN, it's in the System.Threading namespace), which suggests 
that this must be possible in Windows, but I would need to do it using Win32 
API functions.

Also, is it possible to get a thread's current CPU utilization?  For 
example, if my application is using 80-90% of the CPU resources, I'd really 
like to be able to determine which threads are very busy, and which ones are 
using little or no CPU time.

Thanks,

- Dennis 
Re: Getting a thread's state and CPU utilization
#2711
Author: Bob Gonder
Date: Sun, 14 Jan 2007 13:05
33 lines
2147 bytes
Dennis Jones wrote:

>Does anyone know if there is a way to determine the state of a currently 
>running thread?
>  I'd like to be able to find out if a thread is executing, terminated,

GetExitCodeThread()

or check the signal status of the thread handle.

WaitForSingleObject( hThread, 0 )

> or waiting on a synchronization object (such as a critical 
>section or mutex).

Set a flag in your thread data indicatiing this ?

>Also, is it possible to get a thread's current CPU utilization?

I don't think in one call, but GetThreadTimes() might be used to
accumulate statistics over several calls.

>example, if my application is using 80-90% of the CPU resources, I'd really 
>like to be able to determine which threads are very busy, and which ones are 
>using little or no CPU time.

For debugging, and figuring out how to automate it (if this is your
goal) you might reduce the priority of one thread at a time, and see
how that affects the usage.
SetThreadPriority()
Since you are wanting to reduce priority, you might also want to use
the mis-named  SetThreadPriorityBoost() (it actually turns off the
boost)
Re: Getting a thread's state and CPU utilization
#2712
Author: Dennis Jones
Date: Sun, 14 Jan 2007 16:52
31 lines
2066 bytes
"Bob Gonder" <notbg@notmindspring.invalid> wrote in message 
news:999kq2l4vmj0mq60qde1gvpcom1l4arf8a@4ax.com...
> Dennis Jones wrote:
>
>>Does anyone know if there is a way to determine the state of a currently
>>running thread?
>>  I'd like to be able to find out if a thread is executing, terminated,
>
> GetExitCodeThread()
>
> or check the signal status of the thread handle.
>
> WaitForSingleObject( hThread, 0 )

Thanks, this gives a pretty generic result, but it might be all I need.


>>Also, is it possible to get a thread's current CPU utilization?
>
> I don't think in one call, but GetThreadTimes() might be used to
> accumulate statistics over several calls.

Well, I've already got the thread times data, which doesn't really tell me 
what the thread is doing *right now*, but I will be collecting a history, 
and as you say, that might help me determine if a partcular thread is stuck 
in an infinite loop, or if there were a race condition.  Unfortunately, I 
can't detect dead-lock this way, but then dead-lock doesn't eaxctly cause 
the CPU to scream either!

- Dennis 
Re: Getting a thread's state and CPU utilization
#2713
Author: Dennis Jones
Date: Sun, 14 Jan 2007 17:25
27 lines
2021 bytes
"Dennis Jones" <nospam@nospam.com> wrote in message 
news:45aa5fbf$1@newsgroups.borland.com...
>
> "Bob Gonder" <notbg@notmindspring.invalid> wrote in message 
> news:999kq2l4vmj0mq60qde1gvpcom1l4arf8a@4ax.com...
>> Dennis Jones wrote:
>>
>>>Does anyone know if there is a way to determine the state of a currently
>>>running thread?
>>>  I'd like to be able to find out if a thread is executing, terminated,
>>
>> GetExitCodeThread()
>>
>> or check the signal status of the thread handle.
>>
>> WaitForSingleObject( hThread, 0 )
>
> Thanks, this gives a pretty generic result, but it might be all I need.

On second thought, that won't work.  WaitForSingleObject wants the handle of 
an object to wait on, and for threads, the handle is only signaled when the 
thread terminates.  So if it is not signalled, all I know is that it hasn't 
terminated -- but I want to know whether or not the thread is in a wait 
state (waiting on a synchronization object).

- Dennis 
Re: Getting a thread's state and CPU utilization
#2714
Author: Bob Gonder
Date: Sun, 14 Jan 2007 19:46
20 lines
1840 bytes
Dennis Jones wrote:

>thread terminates.  So if it is not signalled, all I know is that it hasn't 
>terminated -- but I want to know whether or not the thread is in a wait 
>state (waiting on a synchronization object).

And for that part, I had suggested adding a thread-data flag that you
would set before and clear after going into wait-states.

> Unfortunately, I can't detect dead-lock this way,

dead-locks should never happen.
My own 2 rules are:
Always lock in the same order
	Don't lock a,b in one place then b,a in another.
Always unlock in the exact opposite order (FILO).
	Don't place a second lock at the same level until the first is
released.

Oh, and #3...don't lock without a good reason.
Re: Getting a thread's state and CPU utilization
#2715
Author: Dennis Jones
Date: Sun, 14 Jan 2007 20:10
26 lines
2100 bytes
"Bob Gonder" <notbg@notmindspring.invalid> wrote in message 
news:up0lq2hp43a79vr8hdd05299c556be7bc8@4ax.com...
> Dennis Jones wrote:
>
>>thread terminates.  So if it is not signalled, all I know is that it 
>>hasn't
>>terminated -- but I want to know whether or not the thread is in a wait
>>state (waiting on a synchronization object).
>
> And for that part, I had suggested adding a thread-data flag that you
> would set before and clear after going into wait-states.


Yeah, sorry...I guess I misread what you had said.  Unfortunately, I don't 
have full control of all source code, so I won't always have the ability to 
set state indicators.  Besides, that would be a lot of effort...it would be 
much nicer to get that information from the OS, which probably has it.


> dead-locks should never happen.

I don't have any (that I am aware of) in my code, but a general method of 
detection would be a nice thing to have in my toolbox.

- Dennis 
Re: Getting a thread's state and CPU utilization
#2716
Author: Dennis Jones
Date: Mon, 15 Jan 2007 05:40
31 lines
2264 bytes
"Dennis Jones" <nospam@nospam.com> wrote in message 
news:45aa8e3b$1@newsgroups.borland.com...
>
> "Bob Gonder" <notbg@notmindspring.invalid> wrote in message 
> news:up0lq2hp43a79vr8hdd05299c556be7bc8@4ax.com...
>> Dennis Jones wrote:
>>
>>>thread terminates.  So if it is not signalled, all I know is that it 
>>>hasn't
>>>terminated -- but I want to know whether or not the thread is in a wait
>>>state (waiting on a synchronization object).
>>
>> And for that part, I had suggested adding a thread-data flag that you
>> would set before and clear after going into wait-states.
>
>
> Yeah, sorry...I guess I misread what you had said.  Unfortunately, I don't 
> have full control of all source code, so I won't always have the ability 
> to set state indicators.  Besides, that would be a lot of effort...it 
> would be much nicer to get that information from the OS, which probably 
> has it.

Thanks to a tip from someone in ms.public.win32.programmer.kernel, I found 
the 'State' and 'WaitReason' members in the SYSTEM_THREAD_INFORMATION 
structure provided by NtQuerySystemInformation(), which I am already 
calling!  Beauty!

I'm going to try it tomorrow.

- Dennis
Re: Getting a thread's state and CPU utilization
#2717
Author: Mike Collins
Date: Mon, 15 Jan 2007 10:43
18 lines
1453 bytes
Sorry for the late posting - looks like you've already hit the nail on the 
head though.

NtQuerySystemInformation() is exactly what you want to use.  I've got a 
semi-working example if you need any information on using the native api's.

Also, if you trying to debug - consider having a look at ProcExplorer from 
sysinternals - gives you pretty much everything you need to know anout the 
state of everything on your system

HTH

Mike C

"Dennis Jones" <nospam@nospam.com> wrote in message 
news:45a9e77e$1@newsgroups.borland.com...
> Hello,
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads