# FreeRTOS task runtime stats: can't we just let the timer rollover?

**URL:** <https://forums.freertos.org/t/freertos-task-runtime-stats-cant-we-just-let-the-timer-rollover/12286>\
**Category:** Kernel\
**Tags:** monitoring\
**Created:** [April 12, 2021, 5:59pm UTC](https://forums.freertos.org/t/freertos-task-runtime-stats-cant-we-just-let-the-timer-rollover/12286 "2021-04-12T17:59:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![john-mamish](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/john-mamish/32/1476_2.png) [@john-mamish](https://forums.freertos.org/u/john-mamish)\
**Post date:** [April 12, 2021, 5:59pm UTC](https://forums.freertos.org/t/freertos-task-runtime-stats-cant-we-just-let-the-timer-rollover/12286/1 "2021-04-12T17:59:02Z")

</div>

I want to use vTaskGetRunTimeStats() to check task runtime in long-running system and make sure that some tasks aren’t taking more time than I expect. ulTotalRuntime is a uint32\_t, meaning that - if my system is up for a month - its maximum frequency without rolling over will be about 4kHz, which is a bit more modest than I’d like. Also, a month of uptime really isn’t that much; I’d like to be able to collect stats for longer than that.

It looks like the accumulation happens [here in vTaskSwitchContext](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/b08c19f74560dc8885eaf04c47483817d28e13e2/tasks.c#L3037). If the timer rolls over, FreeRTOS skips counting .

Why do this? It limits task stats on long-running systems. As long as the user-provided timer doesn’t roll over twice between calls to vTaskSwitchContext, doing an unsigned subtract on two uint32\_t that have rolled over will work out just fine. (of course, the user-provided timer has to have a period of 0x1\_0000\_0000).

Would it be possible to add a macro - say “configALLOW\_RUN\_TIME\_STAT\_TIMER\_ROLLOVER” - that skips this check in vTaskSwitchContext?

Thanks

- John

edit: I just realized that only the time accumulated for the task period where the timer rolls over will be discarded and that it won’t add up to much error at all. It would be nice if some info could be added to the documentation about portGET\_RUN\_TIME\_COUNTER\_VALUE saying that timer overflow is okay, and that it’ll just result in a little bit of unaccounted-for time.

---

<div class="post-metadata">

**Author:** ![n9wxu](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/n9wxu/32/1391_2.png) [@n9wxu](https://forums.freertos.org/u/n9wxu)\
**Post date:** [April 12, 2021, 9:47pm UTC](https://forums.freertos.org/t/freertos-task-runtime-stats-cant-we-just-let-the-timer-rollover/12286/2 "2021-04-12T21:47:39Z")

</div>

There are two rollovers to account for.  
_ulTotalRunTime_  
_pxCurrentTCB-\>ulRunTimeCounter_  
If ulTotalRunTime wraps, it will cause a minor loss of accuracy for ulRunTimeCounter.  
But if ulRunTimeCounter wraps you will need to compensate by adding 4.2e6 counts per rollover. A typical tick rate is 1mS so the system tick will roll ~49 days. If there are two tasks with equal priority, then their task counters will roll in ~100 days.

Is this sufficient for your application?

---

<div class="post-metadata">

**Author:** ![john-mamish](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/john-mamish/32/1476_2.png) [@john-mamish](https://forums.freertos.org/u/john-mamish)\
**Post date:** [April 12, 2021, 11:25pm UTC](https://forums.freertos.org/t/freertos-task-runtime-stats-cant-we-just-let-the-timer-rollover/12286/3 "2021-04-12T23:25:32Z")

</div>

Thanks for the reply. I hadn’t considered that ulRunTimeCounter would also have to not wrap.

In my case, the tasks I’m concerned about run at very low CPU utilization (~1%), so even with a 10KHz counter, it would take about 500 days for rollover to happen in the individual counters. That’s more than enough for my application.

I still think it would be nice if the documentation for vTaskGetRunTimeStats() explained this stuff.

---

<div class="post-metadata">

**Author:** ![n9wxu](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/n9wxu/32/1391_2.png) [@n9wxu](https://forums.freertos.org/u/n9wxu)\
**Post date:** [April 13, 2021, 12:03am UTC](https://forums.freertos.org/t/freertos-task-runtime-stats-cant-we-just-let-the-timer-rollover/12286/4 "2021-04-13T00:03:36Z")

</div>

I have created a feature request for additional clarification.

> <https://github.com/FreeRTOS/FreeRTOS-Kernel/issues/307>
>
> This issue https://forums.freertos.org/t/freertos-task-runtime-stats-cant-we-just-let-the-timer-rollover/12286 caused frustration that could be prevented with additional clarification on the effect of timer rollovers in the task...

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [April 13, 2021, 1:45am UTC](https://forums.freertos.org/t/freertos-task-runtime-stats-cant-we-just-let-the-timer-rollover/12286/5 "2021-04-13T01:45:23Z")

</div>

We already have a feature request to provide an API that allows the run-time counter to be reset to 0. That would be easy enough to do on a task by task basis but more complex if you want to reset the counter back to zero for all tasks at the same time as doing so requires all the state and blocked lists to be traversed to find all the tasks (in the same way other functions do today, such as xTaskGetSystemState() which provides details of every task in the system).

A very simple thing we could do is allow the application writer to determine the type of the variable used to hold the accumulated time. Then the application writer could use a 64-bit time - trading space and execution time for longevity. We already do that for some types - for example [the type used to specify the stack size](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L502) - which is going to be different for 8-bit MCUs with little memory compared to 64-bit MCUs with Gigs of memory.
