Skip to content

Commit

Permalink
backend: Replace linkedlist with map for counter diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
pimpalemahesh committed Nov 21, 2024
1 parent d1737c6 commit 9e148bb
Showing 3 changed files with 25 additions and 38 deletions.
38 changes: 11 additions & 27 deletions src/tracing/esp32_diagnostic_trace/Counter.cpp
Original file line number Diff line number Diff line change
@@ -23,43 +23,27 @@ namespace chip {
namespace Tracing {
namespace Diagnostics {

// This is a one time allocation for counters. It is not supposed to be freed.
ESPDiagnosticCounter * ESPDiagnosticCounter::mHead = nullptr;
std::map<const char *, uint32_t> ESPDiagnosticCounter::mCounterList;

ESPDiagnosticCounter * ESPDiagnosticCounter::GetInstance(const char * label)
void ESPDiagnosticCounter::CountInit(const char* label)
{
ESPDiagnosticCounter * current = mHead;

while (current != nullptr)
{
if (strcmp(current->label, label) == 0)
{
current->instanceCount++;
return current;
}
current = current->mNext;
if (mCounterList.find(label) != mCounterList.end()) {
mCounterList[label]++;
}
else {
mCounterList[label] = 1;
}

// Allocate a new instance if counter is not present in the list.
void * ptr = Platform::MemoryAlloc(sizeof(ESPDiagnosticCounter));
VerifyOrDie(ptr != nullptr);

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

return newInstance;
}

int32_t ESPDiagnosticCounter::GetInstanceCount() const
int32_t ESPDiagnosticCounter::GetInstanceCount(const char* label) const
{
return instanceCount;
return mCounterList[label];
}

void ESPDiagnosticCounter::ReportMetrics()
void ESPDiagnosticCounter::ReportMetrics(const char* label)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Counter counter(label, instanceCount, esp_log_timestamp());
Counter counter(label, GetInstanceCount(label), esp_log_timestamp());
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
err = diagnosticStorage.Store(counter);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));
23 changes: 13 additions & 10 deletions src/tracing/esp32_diagnostic_trace/Counter.h
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPMemString.h>
#include <string.h>
#include <map>

namespace chip {
namespace Tracing {
@@ -39,20 +40,22 @@ namespace Diagnostics {

class ESPDiagnosticCounter
{
private:
static ESPDiagnosticCounter * mHead; // head of the counter list
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
public:
static ESPDiagnosticCounter & GetInstance(const char* label) {
static ESPDiagnosticCounter instance;
CountInit(label);
return instance;
}

ESPDiagnosticCounter(const char * labelParam) : label(labelParam), instanceCount(1), mNext(nullptr) {}
int32_t GetInstanceCount(const char *label) const;

public:
static ESPDiagnosticCounter * GetInstance(const char * label);
void ReportMetrics(const char* label);

int32_t GetInstanceCount() const;
private:
ESPDiagnosticCounter() {}

void ReportMetrics();
static std::map<const char*, uint32_t> mCounterList;
static void CountInit(const char* label);
};

} // namespace Diagnostics
2 changes: 1 addition & 1 deletion src/tracing/esp32_diagnostic_trace/DiagnosticTracing.cpp
Original file line number Diff line number Diff line change
@@ -146,7 +146,7 @@ void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)

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

void ESP32Diagnostics::TraceBegin(const char * label, const char * group)

0 comments on commit 9e148bb

Please sign in to comment.