Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESP32] Platform diagnostics framework #36532

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
chip_gn_arg_append("matter_trace_config" "\"${CHIP_ROOT}/src/tracing/esp32_trace:esp32_trace_tracing\"")
endif()

if (CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE)
chip_gn_arg_bool("matter_enable_tracing_support" "true")
chip_gn_arg_append("matter_trace_config" "\"${CHIP_ROOT}/src/tracing/esp32_diagnostic_trace:esp32_diagnostic_tracing\"")
endif()

if (CONFIG_ENABLE_ESP_INSIGHTS_SYSTEM_STATS)
chip_gn_arg_append("matter_enable_esp_insights_system_stats" "true")
endif()
Expand All @@ -310,6 +315,10 @@ if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/tracing/esp32_trace/include")
endif()

if (CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE)
target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/tracing/esp32_diagnostic_trace/include")
endif()

if (CONFIG_CHIP_DEVICE_ENABLE_DYNAMIC_SERVER)
chip_gn_arg_append("chip_build_controller_dynamic_server" "true")
endif()
Expand Down Expand Up @@ -371,6 +380,11 @@ if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32TracingBackend.a")
endif()

if (CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE)
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32DiagnosticsBackend.a")
endif()


# When using the pregenerated files, there is a edge case where an error appears for
# undeclared argument chip_code_pre_generated_directory. To get around with it we are
# disabling the --fail-on-unused-args flag.
Expand Down
24 changes: 15 additions & 9 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,13 @@ menu "CHIP Device Layer"
NVS namespace. If this option is enabled, the application can use an API to set a CD,
the configured CD will be used for subsequent CD reads.

config ENABLE_ESP_DIAGNOSTICS_TRACE
bool "Enable ESP Platform Diagnostics for Matter"
default n
help
Enables the ESP Diagnostics platform to collect, store, and retrieve diagnostic data for the Matter protocol.
This feature helps monitor system health and performance by providing insights through diagnostics logs.

config ENABLE_ESP_INSIGHTS_TRACE
bool "Enable Matter ESP Insights"
depends on ESP_INSIGHTS_ENABLED
Expand All @@ -1015,15 +1022,14 @@ menu "CHIP Device Layer"
help
This option enables the system statistics to be sent to the insights cloud.

config MAX_PERMIT_LIST_SIZE
int "Set permit list size for Insights traces"
range 5 30
depends on ESP_INSIGHTS_ENABLED
default 20
help
Maximum number of group entries that can be included in the permit list for reporting
the traces to insights.

config MAX_PERMIT_LIST_SIZE
int "Set permit list size for Insights traces"
range 5 30
depends on ESP_INSIGHTS_ENABLED || ENABLE_ESP_DIAGNOSTICS_TRACE
default 20
help
Set the maximum number of group entries that can be included in the permit list for reporting
traces to Insights or diagnostics. This ensures proper management of trace reporting capacity.
endmenu


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,14 @@ depends on ENABLE_PW_RPC
about available pin numbers for UART.

endmenu

menu "Platform Diagnostics"

config END_USER_BUFFER_SIZE
int "Set buffer size for end user diagnostic data"
depends on ENABLE_ESP_DIAGNOSTICS_TRACE
default 4096
help
Defines the buffer size (in bytes) for storing diagnostic data related to end user activity.
This buffer will hold logs and traces relevant to user interactions with the Matter protocol.
endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ size_t LogProvider::GetSizeForIntent(IntentEnum intent)
{
switch (intent)
{
case IntentEnum::kEndUserSupport:
case IntentEnum::kEndUserSupport: {
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
// Returning buffer_size > 1024 bytes to transfer data over BDX otherwise it uses Response Payload.
return CONFIG_END_USER_BUFFER_SIZE;
#else
return static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart);
#endif
}
break;
case IntentEnum::kNetworkDiag:
return static_cast<size_t>(networkDiagnosticLogEnd - networkDiagnosticLogStart);
case IntentEnum::kCrashLogs:
Expand Down Expand Up @@ -108,8 +115,28 @@ CHIP_ERROR LogProvider::PrepareLogContextForIntent(LogContext * context, IntentE
switch (intent)
{
case IntentEnum::kEndUserSupport: {
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
MutableByteSpan endUserSupportSpan(endUserBuffer, CONFIG_END_USER_BUFFER_SIZE);

if (diagnosticStorage.IsEmptyBuffer())
{
ChipLogError(DeviceLayer, "Empty Diagnostic buffer");
return CHIP_ERROR_NOT_FOUND;
}
// Retrieve data from the diagnostic storage
CHIP_ERROR err = diagnosticStorage.Retrieve(endUserSupportSpan);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to retrieve data: %s", chip::ErrorStr(err));
return err;
}
// Now, assign the span to the EndUserSupport object or whatever is required
context->EndUserSupport.span = endUserSupportSpan;
#else
context->EndUserSupport.span =
ByteSpan(&endUserSupportLogStart[0], static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart));
#endif
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@
#pragma once

#include <app/clusters/diagnostic-logs-server/DiagnosticLogsProviderDelegate.h>

#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
#include <tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h>
#endif

#include <map>

#if defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
#include <esp_core_dump.h>
#endif // defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)

#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
static uint8_t endUserBuffer[CONFIG_END_USER_BUFFER_SIZE];
using namespace chip::Tracing::Diagnostics;
#endif

namespace chip {
namespace app {
namespace Clusters {
Expand Down
10 changes: 10 additions & 0 deletions examples/temperature-measurement-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#include <DeviceInfoProviderImpl.h>
#endif // CONFIG_ENABLE_ESP32_DEVICE_INFO_PROVIDER

#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
#include <tracing/esp32_diagnostic_trace/DiagnosticTracing.h>
#endif

namespace {
#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider;
Expand Down Expand Up @@ -83,6 +87,12 @@ extern "C" void app_main()
chip::rpc::Init();
#endif

#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance(endUserBuffer, CONFIG_END_USER_BUFFER_SIZE);
static ESP32Diagnostics diagnosticBackend(diagnosticStorage);
Tracing::Register(diagnosticBackend);
#endif

ESP_LOGI(TAG, "Temperature sensor!");

// Initialize the ESP NVS layer.
Expand Down
3 changes: 3 additions & 0 deletions scripts/tools/check_includes_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@
'src/tracing/json/json_tracing.cpp': {'string', 'sstream'},
'src/tracing/json/json_tracing.h': {'fstream', 'unordered_map', 'string'},

# esp32 diagnostic tracing
'src/tracing/esp32_diagnostic_trace/Counter.h': {'map'},

# esp32 tracing
'src/tracing/esp32_trace/esp32_tracing.h': {'unordered_map'},

Expand Down
47 changes: 47 additions & 0 deletions src/tracing/esp32_diagnostic_trace/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
#Copyright (c) 2024 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" ]
}

static_library("backend") {
output_name = "libEsp32DiagnosticsBackend"
output_dir = "${root_out_dir}/lib"

sources = [
"Counter.cpp",
"Counter.h",
"DiagnosticStorageManager.cpp",
"DiagnosticStorageManager.h",
"DiagnosticTracing.cpp",
"DiagnosticTracing.h",
"Diagnostics.h",
]

public_deps = [
"${chip_root}/src/lib/core",
"${chip_root}/src/tracing",
]
}

source_set("esp32_diagnostic_tracing") {
public = [ "include/matter/tracing/macros_impl.h" ]
public_configs = [ ":tracing" ]
deps = [ ":backend" ]
}
55 changes: 55 additions & 0 deletions src/tracing/esp32_diagnostic_trace/Counter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
*
* Copyright (c) 2024 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 <string.h>
#include <tracing/esp32_diagnostic_trace/Counter.h>

namespace chip {
namespace Tracing {
namespace Diagnostics {

std::map<const char *, uint32_t> ESPDiagnosticCounter::mCounterList;

void ESPDiagnosticCounter::CountInit(const char * label)
{
if (mCounterList.find(label) != mCounterList.end())
{
mCounterList[label]++;
}
else
{
mCounterList[label] = 1;
}
}

uint32_t ESPDiagnosticCounter::GetInstanceCount(const char * label) const
{
return mCounterList[label];
}

void ESPDiagnosticCounter::ReportMetrics(const char * label, DiagnosticStorageInterface & mStorageInstance)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Counter counter(label, GetInstanceCount(label), esp_log_timestamp());
err = mStorageInstance.Store(counter);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));
}

} // namespace Diagnostics
} // namespace Tracing
} // namespace chip
63 changes: 63 additions & 0 deletions src/tracing/esp32_diagnostic_trace/Counter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
*
* Copyright (c) 2024 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

#include "tracing/esp32_diagnostic_trace/Diagnostics.h"
#include <esp_diagnostics_metrics.h>
#include <esp_log.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPMemString.h>
#include <map>
#include <string.h>

namespace chip {
namespace Tracing {
namespace Diagnostics {

/**
* This class is used to monotonically increment the counters as per the label of the counter macro
* 'MATTER_TRACE_COUNTER(label)'
* As per the label of the counter macro, it adds the counter in the linked list with the name label if not
* present and returns the same instance and increments the value if the counter is already present
* in the list.
*/

class ESPDiagnosticCounter
{
public:
static ESPDiagnosticCounter & GetInstance(const char * label)
{
static ESPDiagnosticCounter instance;
CountInit(label);
return instance;
}

uint32_t GetInstanceCount(const char * label) const;

void ReportMetrics(const char * label, DiagnosticStorageInterface & mStorageInstance);

private:
ESPDiagnosticCounter() {}
static std::map<const char *, uint32_t> mCounterList;
static void CountInit(const char * label);
};

} // namespace Diagnostics
} // namespace Tracing
} // namespace chip
Loading
Loading