Skip to content

Commit

Permalink
Counter Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shripad621git committed Oct 3, 2023
1 parent 1ec98c8 commit aa8a820
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 4 deletions.
5 changes: 5 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,11 @@ menu "CHIP Device Layer"
ESP Insights is a remote diagnostics solution to monitor the health of ESP devices in the field.
Enabling the above option will enable the esp32 specific tracing functionality and report the
diagnostic information to the insights cloud.

config ENABLE_ESP_INSIGHTS_COUNTERS
bool "Enable Counters"
depends on ESP_INSIGHTS_ENABLED
default y

endmenu

Expand Down
2 changes: 2 additions & 0 deletions src/lib/support/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static_library("support") {
"IniEscaping.cpp",
"IniEscaping.h",
"Iterators.h",
"LabeledCounter.h",
"LifetimePersistedCounter.h",
"ObjectLifeCycle.h",
"PersistedCounter.h",
Expand Down Expand Up @@ -177,6 +178,7 @@ static_library("support") {
"${nlio_root}:nlio",
]


# These are needed because we include CHIPCore.h, which uses config
# options for src/ble and src/inet, however we cannot depend on those
# directly as such a dependency is cyclic.
Expand Down
5 changes: 5 additions & 0 deletions src/lib/support/CHIPCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,9 @@ class MonotonicallyIncreasingCounter : public Counter<T>
T mCounterValue;
};

#define CHIP_GENERIC_COUNTER(label, group) \
do \
{ \
} while (false)

} // namespace chip
32 changes: 32 additions & 0 deletions src/lib/support/LabeledCounter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <lib/support/CHIPCounter.h>

#if CONFIG_ENABLE_ESP_INSIGHTS_COUNTERS
#include <matter/tracing/ESP32Counter.h>
#else

namespace chip {

template <typename T>
class AdvanceCounter : public Counter<T>
{
public:
AdvanceCounter(const char *label=NULL,const char *group=NULL) : mCounterValue(0) {}
~AdvanceCounter() override{};
CHIP_ERROR Init(T aStartValue)
{
return CHIP_NO_ERROR;
}
CHIP_ERROR Advance() override
{
return CHIP_NO_ERROR;
}
T GetValue() override { return mCounterValue; }

protected:
T mCounterValue;
const char* mlabel;
const char* mgroup;
};

}
#endif
10 changes: 10 additions & 0 deletions src/protocols/secure_channel/CASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <lib/support/SafeInt.h>
#include <lib/support/ScopedBuffer.h>
#include <lib/support/TypeTraits.h>
#include <lib/support/LabeledCounter.h>
#include <lib/support/CHIPCounter.h>
#include <platform/PlatformManager.h>
#include <protocols/Protocols.h>
#include <protocols/secure_channel/CASEDestinationId.h>
Expand All @@ -50,6 +52,9 @@

namespace {

chip::AdvanceCounter<int> sigma1cnt("sigma1cnt","CASE");
chip::AdvanceCounter<int> sigma2cnt("sigma2cnt","CASE");

enum
{
kTag_TBEData_SenderNOC = 1,
Expand Down Expand Up @@ -747,6 +752,9 @@ CHIP_ERROR CASESession::SendSigma1()
CHIP_ERROR CASESession::HandleSigma1_and_SendSigma2(System::PacketBufferHandle && msg)
{
MATTER_TRACE_SCOPE("HandleSigma1_and_SendSigma2", "CASESession");
sigma1cnt.Advance();
printf("Sigma1 cnt %d",sigma1cnt.GetValue());
CHIP_GENERIC_COUNTER("sigma1cnt","CASE");
ReturnErrorOnFailure(HandleSigma1(std::move(msg)));

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -971,6 +979,8 @@ CHIP_ERROR CASESession::SendSigma2Resume()
CHIP_ERROR CASESession::SendSigma2()
{
MATTER_TRACE_SCOPE("SendSigma2", "CASESession");
sigma2cnt.Advance();
printf("Sigma2 cnt %d",sigma2cnt.GetValue());

VerifyOrReturnError(GetLocalSessionId().HasValue(), CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mFabricsTable != nullptr, CHIP_ERROR_INCORRECT_STATE);
Expand Down
11 changes: 9 additions & 2 deletions src/tracing/esp32_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ config("tracing") {
}

source_set("esp32_trace") {
public = [ "include/matter/tracing/macros_impl.h" ]
sources = [ "include/matter/tracing/macros_impl.cpp" ]
public = [ "include/matter/tracing/macros_impl.h",
"include/matter/tracing/helper.h",
"include/matter/tracing/ESP32Counter.h",
"include/matter/tracing/counter.h", ]
sources = [ "include/matter/tracing/macros_impl.cpp",
"include/matter/tracing/counter.cpp",]
public_deps = ["${chip_root}/src/lib/support",
"${chip_root}/src/lib/core",
]
public_configs = [ ":tracing" ]
}
149 changes: 149 additions & 0 deletions src/tracing/esp32_trace/include/matter/tracing/ESP32Counter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2016-2017 Nest Labs, Inc.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <esp_diagnostics_variables.h>
#include <esp_err.h>
#include <esp_log.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPCounter.h>
#include <string.h>

#define PATH "mtr.cntr"
#define space_conc(str1, str2) #str1 " " #str2

namespace chip {
// #if CONFIG_ENABLE_ESP_INSIGHTS_COUNTERS
template <typename T>
class AdvanceCounter : public Counter<T>
{
public:
AdvanceCounter(const char * label = NULL, const char * group = NULL) : mCounterValue(0)
{
if (label)
{
mlabel = label;
}
if (group)
{
mgroup = group;
}
}
~AdvanceCounter() override{};

/**
* @brief
* Initialize a MonotonicallyIncreasingCounter object.
*
* @param[in] aStartValue The starting value of the counter.
*
* @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise
*/
CHIP_ERROR Init(T aStartValue)
{
CHIP_ERROR err = CHIP_NO_ERROR;

mCounterValue = aStartValue;

return err;
}

/**
* @brief
* Advance the value of the counter.
*
* @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise
*/
CHIP_ERROR Advance() override
{
CHIP_ERROR err = CHIP_NO_ERROR;

mCounterValue++;
if (!isRegistered())
{
if (mlabel && mgroup)
{

esp_err_t err1 = esp_diag_variable_register(mgroup, mlabel, mlabel, PATH, ESP_DIAG_DATA_TYPE_INT);
if (err1 == ESP_OK)
{
printf("Success");
registered = true;
}
else
{
ESP_LOGE("CNT", "Registration failed, err: %d", err1);
printf("Fail");
}
esp_diag_variable_add_int(mlabel, (uint32_t) mCounterValue);
}
}

printf("registerd %d", registered);
if (mlabel)
{
esp_diag_variable_add_int(mlabel, (uint32_t) mCounterValue);
}

return err;
}

/**
* @brief
* Get the current value of the counter.
*
* @return The current value of the counter.
*/
T GetValue() override { return mCounterValue; }

protected:
T mCounterValue;
const char * mlabel;
const char * mgroup;
bool registered = false;
bool isRegistered() { return registered; }
};
/*
#else
template <typename T>
class AdvanceCounter : public Counter<T>
{
public:
AdvanceCounter(const char *label=NULL,const char *group=NULL) : mCounterValue(0) {}
~AdvanceCounter() override{};
CHIP_ERROR Init(T aStartValue)
{
return CHIP_NO_ERROR;
}
CHIP_ERROR Advance() override
{
return CHIP_NO_ERROR;
}
T GetValue() override { return mCounterValue; }
protected:
T mCounterValue;
const char* mlabel;
const char* mgroup;
};
#endif
*/
} // namespace chip

// namespace chip
30 changes: 30 additions & 0 deletions src/tracing/esp32_trace/include/matter/tracing/counter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "counter.h"

namespace Insights{
InstantObject* InstantObject::m_head = nullptr;

InstantObject* InstantObject::getInstance(const char* label, const char* group) {

InstantObject* current = m_head;
InstantObject* 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;
}

InstantObject* newInstance = new InstantObject(label, group);
newInstance->m_next = m_head;
m_head = newInstance;
return newInstance;
}

int InstantObject::getInstanceCount() const {
return instanceCount;
}

}
42 changes: 42 additions & 0 deletions src/tracing/esp32_trace/include/matter/tracing/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 InstantObject
{
private:
static InstantObject * m_head;
char label[50];
char group[50];
int instanceCount;
InstantObject * m_next;
bool registered = false;

InstantObject(const char * labelParam, const char * groupParam) : instanceCount(1), m_next(nullptr)
{
strncpy(label, labelParam, sizeof(label));
strncpy(group, groupParam, sizeof(group));
}

public:
static InstantObject * getInstance(const char * label, const char * group);

int getInstanceCount() const;

void traceInstant()
{
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
20 changes: 18 additions & 2 deletions src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@
#include "macros_impl.h"
#include <esp_heap_caps.h>
#include <esp_insights.h>


namespace Insights {

unsigned int ESP32Backend::blacklistHashes[BLACKLIST_SIZE] = {
hash("PacketParser"),
hash("MinMdnsResolver"),
hash("SessionManager"),
hash("DeviceCommissioner"),
hash("NetworkCommissioning")
};

#define LOG_HEAP_INFO(label, group, entry_exit) \
do \
{ \
Expand All @@ -34,11 +44,17 @@ ESP32Backend::ESP32Backend(const char * str, ...)
va_start(args, str);
mlabel = str;
mgroup = va_arg(args, const char *);
LOG_HEAP_INFO(mlabel, mgroup, "Entry");

if (!isBlacklisted(mgroup)) {
LOG_HEAP_INFO(mlabel, mgroup, "Entry");
}

}

ESP32Backend::~ESP32Backend()
{
LOG_HEAP_INFO(mlabel, mgroup, "Exit");
if (!isBlacklisted(mgroup)) {
LOG_HEAP_INFO(mlabel, mgroup, "Exit");
}
}
} // namespace Insights
Loading

0 comments on commit aa8a820

Please sign in to comment.