Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CPU Load in C++

35 views
Skip to first unread message

Oscar

unread,
Sep 16, 2004, 4:14:59 AM9/16/04
to
Hi all,

I'm writing some monitoring routines for Windows CE in order test other
applications I'm writting.
I have found some methods to retreive: Memory and system information
however, nothing to measure CPU load. I know some software exist (e.g.
RhinoStats) but what I need is a C++ code as I plan to use it whithin my
apps.

If anyone can help me, I would be very graithful.

Thank's in advance

Oscar

Oscar

unread,
Sep 16, 2004, 4:23:11 AM9/16/04
to
I forget to precise my soft: I'm using EVC++ 3.0 and an IPAQ with
PocketPC 2002 OS.
thanks

Andrey Yatsyk

unread,
Sep 16, 2004, 5:09:59 AM9/16/04
to
Look at GetThreadTimes. Threads could be enumerated through
Thread32First/Thread32Next.

--
Andrey Yatsyk [Windows Embedded MVP]
"Oscar" <os...@oscarland.com> wrote in message
news:O0c$Jf8mEH...@TK2MSFTNGP11.phx.gbl...

Oscar

unread,
Sep 16, 2004, 6:42:17 AM9/16/04
to
Thank you andrey,

Now, I'm able to calculate times for whole threads in the system.
I'm calculating the differences between two samples of :
TotalKernelTime, TotalUserTime, TotalCreationTime, TotalExitTime.

Know, how can I use these values to measure the CPU load ?
Thank you again

Andrey Yatsyk

unread,
Sep 16, 2004, 7:08:21 AM9/16/04
to
I think that cpu usage ~
(TotalKernelTimeAtTime2-TotalKernelTimeAtTime1+TotalUserTimeAtTime2-TotalUserTimeAtTime1)(time2-time1)
in assumption that there is no new threads created or deleted.

--
Andrey Yatsyk [Windows Embedded MVP]

"Oscar" <os...@oscarland.com> wrote in message

news:OFJEAt9m...@TK2MSFTNGP10.phx.gbl...

Oscar

unread,
Sep 16, 2004, 9:15:13 AM9/16/04
to
Yet a question, I hope it will be the last ;=)

Calling Thread32Next returns a struct THREADENTRY32, of which the member
"th32ThreadID" is given as DWORD. However, I need a HANDLE to that
thead in order to call GetThreadTimes(..).
I tried to cast "th32ThreadID" to HANDLE, but it does not work. I got
Error 6: ERROR_INVALID_HANDLE.

So the question is: how can I retrieve the HANDLE of a thread frim its
ID. ? (With evc++ 3.0 of course).

Thanks

Oscar

Andrey Yatsyk

unread,
Sep 16, 2004, 10:11:49 AM9/16/04
to
You can get handle from id by bare typecasting, but you should set
permissions to access other processes by SetProcPermissions before.

--
Andrey Yatsyk [Windows Embedded MVP]
"Oscar" <os...@oscarland.com> wrote in message
news:OvucdC$mEHA...@TK2MSFTNGP15.phx.gbl...

Oscar

unread,
Sep 20, 2004, 10:39:55 AM9/20/04
to
Hi,
SetProcPermissions is not avalaible under PocketPC 2002.
I tried everything I think (even bare typecast), I also browsed the
PocketPC help.
I did not find an answer to my problem. :=(
Below is the code I'm using to sum thread times.
Is these something wrong ?

many thanks
Oscar

int SumThreadTimes()
{
HANDLE hThreads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
int newKernel =0;
int newUser =0;
if(hThreads)
{
THREADENTRY32 entry;
entry.dwSize = sizeof(THREADENTRY32);
BOOL more = Thread32First(hThreads, &entry);
if(!more) return;
FILETIME creation, exit, kernel, user;
// add up times from all threads
while(more)
{
if(GetThreadTimes( (HANDLE) entry.th32ThreadID , &creation,
&exit, &kernel, &user)==0)
{ // HERE THE CALL RETURNS INVALID_HANDLE
/***************/
break;
}
//try we LowBytes, High Bytes keep inchangeable most of the time
newKernel += kernel.dwLowDateTime;
newUser += user.dwLowDateTime;
entry.dwSize = sizeof(THREADENTRY32);
more = Thread32Next(hThreads, &entry);
}
CloseToolhelp32Snapshot(hThreads);

Andrey Yatsyk

unread,
Sep 20, 2004, 11:48:43 AM9/20/04
to
Try to invoke this function dynamically from coredll.

--
Andrey Yatsyk [Windows Embedded MVP]
"Oscar" <os...@oscarland.com> wrote in message

news:O2JuPCyn...@TK2MSFTNGP09.phx.gbl...

Oscar

unread,
Sep 22, 2004, 5:01:22 AM9/22/04
to
Yes, it works, and seems giving correct values.
Many thanks for your help.

Oscar

0 new messages