Measuring task execution time

I’d like to measure task execution time by using uxTaskGetSystemState in task.h
So I defined configUSE_TRACE_FACILITY and check the included path of task.h
build clean and rebuild…
but link error occurred as below.
what should I check?

c:/users/jjs/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\esp-wrover-kit\src\main.cpp.o:(.literal._Z17checkTaskExecTimev+0x8): undefined reference to `uxTaskGetSystemState'
c:/users/jjs/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\esp-wrover-kit\src\main.cpp.o: in function `checkTaskExecTime()':
C:\Users\JJS\Documents\PlatformIO\Projects\221212-140807-arduino-blink/src/main.cpp:494: undefined reference to `uxTaskGetSystemState'


platform = espressif32
framework = arduino
board = esp-wrover-kit
monitor_speed = 115200
build_type = debug
board_build.filesystem = littlefs
lib_deps = 
build_flags =

<c_cpp_properties.json > “configurations” > “includePath”>



#include <freertos/task.h>

void checkTaskExecTime(void)
  #define TASK_NAME_LEN 16
    // Get the number of tasks in the system
    UBaseType_t numTasks = uxTaskGetNumberOfTasks();

    // Allocate memory for the task status array
    TaskStatus_t *taskStatusArray = (TaskStatus_t *)pvPortMalloc(numTasks * sizeof(TaskStatus_t));

    // Get the task status array
    UBaseType_t numTasksUpdated = uxTaskGetSystemState(taskStatusArray, numTasks, NULL);

    // Iterate through the task status array
    for (UBaseType_t i = 0; i < numTasksUpdated; i++)
        TaskStatus_t taskStatus = taskStatusArray[i];

        // Check if this is the task we are interested in
        if (strncmp(taskStatus.pcTaskName, "Task", TASK_NAME_LEN) == 0)
            // Print the execution time of the task
            Serial.printf("Task execution time: %u ticks\n", taskStatus.ulRunTimeCounter);

    // Free the memory allocated for the task status array

Arduino is based on a precompiled ESP-IDF version, menaing the configXXX macros were set once, static .a files were generated, and are then included in Arduino-ESP32.

Changing the value of the macro after that fact has 0 impact on the .a file where the implementation lives. Meaning the include files will probably tell you that the function is available, but since the .a files are not recompiled, the implementation is still missing, and thus you can “undefined reference”.

You can resolve this by using the “Arduino as an ESP-IDF component” way (aka, framework = arduino, espidf) as shown in platform-espressif32/examples/espidf-arduino-blink at develop · platformio/platform-espressif32 · GitHub.

I don’t want to use espidf and arduino together because of the terrible build times.
Can I install and use an external freertos library instead of the freertos built-in of the esp32 arduino framework? If I set libdeps = ext_freertos, can I use the built-in freertos overriding?

You’ll have to use GitHub - espressif/esp32-arduino-lib-builder to regenerate the .a libraries anew with your wanted target configuration, then them to replace those stored in C:\Users\<user>\.platformio\packages\framework-arduinoespressif32\tools\sdk\esp32\lib. At least libfreertos.a.

1 Like

I ran the command as below, but it said that cmake needs to be registered in the PATH. Please tell me where the cmake executable is located.

$ ./ -t esp32 -b menuconfig
'cmake' must be available on the PATH to use -DIDF_TARGET="esp32" -DSDKCONFIG_DEFAULTS="configs/defconfig.common;configs/defconfig.esp32;configs/defconfig.qio_ram" menuconfig
ESP-IDF v4.4.3-347-g9ee3c8337d
'cmake' must be available on the PATH to use

I found it.


I added cmake.exe path to the PATH in ~/.bashrc
When I ran “./ -t esp32 -b menuconfig”, the same error message as above displayed.

export PATH=$PATH:/mnt/c/Users/JJS/.platformio/packages/tool-cmake/bin

The README says you should install it system-wide with

sudo apt-get install git wget curl libssl-dev libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-click python-cryptography python-future python-pyparsing python-pyelftools cmake ninja-build ccache jq

though? This is not supposed to be built with PlatformIO…

After installing cmake again, when I run it, menuconfig works.
./ -t esp32 -b menuconfig

component > FreeRTOS > Enable FreeRTOS trace facility

./ -t esp32 -b build
All esp-idf libraries compiled. it

Can I build only FreeRTOS Library?

I think it’s safer to replace all the .a libraries if they have dependencies between them.

I have tried the procedure below several times, but it seems that the changed value is not being applied.
is this right?

  1. ./ -t esp32 -b menuconfig
  3. ./ -t esp32 -b build
  4. replace C:\Users<user>.platformio\packages\framework-arduinoespressif32\tools\sdk\esp32\lib with \home<user>\esp32-arduino-lib-builder\build\esp-idf

After replacing the libraries you can also change the value or the trace macro to 1 to indicate the function is available in the new libs.

What does the firmware do when using the new libraries? Just nothing?

When / -t esp32 -b menuconfig is executed, the settings of the library in esp32-arduino-lib-builder are displayed in the menu. How can I display the settings in C:\Users\JJS\.platformio\packages\framework-arduinoespressif32\tools\sdk\esp32\lib in the menuconfig menu?
For example, in the library used in the project, core dump is set to not use, but the library in esp32-arduino-lib-builder is set to flash as shown below.

I do not want to check all the differences, but want to change only some of them while maintaining the existing settings.

Best you can do is read the options from sdkconfig.h, e.g. this one.