From e9a44371d8fb0f63296d9780dd1617c17bd5e7b7 Mon Sep 17 00:00:00 2001 From: shripad621git Date: Fri, 8 Sep 2023 15:39:40 +0530 Subject: [PATCH] Added the support to report the chip system statistics to the esp-insights. - The system stats from src/system/SystemStats.h are reported on enabling a config option to esp-insights. --- config/esp32/components/chip/CMakeLists.txt | 4 + config/esp32/components/chip/Kconfig | 6 + examples/lighting-app/esp32/main/main.cpp | 36 ++-- src/tracing/esp32_trace/BUILD.gn | 18 +- .../matter/tracing/insights_sys_stats.cpp | 159 ++++++++++++++++++ .../matter/tracing/insights_sys_stats.h | 65 +++++++ 6 files changed, 271 insertions(+), 17 deletions(-) create mode 100644 src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.cpp create mode 100644 src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.h diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 61920102605cba..f927d770709279 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -264,6 +264,10 @@ if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE) chip_gn_arg_append("matter_enable_esp_insights_trace" "true") endif() +if (CONFIG_ENABLE_ESP_INSIGHTS_SYSTEM_STATS) + chip_gn_arg_append("matter_enable_esp_insights_system_stats" "true") +endif() + if (CONFIG_USE_ESP32_ECDSA_PERIPHERAL) chip_gn_arg_append("chip_use_esp32_ecdsa_peripheral" "true") endif() diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index 3c099437ad9813..86786b96cf4326 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -818,6 +818,12 @@ menu "CHIP Device Layer" Enabling the above option will enable the esp32 specific tracing functionality and report the diagnostic information to the insights cloud. + config ENABLE_ESP_INSIGHTS_SYSTEM_STATS + bool "Enable System Stats for insights" + depends on ESP_INSIGHTS_ENABLED + default n + help + This option enables the system statistics to be sent to the insights cloud. endmenu diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 79247258658dd8..5d73671f502585 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -39,6 +39,10 @@ #include #include +#if CONFIG_ENABLE_ESP_INSIGHTS_SYSTEM_STATS +#include +#endif + #if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER #include #endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER @@ -113,6 +117,24 @@ static void InitServer(intptr_t context) DeviceCallbacksDelegate::Instance().SetAppDelegate(&sAppDeviceCallbacksDelegate); Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config + +#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 + +#if CONFIG_ENABLE_ESP_INSIGHTS_SYSTEM_STATS + chip::System::Stats::InsightsSystemMetrics::GetInstance().RegisterAndEnable(chip::System::Clock::Timeout(5000)); +#endif } extern "C" void app_main() @@ -134,20 +156,6 @@ 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/src/tracing/esp32_trace/BUILD.gn b/src/tracing/esp32_trace/BUILD.gn index d7580a3e68ac2a..4d89677fabd18e 100644 --- a/src/tracing/esp32_trace/BUILD.gn +++ b/src/tracing/esp32_trace/BUILD.gn @@ -19,9 +19,21 @@ import("//build_overrides/chip.gni") config("tracing") { include_dirs = [ "include" ] } - +declare_args() +{ + matter_enable_esp_insights_system_stats = false +} source_set("esp32_trace") { - public = [ "include/matter/tracing/macros_impl.h" ] - sources = [ "include/matter/tracing/macros_impl.cpp" ] + public = [ "include/matter/tracing/macros_impl.h", ] + sources = [ "include/matter/tracing/macros_impl.cpp", ] public_configs = [ ":tracing" ] + + if (matter_enable_esp_insights_system_stats) + { + public += ["include/matter/tracing/insights_sys_stats.h",] + sources += ["include/matter/tracing/insights_sys_stats.cpp",] + deps = [ "${chip_root}/src/lib/core", + "${chip_root}/src/system", + "${chip_root}/src/platform"] + } } diff --git a/src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.cpp b/src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.cpp new file mode 100644 index 00000000000000..3e3dc02da86cff --- /dev/null +++ b/src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.cpp @@ -0,0 +1,159 @@ +/* + * 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. + */ + +#include "insights_sys_stats.h" +#include +#include +#include +#include +#include +#include +#include + +namespace chip { +namespace System { +namespace Stats { + +InsightsSystemMetrics & InsightsSystemMetrics::GetInstance() +{ + static InsightsSystemMetrics instance; + return instance; +} + +static void StartTimerCallback(Layer * systemLayer, void * context) +{ + auto instance = static_cast(context); + count_t * highwatermarks = GetHighWatermarks(); + for (int i = 0; i < System::Stats::kNumEntries; i++) + { + esp_err_t err = esp_diag_metrics_add_uint(instance->mLabels[i], static_cast(highwatermarks[i])); + if (err != ESP_OK) + { + ESP_LOGE("ERR", "Failed to add the metrics %d", err); + } + } + DeviceLayer::SystemLayer().StartTimer(instance->GetTimeout(), StartTimerCallback, instance); +} + +CHIP_ERROR InsightsSystemMetrics::Unregister() +{ + if (!mRegistered) + { + return CHIP_ERROR_CANCELLED; + } + for (int i = 0; i < System::Stats::kNumEntries; i++) + { + if (mLabels[i] != nullptr) + { + esp_err_t err = esp_diag_metrics_unregister(mLabels[i]); + if (err != ESP_OK) + { + ESP_LOGE(kTag, "Failed to unregister metric %d", err); + } + free(mLabels[i]); + mLabels[i] = nullptr; + } + } + return CHIP_NO_ERROR; +} + +static void CancelTimeout(intptr_t arg) +{ + DeviceLayer::SystemLayer().CancelTimer(StartTimerCallback, NULL); +} + +CHIP_ERROR InsightsSystemMetrics::SetTimeout(chip::System::Clock::Timeout aTimeout) +{ + if (!mRegistered) + { + return CHIP_ERROR_INCORRECT_STATE; + } + + if (aTimeout == System::Clock::kZero) + { + DeviceLayer::PlatformMgr().ScheduleWork(CancelTimeout, reinterpret_cast(this)); + } + else if (aTimeout != mTimeout) + { + DeviceLayer::PlatformMgr().ScheduleWork(CancelTimeout, reinterpret_cast(this)); + mTimeout = aTimeout; + CHIP_ERROR err = DeviceLayer::SystemLayer().StartTimer(mTimeout, StartTimerCallback, this); + if (err != CHIP_NO_ERROR) + { + ESP_LOGE(kTag, "Failed to start the new timer %" CHIP_ERROR_FORMAT, err.Format()); + return err; + } + } + + return CHIP_NO_ERROR; +} + +CHIP_ERROR InsightsSystemMetrics::RegisterAndEnable(chip::System::Clock::Timeout aTimeout) +{ + if (mRegistered) + { + return CHIP_NO_ERROR; + } + + if (aTimeout == System::Clock::kZero) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + + const Label * labels = GetStrings(); + for (int i = 0; i < System::Stats::kNumEntries; i++) + { + size_t labelLength = strlen(labels[i]); + if (labelLength > kMaxStringLength) + { + labelLength = kMaxStringLength; + mLabels[i] = strndup(labels[i], labelLength - 1); + } + else + { + mLabels[i] = strndup(labels[i], labelLength); + } + + if (mLabels[i] == NULL) + { + Unregister(); + return CHIP_ERROR_NO_MEMORY; + } + + esp_err_t err = esp_diag_metrics_register(kTag, mLabels[i], mLabels[i], kPath, ESP_DIAG_DATA_TYPE_UINT); + if (err != ESP_OK) + { + ESP_LOGE(kTag, "Failed to register metrics %d", err); + return chip::DeviceLayer::Internal::ESP32Utils::MapError(err); + } + } + + mTimeout = System::Clock::Milliseconds32(aTimeout); + + CHIP_ERROR err = DeviceLayer::SystemLayer().StartTimer(GetTimeout(), StartTimerCallback, this); + if (err != CHIP_NO_ERROR) + { + ESP_LOGE(kTag, "Failed to start the timer %" CHIP_ERROR_FORMAT, err.Format()); + return err; + } + mRegistered = true; + return CHIP_NO_ERROR; +} + +} // namespace Stats +} // namespace System +} // namespace chip diff --git a/src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.h b/src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.h new file mode 100644 index 00000000000000..9e725733debc59 --- /dev/null +++ b/src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.h @@ -0,0 +1,65 @@ +/* + * 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. + */ + +#include +#include +#include +namespace chip { +namespace System { +namespace Stats { + +class InsightsSystemMetrics +{ +public: + static InsightsSystemMetrics & GetInstance(); + + /* + * This api registers the system metrics to the insights and starts the + * timer to enable system stats periodically to the insights. + */ + CHIP_ERROR RegisterAndEnable(chip::System::Clock::Timeout aTimeout); + + /* + * This api unregisters the system stats which are registered + * as metrics to the esp-insights. + */ + CHIP_ERROR Unregister(); + + /* + * This api cancels the timeout providing the user the flexibility + * to increase or decrease the frequency of sampling the System + * Stats. It cancels the timer if new timeout value is zero. + * If the value of timeout differs from existing value, then + * it cancels the previous timer and starts a new timer. + */ + CHIP_ERROR SetTimeout(chip::System::Clock::Timeout aTimeout); + + System::Clock::Timeout GetTimeout() { return mTimeout; } + char * mLabels[chip::System::Stats::kNumEntries]; + +private: + InsightsSystemMetrics() {} + static constexpr int kMaxStringLength = 16; + bool mRegistered = false; + static constexpr char kPath[] = "sys.mtr"; + static constexpr char kTag[] = "MTR"; + System::Clock::Timeout mTimeout; +}; + +} // namespace Stats +} // namespace System +} // namespace chip