Workaround for FreeRTOS Runtime Counter Issues in VS Code

FreeRTOS has a great performance measurement feature built-in: Performance counters. At each context switch, the RTOS can do a bookkeeping of time spent in tasks. With this, it can estimate the runtime distribution between the tasks. A very useful feature to get a feeling what the tasks are doing.

But I noticed that with recent FreeRTOS versions, VS Code extension have issues showing the correct runtime counter values:

Unknown Runtime Counters in VS Code Extension (mcu-debug.rtos-views)
Unknown Runtime Counters in VS Code Extension (mcu-debug.rtos-views)

VS Code Extensions

The issue exists both for the mcu-debug.rtos-views extension v0.0.7, see above) and for the the NXP extension RTOS view (nxpsemiconductors.mcuxpresso, v25.2.45):

No runtime counters (nxpsemiconductors.mcuxpresso)
No runtime counters (nxpsemiconductors.mcuxpresso)

It works fine for the Embedded Tools (ms-vscode.vscode-embedded-tools, v0.8.0) from Microsoft:

Working Runtime Counters (vs-vscode.vscode-embedded tools)
Working Runtime Counters (vs-vscode.vscode-embedded tools)

There might be other extension affected.

FreeRTOS Multicore Support

After some digging, I discovered that the performance counter display in the extension is broken. This started when FreeRTOS added multicore (SMP) support.

Prior that release, FreeRTOS stored the total runtime counter in a single variable:

PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime = 0U; /**< Holds the total amount of execution time as defined by the run time counter clock. */

With SMP support, there needs to be a counter for each core:

PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */

The affected VS Code Extensions cannot handle that situation. The variable is now an array and not a single variable.

The Solution

One solution would be to fix the extensions or wait for a new release. But it is unknown when that fix would be implemented and available.

Instead, and easy workaround is to revert that change to an array.

This is a rather easy patch in FreeRTOS, with the use of a configuration macro:

#define USE_RUNTIME_COUNTER_ARRAY  (configNUMBER_OF_CORES>1)

For systems with more than one core, it uses an array for the counters. For single core systems it uses a single variable only, which works fine for all the extension I use.

With this, runtime counters are shown again in the view:

Runtime Counters working again with the Patch

Summary

The SMP support in FreeRTOS confuses the runtime information for debug extensions. The reason is that the runtime counter changed from a variable to an array of variables.

A workaround is to patch FreeRTOS revert that change at least for single core systems. You can find the patch for FreeRTOS on GitHub (https://github.com/ErichStyger/McuLib/commit/73b19da5c637039e907d45202be7d5ae9bcff3d9).

Have you noticed that issue too? Are you using performance counters at all? Let me know and share your experience with performance counters.

Happy performing 🙂

Links

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.