Skip to content

Commit

Permalink
diagnostic_storage: unify diagnostic entries into a single type
Browse files Browse the repository at this point in the history
  • Loading branch information
pimpalemahesh committed Dec 13, 2024
1 parent 08a4b12 commit 8512a1f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 209 deletions.
2 changes: 1 addition & 1 deletion src/tracing/esp32_diagnostic_trace/Counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ uint32_t ESPDiagnosticCounter::GetInstanceCount(const char * label) const
void ESPDiagnosticCounter::ReportMetrics(const char * label, DiagnosticStorageInterface & mStorageInstance)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Counter counter(label, GetInstanceCount(label), esp_log_timestamp());
Diagnostic<uint32_t> counter(label, GetInstanceCount(label), esp_log_timestamp());
err = mStorageInstance.Store(counter);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));
}
Expand Down
57 changes: 13 additions & 44 deletions src/tracing/esp32_diagnostic_trace/DiagnosticStorageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <lib/support/CHIPMem.h>
#include <tracing/esp32_diagnostic_trace/Diagnostics.h>

#define TLV_CLOSING_BYTES 4
#define TLV_CLOSING_BYTE 1

namespace chip {
namespace Tracing {
Expand Down Expand Up @@ -65,9 +65,8 @@ class CircularDiagnosticBuffer : public chip::TLV::TLVCircularBuffer, public Dia
chip::TLV::TLVWriter writer;
writer.Init(span.data(), span.size());

chip::TLV::TLVType outWriterContainer;
err = writer.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_List, outWriterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to start container"));
chip::TLV::TLVType outWriterContainer = chip::TLV::kTLVType_NotSpecified;
ReturnErrorOnFailure(writer.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_List, outWriterContainer));

while (CHIP_NO_ERROR == reader.Next())
{
Expand All @@ -76,62 +75,32 @@ class CircularDiagnosticBuffer : public chip::TLV::TLVCircularBuffer, public Dia

if (reader.GetType() == chip::TLV::kTLVType_Structure && reader.GetTag() == chip::TLV::AnonymousTag())
{
chip::TLV::TLVType outerReaderContainer;
err = reader.EnterContainer(outerReaderContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
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)));

if ((reader.GetType() == chip::TLV::kTLVType_Structure) &&
(reader.GetTag() == chip::TLV::ContextTag(DIAGNOSTICS_TAG::METRIC) ||
reader.GetTag() == chip::TLV::ContextTag(DIAGNOSTICS_TAG::TRACE) ||
reader.GetTag() == chip::TLV::ContextTag(DIAGNOSTICS_TAG::COUNTER)))
if ((reader.GetLengthRead() - writer.GetLengthWritten()) < ((writer.GetRemainingFreeLength() + TLV_CLOSING_BYTE)))
{
if ((reader.GetLengthRead() - writer.GetLengthWritten()) <
((writer.GetRemainingFreeLength() + TLV_CLOSING_BYTES)))
err = writer.CopyElement(reader);
if (err == CHIP_ERROR_BUFFER_TOO_SMALL)
{
err = writer.CopyElement(reader);
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
{
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current TLV");
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current element");
break;
}
}
else
{
ChipLogError(DeviceLayer, "Unexpected TLV element in outer container");
reader.ExitContainer(outerReaderContainer);
return CHIP_ERROR_WRONG_TLV_TYPE;
ChipLogProgress(DeviceLayer, "Buffer too small to occupy current TLV");
break;
}
err = reader.ExitContainer(outerReaderContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to exit outer TLV container: %s", ErrorStr(err)));
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to copy TLV element"));
}
else
{
ChipLogError(DeviceLayer, "Unexpected TLV element type or tag in outer container");
}
}

err = writer.EndContainer(outWriterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to close outer container"));
err = writer.Finalize();
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to finalize TLV writing"));
ReturnErrorOnFailure(writer.EndContainer(outWriterContainer));
ReturnErrorOnFailure(writer.Finalize());
span.reduce_size(writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "---------------Total written bytes successfully : %ld----------------\n",
writer.GetLengthWritten());
ChipLogProgress(DeviceLayer, "Retrieval successful");
ChipLogProgress(DeviceLayer, "---------------Total Retrieved bytes : %ld----------------\n", writer.GetLengthWritten());
return CHIP_NO_ERROR;
}

Expand Down
6 changes: 3 additions & 3 deletions src/tracing/esp32_diagnostic_trace/DiagnosticTracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)
{
case ValueType::kInt32: {
ESP_LOGI("mtr", "The value of %s is %ld ", event.key(), event.ValueInt32());
Metric<int32_t> metric(event.key(), event.ValueInt32(), esp_log_timestamp());
Diagnostic<int32_t> metric(event.key(), event.ValueInt32(), esp_log_timestamp());
err = mStorageInstance.Store(metric);
}
break;

case ValueType::kUInt32: {
ESP_LOGI("mtr", "The value of %s is %lu ", event.key(), event.ValueUInt32());
Metric<uint32_t> metric(event.key(), event.ValueUInt32(), esp_log_timestamp());
Diagnostic<uint32_t> metric(event.key(), event.ValueUInt32(), esp_log_timestamp());
err = mStorageInstance.Store(metric);
}
break;
Expand Down Expand Up @@ -169,7 +169,7 @@ void ESP32Diagnostics::StoreDiagnostics(const char * label, const char * group)
HashValue hashValue = MurmurHash(group);
if (IsPermitted(hashValue))
{
Trace trace(label, group, esp_log_timestamp());
Diagnostic<const char *> trace(label, group, esp_log_timestamp());
err = mStorageInstance.Store(trace);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Trace Diagnostic data"));
}
Expand Down
200 changes: 39 additions & 161 deletions src/tracing/esp32_diagnostic_trace/Diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,72 +28,63 @@ namespace Diagnostics {

enum class DIAGNOSTICS_TAG
{
METRIC = 0,
TRACE = 1,
COUNTER = 2,
LABEL = 3,
GROUP = 4,
VALUE = 5,
TIMESTAMP = 6
LABEL = 0,
VALUE,
TIMESTAMP
};

/**
* @class DiagnosticEntry
* @brief Abstract base class for encoding diagnostic entries into TLV format.
*
*/
class DiagnosticEntry
{
public:
virtual ~DiagnosticEntry() = default;
/**
* @brief Virtual destructor for proper cleanup in derived classes.
*/
virtual ~DiagnosticEntry() = default;

/**
* @brief Pure virtual method to encode diagnostic data into a TLV structure.
*
* @param writer A reference to the `chip::TLV::CircularTLVWriter` instance
* used to encode the TLV data.
* @return CHIP_ERROR Returns an error code indicating the success or
* failure of the encoding operation.
*/
virtual CHIP_ERROR Encode(chip::TLV::CircularTLVWriter & writer) = 0;
};

template <typename T>
class Metric : public DiagnosticEntry
class Diagnostic : public DiagnosticEntry
{
public:
Metric(const char * label, T value, uint32_t timestamp) : label_(label), value_(value), timestamp_(timestamp) {}

Metric() {}

const char * GetLabel() const { return label_; }
T GetValue() const { return value_; }
uint32_t GetTimestamp() const { return timestamp_; }
Diagnostic(const char * label, T value, uint32_t timestamp) : label_(label), value_(value), timestamp_(timestamp) {}

CHIP_ERROR Encode(chip::TLV::CircularTLVWriter & writer) override
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::TLV::TLVType metricOuterContainer;
err = writer.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, metricOuterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to Start outer metric container: %s", ErrorStr(err)));
chip::TLV::TLVType metricContainer;
err = writer.StartContainer(chip::TLV::ContextTag(DIAGNOSTICS_TAG::METRIC), chip::TLV::kTLVType_Structure, metricContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to Start inner metric container: %s", ErrorStr(err)));

chip::TLV::TLVType DiagnosticOuterContainer = chip::TLV::kTLVType_NotSpecified;
ReturnErrorOnFailure(
writer.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, DiagnosticOuterContainer));
// TIMESTAMP
err = writer.Put(chip::TLV::ContextTag(DIAGNOSTICS_TAG::TIMESTAMP), timestamp_);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to write TIMESTAMP for METRIC : %s", ErrorStr(err)));

ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(DIAGNOSTICS_TAG::TIMESTAMP), timestamp_));
// LABEL
err = writer.PutString(chip::TLV::ContextTag(DIAGNOSTICS_TAG::LABEL), label_);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to write LABEL for METRIC : %s", ErrorStr(err)));

ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DIAGNOSTICS_TAG::LABEL), label_));
// VALUE
err = writer.Put(chip::TLV::ContextTag(DIAGNOSTICS_TAG::VALUE), value_);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to write VALUE for METRIC : %s", ErrorStr(err)));

ChipLogProgress(DeviceLayer, "Metric Value written to storage successfully. label: %s\n", label_);
err = writer.EndContainer(metricContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to end inner TLV container for Metric : %s", ErrorStr(err)));
err = writer.EndContainer(metricOuterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to end outer TLV container for Metric : %s", ErrorStr(err)));
err = writer.Finalize();
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to Finalize writer : %s", ErrorStr(err)));
if constexpr (std::is_same_v<T, const char *>)
{
ReturnErrorOnFailure(writer.PutString(chip::TLV::ContextTag(DIAGNOSTICS_TAG::VALUE), value_));
}
else
{
ReturnErrorOnFailure(writer.Put(chip::TLV::ContextTag(DIAGNOSTICS_TAG::VALUE), value_));
}
ReturnErrorOnFailure(writer.EndContainer(DiagnosticOuterContainer));
ReturnErrorOnFailure(writer.Finalize());
return err;
ChipLogProgress(DeviceLayer, "Diagnostic Value written to storage successfully. label: %s\n", label_);
return CHIP_NO_ERROR;
}

private:
Expand All @@ -102,119 +93,6 @@ class Metric : public DiagnosticEntry
uint32_t timestamp_;
};

class Trace : public DiagnosticEntry
{
public:
Trace(const char * label, const char * group, uint32_t timestamp) : label_(label), group_(group), timestamp_(timestamp) {}

Trace() {}

const char * GetLabel() const { return label_; }
uint32_t GetTimestamp() const { return timestamp_; }
const char * GetGroup() const { return group_; }

CHIP_ERROR Encode(chip::TLV::CircularTLVWriter & writer) override
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::TLV::TLVType traceOuterContainer;
err = writer.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, traceOuterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to Start outer trace container: %s", ErrorStr(err)));
chip::TLV::TLVType traceContainer;
err = writer.StartContainer(chip::TLV::ContextTag(DIAGNOSTICS_TAG::TRACE), chip::TLV::kTLVType_Structure, traceContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to Start inner trace container: %s", ErrorStr(err)));
// TIMESTAMP
err = writer.Put(chip::TLV::ContextTag(DIAGNOSTICS_TAG::TIMESTAMP), timestamp_);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to write TIMESTAMP for TRACE : %s", ErrorStr(err)));

// GROUP
err = writer.PutString(chip::TLV::ContextTag(DIAGNOSTICS_TAG::GROUP), group_);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to write GROUP for TRACE : %s", ErrorStr(err)));

// LABEL
err = writer.PutString(chip::TLV::ContextTag(DIAGNOSTICS_TAG::LABEL), label_);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to write LABEL for TRACE : %s", ErrorStr(err)));

ChipLogProgress(DeviceLayer, "Trace Value written to storage successfully. label: %s value: %s\n", label_, group_);
err = writer.EndContainer(traceContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to end inner TLV container for Trace : %s", ErrorStr(err)));
err = writer.EndContainer(traceOuterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to end outer TLV container for Trace : %s", ErrorStr(err)));
err = writer.Finalize();
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to Finalize writer : %s", ErrorStr(err)));
ReturnErrorOnFailure(writer.Finalize());
return err;
}

private:
const char * label_;
const char * group_;
uint32_t timestamp_;
};

class Counter : public DiagnosticEntry
{
public:
Counter(const char * label, uint32_t count, uint32_t timestamp) : label_(label), count_(count), timestamp_(timestamp) {}

Counter() {}

uint32_t GetCount() const { return count_; }

uint32_t GetTimestamp() const { return timestamp_; }

CHIP_ERROR Encode(chip::TLV::CircularTLVWriter & writer) override
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::TLV::TLVType counterOuterContainer;
err = writer.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, counterOuterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to Start outer counter container: %s", ErrorStr(err)));
chip::TLV::TLVType counterContainer;
err =
writer.StartContainer(chip::TLV::ContextTag(DIAGNOSTICS_TAG::COUNTER), chip::TLV::kTLVType_Structure, counterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to Start inner counter container: %s", ErrorStr(err)));

// TIMESTAMP
err = writer.Put(chip::TLV::ContextTag(DIAGNOSTICS_TAG::TIMESTAMP), timestamp_);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to write TIMESTAMP for COUNTER : %s", ErrorStr(err)));

// LABEL
err = writer.PutString(chip::TLV::ContextTag(DIAGNOSTICS_TAG::LABEL), label_);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to write LABEL for COUNTER : %s", ErrorStr(err)));

// COUNT
err = writer.Put(chip::TLV::ContextTag(DIAGNOSTICS_TAG::COUNTER), count_);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to write VALUE for COUNTER : %s", ErrorStr(err)));

ChipLogProgress(DeviceLayer, "Counter Value written to storage successfully label: %s count: %ld\n", label_, count_);
err = writer.EndContainer(counterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to end inner TLV container for Counter : %s", ErrorStr(err)));
err = writer.EndContainer(counterOuterContainer);
VerifyOrReturnError(err == CHIP_NO_ERROR, err,
ChipLogError(DeviceLayer, "Failed to end outer TLV container for Counter : %s", ErrorStr(err)));
err = writer.Finalize();
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to Finalize writer : %s", ErrorStr(err)));
return err;
}

private:
const char * label_;
uint32_t count_;
uint32_t timestamp_;
};

/**
* @brief Interface for storing and retrieving diagnostic data.
*/
Expand Down

0 comments on commit 8512a1f

Please sign in to comment.