Re: About some questions on PocketPC (CPU Usage,..)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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



.



Relevant Pages

  • Re: TASM. This works...but it shouldnt
    ... int64 STRUC ... ldw DWORD? ... mov eax,.int64.hdw ...
    (comp.lang.asm.x86)
  • Re: 64 bit data types
    ... Rod wrote: ... Int64 ... DWORD ... I wonder what'll happen to "longint" which is gonna be a half-integer anyway... ...
    (borland.public.delphi.non-technical)
  • Re: Is __int64 dead?
    ... bool, char, int, __int64, are elegant names. ... BYTE, WORD, UINT, and even DWORD are not too bad. ...
    (microsoft.public.vc.mfc)
  • Re: 64 bit data types
    ... >>> DWORD ... > But I do hope that "integer" will be implemented as Int64 or else much ... > legacy code that typecasts pointers into integers will fall flat. ...
    (borland.public.delphi.non-technical)