Skip to content

Commit

Permalink
Addressed review Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shripad621git committed Nov 8, 2023
1 parent e73a883 commit 6bc181a
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/tracing/esp32_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ source_set("esp32_trace_tracing") {
public = [ "include/matter/tracing/macros_impl.h" ]
public_configs = [ ":tracing" ]
deps = [ ":backend" ]

}
120 changes: 117 additions & 3 deletions src/tracing/esp32_trace/esp32_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,113 @@
namespace chip {
namespace Tracing {
namespace Insights {
namespace {
static constexpr size_t gPermitListSize = 10;
struct PermitListEntry
{
size_t hash;
};

// Implements a murmurhash with 0 seed.
static size_t MurmurHash(const void * key)
{
const size_t multiplier = 0x5bd1e995;
const size_t shift = 24;
const unsigned char * data = (const unsigned char *) key;
size_t hash = 0;

while (*data)
{
size_t value = *data++;
value *= multiplier;
value ^= value >> shift;
value *= multiplier;
hash *= multiplier;
hash ^= value;
}

hash ^= hash >> 13;
hash *= multiplier;
hash ^= hash >> 15;

return hash;
}

/* PASESession,CASESession,NetworkCommissioning,GeneralCommissioning,OperationalCredentials
* are well known permitted entries.
*/
static PermitListEntry gPermitList[gPermitListSize] = {
{ MurmurHash("PASESession") },
{ MurmurHash("CASESession") },
{ MurmurHash("NetworkCommissioning") },
{ MurmurHash("GeneralCommissioning") },
{ MurmurHash("OperationalCredentials") },
};

static bool IsPermitted(size_t hashValue)
{
for (size_t i = 0; i < gPermitListSize; i++)
{
if (gPermitList[i].hash == 0)
{
break;
}
if (hashValue == gPermitList[i].hash)
{
return true;
}
}
return false;
}

} // namespace

namespace ESP32Filter {

CHIP_ERROR AddHashToPermitlist(const char * str)
{
size_t hashValue = MurmurHash(str);
for (size_t i = 0; i < gPermitListSize; i++)
{
if (gPermitList[i].hash == 0)
{
gPermitList[i].hash = hashValue;
return CHIP_NO_ERROR;
}
if (hashValue == gPermitList[i].hash)
{
return CHIP_ERROR_INCORRECT_STATE;
}
}
return CHIP_ERROR_NO_MEMORY;
}

void RemoveHashFromPermitlist(const char * str)
{
size_t hashValue = MurmurHash(str);
size_t i = 0;
while (i < gPermitListSize)
{
if (gPermitList[i].hash == 0)
{
break;
}
if (hashValue == gPermitList[i].hash)
{
for (size_t j = i; j < gPermitListSize - 1; j++)
{
gPermitList[j] = gPermitList[j + 1];
}
gPermitList[gPermitListSize - 1].hash = 0;
}
else
{
i++;
}
}
}

} // namespace ESP32Filter

#define LOG_HEAP_INFO(label, group, entry_exit) \
do \
Expand All @@ -48,12 +155,20 @@ void ESP32Backend::LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo & info) {}

void ESP32Backend::TraceBegin(const char * label, const char * group)
{
LOG_HEAP_INFO(label, group, "Entry");
size_t hashValue = MurmurHash(group);
if (IsPermitted(hashValue))
{
LOG_HEAP_INFO(label, group, "Entry");
}
}

void ESP32Backend::TraceEnd(const char * label, const char * group)
{
LOG_HEAP_INFO(label, group, "Exit");
size_t hashValue = MurmurHash(group);
if (IsPermitted(hashValue))
{
LOG_HEAP_INFO(label, group, "Exit");
}
}

void ESP32Backend::TraceInstant(const char * label, const char * group)
Expand All @@ -63,4 +178,3 @@ void ESP32Backend::TraceInstant(const char * label, const char * group)
} // namespace Insights
} // namespace Tracing
} // namespace chip
// namespace chip
9 changes: 8 additions & 1 deletion src/tracing/esp32_trace/esp32_tracing.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#include <lib/core/CHIPError.h>
#include <tracing/backend.h>

#include <memory>

namespace chip {
namespace Tracing {
namespace Insights {

// Provide the user the ability to add/remove trace filters.
namespace ESP32Filter {

CHIP_ERROR AddHashToPermitlist(const char * alabel);
void RemoveHashFromPermitlist(const char * alabel);
} // namespace ESP32Filter

/// A Backend that outputs data to chip logging.
///
/// Structured data is formatted as json strings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class Scoped
public:
inline Scoped(const char * label, const char * group) : mLabel(label), mGroup(group) { MATTER_TRACE_BEGIN(label, group); }
inline ~Scoped() { MATTER_TRACE_END(mLabel, mGroup); }

private:
const char * mLabel;
const char * mGroup;
};

} // namespace Insights
} // namespace Tracing
} // namespace chip
Expand Down

0 comments on commit 6bc181a

Please sign in to comment.