How to find number of times task has been scheduled


Is there a way or FreeRTOS API to find out number of times the task has been scheduled ?

In our existing operating system, there is API which returns thread count i.e. number of times thread has been scheduled. I am replacing current OS(threadX) with FreeRTOS and wondering if there is a API which i can directly use to get this information.I see vTaskGetInfo API but it doesn’t provide this information.

Is there any other way to get this information ?

Any suggestion is appreciated.


There is a set of low level diagnostics / tracing features documented here.
See the example with traceTASK_SWITCHED_IN there. That could match your needs.

This is something we have played around with before, namely extending the run-time stats feature to keep a count of the number of times a task has run as well as the length of time the task has run.

In addition to hs2’s suggestion of implementing a trace macro to keep a count yourself, there are a couple of other methods you could use, although equally as manual.

First you could define a tick hook function and keep a count in there - you can use the xTaskGetCurrentTaskHandle() function to obtain the handle of the running task (that function is ok to call from an interrupt, even though it breaks the rule of thumb of not calling functions that don’t end in “FromISR” from interrupts - the tick hook function being called from an interrupt). You could even use a thread local storage pointer in the task to store the count value.

Another method would be to use a task hook that keeps a count, and invoke the hook from the trace macro as per hs2’s suggestion.

I got diverted into something else for few weeks. Coming back to this again, thanks for the suggestion. I was able to implement task count by invoking callback function on taskTASK_Switched_IN and then maintaining count using thread local storage pointer. This helps to keep task run count value in task TCB itself.

Thanks again.