diff --git a/src/protocols/secure_channel/CASESession.cpp b/src/protocols/secure_channel/CASESession.cpp index 2afc40feacaf02..cdedb3e5cad5f6 100644 --- a/src/protocols/secure_channel/CASESession.cpp +++ b/src/protocols/secure_channel/CASESession.cpp @@ -747,6 +747,8 @@ CHIP_ERROR CASESession::SendSigma1() CHIP_ERROR CASESession::HandleSigma1_and_SendSigma2(System::PacketBufferHandle && msg) { MATTER_TRACE_SCOPE("HandleSigma1_and_SendSigma2", "CASESession"); + CHIP_GENERIC_COUNTER("sigma1cnt", "CASE"); + ReturnErrorOnFailure(HandleSigma1(std::move(msg))); return CHIP_NO_ERROR; diff --git a/src/tracing/backend.h b/src/tracing/backend.h index 7e04490e333ff7..36fda9ae33eea9 100644 --- a/src/tracing/backend.h +++ b/src/tracing/backend.h @@ -63,6 +63,7 @@ class Backend : public ::chip::IntrusiveListNodeBase<> /// Trace a zero-sized event virtual void TraceInstant(const char * label, const char * group) {} + virtual void TraceCounter(const char * label, const char * group) {} virtual void LogMessageSend(MessageSendInfo &) { TraceInstant("MessageSent", "Messaging"); } virtual void LogMessageReceived(MessageReceivedInfo &) { TraceInstant("MessageReceived", "Messaging"); } diff --git a/src/tracing/esp32_trace/BUILD.gn b/src/tracing/esp32_trace/BUILD.gn index cb41004a74289b..6afd359a3323fc 100644 --- a/src/tracing/esp32_trace/BUILD.gn +++ b/src/tracing/esp32_trace/BUILD.gn @@ -25,10 +25,16 @@ static_library("backend") { output_dir = "${root_out_dir}/lib" sources = [ + "counter.cpp", + "counter.h", "esp32_tracing.cpp", "esp32_tracing.h", ] - public_deps = [ "${chip_root}/src/tracing" ] + public_deps = [ + "${chip_root}/src/lib/core", + "${chip_root}/src/lib/support", + "${chip_root}/src/tracing", + ] } source_set("esp32_trace_tracing") { diff --git a/src/tracing/esp32_trace/counter.cpp b/src/tracing/esp32_trace/counter.cpp new file mode 100644 index 00000000000000..0fb03d082d1da7 --- /dev/null +++ b/src/tracing/esp32_trace/counter.cpp @@ -0,0 +1,30 @@ +#include "counter.h" + +namespace Insights{ +ESPInsightsCounter* ESPInsightsCounter::m_head = nullptr; + +ESPInsightsCounter* ESPInsightsCounter::GetInstance(const char* label, const char* group) { + + ESPInsightsCounter* current = m_head; + ESPInsightsCounter* previous = nullptr; + + while (current != nullptr) { + if (strcmp(current->label, label) == 0 && strcmp(current->group, group) == 0) { + current->instanceCount++; + return current; + } + previous = current; + current = current->m_next; + } + + ESPInsightsCounter* newInstance = new ESPInsightsCounter(label, group); + newInstance->m_next = m_head; + m_head = newInstance; + return newInstance; +} + +int ESPInsightsCounter::GetInstanceCount() const { + return instanceCount; +} + +} diff --git a/src/tracing/esp32_trace/counter.h b/src/tracing/esp32_trace/counter.h new file mode 100644 index 00000000000000..c0f6b533f7027a --- /dev/null +++ b/src/tracing/esp32_trace/counter.h @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +#define PATH1 "sys.cnt" +namespace Insights { +class ESPInsightsCounter +{ +private: + static ESPInsightsCounter * m_head; + char label[50]; + char group[50]; + int instanceCount; + ESPInsightsCounter * m_next; + bool registered = false; + + ESPInsightsCounter(const char * labelParam, const char * groupParam) : instanceCount(1), m_next(nullptr) + { + strncpy(label, labelParam, sizeof(label)); + strncpy(group, groupParam, sizeof(group)); + } + +public: + static ESPInsightsCounter * GetInstance(const char * label, const char * group); + + int GetInstanceCount() const; + + void ReportMetrics() + { + std::cout << "Trace instant: Label=" << label << ", Group=" << group << ", Instance Count=" << instanceCount << std::endl; + if (!registered) + { + esp_diag_metrics_register("SYS_CNT", label, label, PATH1, ESP_DIAG_DATA_TYPE_UINT); + registered = true; + } + std::cout << "Label ------" << label << "Count---" << instanceCount; + esp_diag_metrics_add_uint(label, instanceCount); + } +}; + +} // namespace Insights diff --git a/src/tracing/esp32_trace/esp32_tracing.cpp b/src/tracing/esp32_trace/esp32_tracing.cpp index 414cdd83edcc0d..2814870745f8f6 100644 --- a/src/tracing/esp32_trace/esp32_tracing.cpp +++ b/src/tracing/esp32_trace/esp32_tracing.cpp @@ -17,6 +17,7 @@ */ #include "esp32_tracing.h" +#include "counter.h" #include #include #include @@ -46,6 +47,10 @@ void ESP32Backend::LogNodeDiscovered(NodeDiscoveredInfo & info) {} void ESP32Backend::LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo & info) {} +void ESP32Backend::TraceCounter(const char * label, const char * group) +{ + ::Insights::ESPInsightsCounter::GetInstance(label, group)->ReportMetrics(); +} void ESP32Backend::TraceBegin(const char * label, const char * group) { LOG_HEAP_INFO(label, group, "Entry"); diff --git a/src/tracing/esp32_trace/esp32_tracing.h b/src/tracing/esp32_trace/esp32_tracing.h index 9857eb111b2c3d..9d7eeda028a2d9 100644 --- a/src/tracing/esp32_trace/esp32_tracing.h +++ b/src/tracing/esp32_trace/esp32_tracing.h @@ -21,6 +21,8 @@ class ESP32Backend : public ::chip::Tracing::Backend /// Trace a zero-sized event void TraceInstant(const char * label, const char * group) override; + void TraceCounter(const char * label, const char * group) override; + void LogMessageSend(MessageSendInfo &) override; void LogMessageReceived(MessageReceivedInfo &) override; 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 4d8a8a2a214525..d0e03110e17840 100644 --- a/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h +++ b/src/tracing/esp32_trace/include/matter/tracing/macros_impl.h @@ -28,6 +28,7 @@ #define MATTER_TRACE_BEGIN(label, group) ::chip::Tracing::Internal::Begin(label, group) #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 CHIP_GENERIC_COUNTER(label, group) ::chip::Tracing::Internal::Counter(label, group) namespace chip { namespace Tracing { diff --git a/src/tracing/macros.h b/src/tracing/macros.h index 5f836e44d373bc..32229212bda260 100644 --- a/src/tracing/macros.h +++ b/src/tracing/macros.h @@ -26,6 +26,7 @@ // MATTER_TRACE_END(label, group) // MATTER_TRACE_INSTANT(label, group) // MATTER_TRACE_SCOPE(label, group) +// CHIP_GENERIC_COUNTER(label, group) #include #include #include diff --git a/src/tracing/multiplexed/include/matter/tracing/macros_impl.h b/src/tracing/multiplexed/include/matter/tracing/macros_impl.h index e1f2adc56d989e..a1558c3c84e84d 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 CHIP_GENERIC_COUNTER(label, group) ::chip::Tracing::Internal::Counter(label, group) 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 3f7f052b5cac0d..fd34d3ab1b2c99 100644 --- a/src/tracing/none/include/matter/tracing/macros_impl.h +++ b/src/tracing/none/include/matter/tracing/macros_impl.h @@ -31,3 +31,7 @@ #define MATTER_TRACE_END(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) #define MATTER_TRACE_INSTANT(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) #define MATTER_TRACE_SCOPE(...) _MATTER_TRACE_DISABLE(__VA_ARGS__) +#define CHIP_GENERIC_COUNTER(label, group) \ + do \ + { \ + } while (false) diff --git a/src/tracing/perfetto/include/matter/tracing/macros_impl.h b/src/tracing/perfetto/include/matter/tracing/macros_impl.h index 266ac69f5735a0..4b9b8f68b25466 100644 --- a/src/tracing/perfetto/include/matter/tracing/macros_impl.h +++ b/src/tracing/perfetto/include/matter/tracing/macros_impl.h @@ -30,3 +30,7 @@ PERFETTO_DEFINE_CATEGORIES(perfetto::Category("Matter").SetDescription("Matter t #define MATTER_TRACE_END(label, group) TRACE_EVENT_END("Matter") #define MATTER_TRACE_INSTANT(label, group) TRACE_EVENT_INSTANT("Matter", label, "class_name", group) #define MATTER_TRACE_SCOPE(label, group) TRACE_EVENT("Matter", label, "class_name", group) +#define CHIP_GENERIC_COUNTER(label, group) \ + do \ + { \ + } while (false) diff --git a/src/tracing/perfetto/perfetto_tracing.cpp b/src/tracing/perfetto/perfetto_tracing.cpp index 6d89dbbccc9723..791fe26b95f38c 100644 --- a/src/tracing/perfetto/perfetto_tracing.cpp +++ b/src/tracing/perfetto/perfetto_tracing.cpp @@ -52,6 +52,7 @@ void PerfettoBackend::LogMessageReceived(MessageReceivedInfo & info) ); } +void PerfettoBackend::TraceCounter(const char * label, const char * group) {} void PerfettoBackend::LogMessageSend(MessageSendInfo & info) { const char * messageType = "UNKNOWN"; diff --git a/src/tracing/perfetto/perfetto_tracing.h b/src/tracing/perfetto/perfetto_tracing.h index 901165e35a548d..1ffcc6ffd1f797 100644 --- a/src/tracing/perfetto/perfetto_tracing.h +++ b/src/tracing/perfetto/perfetto_tracing.h @@ -38,6 +38,7 @@ class PerfettoBackend : public ::chip::Tracing::Backend // as they would be slower than expected. Perfetto trace macros // are expected to be set exclusively (via matter_trace_config) + void TraceCounter(const char * label, const char * group) override; void LogMessageSend(MessageSendInfo &) override; void LogMessageReceived(MessageReceivedInfo &) override; diff --git a/src/tracing/registry.cpp b/src/tracing/registry.cpp index 9f7507aa9f75a6..80f2a7545a3f21 100644 --- a/src/tracing/registry.cpp +++ b/src/tracing/registry.cpp @@ -76,6 +76,14 @@ void Instant(const char * label, const char * group) } } +void Counter(const char * label, const char * group) +{ + for (auto & backend : gTracingBackends) + { + backend.TraceCounter(label, group); + } +} + void LogMessageSend(::chip::Tracing::MessageSendInfo & info) { for (auto & backend : gTracingBackends) diff --git a/src/tracing/registry.h b/src/tracing/registry.h index 853372585840fa..1dce9c13149f6d 100644 --- a/src/tracing/registry.h +++ b/src/tracing/registry.h @@ -76,6 +76,7 @@ namespace Internal { 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, const char * group); void LogMessageSend(::chip::Tracing::MessageSendInfo & info); void LogMessageReceived(::chip::Tracing::MessageReceivedInfo & info);