CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    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.

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    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...

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Code to measure CPU usage returns inconsistent results for overclocked CPU

    Quote Originally Posted by OReubens View Post
    your assumption that the CPU is at 100% is odd.
    I understood that this was not an assumption, but observation.
    Quote Originally Posted by OReubens View Post
    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...

  5. #5
    Join Date
    May 2007
    Posts
    811

    Re: Code to measure CPU usage returns inconsistent results for overclocked CPU

    Or play some games.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured