From ab45deafd97140eef66031e21a892721b2a5812a Mon Sep 17 00:00:00 2001 From: shripad621git Date: Wed, 30 Apr 2025 18:19:16 +0530 Subject: [PATCH] tools/jenkins: Added support for heap memory numbers in all_device_types app --- .../main/Kconfig.projbuild | 10 +++++ .../all_device_types_app/main/app_main.cpp | 20 +++++++++ examples/common/utils/log_heap_numbers.h | 23 ++++++++++ examples/light/main/app_main.cpp | 45 ++++--------------- tools/jenkins/esp_matter.groovy | 1 + 5 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 examples/common/utils/log_heap_numbers.h diff --git a/examples/all_device_types_app/main/Kconfig.projbuild b/examples/all_device_types_app/main/Kconfig.projbuild index 7cd09a77b..558c34d08 100644 --- a/examples/all_device_types_app/main/Kconfig.projbuild +++ b/examples/all_device_types_app/main/Kconfig.projbuild @@ -85,3 +85,13 @@ menu "Fan driver configuration" endmenu endmenu + +menu "Example Configuration" + config ENABLE_MEMORY_PROFILING + bool "Enable Memory Profiling" + default n + help + Enable this option to include memory profiling features in the example. + This will allow you to monitor memory usage during runtime. + +endmenu diff --git a/examples/all_device_types_app/main/app_main.cpp b/examples/all_device_types_app/main/app_main.cpp index 480a8fc27..6129a9bef 100644 --- a/examples/all_device_types_app/main/app_main.cpp +++ b/examples/all_device_types_app/main/app_main.cpp @@ -18,6 +18,8 @@ #include #include +#include + #include #include @@ -59,6 +61,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::kCommissioningComplete: ESP_LOGI(TAG, "Commissioning complete"); + MEMORY_PROFILER_DUMP_HEAP_STAT("commissioning complete"); break; case chip::DeviceLayer::DeviceEventType::kFailSafeTimerExpired: @@ -75,6 +78,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::kCommissioningWindowOpened: ESP_LOGI(TAG, "Commissioning window opened"); + MEMORY_PROFILER_DUMP_HEAP_STAT("commissioning window opened"); break; case chip::DeviceLayer::DeviceEventType::kCommissioningWindowClosed: @@ -132,6 +136,11 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) } break; + case chip::DeviceLayer::DeviceEventType::kBLEDeinitialized: + ESP_LOGI(TAG, "BLE deinitialized and memory reclaimed"); + MEMORY_PROFILER_DUMP_HEAP_STAT("BLE deinitialized"); + break; + default: break; } @@ -169,12 +178,16 @@ extern "C" void app_main() /* Initialize the ESP NVS layer */ nvs_flash_init(); + MEMORY_PROFILER_DUMP_HEAP_STAT("Bootup"); + /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; // node handle can be used to add/modify other endpoints. node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); + MEMORY_PROFILER_DUMP_HEAP_STAT("node created"); + uint8_t device_type_index; if (esp_matter::nvs_helpers::get_device_type_from_nvs(&device_type_index) != ESP_OK) { semaphoreHandle = xSemaphoreCreateBinary(); @@ -213,6 +226,8 @@ extern "C" void app_main() ESP_LOGE(TAG, "Matter start failed: %d", err); } + MEMORY_PROFILER_DUMP_HEAP_STAT("matter started"); + #if CONFIG_ENABLE_CHIP_SHELL esp_matter::console::diagnostics_register_commands(); esp_matter::console::wifi_register_commands(); @@ -223,4 +238,9 @@ extern "C" void app_main() #endif // CONFIG_OPENTHREAD_BORDER_ROUTER && CONFIG_OPENTHREAD_CLI #endif + + while (true) { + MEMORY_PROFILER_DUMP_HEAP_STAT("Idle"); + vTaskDelay(10000 / portTICK_PERIOD_MS); + } } diff --git a/examples/common/utils/log_heap_numbers.h b/examples/common/utils/log_heap_numbers.h new file mode 100644 index 000000000..61a8b5fca --- /dev/null +++ b/examples/common/utils/log_heap_numbers.h @@ -0,0 +1,23 @@ +#include +#include + +static const char *HEAP_PROFILING_TAG = "heap_profiling"; + +static void memory_profiler_dump_heap_stat(const char *state) +{ +#ifdef CONFIG_ENABLE_MEMORY_PROFILING + ESP_LOGI(HEAP_PROFILING_TAG,"========== HEAP-DUMP-START ==========\n"); + ESP_LOGI(HEAP_PROFILING_TAG,"state: %s\n", state); + ESP_LOGI(HEAP_PROFILING_TAG,"\tDescription\tInternal\n"); + ESP_LOGI(HEAP_PROFILING_TAG,"Current Free Memory\t%d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); + ESP_LOGI(HEAP_PROFILING_TAG,"Largest Free Block\t%d\n", heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL)); + ESP_LOGI(HEAP_PROFILING_TAG,"Min. Ever Free Size\t%d\n", heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL)); + ESP_LOGI(HEAP_PROFILING_TAG,"========== HEAP-DUMP-END ==========\n"); +#endif +} + +#ifdef CONFIG_ENABLE_MEMORY_PROFILING +#define MEMORY_PROFILER_DUMP_HEAP_STAT(state) memory_profiler_dump_heap_stat(state) +#else +#define MEMORY_PROFILER_DUMP_HEAP_STAT(state) +#endif // CONFIG_ENABLE_MEMORY_PROFILING diff --git a/examples/light/main/app_main.cpp b/examples/light/main/app_main.cpp index 6155b68d7..9408593e8 100644 --- a/examples/light/main/app_main.cpp +++ b/examples/light/main/app_main.cpp @@ -15,6 +15,8 @@ #include #include +#include + #include #include #if CHIP_DEVICE_CONFIG_ENABLE_THREAD @@ -60,19 +62,6 @@ static const char *s_decryption_key = decryption_key_start; static const uint16_t s_decryption_key_len = decryption_key_end - decryption_key_start; #endif // CONFIG_ENABLE_ENCRYPTED_OTA -#ifdef CONFIG_ENABLE_MEMORY_PROFILING -static void memory_profiler_dump_heap_stat(const char *state) -{ - ESP_LOGI(TAG,"========== HEAP-DUMP-START ==========\n"); - ESP_LOGI(TAG,"state: %s\n", state); - ESP_LOGI(TAG,"\tDescription\tInternal\n"); - ESP_LOGI(TAG,"Current Free Memory\t%d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); - ESP_LOGI(TAG,"Largest Free Block\t%d\n", heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL)); - ESP_LOGI(TAG,"Min. Ever Free Size\t%d\n", heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL)); - ESP_LOGI(TAG,"========== HEAP-DUMP-END ==========\n"); -} -#endif - static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) { switch (event->Type) { @@ -82,10 +71,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::kCommissioningComplete: ESP_LOGI(TAG, "Commissioning complete"); -#ifdef CONFIG_ENABLE_MEMORY_PROFILING - memory_profiler_dump_heap_stat("commissioning complete"); -#endif - + MEMORY_PROFILER_DUMP_HEAP_STAT("commissioning complete"); break; case chip::DeviceLayer::DeviceEventType::kFailSafeTimerExpired: @@ -102,10 +88,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::kCommissioningWindowOpened: ESP_LOGI(TAG, "Commissioning window opened"); -#ifdef CONFIG_ENABLE_MEMORY_PROFILING - memory_profiler_dump_heap_stat("commissioning window opened"); -#endif - + MEMORY_PROFILER_DUMP_HEAP_STAT("commissioning window opened"); break; case chip::DeviceLayer::DeviceEventType::kCommissioningWindowClosed: @@ -149,9 +132,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::kBLEDeinitialized: ESP_LOGI(TAG, "BLE deinitialized and memory reclaimed"); -#ifdef CONFIG_ENABLE_MEMORY_PROFILING - memory_profiler_dump_heap_stat("BLE deinitialized"); -#endif + MEMORY_PROFILER_DUMP_HEAP_STAT("BLE deinitialized"); break; default: @@ -192,9 +173,7 @@ extern "C" void app_main() /* Initialize the ESP NVS layer */ nvs_flash_init(); -#ifdef CONFIG_ENABLE_MEMORY_PROFILING - memory_profiler_dump_heap_stat("Bootup"); -#endif + MEMORY_PROFILER_DUMP_HEAP_STAT("Bootup"); /* Initialize driver */ app_driver_handle_t light_handle = app_driver_light_init(); @@ -208,9 +187,7 @@ extern "C" void app_main() node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); -#ifdef CONFIG_ENABLE_MEMORY_PROFILING - memory_profiler_dump_heap_stat("node created"); -#endif + MEMORY_PROFILER_DUMP_HEAP_STAT("node created"); extended_color_light::config_t light_config; light_config.on_off.on_off = DEFAULT_POWER; @@ -275,9 +252,7 @@ extern "C" void app_main() err = esp_matter::start(app_event_cb); ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); -#ifdef CONFIG_ENABLE_MEMORY_PROFILING - memory_profiler_dump_heap_stat("matter started"); -#endif + MEMORY_PROFILER_DUMP_HEAP_STAT("matter started"); /* Starting driver with default values */ app_driver_light_set_defaults(light_endpoint_id); @@ -297,10 +272,8 @@ extern "C" void app_main() esp_matter::console::init(); #endif -#ifdef CONFIG_ENABLE_MEMORY_PROFILING while (true) { - memory_profiler_dump_heap_stat("Idle"); + MEMORY_PROFILER_DUMP_HEAP_STAT("Idle"); vTaskDelay(10000 / portTICK_PERIOD_MS); } -#endif } diff --git a/tools/jenkins/esp_matter.groovy b/tools/jenkins/esp_matter.groovy index 340ea8c5d..e3922bad9 100644 --- a/tools/jenkins/esp_matter.groovy +++ b/tools/jenkins/esp_matter.groovy @@ -69,6 +69,7 @@ def firmware_build() { echo "CONFIG_ENABLE_OTA_REQUESTOR=y" >> sdkconfig.defaults echo "CONFIG_ESP_COREDUMP_ENABLE_TO_UART=y" >> sdkconfig.defaults echo "CONFIG_FACTORY_DEVICE_INSTANCE_INFO_PROVIDER=y" >> sdkconfig.defaults + echo "CONFIG_ENABLE_MEMORY_PROFILING=y" >> sdkconfig.defaults idf.py set-target ${chip} if [ "${FIRMWARE_TYPE}" = "OTA" ]; then