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

how to get cpu usage by prcess

946 views
Skip to first unread message

Tomasz Littau

unread,
Sep 22, 2006, 1:39:42 PM9/22/06
to
Hello

I`m new here and if this question in not for this group please tell me
where i should ask about it

i need to get a process cpu usage by his ID in win32, how can i do it ?

best regards
--
_____________________________

tlen: lemkat
gg: 4891654

http:///www.lemkat.soft-pc.pl
_____________________________

Bertel Brander

unread,
Sep 22, 2006, 2:45:27 PM9/22/06
to
Tomasz Littau wrote:
> Hello
>
> I`m new here and if this question in not for this group please tell me
> where i should ask about it

I think it is

> i need to get a process cpu usage by his ID in win32, how can i do it ?

GetProcessTimes:

http://windowssdk.msdn.microsoft.com/en-us/library/ms683223.aspx

--
Just another homepage:
http://damb.dk
But it's mine - Bertel

Tomasz Littau

unread,
Sep 22, 2006, 3:18:55 PM9/22/06
to
Bertel Brander napisał(a):

> Tomasz Littau wrote:
>> Hello
>>
>> I`m new here and if this question in not for this group please tell me
>> where i should ask about it
>
> I think it is
>
>> i need to get a process cpu usage by his ID in win32, how can i do it ?
>
> GetProcessTimes:
>
> http://windowssdk.msdn.microsoft.com/en-us/library/ms683223.aspx
>
Thank you

Tomasz Littau

unread,
Sep 23, 2006, 8:03:32 AM9/23/06
to
Bertel Brander napisał(a):

> Tomasz Littau wrote:
>> Hello
>>
>> I`m new here and if this question in not for this group please tell me
>> where i should ask about it
>
> I think it is
>
>> i need to get a process cpu usage by his ID in win32, how can i do it ?
>
> GetProcessTimes:
>
> http://windowssdk.msdn.microsoft.com/en-us/library/ms683223.aspx
>
Thank you, could some one give me some sample code how can i calculate
this cpu usage by process using GetProcessTime(), best in c++

thanks a lot

Message has been deleted

Tomasz Littau

unread,
Sep 23, 2006, 8:30:14 AM9/23/06
to
Bertel Brander napisał(a):

> Tomasz Littau wrote:
>> Hello
>>
>> I`m new here and if this question in not for this group please tell me
>> where i should ask about it
>
> I think it is
>
>> i need to get a process cpu usage by his ID in win32, how can i do it ?
>
> GetProcessTimes:
>
> http://windowssdk.msdn.microsoft.com/en-us/library/ms683223.aspx
>
Thank you, could some one give me some sample code how can i calculate
this cpu usage by process using GetProcessTime(), best in c++

i write something like this but it not work:

int TFMain::iFunGetCpuProcUsage( HANDLE hHandle )
{
LONG lKernel, lUser, lProcTime;
int iProcUsage;
DWORD dwTime;

FILETIME ftCreate, ftExit, ftUser, ftKernel,
ftNewUser, ftNewKernel;
DWORD dwOldTime, dwNewTime;

dwOldTime = timeGetTime();
GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel );

dwNewTime = timeGetTime();
GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftNewUser, &ftNewKernel );


lKernel = ftNewKernel.dwLowDateTime - ftKernel.dwLowDateTime;
lUser = ftNewUser.dwLowDateTime - ftUser.dwLowDateTime;
lProcTime = lKernel + lUser;
dwTime = dwNewTime-dwOldTime;

if( dwTime == 0 )
{
Sleep( 100 );
dwNewTime = timeGetTime();
dwTime = dwNewTime-dwOldTime;
}

iProcUsage = ( (lProcTime*100 ) / dwTime );

return iProcUsage;

Message has been deleted

Tomasz Littau

unread,
Sep 23, 2006, 9:19:52 AM9/23/06
to
Bertel Brander napisał(a):

> Tomasz Littau wrote:
>> Hello
>>
>> I`m new here and if this question in not for this group please tell me
>> where i should ask about it
>
> I think it is
>
>> i need to get a process cpu usage by his ID in win32, how can i do it ?
>
> GetProcessTimes:
>
> http://windowssdk.msdn.microsoft.com/en-us/library/ms683223.aspx
>
Thank you, could some one give me some sample code how can i calculate
this cpu usage by process using GetProcessTime(), best in c++

i write something like this but it not work, the value is always 0:

int TFMain::iFunGetCpuProcUsage( HANDLE hHandle )
{

LONG lOldUser, lNewUser, lOldKernel, lNewKernel, lProcUsage, lUser, lKernel;
DWORD dwOldTime, dwNewTime, dwTime;
FILETIME ftCreate, ftExit, ftUser, ftKernel;
int iProcUsage;

dwOldTime = timeGetTime();
GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel );

// usertime
if( ftUser.dwLowDateTime < 0 ) ftUser.dwLowDateTime = 0;
if( ftUser.dwHighDateTime < 0 ) ftUser.dwHighDateTime = 0;
lOldUser = (((long)ftUser.dwHighDateTime) << 32 ) + ftUser.dwLowDateTime;

// kernel
if( ftKernel.dwLowDateTime < 0 ) ftKernel.dwLowDateTime = 0;
if( ftKernel.dwHighDateTime < 0 ) ftKernel.dwHighDateTime = 0;
lOldKernel = (((long)ftKernel.dwHighDateTime) << 32 ) +
ftKernel.dwLowDateTime;

dwNewTime = timeGetTime();
GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel );

// usertime
if( ftUser.dwLowDateTime < 0 ) ftUser.dwLowDateTime = 0;
if( ftUser.dwHighDateTime < 0 ) ftUser.dwHighDateTime = 0;
lNewUser = (((long)ftUser.dwHighDateTime) << 32 ) + ftUser.dwLowDateTime;

// kernel
if( ftKernel.dwLowDateTime < 0 ) ftKernel.dwLowDateTime = 0;
if( ftKernel.dwHighDateTime < 0 ) ftKernel.dwHighDateTime = 0;
lNewKernel = (((long)ftKernel.dwHighDateTime) << 32 ) +
ftKernel.dwLowDateTime;

lKernel = lNewKernel - lOldKernel;
lUser = lNewUser - lOldUser;
dwTime = dwNewTime-dwOldTime;

if( dwTime == 0 )
{
Sleep( 100 );
dwNewTime = timeGetTime();
dwTime = dwNewTime-dwOldTime;
}

iProcUsage = (((lKernel+lUser)*100 ) / dwTime );

return iProcUsage;
}

Message has been deleted

Tomasz Littau

unread,
Sep 23, 2006, 10:05:48 AM9/23/06
to

This is a working kode ;)

best regards

//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <conio.h>
#include <mmsystem.h>
#include <tlhelp32.h>
#include <psapi.h>
#pragma hdrstop
#pragma argsused

int iFunGetTime( FILETIME ftTime )
{
SYSTEMTIME stTime;
int iTime;

FileTimeToSystemTime( &ftTime, &stTime );

iTime = stTime.wSecond * 1000;
iTime += stTime.wMilliseconds;

return iTime;
}


int iFunGetCpuProcUsage( HANDLE hHandle )


{
LONG lOldUser, lNewUser, lOldKernel, lNewKernel, lProcUsage, lUser, lKernel;
DWORD dwOldTime, dwNewTime, dwTime;
FILETIME ftCreate, ftExit, ftUser, ftKernel;
int iProcUsage;

/*
Yes:
GetProcessTimes( GetCurrentProcess(), t1, t2, t3, t4 );
t3 is Kerneltime in 100ns-Setps
t4 is Usertime in 100ns-Setps
t3+t4 is overall CPU-Time of Process

use a timer and calculate Percent = (actualtime-oldtime) /
((actualTickCount-OldTickCount)*100)
works fine and results are equal to Taskmanager!
*/


dwOldTime = timeGetTime();
if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) )
{
printf("error old getprocesstime %d", GetLastError() );
getch();
}

lOldUser = iFunGetTime( ftUser );
lOldKernel = iFunGetTime( ftKernel );

Sleep( 1000 );

dwNewTime = timeGetTime();
if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) )
{
printf("error new getprocesstime %d", GetLastError() );
getch();
}

lNewUser = iFunGetTime( ftUser );
lNewKernel = iFunGetTime( ftKernel );

lKernel = lNewKernel - lOldKernel;
lUser = lNewUser - lOldUser;
dwTime = dwNewTime-dwOldTime;

if( dwTime == 0 )
{
Sleep( 100 );
dwNewTime = timeGetTime();
dwTime = dwNewTime-dwOldTime;
}

iProcUsage = (((lKernel+lUser)*100 ) / dwTime );

return iProcUsage;
}


int main(int argc, char* argv[])
{
void *Snap;
PROCESSENTRY32 pe;
HEAPENTRY32 he;
HANDLE hHandle;
HANDLE hProcess;
AnsiString sText;
PROCESS_MEMORY_COUNTERS pmc;


memset( &pmc , 0, sizeof( pmc ) );
pmc.cb = sizeof( pmc );
Snap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS , 0);
pe.dwSize = sizeof( PROCESSENTRY32 );


if( Process32First( Snap , &pe ) )
{
do
{
if( strstr( pe.szExeFile , "bcb.exe" ) )
{
sText = pe.szExeFile;
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION ,
false ,
pe.th32ProcessID );
}
}
while( Process32Next( Snap , &pe ) );
CloseHandle( Snap );
}

while( !kbhit() )
{
clrscr();
printf("Uzycie procesora dla %s wynosi %d", sText.c_str() ,
iFunGetCpuProcUsage( hProcess ) );
Sleep( 1000 );
}
CloseHandle( hProcess );
}
//---------------------------------------------------------------------------

Message has been deleted
0 new messages