Skip to content

Commit

Permalink
New Counter Implementation macro
Browse files Browse the repository at this point in the history
  • Loading branch information
shripad621git committed Nov 21, 2023
1 parent a3c5d19 commit f0263e1
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 5 deletions.
5 changes: 5 additions & 0 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/protocols/secure_channel/CASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions src/tracing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,5 @@ source_set("macros") {
":tracing",
":tracing_buildconfig",
]

if (matter_enable_tracing_support) {
public_deps += [ matter_trace_config ]
}
public_deps += [ matter_trace_config ]
}
1 change: 1 addition & 0 deletions src/tracing/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"); }

Expand Down
8 changes: 7 additions & 1 deletion src/tracing/esp32_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
30 changes: 30 additions & 0 deletions src/tracing/esp32_trace/counter.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

}
42 changes: 42 additions & 0 deletions src/tracing/esp32_trace/counter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <esp_diagnostics_metrics.h>
#include <iostream>
#include <lib/support/CHIPCounter.h>
#include <string.h>

#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
5 changes: 5 additions & 0 deletions src/tracing/esp32_trace/esp32_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "esp32_tracing.h"
#include "counter.h"
#include <esp_heap_caps.h>
#include <esp_insights.h>
#include <esp_log.h>
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions src/tracing/esp32_trace/esp32_tracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/tracing/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <matter/tracing/macros_impl.h>
#include <tracing/log_declares.h>
#include <tracing/registry.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
4 changes: 4 additions & 0 deletions src/tracing/none/include/matter/tracing/macros_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 4 additions & 0 deletions src/tracing/perfetto/include/matter/tracing/macros_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions src/tracing/perfetto/perfetto_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/tracing/perfetto/perfetto_tracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions src/tracing/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/tracing/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f0263e1

Please sign in to comment.