Measuring CPU usage?
Posted by keith soldavin
on December 6, 2011
I have an application I am currently developing that requires me to report the CPU usage of the system periodically. I have my code compiled (FreeRTOS 7.0.1, ARM7, IAR 6.2) with the configGENERATE_RUN_TIME_STATS flag set and I can display the stats off all tasks on my debug screen. What I am looking for is not the stats of each individual task but of the overall system. My thought was that if I can get the stats for the Idle task I can subtract the number from 100 and get the total CPU usage. My problem is that I do not know how, or if it is even possible, to get the usage stats of an individual task. One option would be to get the stats for all the tasks and parse out the idle task information but that would be a waste of resources which I don't have.
The one thing I tried, that didn't seem to work well, is to set up idle and tick hook functions that incremented a uint32 variable every time they are entered. I used these values to try to calculate the percentage of time in idle. Unfortunately the values I calculated did not correspond to the RTOS task stats.
Does anyone have any suggestions?
RE: Measuring CPU usage?
Posted by Dave
on December 6, 2011
You can use the traceTASK_SWICHED_IN macro.
In the macro, if pxCurrentTCB equals the handle of the idle task, then you know the idle task has just been switched in and you can start a high frequency timer (or note the time of a free running high frequency timer). If the idle task was running, and pxCurrentTCB is no longer the idle task, you know the idle task has just been switched out and you can stop the high frequency timer and note the elapsed time (or again note the time of a free running high frequency timer).
If you use FreeRTOS 7.0.2 and define INCLUDE_xTaskGetIdleTaskHandle to 1 in FreeRTOSConfig.h, then you can use xTaskGetIdleTaskHandle() after the scheduler is started to get the idle task handle.
Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd..
See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.