From 925eb56b95873e996ae85d6aee21efa400bff6b5 Mon Sep 17 00:00:00 2001 From: shripad621git Date: Thu, 12 Oct 2023 13:49:11 +0530 Subject: [PATCH] New Counter Implementation macro --- config/esp32/components/chip/CMakeLists.txt | 5 +++ src/protocols/secure_channel/CASESession.cpp | 2 + src/tracing/backend.h | 1 + src/tracing/esp32_trace/BUILD.gn | 8 +++- src/tracing/esp32_trace/counter.cpp | 30 +++++++++++++ src/tracing/esp32_trace/counter.h | 42 +++++++++++++++++++ src/tracing/esp32_trace/esp32_tracing.cpp | 5 +++ src/tracing/esp32_trace/esp32_tracing.h | 2 + .../include/matter/tracing/macros_impl.h | 1 + src/tracing/macros.h | 2 + .../include/matter/tracing/macros_impl.h | 1 + .../none/include/matter/tracing/macros_impl.h | 4 ++ .../include/matter/tracing/macros_impl.h | 4 ++ src/tracing/perfetto/perfetto_tracing.cpp | 1 + src/tracing/perfetto/perfetto_tracing.h | 1 + src/tracing/registry.cpp | 8 ++++ src/tracing/registry.h | 1 + 17 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/tracing/esp32_trace/counter.cpp create mode 100644 src/tracing/esp32_trace/counter.h diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index b2058fe75df41c..d84277262d89ee 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -263,6 +263,9 @@ endif() if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE) chip_gn_arg_bool("matter_enable_tracing_support" "true") chip_gn_arg_append("matter_trace_config" "\"${CHIP_ROOT}/src/tracing/esp32_trace:esp32_trace_tracing\"") +else() + chip_gn_arg_bool("matter_enable_tracing_support" "true") + chip_gn_arg_append("matter_trace_config" "\"${CHIP_ROOT}/src/tracing/none\"") endif() if (CONFIG_USE_ESP32_ECDSA_PERIPHERAL) @@ -271,6 +274,8 @@ endif() if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE) target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/tracing/esp32_trace/include") +else() + target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/tracing/none/include") endif() if (CONFIG_ENABLE_MATTER_EVENT_LIST) diff --git a/src/protocols/secure_channel/CASESession.cpp b/src/protocols/secure_channel/CASESession.cpp index 6e51d000e3822c..e4dc1bb5e855c2 100644 --- a/src/protocols/secure_channel/CASESession.cpp +++ b/src/protocols/secure_channel/CASESession.cpp @@ -742,6 +742,8 @@ CHIP_ERROR CASESession::SendSigma1() CHIP_ERROR CASESession::HandleSigma1_and_SendSigma2(System::PacketBufferHandle && msg) { MATTER_TRACE_SCOPE("HandleSigma1_and_SendSigma2", "CASESession"); + MATTER_TRACE_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..c439922f2cd5ff 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 MATTER_TRACE_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..2d5188f89eec44 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) +// MATTER_TRACE_COUNTER(label, group) #include #include #include @@ -78,6 +79,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 MATTER_TRACE_COUNTER(...) _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 e1f2adc56d989e..4fe42e930da1fd 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, 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..456264765d16c4 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 MATTER_TRACE_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..ec663d90dbd750 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 MATTER_TRACE_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);