Task Stack Info & Profiling Features

cstrahm wrote on Thursday, April 30, 2009:

Most RTOS give stack space info and profiling.  While FreeRTOS has the stackfree function, the tskTCB record doesn’t seem to retain the orignal stack space.  I find it difficut to remember how much stack space I gave a task unless I go back and look it up in the code.  I added that to my task.c tskTCB record and changed the prvListTaskWithinSingleList() function to report the USED space and the TOTAL space, which I think is far more useful.  My console task list output now looks like this:

TaskName   State  Priority Stack:Used/Total   TCB

CONRTOS         R       1          35/  128     0
IDLE            R       0          59/  128     4
HTTP            B       2         102/ 1024     1
Check           B       2          32/  128     3
IntVolt         B       1          39/  128     2

I also wrote a module of profiling code using the trace functions, and created a TaskProfileList() function that spits out a similar table as shown below:

TaskName          Exec/Sec        CPU

Check                    1       0.00%
HTTP                   100       0.78%
CONRTOS                  0       0.00%
IntVolt                 10       0.00%
IDLE                   100      98.67%

These kinds of tools are invaluable for any serious RTOS work.  It seems like FreeRTOS should include somekind of profiling support built in.  It doesn’t take much code/ROM space to do this.  Just a suggestion.

aturowski wrote on Thursday, April 30, 2009:

I agree. I have written some similar tools when I was involved in properiary RTOS and it proved to be exteremely useful.

But you have to remember that FreeRTOS runs on micros with very small memory footprint, so these tools should be optionally compiled by enabling them in FreeRTOSConfig.h file

alainm3 wrote on Thursday, April 30, 2009:

I have one suggestion: why not put both informations on the same table?

it would make it much easyer to use and probably smaller…

I would like very much to have that… where can I find it?

Richard, can you consider including that in FreeRTOS? please?


destremps wrote on Thursday, April 30, 2009:

Yes I too would very much like to see the source code made available for the CPU % usage. Is there a way for you to post it this forum? Or somewhere else?


rtel wrote on Thursday, April 30, 2009:

Sounds like a good idea to (optionally) include some profiling features.  I would like to see how you have done it, either posted here (formatting can be a problem) or you could send it to me at r (dot) barry (at) freertos.org.

I have done similar things using the trace macros whereby the traceTASK_SWITCHED_IN() macro is used in combination with a high frequency timer to "count" the time each task is running for.  A function can then be written to either simply display the total time, or if processing overhead is not an issue display the time each task is running as a percentage.


cstrahm wrote on Saturday, May 02, 2009:

Doing the Profiling only takes a few hundred bytes of ROM on an ARM7 and minimal RAM.  But I also have some other features in here such as a Performance Timer that has uSec resolution.  I find it indispensible for timing routines and performance tuning.  FreeRTOS doesn’t do anything with the main Timer Prescale, it is set to div by 1. Much better default would be to always set this up to provide a 1MHz master clock into the main Timer Counter, and then you have a standard 1uSec resolution time base.

I also have a single file module for a stand alone Console/printf/sprintf that dumps out the task info and profiling on demand to a RS232 terminal, and also takes commands from the terminal.  Does not use any lib functions so that usually saves ROM space.  You can easily add custom command support also to display anything you want.

I don’t think there’s anyway to attach files here, and it’s probably too much to stick in the msg portion.  I will send Richard an email with the stuff.

alainm3 wrote on Friday, May 15, 2009:

Hi Chris, I would like to test that code too…

Could you please put that code in some public repositoty, like www.pastebin.ca so that you can add just the link here in nthe forum.

Thanks, Alain

yyang2000 wrote on Friday, May 15, 2009:


I wonder how many stack space should be defined during creating a task. Does a task stack size relate to a queue size which is used in the task? for example, if I create a queue in size of 400 bytes, does it mean the task stack size must be bigger than 400 bytes? I could not find any information regarding set task stack info in FreeRTOS.org.

Also I am interested in Chris Strahm mentioned above the Task Stack Profiling and other features. I wonder if Richard got the source code. Could you please email the source code to me? Or let me know where it posted. My email address: xxxx@xxxx.com

Bill Yang

thmjpr wrote on Saturday, May 16, 2009:

xQueueCreate calls pvPortMalloc which uses xHeap which is a global (whose size is defined by configTOTAL_HEAP_SIZE), so no.

You can use this function: http://www.freertos.org/uxTaskGetStackHighWaterMark.html

tpham3783 wrote on Wednesday, June 03, 2009:

I am a novice to FreeRtos.  I find it very hard to keep track of size size, cpu time utilitization of each task.  Many things that I do, for example, changing a priority of a task that does nothing causes
the lwip stack to fail completely.  or overflowing the stack or RAM with two much dynamic memory allocation.  Many times, the processor would stop working completely. 

Does anyone know where I can find the source code to find out stack watermark level, and cpu profiling?  I do not think i can continue developing in freertos without this tool.  Please help.

Thank you.

rtel wrote on Thursday, June 04, 2009: