Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

how to get cpu usage by prcess

瀏覽次數:960 次
跳到第一則未讀訊息

Tomasz Littau

未讀,
2006年9月22日 下午1:39:422006/9/22
收件者:
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

未讀,
2006年9月22日 下午2:45:272006/9/22
收件者:
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

未讀,
2006年9月22日 下午3:18:552006/9/22
收件者:
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

未讀,
2006年9月23日 上午8:03:322006/9/23
收件者:
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

訊息已遭刪除

Tomasz Littau

未讀,
2006年9月23日 上午8:30:142006/9/23
收件者:
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;

訊息已遭刪除

Tomasz Littau

未讀,
2006年9月23日 上午9:19:522006/9/23
收件者:
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;
}

訊息已遭刪除

Tomasz Littau

未讀,
2006年9月23日 上午10:05:482006/9/23
收件者:

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 );
}
//---------------------------------------------------------------------------

訊息已遭刪除
0 則新訊息