-
August 14th, 2015, 10:24 AM
#1
Code to measure CPU usage returns inconsistent results for overclocked CPU
I have a somewhat strange behavior of the code I'm showing below. This code is called repeatedly to measure the current CPU usage in my application (the value measured for all CPU cores):
Code:
int getCPUUsage(void)
{
//RETURN: = CPU Usage in percent [0 - 100], or
// = -1 if 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;
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));
}
}
bUsedOnce = TRUE;
uOldIdle = uIdle;
uOldKrnl = uKrnl;
uOldUsr = uUsr;
}
return nRes;
}
I tested it on several of my PCs and the code seems to return reliable results. Unfortunately, on one machine with overclocked CPU the code above seems to produce results twenty percent lower than the reading from the Task Manager (namely, the task manager would be showing 100% CPU utilization and my code would consistently return only 80%.)
CPU specifics on that "overclocked" machine:
Intel(R) Core(TM) i7
Cores: 4
Logical CPUs: 8
NUMA nodes: 1
Does anyone have any idea why this code could get such faulty reading?
PS. Note that that CPU doesn't seem to produce any strange behavior, besides giving this inconsistent reading.
-
August 17th, 2015, 02:17 PM
#2
Re: Code to measure CPU usage returns inconsistent results for overclocked CPU
Not sure what you read first - here or there, but could you try QueryPerformanceFrequency() on that CPU? Does it reflect regular or overclocked speed?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 18th, 2015, 07:46 AM
#3
Re: Code to measure CPU usage returns inconsistent results for overclocked CPU
overclocking has no effect on GetSystemTimes()
your assumption that the CPU is at 100% is odd. If your system is running correctly, it should rarely, if at all, be at 100% for anything other than momentary peaks.
note that GetSystemTimes() is for all logical processors, not just the ones your app is running on.
you may have task manager set to a view that does not show total CPU over all logical processors.
-
August 18th, 2015, 10:13 AM
#4
Re: Code to measure CPU usage returns inconsistent results for overclocked CPU
Originally Posted by OReubens
your assumption that the CPU is at 100% is odd.
I understood that this was not an assumption, but observation.
Originally Posted by OReubens
If your system is running correctly, it should rarely, if at all, be at 100% for anything other than momentary peaks.
That is only true for "lazy" system. Try video encoding - it will keep your CPU at 100% for hours...
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 18th, 2015, 11:40 AM
#5
Re: Code to measure CPU usage returns inconsistent results for overclocked CPU
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|