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
  • Loading branch information
pimpalemahesh committed Nov 18, 2024
1 parent 46217f2 commit 7178050
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 109 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
6 changes: 3 additions & 3 deletions src/tracing/esp32_diagnostic_trace/Counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,7 +61,7 @@ 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"));
}

Expand Down
4 changes: 2 additions & 2 deletions src/tracing/esp32_diagnostic_trace/Counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

#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;

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 Down
28 changes: 15 additions & 13 deletions src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ namespace chip {
namespace Tracing {

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 +42,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 +97,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 +126,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 +138,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
9 changes: 4 additions & 5 deletions src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#pragma once

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

namespace chip {
namespace Tracing {
Expand All @@ -30,15 +30,14 @@ 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 Down
18 changes: 11 additions & 7 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 @@ -149,21 +149,25 @@ void ESP32Diagnostics::TraceCounter(const char * label)
::Diagnostics::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 7178050

Please sign in to comment.