Skip to content

Commit bb27419

Browse files
shripad621giterwinpan1
authored andcommitted
[ESP32] Tracing : Added the fix of data type mismatch while registering the metrics in esp32 tracing framework. (project-chip#32355)
* Fixed the type mismatch in the esp32 tracing framework * Removed the heap metrics as insights framework maps traces to heap metrics * addressed review comments * restlyed * Added a check to ensure one type associated with one key * Addressed the further review comments
1 parent 724fb33 commit bb27419

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

scripts/tools/check_includes_config.py

+3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@
160160
'src/tracing/json/json_tracing.cpp': {'string', 'sstream'},
161161
'src/tracing/json/json_tracing.h': {'fstream', 'unordered_map'},
162162

163+
# esp32 tracing
164+
'src/tracing/esp32_trace/esp32_tracing.h': {'unordered_map'},
165+
163166
# Not intended for embedded clients
164167
'src/app/PendingResponseTrackerImpl.h': {'unordered_set'},
165168

src/tracing/esp32_trace/esp32_tracing.cpp

+46-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include <algorithm>
20+
#include <esp_err.h>
2021
#include <esp_heap_caps.h>
2122
#include <esp_insights.h>
2223
#include <esp_log.h>
@@ -134,10 +135,7 @@ void RemoveHashFromPermitlist(const char * str)
134135
#define LOG_HEAP_INFO(label, group, entry_exit) \
135136
do \
136137
{ \
137-
ESP_DIAG_EVENT("MTR_TRC", "%s - %s - %s Min Free heap - %u - LFB - %u Start free heap - %u", entry_exit, label, group, \
138-
heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT), \
139-
heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT), \
140-
heap_caps_get_free_size(MALLOC_CAP_8BIT)); \
138+
ESP_DIAG_EVENT("MTR_TRC", "%s - %s - %s", entry_exit, label, group); \
141139
} while (0)
142140

143141
void ESP32Backend::LogMessageReceived(MessageReceivedInfo & info) {}
@@ -155,34 +153,71 @@ void ESP32Backend::TraceCounter(const char * label)
155153
::Insights::ESPInsightsCounter::GetInstance(label)->ReportMetrics();
156154
}
157155

156+
void ESP32Backend::RegisterMetric(const char * key, ValueType type)
157+
{
158+
// Check for the same key will not have two different types.
159+
if (mRegisteredMetrics.find(key) != mRegisteredMetrics.end())
160+
{
161+
if (mRegisteredMetrics[key] != type)
162+
{
163+
ESP_LOGE("SYS.MTR", "Type mismatch for metric key %s", key);
164+
return;
165+
}
166+
}
167+
168+
switch (type)
169+
{
170+
case ValueType::kUInt32:
171+
esp_diag_metrics_register("SYS_MTR" /*Tag of metrics */, key /* Unique key 8 */, key /* label displayed on dashboard */,
172+
"insights.mtr" /* hierarchical path */, ESP_DIAG_DATA_TYPE_UINT /* data_type */);
173+
break;
174+
175+
case ValueType::kInt32:
176+
esp_diag_metrics_register("SYS_MTR" /*Tag of metrics */, key /* Unique key 8 */, key /* label displayed on dashboard */,
177+
"insights.mtr" /* hierarchical path */, ESP_DIAG_DATA_TYPE_INT /* data_type */);
178+
break;
179+
180+
case ValueType::kChipErrorCode:
181+
esp_diag_metrics_register("SYS_MTR" /*Tag of metrics */, key /* Unique key 8 */, key /* label displayed on dashboard */,
182+
"insights.mtr" /* hierarchical path */, ESP_DIAG_DATA_TYPE_UINT /* data_type */);
183+
break;
184+
185+
case ValueType::kUndefined:
186+
ESP_LOGE("mtr", "failed to register %s as its value is undefined", key);
187+
break;
188+
}
189+
190+
mRegisteredMetrics[key] = type;
191+
}
192+
158193
void ESP32Backend::LogMetricEvent(const MetricEvent & event)
159194
{
160-
if (!mRegistered)
195+
if (mRegisteredMetrics.find(event.key()) == mRegisteredMetrics.end())
161196
{
162-
esp_diag_metrics_register("SYS_MTR" /*Tag of metrics */, event.key() /* Unique key 8 */,
163-
event.key() /* label displayed on dashboard */, "insights.mtr" /* hierarchical path */,
164-
ESP_DIAG_DATA_TYPE_INT /* data_type */);
165-
mRegistered = true;
197+
RegisterMetric(event.key(), event.ValueType());
166198
}
167199

168-
using ValueType = MetricEvent::Value::Type;
169200
switch (event.ValueType())
170201
{
171202
case ValueType::kInt32:
172203
ESP_LOGI("mtr", "The value of %s is %ld ", event.key(), event.ValueInt32());
173204
esp_diag_metrics_add_int(event.key(), event.ValueInt32());
174205
break;
206+
175207
case ValueType::kUInt32:
176208
ESP_LOGI("mtr", "The value of %s is %lu ", event.key(), event.ValueUInt32());
177209
esp_diag_metrics_add_uint(event.key(), event.ValueUInt32());
178210
break;
211+
179212
case ValueType::kChipErrorCode:
180213
ESP_LOGI("mtr", "The value of %s is error with code %lu ", event.key(), event.ValueErrorCode());
181214
esp_diag_metrics_add_uint(event.key(), event.ValueErrorCode());
182215
break;
216+
183217
case ValueType::kUndefined:
184218
ESP_LOGI("mtr", "The value of %s is undefined", event.key());
185219
break;
220+
186221
default:
187222
ESP_LOGI("mtr", "The value of %s is of an UNKNOWN TYPE", event.key());
188223
break;
@@ -211,6 +246,7 @@ void ESP32Backend::TraceInstant(const char * label, const char * group)
211246
{
212247
ESP_DIAG_EVENT("MTR_TRC", "Instant : %s -%s", label, group);
213248
}
249+
214250
} // namespace Insights
215251
} // namespace Tracing
216252
} // namespace chip

src/tracing/esp32_trace/esp32_tracing.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <lib/core/CHIPError.h>
22
#include <tracing/backend.h>
3+
#include <tracing/metric_event.h>
4+
#include <unordered_map>
35

46
#include <memory>
57
namespace chip {
@@ -39,7 +41,9 @@ class ESP32Backend : public ::chip::Tracing::Backend
3941
void LogMetricEvent(const MetricEvent &) override;
4042

4143
private:
42-
bool mRegistered = false;
44+
using ValueType = MetricEvent::Value::Type;
45+
std::unordered_map<const char *, ValueType> mRegisteredMetrics;
46+
void RegisterMetric(const char * key, ValueType type);
4347
};
4448

4549
} // namespace Insights

0 commit comments

Comments
 (0)