From: nki00 on
Hi everyone:


I've been struggling to come up with a correct code to determine CPU usage
on the Windows-based system. So far I came up with the following. The code
is called internally from WM_TIMER message that is called once every 100
milliseconds, but if compared with the Task Manager's CPU usage, it does not
always match. What could be the reason for that?


int g_nCPU_usage_percent; //Global variable
.....

SetTimer(m_hMainWndHandle, TIMER_ID, 100, NULL);

.....

void OnTimer(UINT nIDEvent)
{
g_nCPU_usage_percent = GetCPU_Usage();
}


.....


int GetCPU_Usage()
{
//RETURN:
// = Percentage value [0-100], or
// = -1 if not known, or error

int nRes = -1;
FILETIME ftIdle, ftKrnl, ftUsr;
if(GetSystemTimes(&ftIdle, &ftKrnl, &ftUsr))
{
static BOOL bUsedOnce = FALSE;
static ULONGLONG uOldIdle = 0;
static ULONGLONG uOldKrnl = 0;
static ULONGLONG uOldUsr = 0;

ULONGLONG uIdle = ((ULONGLONG)ftIdle.dwHighDateTime << 32) |
ftIdle.dwLowDateTime;
ULONGLONG uKrnl = ((ULONGLONG)ftKrnl.dwHighDateTime << 32) |
ftKrnl.dwLowDateTime;
ULONGLONG uUsr = ((ULONGLONG)ftUsr.dwHighDateTime << 32) |
ftUsr.dwLowDateTime;

//Only if we have previous values
if(bUsedOnce)
{
ULONGLONG uDiffIdle = uIdle - uOldIdle;
ULONGLONG uDiffKrnl = uKrnl - uOldKrnl;
ULONGLONG uDiffUsr = uUsr - uOldUsr;

if(uDiffKrnl + uDiffUsr)
{
//Calculate percentage
nRes = (int)((uDiffKrnl + uDiffUsr - uDiffIdle) * 100 / (uDiffKrnl +
uDiffUsr));
}
}

//Remember data
bUsedOnce = TRUE;
uOldIdle = uIdle;
uOldKrnl = uKrnl;
uOldUsr = uUsr;
}

return nRes;
}


From: Jackie on
nki00 wrote:
> Hi everyone:
>
>
> I've been struggling to come up with a correct code to determine CPU usage
> on the Windows-based system. So far I came up with the following. The code
> is called internally from WM_TIMER message that is called once every 100
> milliseconds, but if compared with the Task Manager's CPU usage, it does not
> always match. What could be the reason for that?
>
>
> int g_nCPU_usage_percent; //Global variable
> ....
>
> SetTimer(m_hMainWndHandle, TIMER_ID, 100, NULL);
>
> ....
>
> void OnTimer(UINT nIDEvent)
> {
> g_nCPU_usage_percent = GetCPU_Usage();
> }
>
>
> ....
>
>
> int GetCPU_Usage()
> {
> //RETURN:
> // = Percentage value [0-100], or
> // = -1 if not known, or error
>
> int nRes = -1;
> FILETIME ftIdle, ftKrnl, ftUsr;
> if(GetSystemTimes(&ftIdle,&ftKrnl,&ftUsr))
> {
> static BOOL bUsedOnce = FALSE;
> static ULONGLONG uOldIdle = 0;
> static ULONGLONG uOldKrnl = 0;
> static ULONGLONG uOldUsr = 0;
>
> ULONGLONG uIdle = ((ULONGLONG)ftIdle.dwHighDateTime<< 32) |
> ftIdle.dwLowDateTime;
> ULONGLONG uKrnl = ((ULONGLONG)ftKrnl.dwHighDateTime<< 32) |
> ftKrnl.dwLowDateTime;
> ULONGLONG uUsr = ((ULONGLONG)ftUsr.dwHighDateTime<< 32) |
> ftUsr.dwLowDateTime;
>
> //Only if we have previous values
> if(bUsedOnce)
> {
> ULONGLONG uDiffIdle = uIdle - uOldIdle;
> ULONGLONG uDiffKrnl = uKrnl - uOldKrnl;
> ULONGLONG uDiffUsr = uUsr - uOldUsr;
>
> if(uDiffKrnl + uDiffUsr)
> {
> //Calculate percentage
> nRes = (int)((uDiffKrnl + uDiffUsr - uDiffIdle) * 100 / (uDiffKrnl +
> uDiffUsr));
> }
> }
>
> //Remember data
> bUsedOnce = TRUE;
> uOldIdle = uIdle;
> uOldKrnl = uKrnl;
> uOldUsr = uUsr;
> }
>
> return nRes;
> }
>
>

I remember working on a process manager sime time ago in C#. It seems
like I used WMI and PercentProcessorTime from
Win32_PerfFormattedData_PerfOS_Processor to get processes' usage or
something like that. Maybe you can try to look at that?

--
Regards,
Jackie
From: Jackie on
nki00 wrote:
> Hi everyone:
>
>
> I've been struggling to come up with a correct code to determine CPU usage
> on the Windows-based system. So far I came up with the following. The code
> is called internally from WM_TIMER message that is called once every 100
> milliseconds, but if compared with the Task Manager's CPU usage, it does not
> always match. What could be the reason for that?
>

Tried to cancel my first replies. No idea if it worked.

Either way, I meant processors' usage and not processes' usage.
PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor or
Win32_PerfRawData_PerfOS_Processor (via WMI).
I hope it help.


--
Regards,
Jackie
From: Bob Masta on
On Fri, 4 Jun 2010 16:06:43 -0700, "nki00"
<lukkycharm1(a)gmail.com> wrote:

>Hi everyone:
>
>
>I've been struggling to come up with a correct code to determine CPU usage
>on the Windows-based system. So far I came up with the following. The code
>is called internally from WM_TIMER message that is called once every 100
>milliseconds, but if compared with the Task Manager's CPU usage, it does not
>always match. What could be the reason for that?
>
>
>int g_nCPU_usage_percent; //Global variable
>....
>
>SetTimer(m_hMainWndHandle, TIMER_ID, 100, NULL);
>
>....
>
>void OnTimer(UINT nIDEvent)
>{
> g_nCPU_usage_percent = GetCPU_Usage();
>}
>
>
>....
>
>
>int GetCPU_Usage()
>{
> //RETURN:
> // = Percentage value [0-100], or
> // = -1 if not known, or error
>
> int nRes = -1;
> FILETIME ftIdle, ftKrnl, ftUsr;
> if(GetSystemTimes(&ftIdle, &ftKrnl, &ftUsr))
> {
> static BOOL bUsedOnce = FALSE;
> static ULONGLONG uOldIdle = 0;
> static ULONGLONG uOldKrnl = 0;
> static ULONGLONG uOldUsr = 0;
>
> ULONGLONG uIdle = ((ULONGLONG)ftIdle.dwHighDateTime << 32) |
>ftIdle.dwLowDateTime;
> ULONGLONG uKrnl = ((ULONGLONG)ftKrnl.dwHighDateTime << 32) |
>ftKrnl.dwLowDateTime;
> ULONGLONG uUsr = ((ULONGLONG)ftUsr.dwHighDateTime << 32) |
>ftUsr.dwLowDateTime;
>
> //Only if we have previous values
> if(bUsedOnce)
> {
> ULONGLONG uDiffIdle = uIdle - uOldIdle;
> ULONGLONG uDiffKrnl = uKrnl - uOldKrnl;
> ULONGLONG uDiffUsr = uUsr - uOldUsr;
>
> if(uDiffKrnl + uDiffUsr)
> {
> //Calculate percentage
> nRes = (int)((uDiffKrnl + uDiffUsr - uDiffIdle) * 100 / (uDiffKrnl +
>uDiffUsr));
> }
> }
>
> //Remember data
> bUsedOnce = TRUE;
> uOldIdle = uIdle;
> uOldKrnl = uKrnl;
> uOldUsr = uUsr;
> }
>
> return nRes;
>}

This approach is used by "ejor" at
<http://www.codeproject.com/KB/threads/Get_CPU_Usage.aspx>

But he warns that it doesn't give the correct
results with a multiprocessor system. I didn't
download his complete code, but he says it has a
solution for that.

Best regards,


Bob Masta

DAQARTA v5.10
Data AcQuisition And Real-Time Analysis
www.daqarta.com
Scope, Spectrum, Spectrogram, Sound Level Meter
Frequency Counter, FREE Signal Generator
Pitch Track, Pitch-to-MIDI
DaqMusic - FREE MUSIC, Forever!
(Some assembly required)
Science (and fun!) with your sound card!
From: alain on
nki00 wrote:
> Hi everyone:
>
>
> I've been struggling to come up with a correct code to determine CPU usage
> on the Windows-based system. So far I came up with the following. The code
> is called internally from WM_TIMER message that is called once every 100
> milliseconds, but if compared with the Task Manager's CPU usage, it does not
> always match. What could be the reason for that?

See this thread :
http://groups.google.fr/group/comp.os.ms-windows.programmer.win32/browse_thread/thread/a36b6af342c88b1/cd30a0aed8483a15?hl=fr&ie=UTF-8&q=_SYSTEM_INFORMATION_CLASS++cpu+usage#cd30a0aed8483a15