thread profiling

eavg wrote on Monday, February 09, 2009:

HI :slight_smile:

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

davedoors wrote on Monday, February 09, 2009:

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

incrediball wrote on Thursday, February 12, 2009:

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 {
    #if (TRACE_TASK_EXECUTION)
        volatile unsigned int task_time;
    #endif
} tTaskData;

typedef tTaskData* pTaskData;

#if (TRACE_TASK_EXECUTION)
    extern unsigned short task_switch_time;    // timers are only 16 bit

    #define HIGH_RES_TIMER                    (unsigned short)AT91C_BASE_TC0->TC_CV    // AT91SAM7S hardware timer at 374kHz

    #define traceTASK_SWITCHED_IN()        task_switch_time = HIGH_RES_TIMER
    #define traceTASK_SWITCHED_OUT()       
        if (pxCurrentTCB->pxTaskTag)       
            ((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.