Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


measure execution by vApplicationTickHook, change priority?

Posted by electronicdev on May 23, 2017

Hi 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

measure execution by vApplicationTickHook, change priority?

Posted by heinbali01 on 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 configGENERATERUNTIME_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 configUSEPREEMPTION 1 #define configUSETIME_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.

measure execution by vApplicationTickHook, change priority?

Posted by richard_damon on 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).

measure execution by vApplicationTickHook, change priority?

Posted by electronicdev on 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

measure execution by vApplicationTickHook, change priority?

Posted by heinbali01 on 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 uint32t ulGetRunTimeCounterValue(void); #define portGETRUNTIMECOUNTER_VALUE() ulGetRunTimeCounterValue() ~~~

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

measure execution by vApplicationTickHook, change priority?

Posted by rtel on 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.

measure execution by vApplicationTickHook, change priority?

Posted by electronicdev on 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;


measure execution by vApplicationTickHook, change priority?

Posted by heinbali01 on May 25, 2017

Oops 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 (taskahandle == xTaskGetCurrentTaskHandle()) { iCount++; printf("exequtiontime_taska %drn", iCount); } else { iCount = 0; } } ~~~


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists