@@ -23,43 +23,29 @@ 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 )
30
+ if (mCounterList .find (label) != mCounterList .end ())
34
31
{
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 ;
41
37
}
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
38
}
53
39
54
- int32_t ESPDiagnosticCounter::GetInstanceCount () const
40
+ int32_t ESPDiagnosticCounter::GetInstanceCount (const char * label ) const
55
41
{
56
- return instanceCount ;
42
+ return mCounterList [label] ;
57
43
}
58
44
59
- void ESPDiagnosticCounter::ReportMetrics ()
45
+ void ESPDiagnosticCounter::ReportMetrics (const char * label )
60
46
{
61
47
CHIP_ERROR err = CHIP_NO_ERROR;
62
- Counter counter (label, instanceCount , esp_log_timestamp ());
48
+ Counter counter (label, GetInstanceCount (label) , esp_log_timestamp ());
63
49
DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance ();
64
50
err = diagnosticStorage.Store (counter);
65
51
VerifyOrReturn (err == CHIP_NO_ERROR, ChipLogError (DeviceLayer, " Failed to store Counter diagnostic data" ));
0 commit comments