From: Tomasz Littau on 23 Sep 2006 09:19 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; } -- _____________________________ tlen: lemkat gg: 4891654 http:///www.lemkat.soft-pc.pl _____________________________
From: Tomasz Littau on 23 Sep 2006 10:05 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 ); } //---------------------------------------------------------------------------
First
|
Prev
|
Pages: 1 2 Prev: how to find source of conflicting dll Next: PDH_CSTATUS_NO_OBJECT - but why ? |