Skip to content

Commit a98b1f8

Browse files
committed
backend: Replace linkedlist with map for counter diagnostics
1 parent 168a5ac commit a98b1f8

File tree

4 files changed

+30
-37
lines changed

4 files changed

+30
-37
lines changed

scripts/tools/check_includes_config.py

+3
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@
167167
'src/tracing/json/json_tracing.cpp': {'string', 'sstream'},
168168
'src/tracing/json/json_tracing.h': {'fstream', 'unordered_map', 'string'},
169169

170+
# esp32 diagnostic tracing
171+
'src/tracing/esp32_diagnostic_trace/Counter.h': {'map'},
172+
170173
# esp32 tracing
171174
'src/tracing/esp32_trace/esp32_tracing.h': {'unordered_map'},
172175

src/tracing/esp32_diagnostic_trace/Counter.cpp

+12-26
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,29 @@ namespace chip {
2323
namespace Tracing {
2424
namespace Diagnostics {
2525

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

29-
ESPDiagnosticCounter * ESPDiagnosticCounter::GetInstance(const char * label)
28+
void ESPDiagnosticCounter::CountInit(const char * label)
3029
{
31-
ESPDiagnosticCounter * current = mHead;
32-
33-
while (current != nullptr)
30+
if (mCounterList.find(label) != mCounterList.end())
3431
{
35-
if (strcmp(current->label, label) == 0)
36-
{
37-
current->instanceCount++;
38-
return current;
39-
}
40-
current = current->mNext;
32+
mCounterList[label]++;
33+
}
34+
else
35+
{
36+
mCounterList[label] = 1;
4137
}
42-
43-
// Allocate a new instance if counter is not present in the list.
44-
void * ptr = Platform::MemoryAlloc(sizeof(ESPDiagnosticCounter));
45-
VerifyOrDie(ptr != nullptr);
46-
47-
ESPDiagnosticCounter * newInstance = new (ptr) ESPDiagnosticCounter(label);
48-
newInstance->mNext = mHead;
49-
mHead = newInstance;
50-
51-
return newInstance;
5238
}
5339

54-
int32_t ESPDiagnosticCounter::GetInstanceCount() const
40+
int32_t ESPDiagnosticCounter::GetInstanceCount(const char * label) const
5541
{
56-
return instanceCount;
42+
return mCounterList[label];
5743
}
5844

59-
void ESPDiagnosticCounter::ReportMetrics()
45+
void ESPDiagnosticCounter::ReportMetrics(const char * label)
6046
{
6147
CHIP_ERROR err = CHIP_NO_ERROR;
62-
Counter counter(label, instanceCount, esp_log_timestamp());
48+
Counter counter(label, GetInstanceCount(label), esp_log_timestamp());
6349
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance();
6450
err = diagnosticStorage.Store(counter);
6551
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data"));

src/tracing/esp32_diagnostic_trace/Counter.h

+14-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <esp_log.h>
2424
#include <lib/support/CHIPMem.h>
2525
#include <lib/support/CHIPMemString.h>
26+
#include <map>
2627
#include <string.h>
2728

2829
namespace chip {
@@ -39,20 +40,23 @@ namespace Diagnostics {
3940

4041
class ESPDiagnosticCounter
4142
{
42-
private:
43-
static ESPDiagnosticCounter * mHead; // head of the counter list
44-
const char * label; // unique key ,it is used as a static string.
45-
int32_t instanceCount;
46-
ESPDiagnosticCounter * mNext; // pointer to point to the next entry in the list
43+
public:
44+
static ESPDiagnosticCounter & GetInstance(const char * label)
45+
{
46+
static ESPDiagnosticCounter instance;
47+
CountInit(label);
48+
return instance;
49+
}
4750

48-
ESPDiagnosticCounter(const char * labelParam) : label(labelParam), instanceCount(1), mNext(nullptr) {}
51+
int32_t GetInstanceCount(const char * label) const;
4952

50-
public:
51-
static ESPDiagnosticCounter * GetInstance(const char * label);
53+
void ReportMetrics(const char * label);
5254

53-
int32_t GetInstanceCount() const;
55+
private:
56+
ESPDiagnosticCounter() {}
5457

55-
void ReportMetrics();
58+
static std::map<const char *, uint32_t> mCounterList;
59+
static void CountInit(const char * label);
5660
};
5761

5862
} // namespace Diagnostics

src/tracing/esp32_diagnostic_trace/DiagnosticTracing.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void ESP32Diagnostics::LogMetricEvent(const MetricEvent & event)
146146

147147
void ESP32Diagnostics::TraceCounter(const char * label)
148148
{
149-
ESPDiagnosticCounter::GetInstance(label)->ReportMetrics();
149+
ESPDiagnosticCounter::GetInstance(label).ReportMetrics(label);
150150
}
151151

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

0 commit comments

Comments
 (0)