I've got a class that gets the CPU usage, so I can display it on my Matrix Orbital LCD. I originally wrote it on a single core machine, so it just gets the overall CPU usage. It still works on my new rig, but I'd like to modify it so it could get the usage for each of the cores. Here's the code, as it exists now:
Is there a way to easily modify this to report on multiple cores? Or can somebody point me towards a class that could?
Code:
BOOL CCpuUsage::InitMonitor(void)
{
BOOL res;
DWORD idlLow, idlHigh, kerLow, kerHigh, usrLow, usrHigh;
FILETIME idleTime, kernelTime, userTime;
res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
if (res)
{
idlLow = idleTime.dwLowDateTime;
idlHigh = idleTime.dwHighDateTime;
idlLast = idlHigh;
idlLast = idlLast << 32;
idlLast += idlLow;
kerLow = kernelTime.dwLowDateTime;
kerHigh = kernelTime.dwHighDateTime;
kerLast = kerHigh;
kerLast = kerLast << 32;
kerLast += kerLow;
usrLow = userTime.dwLowDateTime;
usrHigh = userTime.dwHighDateTime;
usrLast = usrHigh;
usrLast = usrLast << 32;
usrLast += usrLow;
}
return(res);
}
int CCpuUsage::GetUsage(void)
{
CString msg;
DWORD idlLow, idlHigh, kerLow, kerHigh, usrLow, usrHigh;
__int64 idlTotal, usrTotal, kerTotal, sys, cpu = 0,
idlDif, usrDif, kerDif;
FILETIME idleTime, kernelTime, userTime;
BOOL res;
res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
if (res)
{
idlLow = idleTime.dwLowDateTime;
idlHigh = idleTime.dwHighDateTime;
idlTotal = idlHigh;
idlTotal = idlTotal << 32;
idlTotal += idlLow;
idlDif = idlTotal - idlLast;
kerLow = kernelTime.dwLowDateTime;
kerHigh = kernelTime.dwHighDateTime;
kerTotal = kerHigh;
kerTotal = kerTotal << 32;
kerTotal += kerLow;
kerDif = kerTotal - kerLast;
usrLow = userTime.dwLowDateTime;
usrHigh = userTime.dwHighDateTime;
usrTotal = usrHigh;
usrTotal = usrTotal << 32;
usrTotal += usrLow;
usrDif = usrTotal - usrLast;
sys = kerDif + usrDif;
if (sys != 0)
{
cpu = ((sys - idlDif) * 100 / sys);
}
idlLast = idlTotal;
usrLast = usrTotal;
kerLast = kerTotal;
}
else
{
cpu = -1;
}
return((int)cpu);
}
Is there a way to easily modify this to report on multiple cores? Or can somebody point me towards a class that could?