Re: About some questions on PocketPC (CPU Usage,..)
- From: "Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com>
- Date: Mon, 20 Jun 2005 14:40:43 -0700
Here's my code to do roughly the same thing 10 times (sleeping each time
through the loop for a while). I use a hash table (code not included) to
store the last thread times for active threads.
Paul T.
-----
ThreadTimes threadTimes( &hashfcn, HASH_COUNT );
DWORD permissions = GetCurrentPermissions();
SetProcPermissions( 0xffffffff );
// Do a given number of loops. In each loop, wait for a while,
// then get thread times, then calculate the change in each for
// each thread and report that, divided by the total elapsed
// time.
DWORD id = GetCurrentProcessId();
DWORD lasttc;
for ( int i = 0; i < 10; i++ )
{
DEBUGMSG( 1, ( TEXT( "Pass #%d\r\n" ), i ) );
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
DWORD tc = GetTickCount();
// Check.
if ( hSnapShot == INVALID_HANDLE_VALUE )
break;
// Enumerate the threads.
THREADENTRY32 te;
te.dwSize = sizeof( te );
if ( Thread32First( hSnapShot, &te ) )
{
do
{
// Do nothing if the thread is ours.
if ( te.th32OwnerProcessID != id )
{
HANDLE threadH = (HANDLE)te.th32ThreadID;
// Process the thread.
__int64 creation;
__int64 exit;
__int64 kernel;
__int64 user;
if ( !GetThreadTimes( (HANDLE)threadH,
(FILETIME*)&creation, (FILETIME*)&exit,
(FILETIME*)&kernel, (FILETIME*)&user ) )
{
DEBUGMSG( 1, ( TEXT( "Error in GetThreadTimes. Error code =
%d\r\n" ), GetLastError() ) );
// Remove the thread entry, if any, from the table.
threadTimes.remove( te.th32ThreadID );
}
else // if ( !GetThreadTimes( (HANDLE)te.th32ThreadID,
{
__int64 totalTime = kernel + user;
// If this is the first loop, put the values in the 'old' entries
// table and do no calcs. If this is not the first loop, do the calcs
// before saving the old values.
if ( i )
{
__int64 oldTotalTime;
// Get old totalTime.
if ( !threadTimes.findValue( te.th32ThreadID, oldTotalTime ) )
{
DEBUGMSG( 1, ( TEXT( "Error in findValue. No previous entry for
thread 0x%x\r\n" ),
te.th32ThreadID ) );
}
else
{
// Subtract.
__int64 timeSpent = totalTime - oldTotalTime;
// Calculate percent of processor total time.
__int64 tcSpent = ( tc - lasttc ) * 10000;
int percent = (int)( ( (double)timeSpent / (double)tcSpent ) *
100 );
// Only show threads which are reasonably active
if ( percent )
{
DEBUGMSG( 1, ( TEXT ("Thread 0x%x took %d%% of the time\r\n" ),
te.th32ThreadID, percent ) );
}
}
// Update time by removing key.
threadTimes.remove( te.th32ThreadID );
}
// Store new time in table.
threadTimes.insertKeyAndValue( te.th32ThreadID, totalTime );
} // if ( !GetThreadTimes( (HANDLE)te.th32ThreadID,
}
else // if ( te.th32OwnerProcessID != id )
{
// DEBUGMSG( 1, ( TEXT( "Our thread = 0x%x\r\n" ), id ) );
}
} while ( Thread32Next( hSnapShot, &te ) );
}
CloseToolhelp32Snapshot( hSnapShot );
// Save last tick count.
lasttc = tc;
Sleep( 5000 );
}
SetProcPermissions( permissions );
.
- Follow-Ups:
- References:
- About some questions on PocketPC (CPU Usage,..)
- From: hidex
- Re: About some questions on PocketPC (CPU Usage,..)
- From: Paul G. Tobey [eMVP]
- Re: About some questions on PocketPC (CPU Usage,..)
- From: hidex
- Re: About some questions on PocketPC (CPU Usage,..)
- From: Paul G. Tobey [eMVP]
- Re: About some questions on PocketPC (CPU Usage,..)
- From: hidex
- Re: About some questions on PocketPC (CPU Usage,..)
- From: Paul G. Tobey [eMVP]
- Re: About some questions on PocketPC (CPU Usage,..)
- From: hidex
- About some questions on PocketPC (CPU Usage,..)
- Prev by Date: Re: About some questions on PocketPC (CPU Usage,..)
- Next by Date: Re: Using CString on a platform without MFC or OLE - Howto?
- Previous by thread: Re: About some questions on PocketPC (CPU Usage,..)
- Next by thread: Re: About some questions on PocketPC (CPU Usage,..)
- Index(es):
Relevant Pages
|