From 136649400b8ee2ac4b22fd0e964d2cb5e3817f80 Mon Sep 17 00:00:00 2001 From: shripad621git <79364691+shripad621git@users.noreply.github.com> Date: Thu, 24 Aug 2023 20:14:40 +0530 Subject: [PATCH] Added tracing_functionality for esp32 platform and integrated the tracing with esp_insights. (#28466) Added a esp32_trace directory to provide esp32 specific basic implementation of the scoped tracing macros. Integrated esp-insights on a config option in the esp32 lighting-app. This is an initial implemenation of the esp32 specific tracing functionality. --- config/esp32/components/chip/CMakeLists.txt | 13 +++++ config/esp32/components/chip/Kconfig | 9 ++++ .../esp32/components/chip/idf_component.yml | 4 ++ .../esp32/sdkconfig_m5stack_rpc.defaults | 2 + .../esp32/sdkconfig_m5stack_rpc.defaults | 2 + examples/chef/esp32/sdkconfig_rpc.defaults | 2 + examples/lighting-app/esp32/README.md | 19 +++++++ .../lighting-app/esp32/main/CMakeLists.txt | 4 ++ examples/lighting-app/esp32/main/main.cpp | 23 +++++++++ .../lighting-app/esp32/sdkconfig_rpc.defaults | 2 + .../esp32/sdkconfig_m5stack_rpc.defaults | 2 + .../esp32/sdkconfig_rpc.defaults | 2 + examples/pigweed-app/esp32/sdkconfig.defaults | 2 + examples/platform/esp32/PigweedLogger.cpp | 12 +++++ .../esp32/sdkconfig.optimize.defaults | 2 + .../esp32/sdkconfig_rpc.defaults | 2 + src/tracing/esp32_trace/BUILD.gn | 27 ++++++++++ .../include/matter/tracing/macros_impl.cpp | 44 ++++++++++++++++ .../include/matter/tracing/macros_impl.h | 50 +++++++++++++++++++ src/tracing/tracing_args.gni | 24 ++++++--- 20 files changed, 241 insertions(+), 6 deletions(-) create mode 100644 src/tracing/esp32_trace/BUILD.gn create mode 100644 src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp create mode 100644 src/tracing/esp32_trace/include/matter/tracing/macros_impl.h diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 8dc09e91131be2..2f8655b81130ee 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -260,6 +260,10 @@ if (CONFIG_SEC_CERT_DAC_PROVIDER) chip_gn_arg_append("chip_use_secure_cert_dac_provider" "true") endif() +if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE) + chip_gn_arg_append("matter_enable_esp_insights_trace" "true") +endif() + if (CONFIG_USE_ESP32_ECDSA_PERIPHERAL) chip_gn_arg_append("chip_use_esp32_ecdsa_peripheral" "true") endif() @@ -367,6 +371,10 @@ target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/config/esp32/${CONFIG_CHIP_EXTERNAL_PLATFORM_DIR}/../../" ) +if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE) + target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/tracing/esp32_trace/include") +endif() + idf_component_get_property(mbedtls_lib mbedtls COMPONENT_LIB) idf_build_get_property(idf_target IDF_TARGET) @@ -422,6 +430,11 @@ if (CONFIG_SEC_CERT_DAC_PROVIDER) list(APPEND chip_libraries $) endif() +if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE) + idf_component_get_property(esp_insights_lib espressif__esp_insights COMPONENT_LIB) + list(APPEND chip_libraries $) +endif() + idf_component_get_property(lwip_lib lwip COMPONENT_LIB) list(APPEND chip_libraries $) diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index 30c152eff480fa..e7ec5ebd7a6645 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -798,6 +798,15 @@ menu "CHIP Device Layer" then this option gets enabled. Also, please disable ESP_SECURE_CERT_DS_PERIPHERAL from the menuconfig when this option is disabled + config ENABLE_ESP_INSIGHTS_TRACE + bool "Enable Matter ESP Insights" + depends on ESP_INSIGHTS_ENABLED + default y + help + ESP Insights is a remote diagnostics solution to monitor the health of ESP devices in the field. + Enabling the above option will enable the esp32 specific tracing functionality and report the + diagnostic information to the insights cloud. + endmenu diff --git a/config/esp32/components/chip/idf_component.yml b/config/esp32/components/chip/idf_component.yml index 82c7fe3fc81338..91340b4bfb6fe2 100644 --- a/config/esp32/components/chip/idf_component.yml +++ b/config/esp32/components/chip/idf_component.yml @@ -15,3 +15,7 @@ dependencies: version: "2.0.3" rules: - if: "idf_version >=4.4" + + espressif/esp_insights: + version: "1.0.1" + require: public diff --git a/examples/all-clusters-app/esp32/sdkconfig_m5stack_rpc.defaults b/examples/all-clusters-app/esp32/sdkconfig_m5stack_rpc.defaults index 213d44fe9c616e..56fed5d65e6797 100644 --- a/examples/all-clusters-app/esp32/sdkconfig_m5stack_rpc.defaults +++ b/examples/all-clusters-app/esp32/sdkconfig_m5stack_rpc.defaults @@ -80,3 +80,5 @@ CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y # to avoid dram overflow, reduce the critical loggin buffer to 1K CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE=1024 + +CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP=y diff --git a/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack_rpc.defaults b/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack_rpc.defaults index 213d44fe9c616e..56fed5d65e6797 100644 --- a/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack_rpc.defaults +++ b/examples/all-clusters-minimal-app/esp32/sdkconfig_m5stack_rpc.defaults @@ -80,3 +80,5 @@ CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y # to avoid dram overflow, reduce the critical loggin buffer to 1K CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE=1024 + +CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP=y diff --git a/examples/chef/esp32/sdkconfig_rpc.defaults b/examples/chef/esp32/sdkconfig_rpc.defaults index a1506dd52891bc..bf2bcc8a7b7bc7 100644 --- a/examples/chef/esp32/sdkconfig_rpc.defaults +++ b/examples/chef/esp32/sdkconfig_rpc.defaults @@ -61,3 +61,5 @@ CONFIG_MBEDTLS_HKDF_C=y # IRAM optimizations CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y + +CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP=y diff --git a/examples/lighting-app/esp32/README.md b/examples/lighting-app/esp32/README.md index b84deeaf9d3fbd..5a6f9be37d286a 100644 --- a/examples/lighting-app/esp32/README.md +++ b/examples/lighting-app/esp32/README.md @@ -8,6 +8,25 @@ and refer [building and commissioning](../../../docs/guides/esp32/build_app_and_commission.md) guides to get started. +### Enabling ESP-Insights: + +- Before building the app, enable the option: ESP_INSIGHTS_ENABLED through + menuconfig. + +- Create a file named insights_auth_key.txt in the main directory of the + example. + +- Follow the steps + present[here](https://github.com/espressif/esp-insights/blob/main/examples/README.md#set-up-esp-insights-account) + to set up an insights_account and the auth key created while setting it up + will be used in the example. + +- Download the auth key and copy Auth Key to the example + +``` +cp /path/to/auth/key.txt path/to/connectedhomeip/examples/lighting-app/esp32/main/insights_auth_key.txt +``` + --- - [Cluster Control](#cluster-control) diff --git a/examples/lighting-app/esp32/main/CMakeLists.txt b/examples/lighting-app/esp32/main/CMakeLists.txt index fcb0121d9f6d78..a3149e3fc7df90 100644 --- a/examples/lighting-app/esp32/main/CMakeLists.txt +++ b/examples/lighting-app/esp32/main/CMakeLists.txt @@ -106,6 +106,10 @@ include("${CHIP_ROOT}/build/chip/esp32/esp32_codegen.cmake") chip_app_component_codegen("${CHIP_ROOT}/examples/lighting-app/lighting-common/lighting-app.matter") chip_app_component_zapgen("${CHIP_ROOT}/examples/lighting-app/lighting-common/lighting-app.zap") +if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE) + target_add_binary_data(${COMPONENT_TARGET} "insights_auth_key.txt" TEXT) +endif() + set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") target_compile_options(${COMPONENT_LIB} PUBLIC diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 38d9ea040b05fe..79247258658dd8 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -59,11 +59,20 @@ #include #endif +#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE +#include +#endif + using namespace ::chip; using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; using namespace ::chip::DeviceLayer; +#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE +extern const char insights_auth_key_start[] asm("_binary_insights_auth_key_txt_start"); +extern const char insights_auth_key_end[] asm("_binary_insights_auth_key_txt_end"); +#endif + static const char * TAG = "light-app"; static AppDeviceCallbacks EchoCallbacks; @@ -125,6 +134,20 @@ extern "C" void app_main() chip::rpc::Init(); #endif +#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE + esp_insights_config_t config = { + .log_type = ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING | ESP_DIAG_LOG_TYPE_EVENT, + .auth_key = insights_auth_key_start, + }; + + esp_err_t ret = esp_insights_init(&config); + + if (ret != ESP_OK) + { + ESP_LOGE(TAG, "Failed to initialize ESP Insights, err:0x%x", ret); + } +#endif + ESP_LOGI(TAG, "=================================================="); ESP_LOGI(TAG, "chip-esp32-light-example starting"); ESP_LOGI(TAG, "=================================================="); diff --git a/examples/lighting-app/esp32/sdkconfig_rpc.defaults b/examples/lighting-app/esp32/sdkconfig_rpc.defaults index ef9e2e510545e9..57ac2d6b027c56 100644 --- a/examples/lighting-app/esp32/sdkconfig_rpc.defaults +++ b/examples/lighting-app/esp32/sdkconfig_rpc.defaults @@ -53,3 +53,5 @@ CONFIG_ENABLE_PW_RPC=y # Enable HKDF in mbedtls CONFIG_MBEDTLS_HKDF_C=y + +CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP=y diff --git a/examples/ota-requestor-app/esp32/sdkconfig_m5stack_rpc.defaults b/examples/ota-requestor-app/esp32/sdkconfig_m5stack_rpc.defaults index 8d425ec83719cd..d9b1fb6241bb4e 100644 --- a/examples/ota-requestor-app/esp32/sdkconfig_m5stack_rpc.defaults +++ b/examples/ota-requestor-app/esp32/sdkconfig_m5stack_rpc.defaults @@ -73,3 +73,5 @@ CONFIG_ENABLE_PW_RPC=y # Enable HKDF in mbedtls CONFIG_MBEDTLS_HKDF_C=y + +CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP=y diff --git a/examples/ota-requestor-app/esp32/sdkconfig_rpc.defaults b/examples/ota-requestor-app/esp32/sdkconfig_rpc.defaults index 8e3f95fd44319f..5f75e4705a7413 100644 --- a/examples/ota-requestor-app/esp32/sdkconfig_rpc.defaults +++ b/examples/ota-requestor-app/esp32/sdkconfig_rpc.defaults @@ -66,3 +66,5 @@ CONFIG_ENABLE_PW_RPC=y # Enable HKDF in mbedtls CONFIG_MBEDTLS_HKDF_C=y + +CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP=y diff --git a/examples/pigweed-app/esp32/sdkconfig.defaults b/examples/pigweed-app/esp32/sdkconfig.defaults index 3421646d8fbbdf..8a5033a3f6f3b1 100644 --- a/examples/pigweed-app/esp32/sdkconfig.defaults +++ b/examples/pigweed-app/esp32/sdkconfig.defaults @@ -43,3 +43,5 @@ CONFIG_DEVICE_PRODUCT_ID=0x800B # Enable HKDF in mbedtls CONFIG_MBEDTLS_HKDF_C=y + +CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP=y diff --git a/examples/platform/esp32/PigweedLogger.cpp b/examples/platform/esp32/PigweedLogger.cpp index d10b89bcd9f69f..abd694f994ed81 100644 --- a/examples/platform/esp32/PigweedLogger.cpp +++ b/examples/platform/esp32/PigweedLogger.cpp @@ -23,6 +23,10 @@ #include #include +#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE && CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP +#include +#endif + namespace PigweedLogger { namespace { @@ -126,6 +130,10 @@ extern "C" void __wrap_esp_log_write(esp_log_level_t level, const char * tag, co } #endif +#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE && CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP + esp_diag_log_writev(level, tag, format, v); +#endif + va_end(v); } @@ -153,6 +161,10 @@ extern "C" void __wrap_esp_log_writev(esp_log_level_t level, const char * tag, c PigweedLogger::putString(logResetColor, strlen(logResetColor)); } #endif + +#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE && CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP + esp_diag_log_write(level, tag, format, v); +#endif } } // namespace PigweedLogger diff --git a/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults b/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults index ec8e29e4726ee6..0e72bfa20bd3d8 100644 --- a/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults +++ b/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults @@ -79,3 +79,5 @@ CONFIG_TCP_SYNMAXRTX=6 # Enable HKDF in mbedtls CONFIG_MBEDTLS_HKDF_C=y + +CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP=y diff --git a/examples/temperature-measurement-app/esp32/sdkconfig_rpc.defaults b/examples/temperature-measurement-app/esp32/sdkconfig_rpc.defaults index 936e8ee05908da..d839e526cfd0de 100644 --- a/examples/temperature-measurement-app/esp32/sdkconfig_rpc.defaults +++ b/examples/temperature-measurement-app/esp32/sdkconfig_rpc.defaults @@ -94,3 +94,5 @@ CONFIG_ENABLE_PW_RPC=y # Enable HKDF in mbedtls CONFIG_MBEDTLS_HKDF_C=y + +CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP=y diff --git a/src/tracing/esp32_trace/BUILD.gn b/src/tracing/esp32_trace/BUILD.gn new file mode 100644 index 00000000000000..d7580a3e68ac2a --- /dev/null +++ b/src/tracing/esp32_trace/BUILD.gn @@ -0,0 +1,27 @@ +# +#Copyright (c) 2023 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build_overrides/build.gni") +import("//build_overrides/chip.gni") + +config("tracing") { + include_dirs = [ "include" ] +} + +source_set("esp32_trace") { + public = [ "include/matter/tracing/macros_impl.h" ] + sources = [ "include/matter/tracing/macros_impl.cpp" ] + public_configs = [ ":tracing" ] +} diff --git a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp new file mode 100644 index 00000000000000..7046dd9cc8d7d9 --- /dev/null +++ b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "macros_impl.h" +#include +#include +namespace Insights { + +#define LOG_HEAP_INFO(label, group, entry_exit) \ + do \ + { \ + ESP_DIAG_EVENT("MTR_TRC", "%s - %s - %s Min Free heap - %u - LFB - %u Start free heap - %u", entry_exit, label, group, \ + heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT), \ + heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT), \ + heap_caps_get_free_size(MALLOC_CAP_8BIT)); \ + } while (0) + +ESP32Backend::ESP32Backend(const char * str, ...) +{ + va_list args; + va_start(args, str); + mlabel = str; + mgroup = va_arg(args, const char *); + LOG_HEAP_INFO(mlabel, mgroup, "Entry"); +} + +ESP32Backend::~ESP32Backend() +{ + LOG_HEAP_INFO(mlabel, mgroup, "Exit"); +} +} // namespace Insights diff --git a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h new file mode 100644 index 00000000000000..1ab529313c3192 --- /dev/null +++ b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +/* Ensure we do not have double tracing macros defined */ +#if defined(MATTER_TRACE_BEGIN) +#error "Tracing macros seem to be double defined" +#endif + +namespace Insights { +class ESP32Backend +{ +public: + ESP32Backend(const char * str, ...); + ~ESP32Backend(); + +private: + const char * mlabel; + const char * mgroup; +}; +} // namespace Insights + +#define MATTER_TRACE_SCOPE(...) \ + do \ + { \ + Insights::ESP32Backend backend(__VA_ARGS__); \ + } while (0) + +#define _MATTER_TRACE_DISABLE(...) \ + do \ + { \ + } while (false) + +#define MATTER_TRACE_BEGIN(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) +#define MATTER_TRACE_END(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) +#define MATTER_TRACE_INSTANT(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) diff --git a/src/tracing/tracing_args.gni b/src/tracing/tracing_args.gni index 7c27f9ecaba39a..8c4dbd66656b88 100644 --- a/src/tracing/tracing_args.gni +++ b/src/tracing/tracing_args.gni @@ -14,6 +14,7 @@ import("//build_overrides/build.gni") import("//build_overrides/chip.gni") import("${build_root}/config/compiler/compiler.gni") +import("${chip_root}/src/platform/device.gni") declare_args() { # Tracing can have a non-zero size impact to binaries. We try to make @@ -22,7 +23,7 @@ declare_args() { # # Additionally, if tracing is enabled, the main() function has to add # backends explicitly - matter_enable_tracing_support = current_os == "android" + matter_enable_tracing_support = false # Defines the trace backend. Current matter tracing splits the logic # into two parts: @@ -43,9 +44,20 @@ declare_args() { # since tracing is very noisy, we generally expect it to be explicitly # set up. # - if (current_os == "linux" || current_os == "android") { - matter_trace_config = "${chip_root}/src/tracing/perfetto:perfetto_tracing" - } else { - matter_trace_config = "${chip_root}/src/tracing/none" - } + matter_trace_config = "" + matter_enable_esp_insights_trace = false +} + +if (current_os == "android" || + (chip_device_platform == "esp32" && matter_enable_esp_insights_trace)) { + matter_enable_tracing_support = true +} + +if (current_os == "linux" || current_os == "android") { + matter_trace_config = "${chip_root}/src/tracing/perfetto:perfetto_tracing" +} else if (chip_device_platform == "esp32" && + matter_enable_esp_insights_trace) { + matter_trace_config = "${chip_root}/src/tracing/esp32_trace" +} else { + matter_trace_config = "${chip_root}/src/tracing/none" }