From f8e09323163006dd20b6732bbf82bc25ce87b3cf Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Wed, 21 Aug 2024 11:46:51 +0200 Subject: [PATCH] feat(freertos): add functions to better measure the CPU usage (#6619) --- src/core/lv_global.h | 8 ++++++++ src/osal/lv_freertos.c | 38 +++++++++++++++++++++++++++++++++++++- src/osal/lv_freertos.h | 23 +++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/core/lv_global.h b/src/core/lv_global.h index 424e191291..c106e4f507 100644 --- a/src/core/lv_global.h +++ b/src/core/lv_global.h @@ -222,6 +222,14 @@ typedef struct lv_global_t { lv_mutex_t lv_general_mutex; #endif +#if LV_USE_OS == LV_OS_FREERTOS + uint32_t freertos_idle_time_sum; + uint32_t freertos_non_idle_time_sum; + uint32_t freertos_task_switch_timestamp; + bool freertos_idle_task_running; +#endif + + void * user_data; } lv_global_t; diff --git a/src/osal/lv_freertos.c b/src/osal/lv_freertos.c index 80d16a1e8b..33fd5f6e09 100644 --- a/src/osal/lv_freertos.c +++ b/src/osal/lv_freertos.c @@ -13,7 +13,6 @@ * INCLUDES *********************/ #include "lv_os.h" - #if LV_USE_OS == LV_OS_FREERTOS #if (ESP_PLATFORM) @@ -22,7 +21,10 @@ #include "atomic.h" #endif +#include "../tick/lv_tick.h" #include "../misc/lv_log.h" +#include "../core/lv_global.h" + /********************* * DEFINES *********************/ @@ -32,6 +34,8 @@ #define pcTASK_NAME "lvglDraw" #endif +#define globals LV_GLOBAL_DEFAULT() + /********************** * TYPEDEFS **********************/ @@ -348,6 +352,38 @@ lv_result_t lv_thread_sync_signal_isr(lv_thread_sync_t * pxCond) return LV_RESULT_OK; } + +void lv_freertos_task_switch_in(const char * name) +{ + if(lv_strcmp(name, "IDLE")) globals->freertos_idle_task_running = false; + else globals->freertos_idle_task_running = true; + + globals->freertos_task_switch_timestamp = lv_tick_get(); +} + +void lv_freertos_task_switch_out(void) +{ + uint32_t elaps = lv_tick_elaps(globals->freertos_task_switch_timestamp); + if(globals->freertos_idle_task_running) globals->freertos_idle_time_sum += elaps; + else globals->freertos_non_idle_time_sum += elaps; +} + +uint32_t lv_os_get_idle_percent(void) +{ + if(globals->freertos_non_idle_time_sum + globals->freertos_idle_time_sum == 0) { + LV_LOG_WARN("Not enough time elapsed to provide idle percentage"); + return 0; + } + + uint32_t pct = (globals->freertos_idle_time_sum * 100) / (globals->freertos_idle_time_sum + + globals->freertos_non_idle_time_sum); + + globals->freertos_non_idle_time_sum = 0; + globals->freertos_idle_time_sum = 0; + + return pct; +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/osal/lv_freertos.h b/src/osal/lv_freertos.h index 093500611e..5de9a95a82 100644 --- a/src/osal/lv_freertos.h +++ b/src/osal/lv_freertos.h @@ -77,6 +77,29 @@ typedef struct { * GLOBAL PROTOTYPES **********************/ +/** + * Set it for `traceTASK_SWITCHED_IN()` as + * `lv_freertos_task_switch_in(pxCurrentTCB->pcTaskName)` + * to save the start time stamp of a task + * @param name the name of the which is switched in + */ +void lv_freertos_task_switch_in(const char * name); + +/** + * Set it for `traceTASK_SWITCHED_OUT()` as + * `lv_freertos_task_switch_out()` + * to save finish time stamp of a task + */ +void lv_freertos_task_switch_out(void); + +/** + * Set it for `LV_SYSMON_GET_IDLE` to show the CPU usage + * as reported based the usage of FreeRTOS's idle task + * If it's important when a GPU is used. + * @return the idle percentage since the last call + */ +uint32_t lv_os_get_idle_percent(void); + /********************** * MACROS **********************/