Skip to content

Commit

Permalink
Try Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shripad621git committed Aug 31, 2023
1 parent 0eafac9 commit b7daabd
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 5 deletions.
9 changes: 9 additions & 0 deletions examples/lighting-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE
#include <esp_insights.h>
#include <matter/tracing/system_stats.h>
#endif

using namespace ::chip;
Expand All @@ -78,6 +79,7 @@ static const char * TAG = "light-app";
static AppDeviceCallbacks EchoCallbacks;
static AppDeviceCallbacksDelegate sAppDeviceCallbacksDelegate;


namespace {
#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider;
Expand Down Expand Up @@ -115,6 +117,12 @@ static void InitServer(intptr_t context)
Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config
}

static void InitTimer(intptr_t context)
{
register_system_metrics();
fetch_system_metrics();
}

extern "C" void app_main()
{
// Initialize the ESP NVS layer.
Expand Down Expand Up @@ -184,6 +192,7 @@ extern "C" void app_main()
ESPOpenThreadInit();

chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast<intptr_t>(nullptr));
chip::DeviceLayer::PlatformMgr().ScheduleWork(InitTimer, reinterpret_cast<intptr_t>(nullptr));

error = GetAppTask().StartAppTask();
if (error != CHIP_NO_ERROR)
Expand Down
21 changes: 21 additions & 0 deletions src/protocols/secure_channel/CASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@
#include <system/SystemClock.h>
#include <system/TLVPacketBufferBackingStore.h>
#include <tracing/macros.h>
#include <matter/tracing/ESP32Counter.h>
#include <transport/SessionManager.h>

namespace {

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

enum
{
kTag_TBEData_SenderNOC = 1,
Expand Down Expand Up @@ -547,6 +553,11 @@ void CASESession::OnResponseTimeout(ExchangeContext * ec)
VerifyOrReturn(mExchangeCtxt == ec, ChipLogError(SecureChannel, "CASESession::OnResponseTimeout exchange doesn't match"));
ChipLogError(SecureChannel, "CASESession timed out while waiting for a response from the peer. Current state was %u",
to_underlying(mState));

casefailcount.Advance();
printf("CASE fail count %d",casefailcount.GetValue());
MATTER_TRACE_INSTANT("casefailcnt","CASE");

// Discard the exchange so that Clear() doesn't try aborting it. The
// exchange will handle that.
DiscardExchange();
Expand Down Expand Up @@ -747,6 +758,10 @@ CHIP_ERROR CASESession::SendSigma1()
CHIP_ERROR CASESession::HandleSigma1_and_SendSigma2(System::PacketBufferHandle && msg)
{
MATTER_TRACE_SCOPE("HandleSigma1_and_SendSigma2", "CASESession");

MATTER_TRACE_INSTANT("sigma1","CASE");
sigma1.Advance();
printf("Sigma1cnt %d ",sigma1.GetValue());
ReturnErrorOnFailure(HandleSigma1(std::move(msg)));

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -971,6 +986,9 @@ CHIP_ERROR CASESession::SendSigma2Resume()
CHIP_ERROR CASESession::SendSigma2()
{
MATTER_TRACE_SCOPE("SendSigma2", "CASESession");
CHIP_ERROR err1 = sigma2cnt.Advance();
printf("Sigma 2 cnt %d",sigma2cnt.GetValue());
MATTER_TRACE_INSTANT("sigma2cnt","CASE");

VerifyOrReturnError(GetLocalSessionId().HasValue(), CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mFabricsTable != nullptr, CHIP_ERROR_INCORRECT_STATE);
Expand Down Expand Up @@ -1568,6 +1586,9 @@ CHIP_ERROR CASESession::SendSigma3c(SendSigma3Data & data, CHIP_ERROR status)
CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
{
MATTER_TRACE_SCOPE("HandleSigma3", "CASESession");
CHIP_ERROR err1 = sigma3cnt.Advance();
printf("Sigma 3 cnt %d",sigma3cnt.GetValue());
MATTER_TRACE_INSTANT("sigma3cnt","CASE");
CHIP_ERROR err = CHIP_NO_ERROR;
System::PacketBufferTLVReader tlvReader;
TLV::TLVReader decryptedDataTlvReader;
Expand Down
5 changes: 5 additions & 0 deletions src/protocols/secure_channel/PASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
#include <lib/support/TypeTraits.h>
#include <lib/support/CHIPCounter.h>
#include <protocols/Protocols.h>
#include <protocols/secure_channel/Constants.h>
#include <protocols/secure_channel/StatusReport.h>
Expand Down Expand Up @@ -521,6 +522,7 @@ CHIP_ERROR PASESession::HandlePBKDFParamResponse(System::PacketBufferHandle && m
SuccessOrExit(err);

err = SendMsg1();

SuccessOrExit(err);

exit:
Expand Down Expand Up @@ -620,6 +622,7 @@ CHIP_ERROR PASESession::HandleMsg1_and_SendMsg2(System::PacketBufferHandle && ms
err = mExchangeCtxt->SendMessage(MsgType::PASE_Pake2, std::move(msg2), SendFlags(SendMessageFlags::kExpectResponse));
SuccessOrExit(err);

MATTER_TRACE_INSTANT("pake2cnt","PASE");
mNextExpectedMsg.SetValue(MsgType::PASE_Pake3);
}

Expand Down Expand Up @@ -715,6 +718,8 @@ CHIP_ERROR PASESession::HandleMsg3(System::PacketBufferHandle && msg)
MATTER_TRACE_SCOPE("HandleMsg3", "PASESession");
CHIP_ERROR err = CHIP_NO_ERROR;

MATTER_TRACE_INSTANT("pake3cnt","PASE");

ChipLogDetail(SecureChannel, "Received spake2p msg3");

mNextExpectedMsg.ClearValue();
Expand Down
12 changes: 10 additions & 2 deletions src/tracing/esp32_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ 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/system_stats.h",
"include/matter/tracing/ESP32Counter.h",
"include/matter/tracing/counter.h" ]
public_deps = ["${chip_root}/src/lib/support",
"${chip_root}/src/system",
"${chip_root}/src/lib/core"]
sources = [ "include/matter/tracing/macros_impl.cpp",
"include/matter/tracing/counter.cpp" ]
public_configs = [ ":tracing" ]
}
132 changes: 132 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,132 @@
/*
*
* 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 <lib/support/CHIPCounter.h>
#include <lib/core/CHIPError.h>
#include <esp_diagnostics_variables.h>
#include <string.h>
#include <esp_err.h>
#include <esp_log.h>

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

namespace chip{
/**
* @class MonotonicallyIncreasingCounter
*
* @brief
* A class for managing a monotonically-increasing counter as an integer value.
*/
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;
}
};
}
// 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 <iostream>
#include <string.h>
#include <esp_diagnostics_metrics.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;
}
esp_diag_metrics_add_uint(label,instanceCount);

}
};

}

19 changes: 17 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 @@ -15,8 +15,10 @@
*/

#include "macros_impl.h"
#include "helper.h"
#include <esp_heap_caps.h>
#include <esp_insights.h>

namespace Insights {

#define LOG_HEAP_INFO(label, group, entry_exit) \
Expand All @@ -28,17 +30,30 @@ namespace Insights {
heap_caps_get_free_size(MALLOC_CAP_8BIT)); \
} while (0)

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

ESP32Backend::ESP32Backend(const char * str, ...)
{
va_list args;
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 b7daabd

Please sign in to comment.