@@ -23,43 +23,27 @@ namespace chip {
23
23
namespace Tracing {
24
24
namespace Diagnostics {
25
25
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 ;
28
27
29
- ESPDiagnosticCounter * ESPDiagnosticCounter::GetInstance (const char * label)
28
+ void ESPDiagnosticCounter::CountInit (const char * label)
30
29
{
31
- ESPDiagnosticCounter * current = mHead ;
32
-
33
- while (current != nullptr )
34
- {
35
- if (strcmp (current->label , label) == 0 )
36
- {
37
- current->instanceCount ++;
38
- return current;
39
- }
40
- current = current->mNext ;
30
+ if (mCounterList .find (label) != mCounterList .end ()) {
31
+ mCounterList [label]++;
32
+ }
33
+ else {
34
+ mCounterList [label] = 1 ;
41
35
}
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;
52
36
}
53
37
54
- int32_t ESPDiagnosticCounter::GetInstanceCount () const
38
+ int32_t ESPDiagnosticCounter::GetInstanceCount (const char * label ) const
55
39
{
56
- return instanceCount ;
40
+ return mCounterList [label] ;
57
41
}
58
42
59
- void ESPDiagnosticCounter::ReportMetrics ()
43
+ void ESPDiagnosticCounter::ReportMetrics (const char * label )
60
44
{
61
45
CHIP_ERROR err = CHIP_NO_ERROR;
62
- Counter counter (label, instanceCount , esp_log_timestamp ());
46
+ Counter counter (label, GetInstanceCount (label) , esp_log_timestamp ());
63
47
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance ();
64
48
err = diagnosticStorage.Store (counter);
65
49
VerifyOrReturn (err == CHIP_NO_ERROR, ChipLogError (DeviceLayer, " Failed to store Counter diagnostic data" ));
0 commit comments