Skip to content

Commit ae51ae7

Browse files
committed
esp32 diagnostic trace changes
- Resolve buffer related issues - Add store diagnostic call for trace_end and trace_instant - Update scoped macro - Remove extra namespace values, spaces and print statements - Remove evicthead call for circular buffer after read successful
1 parent c4da412 commit ae51ae7

File tree

14 files changed

+135
-154
lines changed

14 files changed

+135
-154
lines changed

config/esp32/components/chip/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,11 @@ if(CONFIG_ENABLE_PW_RPC)
373373
endif()
374374

375375
if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
376-
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32TracingBackend.a")
376+
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32TracingBackend.a")
377377
endif()
378378

379379
if (CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE)
380-
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32DiagnosticsBackend.a")
380+
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32DiagnosticsBackend.a")
381381
endif()
382382

383383

@@ -607,4 +607,4 @@ if (CONFIG_CHIP_OTA_IMAGE_BUILD)
607607
)
608608
# Adding dependecy as app target so that this runs after images are ready
609609
add_dependencies(chip-ota-image app)
610-
endif()
610+
endif()

config/esp32/components/chip/Kconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1404,4 +1404,4 @@ menu "CHIP Device Layer"
14041404

14051405
endmenu
14061406

1407-
endmenu
1407+
endmenu

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

-18
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,11 @@ menu "Platform Diagnostics"
8989
bool "Enable ESP Diagnostics"
9090
default n
9191

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-
10092
config END_USER_BUFFER_SIZE
10193
int "Set buffer size for end user diagnostic data"
10294
depends on ESP_DIAGNOSTICS_ENABLED
10395
default 4096
10496
help
10597
Defines the buffer size (in bytes) for storing diagnostic data related to end user activity.
10698
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.
11799
endmenu

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ using namespace chip::app::Clusters::DiagnosticLogs;
2929
LogProvider LogProvider::sInstance;
3030
LogProvider::CrashLogContext LogProvider::sCrashLogContext;
3131

32+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
33+
static uint8_t retrieveBuffer[RETRIEVAL_BUFFER_SIZE];
34+
MutableByteSpan endUserSupportSpan(retrieveBuffer, sizeof(retrieveBuffer));
35+
#endif
36+
3237
namespace {
3338
bool IsValidIntent(IntentEnum intent)
3439
{
@@ -76,7 +81,14 @@ size_t LogProvider::GetSizeForIntent(IntentEnum intent)
7681
switch (intent)
7782
{
7883
case IntentEnum::kEndUserSupport:
79-
return static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart);
84+
{
85+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
86+
return RETRIEVAL_BUFFER_SIZE;
87+
#else
88+
return static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart);
89+
#endif
90+
}
91+
break;
8092
case IntentEnum::kNetworkDiag:
8193
return static_cast<size_t>(networkDiagnosticLogEnd - networkDiagnosticLogStart);
8294
case IntentEnum::kCrashLogs:
@@ -105,17 +117,12 @@ CHIP_ERROR LogProvider::PrepareLogContextForIntent(LogContext * context, IntentE
105117
{
106118
context->intent = intent;
107119

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-
115120
switch (intent)
116121
{
117122
case IntentEnum::kEndUserSupport: {
118123
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
124+
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
125+
119126
if (diagnosticStorage.IsEmptyBuffer())
120127
{
121128
printf("Buffer is empty\n");
@@ -129,7 +136,6 @@ CHIP_ERROR LogProvider::PrepareLogContextForIntent(LogContext * context, IntentE
129136
ChipLogError(DeviceLayer, "Failed to retrieve data: %s", chip::ErrorStr(err));
130137
return err;
131138
}
132-
133139
// Now, assign the span to the EndUserSupport object or whatever is required
134140
context->EndUserSupport.span = endUserSupportSpan;
135141
#else

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
#endif // defined(CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH) && defined(CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF)
3232

3333
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
34-
#define RETRIEVAL_BUFFER_SIZE CONFIG_DIAGNOSTIC_RETRIEVAL_BUFFER_SIZE
35-
using namespace chip::Tracing;
34+
#define RETRIEVAL_BUFFER_SIZE CONFIG_END_USER_BUFFER_SIZE
35+
using namespace chip::Tracing::Diagnostics;
3636
#endif
3737

3838
namespace chip {

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ extern const char TAG[] = "temperature-measurement-app";
7676

7777
static AppDeviceCallbacks EchoCallbacks;
7878

79+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
80+
using namespace chip::Tracing::Diagnostics;
81+
static uint8_t endUserBuffer[CONFIG_END_USER_BUFFER_SIZE];
82+
static size_t buffer_size = CONFIG_END_USER_BUFFER_SIZE;
83+
#endif
84+
7985
static void InitServer(intptr_t context)
8086
{
8187
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
8788
}
8889

8990
extern "C" void app_main()
@@ -92,6 +93,11 @@ extern "C" void app_main()
9293
chip::rpc::Init();
9394
#endif
9495

96+
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
97+
static ESP32Diagnostics diagnosticBackend(endUserBuffer, buffer_size);
98+
Tracing::Register(diagnosticBackend);
99+
#endif
100+
95101
ESP_LOGI(TAG, "Temperature sensor!");
96102

97103
// Initialize the ESP NVS layer.

src/tracing/esp32_diagnostic_trace/Counter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
using namespace chip;
2323

24-
namespace Insights {
24+
namespace Diagnostics {
2525

2626
// This is a one time allocation for counters. It is not supposed to be freed.
2727
ESPDiagnosticCounter * ESPDiagnosticCounter::mHead = nullptr;
@@ -65,4 +65,4 @@ void ESPDiagnosticCounter::ReportMetrics()
6565
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));
6666
}
6767

68-
} // namespace Insights
68+
} // namespace Diagnostics

src/tracing/esp32_diagnostic_trace/Counter.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
#include <string.h>
2626
#include "tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h"
2727

28-
using namespace chip::Tracing;
28+
using namespace chip::Tracing::Diagnostics;
2929

30-
namespace Insights {
30+
namespace Diagnostics {
3131

3232
/**
3333
* 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.
34+
* 'MATTER_TRACE_COUNTER(label)'
3535
* As per the label of the counter macro, it adds the counter in the linked list with the name label if not
3636
* present and returns the same instance and increments the value if the counter is already present
3737
* in the list.
@@ -55,4 +55,4 @@ class ESPDiagnosticCounter
5555
void ReportMetrics();
5656
};
5757

58-
} // namespace Insights
58+
} // namespace Diagnostics

src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.cpp

+25-41
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@
1717
* limitations under the License.
1818
*/
1919

20-
#include <esp_log.h>
2120
#include <lib/core/CHIPError.h>
2221
#include <lib/support/logging/CHIPLogging.h>
2322
#include <tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h>
2423

2524
namespace chip {
2625
namespace Tracing {
2726

28-
DiagnosticStorageImpl::DiagnosticStorageImpl() :
29-
mEndUserCircularBuffer(mEndUserBuffer, END_USER_BUFFER_SIZE), mNetworkCircularBuffer(mNetworkBuffer, NETWORK_BUFFER_SIZE)
30-
{}
27+
namespace Diagnostics {
28+
DiagnosticStorageImpl::DiagnosticStorageImpl(uint8_t * buffer, size_t bufferSize)
29+
: mEndUserCircularBuffer(buffer, bufferSize) {}
30+
31+
DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance(uint8_t * buffer, size_t bufferSize) {
32+
static DiagnosticStorageImpl instance(buffer, bufferSize);
33+
return instance;
34+
}
3135

3236
DiagnosticStorageImpl::~DiagnosticStorageImpl() {}
3337

@@ -39,10 +43,10 @@ CHIP_ERROR DiagnosticStorageImpl::Store(DiagnosticEntry & diagnostic)
3943
writer.Init(mEndUserCircularBuffer);
4044

4145
// Start a TLV structure container (Anonymous)
42-
chip::TLV::TLVType outerContainer;
43-
err = writer.StartContainer(AnonymousTag(), chip::TLV::TLVType::kTLVType_Structure, outerContainer);
46+
TLVType outerContainer;
47+
err = writer.StartContainer(AnonymousTag(), kTLVType_Structure, outerContainer);
4448
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
45-
ChipLogError(DeviceLayer, "Failed to start TLV container for metric : %s", chip::ErrorStr(err)));
49+
ChipLogError(DeviceLayer, "Failed to start TLV container for metric : %s", ErrorStr(err)));
4650

4751
err = diagnostic.Encode(writer);
4852
if (err != CHIP_NO_ERROR)
@@ -55,32 +59,24 @@ CHIP_ERROR DiagnosticStorageImpl::Store(DiagnosticEntry & diagnostic)
5559
}
5660
err = writer.EndContainer(outerContainer);
5761
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
58-
ChipLogError(DeviceLayer, "Failed to end TLV container for metric : %s", chip::ErrorStr(err)));
62+
ChipLogError(DeviceLayer, "Failed to end TLV container for metric : %s", ErrorStr(err)));
5963

6064
err = writer.Finalize();
6165
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to finalize TLV writing"));
62-
63-
printf("Total Data Length in Buffer : %lu\n Total available length in buffer : %lu\nTotal buffer length : %lu\n",
64-
mEndUserCircularBuffer.DataLength(), mEndUserCircularBuffer.AvailableDataLength(),
65-
mEndUserCircularBuffer.GetTotalDataLength());
6666
return CHIP_NO_ERROR;
6767
}
6868

6969
CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
7070
{
71-
printf("***************************************************************************RETRIEVAL "
72-
"STARTED**********************************************************\n");
7371
CHIP_ERROR err = CHIP_NO_ERROR;
74-
chip::TLV::TLVReader reader;
72+
CircularTLVReader reader;
7573
reader.Init(mEndUserCircularBuffer);
7674

77-
chip::TLV::TLVWriter writer;
75+
TLVWriter writer;
7876
writer.Init(payload);
7977

80-
uint32_t totalBufferSize = 0;
81-
82-
chip::TLV::TLVType outWriterContainer;
83-
err = writer.StartContainer(AnonymousTag(), chip::TLV::TLVType::kTLVType_List, outWriterContainer);
78+
TLVType outWriterContainer;
79+
err = writer.StartContainer(AnonymousTag(), kTLVType_List, outWriterContainer);
8480
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to start container"));
8581

8682
while (true)
@@ -92,35 +88,29 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
9288
break;
9389
}
9490
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
95-
ChipLogError(DeviceLayer, "Failed to read next TLV element: %s", chip::ErrorStr(err)));
91+
ChipLogError(DeviceLayer, "Failed to read next TLV element: %s", ErrorStr(err)));
9692

97-
if (reader.GetType() == chip::TLV::TLVType::kTLVType_Structure && reader.GetTag() == chip::TLV::AnonymousTag())
93+
if (reader.GetType() == kTLVType_Structure && reader.GetTag() == AnonymousTag())
9894
{
99-
chip::TLV::TLVType outerReaderContainer;
95+
TLVType outerReaderContainer;
10096
err = reader.EnterContainer(outerReaderContainer);
10197
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
102-
ChipLogError(DeviceLayer, "Failed to enter outer TLV container: %s", chip::ErrorStr(err)));
98+
ChipLogError(DeviceLayer, "Failed to enter outer TLV container: %s", ErrorStr(err)));
10399

104100
err = reader.Next();
105-
VerifyOrReturnError(
106-
err == CHIP_NO_ERROR, err,
107-
ChipLogError(DeviceLayer, "Failed to read next TLV element in outer container: %s", chip::ErrorStr(err)));
101+
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to read next TLV element in outer container: %s", ErrorStr(err)));
108102

109103
// Check if the current element is a METRIC or TRACE container
110-
if ((reader.GetType() == chip::TLV::kTLVType_Structure) &&
104+
if ((reader.GetType() == kTLVType_Structure) &&
111105
(reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::METRIC) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::TRACE) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::COUNTER)))
112106
{
113-
ESP_LOGW("SIZE", "Total read till now: %ld Total write till now: %ld", reader.GetLengthRead(), writer.GetLengthWritten());
114-
115107
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < writer.GetRemainingFreeLength()) {
116108
err = writer.CopyElement(reader);
117109
if (err == CHIP_ERROR_BUFFER_TOO_SMALL) {
118110
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current element");
119111
break;
120112
}
121113
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to copy TLV element"));
122-
ChipLogProgress(DeviceLayer, "Read metric container successfully");
123-
mEndUserCircularBuffer.EvictHead();
124114
}
125115
else {
126116
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current TLV");
@@ -136,18 +126,12 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
136126
// Exit the outer container
137127
err = reader.ExitContainer(outerReaderContainer);
138128
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
139-
ChipLogError(DeviceLayer, "Failed to exit outer TLV container: %s", chip::ErrorStr(err)));
140-
141-
129+
ChipLogError(DeviceLayer, "Failed to exit outer TLV container: %s", ErrorStr(err)));
142130
}
143131
else
144132
{
145133
ChipLogError(DeviceLayer, "Unexpected TLV element type or tag in outer container");
146134
}
147-
148-
printf("Total Data Length in Buffer: %lu\n Total available length in buffer: %lu\nTotal buffer length: %lu\n",
149-
mEndUserCircularBuffer.DataLength(), mEndUserCircularBuffer.AvailableDataLength(),
150-
mEndUserCircularBuffer.GetTotalDataLength());
151135
}
152136

153137
err = writer.EndContainer(outWriterContainer);
@@ -156,7 +140,7 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
156140
err = writer.Finalize();
157141
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to finalize TLV writing"));
158142
payload.reduce_size(writer.GetLengthWritten());
159-
printf("---------------Total written bytes successfully : %ld----------------\n", writer.GetLengthWritten());
143+
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n", writer.GetLengthWritten());
160144
ChipLogProgress(DeviceLayer, "Retrieval successful");
161145
return CHIP_NO_ERROR;
162146
}
@@ -165,6 +149,6 @@ bool DiagnosticStorageImpl::IsEmptyBuffer()
165149
{
166150
return mEndUserCircularBuffer.DataLength() == 0;
167151
}
168-
152+
} // namespace Diagnostics
169153
} // namespace Tracing
170154
} // namespace chip

src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h

+5-13
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@
2222
#include <lib/support/CHIPMem.h>
2323
#include <lib/core/CHIPError.h>
2424

25-
#define END_USER_BUFFER_SIZE CONFIG_END_USER_BUFFER_SIZE
26-
#define NETWORK_BUFFER_SIZE CONFIG_NETWORK_BUFFER_SIZE
27-
2825
namespace chip {
2926
namespace Tracing {
27+
namespace Diagnostics {
3028
using namespace chip::Platform;
31-
29+
using chip::TLV::TLVType;
3230
class DiagnosticStorageImpl : public DiagnosticStorageInterface
3331
{
3432
public:
3533

36-
static DiagnosticStorageImpl& GetInstance()
37-
{
38-
static DiagnosticStorageImpl instance;
39-
return instance;
40-
}
34+
static DiagnosticStorageImpl& GetInstance(uint8_t * buffer = nullptr, size_t bufferSize = 0);
4135

4236
DiagnosticStorageImpl(const DiagnosticStorageImpl &) = delete;
4337
DiagnosticStorageImpl & operator=(const DiagnosticStorageImpl &) = delete;
@@ -49,14 +43,12 @@ class DiagnosticStorageImpl : public DiagnosticStorageInterface
4943
bool IsEmptyBuffer();
5044

5145
private:
46+
DiagnosticStorageImpl(uint8_t * buffer, size_t bufferSize);
5247
DiagnosticStorageImpl();
5348
~DiagnosticStorageImpl();
5449

5550
TLVCircularBuffer mEndUserCircularBuffer;
56-
TLVCircularBuffer mNetworkCircularBuffer;
57-
uint8_t mEndUserBuffer[END_USER_BUFFER_SIZE];
58-
uint8_t mNetworkBuffer[NETWORK_BUFFER_SIZE];
5951
};
60-
52+
} // namespace Diagnostics
6153
} // namespace Tracing
6254
} // namespace chip

0 commit comments

Comments
 (0)