Calculating CPU Usage

This is a discussion on Calculating CPU Usage within the C Programming forums, part of the General Programming Boards category; Hello, I'm writing a function to find the CPU Usage, simiar to the one shown in Task Manager. The following ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    Calculating CPU Usage

    Hello,

    I'm writing a function to find the CPU Usage, simiar to the one shown in Task Manager. The following is what I have so far. The values seem reasonable for low numbers, but when I see it at about 30-40% on Task Manager, this function shows it up to 70-80%. I would like to know if anyone has any suggestions regarding my code.

    Thanks!!

    Code:
    int iFunGetTime( FILETIME ftTime ) 
    { 
    SYSTEMTIME stTime; 
    int iTime; 
    
    
    FileTimeToSystemTime( &ftTime, &stTime ); 
    
    iTime = stTime.wSecond * 1000; 
    iTime += stTime.wMilliseconds; 
    
    return iTime; 
    } 
    
    
    int iFunGetCpuProcUsage( HANDLE hHandle ) 
    { 
    LONG lOldIdle, lOldUser, lNewUser, lOldKernel, lNewKernel, lNewIdle, lProcUsage, lUser, lKernel, lIdle, lSys; 
    DWORD dwOldTime, dwNewTime, dwTime; 
    FILETIME idleTime, kernelTime, userTime, ftCreate, ftExit, ftUser, ftKernel; 
    int iProcUsage; 
    
    dwOldTime = timeGetTime(); 
    
    if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) ) 
         { 
         printf("error old getprocesstime %d", GetLastError() ); 
         getch(); 
         } 
    
    lOldUser = iFunGetTime( ftUser ); 
    lOldKernel = iFunGetTime( ftKernel ); 
    
    Sleep( 1000 ); 
    
    dwNewTime = timeGetTime(); 
    
    if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) ) 
         { 
         printf("error new getprocesstime %d", GetLastError() ); 
         getch(); 
         }
    
    lNewUser = iFunGetTime( ftUser ); 
    lNewKernel = iFunGetTime( ftKernel ); 
    
    lKernel = lNewKernel - lOldKernel; 
    lUser = lNewUser - lOldUser; 
    
    dwTime = dwNewTime-dwOldTime; 
    
    if( dwTime == 0 ) 
         { 
         Sleep( 100 ); 
         dwNewTime = timeGetTime(); 
         dwTime = dwNewTime-dwOldTime; 
         } 
    
    iProcUsage =  (((lKernel+lUser)*100 )  / dwTime ); 
    
    return iProcUsage; 
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you on a multiprocessor (e.g. dual core) system? Task manager counts 2 cores working ll the time as 100%, your calculation would make 100% on a single CPU. Since your numbers are roughly double that of task manager, I'm guessing this is the case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    I believe that it is a multiprocessor system. How do I remedy the problem?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends on what you want to "remedy".

    If you want to know how much you use out of ALL the processors, then you count the "wall-time" as X * time_elapsed, where X is the number of processors [there is a Windows function to find the number of processors].

    If the application being measured is just using a single thread, the more appropriate method is probably just say it's using N% of one CPU - it really can't go over 100% anyways.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    By "wall-time", do you mean the dwTime portion of this line

    Code:
    iProcUsage =  (((lKernel+lUser)*100 )  / dwTime );
    So since I have 2 processes, would I change it to this?
    Code:
    iProcUsage =  (((lKernel+lUser)*100 )  / 2* dwTime );

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Something like that yes,

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Well that wouldn't necessarily indicate the correct loads, because the usage isn't always shared half/half.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JFonseka View Post
    Well that wouldn't necessarily indicate the correct loads, because the usage isn't always shared half/half.
    Of course, if you want to know how much of EACH process used how much of EACH core/processor, then you would need to use a different API function (or different variation of the same API) to get the timing per processor from the OS, first of all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 03:13 PM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 08:44 AM
  3. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 08:59 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 10:38 AM
  5. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 03:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21