Skip to content

Commit c4da412

Browse files
committed
esp32 diagnostic trace
- Working backend with metric, trace and counter diagnostics - Diagnostic interface implementation with ring buffer storage - Added option ENABLE_ESP_DIAGNOSTICS_TRACE in chip KConfig - Added required options for enabling matter diagnostic trace in project Kconfig - Enabled diagnostic trace for temperature-measurement-app example
1 parent 57489d1 commit c4da412

File tree

15 files changed

+1006
-15
lines changed

15 files changed

+1006
-15
lines changed

config/esp32/components/chip/CMakeLists.txt

+16-2
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
298298
chip_gn_arg_append("matter_trace_config" "\"${CHIP_ROOT}/src/tracing/esp32_trace:esp32_trace_tracing\"")
299299
endif()
300300

301+
if (CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE)
302+
chip_gn_arg_bool("matter_enable_tracing_support" "true")
303+
chip_gn_arg_append("matter_trace_config" "\"${CHIP_ROOT}/src/tracing/esp32_diagnostic_trace:esp32_diagnostic_tracing\"")
304+
endif()
305+
301306
if (CONFIG_ENABLE_ESP_INSIGHTS_SYSTEM_STATS)
302307
chip_gn_arg_append("matter_enable_esp_insights_system_stats" "true")
303308
endif()
@@ -310,6 +315,10 @@ if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
310315
target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/tracing/esp32_trace/include")
311316
endif()
312317

318+
if (CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE)
319+
target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/tracing/esp32_diagnostic_trace/include")
320+
endif()
321+
313322
set(args_gn_input "${CMAKE_CURRENT_BINARY_DIR}/args.gn.in")
314323
file(GENERATE OUTPUT "${args_gn_input}" CONTENT "${chip_gn_args}")
315324

@@ -364,9 +373,14 @@ if(CONFIG_ENABLE_PW_RPC)
364373
endif()
365374

366375
if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
367-
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32TracingBackend.a")
376+
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32TracingBackend.a")
368377
endif()
369378

379+
if (CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE)
380+
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32DiagnosticsBackend.a")
381+
endif()
382+
383+
370384
# When using the pregenerated files, there is a edge case where an error appears for
371385
# undeclared argument chip_code_pre_generated_directory. To get around with it we are
372386
# disabling the --fail-on-unused-args flag.
@@ -593,4 +607,4 @@ if (CONFIG_CHIP_OTA_IMAGE_BUILD)
593607
)
594608
# Adding dependecy as app target so that this runs after images are ready
595609
add_dependencies(chip-ota-image app)
596-
endif()
610+
endif()

config/esp32/components/chip/Kconfig

+18-10
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,15 @@ menu "CHIP Device Layer"
992992
NVS namespace. If this option is enabled, the application can use an API to set a CD,
993993
the configured CD will be used for subsequent CD reads.
994994

995+
config ENABLE_ESP_DIAGNOSTICS_TRACE
996+
bool "Enable ESP Platform Diagnostics for Matter"
997+
depends on ESP_DIAGNOSTICS_ENABLED
998+
default y
999+
help
1000+
Enables the ESP Diagnostics platform to collect, store, and retrieve diagnostic data for the Matter protocol.
1001+
This feature helps monitor system health and performance by providing insights through diagnostics logs.
1002+
Requires ESP_DIAGNOSTICS_ENABLED to be activated.
1003+
9951004
config ENABLE_ESP_INSIGHTS_TRACE
9961005
bool "Enable Matter ESP Insights"
9971006
depends on ESP_INSIGHTS_ENABLED
@@ -1008,15 +1017,14 @@ menu "CHIP Device Layer"
10081017
help
10091018
This option enables the system statistics to be sent to the insights cloud.
10101019

1011-
config MAX_PERMIT_LIST_SIZE
1012-
int "Set permit list size for Insights traces"
1013-
range 5 30
1014-
depends on ESP_INSIGHTS_ENABLED
1015-
default 20
1016-
help
1017-
Maximum number of group entries that can be included in the permit list for reporting
1018-
the traces to insights.
1019-
1020+
config MAX_PERMIT_LIST_SIZE
1021+
int "Set permit list size for Insights traces"
1022+
range 5 30
1023+
depends on ESP_INSIGHTS_ENABLED || ESP_DIAGNOSTICS_ENABLED
1024+
default 20
1025+
help
1026+
Set the maximum number of group entries that can be included in the permit list for reporting
1027+
traces to Insights or diagnostics. This ensures proper management of trace reporting capacity.
10201028
endmenu
10211029

10221030

@@ -1396,4 +1404,4 @@ menu "CHIP Device Layer"
13961404

13971405
endmenu
13981406

1399-
endmenu
1407+
endmenu

examples/temperature-measurement-app/esp32/main/Kconfig.projbuild

+32
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,35 @@ depends on ENABLE_PW_RPC
8383
about available pin numbers for UART.
8484

8585
endmenu
86+
87+
menu "Platform Diagnostics"
88+
config ESP_DIAGNOSTICS_ENABLED
89+
bool "Enable ESP Diagnostics"
90+
default n
91+
92+
config DIAGNOSTIC_RETRIEVAL_BUFFER_SIZE
93+
int "Set buffer size to retrieve diagnostic data"
94+
depends on ESP_DIAGNOSTICS_ENABLED
95+
default 1024
96+
help
97+
Defines the buffer size (in bytes) for retrieving diagnostic data through diagnostic logs cluster.
98+
Increase this size if the diagnostic data generated by the application requires more space.
99+
100+
config END_USER_BUFFER_SIZE
101+
int "Set buffer size for end user diagnostic data"
102+
depends on ESP_DIAGNOSTICS_ENABLED
103+
default 4096
104+
help
105+
Defines the buffer size (in bytes) for storing diagnostic data related to end user activity.
106+
This buffer will hold logs and traces relevant to user interactions with the Matter protocol.
107+
Increase this size if the diagnostic data generated by the application requires more space.
108+
109+
config NETWORK_BUFFER_SIZE
110+
int "Set buffer size for network diagnostic data"
111+
depends on ESP_DIAGNOSTICS_ENABLED
112+
default 2048
113+
help
114+
Defines the buffer size (in bytes) for storing network-related diagnostic data.
115+
This buffer will store logs and traces related to network events and communication for the Matter protocol.
116+
Adjust this size based on the expected network diagnostics requirements.
117+
endmenu

examples/temperature-measurement-app/esp32/main/diagnostic-logs-provider-delegate-impl.cpp

+28-3
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,39 @@ CHIP_ERROR LogProvider::PrepareLogContextForIntent(LogContext * context, IntentE
105105
{
106106
context->intent = intent;
107107

108+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
109+
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
110+
111+
static uint8_t retrieveBuffer[RETRIEVAL_BUFFER_SIZE];
112+
MutableByteSpan endUserSupportSpan(retrieveBuffer, sizeof(retrieveBuffer));
113+
#endif
114+
108115
switch (intent)
109116
{
110117
case IntentEnum::kEndUserSupport: {
111-
context->EndUserSupport.span =
112-
ByteSpan(&endUserSupportLogStart[0], static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart));
118+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
119+
if (diagnosticStorage.IsEmptyBuffer())
120+
{
121+
printf("Buffer is empty\n");
122+
ChipLogError(DeviceLayer, "Empty Diagnostic buffer");
123+
return CHIP_ERROR_NOT_FOUND;
124+
}
125+
// Retrieve data from the diagnostic storage
126+
CHIP_ERROR err = diagnosticStorage.Retrieve(endUserSupportSpan);
127+
if (err != CHIP_NO_ERROR)
128+
{
129+
ChipLogError(DeviceLayer, "Failed to retrieve data: %s", chip::ErrorStr(err));
130+
return err;
131+
}
132+
133+
// Now, assign the span to the EndUserSupport object or whatever is required
134+
context->EndUserSupport.span = endUserSupportSpan;
135+
#else
136+
context->EndUserSupport.span =
137+
ByteSpan(&endUserSupportLogStart[0], static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart));
138+
#endif
113139
}
114140
break;
115-
116141
case IntentEnum::kNetworkDiag: {
117142
context->NetworkDiag.span =
118143
ByteSpan(&networkDiagnosticLogStart[0], static_cast<size_t>(networkDiagnosticLogEnd - networkDiagnosticLogStart));

examples/temperature-measurement-app/esp32/main/include/diagnostic-logs-provider-delegate-impl.h

+10
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,22 @@
1919
#pragma once
2020

2121
#include <app/clusters/diagnostic-logs-server/DiagnosticLogsProviderDelegate.h>
22+
23+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
24+
#include <src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h>
25+
#endif
26+
2227
#include <map>
2328

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

33+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
34+
#define RETRIEVAL_BUFFER_SIZE CONFIG_DIAGNOSTIC_RETRIEVAL_BUFFER_SIZE
35+
using namespace chip::Tracing;
36+
#endif
37+
2838
namespace chip {
2939
namespace app {
3040
namespace Clusters {

examples/temperature-measurement-app/esp32/main/main.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
#include <DeviceInfoProviderImpl.h>
5353
#endif // CONFIG_ENABLE_ESP32_DEVICE_INFO_PROVIDER
5454

55+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
56+
#include <tracing/esp32_diagnostic_trace/DiagnosticTracing.h>
57+
#endif
58+
5559
namespace {
5660
#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
5761
chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider;
@@ -75,6 +79,11 @@ static AppDeviceCallbacks EchoCallbacks;
7579
static void InitServer(intptr_t context)
7680
{
7781
Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config
82+
83+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
84+
static Tracing::Insights::ESP32Diagnostics diagnosticBackend;
85+
Tracing::Register(diagnosticBackend);
86+
#endif
7887
}
7988

8089
extern "C" void app_main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
#Copyright (c) 2024 Project CHIP Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import("//build_overrides/build.gni")
17+
import("//build_overrides/chip.gni")
18+
19+
config("tracing") {
20+
include_dirs = [ "include" ]
21+
}
22+
23+
static_library("backend") {
24+
output_name = "libEsp32DiagnosticsBackend"
25+
output_dir = "${root_out_dir}/lib"
26+
27+
sources = [
28+
"Counter.cpp",
29+
"Counter.h",
30+
"DiagnosticTracing.cpp",
31+
"DiagnosticTracing.h",
32+
"DiagnosticStorageManager.cpp",
33+
"DiagnosticStorageManager.h",
34+
"Diagnostics.h",
35+
]
36+
37+
public_deps = [
38+
"${chip_root}/src/lib/core",
39+
"${chip_root}/src/tracing",
40+
]
41+
}
42+
43+
source_set("esp32_diagnostic_tracing") {
44+
public = [ "include/matter/tracing/macros_impl.h" ]
45+
public_configs = [ ":tracing" ]
46+
deps = [ ":backend" ]
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <string.h>
20+
#include <tracing/esp32_diagnostic_trace/Counter.h>
21+
22+
using namespace chip;
23+
24+
namespace Insights {
25+
26+
// This is a one time allocation for counters. It is not supposed to be freed.
27+
ESPDiagnosticCounter * ESPDiagnosticCounter::mHead = nullptr;
28+
29+
ESPDiagnosticCounter * ESPDiagnosticCounter::GetInstance(const char * label)
30+
{
31+
ESPDiagnosticCounter * current = mHead;
32+
33+
while (current != nullptr)
34+
{
35+
if (strcmp(current->label, label) == 0)
36+
{
37+
current->instanceCount++;
38+
return current;
39+
}
40+
current = current->mNext;
41+
}
42+
43+
// Allocate a new instance if counter is not present in the list.
44+
void * ptr = Platform::MemoryAlloc(sizeof(ESPDiagnosticCounter));
45+
VerifyOrDie(ptr != nullptr);
46+
47+
ESPDiagnosticCounter * newInstance = new (ptr) ESPDiagnosticCounter(label);
48+
newInstance->mNext = mHead;
49+
mHead = newInstance;
50+
51+
return newInstance;
52+
}
53+
54+
int32_t ESPDiagnosticCounter::GetInstanceCount() const
55+
{
56+
return instanceCount;
57+
}
58+
59+
void ESPDiagnosticCounter::ReportMetrics()
60+
{
61+
CHIP_ERROR err = CHIP_NO_ERROR;
62+
Counter counter(label, instanceCount, esp_log_timestamp());
63+
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
64+
err = diagnosticStorage.Store(counter);
65+
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));
66+
}
67+
68+
} // namespace Insights
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <esp_diagnostics_metrics.h>
22+
#include <esp_log.h>
23+
#include <lib/support/CHIPMem.h>
24+
#include <lib/support/CHIPMemString.h>
25+
#include <string.h>
26+
#include "tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h"
27+
28+
using namespace chip::Tracing;
29+
30+
namespace Insights {
31+
32+
/**
33+
* This class is used to monotonically increment the counters as per the label of the counter macro
34+
* 'MATTER_TRACE_COUNTER(label)' and report the metrics to esp-insights.
35+
* As per the label of the counter macro, it adds the counter in the linked list with the name label if not
36+
* present and returns the same instance and increments the value if the counter is already present
37+
* in the list.
38+
*/
39+
40+
class ESPDiagnosticCounter
41+
{
42+
private:
43+
static ESPDiagnosticCounter * mHead; // head of the counter list
44+
const char * label; // unique key ,it is used as a static string.
45+
int32_t instanceCount;
46+
ESPDiagnosticCounter * mNext; // pointer to point to the next entry in the list
47+
48+
ESPDiagnosticCounter(const char * labelParam) : label(labelParam), instanceCount(1), mNext(nullptr) {}
49+
50+
public:
51+
static ESPDiagnosticCounter * GetInstance(const char * label);
52+
53+
int32_t GetInstanceCount() const;
54+
55+
void ReportMetrics();
56+
};
57+
58+
} // namespace Insights

0 commit comments

Comments
 (0)