measure execution by vApplicationTickHook, change priority?

electronicdev wrote on Tuesday, May 23, 2017:

I have questions about the vApplicationTickHook(). How could this be used to
measure the execution time of e.g. taska and taskb? Also how could I change the priority of
e.g. taskb after having meausured the execution time?

The vApplicationTickHook is always run but how to measure the execution time for e.g taska and taskb so
I can have the specific execution time for each of them?
Also how to use e.g. vTaskPrioritySet(handle, 2) to change the priority of a task?

Grateful for examples

heinbali01 wrote on Tuesday, May 23, 2017:

Hi, I would introduce a third lightweight task to do that job. Let it sleep (block) for 5 seconds, check the system load, and sleep again.

vApplicationTickHook() is being called from an ISR and that gives many limitations. You can not do much from within an ISR. vApplicationTickHook() is handy if you want to have a blinking LED or so.

There are several ways to get an estimate of CPU-time per task, for instance configGENERATE_RUN_TIME_STATS

I never had the need to do what you want to do, and I wonder if you can take a different approach? Have a look at the following :

    #define configUSE_PREEMPTION       1
    #define configUSE_TIME_SLICING     1

It is commented in this post

With these settings, the OS makes sure that runnable tasks of equal priority will get an equal amount of CPU-time!
It will not depend on the task’s willingness to yield. I would consider using that option and not play with the tasks’ priorities.

richard_damon wrote on Tuesday, May 23, 2017:

As Hein says, seems wrong to need to change priorities based on CPU usage. Priorities should reflect the needs of the system, and generally don’t change. The one exception that I can think of is if something changes ‘mode’ and some operation changes significantly in need (something changes so what used to be not important exactly when it occured, to being very important).

electronicdev wrote on Wednesday, May 24, 2017:

Is it possible by vApplicationTickHook to check which task that is running and then increment a counter as long as that task is executed? What function could I call inside vApplicationTickHook to check if e.g. taskb is executing and then increase my counter for every tick my taskb is executing, if not executed no count is performed. How to do this and what arguments are returned etc if checking taskb is executed?

Thank you

heinbali01 wrote on Wednesday, May 24, 2017:

You could ask for the current task handle ( xTaskGetCurrentTaskHandle() ), and use that pointer to increment some counter belonging to a task.
The API xTaskGetCurrentTaskHandle() does not have a critical section, so it should be safe to call it from vApplicationTickHook().

I still think it would be much cleaner and more exact to use configGENERATE_RUN_TIME_STATS. You can use a spare TC timer to get an exact time in uS, and provide this function in your FreeRTOSConfig.h:

    extern uint32_t ulGetRunTimeCounterValue(void);
    #define portGET_RUN_TIME_COUNTER_VALUE()    ulGetRunTimeCounterValue()

What it misses is the possibility to clear the ulRunTimeCounter. Here is a description of how you can add that.

rtel wrote on Wednesday, May 24, 2017:

You could use the task tag, or thread local storage, but it’s not clear why you would do this in the tick hook rather than use the task stats or trace macros.

electronicdev wrote on Thursday, May 25, 2017:

I tried vApplicationTickHook with xTaskGetCurrentTaskHandle as shown below to measure task a execution time. Only 3 task are present. I check if task_a is running by vapplicationtickhook and increment counter and print result otherwise clear the counter if another task is running. However, the icount display an increasing value at each print no clear and start from 1 except the very first which is wrong behavior.
What is wrong? I’m new to rtos and still learning.
Thank you all

void vApplicationTickHook(void)
static int iCount = 0;

	if (taska_handle = xTaskGetCurrentTaskHandle())
	printf("exeqution_time_taska %d\r\n", iCount);

	if (taska_handle != xTaskGetCurrentTaskHandle())
	iCount = 0;


heinbali01 wrote on Thursday, May 25, 2017:

Your implementation of printf() is probably not fit to be called from within an ISR. Many standard implementations of printf() use the heap.

Beside that, you’re making a C-style error :

    if (taska_handle = xTaskGetCurrentTaskHandle())

I suppose you mean to write a comparison ( == ) and not an assignment ( = ) :

So simply write:

    if (taska_handle == xTaskGetCurrentTaskHandle())
        printf("exeqution_time_taska %d\r\n", iCount);
        iCount = 0;