Skip to content

Commit

Permalink
Addressed review Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shripad621git committed Sep 22, 2023
1 parent 300abbb commit 191d1d0
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 119 deletions.
5 changes: 1 addition & 4 deletions src/tracing/esp32_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ source_set("esp32_trace") {
public = [ "include/matter/tracing/macros_impl.h" ]
sources = [ "include/matter/tracing/macros_impl.cpp" ]
public_configs = [ ":tracing" ]
public_deps = [
"${chip_root}/src/crypto",
"${chip_root}/src/lib/core",
]
public_deps = [ "${chip_root}/src/lib/core" ]
}
156 changes: 126 additions & 30 deletions src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,159 @@

namespace Insights {

bool ESP32Backend::isBlacklistInitialized = false;
uint64_t ESP32Backend::blacklistHashes[blacklistSize] = { 0 };
const char * ESP32Backend::blackListStrings[] = {
/* The traces of PacketParser,MinMdnsResolver and SesssionManager are filtered out :
* i) To report the traces of more concern during commissioning like CASESession,PASESession,
* traces for OperationalSessionSetup, GeneralCommissioning and NetworkCommissioning
* ii) although providing the user with ablity to add or remove the strings whose traces
* can be filtered out.
*/
namespace {
static constexpr size_t kBlackListSize = 10;
static bool isBlacklistInitialized = false;
static size_t blacklistHashes[kBlackListSize];
static const char * blackListStrings[kBlackListSize] = {
"PacketParser",
"MinMdnsResolver",
"SessionManager",
"DeviceCommissioner",
};

#define LOG_HEAP_INFO(label, group, entry_exit) \
do \
{ \
ESP_DIAG_EVENT("MTR_TRC", "%s - %s - %s Min Free heap - %u - LFB - %u Start free heap - %u", entry_exit, label, group, \
heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT), \
heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT), \
heap_caps_get_free_size(MALLOC_CAP_8BIT)); \
} while (0)
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;
}

static bool IsBlacklistedString(const char * input)
{
size_t hashValue = MurmurHash(input); // Seed is always zero

for (size_t i = 0; i < kBlackListSize; i++)
{
if (hashValue == static_cast<uint32_t>(blacklistHashes[i]))
{
return true;
}
}
return false;
}

const char * GetString(int aIndex)
{
return blackListStrings[aIndex];
}

static void InitializeBlacklistHashes()
{
for (size_t i = 0; i < kBlackListSize; i++)
{
if (blackListStrings[i] == nullptr)
{
break;
}
const char * str = blackListStrings[i];
size_t hashValue = MurmurHash(str);
blacklistHashes[i] = static_cast<uint64_t>(hashValue);
}
}
} // namespace

namespace ESP32Filter {

CHIP_ERROR AddStringToBlacklist(const char * aLabel)
{
size_t i = 0;
for (i = 0; i < kBlackListSize; i++)
{
if (blackListStrings[i] == nullptr)
{
break;
}
if (strcmp(aLabel, blackListStrings[i]) == 0)
{
return CHIP_ERROR_INCORRECT_STATE;
}
}
if (i < kBlackListSize)
{
blackListStrings[i] = aLabel;
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

void ESP32Backend::InitializeBlacklistHashes()
void RemoveStringFromBlacklist(const char * aLabel)
{
for (int i = 0; i < blacklistSize; i++)
bool found = false;
size_t i = 0;
for (i = 0; i < kBlackListSize; i++)
{
if (blackListStrings[i] == NULL)
if (blackListStrings[i] == nullptr)
{
break;
}
if (strcmp(aLabel, blackListStrings[i]) == 0)
{
found = true;
break;
size_t data_length = strlen(blackListStrings[i]);
uint8_t sha1_hash[20];
CHIP_ERROR error = chip::Crypto::Hash_SHA1((const uint8_t *) blackListStrings[i], data_length, sha1_hash);
uint64_t truncated_hash = 0;
for (int j = 0; j < 8; j++)
}
}
if (found)
{
for (size_t j = i; j < kBlackListSize - 1; j++)
{
truncated_hash <<= 8;
truncated_hash |= sha1_hash[j];
blackListStrings[j] = blackListStrings[j + 1];
}
blacklistHashes[i] = truncated_hash;
blackListStrings[kBlackListSize - 1] = nullptr;
}
}
} // namespace ESP32Filter

#define LOG_HEAP_INFO(label, group, entry_exit) \
do \
{ \
ESP_DIAG_EVENT("MTR_TRC", "%s - %s - %s Min Free heap - %u - LFB - %u Start free heap - %u", entry_exit, label, group, \
heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT), \
heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT), \
heap_caps_get_free_size(MALLOC_CAP_8BIT)); \
} while (0)

ESP32Backend::ESP32Backend(const char * str, ...)
ESP32Trace::ESP32Trace(const char * label, const char * group)
{
va_list args;
va_start(args, str);
mlabel = str;
mgroup = va_arg(args, const char *);
mlabel = label;
mgroup = group;
if (!isBlacklistInitialized)
{
InitializeBlacklistHashes();
isBlacklistInitialized = true;
}
if (!isBlacklistedString(mgroup))
if (!IsBlacklistedString(mgroup))
{
LOG_HEAP_INFO(mlabel, mgroup, "Entry");
}
}

ESP32Backend::~ESP32Backend()
ESP32Trace::~ESP32Trace()
{
if (!isBlacklistedString(mgroup))
if (!IsBlacklistedString(mgroup))
{
LOG_HEAP_INFO(mlabel, mgroup, "Exit");
}
Expand Down
111 changes: 26 additions & 85 deletions src/tracing/esp32_trace/include/matter/tracing/macros_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,106 +21,47 @@
#error "Tracing macros seem to be double defined"
#endif

#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPError.h>

namespace Insights {
class ESP32Backend
{
public:
ESP32Backend(const char * str, ...);
~ESP32Backend();

static void AddStringToBlacklist(const char * aLabel)
{
bool duplicate = false;
int i = 0;
for (i = 0; i < blacklistSize; i++)
{
if (blackListStrings[i] == NULL)
{
break;
}
if (strcmp(aLabel, blackListStrings[i]) == 0)
{
duplicate = true;
break;
}
}
if (i < blacklistSize && !duplicate)
{
blackListStrings[i] = aLabel;
}
}
static void RemoveStringFromBlacklist(const char * aLabel)
{
bool found = false;
int i = 0;
for (i = 0; i < blacklistSize; i++)
{
if (blackListStrings[i] == NULL)
{
break;
}
if (strcmp(aLabel, blackListStrings[i]) == 0)
{
found = true;
break;
}
}
if (found)
{
for (int j = i; j < blacklistSize - 1; j++)
{
blackListStrings[j] = blackListStrings[j + 1];
}
blackListStrings[blacklistSize - 1] = NULL;
}
}
/* The ESP32Filter namespace contains two apis AddStringToBlacklist and
* RemoveStringFromBlacklist to provide some flexibility to the user to add or
* remove string from the list of blacklisted strings which the user wants to
* filter out.
*/
namespace ESP32Filter {

CHIP_ERROR AddStringToBlacklist(const char * aLabel);
void RemoveStringFromBlacklist(const char * aLabel);
} // namespace ESP32Filter

static const char * GetString(int aIndex) { return blackListStrings[aIndex]; }
/* The ESP32Trace class helps to send the entry and exit traces of the scope
* macros in the code along with some trace info like largest free block,
* min free heap,start free heap ,etc.
* For ex:
* MTR_TRC: Entry - HandleSigma1_and_SendSigma2 - CASESession Min Free heap - 35640 - LFB - 22528 Start free heap - 44124
* MTR_TRC: Exit - HandleSigma1_and_SendSigma2 - CASESession Min Free heap - 35640 - LFB - 22528 Start free heap - 44216
* It does this generically for all the scoped trace macros in the code for better debugging if required and
* reports these traces to esp-insights cloud using ESP_DIAG_LOG.
*/
class ESP32Trace
{
public:
ESP32Trace(const char * label, const char * group);
~ESP32Trace();

private:
const char * mlabel;
const char * mgroup;
static constexpr int blacklistSize = 10;
static uint64_t blacklistHashes[blacklistSize];
static void InitializeBlacklistHashes();
static const char * blackListStrings[blacklistSize];
static bool isBlacklistInitialized;
bool isBlacklistedString(const char * input)
{
uint8_t sha1_hash[20];
size_t data_length = strlen(input);
CHIP_ERROR error = chip::Crypto::Hash_SHA1((const uint8_t *) input, data_length, sha1_hash);
if (error != CHIP_NO_ERROR)
{
return false;
}
uint64_t truncated_hash = 0;
for (int j = 0; j < 8; j++)
{
truncated_hash <<= 8;
truncated_hash |= sha1_hash[j];
}

for (int i = 0; i < blacklistSize; i++)
{
if (truncated_hash == blacklistHashes[i])
{
return true;
}
}
return false;
}
};

} // namespace Insights

#define MATTER_TRACE_SCOPE(...) \
#define MATTER_TRACE_SCOPE(label, group) \
do \
{ \
Insights::ESP32Backend backend(__VA_ARGS__); \
Insights::ESP32Trace trace(label, group); \
} while (0)

#define _MATTER_TRACE_DISABLE(...) \
Expand Down

0 comments on commit 191d1d0

Please sign in to comment.