From 23865202e38ddd518f3ac8d003494f911d850a69 Mon Sep 17 00:00:00 2001 From: shripad621git <79364691+shripad621git@users.noreply.github.com> Date: Mon, 12 Feb 2024 18:36:13 +0530 Subject: [PATCH] tracing: Added a macro to report the metric values from the sdk to the tracing framework. (#32032) * Added metric support * Addressed the review comments --- .../wifi-network-diagnostics-server.cpp | 1 + src/tracing/backend.h | 1 + src/tracing/esp32_trace/esp32_tracing.cpp | 13 +++++++++++++ src/tracing/esp32_trace/esp32_tracing.h | 4 ++++ .../include/matter/tracing/macros_impl.h | 1 + src/tracing/json/json_tracing.cpp | 9 +++++++++ src/tracing/json/json_tracing.h | 1 + src/tracing/macros.h | 9 +++++++++ .../include/matter/tracing/macros_impl.h | 1 + .../none/include/matter/tracing/macros_impl.h | 1 + .../perfetto/include/matter/tracing/macros_impl.h | 2 ++ src/tracing/registry.cpp | 8 ++++++++ src/tracing/registry.h | 1 + 13 files changed, 52 insertions(+) diff --git a/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp b/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp index bd02f36d731cf2..f116def50c089a 100644 --- a/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp +++ b/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp @@ -163,6 +163,7 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::ReadWiFiRssi(AttributeValueEncoder & aEncod { rssi.SetNonNull(value); ChipLogProgress(Zcl, "The current RSSI of the Node’s Wi-Fi radio in dB: %d", value); + MATTER_TRACE_METRIC("wifi_rssi", value); } else { diff --git a/src/tracing/backend.h b/src/tracing/backend.h index 3d8bec8ada501f..c57e97ddd27638 100644 --- a/src/tracing/backend.h +++ b/src/tracing/backend.h @@ -64,6 +64,7 @@ class Backend : public ::chip::IntrusiveListNodeBase<> virtual void TraceInstant(const char * label, const char * group) {} virtual void TraceCounter(const char * label) {} + virtual void TraceMetric(const char * label, int32_t value) {} virtual void LogMessageSend(MessageSendInfo &) { TraceInstant("MessageSent", "Messaging"); } virtual void LogMessageReceived(MessageReceivedInfo &) { TraceInstant("MessageReceived", "Messaging"); } diff --git a/src/tracing/esp32_trace/esp32_tracing.cpp b/src/tracing/esp32_trace/esp32_tracing.cpp index 7937772606eec0..6193587be49c7d 100644 --- a/src/tracing/esp32_trace/esp32_tracing.cpp +++ b/src/tracing/esp32_trace/esp32_tracing.cpp @@ -153,6 +153,19 @@ void ESP32Backend::TraceCounter(const char * label) { ::Insights::ESPInsightsCounter::GetInstance(label)->ReportMetrics(); } + +void ESP32Backend::TraceMetric(const char * label, int32_t value) +{ + if (!mRegistered) + { + esp_diag_metrics_register("SYS_MTR" /*Tag of metrics */, label /* Unique key 8 */, label /* label displayed on dashboard */, + "insights.mtr" /* hierarchical path */, ESP_DIAG_DATA_TYPE_INT /* data_type */); + mRegistered = true; + } + ESP_LOGI("mtr", "The value of %s is %ld ", label, value); + esp_diag_metrics_add_int(label, value); +} + void ESP32Backend::TraceBegin(const char * label, const char * group) { HashValue hashValue = MurmurHash(group); diff --git a/src/tracing/esp32_trace/esp32_tracing.h b/src/tracing/esp32_trace/esp32_tracing.h index 6d9a277e06561d..3eb127c1d3367f 100644 --- a/src/tracing/esp32_trace/esp32_tracing.h +++ b/src/tracing/esp32_trace/esp32_tracing.h @@ -29,6 +29,7 @@ class ESP32Backend : public ::chip::Tracing::Backend void TraceInstant(const char * label, const char * group) override; void TraceCounter(const char * label) override; + void TraceMetric(const char * label, int32_t value) override; void LogMessageSend(MessageSendInfo &) override; void LogMessageReceived(MessageReceivedInfo &) override; @@ -36,6 +37,9 @@ class ESP32Backend : public ::chip::Tracing::Backend void LogNodeLookup(NodeLookupInfo &) override; void LogNodeDiscovered(NodeDiscoveredInfo &) override; void LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo &) override; + +private: + bool mRegistered = false; }; } // namespace Insights diff --git a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h index b95c20754237a1..8ebde173d8297b 100644 --- a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h +++ b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h @@ -29,6 +29,7 @@ #define MATTER_TRACE_END(label, group) ::chip::Tracing::Internal::End(label, group) #define MATTER_TRACE_INSTANT(label, group) ::chip::Tracing::Internal::Instant(label, group) #define MATTER_TRACE_COUNTER(label) ::chip::Tracing::Internal::Counter(label) +#define MATTER_TRACE_METRIC(label, value) ::chip::Tracing::Internal::Metric(label, value) namespace chip { namespace Tracing { diff --git a/src/tracing/json/json_tracing.cpp b/src/tracing/json/json_tracing.cpp index af66e12a61a50b..64627f079d04dd 100644 --- a/src/tracing/json/json_tracing.cpp +++ b/src/tracing/json/json_tracing.cpp @@ -295,6 +295,15 @@ void JsonBackend::TraceCounter(const char * label) OutputValue(value); } +void JsonBackend::TraceMetric(const char * label, int32_t val) +{ + ::Json::Value value; + value["label"] = label; + value["value"] = val; + + OutputValue(value); +} + void JsonBackend::LogMessageSend(MessageSendInfo & info) { ::Json::Value value; diff --git a/src/tracing/json/json_tracing.h b/src/tracing/json/json_tracing.h index 302c4bd5b5a485..e8ad8ee5894b03 100644 --- a/src/tracing/json/json_tracing.h +++ b/src/tracing/json/json_tracing.h @@ -52,6 +52,7 @@ class JsonBackend : public ::chip::Tracing::Backend void TraceEnd(const char * label, const char * group) override; void TraceInstant(const char * label, const char * group) override; void TraceCounter(const char * label) override; + void TraceMetric(const char * label, int32_t val) override; void LogMessageSend(MessageSendInfo &) override; void LogMessageReceived(MessageReceivedInfo &) override; void LogNodeLookup(NodeLookupInfo &) override; diff --git a/src/tracing/macros.h b/src/tracing/macros.h index b456137b88a166..0643a466c2a77d 100644 --- a/src/tracing/macros.h +++ b/src/tracing/macros.h @@ -26,6 +26,14 @@ // MATTER_TRACE_END(label, group) // MATTER_TRACE_INSTANT(label, group) // MATTER_TRACE_SCOPE(label, group) + +// Tracing macro to trace monotonically increasing counter values. +// MATTER_TRACE_COUNTER(label) + +// Tracing macro to represent historical metric data i.e the data points which represent different +// values at different point of time. +// MATTER_TRACE_METRIC(label, value) + #include #include #include @@ -79,6 +87,7 @@ #define MATTER_TRACE_INSTANT(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) #define MATTER_TRACE_SCOPE(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) #define MATTER_TRACE_COUNTER(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) +#define MATTER_TRACE_METRIC(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) #define MATTER_LOG_MESSAGE_SEND(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) #define MATTER_LOG_MESSAGE_RECEIVED(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) diff --git a/src/tracing/multiplexed/include/matter/tracing/macros_impl.h b/src/tracing/multiplexed/include/matter/tracing/macros_impl.h index 8b3e728289dc1d..eda5f2ed5142a4 100644 --- a/src/tracing/multiplexed/include/matter/tracing/macros_impl.h +++ b/src/tracing/multiplexed/include/matter/tracing/macros_impl.h @@ -29,6 +29,7 @@ #define MATTER_TRACE_END(label, group) ::chip::Tracing::Internal::End(label, group) #define MATTER_TRACE_INSTANT(label, group) ::chip::Tracing::Internal::Instant(label, group) #define MATTER_TRACE_COUNTER(label) ::chip::Tracing::Internal::Counter(label) +#define MATTER_TRACE_METRIC(label, value) ::chip::Tracing::Internal::Metric(label, value) namespace chip { namespace Tracing { diff --git a/src/tracing/none/include/matter/tracing/macros_impl.h b/src/tracing/none/include/matter/tracing/macros_impl.h index 3119dce2a2b49f..1fc940683d75e2 100644 --- a/src/tracing/none/include/matter/tracing/macros_impl.h +++ b/src/tracing/none/include/matter/tracing/macros_impl.h @@ -32,3 +32,4 @@ #define MATTER_TRACE_INSTANT(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) #define MATTER_TRACE_SCOPE(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) #define MATTER_TRACE_COUNTER(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) +#define MATTER_TRACE_METRIC(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) diff --git a/src/tracing/perfetto/include/matter/tracing/macros_impl.h b/src/tracing/perfetto/include/matter/tracing/macros_impl.h index 9b05d2f3539a45..559e877db1fe11 100644 --- a/src/tracing/perfetto/include/matter/tracing/macros_impl.h +++ b/src/tracing/perfetto/include/matter/tracing/macros_impl.h @@ -37,3 +37,5 @@ PERFETTO_DEFINE_CATEGORIES(perfetto::Category("Matter").SetDescription("Matter t static int count##_label = 0; \ TRACE_COUNTER("Matter", label, ++count##_label); \ } while (0) + +#define MATTER_TRACE_METRIC(label, value) TRACE_COUNTER("Matter", label, value) diff --git a/src/tracing/registry.cpp b/src/tracing/registry.cpp index 7c43442b5b83db..ee46b542e7f5f2 100644 --- a/src/tracing/registry.cpp +++ b/src/tracing/registry.cpp @@ -84,6 +84,14 @@ void Counter(const char * label) } } +void Metric(const char * label, int32_t value) +{ + for (auto & backend : gTracingBackends) + { + backend.TraceMetric(label, value); + } +} + void LogMessageSend(::chip::Tracing::MessageSendInfo & info) { for (auto & backend : gTracingBackends) diff --git a/src/tracing/registry.h b/src/tracing/registry.h index 083ebcb2225ad2..5f3b3c1a6cea84 100644 --- a/src/tracing/registry.h +++ b/src/tracing/registry.h @@ -77,6 +77,7 @@ void Begin(const char * label, const char * group); void End(const char * label, const char * group); void Instant(const char * label, const char * group); void Counter(const char * label); +void Metric(const char * label, int32_t value); void LogMessageSend(::chip::Tracing::MessageSendInfo & info); void LogMessageReceived(::chip::Tracing::MessageReceivedInfo & info);