locked
Total CPU Usage of mobile device! RRS feed

  • Question

  • Hi everybody

    I need to estimate total CPU usage of my mobile device in my application.

    I've developed my application in VS2008, C# language and run it on windows mobile 6.5.

    I'll be appreciated if anyone can give me a sample code to do this.

     

    Wednesday, December 1, 2010 9:52 PM

Answers

  • I forgot about something else. If you don't care about individual process usage and just want the overall usage, you can use GetIdleTime() to do it very easily:

        DWORD start = ::GetTickCount();
        DWORD idle_start = ::GetIdleTime();
        Sleep( 1000 );
        DWORD stop = ::GetTickCount();
        DWORD idle_end = ::GetIdleTime();
        float percent_idle = ( 100.0f * ( idle_end - idle_start ) ) / static_cast< float >( stop - start );
    
    

    -PaulH

    • Marked as answer by somayeh_aut Saturday, December 4, 2010 1:07 PM
    • Unmarked as answer by somayeh_aut Saturday, December 4, 2010 1:07 PM
    • Marked as answer by somayeh_aut Saturday, December 4, 2010 1:07 PM
    Thursday, December 2, 2010 9:40 PM

All replies

  • It is not trivial.

    Use the ToolHelperAPI's Thread32First() and Thread32Next() functions to get a list of all running threads for every process.
    http://msdn.microsoft.com/en-us/library/ms913267.aspx

    Then, use GetThreadTimes() to find the amount of time spent in kernel and user space for each thread.
    http://msdn.microsoft.com/en-us/library/ms683237%28VS.85%29.aspx

    If you want a nice % utilization like task manager gives you, then you will need to do that twice and delay for some known amount of time between calls. For example:

    int total_time1 = current_kernel_time + current_user_time;  
    Sleep(100);
    int total_time2 = current_kernel_time + current_user_time;
    int percent_utilization = total_time2 - total_time1 / 100;

    You will need to P/Invoke these functions from coredll.dll for C#

    -PaulH

    Wednesday, December 1, 2010 10:21 PM
  • This may be obvious, but PaulH is presuming that you know:

    a) that the Sleep parameters is in milliseconds

    b) that Sleep creates is a minimum excution delay (in a heavily loaded system it may come back any time after 100 milliseconds have elapsed). A more accurate approach would query the current time before and after the Sleep call.

    c) GetThreadTimes is using FILETIME structures so the appropriate unit conversions will be necessary.

    Wednesday, December 1, 2010 11:32 PM
  • @2forJL - All true.

    Actually, I think the Sleep() is okay. But, I'd recommend setting your thread to the highest priority and smallest quantum while you're taking measurements (to ensure the other threads don't run while you're measuring them) and to use QueryPerformanceCounter() to determine how long you actually slept. Ex:

    SetThreadPriorityAndQuantum(MAX)
    int total_time1 = current_kernel_time + current_user_time;  
    SetThreadPriorityAndQuantum(NORMAL)
    int start_sleep = QueryPerformanceCounter()
    Sleep(100);
    int stop_sleep = QueryPerformanceCounter()
    SetThreadPriorityAndQuantum(MAX)
    int total_time2 = current_kernel_time + current_user_time;
    SetThreadPriorityAndQuantum(NORMAL)
    int percent_utilization = (total_time2 - total_time1) / (stop_sleep-start_sleep) * 100;

    SetThreadQuantum: http://msdn.microsoft.com/en-us/library/aa909212.aspx
    SetThreadPriority: http://msdn.microsoft.com/en-us/library/bb202728.aspx

    To convert FILETIME user & kernel times to a combined millisecond value, you can use a function like this:

    static DWORD GetThreadTick( const FILETIME& user, const FILETIME& kernel )
    {
      __int64 tick = MAKEDWORDLONG( user.dwLowDateTime, user.dwHighDateTime );
      tick += MAKEDWORDLONG( kernel.dwLowDateTime, kernel.dwHighDateTime );
      tick /= 10000;
      return static_cast< DWORD >( tick );
    }

    -PaulH

    • Edited by PaulH79 Thursday, December 2, 2010 10:24 PM
    Thursday, December 2, 2010 2:34 PM
  • I forgot about something else. If you don't care about individual process usage and just want the overall usage, you can use GetIdleTime() to do it very easily:

        DWORD start = ::GetTickCount();
        DWORD idle_start = ::GetIdleTime();
        Sleep( 1000 );
        DWORD stop = ::GetTickCount();
        DWORD idle_end = ::GetIdleTime();
        float percent_idle = ( 100.0f * ( idle_end - idle_start ) ) / static_cast< float >( stop - start );
    
    

    -PaulH

    • Marked as answer by somayeh_aut Saturday, December 4, 2010 1:07 PM
    • Unmarked as answer by somayeh_aut Saturday, December 4, 2010 1:07 PM
    • Marked as answer by somayeh_aut Saturday, December 4, 2010 1:07 PM
    Thursday, December 2, 2010 9:40 PM
  • Oh!Thanks! It was really helpful!

    Thank you so much!

    Saturday, December 4, 2010 1:09 PM