# thread profiling

**URL:** <https://forums.freertos.org/t/thread-profiling/5139>\
**Category:** Kernel\
**Created:** [February 9, 2009, 7:40am UTC](https://forums.freertos.org/t/thread-profiling/5139 "2009-02-09T07:40:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 9, 2009, 7:40am UTC](https://forums.freertos.org/t/thread-profiling/5139/1 "2009-02-09T07:40:39Z")

</div>

**[eavg](https://sourceforge.net/u/eavg/)** wrote on [Monday, February 09, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/e015aabc/#c08e):

HI 🙂

Does one know if it is possible to find out how much a task (or all tasks) have been running for a period of time. That is I would like to have something like:

In the last x second  
Task A has run 23 ms  
Task B has run 281 ms  
Task C has run 0 ms  
Task idle has run 900 ms  
INT 1 has run 100 us  
…

Any ideas?  
thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 9, 2009, 9:21am UTC](https://forums.freertos.org/t/thread-profiling/5139/2 "2009-02-09T09:21:24Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Monday, February 09, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/e015aabc/#c08e/449a):

Take a look at this page [http://www.freertos.org/rtos-trace-macros.html](http://www.freertos.org/rtos-trace-macros.html) you can trace nearly everything.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 12, 2009, 7:43pm UTC](https://forums.freertos.org/t/thread-profiling/5139/3 "2009-02-12T19:43:51Z")

</div>

**[incrediball](https://sourceforge.net/u/incrediball/)** wrote on [Thursday, February 12, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/e015aabc/#c08e/2585):

As davedoors mentioned, use the trace macros. Using a hardware based timer you can accurately measure the time spent in each task.

Define the following in a header that freertos uses:

typedef struct {  
&nbsp;&nbsp;&nbsp; #if (TRACE\_TASK\_EXECUTION)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; volatile unsigned int task\_time;  
&nbsp;&nbsp;&nbsp; #endif  
} tTaskData;

typedef tTaskData\* pTaskData;

#if (TRACE\_TASK\_EXECUTION)  
&nbsp;&nbsp;&nbsp; extern unsigned short task\_switch\_time;&nbsp;&nbsp;&nbsp; // timers are only 16 bit

&nbsp;&nbsp;&nbsp; #define HIGH\_RES\_TIMER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (unsigned short)AT91C\_BASE\_TC0-\>TC\_CV&nbsp;&nbsp;&nbsp; // AT91SAM7S hardware timer at 374kHz

&nbsp;&nbsp;&nbsp; #define traceTASK\_SWITCHED\_IN()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; task\_switch\_time = HIGH\_RES\_TIMER  
&nbsp;&nbsp;&nbsp; #define traceTASK\_SWITCHED\_OUT()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (pxCurrentTCB-\>pxTaskTag)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((pTaskData)pxCurrentTCB-\>pxTaskTag)-\>task\_time += (unsigned short)(HIGH\_RES\_TIMER - task\_switch\_time);  
#endif

…

When the tasks start up, they do:

tTaskData taskdata;

vTaskSetApplicationTaskTag (NULL, (pdTASK\_HOOK\_CODE) &taskdata);

Then somewhere (e.g. in the idle task), regularly read out the task\_time values from all your tTaskData structs. You can obviously easily get percentages by dividing each time by the total and you can get an overall system busy percentage (e.g. like in the Windows task manager) based on the idle task, i.e. if the idle task gets 100% of the time then the system is 0% busy, if the idle task only gets 5% then the system is 95% busy, etc.
