Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


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.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner