From 9a4341bb8714ac7dc4ae5f7517e3fb579448f909 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Wed, 17 Sep 2025 12:27:41 +0200 Subject: [PATCH 1/3] refactor(freertos): Moved FreeRTOS Run Time Stats gathering to port.c This commit creates a new port layer API xPortGetRunTimeCounterValue() in port.c files. This helps to remove inclusion of header files such as esp_timer.h and xtensa/hal.h from portmacro.h --- .../riscv/include/freertos/portmacro.h | 17 +++++------------ .../FreeRTOS-Kernel-SMP/portable/riscv/port.c | 18 ++++++++++++++++++ .../xtensa/include/freertos/portmacro.h | 16 +++++----------- .../portable/xtensa/port.c | 19 +++++++++++++++++++ .../riscv/include/freertos/portmacro.h | 18 +++++------------- .../FreeRTOS-Kernel/portable/riscv/port.c | 18 ++++++++++++++++++ .../xtensa/include/freertos/portmacro.h | 19 ++++--------------- .../FreeRTOS-Kernel/portable/xtensa/port.c | 19 +++++++++++++++++++ 8 files changed, 93 insertions(+), 51 deletions(-) diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h index e238498ec8..d66b6df762 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h @@ -254,11 +254,10 @@ extern void vTaskExitCritical( void ); // ------------------- Run Time Stats ---------------------- #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() -#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time()) -#else -#define portGET_RUN_TIME_COUNTER_VALUE() 0 -#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ); +#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue() +#endif // --------------------- TCB Cleanup ----------------------- @@ -420,19 +419,13 @@ portmacro.h. Therefore, we need to keep these headers around for now to allow th #include #include #include +#include #include "esp_attr.h" #include "esp_newlib.h" #include "esp_heap_caps.h" #include "esp_rom_sys.h" #include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */ -/* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */ -#include - -/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */ -#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#include "esp_timer.h" -#endif #ifdef __cplusplus } diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c index 914964fa00..6d61e46c61 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c @@ -31,6 +31,9 @@ #include "port_systick.h" #include "portmacro.h" #include "esp_memory_utils.h" +#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#include "esp_timer.h" +#endif #ifdef CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER #include "soc/periph_defs.h" #include "soc/system_reg.h" @@ -516,3 +519,18 @@ void vApplicationPassiveIdleHook( void ) esp_vApplicationIdleHook(); //Run IDF style hooks } #endif // CONFIG_FREERTOS_USE_PASSIVE_IDLE_HOOK + +/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */ + +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) + +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ) +{ +#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER + return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time(); +#else + return 0; +#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +} + +#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */ diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h index d29389c457..59ae3548eb 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -239,11 +239,10 @@ extern void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus ); //Timers are already configured, so nothing to do for configuration of run time stats timer #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() -#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time()) -#else // Uses CCOUNT -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) xthal_get_ccount()) -#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ); +#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue() +#endif // --------------------- TCB Cleanup ----------------------- @@ -451,11 +450,6 @@ portmacro.h. Therefore, we need to keep these headers around for now to allow th #include #include -/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */ -#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#include "esp_timer.h" -#endif - #ifdef __cplusplus } #endif diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c index a9dbaaebe8..6ccd739fae 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c @@ -34,6 +34,10 @@ #include "esp_freertos_hooks.h" #include "esp_intr_alloc.h" #include "esp_memory_utils.h" +#include /* required for xthal_get_ccount() */ +#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#include "esp_timer.h" +#endif #ifdef CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER #include "soc/periph_defs.h" #include "soc/system_reg.h" @@ -695,3 +699,18 @@ void vApplicationPassiveIdleHook( void ) esp_vApplicationIdleHook(); //Run IDF style hooks } #endif // CONFIG_FREERTOS_USE_PASSIVE_IDLE_HOOK + +/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */ + +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) + +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ) +{ +#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER + return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time(); +#else // Uses CCOUNT + return (configRUN_TIME_COUNTER_TYPE) xthal_get_ccount(); +#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +} + +#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */ diff --git a/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h index 56939101fe..83017af485 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h @@ -6,7 +6,7 @@ * * SPDX-License-Identifier: MIT * - * SPDX-FileContributor: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileContributor: 2023-2025 Espressif Systems (Shanghai) CO LTD * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in @@ -71,15 +71,8 @@ #include "esp_heap_caps.h" #include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */ #include "esp_newlib.h" - -/* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */ #include -/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */ -#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#include "esp_timer.h" -#endif - #ifdef __cplusplus extern "C" { #endif @@ -603,11 +596,10 @@ void vPortTCBPreDeleteHook( void *pxTCB ); // ------------------- Run Time Stats ---------------------- #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() -#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time()) -#else -#define portGET_RUN_TIME_COUNTER_VALUE() 0 -#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ); +#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue() +#endif // --------------------- TCB Cleanup ----------------------- diff --git a/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c b/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c index 902e761d7a..e2ca2a5ef8 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c +++ b/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c @@ -56,6 +56,9 @@ #include "portmacro.h" #include "port_systick.h" #include "esp_memory_utils.h" +#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#include "esp_timer.h" +#endif #if SOC_CPU_HAS_HWLOOP #include "riscv/csr.h" @@ -874,6 +877,21 @@ void vPortCoprocUsedInISR(void* frame) #endif /* SOC_CPU_COPROC_NUM > 0 */ +/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */ + +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) + +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ) +{ +#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER + return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time(); +#else + return 0; +#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +} + +#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */ + /* ---------------------------------------------- Misc Implementations ------------------------------------------------- * * ------------------------------------------------------------------------------------------------------------------ */ diff --git a/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h index 53a35e0dfb..2da5d7b714 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h @@ -43,7 +43,6 @@ #include #include #include -#include /* required for xthal_get_ccount. [refactor-todo] use cpu_hal instead */ #include /* required for XTOS_SET_INTLEVEL. [refactor-todo] add common intr functions to esp_hw_support */ #include "xt_instr_macros.h" #include "spinlock.h" @@ -57,16 +56,7 @@ #include "esp_rom_sys.h" #include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */ #include "portbenchmark.h" - -/* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */ #include -#include -#include - -/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */ -#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#include "esp_timer.h" -#endif #ifdef __cplusplus extern "C" { @@ -510,11 +500,10 @@ extern void _frxt_setup_switch( void ); //Defined in portasm.S #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() -#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time()) -#else // Uses CCOUNT -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) xthal_get_ccount()) -#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ); +#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue() +#endif // --------------------- TCB Cleanup ----------------------- diff --git a/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c b/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c index 7f75dc241a..36337aa11a 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c +++ b/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c @@ -48,6 +48,10 @@ #include "task.h" /* Required for TaskHandle_t, tskNO_AFFINITY, and vTaskStartScheduler */ #include "port_systick.h" #include "esp_cpu.h" +#include /* required for xthal_get_ccount() */ +#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#include "esp_timer.h" +#endif #include "esp_memory_utils.h" _Static_assert(portBYTE_ALIGNMENT == 16, "portBYTE_ALIGNMENT must be set to 16"); @@ -659,3 +663,18 @@ void vPortTCBPreDeleteHook( void *pxTCB ) vPortCleanUpCoprocArea( pxTCB ); #endif /* XCHAL_CP_NUM > 0 */ } + +/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */ + +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) + +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ) +{ +#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER + return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time(); +#else // Uses CCOUNT + return (configRUN_TIME_COUNTER_TYPE) xthal_get_ccount(); +#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +} + +#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */ From 141e62806c1e248144873c8df519cfd4a3ef6259 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Mon, 22 Sep 2025 08:45:10 +0200 Subject: [PATCH 2/3] feat(freertos-smp): Enabled FreeRTOS Runtime stats gathering for Amazon SMP Kernel --- components/freertos/Kconfig | 2 +- .../config/include/freertos/FreeRTOSConfig.h | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/components/freertos/Kconfig b/components/freertos/Kconfig index 021e01d5e2..df75e9c63e 100644 --- a/components/freertos/Kconfig +++ b/components/freertos/Kconfig @@ -299,7 +299,7 @@ menu "FreeRTOS" choice FREERTOS_RUN_TIME_COUNTER_TYPE prompt "configRUN_TIME_COUNTER_TYPE" - depends on FREERTOS_GENERATE_RUN_TIME_STATS && !FREERTOS_SMP + depends on FREERTOS_GENERATE_RUN_TIME_STATS default FREERTOS_RUN_TIME_COUNTER_TYPE_U32 help Sets the data type used for the FreeRTOS run time stats. A larger data type can be used to reduce the diff --git a/components/freertos/config/include/freertos/FreeRTOSConfig.h b/components/freertos/config/include/freertos/FreeRTOSConfig.h index 3fb51d1618..8a505d5bc2 100644 --- a/components/freertos/config/include/freertos/FreeRTOSConfig.h +++ b/components/freertos/config/include/freertos/FreeRTOSConfig.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -168,13 +168,11 @@ #define configUSE_STATS_FORMATTING_FUNCTIONS 1 /* Used by vTaskList() */ #endif /* CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS */ -#if !CONFIG_FREERTOS_SMP - #if CONFIG_FREERTOS_RUN_TIME_COUNTER_TYPE_U32 - #define configRUN_TIME_COUNTER_TYPE uint32_t - #elif CONFIG_FREERTOS_RUN_TIME_COUNTER_TYPE_U64 - #define configRUN_TIME_COUNTER_TYPE uint64_t - #endif /* CONFIG_FREERTOS_RUN_TIME_COUNTER_TYPE_U64 */ -#endif /* !CONFIG_FREERTOS_SMP */ +#if CONFIG_FREERTOS_RUN_TIME_COUNTER_TYPE_U32 + #define configRUN_TIME_COUNTER_TYPE uint32_t +#elif CONFIG_FREERTOS_RUN_TIME_COUNTER_TYPE_U64 + #define configRUN_TIME_COUNTER_TYPE uint64_t +#endif /* CONFIG_FREERTOS_RUN_TIME_COUNTER_TYPE_U64 */ /* -------------------- Co-routines ----------------------- */ From cf124780492386f63349c857e421e7195056d232 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Wed, 17 Sep 2025 17:07:32 +0200 Subject: [PATCH 3/3] refactor(freertos): Deprecate xPortGetTickRateHz() This commit deprecates xPortGetTickRateHz() from the FreeRTOS port API as the FreeRTOS tickrate is constant for an application and can be inferred using the CONFIG_FREERTOS_HZ config option. --- .../portable/riscv/include/freertos/portmacro.h | 6 +++--- .../portable/xtensa/include/freertos/portmacro.h | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h index 83017af485..2280f54d46 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h @@ -407,11 +407,11 @@ void vApplicationSleep(TickType_t xExpectedIdleTime); /** * @brief Get the tick rate per second * - * @note [refactor-todo] make this inline - * @note [refactor-todo] Check if this function should be renamed (due to uint return type) + * @deprecated This function will be removed in IDF 7.0. Use CONFIG_FREERTOS_HZ directly instead. + * @note [refactor-todo] Remove this function in IDF 7.0 (IDF-14115) * @return uint32_t Tick rate in Hz */ -uint32_t xPortGetTickRateHz(void); +uint32_t xPortGetTickRateHz(void) __attribute__((deprecated("This function will be removed in IDF 7.0. Use CONFIG_FREERTOS_HZ directly instead."))); /** * @brief Set a watchpoint to watch the last 32 bytes of the stack diff --git a/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h index 2da5d7b714..ce0b206419 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h @@ -345,10 +345,11 @@ void vApplicationSleep(TickType_t xExpectedIdleTime); /** * @brief Get the tick rate per second * - * @note [refactor-todo] make this inline + * @deprecated This function will be removed in IDF 7.0. Use CONFIG_FREERTOS_HZ directly instead. + * @note [refactor-todo] Remove this function in IDF 7.0 (IDF-14115) * @return uint32_t Tick rate in Hz */ -uint32_t xPortGetTickRateHz(void); +uint32_t xPortGetTickRateHz(void) __attribute__((deprecated("This function will be removed in IDF 7.0. Use CONFIG_FREERTOS_HZ directly instead."))); /** * @brief Set a watchpoint to watch the last 32 bytes of the stack