Skip to content

Commit

Permalink
backend: Add description for diagnosticstorage interface, remove unnc…
Browse files Browse the repository at this point in the history
…essary comments, format files, namespace changes
  • Loading branch information
pimpalemahesh committed Nov 19, 2024
1 parent ecb3950 commit 6571c5e
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ size_t LogProvider::GetSizeForIntent(IntentEnum intent)
{
switch (intent)
{
case IntentEnum::kEndUserSupport:
{
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
return DIAGNOSTIC_BUFFER_SIZE;
#else
return static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart);
#endif
}
break;
case IntentEnum::kEndUserSupport: {
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
return DIAGNOSTIC_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 @@ -115,28 +114,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, DIAGNOSTIC_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
#if CONFIG_ENABLE_ESP_DIAGNOSTICS_TRACE
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
MutableByteSpan endUserSupportSpan(endUserBuffer, DIAGNOSTIC_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
4 changes: 2 additions & 2 deletions src/tracing/esp32_diagnostic_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ static_library("backend") {
sources = [
"Counter.cpp",
"Counter.h",
"DiagnosticTracing.cpp",
"DiagnosticTracing.h",
"DiagnosticStorageManager.cpp",
"DiagnosticStorageManager.h",
"DiagnosticTracing.cpp",
"DiagnosticTracing.h",
"Diagnostics.h",
]

Expand Down
12 changes: 7 additions & 5 deletions src/tracing/esp32_diagnostic_trace/Counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include <string.h>
#include <tracing/esp32_diagnostic_trace/Counter.h>

using namespace chip;

namespace chip {
namespace Tracing {
namespace Diagnostics {

// This is a one time allocation for counters. It is not supposed to be freed.
Expand All @@ -45,8 +45,8 @@ ESPDiagnosticCounter * ESPDiagnosticCounter::GetInstance(const char * label)
VerifyOrDie(ptr != nullptr);

ESPDiagnosticCounter * newInstance = new (ptr) ESPDiagnosticCounter(label);
newInstance->mNext = mHead;
mHead = newInstance;
newInstance->mNext = mHead;
mHead = newInstance;

return newInstance;
}
Expand All @@ -61,8 +61,10 @@ void ESPDiagnosticCounter::ReportMetrics()
CHIP_ERROR err = CHIP_NO_ERROR;
Counter counter(label, instanceCount, esp_log_timestamp());
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
err = diagnosticStorage.Store(counter);
err = diagnosticStorage.Store(counter);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));
}

} // namespace Diagnostics
} // namespace Tracing
} // namespace chip
10 changes: 6 additions & 4 deletions src/tracing/esp32_diagnostic_trace/Counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

#pragma once

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

using namespace chip::Tracing::Diagnostics;

namespace chip {
namespace Tracing {
namespace Diagnostics {

/**
Expand All @@ -41,7 +41,7 @@ class ESPDiagnosticCounter
{
private:
static ESPDiagnosticCounter * mHead; // head of the counter list
const char * label; // unique key ,it is used as a static string.
const char * label; // unique key ,it is used as a static string.
int32_t instanceCount;
ESPDiagnosticCounter * mNext; // pointer to point to the next entry in the list

Expand All @@ -56,3 +56,5 @@ class ESPDiagnosticCounter
};

} // namespace Diagnostics
} // namespace Tracing
} // namespace chip
29 changes: 16 additions & 13 deletions src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@

namespace chip {
namespace Tracing {
using namespace chip::TLV;

namespace Diagnostics {
DiagnosticStorageImpl::DiagnosticStorageImpl(uint8_t * buffer, size_t bufferSize)
: mEndUserCircularBuffer(buffer, bufferSize) {}
DiagnosticStorageImpl::DiagnosticStorageImpl(uint8_t * buffer, size_t bufferSize) : mEndUserCircularBuffer(buffer, bufferSize) {}

DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance(uint8_t * buffer, size_t bufferSize) {
DiagnosticStorageImpl & DiagnosticStorageImpl::GetInstance(uint8_t * buffer, size_t bufferSize)
{
static DiagnosticStorageImpl instance(buffer, bufferSize);
return instance;
}
Expand All @@ -42,7 +43,6 @@ CHIP_ERROR DiagnosticStorageImpl::Store(DiagnosticEntry & diagnostic)
CircularTLVWriter writer;
writer.Init(mEndUserCircularBuffer);

// Start a TLV structure container (Anonymous)
TLVType outerContainer;
err = writer.StartContainer(AnonymousTag(), kTLVType_Structure, outerContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
Expand Down Expand Up @@ -98,21 +98,25 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
ChipLogError(DeviceLayer, "Failed to enter outer TLV container: %s", ErrorStr(err)));

err = reader.Next();
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to read next TLV element in outer container: %s", ErrorStr(err)));
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to read next TLV element in outer container: %s", ErrorStr(err)));

// Check if the current element is a METRIC or TRACE container
if ((reader.GetType() == kTLVType_Structure) &&
(reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::METRIC) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::TRACE) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::COUNTER)))
(reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::METRIC) || reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::TRACE) ||
reader.GetTag() == ContextTag(DIAGNOSTICS_TAG::COUNTER)))
{
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < writer.GetRemainingFreeLength()) {
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < writer.GetRemainingFreeLength())
{
err = writer.CopyElement(reader);
if (err == CHIP_ERROR_BUFFER_TOO_SMALL) {
if (err == CHIP_ERROR_BUFFER_TOO_SMALL)
{
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current element");
break;
}
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to copy TLV element"));
}
else {
else
{
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current TLV");
break;
}
Expand All @@ -123,7 +127,6 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)
reader.ExitContainer(outerReaderContainer);
return CHIP_ERROR_WRONG_TLV_TYPE;
}
// Exit the outer container
err = reader.ExitContainer(outerReaderContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to exit outer TLV container: %s", ErrorStr(err)));
Expand All @@ -136,11 +139,11 @@ CHIP_ERROR DiagnosticStorageImpl::Retrieve(MutableByteSpan & payload)

err = writer.EndContainer(outWriterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to close outer container"));
// Finalize the writing process
err = writer.Finalize();
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to finalize TLV writing"));
payload.reduce_size(writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n", writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n",
writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "Retrieval successful");
return CHIP_NO_ERROR;
}
Expand Down
13 changes: 5 additions & 8 deletions src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,23 @@
#pragma once

#include "Diagnostics.h"
#include <lib/support/CHIPMem.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPMem.h>

namespace chip {
namespace Tracing {
namespace Diagnostics {
using namespace chip::Platform;
using chip::TLV::TLVType;
class DiagnosticStorageImpl : public DiagnosticStorageInterface
{
public:
static DiagnosticStorageImpl & GetInstance(uint8_t * buffer = nullptr, size_t bufferSize = 0);

static DiagnosticStorageImpl& GetInstance(uint8_t * buffer = nullptr, size_t bufferSize = 0);

DiagnosticStorageImpl(const DiagnosticStorageImpl &) = delete;
DiagnosticStorageImpl(const DiagnosticStorageImpl &) = delete;
DiagnosticStorageImpl & operator=(const DiagnosticStorageImpl &) = delete;

CHIP_ERROR Store(DiagnosticEntry & diagnostic) override;

CHIP_ERROR Retrieve(MutableByteSpan &payload) override;
CHIP_ERROR Retrieve(MutableByteSpan & payload) override;

bool IsEmptyBuffer();

Expand All @@ -47,7 +44,7 @@ class DiagnosticStorageImpl : public DiagnosticStorageInterface
DiagnosticStorageImpl();
~DiagnosticStorageImpl();

TLVCircularBuffer mEndUserCircularBuffer;
chip::TLV::TLVCircularBuffer mEndUserCircularBuffer;
};
} // namespace Diagnostics
} // namespace Tracing
Expand Down
20 changes: 12 additions & 8 deletions src/tracing/esp32_diagnostic_trace/DiagnosticTracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void ESP32Diagnostics::LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo & info) {}
void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)
{
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err = CHIP_NO_ERROR;
switch (event.ValueType())
{
case ValueType::kInt32: {
Expand Down Expand Up @@ -146,24 +146,28 @@ void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)

void ESP32Diagnostics::TraceCounter(const char * label)
{
::Diagnostics::ESPDiagnosticCounter::GetInstance(label)->ReportMetrics();
ESPDiagnosticCounter::GetInstance(label)->ReportMetrics();
}

void ESP32Diagnostics::TraceBegin(const char * label, const char * group) {
void ESP32Diagnostics::TraceBegin(const char * label, const char * group)
{
StoreDiagnostics(label, group);
}

void ESP32Diagnostics::TraceEnd(const char * label, const char * group) {
void ESP32Diagnostics::TraceEnd(const char * label, const char * group)
{
StoreDiagnostics(label, group);
}

void ESP32Diagnostics::TraceInstant(const char * label, const char * group) {
void ESP32Diagnostics::TraceInstant(const char * label, const char * group)
{
StoreDiagnostics(label, group);
}

void ESP32Diagnostics::StoreDiagnostics(const char* label, const char* group) {
CHIP_ERROR err = CHIP_NO_ERROR;
HashValue hashValue = MurmurHash(group);
void ESP32Diagnostics::StoreDiagnostics(const char * label, const char * group)
{
CHIP_ERROR err = CHIP_NO_ERROR;
HashValue hashValue = MurmurHash(group);
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
if (IsPermitted(hashValue))
{
Expand Down
18 changes: 7 additions & 11 deletions src/tracing/esp32_diagnostic_trace/DiagnosticTracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
* limitations under the License.
*/

#include <esp_log.h>
#include <lib/core/CHIPError.h>
#include <tracing/backend.h>
#include <tracing/metric_event.h>
#include <tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h>
#include <esp_log.h>

#include <tracing/metric_event.h>

#include <memory>
namespace chip {
Expand All @@ -35,14 +34,11 @@ namespace Diagnostics {
class ESP32Diagnostics : public ::chip::Tracing::Backend
{
public:
ESP32Diagnostics(uint8_t *buffer, size_t buffer_size)
{
DiagnosticStorageImpl::GetInstance(buffer, buffer_size);
}
ESP32Diagnostics(uint8_t * buffer, size_t buffer_size) { DiagnosticStorageImpl::GetInstance(buffer, buffer_size); }

// Deleted copy constructor and assignment operator to prevent copying
ESP32Diagnostics(const ESP32Diagnostics&) = delete;
ESP32Diagnostics& operator=(const ESP32Diagnostics&) = delete;
ESP32Diagnostics(const ESP32Diagnostics &) = delete;
ESP32Diagnostics & operator=(const ESP32Diagnostics &) = delete;

void TraceBegin(const char * label, const char * group) override;

Expand All @@ -60,12 +56,12 @@ class ESP32Diagnostics : public ::chip::Tracing::Backend
void LogNodeDiscovered(NodeDiscoveredInfo &) override;
void LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo &) override;
void LogMetricEvent(const MetricEvent &) override;
void StoreDiagnostics(const char* label, const char* group);
void StoreDiagnostics(const char * label, const char * group);

private:
using ValueType = MetricEvent::Value::Type;
};

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

0 comments on commit 6571c5e

Please sign in to comment.